mirror of
https://github.com/djdevin/recflare.git
synced 2026-09-08 14:41:28 -07:00
142 lines
5.8 KiB
TypeScript
142 lines
5.8 KiB
TypeScript
import { env } from 'cloudflare:test'
|
|
import { exports } from 'cloudflare:workers'
|
|
import { describe, expect, test } from 'vitest'
|
|
|
|
import '../../cdn.app'
|
|
|
|
import type { Env } from '../../context'
|
|
|
|
declare module 'cloudflare:test' {
|
|
interface ProvidedEnv extends Env {}
|
|
}
|
|
|
|
const ORIGIN = 'https://example.com'
|
|
|
|
describe('cdn endpoints', () => {
|
|
test('GET / reports service status', async () => {
|
|
const res = await exports.default.fetch(`${ORIGIN}/`)
|
|
expect(res.status).toBe(200)
|
|
expect(await res.json()).toEqual({ service: 'cdn', status: 'ok' })
|
|
})
|
|
|
|
test('GET /config/LoadingScreenTipData returns the tip array', async () => {
|
|
const res = await exports.default.fetch(`${ORIGIN}/config/LoadingScreenTipData`)
|
|
expect(res.status).toBe(200)
|
|
const body = (await res.json()) as Array<{ Title: string }>
|
|
expect(Array.isArray(body)).toBe(true)
|
|
expect(body.length).toBeGreaterThan(0)
|
|
expect(body[0]).toHaveProperty('Title')
|
|
})
|
|
|
|
test('GET /sigs/:sigName 404s when the blob is absent', async () => {
|
|
const res = await exports.default.fetch(`${ORIGIN}/sigs/does-not-exist`)
|
|
expect(res.status).toBe(404)
|
|
})
|
|
|
|
test('GET /sigs/:sigName streams the blob from R2 as octet-stream', async () => {
|
|
await env.CDN_ASSETS.put('sigs/682c1283', new Uint8Array([1, 2, 3, 4]))
|
|
const res = await exports.default.fetch(`${ORIGIN}/sigs/682c1283`)
|
|
expect(res.status).toBe(200)
|
|
expect(res.headers.get('content-type')).toBe('application/octet-stream')
|
|
expect(new Uint8Array(await res.arrayBuffer())).toEqual(new Uint8Array([1, 2, 3, 4]))
|
|
})
|
|
|
|
test('GET /sigs/:sigName honors a Range request with 206', async () => {
|
|
await env.CDN_ASSETS.put('sigs/ranged', new Uint8Array([10, 11, 12, 13, 14, 15]))
|
|
const res = await exports.default.fetch(`${ORIGIN}/sigs/ranged`, {
|
|
headers: { Range: 'bytes=2-4' },
|
|
})
|
|
expect(res.status).toBe(206)
|
|
expect(res.headers.get('content-range')).toBe('bytes 2-4/6')
|
|
expect(res.headers.get('accept-ranges')).toBe('bytes')
|
|
expect(new Uint8Array(await res.arrayBuffer())).toEqual(new Uint8Array([12, 13, 14]))
|
|
})
|
|
|
|
// The other two Range forms. Both resolve to a concrete offset/length inside R2, so
|
|
// they exercise the same Content-Range math as the closed range above — which read
|
|
// every range as a suffix range and emitted `bytes NaN-NaN/6` until it was fixed.
|
|
test('GET /sigs/:sigName honors open-ended and suffix Range requests', async () => {
|
|
await env.CDN_ASSETS.put('sigs/ranged2', new Uint8Array([10, 11, 12, 13, 14, 15]))
|
|
const fetchRange = (range: string) =>
|
|
exports.default.fetch(`${ORIGIN}/sigs/ranged2`, { headers: { Range: range } })
|
|
|
|
// `bytes=4-` — from an offset to the end.
|
|
const open = await fetchRange('bytes=4-')
|
|
expect(open.status).toBe(206)
|
|
expect(open.headers.get('content-range')).toBe('bytes 4-5/6')
|
|
expect(new Uint8Array(await open.arrayBuffer())).toEqual(new Uint8Array([14, 15]))
|
|
|
|
// `bytes=-2` — the last N bytes.
|
|
const suffix = await fetchRange('bytes=-2')
|
|
expect(suffix.status).toBe(206)
|
|
expect(suffix.headers.get('content-range')).toBe('bytes 4-5/6')
|
|
expect(new Uint8Array(await suffix.arrayBuffer())).toEqual(new Uint8Array([14, 15]))
|
|
})
|
|
|
|
test('GET /room/:dataBlob streams the room blob from R2', async () => {
|
|
await env.CDN_ASSETS.put('room/94tp5zjtwz0gppp8xlv1j9l5b.room', new Uint8Array([9, 8, 7]))
|
|
const res = await exports.default.fetch(`${ORIGIN}/room/94tp5zjtwz0gppp8xlv1j9l5b.room`)
|
|
expect(res.status).toBe(200)
|
|
expect(res.headers.get('content-type')).toBe('application/octet-stream')
|
|
expect(new Uint8Array(await res.arrayBuffer())).toEqual(new Uint8Array([9, 8, 7]))
|
|
})
|
|
|
|
test('GET /room/:dataBlob 404s when the blob is absent', async () => {
|
|
const res = await exports.default.fetch(`${ORIGIN}/room/missing.room`)
|
|
expect(res.status).toBe(404)
|
|
})
|
|
|
|
test('GET /invention/:dataBlob streams the invention blob from R2', async () => {
|
|
// Date-foldered, `.inv`-suffixed — the name the storage worker generates and the
|
|
// api worker hands back as the invention's BlobName.
|
|
const name = '2026-07-12/6f1c0c3e-1b6a-4a52-9f52-0f4a1a6d2f77.inv'
|
|
await env.CDN_ASSETS.put(`invention/${name}`, new Uint8Array([1, 2, 3]))
|
|
const res = await exports.default.fetch(`${ORIGIN}/invention/${name}`)
|
|
expect(res.status).toBe(200)
|
|
expect(new Uint8Array(await res.arrayBuffer())).toEqual(new Uint8Array([1, 2, 3]))
|
|
})
|
|
|
|
test('GET /invention/:dataBlob 404s when the blob is absent', async () => {
|
|
const res = await exports.default.fetch(`${ORIGIN}/invention/missing.inv`)
|
|
expect(res.status).toBe(404)
|
|
})
|
|
|
|
test('GET /openapi.json documents every route', async () => {
|
|
const res = await exports.default.fetch(`${ORIGIN}/openapi.json`)
|
|
expect(res.status).toBe(200)
|
|
const spec = (await res.json()) as {
|
|
openapi: string
|
|
paths: Record<string, Record<string, { summary?: string }>>
|
|
}
|
|
expect(spec.openapi).toMatch(/^3\.1/)
|
|
|
|
// The spec route hides itself.
|
|
expect(spec.paths['/openapi.json']).toBeUndefined()
|
|
|
|
// Every route the worker serves is described. This is the drift guard: adding a
|
|
// route without a describeRoute() block fails here rather than silently shipping
|
|
// an incomplete spec. Hono's `:param` syntax becomes OpenAPI's `{param}`.
|
|
const documented = new Set(
|
|
Object.entries(spec.paths).flatMap(([path, ops]) =>
|
|
Object.keys(ops).map((method) => `${method.toUpperCase()} ${path}`)
|
|
)
|
|
)
|
|
expect([...documented].sort()).toEqual([
|
|
'GET /',
|
|
'GET /config/LoadingScreenTipData',
|
|
'GET /invention/{dataBlob}',
|
|
'GET /room/{dataBlob}',
|
|
'GET /sigs/{sigName}',
|
|
])
|
|
|
|
// Every operation carries a summary — a path present but undescribed is not
|
|
// documentation.
|
|
for (const ops of Object.values(spec.paths)) {
|
|
for (const op of Object.values(ops)) expect(op.summary).toBeTruthy()
|
|
}
|
|
|
|
// Schemas must inline: a `$ref` here is a dangling reference (see openapi.ts).
|
|
expect(JSON.stringify(spec).includes('"$ref"')).toBe(false)
|
|
})
|
|
})
|