Files
faunosteo/packages/supabase/tests/anatomical-sheets.test.ts
T
bchanot 6891f9ea25 feat(supabase): publish gate on the Rust core's sheet.check, fail closed
publishSheetVersion(client, versionId, validate) takes the core's
sheet.check (the package never loads the module itself); anything but a
publishable verdict refuses publication before the RPC: invalid_shape,
not_publishable (same message as before: first error code), and
validator_unavailable when the core cannot run. Tests run on the real
Node core; the shape case now asserts the closed kind instead of any
throw. The package no longer depends on @faunosteo/domain.
2026-10-14 00:29:03 +02:00

103 lines
3.3 KiB
TypeScript

import { nodeCoreRuntime } from '@faunosteo/core-wasm/node'
import type { CoreRuntime } from '@faunosteo/core-wasm'
import { publishSheetVersion, SheetPublishError } from '../src'
import type { FaunosteoClient, SheetCheckOutcome, SheetValidator } from '../src'
/** The real core's `sheet.check` (R2): the gate is never tested against a stub verdict. */
let core: CoreRuntime
beforeAll(async () => {
core = await nodeCoreRuntime()
})
const validate: SheetValidator = (content) =>
core.call('sheet.check', JSON.stringify(content)) as SheetCheckOutcome
/**
* Stub of the two client calls publishSheetVersion makes. The real RPC is
* covered by supabase/tests/005_anatomical_sheets.sql; here the point is the
* order of the gate: no RPC call unless the document is publishable.
*/
function stubClient(content: unknown) {
const rpcCalls: unknown[] = []
const client = {
from: () => ({
select: () => ({
eq: () => ({
single: async () => ({ data: { content }, error: null }),
}),
}),
}),
rpc: async (name: string, args: unknown) => {
rpcCalls.push([name, args])
return { data: 'published-id', error: null }
},
}
return { client: client as unknown as FaunosteoClient, rpcCalls }
}
const publishable = {
schema_version: 2,
species_id: 'equine',
name: { fr: 'Test', en: 'Test' },
description: {},
scope_disclaimer: {},
attribution: { authors: [], license: null, notes: null },
vertebral_config: { regions: [] },
references: [],
categories: [],
views: [
{
id: 'view-main',
kind: 'main',
name: { fr: 'Vue', en: 'View' },
svg_asset_id: '11111111-1111-4111-8111-111111111111',
view_box: null,
},
],
annotations: [],
finding_zones: [],
}
describe('publishSheetVersion', () => {
it('refuses publication when the validator cannot run (fail closed)', async () => {
const { client, rpcCalls } = stubClient(publishable)
const unavailable: SheetValidator = () => ({
ok: false,
kind: 'core_unavailable',
})
await expect(
publishSheetVersion(client, 'v1', unavailable),
).rejects.toMatchObject({ kind: 'validator_unavailable' })
expect(rpcCalls).toEqual([])
})
it('calls the RPC once the document passes shape and integrity checks', async () => {
const { client, rpcCalls } = stubClient(publishable)
await expect(publishSheetVersion(client, 'v1', validate)).resolves.toBe(
'published-id',
)
expect(rpcCalls).toEqual([['publish_sheet_version', { version_id: 'v1' }]])
})
it('refuses a document with integrity errors before any RPC', async () => {
const { client, rpcCalls } = stubClient({
...publishable,
views: [{ ...publishable.views[0], svg_asset_id: null }],
})
await expect(publishSheetVersion(client, 'v1', validate)).rejects.toThrow(
/view\.asset\.missing/,
)
expect(rpcCalls).toEqual([])
})
it('refuses a document that does not match the schema', async () => {
const { client, rpcCalls } = stubClient({ schema_version: 1 })
await expect(
publishSheetVersion(client, 'v1', validate),
).rejects.toBeInstanceOf(SheetPublishError)
await expect(
publishSheetVersion(client, 'v1', validate),
).rejects.toMatchObject({ kind: 'invalid_shape' })
expect(rpcCalls).toEqual([])
})
})