# 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: every index link resolves >> /elections/ first-page links all answer 200 or 301
- Location: tests/ui/routing.spec.ts:73:9

# Error details

```
Error: /elections/ links that do not resolve

expect(received).toEqual(expected) // deep equality

- Expected  -  1
+ Received  + 26

- Array []
+ Array [
+   "/elections/2026/ -> 503",
+   "/elections/2024/ -> 503",
+   "/elections/2022/ -> 503",
+   "/elections/2020/ -> 503",
+   "/elections/2018/ -> 503",
+   "/elections/2016/ -> 503",
+   "/elections/2014/ -> 503",
+   "/elections/2012/ -> 503",
+   "/elections/2010/ -> 503",
+   "/elections/2008/ -> 503",
+   "/elections/2006/ -> 503",
+   "/elections/2004/ -> 503",
+   "/elections/2002/ -> 503",
+   "/elections/2000/ -> 503",
+   "/elections/1998/ -> 503",
+   "/elections/1996/ -> 503",
+   "/elections/1994/ -> 503",
+   "/elections/1992/ -> 503",
+   "/elections/1990/ -> 503",
+   "/elections/1988/ -> 503",
+   "/elections/1986/ -> 503",
+   "/elections/1984/ -> 503",
+   "/elections/1982/ -> 503",
+   "/elections/1980/ -> 503",
+ ]
```

# Page snapshot

