# 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/routing.spec.ts >> routing: unknown addresses >> an unknown entity path answers 404
- Location: tests/ui/routing.spec.ts:103:7

# Error details

```
Error: /candidates/no-such-entity-zz/ should answer 404

expect(received).toBe(expected) // Object.is equality

Expected: 404
Received: 400
```

# Test source

```ts
  9   |  *     URL answered 200 with the JS browse page and no rel=canonical (sites#161)
  10  |  *     -- nothing sampled tracts;
  11  |  *   - the preview nginx lacked the /politicians/ -> /candidates/ rewrite the
  12  |  *     production config has (sites#162) -- only 3 random index links were followed;
  13  |  *   - unknown paths fall through to the Payload slug lookup, which answers
  14  |  *     400/500 instead of 404.
  15  |  */
  16  | 
  17  | const ALIASES = process.env.EXPECT_ALIASES === '1';
  18  | 
  19  | /** Collections whose sitemap lists canonical URLs (rundeck PagePaths Sitemaps + CyclePages). */
  20  | const SITEMAP_COLLECTIONS = ['candidates', 'committees', 'parties', 'offices', 'districts', 'states', 'elections'];
  21  | 
  22  | /** Indexes whose first results page lists entity links (.entity-search-result). */
  23  | // (/states/ lists map chips and /districts/ lists .district-result behind a state
  24  | // filter; their sitemap tests cover them)
  25  | const LINKED_INDEXES = ['/candidates/', '/committees/', '/parties/', '/offices/', '/elections/', '/employers/', '/vendors/'];
  26  | 
  27  | const locs = (xml: string) => [...xml.matchAll(/<loc>([^<]+)<\/loc>/g)].map(m => m[1]);
  28  | 
  29  | /** Canonical page URLs from a collection's sitemap, following one level of sitemap index. */
  30  | async function sitemapLocs(request: APIRequestContext, collection: string): Promise<string[]> {
  31  |   const path = `/${collection}/sitemap-${collection}.xml`;
  32  |   const res = await request.get(path);
  33  |   expect(res.status(), `${path} should answer 200`).toBe(200);
  34  |   const xml = await res.text();
  35  |   if (!xml.includes('<sitemapindex')) return locs(xml);
  36  |   const [child] = getRandomItems(locs(xml), 1, `child:${path}`);
  37  |   const childPath = toRelativePath(child);
  38  |   const childRes = await request.get(childPath);
  39  |   expect(childRes.status(), `${childPath} should answer 200`).toBe(200);
  40  |   return locs(await childRes.text());
  41  | }
  42  | 
  43  | test.describe('routing: canonical URLs per collection', () => {
  44  |   test.skip(!ALIASES, 'Set EXPECT_ALIASES=1 against a target with published page paths');
  45  | 
  46  |   for (const collection of SITEMAP_COLLECTIONS) {
  47  |     test(`${collection}: sitemap URLs serve their page with rel=canonical = the sitemap URL`, async ({ request }) => {
  48  |       // Entity pages only: a sitemap may also list the collection's index (/elections/)
  49  |       const entityLocs = (await sitemapLocs(request, collection)).filter(l => toRelativePath(l) !== `/${collection}/`);
  50  |       const sample = getRandomItems(entityLocs, 3);
  51  |       expect(sample.length, `${collection} sitemap should list URLs`).toBeGreaterThan(0);
  52  |       for (const loc of sample) {
  53  |         const path = toRelativePath(loc);
  54  |         const res = await request.get(path, { maxRedirects: 0 });
  55  |         expect(res.status(), `${path} (from the ${collection} sitemap) should answer 200`).toBe(200);
  56  |         expect(canonicalPath(await res.text()), `${path} rel=canonical`).toBe(path);
  57  |       }
  58  |     });
  59  |   }
  60  | 
  61  |   test('tract URLs resolve through the alias resolver, not a catch-all', async ({ request }) => {
  62  |     // Regression for sites#161: Travis County tract 48453000101 by GEOID
  63  |     const res = await request.get('/tracts/48453000101/', { maxRedirects: 0 });
  64  |     expect([200, 301], '/tracts/48453000101/ should answer 200 or 301').toContain(res.status());
  65  |     if (res.status() === 200) {
  66  |       expect(canonicalPath(await res.text()), '/tracts/48453000101/ rel=canonical').toMatch(/^\/tracts\/[^/]+\/$/);
  67  |     }
  68  |   });
  69  | 
  70  |   test('party badge links by FEC code redirect to the party page', async ({ request }) => {
  71  |     // us.elect.news links parties as /parties/<code>/ (sites#164)
  72  |     for (const code of ['rep', 'dem']) {
  73  |       const res = await request.get(`/parties/${code}/`, { maxRedirects: 0 });
  74  |       expect(res.status(), `/parties/${code}/ should redirect permanently`).toBe(301);
  75  |       const target = res.headers()['location'] ?? '';
  76  |       const page = await request.get(target, { maxRedirects: 0 });
  77  |       expect(page.status(), `/parties/${code}/ -> ${target} should answer 200`).toBe(200);
  78  |     }
  79  |   });
  80  | });
  81  | 
  82  | test.describe('routing: every index link resolves', () => {
  83  |   for (const index of LINKED_INDEXES) {
  84  |     test(`${index} first-page links all answer 200 or 301`, async ({ page, request }) => {
  85  |       test.setTimeout(120000);
  86  |       const res = await page.goto(index);
  87  |       expect(res?.status(), `${index} should answer 200`).toBe(200);
  88  |       await expect(page.locator('.entity-search-result').first(), `${index} should list results`).toBeVisible({ timeout: 15000 });
  89  |       const hrefs = (await page.locator('.entity-search-result').evaluateAll(els => els.map(e => e.getAttribute('href'))))
  90  |         .filter((h): h is string => !!h)
  91  |         .map(toRelativePath);
  92  |       const broken: string[] = [];
  93  |       for (const href of [...new Set(hrefs)]) {
  94  |         const r = await request.get(href, { maxRedirects: 0 });
  95  |         if (r.status() !== 200 && r.status() !== 301) broken.push(`${href} -> ${r.status()}`);
  96  |       }
  97  |       expect(broken, `${index} links that do not resolve`).toEqual([]);
  98  |     });
  99  |   }
  100 | });
  101 | 
  102 | test.describe('routing: unknown addresses', () => {
  103 |   test('an unknown entity path answers 404', async ({ request }) => {
  104 |     // Today the miss falls through to Payload /api/slug-lookup, which answers
  105 |     // 400/500. Remove test.fail once slug-lookup returns 404 (ops#514 follow-up).
  106 |     test.fail(true, 'Payload slug-lookup answers 400/500 for unknown paths');
  107 |     for (const path of ['/candidates/no-such-entity-zz/', '/tracts/no-such-tract/']) {
  108 |       const res = await request.get(path, { maxRedirects: 0 });
> 109 |       expect(res.status(), `${path} should answer 404`).toBe(404);
      |                                                         ^ Error: /candidates/no-such-entity-zz/ should answer 404
  110 |     }
  111 |   });
  112 | });
  113 | 
```