|
| 1 | +import { beforeAll, describe, expect, test } from 'vitest' |
| 2 | + |
| 3 | +import { get } from '#src/tests/helpers/e2etest.js' |
| 4 | + |
| 5 | +const makeURL = (pathname: string) => `/api/article/body?${new URLSearchParams({ pathname })}` |
| 6 | + |
| 7 | +describe('article body api', () => { |
| 8 | + beforeAll(() => { |
| 9 | + // If you didn't set the `ROOT` variable, the tests will fail rather |
| 10 | + // cryptically. So as a warning for engineers running these tests, |
| 11 | + // alert in case it was accidentally forgotten. |
| 12 | + if (!process.env.ROOT) { |
| 13 | + console.warn( |
| 14 | + 'WARNING: The articlebody tests require the ROOT environment variable to be set to the fixture root', |
| 15 | + ) |
| 16 | + } |
| 17 | + // Ditto for fixture-based translations to work |
| 18 | + if (!process.env.TRANSLATIONS_FIXTURE_ROOT) { |
| 19 | + console.warn( |
| 20 | + 'WARNING: The articlebody tests require the TRANSLATIONS_FIXTURE_ROOT environment variable to be set', |
| 21 | + ) |
| 22 | + } |
| 23 | + }) |
| 24 | + |
| 25 | + test('happy path', async () => { |
| 26 | + const res = await get(makeURL('/en/get-started/start-your-journey/api-article-body-test-page')) |
| 27 | + expect(res.statusCode).toBe(200) |
| 28 | + expect(res.headers['content-type']).toContain('text/markdown') |
| 29 | + expect(res.body).toContain('## About GitHub') |
| 30 | + expect(res.body).toContain('## About Git') |
| 31 | + expect(res.body).toMatch(/^#+\s+\w+/m) // Check for any markdown heading pattern |
| 32 | + |
| 33 | + expect(res.headers['set-cookie']).toBeUndefined() |
| 34 | + expect(res.headers['cache-control']).toContain('public') |
| 35 | + expect(res.headers['cache-control']).toMatch(/max-age=[1-9]/) |
| 36 | + expect(res.headers['surrogate-control']).toContain('public') |
| 37 | + expect(res.headers['surrogate-control']).toMatch(/max-age=[1-9]/) |
| 38 | + }) |
| 39 | + |
| 40 | + test('a pathname that does not exist', async () => { |
| 41 | + const res = await get(makeURL('/en/never/heard/of')) |
| 42 | + expect(res.statusCode).toBe(404) |
| 43 | + const { error } = JSON.parse(res.body) |
| 44 | + expect(error).toBe("No page found for '/en/never/heard/of'") |
| 45 | + }) |
| 46 | + |
| 47 | + test("no 'pathname' query string at all", async () => { |
| 48 | + const res = await get('/api/article/body') |
| 49 | + expect(res.statusCode).toBe(400) |
| 50 | + const { error } = JSON.parse(res.body) |
| 51 | + expect(error).toBe("No 'pathname' query") |
| 52 | + }) |
| 53 | + |
| 54 | + test('has proper markdown structure with frontmatter removed', async () => { |
| 55 | + const res = await get(makeURL('/en/get-started/start-your-journey/api-article-body-test-page')) |
| 56 | + |
| 57 | + expect(res.statusCode).toBe(200) |
| 58 | + // Should not contain frontmatter |
| 59 | + expect(res.body).not.toMatch(/^---/) |
| 60 | + // Should have at least one heading |
| 61 | + expect(res.body).toMatch(/^#{1,6}\s+\w+/m) |
| 62 | + }) |
| 63 | + |
| 64 | + test("empty 'pathname' query string", async () => { |
| 65 | + const res = await get('/api/article/body?pathname=%20') |
| 66 | + expect(res.statusCode).toBe(400) |
| 67 | + const { error } = JSON.parse(res.body) |
| 68 | + expect(error).toBe("'pathname' query empty") |
| 69 | + }) |
| 70 | + |
| 71 | + test('repeated pathname query string key', async () => { |
| 72 | + const res = await get('/api/article/body?pathname=a&pathname=b') |
| 73 | + expect(res.statusCode).toBe(400) |
| 74 | + const { error } = JSON.parse(res.body) |
| 75 | + expect(error).toBe("Multiple 'pathname' keys") |
| 76 | + }) |
| 77 | +}) |
0 commit comments