Compare commits

1 Commits
v0.1.0 ... main

Author SHA1 Message Date
321bc90ddf test(17-06): FAQ layer client tests
- the encoded slug and locale, non-string descriptions and content
- useFaqApi uses the server base with the forwarded visitor on the
  server and the public base in the browser
2026-10-12 19:46:29 +02:00

View File

@@ -89,3 +89,73 @@ test('isNotFound reads status, statusCode and response.status', () => {
assert.equal(isNotFound({ status: 500 }), false)
assert.equal(isNotFound(undefined), false)
})
// Plan 06 coverage: the encoded slug and locale, non-string descriptions,
// and useFaqApi picking the base. The composable reads Nuxt globals and
// import.meta.server, so the test loads its source with import.meta.server
// replaced by a flag, next to the original (its relative import resolves),
// with stubbed globals.
import { readFileSync, rmSync, writeFileSync } from 'node:fs'
import { dirname, join } from 'node:path'
import { fileURLToPath, pathToFileURL } from 'node:url'
test('the slug is encoded, the locale passed as it is', async () => {
const { fetcher, calls } = fakeFetcher(() => FAQ)
await createFaqClient(fetcher).getFaq('a b?c#d', 'pt-BR')
assert.deepEqual(calls, [{ path: '/faqs/a%20b%3Fc%23d', query: { locale: 'pt-BR' } }])
// An empty locale is no locale.
await createFaqClient(fetcher).getFaq('x', '')
assert.deepEqual(calls[1], { path: '/faqs/x' })
})
test('a description that is not a string is null; content that is not a list is empty', async () => {
const faq = await createFaqClient(async () => ({ data: { ...FAQ.data, description: 5, content: 'x' } })).getFaq('x')
assert.equal(faq?.description, null)
assert.deepEqual(faq?.content, [])
})
type FetchCall = { path: string; opts: { baseURL: string; query?: Record<string, string>; headers?: Record<string, string> } }
let loads = 0
async function useFaqApiAs(server: boolean, request: { context?: Record<string, unknown>; xff?: string; peer?: string } = {}) {
const calls: FetchCall[] = []
const g = globalThis as Record<string, unknown>
g.__faqLayerServer = server
g.useRuntimeConfig = () => ({ faq: { apiBase: 'http://127.0.0.1:8096/_faq/api/v1' }, public: { faq: { apiBase: '/_faq/api/v1' } } })
g.useRequestEvent = () => ({ context: request.context ?? {}, node: { req: { socket: { remoteAddress: request.peer } } } })
g.useRequestHeaders = () => (request.xff === undefined ? {} : { 'x-forwarded-for': request.xff })
g.$fetch = async (path: string, opts: FetchCall['opts']) => {
calls.push({ path, opts })
return FAQ
}
const source = join(dirname(fileURLToPath(import.meta.url)), '..', 'app', 'composables', 'useFaqApi.ts')
const out = join(dirname(source), `.useFaqApi-${process.pid}-${++loads}.compiled.ts`)
writeFileSync(out, readFileSync(source, 'utf8').replaceAll('import.meta.server', '(globalThis as Record<string, unknown>).__faqLayerServer'))
try {
const mod = (await import(pathToFileURL(out).href)) as { useFaqApi: () => ReturnType<typeof createFaqClient> }
return { client: mod.useFaqApi(), calls }
} finally {
rmSync(out, { force: true })
}
}
test('the server base is used on the server, with the visitor forwarded', async () => {
const { client, calls } = await useFaqApiAs(true, { xff: '203.0.113.7', peer: '10.0.0.2' })
await client.getFaq('shipping', 'pl')
assert.deepEqual(calls, [
{ path: '/faqs/shipping', opts: { baseURL: 'http://127.0.0.1:8096/_faq/api/v1', query: { locale: 'pl' }, headers: { 'x-forwarded-for': '203.0.113.7, 10.0.0.2' } } },
])
const recorded = await useFaqApiAs(true, { context: { faqForwardedFor: '198.51.100.9' }, xff: '1.1.1.1', peer: '10.0.0.2' })
await recorded.client.getFaq('shipping')
assert.deepEqual(recorded.calls[0]!.opts.headers, { 'x-forwarded-for': '198.51.100.9' })
const bare = await useFaqApiAs(true)
await bare.client.getFaq('shipping')
assert.deepEqual(bare.calls[0]!.opts, { baseURL: 'http://127.0.0.1:8096/_faq/api/v1', query: undefined, headers: {} })
})
test('the public base is used in the browser, without forwarded headers', async () => {
const { client, calls } = await useFaqApiAs(false, { xff: '203.0.113.7', peer: '10.0.0.2' })
await client.getFaq('shipping', 'en')
assert.deepEqual(calls, [{ path: '/faqs/shipping', opts: { baseURL: '/_faq/api/v1', query: { locale: 'en' } } }])
})