test(tests): R4.1c text, AFM, dates and PNG oracles (O4, O5, O6)

This commit is contained in:
bchanot committed 2026-10-18 05:37:18 +02:00
1 parent 96df15edae
commit 516342beca
111 files changed
+73567

No files matched your search

+121
View File
@@ -0,0 +1,121 @@
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import { dirname, resolve } from 'node:path'
import { expect } from 'vitest'
import { REPO_ROOT } from './draft'
/**
* Shared plumbing of the R4.1 freeze generators (plan 2026-10-17 §B.5.1,
* O4 to O6). `MAKE_R4_ORACLES=1` writes the files; without it the
* generator recomputes and compares with the committed ones.
*/
export const R4_ORACLES = resolve(REPO_ROOT, 'crates/core/tests/oracles/r4')
export const MAKE_R4 = process.env.MAKE_R4_ORACLES === '1'
/** Where the oracle was computed; informative, left out of the comparison. */
export interface OracleEnvironment {
node: string
v8: string
icu: string | null
unicode: string | null
}
export function environment(): OracleEnvironment {
return {
node: process.version,
v8: process.versions.v8,
icu: process.versions.icu ?? null,
unicode: process.versions.unicode ?? null,
}
}
/** B.5.2: no generator writes while the field corpus is reachable. */
function assertNoFieldCorpus(): void {
if (process.env.FAUNOSTEO_FIELD_CORPUS) {
throw new Error(
'FAUNOSTEO_FIELD_CORPUS is set: R4 oracle generators refuse to write',
)
}
}
export function writeOracleFile(
path: string,
bytes: string | Uint8Array,
): void {
assertNoFieldCorpus()
mkdirSync(dirname(path), { recursive: true })
writeFileSync(path, bytes)
}
/** The JSON value as the file holds it: `-0` becomes `0`, `undefined` fields vanish. */
const asJson = (value: unknown): unknown => JSON.parse(JSON.stringify(value))
/**
* Writes `value` as `JSON.stringify(value, null, 2)` (MAKE), or asserts
* deep equality with the committed file, `environment` excepted.
*/
export function freezeJson(
name: string,
value: { environment: OracleEnvironment },
): void {
const path = resolve(R4_ORACLES, name)
if (MAKE_R4) {
writeOracleFile(path, `${JSON.stringify(value, null, 2)}\n`)
return
}
const expected = JSON.parse(readFileSync(path, 'utf8')) as Record<
string,
unknown
>
const computed = asJson(value) as Record<string, unknown>
delete expected.environment
delete computed.environment
expect(computed).toEqual(expected)
}
/** A number as JavaScript prints it, `-0` kept apart from `0`. */
export const jsNumber = (value: number): string =>
Object.is(value, -0) ? '-0' : String(value)
/** mulberry32: a small seeded PRNG, [0, 1). */
export function mulberry32(seed: number): () => number {
let state = seed >>> 0
return () => {
state = (state + 0x6d2b79f5) >>> 0
let t = state
t = Math.imul(t ^ (t >>> 15), t | 1)
t ^= t + Math.imul(t ^ (t >>> 7), t | 61)
return ((t ^ (t >>> 14)) >>> 0) / 4294967296
}
}
export const MAX_CODE_POINT = 0x10ffff
/** Every code point from 0 to 0x10FFFF run-length encoded as `[start, end, class]` (end inclusive). */
export function classRanges<T extends string>(
classOf: (cp: number) => T,
): Array<[number, number, T]> {
const out: Array<[number, number, T]> = []
for (let cp = 0; cp <= MAX_CODE_POINT; cp += 1) {
const cls = classOf(cp)
const last = out.at(-1)
if (last && last[2] === cls && last[1] === cp - 1) last[1] = cp
else out.push([cp, cp, cls])
}
return out
}
/** The code points where `test` holds, as `[start, end]` ranges (end inclusive). */
export function codePointRanges(
test: (cp: number) => boolean,
): Array<[number, number]> {
return classRanges((cp) => (test(cp) ? 'in' : 'out'))
.filter(([, , cls]) => cls === 'in')
.map(([start, end]) => [start, end])
}
export const isSurrogate = (cp: number): boolean => cp >= 0xd800 && cp <= 0xdfff
/** A one-code-point string; a surrogate stays a lone single-unit string. */
export const charOf = (cp: number): string =>
isSurrogate(cp) ? String.fromCharCode(cp) : String.fromCodePoint(cp)