Public: anyone can read this; the crew can change it.
Property-based tests for the forge's pure cores, with a replayable seed in `b… #16530
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/16511-property-based-tests-for-the-forge-s-pur → main 0 commits fork
2 files
changed +169
−0
test/helpers/prop.ts added +55
| @@ -0,0 +1,55 @@ | ||
| 1 | /** | |
| 2 | * Property tests in fifty lines: a seeded generator, a shrinker you hand it, | |
| 3 | * and a failure that says how to replay. Values, not only strings (fuzz.ts | |
| 4 | * is the text-only sibling). `PROP_SEED=<n>` replays; `PROP_RUNS=<n>` sets | |
| 5 | * the count. The default seed is fixed, so the suite stays deterministic. | |
| 6 | */ | |
| 7 | import { int, pick, rng, type Rand } from "./fuzz.ts"; | |
| 8 | ||
| 9 | export { int, pick, rng, type Rand }; | |
| 10 | export const PROP_SEED = Number(process.env["PROP_SEED"] ?? 16511); | |
| 11 | export const PROP_RUNS = Number(process.env["PROP_RUNS"] ?? 300); | |
| 12 | ||
| 13 | /** `n` items from `gen`, `n` below `max`. */ | |
| 14 | export const listOf = <T>(r: Rand, max: number, gen: (r: Rand) => T): T[] => | |
| 15 | Array.from({ length: int(r, max) }, () => gen(r)); | |
| 16 | ||
| 17 | /** Smaller lists: halves, then each item dropped. For a shrinker of arrays. */ | |
| 18 | export function* shrinkList<T>(xs: readonly T[]): Generator<T[]> { | |
| 19 | if (xs.length > 1) yield xs.slice(0, xs.length >> 1), yield xs.slice(xs.length >> 1); | |
| 20 | for (let i = 0; i < xs.length; i++) yield [...xs.slice(0, i), ...xs.slice(i + 1)]; | |
| 21 | } | |
| 22 | ||
| 23 | const breaks = (property: (v: any) => unknown, v: unknown): unknown => { | |
| 24 | try { | |
| 25 | return property(v) === false ? "returned false" : undefined; | |
| 26 | } catch (error) { | |
| 27 | return error; | |
| 28 | } | |
| 29 | }; | |
| 30 | ||
| 31 | /** Run `property` on generated cases; on a failure shrink and name the seed. */ | |
| 32 | export function prop<T>( | |
| 33 | name: string, | |
| 34 | gen: (r: Rand) => T, | |
| 35 | property: (value: T) => void | boolean, | |
| 36 | shrink: (value: T) => Iterable<T> = () => [], | |
| 37 | runs = PROP_RUNS, | |
| 38 | ): void { | |
| 39 | const r = rng(PROP_SEED); | |
| 40 | for (let i = 0; i < runs; i++) { | |
| 41 | let value = gen(r); | |
| 42 | let why = breaks(property, value); | |
| 43 | if (why === undefined) continue; | |
| 44 | for (let moved = true, steps = 0; moved && steps < 500; steps++) { | |
| 45 | moved = false; | |
| 46 | for (const smaller of shrink(value)) { | |
| 47 | const again = breaks(property, smaller); | |
| 48 | if (again !== undefined) { value = smaller; why = again; moved = true; break; } | |
| 49 | } | |
| 50 | } | |
| 51 | throw new Error( | |
| 52 | `property ${name}: PROP_SEED=${PROP_SEED} case ${i}\n minimal: ${JSON.stringify(value)}\n ${String(why)}`, | |
| 53 | ); | |
| 54 | } | |
| 55 | } |
test/property-access.test.ts added +114
| @@ -0,0 +1,114 @@ | ||
| 1 | /** | |
| 2 | * Properties of the access evaluator and the wall (#16511). Replay a failure | |
| 3 | * with `PROP_SEED=<n> bun test test/property-access.test.ts`. | |
| 4 | */ | |
| 5 | import { describe, expect, test } from "bun:test"; | |
| 6 | import { | |
| 7 | NOBODY, VERBS, actingAs, atOrBelow, decide, identities, table, | |
| 8 | type Contract, type Role, type Verb, | |
| 9 | } from "../src/contract/can.ts"; | |
| 10 | import { loosened } from "../src/contract/policy.ts"; | |
| 11 | import { firstBadOffset } from "../src/http/json_body.ts"; | |
| 12 | import { int, listOf, pick, prop, shrinkList, type Rand } from "./helpers/prop.ts"; | |
| 13 | ||
| 14 | /** | |
| 15 | * Roles r0 (everyone), then identities and row-held roles; `above` only points down, so it is a | |
| 16 | * DAG. | |
| 17 | */ | |
| 18 | const genContract = (r: Rand): Contract => { | |
| 19 | const n = 2 + int(r, 4); | |
| 20 | const roles: Record<string, Role> = {}; | |
| 21 | for (let i = 0; i < n; i++) { | |
| 22 | const held = | |
| 23 | i === 0 ? { by: "everyone" as const } | |
| 24 | : r() < 0.3 ? { by: "row" as const, column: pick(r, ["owner", "author"]) } | |
| 25 | : { by: "identity" as const, role: `sig${i}` }; | |
| 26 | const above = Array.from({ length: i }, (_, j) => `r${j}`).filter(() => r() < 0.5); | |
| 27 | roles[`r${i}`] = { above, held }; | |
| 28 | } | |
| 29 | const names = [...Object.keys(roles), NOBODY]; | |
| 30 | const resources: Record<string, Contract["resources"][string]> = {}; | |
| 31 | for (let k = 0, count = 1 + int(r, 3); k < count; k++) | |
| 32 | resources[`res${k}`] = { | |
| 33 | can: Object.fromEntries(VERBS.map((v) => [v, pick(r, names)])) as Record<Verb, string>, | |
| 34 | }; | |
| 35 | return { roles, resources }; | |
| 36 | }; | |
| 37 | const withCell = (c: Contract, res: string, verb: Verb, need: string): Contract => ({ | |
| 38 | ...c, | |
| 39 | resources: { ...c.resources, [res]: { ...c.resources[res]!, can: { ...c.resources[res]!.can, [verb]: need } } }, | |
| 40 | }); | |
| 41 | const shrinkContract = function* (c: Contract): Generator<Contract> { | |
| 42 | const keys = Object.keys(c.resources); | |
| 43 | for (const k of keys.length > 1 ? keys : []) { | |
| 44 | yield { ...c, resources: Object.fromEntries(Object.entries(c.resources).filter(([n]) => n !== k)) }; | |
| 45 | } | |
| 46 | }; | |
| 47 | ||
| 48 | describe("access properties", () => { | |
| 49 | test("permit is monotone in role: whatever a role may, anything above it may", () => { | |
| 50 | prop("monotone", genContract, (c) => { | |
| 51 | const ids = identities(c).filter((n) => c.roles[n]!.held.by === "identity"); | |
| 52 | for (const res of Object.keys(c.resources)) for (const verb of VERBS) | |
| 53 | for (const low of ids) for (const high of ids) { | |
| 54 | if (!atOrBelow(c, low, high)) continue; | |
| 55 | const asLow = decide(c, res, verb, actingAs(c, low).actor); | |
| 56 | const asHigh = decide(c, res, verb, actingAs(c, high).actor); | |
| 57 | if (asLow === "yes" && asHigh !== "yes") return false; | |
| 58 | } | |
| 59 | }, shrinkContract); | |
| 60 | }); | |
| 61 | ||
| 62 | test("the table and the action agree for every identity at every door", () => { | |
| 63 | prop("table", genContract, (c) => { | |
| 64 | for (const cell of table(c)) { | |
| 65 | const { actor, mine, theirs } = actingAs(c, cell.as); | |
| 66 | const rowless = cell.verb === "create" || cell.verb === "list"; | |
| 67 | const any = decide(c, cell.resource, cell.verb, actor, rowless ? undefined : theirs); | |
| 68 | const own = decide(c, cell.resource, cell.verb, actor, rowless ? undefined : mine); | |
| 69 | if (cell.answer === "yes" && any !== "yes") return false; | |
| 70 | if (cell.answer === "no" && (any === "yes" || own !== "no")) return false; | |
| 71 | if (cell.answer === "own" && (any === "yes" || own === "no")) return false; | |
| 72 | } | |
| 73 | }, shrinkContract); | |
| 74 | }); | |
| 75 | ||
| 76 | test("loosening a cell is always a wall, tightening never is", () => { | |
| 77 | prop("wall", (r) => ({ c: genContract(r), res: "res0", verb: pick(r, VERBS), to: r() }), ({ c, res, verb, to }) => { | |
| 78 | const roles = Object.keys(c.roles); | |
| 79 | const need = roles[Math.floor(to * roles.length)]!; | |
| 80 | const after = withCell(c, res, verb, need); | |
| 81 | const key = (x: { resource: string; verb: string; as: string }) => `${x.resource}.${x.verb}.${x.as}`; | |
| 82 | const rank = { no: 0, own: 1, yes: 2 }; | |
| 83 | const was = new Map(table(c).map((x) => [key(x), rank[x.answer]])); | |
| 84 | const rose = table(after).filter((x) => rank[x.answer] > was.get(key(x))!).map(key).sort(); | |
| 85 | const walls = loosened(c, after).map(key).sort(); | |
| 86 | if (JSON.stringify(rose) !== JSON.stringify(walls)) return false; | |
| 87 | // Setting the cell to nobody can only tighten. | |
| 88 | return loosened(c, withCell(c, res, verb, NOBODY)).length === 0; | |
| 89 | }); | |
| 90 | }); | |
| 91 | ||
| 92 | test("a contract is never a wall against itself", () => { | |
| 93 | prop("self", genContract, (c) => loosened(c, c).length === 0, shrinkContract); | |
| 94 | }); | |
| 95 | }); | |
| 96 | ||
| 97 | describe("json body offsets", () => { | |
| 98 | const pieces = ['{"a":', "1", "[", "]", "}", ",", '"x"', "\\", '"', "\u0000", " ", "null", "tru", "-", "e5", "é"]; | |
| 99 | const gen = (r: Rand) => listOf(r, 10, (q) => pick(q, pieces)); | |
| 100 | test("firstBadOffset is -1 exactly when JSON.parse accepts the text", () => { | |
| 101 | prop("offset", gen, (parts) => { | |
| 102 | const text = parts.join(""); | |
| 103 | let ok = true; | |
| 104 | try { JSON.parse(text); } catch { ok = false; } | |
| 105 | const off = firstBadOffset(text); | |
| 106 | return ok ? off === -1 : off >= 0 && off <= text.length; | |
| 107 | }, shrinkList); | |
| 108 | }); | |
| 109 | }); | |
| 110 | ||
| 111 | test("prop shrinks to a minimal case and names the seed", () => { | |
| 112 | expect(() => prop("sum", (r) => listOf(r, 20, (q) => int(q, 9)), (xs) => xs.every((x) => x !== 7), shrinkList, 500)) | |
| 113 | .toThrow(/PROP_SEED=\d+ case \d+\n {2}minimal: \[7\]/); | |
| 114 | }); |
new above
offline