96 lines
5.2 KiB
TypeScript
96 lines
5.2 KiB
TypeScript
// The library's API handlers, run against stand-ins for Nitro's request
|
|
// helpers and an in-memory store: publish, update, list, read, remove.
|
|
import { beforeAll, describe, expect, it } from 'vitest'
|
|
import { libraryBundle } from '../app/lib/logic/library'
|
|
import { createProject } from '../app/lib/logic/ops'
|
|
|
|
interface FakeEvent {
|
|
body?: string
|
|
params?: Record<string, string>
|
|
headers?: Record<string, string>
|
|
query?: Record<string, string>
|
|
status?: number
|
|
}
|
|
type Handler = (e: FakeEvent) => Promise<Record<string, unknown>>
|
|
|
|
const stores = new Map<string, Map<string, unknown>>()
|
|
const g = globalThis as Record<string, unknown>
|
|
const handlers: Record<string, Handler> = {}
|
|
|
|
beforeAll(async () => {
|
|
Object.assign(g, {
|
|
defineEventHandler: (fn: Handler) => fn,
|
|
createError: ({ statusCode, message }: { statusCode: number; message: string }) => Object.assign(new Error(message), { statusCode }),
|
|
readRawBody: async (e: FakeEvent) => e.body,
|
|
readBody: async (e: FakeEvent) => JSON.parse(e.body ?? 'null'),
|
|
getRouterParam: (e: FakeEvent, k: string) => e.params?.[k],
|
|
getHeader: (e: FakeEvent, k: string) => e.headers?.[k],
|
|
getQuery: (e: FakeEvent) => e.query ?? {},
|
|
getRequestIP: () => '127.0.0.1',
|
|
setHeader: () => {},
|
|
setResponseStatus: (e: FakeEvent, s: number) => (e.status = s),
|
|
useStorage: (name: string) => {
|
|
if (!stores.has(name)) stores.set(name, new Map())
|
|
const m = stores.get(name)!
|
|
return {
|
|
getItem: async (k: string) => structuredClone(m.get(k) ?? null),
|
|
setItem: async (k: string, v: unknown) => void m.set(k, structuredClone(v)),
|
|
hasItem: async (k: string) => m.has(k),
|
|
removeItem: async (k: string) => void m.delete(k),
|
|
}
|
|
},
|
|
})
|
|
// The server's own helpers, auto-imported there.
|
|
for (const mod of [await import('../server/utils/shares'), await import('../server/utils/puzzles'), await import('../server/utils/library')]) Object.assign(g, mod)
|
|
handlers.post = (await import('../server/api/library/index.post')).default as unknown as Handler
|
|
handlers.list = (await import('../server/api/library/index.get')).default as unknown as Handler
|
|
handlers.get = (await import('../server/api/library/[id].get')).default as unknown as Handler
|
|
handlers.put = (await import('../server/api/library/[id].put')).default as unknown as Handler
|
|
handlers.delete = (await import('../server/api/library/[id].delete')).default as unknown as Handler
|
|
handlers.name = (await import('../server/api/players/name.post')).default as unknown as Handler
|
|
})
|
|
|
|
const ADA = 'player-ada-000001'
|
|
const call = (h: string, e: FakeEvent) => handlers[h]!(e)
|
|
|
|
describe('library API', () => {
|
|
it('publishes, lists, updates and removes an entry; only the key holder may change it', async () => {
|
|
const p = createProject({ examples: true })
|
|
await call('name', { body: JSON.stringify({ player: ADA, name: 'Ada' }) })
|
|
const made = await call('post', { body: JSON.stringify({ player: ADA, ...libraryBundle(p, 'ex-fa') }) })
|
|
expect(made).toMatchObject({ version: 1 })
|
|
const id = made.id as string
|
|
const key = made.key as string
|
|
|
|
const list = await call('list', { query: { q: 'adder ada' } })
|
|
expect(list.total).toBe(1)
|
|
const [entry] = list.entries as Record<string, unknown>[]
|
|
// Two half adders (XOR of 4 NANDs, AND of 2) and an OR of 3: 15 NANDs.
|
|
expect(entry).toMatchObject({ id, name: 'Full Adder', author: 'Ada', transistors: 60, inputs: ['A', 'B', 'Cin'], outputs: ['S', 'Cout'], uses: 5, version: 1 })
|
|
expect(JSON.stringify(list)).not.toContain(ADA)
|
|
expect((await call('list', { query: { q: 'counter' } })).entries).toEqual([])
|
|
|
|
const full = await call('get', { params: { id } })
|
|
expect((full.bundle as { root: string }).root).toBe('ex-fa')
|
|
expect(await call('get', { params: { id }, query: { meta: '' } })).not.toHaveProperty('bundle')
|
|
|
|
const next = JSON.stringify({ player: ADA, ...libraryBundle(p, 'ex-fa') })
|
|
await expect(call('put', { params: { id }, headers: { 'x-edit-key': 'wrong' }, body: next })).rejects.toMatchObject({ statusCode: 403 })
|
|
expect(await call('put', { params: { id }, headers: { 'x-edit-key': key }, body: next })).toEqual({ id, version: 2 })
|
|
expect(((await call('list', {})).entries as { version: number }[])[0]!.version).toBe(2)
|
|
|
|
await expect(call('delete', { params: { id }, headers: { 'x-edit-key': 'wrong' } })).rejects.toMatchObject({ statusCode: 403 })
|
|
await call('delete', { params: { id }, headers: { 'x-edit-key': key } })
|
|
expect((await call('list', {})).total).toBe(0)
|
|
await expect(call('get', { params: { id } })).rejects.toMatchObject({ statusCode: 404 })
|
|
})
|
|
|
|
it('refuses requests without a player, broken components and oversized ones', async () => {
|
|
const p = createProject({ examples: true })
|
|
const bundle = libraryBundle(p, 'ex-ha')
|
|
await expect(call('post', { body: JSON.stringify(bundle) })).rejects.toMatchObject({ statusCode: 400 })
|
|
await expect(call('post', { body: JSON.stringify({ player: ADA, root: 'ex-ha', components: [] }) })).rejects.toMatchObject({ statusCode: 400 })
|
|
await expect(call('post', { body: JSON.stringify({ player: ADA, ...bundle, pad: 'x'.repeat(1_000_001) }) })).rejects.toMatchObject({ statusCode: 413 })
|
|
})
|
|
})
|