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([]) }) })