# Instructions

- Following Playwright test failed.
- Explain why, be concise, respect Playwright best practices.
- Provide a snippet of code with the fix, if possible.

# Test info

- Name: tests/ui/entities.spec.ts >> district profile - dynamic candidates >> district profile candidates sorted by total contributions descending
- Location: tests/ui/entities.spec.ts:330:7

# Error details

```
TimeoutError: page.waitForResponse: Timeout 15000ms exceeded while waiting for event "response"
```

# Page snapshot

```yaml
- generic [active] [ref=e1]:
  - heading "503 Service Temporarily Unavailable" [level=1] [ref=e3]
  - separator [ref=e4]
  - generic [ref=e5]: nginx/1.31.8
```

# Test source

```ts
  231 |         return meta.total_contributions || 0;
  232 |       } catch { return 0; }
  233 |     });
  234 | 
  235 |     for (let i = 1; i < contributions.length; i++) {
  236 |       expect(contributions[i - 1]).toBeGreaterThanOrEqual(contributions[i]);
  237 |     }
  238 |   });
  239 | 
  240 |   test('office profile renders candidate results in UI', async ({ page }) => {
  241 |     await page.goto(PRESIDENT_PATH);
  242 | 
  243 |     // Wait for candidate results to render
  244 |     await expect(page.locator('.entity-search-result').first()).toBeVisible({ timeout: DATA_LOAD_TIMEOUT });
  245 | 
  246 |     const results = await page.locator('.entity-search-result').all();
  247 |     expect(results.length).toBeGreaterThan(0);
  248 | 
  249 |     // First result should be a well-known presidential candidate with high contributions
  250 |     const firstResultText = await results[0].textContent();
  251 |     expect(firstResultText).toMatch(/\$/); // Should show dollar amount
  252 |   });
  253 | });
  254 | 
  255 | test.describe('state profile - dynamic candidates', () => {
  256 |   const TEXAS_PATH = '/states/tx/';
  257 | 
  258 |   test('state profile loads candidates filtered to state', async ({ page }) => {
  259 |     const responsePromise = page.waitForResponse(resp => {
  260 |       if (!resp.url().includes('/api/graphql')) return false;
  261 |       if (resp.request().method() !== 'POST') return false;
  262 |       const body = resp.request().postData() || '';
  263 |       return body.includes('browseEntities');
  264 |     }, { timeout: DATA_LOAD_TIMEOUT });
  265 | 
  266 |     await page.goto(TEXAS_PATH);
  267 |     const response = await responsePromise;
  268 | 
  269 |     const requestBody = JSON.parse(response.request().postData() || '{}');
  270 |     expect(requestBody.variables?.filter).toContain('office_state:TX');
  271 | 
  272 |     const json = await response.json();
  273 |     const results = json.data?.browseEntities?.results || [];
  274 |     expect(results.length).toBeGreaterThan(0);
  275 |   });
  276 | 
  277 |   test('state profile candidates sorted by total contributions descending', async ({ page }) => {
  278 |     const responsePromise = page.waitForResponse(resp => {
  279 |       if (!resp.url().includes('/api/graphql')) return false;
  280 |       if (resp.request().method() !== 'POST') return false;
  281 |       const body = resp.request().postData() || '';
  282 |       return body.includes('browseEntities');
  283 |     }, { timeout: DATA_LOAD_TIMEOUT });
  284 | 
  285 |     await page.goto(TEXAS_PATH);
  286 |     const response = await responsePromise;
  287 | 
  288 |     const json = await response.json();
  289 |     const results = json.data?.browseEntities?.results || [];
  290 |     expect(results.length).toBeGreaterThan(1);
  291 | 
  292 |     const contributions = results.map((r: { metadata?: string }) => {
  293 |       try {
  294 |         const meta = JSON.parse(r.metadata || '{}');
  295 |         return meta.total_contributions || 0;
  296 |       } catch { return 0; }
  297 |     });
  298 | 
  299 |     for (let i = 1; i < contributions.length; i++) {
  300 |       expect(contributions[i - 1]).toBeGreaterThanOrEqual(contributions[i]);
  301 |     }
  302 |   });
  303 | });
  304 | 
  305 | test.describe('district profile - dynamic candidates', () => {
  306 |   const DISTRICT_PATH = '/districts/tx-10/';
  307 | 
  308 |   test('district profile loads candidates filtered to district', async ({ page }) => {
  309 |     const responsePromise = page.waitForResponse(resp => {
  310 |       if (!resp.url().includes('/api/graphql')) return false;
  311 |       if (resp.request().method() !== 'POST') return false;
  312 |       const body = resp.request().postData() || '';
  313 |       return body.includes('browseEntities');
  314 |     }, { timeout: DATA_LOAD_TIMEOUT });
  315 | 
  316 |     await page.goto(DISTRICT_PATH);
  317 |     const response = await responsePromise;
  318 | 
  319 |     const requestBody = JSON.parse(response.request().postData() || '{}');
  320 |     const filter = requestBody.variables?.filter || '';
  321 |     expect(filter).toContain('office:H');
  322 |     expect(filter).toContain('office_state:TX');
  323 |     expect(filter).toContain('office_district:10');
  324 | 
  325 |     const json = await response.json();
  326 |     const results = json.data?.browseEntities?.results || [];
  327 |     expect(results.length).toBeGreaterThan(0);
  328 |   });
  329 | 
  330 |   test('district profile candidates sorted by total contributions descending', async ({ page }) => {
> 331 |     const responsePromise = page.waitForResponse(resp => {
      |                                  ^ TimeoutError: page.waitForResponse: Timeout 15000ms exceeded while waiting for event "response"
  332 |       if (!resp.url().includes('/api/graphql')) return false;
  333 |       if (resp.request().method() !== 'POST') return false;
  334 |       const body = resp.request().postData() || '';
  335 |       return body.includes('browseEntities');
  336 |     }, { timeout: DATA_LOAD_TIMEOUT });
  337 | 
  338 |     await page.goto(DISTRICT_PATH);
  339 |     const response = await responsePromise;
  340 | 
  341 |     const json = await response.json();
  342 |     const results = json.data?.browseEntities?.results || [];
  343 |     expect(results.length).toBeGreaterThan(1);
  344 | 
  345 |     const contributions = results.map((r: { metadata?: string }) => {
  346 |       try {
  347 |         const meta = JSON.parse(r.metadata || '{}');
  348 |         return meta.total_contributions || 0;
  349 |       } catch { return 0; }
  350 |     });
  351 | 
  352 |     for (let i = 1; i < contributions.length; i++) {
  353 |       expect(contributions[i - 1]).toBeGreaterThanOrEqual(contributions[i]);
  354 |     }
  355 |   });
  356 | });
  357 | 
  358 | // Helper to intercept browseEntities GraphQL response and extract score values
  359 | async function getBrowseScores(page: import('@playwright/test').Page, url: string): Promise<number[]> {
  360 |   const responsePromise = page.waitForResponse(resp => {
  361 |     if (!resp.url().includes('/api/graphql')) return false;
  362 |     if (resp.request().method() !== 'POST') return false;
  363 |     const body = resp.request().postData() || '';
  364 |     return body.includes('browseEntities');
  365 |   }, { timeout: DATA_LOAD_TIMEOUT });
  366 |   await page.goto(url);
  367 |   const response = await responsePromise;
  368 |   const json = await response.json();
  369 |   const results = json.data?.browseEntities?.results || [];
  370 |   return results.map((r: { score?: number }) => r.score || 0);
  371 | }
  372 | 
  373 | test.describe('entity browse sort order - quantitative fields', () => {
  374 | 
  375 |   test('candidates sorted by total contributions descending', async ({ page }) => {
  376 |     const scores = await getBrowseScores(page, '/candidates/');
  377 |     expect(scores.length).toBeGreaterThan(1);
  378 |     for (let i = 1; i < scores.length; i++) {
  379 |       expect(scores[i - 1]).toBeGreaterThanOrEqual(scores[i]);
  380 |     }
  381 |   });
  382 | 
  383 |   test('committees sorted by total receipts descending', async ({ page }) => {
  384 |     const scores = await getBrowseScores(page, '/committees/');
  385 |     expect(scores.length).toBeGreaterThan(1);
  386 |     for (let i = 1; i < scores.length; i++) {
  387 |       expect(scores[i - 1]).toBeGreaterThanOrEqual(scores[i]);
  388 |     }
  389 |   });
  390 | 
  391 |   test('vendors sorted by total received descending', async ({ page }) => {
  392 |     const scores = await getBrowseScores(page, '/vendors/');
  393 |     expect(scores.length).toBeGreaterThan(1);
  394 |     for (let i = 1; i < scores.length; i++) {
  395 |       expect(scores[i - 1]).toBeGreaterThanOrEqual(scores[i]);
  396 |     }
  397 |   });
  398 | 
  399 |   test('individuals sorted by total contributions descending', async ({ page, request, baseURL }) => {
  400 |     await skipIfPremiumGated(request, '/individuals/');
  401 |     await premiumVisitor(page.context(), baseURL); // donor records are premium (sites#277)
  402 |     const scores = await getBrowseScores(page, '/individuals/');
  403 |     expect(scores.length).toBeGreaterThan(1);
  404 |     // A stale search index scores every donor 0 and the order means nothing.
  405 |     expect(scores[0], 'top individual donor should have contributions').toBeGreaterThan(0);
  406 |     for (let i = 1; i < scores.length; i++) {
  407 |       expect(scores[i - 1]).toBeGreaterThanOrEqual(scores[i]);
  408 |     }
  409 |   });
  410 | 
  411 |   test('employers sorted by total employee contributions descending', async ({ page }) => {
  412 |     const scores = await getBrowseScores(page, '/employers/');
  413 |     expect(scores.length).toBeGreaterThan(1);
  414 |     for (let i = 1; i < scores.length; i++) {
  415 |       expect(scores[i - 1]).toBeGreaterThanOrEqual(scores[i]);
  416 |     }
  417 |   });
  418 | });
  419 | 
  420 | test.describe('us.elect.info entity sections - additional index + detail', () => {
  421 | 
  422 |   test('employers section - index and random pages', async ({ page }) => {
  423 |     const indexResponse = await page.goto('/employers/');
  424 |     expect(indexResponse?.status(), '/employers/ should answer 200').toBe(200);
  425 |     await expect(page.locator('h1')).toContainText(/Employers/i);
  426 | 
  427 |     await expect(page.locator('.entity-search-result').first()).toBeVisible({ timeout: DATA_LOAD_TIMEOUT });
  428 | 
  429 |     const links = await page.locator('.entity-search-result').all();
  430 |     expect(links.length).toBeGreaterThan(0);
  431 | 
```