```yaml
- generic [active] [ref=e1]:
  - link "← Back to Home" [ref=e2] [cursor=pointer]:
    - /url: /
  - banner [ref=e3]:
    - heading "Election Cycles" [level=1] [ref=e4]
    - paragraph [ref=e5]: Federal election cycles from 1980 to present
  - generic [ref=e6]:
    - generic [ref=e7]:
      - textbox "Search election cycles..." [ref=e8]
      - button "Search" [ref=e9] [cursor=pointer]
    - generic [ref=e10]:
      - heading "All Election Cycles" [level=3] [ref=e12]
      - generic [ref=e13]:
        - link "2026 Election Cycle Federal election cycle 2026" [ref=e14] [cursor=pointer]:
          - /url: /elections/2026/
          - generic [ref=e15]: 2026 Election Cycle
          - generic [ref=e16]: Federal election cycle 2026
        - link "2024 Election Cycle Federal election cycle 2024" [ref=e17] [cursor=pointer]:
          - /url: /elections/2024/
          - generic [ref=e18]: 2024 Election Cycle
          - generic [ref=e19]: Federal election cycle 2024
        - link "2022 Election Cycle Federal election cycle 2022" [ref=e20] [cursor=pointer]:
          - /url: /elections/2022/
          - generic [ref=e21]: 2022 Election Cycle
          - generic [ref=e22]: Federal election cycle 2022
        - link "2020 Election Cycle Federal election cycle 2020" [ref=e23] [cursor=pointer]:
          - /url: /elections/2020/
          - generic [ref=e24]: 2020 Election Cycle
          - generic [ref=e25]: Federal election cycle 2020
        - link "2018 Election Cycle Federal election cycle 2018" [ref=e26] [cursor=pointer]:
          - /url: /elections/2018/
          - generic [ref=e27]: 2018 Election Cycle
          - generic [ref=e28]: Federal election cycle 2018
        - link "2016 Election Cycle Federal election cycle 2016" [ref=e29] [cursor=pointer]:
          - /url: /elections/2016/
          - generic [ref=e30]: 2016 Election Cycle
          - generic [ref=e31]: Federal election cycle 2016
        - link "2014 Election Cycle Federal election cycle 2014" [ref=e32] [cursor=pointer]:
          - /url: /elections/2014/
          - generic [ref=e33]: 2014 Election Cycle
          - generic [ref=e34]: Federal election cycle 2014
        - link "2012 Election Cycle Federal election cycle 2012" [ref=e35] [cursor=pointer]:
          - /url: /elections/2012/
          - generic [ref=e36]: 2012 Election Cycle
          - generic [ref=e37]: Federal election cycle 2012
        - link "2010 Election Cycle Federal election cycle 2010" [ref=e38] [cursor=pointer]:
          - /url: /elections/2010/
          - generic [ref=e39]: 2010 Election Cycle
          - generic [ref=e40]: Federal election cycle 2010
        - link "2008 Election Cycle Federal election cycle 2008" [ref=e41] [cursor=pointer]:
          - /url: /elections/2008/
          - generic [ref=e42]: 2008 Election Cycle
          - generic [ref=e43]: Federal election cycle 2008
        - link "2006 Election Cycle Federal election cycle 2006" [ref=e44] [cursor=pointer]:
          - /url: /elections/2006/
          - generic [ref=e45]: 2006 Election Cycle
          - generic [ref=e46]: Federal election cycle 2006
        - link "2004 Election Cycle Federal election cycle 2004" [ref=e47] [cursor=pointer]:
          - /url: /elections/2004/
          - generic [ref=e48]: 2004 Election Cycle
          - generic [ref=e49]: Federal election cycle 2004
        - link "2002 Election Cycle Federal election cycle 2002" [ref=e50] [cursor=pointer]:
          - /url: /elections/2002/
          - generic [ref=e51]: 2002 Election Cycle
          - generic [ref=e52]: Federal election cycle 2002
        - link "2000 Election Cycle Federal election cycle 2000" [ref=e53] [cursor=pointer]:
          - /url: /elections/2000/
          - generic [ref=e54]: 2000 Election Cycle
          - generic [ref=e55]: Federal election cycle 2000
        - link "1998 Election Cycle Federal election cycle 1998" [ref=e56] [cursor=pointer]:
          - /url: /elections/1998/
          - generic [ref=e57]: 1998 Election Cycle
          - generic [ref=e58]: Federal election cycle 1998
        - link "1996 Election Cycle Federal election cycle 1996" [ref=e59] [cursor=pointer]:
          - /url: /elections/1996/
          - generic [ref=e60]: 1996 Election Cycle
          - generic [ref=e61]: Federal election cycle 1996
        - link "1994 Election Cycle Federal election cycle 1994" [ref=e62] [cursor=pointer]:
          - /url: /elections/1994/
          - generic [ref=e63]: 1994 Election Cycle
          - generic [ref=e64]: Federal election cycle 1994
        - link "1992 Election Cycle Federal election cycle 1992" [ref=e65] [cursor=pointer]:
          - /url: /elections/1992/
          - generic [ref=e66]: 1992 Election Cycle
          - generic [ref=e67]: Federal election cycle 1992
        - link "1990 Election Cycle Federal election cycle 1990" [ref=e68] [cursor=pointer]:
          - /url: /elections/1990/
          - generic [ref=e69]: 1990 Election Cycle
          - generic [ref=e70]: Federal election cycle 1990
        - link "1988 Election Cycle Federal election cycle 1988" [ref=e71] [cursor=pointer]:
          - /url: /elections/1988/
          - generic [ref=e72]: 1988 Election Cycle
          - generic [ref=e73]: Federal election cycle 1988
        - link "1986 Election Cycle Federal election cycle 1986" [ref=e74] [cursor=pointer]:
          - /url: /elections/1986/
          - generic [ref=e75]: 1986 Election Cycle
          - generic [ref=e76]: Federal election cycle 1986
        - link "1984 Election Cycle Federal election cycle 1984" [ref=e77] [cursor=pointer]:
          - /url: /elections/1984/
          - generic [ref=e78]: 1984 Election Cycle
          - generic [ref=e79]: Federal election cycle 1984
        - link "1982 Election Cycle Federal election cycle 1982" [ref=e80] [cursor=pointer]:
          - /url: /elections/1982/
          - generic [ref=e81]: 1982 Election Cycle
          - generic [ref=e82]: Federal election cycle 1982
        - link "1980 Election Cycle Federal election cycle 1980" [ref=e83] [cursor=pointer]:
          - /url: /elections/1980/
          - generic [ref=e84]: 1980 Election Cycle
          - generic [ref=e85]: Federal election cycle 1980
      - generic [ref=e86]: Showing all 24 results
  - main [ref=e88]
```

