Files
faunosteo/packages/pdf/tests/helpers/draft.ts
T

93 lines
2.8 KiB
TypeScript

import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { join, resolve } from 'node:path'
import {
AnatomicalSheetDocumentSchema,
ReportMigrationMapSchema,
type ReportMigrationMap,
type SnapshotSource,
} from '@faunosteo/domain'
/**
* Paths and loaders shared by the engine tests. The committed equine draft
* is read from the seed as the application bundles it; the Phase 0 goldens
* and fixtures are read from the app's test folder, never copied.
*/
const HERE = resolve(fileURLToPath(import.meta.url), '..')
export const REPO_ROOT = resolve(HERE, '../../../..')
export const DRAFT_DIR = resolve(
REPO_ROOT,
'supabase/seed/anatomical-sheets/equine/draft',
)
export const LEGACY_BASELINE = resolve(
REPO_ROOT,
'supabase/seed/anatomical-sheets/equine/legacy-import/document.json',
)
export const FIXTURES_DIR = resolve(REPO_ROOT, 'apps/app/tests/fixtures')
export const GOLDEN_DIR = resolve(FIXTURES_DIR, 'goldens/step6')
export const PUBLIC_DIR = resolve(REPO_ROOT, 'apps/app/public')
/** The logo files the app bundles into its export branding. */
export const BRAND_ASSETS_DIR = resolve(REPO_ROOT, 'apps/app/src/assets/brand')
export function readJson<T = unknown>(path: string): T {
return JSON.parse(readFileSync(path, 'utf8')) as T
}
export function readFixtureJson<T = unknown>(name: string): T {
return readJson<T>(resolve(FIXTURES_DIR, name))
}
export function readFixtureBytes(name: string): Uint8Array {
return new Uint8Array(readFileSync(resolve(FIXTURES_DIR, name)))
}
interface SeedRef {
sheet_id: string
version_id: string | null
document_sha256: string
schema_version: number
species_id: string
}
interface SeedAsset {
file: string
sha256: string
svg_asset_id: string
}
export interface DraftSheet extends SnapshotSource {
assetHashes: Readonly<Record<string, string>>
migrationMap: ReportMigrationMap
}
export function loadDraftSheet(): DraftSheet {
const json = <T>(name: string) => readJson<T>(join(DRAFT_DIR, name))
const ref = json<SeedRef>('sheet-ref.json')
const assets: Record<string, string> = {}
const assetHashes: Record<string, string> = {}
for (const asset of json<SeedAsset[]>('assets.json')) {
assets[asset.svg_asset_id] = readFileSync(
join(DRAFT_DIR, 'assets', asset.file),
'utf8',
)
assetHashes[asset.svg_asset_id] = asset.sha256
}
return {
status: 'draft',
ref: {
sheetId: ref.sheet_id,
versionId: ref.version_id,
documentSha256: ref.document_sha256,
},
speciesId: ref.species_id,
schemaVersion: ref.schema_version,
document: AnatomicalSheetDocumentSchema.parse(json('document.json')),
assets,
assetHashes,
migrationMap: ReportMigrationMapSchema.parse(
json('report-migration-map.json'),
),
}
}