test(app): catalogue card integrity and retry on the sandbox

This commit is contained in:
bchanot committed 2026-11-17 00:24:14 +01:00
1 parent 6ad8d8347c
commit dadd8e5d29
3 files changed
+357 -8

No files matched your search

+4 -4
View File
@@ -34,14 +34,14 @@ export async function assignRole(
)
}
/** A right granted earlier by the owner (a prior action, written as postgres). */
/** A right granted earlier by the owner (a prior action, written as postgres); resolves with its id. */
export async function seedGrant(
account: SandboxAccount,
kind: GrantKind,
): Promise<void> {
): Promise<string> {
const note = kind === 'catalog_reader' ? 'null' : "'sandbox'"
await sql(`insert into public.anatomical_catalog_grants (user_id, grant_kind, reason, verification_note, granted_by)
values ('${account.id}', '${kind}', 'sandbox', ${note}, '${account.id}')`)
return sql(`insert into public.anatomical_catalog_grants (user_id, grant_kind, reason, verification_note, granted_by)
values ('${account.id}', '${kind}', 'sandbox', ${note}, '${account.id}') returning id`)
}
/** A password session of the account on GoTrue: always aal1. */
+53 -4
View File
@@ -37,6 +37,13 @@ export interface SheetNames {
export interface FixtureSheet {
id: string
names: SheetNames
/** The version the catalogue lists (v2 of a listed sheet), null for a draft-only one. */
currentVersionId: string | null
}
export interface SeedOptions {
/** Stored on the published and retired versions in place of the core's (an altered record). */
storedFingerprint?: string
}
/**
@@ -141,6 +148,7 @@ export type SheetShape =
export async function seedOfficialSheet(
names: SheetNames,
shape: SheetShape,
options: SeedOptions = {},
): Promise<FixtureSheet> {
const { drawing, document: text, sha } = await seedFiles()
await storeDrawing(sha, drawing)
@@ -151,7 +159,8 @@ export async function seedOfficialSheet(
const fingerprint =
shape === 'draft'
? null
: await fingerprintOf(document, asset, sha, drawing)
: (options.storedFingerprint ??
(await fingerprintOf(document, asset, sha, drawing)))
const slug = `e2e-${id.slice(0, 12)}`
const archived = shape === 'archived'
const content = `${literal(document)}::jsonb`
@@ -162,8 +171,9 @@ export async function seedOfficialSheet(
: shape === 'archived'
? `('${v1}', '${id}', 1, 'published', 2, ${content}, now(), null, ${literal(fingerprint)})`
: `('${v1}', '${id}', 1, 'draft', 2, ${content}, null, null, null)`
const current =
shape === 'listed' ? `'${v2}'` : shape === 'archived' ? `'${v1}'` : 'null'
const currentVersionId =
shape === 'listed' ? v2 : shape === 'archived' ? v1 : null
const current = literal(currentVersionId)
await sql(`begin;
select set_config('request.jwt.claims', '{"sub":"${SEED_OWNER}"}', true);
insert into public.anatomical_sheets (id, species_id, name, slug, is_official, visibility, status, archived_at)
@@ -177,5 +187,44 @@ export async function seedOfficialSheet(
update public.anatomical_sheets set current_version_id = ${current},
last_version_number = ${shape === 'listed' ? 2 : 1} where id = '${id}';
commit;`)
return { id, names }
return { id, names, currentVersionId }
}
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
/** An id a run captured, checked before it is written into a statement. */
function capturedId(id: string): string {
if (!UUID.test(id)) throw new Error(`cleanup: not a captured id (${id})`)
return id
}
/**
* Cleanup of a run (D4): one sheet the run seeded archived through the
* migrations' own path as the seed owner (audit row included), then its status
* read back; resolves with that status. Names that one id, nothing by pattern.
*/
export async function archiveOwnSheet(sheetId: string): Promise<string> {
const id = capturedId(sheetId)
await sql(`select private.set_sheet_status('${SEED_OWNER}', '${id}', 'active', 'archived',
'anatomical_sheet.archived')`)
return sql(`select status from public.anatomical_sheets where id = '${id}'`)
}
/**
* Cleanup of a run (D4): the catalogue grant the run seeded revoked by its id,
* as its account (the single change the grant guard allows); resolves with the
* id the update returned and the id read back as revoked (empty when none).
*/
export async function revokeOwnGrant(
grantId: string,
accountId: string,
): Promise<{ returned: string; readBack: string }> {
const [id, account] = [capturedId(grantId), capturedId(accountId)]
const returned =
await sql(`update public.anatomical_catalog_grants set revoked_at = now(),
revoked_by = '${account}', revocation_reason = 'e2e cleanup'
where id = '${id}' and revoked_at is null returning id`)
const readBack = await sql(`select id from public.anatomical_catalog_grants
where id = '${id}' and revoked_at is not null and revocation_reason = 'e2e cleanup'`)
return { returned, readBack }
}