# Test source

```ts
  1   | import { test, expect, APIRequestContext } from '@playwright/test';
  2   | import { canonicalPath, getRandomItems, toRelativePath } from './helpers';
  3   | 
  4   | /*
  5   |  * Routing coverage across every collection (electinfo/rundeck#700, ops#514).
  6   |  *
  7   |  * What slipped past the existing specs:
  8   |  *   - the preview nginx lacked the /politicians/ -> /candidates/ rewrite the
  9   |  *     production config has (sites#162) -- only 3 random index links were followed;
  10  |  *   - unknown paths fall through to the Payload slug lookup, which answers
  11  |  *     400/500 instead of 404.
  12  |  */
  13  | 
  14  | const ALIASES = process.env.EXPECT_ALIASES === '1';
  15  | 
  16  | /** Collections whose sitemap lists canonical URLs (rundeck PagePaths Sitemaps + CyclePages). */
  17  | const SITEMAP_COLLECTIONS = ['candidates', 'committees', 'parties', 'offices', 'districts', 'states', 'elections'];
  18  | 
  19  | /** Indexes whose first results page lists entity links (.entity-search-result). */
  20  | // (/states/ lists map chips and /districts/ lists .district-result behind a state
  21  | // filter; their sitemap tests cover them)
  22  | const LINKED_INDEXES = ['/candidates/', '/committees/', '/parties/', '/offices/', '/elections/', '/employers/', '/vendors/'];
  23  | 
  24  | const locs = (xml: string) => [...xml.matchAll(/<loc>([^<]+)<\/loc>/g)].map(m => m[1]);
  25  | 
  26  | /** Canonical page URLs from a collection's sitemap, following one level of sitemap index. */
  27  | async function sitemapLocs(request: APIRequestContext, collection: string): Promise<string[]> {
  28  |   const path = `/${collection}/sitemap-${collection}.xml`;
  29  |   const res = await request.get(path);
  30  |   expect(res.status(), `${path} should answer 200`).toBe(200);
  31  |   const xml = await res.text();
  32  |   if (!xml.includes('<sitemapindex')) return locs(xml);
  33  |   const [child] = getRandomItems(locs(xml), 1, `child:${path}`);
  34  |   const childPath = toRelativePath(child);
  35  |   const childRes = await request.get(childPath);
  36  |   expect(childRes.status(), `${childPath} should answer 200`).toBe(200);
  37  |   return locs(await childRes.text());
  38  | }
  39  | 
  40  | test.describe('routing: canonical URLs per collection', () => {
  41  |   test.skip(!ALIASES, 'Set EXPECT_ALIASES=1 against a target with published page paths');
  42  | 
  43  |   for (const collection of SITEMAP_COLLECTIONS) {
  44  |     test(`${collection}: sitemap URLs serve their page with rel=canonical = the sitemap URL`, async ({ request }) => {
  45  |       // Entity pages only: a sitemap may also list the collection's index (/elections/)
  46  |       const entityLocs = (await sitemapLocs(request, collection)).filter(l => toRelativePath(l) !== `/${collection}/`);
  47  |       const sample = getRandomItems(entityLocs, 3);
  48  |       expect(sample.length, `${collection} sitemap should list URLs`).toBeGreaterThan(0);
  49  |       for (const loc of sample) {
  50  |         const path = toRelativePath(loc);
  51  |         const res = await request.get(path, { maxRedirects: 0 });
  52  |         expect(res.status(), `${path} (from the ${collection} sitemap) should answer 200`).toBe(200);
  53  |         expect(canonicalPath(await res.text()), `${path} rel=canonical`).toBe(path);
  54  |       }
  55  |     });
  56  |   }
  57  | 
  58  | 
  59  |   test('party badge links by FEC code redirect to the party page', async ({ request }) => {
  60  |     // us.elect.news links parties as /parties/<code>/ (sites#164)
  61  |     for (const code of ['rep', 'dem']) {
  62  |       const res = await request.get(`/parties/${code}/`, { maxRedirects: 0 });
  63  |       expect(res.status(), `/parties/${code}/ should redirect permanently`).toBe(301);
  64  |       const target = res.headers()['location'] ?? '';
  65  |       const page = await request.get(target, { maxRedirects: 0 });
  66  |       expect(page.status(), `/parties/${code}/ -> ${target} should answer 200`).toBe(200);
  67  |     }
  68  |   });
  69  | });
  70  | 
  71  | test.describe('routing: every index link resolves', () => {
  72  |   for (const index of LINKED_INDEXES) {
  73  |     test(`${index} first-page links all answer 200 or 301`, async ({ page, request }) => {
  74  |       test.setTimeout(120000);
  75  |       const res = await page.goto(index);
  76  |       expect(res?.status(), `${index} should answer 200`).toBe(200);
  77  |       await expect(page.locator('.entity-search-result').first(), `${index} should list results`).toBeVisible({ timeout: 15000 });
  78  |       const hrefs = (await page.locator('.entity-search-result').evaluateAll(els => els.map(e => e.getAttribute('href'))))
  79  |         .filter((h): h is string => !!h)
  80  |         .map(toRelativePath);
  81  |       const broken: string[] = [];
  82  |       for (const href of [...new Set(hrefs)]) {
  83  |         const r = await request.get(href, { maxRedirects: 0 });
  84  |         if (r.status() !== 200 && r.status() !== 301) broken.push(`${href} -> ${r.status()}`);
  85  |       }
> 86  |       expect(broken, `${index} links that do not resolve`).toEqual([]);
      |                                                            ^ Error: /elections/ links that do not resolve
  87  |     });
  88  |   }
  89  | });
  90  | 
  91  | test.describe('routing: unknown addresses', () => {
  92  |   test('an unknown entity path answers 404', async ({ request }) => {
  93  |     // A miss in /_resolve (or the API being unavailable) ends in nginx's own
  94  |     // 404. It used to fall through to Payload's slug-lookup, which answered
  95  |     // 400/500 (electinfo/sites#218). Only the alias-routed collections apply:
  96  |     // leaf collections are covered below.
  97  |     for (const path of ['/candidates/no-such-entity-zz/', '/committees/no-such-entity-zz/']) {
  98  |       const res = await request.get(path, { maxRedirects: 0 });
  99  |       expect(res.status(), `${path} should answer 404`).toBe(404);
  100 |     }
  101 |   });
  102 | 
  103 |   test('an unknown leaf entity path serves the collection shell', async ({ request }) => {
  104 |     // Leaf collections (individuals, employers, vendors, blocks, precincts,
  105 |     // tracts) render client-side from the baked collection shell (sites#209),
  106 |     // so nginx answers every subpath with that shell and the runtime reports
  107 |     // a missing entity itself. There is no server-side 404 to expect here.
  108 |     for (const col of ['individuals', 'employers', 'vendors', 'blocks', 'precincts', 'tracts']) {
  109 |       const res = await request.get(`/${col}/no-such-entity-zz/`, { maxRedirects: 0 });
  110 |       expect(res.status(), `/${col}/no-such-entity-zz/ should serve the shell`).toBe(200);
  111 |       expect(res.headers()['content-type'] ?? '', `/${col}/ shell is HTML`).toContain('text/html');
  112 |     }
  113 |   });
  114 | });
  115 | 
```