/**
* V1-2.3 end to end (contract W6, V1-2 contract §6.4, §6.5, §16.4): the REAL
* verification worker (`faunosteo_worker:dev`, `run --jobs sheet-verify`)
* against the throwaway stack, through a gateway with Kong's routes. Every
* drawing goes through the register / Storage / confirm path of an editor at
* aal2; every verdict below is the worker's, never written by the test. The
* one SQL stand-in is a lease held for another worker (case d), the only way
* to keep a round `verifying` while a live worker runs.
*
* Run through `pnpm db:sandbox verify-e2e`, which starts the gateway and the
* worker and sets SANDBOX_VERIFY_WORKER; skipped otherwise, so `pnpm
* db:sandbox test` without a worker stays green.
*/
import { execFile } from 'node:child_process'
import { randomUUID } from 'node:crypto'
import { readFile } from 'node:fs/promises'
import { join } from 'node:path'
import { performance } from 'node:perf_hooks'
import { repoRoot } from '../db/harness'
import type { SteppedAccount } from './mfa'
import { holder, message, ok, sha256 } from './sheet-fixtures'
import { rpc, sql, stack } from './stack'
const WORKER = process.env.SANDBOX_VERIFY_WORKER ?? ''
const HISTORICAL = '503dc301-a8b1-50d4-9e44-0919b2fc2916'
const BUCKET = 'anatomical-sheet-assets'
const SEED = 'supabase/seed/anatomical-sheets/equine/draft'
/** The contract's budget for one verification at the caps (§16.4, W6). */
const CAPS_BUDGET_MS = 120_000
const ROUND_TIMEOUT_MS = 60_000
interface Round {
id: string
status: string
attempts: number
failure_outcome: string | null
failure_code: string | null
lease_token: string | null
verified_with: string | null
verdict: Verdict | null
}
interface Verdict {
outcome: string
review_id: string
version_id: string
sheet_id: string
revision: number
fingerprint: string | null
failure_code: string | null
failures: string[]
drawings: { asset_id: string; sha256: string; result: string }[]
groups: { key: string; digest: string; coverable: boolean }[]
}
interface Draft {
sheet: string
draft: string
revision: number
}
const name = (text: string) => ({ fr: text, en: text })
/** A drawing in the stored form (`serialize::stored_form`), distinct per `tag`. */
const storedDrawing = (tag = randomUUID().replace(/-/g, '')) =>
Buffer.from(
'\n' +
``,
)
/** The core's `DRAWING` (crates/core/tests/sheet_verdict.rs), the drawing its `at_caps` geometry is sized on. */
const CORE_DRAWING = Buffer.from(
'\n',
)
/** One view on `asset`, one point, one zone; every element reviewed (publishable). */
function smallDoc(asset: string, geometry: unknown = { type: 'circle', cx: 1, cy: 1, r: 1 }) {
return {
schema_version: 2, species_id: 'equine', name: name('Cheval vérifié'), description: name('d'),
scope_disclaimer: name('Outil'), attribution: { authors: ['F'], license: null, notes: null },
vertebral_config: { regions: [] }, references: [],
categories: [{ id: 'c', name: name('C'), color: null }],
views: [{ id: 'main', kind: 'main', name: name('Vue'), svg_asset_id: asset, view_box: null }],
annotations: [{
id: 'a1', kind: 'point', view_id: 'main', category_id: 'c', laterality: 'unpaired', geometry,
review_status: 'reviewed', name: name('P'), finding_zone_id: 'z1', vertebral_region_code: null,
}],
finding_zones: [{
id: 'z1', name: name('Z'),
categories: [{
id: 'cat', kind: 'dysfunction', name: name('D'), review_status: 'reviewed',
children: [{ id: 'n1', name: name('N'), review_status: 'reviewed' }],
}],
}],
}
}
/** The core's `at_caps` document (crates/core/tests/sheet_verdict.rs): 64 views, 1 024 annotations, 8 192 nodes. */
function atCapsDoc(asset: string) {
const doc = smallDoc(asset) as Record
doc.description = {}
const chain = (levels: number) => {
let node: Record = { id: `c${levels}`, name: name('N'), review_status: 'reviewed' }
for (let level = levels - 1; level >= 1; level--)
node = { id: `c${level}`, name: name('N'), review_status: 'reviewed', children: [node] }
return node
}
const views: unknown[] = [{ id: 'main', kind: 'main', name: name('V'), svg_asset_id: asset, view_box: null }]
const annotations: unknown[] = []
for (let i = 1; i < 64; i++) {
views.push({ id: `d${i}`, kind: 'detail', name: name('D'), svg_asset_id: null,
view_box: { min_x: 0, min_y: 0, width: 5, height: 5 }, parent_view_id: 'main' })
annotations.push({ id: `t${i}`, kind: 'detail_trigger', view_id: 'main', category_id: 'c', laterality: 'unpaired',
geometry: { type: 'polygon', points: [[0, 0], [1, 0], [1, 1]] }, review_status: 'reviewed', name: null,
opens_view_id: `d${i}` })
}
for (let i = 0; i < 1024 - 63; i++)
annotations.push({ id: `p${i}`, kind: 'point', view_id: `d${1 + (i % 63)}`, category_id: 'c', laterality: 'left',
geometry: { type: 'circle', cx: 1.25, cy: 2.5, r: 0.5 }, review_status: 'reviewed', name: name('Point'),
finding_zone_id: `z${i % 64}`, vertebral_region_code: null })
doc.views = views
doc.annotations = annotations
doc.finding_zones = Array.from({ length: 64 }, (_, z) => ({
id: `z${z}`, name: name('Zone'),
categories: [{ id: 'cat', kind: 'dysfunction', name: name('Catégorie'), review_status: 'reviewed',
children: [chain(7), ...Array.from({ length: 120 }, (_, n) =>
({ id: `l${n}`, name: name('Nœud de lésion'), review_status: 'reviewed' }))] }],
}))
return doc
}
const objectExists = async (sha: string) =>
(await sql(`select count(*) from storage.objects where bucket_id = '${BUCKET}' and name = '${sha}.svg'`)) === '1'
/** POST of the object under its content key with `token`; the Storage INSERT policy decides. */
async function putObject(token: string, bytes: Buffer): Promise {
const res = await fetch(`${stack.storageUrl}/object/${BUCKET}/${sha256(bytes)}.svg`, {
method: 'POST',
headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'image/svg+xml' },
body: new Uint8Array(bytes),
})
await res.arrayBuffer()
return res.status
}
/** Registers `bytes` for `sheet`, uploads them (unless the shared object already exists), confirms. */
async function putDrawing(editor: SteppedAccount, sheet: string, bytes: Buffer, file = 'd.svg'): Promise {
const asset = ok(await rpc('register_sheet_asset',
{ sheet_id: sheet, sha256: sha256(bytes), byte_size: bytes.length, file_name: file }, editor.aal2)) as string
if (!(await objectExists(sha256(bytes)))) expect(await putObject(editor.aal2, bytes)).toBe(200)
ok(await rpc('confirm_sheet_asset_upload', { asset_id: asset }, editor.aal2))
return asset
}
/** A new official sheet by `editor`, its draft at revision 0. */
async function newSheet(editor: SteppedAccount): Promise {
const placeholder = smallDoc('00000000-0000-4000-8000-000000000000')
;(placeholder.views[0] as { svg_asset_id: string | null }).svg_asset_id = null
const sheet = ok(await rpc('create_official_sheet',
{ slug: `vf-${randomUUID().slice(0, 12)}`, document: placeholder }, editor.aal2)) as string
const draft = await sql(`select id from public.anatomical_sheet_versions where sheet_id = '${sheet}' and status = 'draft'`)
return { sheet, draft, revision: 0 }
}
async function save(editor: SteppedAccount, d: Draft, document: unknown): Promise {
ok(await rpc('save_sheet_draft',
{ version_id: d.draft, expected_revision: d.revision, document, change_note: 'e2e' }, editor.aal2))
d.revision += 1
}
const submit = async (editor: SteppedAccount, d: Draft): Promise =>
ok(await rpc('submit_sheet_review',
{ version_id: d.draft, expected_revision: d.revision, note: 'e2e' }, editor.aal2)) as string
async function round(id: string): Promise {
return JSON.parse(await sql(`select row_to_json(r)::text from public.anatomical_sheet_reviews r where id = '${id}'`))
}
/** The full row as text: a write of any column shows. */
const roundText = (id: string) =>
sql(`select row_to_json(r)::text from public.anatomical_sheet_reviews r where id = '${id}'`)
/** Polls until the worker moved the round out of `verifying`; resolves with it and the elapsed time. */
async function settled(id: string, timeoutMs = ROUND_TIMEOUT_MS): Promise<{ round: Round; ms: number }> {
const start = performance.now()
while (performance.now() - start < timeoutMs) {
const status = await sql(`select status from public.anatomical_sheet_reviews where id = '${id}'`)
if (status !== 'verifying') return { round: await round(id), ms: Math.round(performance.now() - start) }
await new Promise((resolve) => setTimeout(resolve, 200))
}
const last = await round(id)
throw new Error(`round ${id} still verifying after ${timeoutMs} ms (attempts ${last.attempts}, code ${last.failure_code})`)
}
/** The worker's own measure of a round (`duration_ms` of its verify_* line), or null. */
function workerDuration(review: string): Promise {
return new Promise((resolve) => {
execFile('docker', ['logs', WORKER], { maxBuffer: 16 * 1024 * 1024 }, (error, stdout) => {
if (error) return resolve(null)
const line = stdout.split('\n').reverse().map((l) => {
try { return JSON.parse(l) as Record } catch { return null }
}).find((l) => l?.review_id === review && String(l?.event).startsWith('verify_'))
resolve(typeof line?.duration_ms === 'number' ? line.duration_ms : null)
})
})
}
function expectPassedFor(r: Round, d: Draft, assets: { id: string; sha: string }[]): void {
expect(r.status, JSON.stringify(r.verdict?.failures)).toBe('open')
const v = r.verdict as Verdict
expect(v.outcome).toBe('passed')
expect([v.sheet_id, v.version_id, v.review_id, v.revision]).toEqual([d.sheet, d.draft, r.id, d.revision])
expect(v.fingerprint).toMatch(/^[0-9a-f]{64}$/)
expect(v.drawings.map((x) => [x.asset_id, x.sha256, x.result])).toEqual(assets.map((a) => [a.id, a.sha, 'ok']))
expect(v.groups.map((g) => g.key)).toContain('general')
expect(r.verified_with).toMatch(/^faunosteo-core /)
expect([r.lease_token, r.failure_code]).toEqual([null, null])
}
function expectFailed(r: Round, code: string): void {
expect(r.status).toBe('verification_failed')
expect([r.failure_outcome, r.failure_code]).toEqual(['failed', code])
expect(r.verdict?.outcome).toBe('failed')
expect(r.verdict?.failures[0]).toBe(code)
}
const avatarsAndPurges = () =>
sql(`select (select count(*) || ':' || md5(coalesce(string_agg(row_to_json(a)::text, ',' order by a.id), '')) from public.assets a)
|| ' ' ||
(select count(*) || ':' || md5(coalesce(string_agg(row_to_json(q)::text, ',' order by q.id), '')) from private.object_purge_queue q)`)
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
/** An id read back from the stack, checked before it is interpolated into SQL. */
function uuidOf(value: unknown): string {
if (typeof value !== 'string' || !UUID.test(value)) throw new Error(`not a uuid: ${String(value)}`)
return value
}
/** `octet_length(value::jsonb::text)` of a value whose strings need no JSON escape (crates/core/src/domain/sheet/size.rs). */
function jsonbBytes(value: unknown): number {
if (value === null) return 4
if (typeof value === 'boolean') return value ? 4 : 5
if (typeof value === 'number') return String(value).length
if (typeof value === 'string') return Buffer.byteLength(value) + 2
if (Array.isArray(value)) return 2 + value.reduce((n: number, v) => n + jsonbBytes(v), 0) + 2 * Math.max(0, value.length - 1)
const entries = Object.entries(value as Record)
return 2 + entries.reduce((n, [k, v]) => n + jsonbBytes(k) + 2 + jsonbBytes(v), 0) + 2 * Math.max(0, entries.length - 1)
}
/**
* A drawing of exactly `size` bytes in the stored form (`serialize::stored_form`): the UTF-8
* prolog and one `