diff --git a/e2e/tests/journeys.spec.ts b/e2e/tests/journeys.spec.ts index 7a81e60..e99174d 100644 --- a/e2e/tests/journeys.spec.ts +++ b/e2e/tests/journeys.spec.ts @@ -1226,6 +1226,27 @@ const CUTOFF_CANDIDATE_URNS = [ 101099, 100553, 102574, 100769, // mixed ]; +/** + * Whether the last-distance-offered feature is switched on here. + * + * Read from the data rather than from /api/flags, which the public proxy + * denies on purpose — the endpoint names unreleased features. The observable + * effect is the field's presence: the flag is off iff no candidate school + * carries an `admission_distance` key at all. + * + * The distinction that matters: `admission_distance: null` means this school + * has no published cut-off, and the key being ABSENT means cut-offs are not + * being published at all. + */ +async function distanceFeatureIsOn(page: Page): Promise { + for (const urn of CUTOFF_CANDIDATE_URNS) { + const res = await page.request.get(`/api/schools/${urn}`); + if (!res.ok()) continue; + if ('admission_distance' in (await res.json())) return true; + } + return false; +} + async function schoolWithCutoff(page: Page) { for (const urn of CUTOFF_CANDIDATE_URNS) { const res = await page.request.get(`/api/schools/${urn}`); @@ -1237,6 +1258,59 @@ async function schoolWithCutoff(page: Page) { return null; } +test('when the distance feature is on, a school with a cut-off is findable', async ({ page }) => { + /* + * The gate that stops the other distance journeys passing vacuously. + * + * They all skip when schoolWithCutoff() finds nothing, which is right when + * the feature is off — but it means a feature that is *supposed* to be on + * and is silently broken shows up as a green run full of skips. This test + * fails in that case. + */ + test.skip(!(await distanceFeatureIsOn(page)), + 'the admission_distance flag is off in this environment'); + + expect(await schoolWithCutoff(page), + 'the distance feature is on, but no candidate school has a cut-off — ' + + 'the flag is on and the data or the query behind it is broken') + .not.toBeNull(); +}); + +test('with the distance feature off, the section is absent rather than empty', async ({ page }) => { + // Shipping dark means the page renders as it did before the feature existed, + // not as a feature with its content removed. + test.skip(await distanceFeatureIsOn(page), + 'the admission_distance flag is on in this environment'); + + // A school that exists, found rather than hardcoded — a 404 page would + // satisfy the absent-heading assertion without proving anything. + // + // A plain loop, not Array.find: find's predicate is synchronous, so an async + // one returns a Promise, every Promise is truthy, and it would always hand + // back the first URN whether or not that school exists. + let urn: number | null = null; + for (const candidate of CUTOFF_CANDIDATE_URNS) { + if ((await page.request.get(`/api/schools/${candidate}`)).ok()) { + urn = candidate; + break; + } + } + expect(urn, 'no candidate school resolves in this environment').not.toBeNull(); + + await page.goto(`/school/${urn}`); + await expect(page.locator('h1')).toBeVisible(); + + await expect(page.getByRole('heading', { name: /How far away are you\?/ })) + .toHaveCount(0); +}); + +test('/api/flags is not reachable from the public internet', async ({ page }) => { + // It names every unreleased feature and whether it is on. Next reads it + // server-side over the Docker network; the public proxy must deny it. + const res = await page.request.get('/api/flags'); + expect(res.status()).toBe(404); +}); + test('a published cut-off distance is shown with the year it belongs to', async ({ page }) => { const found = await schoolWithCutoff(page); test.skip(found === null, 'no school in the sample has a published cut-off distance yet');