import assert from 'node:assert/strict'
import { createServer, type Server } from 'node:http'
import type { AddressInfo } from 'node:net'
import test from 'node:test'
import { onlyProductionPages, sitemapGuidePaths, splitByProduction } from './production-pages.ts'
test('reads guide paths from the sitemap, including tags the formatter wrapped', () => {
const xml = `
https://supabase.com/docs/guides/ai
https://supabase.com/docs/guides/integrations/build-a-supabase-oauth-integration/oauth-scopes
https://supabase.com/docs/guides/auth/
https://supabase.com/docs/reference/javascript/select
not a url
`
assert.deepEqual(
[...sitemapGuidePaths(xml)],
[
'/docs/guides/ai',
'/docs/guides/integrations/build-a-supabase-oauth-integration/oauth-scopes',
'/docs/guides/auth',
]
)
})
test('skips a page that is on master but not yet on production', () => {
const pages = Array.from({ length: 20 }, (_, index) => `/docs/guides/page-${index}`)
const production = new Set(pages.slice(1))
assert.deepEqual(splitByProduction(pages, production), {
live: pages.slice(1),
notOnProduction: ['/docs/guides/page-0'],
})
})
test('fails when too many pages are missing for the sitemap to be complete', () => {
const pages = Array.from({ length: 20 }, (_, index) => `/docs/guides/page-${index}`)
const production = new Set(pages.slice(3))
assert.throws(() => splitByProduction(pages, production), /3 of 20 guides are missing/)
})
const guidePages = Array.from({ length: 20 }, (_, index) => `/docs/guides/page-${index}`)
const sitemapOf = (pages: string[]) =>
`${pages.map((page) => `https://supabase.com${page}`).join('')}`
// Answers every request with the same response, and returns a sitemap URL on that server
async function serveSitemap(
status: number,
body: string
): Promise<{ url: string; server: Server }> {
const server = createServer((_request, response) => {
response.writeHead(status, { 'content-type': 'application/xml' })
response.end(body)
})
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve))
const { port } = server.address() as AddressInfo
return { url: `http://127.0.0.1:${port}/docs/sitemap.xml`, server }
}
test('keeps the guides in the sitemap and returns the ones it skips', async (t) => {
const { url, server } = await serveSitemap(200, sitemapOf(guidePages.slice(1)))
t.after(() => server.close())
t.mock.method(console, 'log', () => {})
assert.deepEqual(await onlyProductionPages(guidePages, url), {
live: guidePages.slice(1),
notOnProduction: ['/docs/guides/page-0'],
})
})
test('fails when the sitemap returns an error status', async (t) => {
const { url, server } = await serveSitemap(500, 'Internal Server Error')
t.after(() => server.close())
await assert.rejects(onlyProductionPages(guidePages, url), /returned 500/)
})
test('fails when the sitemap lists no guides', async (t) => {
const { url, server } = await serveSitemap(200, sitemapOf([]))
t.after(() => server.close())
await assert.rejects(onlyProductionPages(guidePages, url), /lists no docs guides/)
})
test("fails when the sitemap can't be reached", async () => {
const { url, server } = await serveSitemap(200, '')
await new Promise((resolve) => server.close(resolve))
await assert.rejects(onlyProductionPages(guidePages, url), /Couldn't read .*sitemap\.xml/)
})