From 6891f9ea25d36c869e14b37367b8b6b342d41d4b Mon Sep 17 00:00:00 2001 From: bchanot Date: Wed, 14 Oct 2026 00:29:03 +0200 Subject: [PATCH] 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. --- packages/supabase/package.json | 2 +- packages/supabase/src/anatomical-sheets.ts | 81 +++++++++++++++++-- packages/supabase/src/index.ts | 9 ++- .../supabase/tests/anatomical-sheets.test.ts | 37 +++++++-- packages/supabase/vitest.config.ts | 2 + pnpm-lock.yaml | 6 +- 6 files changed, 119 insertions(+), 18 deletions(-) diff --git a/packages/supabase/package.json b/packages/supabase/package.json index ca830be9..38e69cd3 100644 --- a/packages/supabase/package.json +++ b/packages/supabase/package.json @@ -21,12 +21,12 @@ "assets:upload": "node scripts/upload-sheet-assets.ts" }, "dependencies": { - "@faunosteo/domain": "workspace:*", "@supabase/supabase-js": "^2.100.0", "zod": "^3.24.1" }, "devDependencies": { "@electric-sql/pglite": "^0.3.0", + "@faunosteo/core-wasm": "workspace:*", "@types/node": "^20.19.41", "eslint": "^9.17.0", "globals": "^15.14.0", diff --git a/packages/supabase/src/anatomical-sheets.ts b/packages/supabase/src/anatomical-sheets.ts index 373979c6..dc586bc8 100644 --- a/packages/supabase/src/anatomical-sheets.ts +++ b/packages/supabase/src/anatomical-sheets.ts @@ -1,18 +1,83 @@ -import { - AnatomicalSheetDocumentSchema, - assertPublishable, -} from '@faunosteo/domain' import type { FaunosteoClient } from './client' /** - * Publication gate on the client side: the full Zod shape and every - * integrity rule run before the RPC, so the editor gets the complete issue - * list. The database re-checks the referential rules on its own + * Publication gate on the client side: the full shape and every integrity + * rule run before the RPC, so the editor gets the complete issue list. The + * rules run in the Rust core (R2): the caller passes its `sheet.check` + * operation, so this package never loads the module itself. The gate fails + * closed: anything but a publishable verdict refuses publication. The + * database re-checks the referential rules on its own * (private.sheet_document_issues); this call never replaces that guard. */ + +export interface SheetIssue { + code: string + severity: 'error' | 'warning' + path: string + message: string +} + +/** The core's verdict on a document (`sheet.check`), or why it could not give one. */ +export type SheetCheckOutcome = + | { ok: true; value: { issues: SheetIssue[]; publishable: boolean } } + | { ok: false; kind: string; detail?: string } + +export type SheetValidator = (content: unknown) => SheetCheckOutcome + +export type SheetPublishErrorKind = + /** The document does not match the strict schema. */ + | 'invalid_shape' + /** Integrity errors (warnings never block). */ + | 'not_publishable' + /** The validator could not run (core unavailable, trap): publication refused. */ + | 'validator_unavailable' + +export class SheetPublishError extends Error { + constructor( + readonly kind: SheetPublishErrorKind, + readonly issues: SheetIssue[] = [], + detail = '', + ) { + super(SheetPublishError.messageOf(kind, issues, detail)) + this.name = 'SheetPublishError' + } + + private static messageOf( + kind: SheetPublishErrorKind, + issues: SheetIssue[], + detail: string, + ): string { + if (kind === 'not_publishable') { + const errors = issues.filter((issue) => issue.severity === 'error') + return ( + `Sheet document is not publishable: ${errors.length} error(s), ` + + `first: ${errors[0]?.code ?? 'none'}` + ) + } + if (kind === 'invalid_shape') { + return `Sheet document does not match the schema${detail ? `: ${detail}` : ''}` + } + return `Sheet validator unavailable${detail ? ` (${detail})` : ''}` + } +} + +/** Throws unless the validator says the document is publishable. */ +function assertPublishable(validate: SheetValidator, content: unknown): void { + const check = validate(content) + if (!check.ok) { + throw check.kind === 'invalid_shape' + ? new SheetPublishError('invalid_shape', [], check.detail) + : new SheetPublishError('validator_unavailable', [], check.kind) + } + if (!check.value.publishable) { + throw new SheetPublishError('not_publishable', check.value.issues) + } +} + export async function publishSheetVersion( client: FaunosteoClient, versionId: string, + validate: SheetValidator, ): Promise { const draft = await client .from('anatomical_sheet_versions') @@ -21,7 +86,7 @@ export async function publishSheetVersion( .single() if (draft.error) throw draft.error - assertPublishable(AnatomicalSheetDocumentSchema.parse(draft.data.content)) + assertPublishable(validate, draft.data.content) const published = await client.rpc('publish_sheet_version', { version_id: versionId, diff --git a/packages/supabase/src/index.ts b/packages/supabase/src/index.ts index 0523ef9c..501f1149 100644 --- a/packages/supabase/src/index.ts +++ b/packages/supabase/src/index.ts @@ -43,7 +43,14 @@ export { } from './profiles' /** owner | admin | moderator | support. Not a professional status. */ export { fetchOwnPlatformRoles, type PlatformRole } from './platform-roles' -export { publishSheetVersion } from './anatomical-sheets' +export { + publishSheetVersion, + SheetPublishError, + type SheetCheckOutcome, + type SheetIssue, + type SheetPublishErrorKind, + type SheetValidator, +} from './anatomical-sheets' export { ASSET_FAILURE_KINDS, ASSET_OWNER_COLUMNS, diff --git a/packages/supabase/tests/anatomical-sheets.test.ts b/packages/supabase/tests/anatomical-sheets.test.ts index 413f9abb..e472f498 100644 --- a/packages/supabase/tests/anatomical-sheets.test.ts +++ b/packages/supabase/tests/anatomical-sheets.test.ts @@ -1,5 +1,15 @@ -import { publishSheetVersion } from '../src' -import type { FaunosteoClient } from '../src' +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 @@ -48,9 +58,21 @@ const publishable = { } 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')).resolves.toBe( + await expect(publishSheetVersion(client, 'v1', validate)).resolves.toBe( 'published-id', ) expect(rpcCalls).toEqual([['publish_sheet_version', { version_id: 'v1' }]]) @@ -61,7 +83,7 @@ describe('publishSheetVersion', () => { ...publishable, views: [{ ...publishable.views[0], svg_asset_id: null }], }) - await expect(publishSheetVersion(client, 'v1')).rejects.toThrow( + await expect(publishSheetVersion(client, 'v1', validate)).rejects.toThrow( /view\.asset\.missing/, ) expect(rpcCalls).toEqual([]) @@ -69,7 +91,12 @@ describe('publishSheetVersion', () => { it('refuses a document that does not match the schema', async () => { const { client, rpcCalls } = stubClient({ schema_version: 1 }) - await expect(publishSheetVersion(client, 'v1')).rejects.toThrow() + await expect( + publishSheetVersion(client, 'v1', validate), + ).rejects.toBeInstanceOf(SheetPublishError) + await expect( + publishSheetVersion(client, 'v1', validate), + ).rejects.toMatchObject({ kind: 'invalid_shape' }) expect(rpcCalls).toEqual([]) }) }) diff --git a/packages/supabase/vitest.config.ts b/packages/supabase/vitest.config.ts index 91ab39c3..c4b56b84 100644 --- a/packages/supabase/vitest.config.ts +++ b/packages/supabase/vitest.config.ts @@ -5,6 +5,8 @@ export default defineConfig({ environment: 'node', globals: true, include: ['tests/**/*.test.ts'], + // The publish gate runs on the real core: refuse a missing or stale module. + globalSetup: ['../core-wasm/tests/global-setup.ts'], // PGlite boots a WASM Postgres per file: give it room on slow CI runners. testTimeout: 60_000, hookTimeout: 120_000, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 68c21f90..9b876b99 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -244,9 +244,6 @@ importers: packages/supabase: dependencies: - '@faunosteo/domain': - specifier: workspace:* - version: link:../domain '@supabase/supabase-js': specifier: ^2.100.0 version: 2.117.3 @@ -257,6 +254,9 @@ importers: '@electric-sql/pglite': specifier: ^0.3.0 version: 0.3.16 + '@faunosteo/core-wasm': + specifier: workspace:* + version: link:../core-wasm '@types/node': specifier: ^20.19.41 version: 20.19.41