Skip to content
Myr

scenarios.test.ts

This file

Path

packages/testkit/test/scenarios.test.ts

359 lines · 10.3 KiB · TypeScript

Last changed by

Merge build/maintainer-wording into mainoscabrielbefore adoption4c1a571

History of this file

Take it

The walk stopped after 200 commits: lines older than 678c8b8 point at it.

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 1–77why, one step back
1import { describe, expect, it } from "@effect/vitest";
2import { Brand } from "@repo/brand";3import { Ask } from "@repo/protocol/domain/ask";4import { CheckSpec } from "@repo/protocol/domain/check";5import { Command, SymbolName } from "@repo/protocol/domain/common";6import { Policy } from "@repo/protocol/domain/policy";7import {8 CommitSha,9 DecisionId,10 ProposalId,11 RepoPath,12 RequestId,13} from "@repo/protocol/ids";14import { DateTime, Equal, Schema } from "effect";15import { onTestFinished, vi } from "vitest";1617import {18 aProject,19 ids,20 sha,21 sixChildren,22 withAsk,23 withKind,24 withVouches,25} from "../src/scenarios.ts";2627const six = [1, 2, 3, 4, 5, 6];2829const children = six.map((n) => `drop-legacy-fetch-p${n}`);3031const isPolicy = Schema.is(Policy);3233const isAsk = Schema.is(Ask);3435const lintToZero = CheckSpec.cases.LintToZero.make({36 command: Command.make("bun run lint --format json"),37 rule: "no-floating-effect",38});3940const lintKind = {41 checkKind: "lintToZero",42 claimers: "anyone",43 finish: "never",44 name: "lintToZero",45 oneWay: false,46 race: 1,47 requires: ["want"],48 strength: "assertion",49} as const;5051const parserFix = () =>52 aProject().pipe(53 withAsk({ id: "fix-parser", territory: "packages/parser" }),54 withVouches(["bob"])55 );5657describe("a project scenario", () => {58 it("holds a policy that decodes as a Policy, at its landed commit", () => {59 const { landed, policy } = aProject();6061 expect(isPolicy(policy)).toBeTruthy();62 expect(63 Schema.decodeSync(Policy)(Schema.encodeSync(Policy)(policy))64 ).toStrictEqual(policy);65 expect(policy.at).toBe(landed);66 expect(policy.vouch.file).toBe(`${Brand.configDir}/vouched.td`);67 expect(policy.kinds.map(({ name }) => name)).toStrictEqual([68 "fix",69 "migrate",70 ]);71 });7273 it("is decided, by default, by its initializer alice, for every path", () => {74 const { initializer, policy, project } = aProject();7576 expect({ initializer, project }).toStrictEqual({77 initializer: "alice",

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 78why, one step back
78 project: "alice/datekit",

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 79–84why, one step back
79 });
80 expect(policy.deciders).toStrictEqual([81 { handles: ["alice"], paths: "**" },82 ]);83 });84

Merge build/maintainer-wording into mainoscabrielbefore adoption4c1a571

line 85why, one step back
85 it("names its people by the names it was given, maintainers first", () => {

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 86–91why, one step back
86 const scenario = aProject({
87 deciders: ["dana", "eve"],88 name: "burrow",89 people: ["bob", "carol"],90 });91

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 92why, one step back
92 expect(scenario.project).toBe("dana/burrow");

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 93–126why, one step back
93 expect(scenario.initializer).toBe("dana");
94 expect([...scenario.people]).toStrictEqual([95 ["dana", "dana"],96 ["eve", "eve"],97 ["bob", "bob"],98 ["carol", "carol"],99 ]);100 expect(scenario.policy.deciders).toStrictEqual([101 { handles: ["dana", "eve"], paths: "**" },102 ]);103 });104105 it("opens an ask in its territory, asked by the initializer in the landed commit", () => {106 const now = DateTime.makeUnsafe("2026-10-09T08:30:00Z");107108 const scenario = aProject({ now }).pipe(109 withAsk({ id: "fix-parser", territory: "packages/parser" })110 );111112 const [ask] = scenario.policy.asks;113114 expect(isAsk(ask)).toBeTruthy();115 expect(ask).toStrictEqual({116 askedBy: "alice",117 blockedBy: [],118 brief: { want: "fix-parser: its check passes in packages/parser" },119 check: CheckSpec.cases.RedThenGreen.make({120 path: RepoPath.make("packages/parser/test/fix-parser.test.ts"),121 }),122 file: `${Brand.configDir}/asks/fix-parser.ask.ts`,123 id: "fix-parser",124 kind: "fix",125 openedAt: now,126 openedIn: scenario.landed,

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 127why, one step back
127 project: "alice/datekit",

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 128–306why, one step back
128 state: "open",
129 territory: { module: "parser", paths: ["packages/parser/**"] },130 });131 expect(scenario.policy.modules).toStrictEqual([132 { module: "parser", paths: ["packages/parser/**"] },133 ]);134 expect(isPolicy(scenario.policy)).toBeTruthy();135 });136137 it("takes an ask's kind, check, blockers and reserve, and keeps one module per territory", () => {138 const scenario = aProject().pipe(139 withAsk({ id: "fix-parser", territory: "packages/parser" }),140 withAsk({141 blockedBy: ["fix-parser"],142 check: lintToZero,143 id: "lint-parser",144 kind: "lintToZero",145 reserve: "newcomers",146 territory: "packages/parser",147 })148 );149150 const [, ask] = scenario.policy.asks;151152 expect(isAsk(ask)).toBeTruthy();153 expect(ask).toMatchObject({154 blockedBy: ["fix-parser"],155 check: lintToZero,156 kind: "lintToZero",157 reserve: "newcomers",158 });159 expect(scenario.policy.modules).toHaveLength(1);160 });161162 it("opens an ask over the whole repo without making the repo a module", () => {163 const scenario = aProject().pipe(164 withAsk({ id: "tidy-readme", territory: "." })165 );166167 expect(scenario.policy.asks[0]?.territory).toStrictEqual({168 module: ".",169 paths: ["**"],170 });171 expect(scenario.policy.modules).toStrictEqual([]);172 });173174 it("refuses an ask the domain refuses, such as one named after a reserved word", () => {175 expect(() =>176 aProject().pipe(withAsk({ id: "me", territory: "packages/parser" }))177 ).toThrow("reserved");178 });179180 it("adds a kind, replacing one of the same name", () => {181 const scenario = aProject().pipe(182 withKind(lintKind),183 withKind({ ...lintKind, race: 2 })184 );185186 expect(scenario.policy.kinds.map(({ name }) => name)).toStrictEqual([187 "fix",188 "migrate",189 "lintToZero",190 ]);191 expect(scenario.policy.kinds[2]).toStrictEqual({ ...lintKind, race: 2 });192 });193194 it("records vouches and denouncements as the vouch file's lines", () => {195 const scenario = aProject().pipe(196 withVouches(["bob", "carol"], ["mallory"])197 );198199 expect(scenario.policy.vouches).toStrictEqual([200 { denounced: false, handle: "bob" },201 { denounced: false, handle: "carol" },202 { denounced: true, handle: "mallory" },203 ]);204 expect(isPolicy(scenario.policy)).toBeTruthy();205 });206207 it("builds the same value every time", () => {208 expect(Equal.equals(parserFix(), parserFix())).toBeTruthy();209 expect(Equal.equals(sixChildren(), sixChildren())).toBeTruthy();210 expect(Equal.equals(parserFix(), aProject({ name: "burrow" }))).toBeFalsy();211 });212});213214describe("six children of a migrate ask", () => {215 it("is a parent, six children and a delete child, each a valid ask", () => {216 const { policy } = sixChildren();217218 expect(policy.asks.map(({ id }) => id)).toStrictEqual([219 "drop-legacy-fetch",220 ...children,221 "drop-legacy-fetch-delete",222 ]);223 expect(policy.asks.every((ask) => isAsk(ask))).toBeTruthy();224 expect(isPolicy(policy)).toBeTruthy();225 });226227 it("has a parent migrating the symbol across the six packages", () => {228 const [parent] = sixChildren().policy.asks;229230 expect(parent).toMatchObject({231 check: CheckSpec.cases.Migrate.make({232 count: Command.make(233 "node scripts/count-callers.mjs legacyFetch --json"234 ),235 from: SymbolName.make("legacyFetch"),236 to: SymbolName.make("http.get"),237 }),238 kind: "migrate",239 territory: { paths: six.map((n) => `packages/p${n}/**`) },240 });241 expect(parent?.parent).toBeUndefined();242 });243244 it("splits into six children on disjoint packages, the project's modules", () => {245 const { policy } = sixChildren();246 const steps = policy.asks.slice(1, 7);247248 expect(249 steps.map(({ blockedBy, id, parent, territory }) => ({250 blockedBy,251 id,252 parent,253 territory,254 }))255 ).toStrictEqual(256 six.map((n) => ({257 blockedBy: [],258 id: `drop-legacy-fetch-p${n}`,259 parent: "drop-legacy-fetch",260 territory: { module: `p${n}`, paths: [`packages/p${n}/**`] },261 }))262 );263 expect(264 steps.every(({ check }) => CheckSpec.guards.Migrate(check))265 ).toBeTruthy();266 expect(policy.modules.map(({ module }) => module)).toStrictEqual(267 six.map((n) => `p${n}`)268 );269 });270271 it("ends with a delete child blocked by all six", () => {272 const last = sixChildren().policy.asks.at(-1);273274 expect(last).toMatchObject({275 blockedBy: children,276 check: CheckSpec.cases.SymbolAbsent.make({277 count: Command.make(278 "node scripts/count-callers.mjs legacyFetch --json"279 ),280 symbol: SymbolName.make("legacyFetch"),281 }),282 parent: "drop-legacy-fetch",283 });284 });285286 it("names the family after the symbol it moves", () => {287 const { policy } = sixChildren({ symbol: "oldClock", to: "clock.now" });288289 expect(policy.asks.map(({ id }) => id)).toStrictEqual([290 "drop-old-clock",291 ...six.map((n) => `drop-old-clock-p${n}`),292 "drop-old-clock-delete",293 ]);294 expect(policy.asks[0]?.check).toMatchObject({295 from: "oldClock",296 to: "clock.now",297 });298 });299});300301describe("deterministic ids", () => {302 it("makes a commit id from a seed with FNV-1a, the same every run", () => {303 // Worked out by a separate FNV-1a implementation over `0:main` … `4:main`.304 expect(sha("main")).toBe("72245cbe78d5b41b2cadf2502b38c3a5d6578b1a");305 expect(Schema.is(CommitSha)(sha("main"))).toBeTruthy();306 expect(sha("main")).not.toBe(sha("answer"));

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 307why, one step back
307 expect(aProject().landed).toBe("82d7409c24ead4d50f7614f6673ecdd72315be58");

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 308–359why, one step back
308 });
309310 it("mints request, proposal and decision ids from counters", () => {311 const next = ids();312313 expect([next.request(), next.request()]).toStrictEqual([314 "req-000001",315 "req-000002",316 ]);317 expect([next.proposal(), next.decision()]).toStrictEqual([318 "prop-000001",319 "dec-000001",320 ]);321 expect(ids().request()).toBe("req-000001");322 });323324 it("mints ids the domain accepts", () => {325 const next = ids();326327 expect(Schema.is(RequestId)(next.request())).toBeTruthy();328 expect(Schema.is(ProposalId)(next.proposal())).toBeTruthy();329 expect(Schema.is(DecisionId)(next.decision())).toBeTruthy();330 });331332 it("builds every value without reading the clock or randomness", () => {333 const reads = {334 getRandomValues: vi.spyOn(crypto, "getRandomValues"),335 now: vi.spyOn(Date, "now"),336 random: vi.spyOn(Math, "random"),337 randomUUID: vi.spyOn(crypto, "randomUUID"),338 };339340 onTestFinished(() => {341 for (const read of Object.values(reads)) {342 read.mockRestore();343 }344 });345346 const next = ids();347348 parserFix();349 sixChildren();350 sha("main");351 next.request();352 next.proposal();353 next.decision();354355 expect(356 Object.values(reads).map((read) => read.mock.calls.length)357 ).toStrictEqual([0, 0, 0, 0]);358 });359});