evidence.test.ts
This file
Path
packages/project/test/evidence.test.ts
312 lines · 8.9 KiB · TypeScript
Last changed by
Merge build/maintainer-wording into mainoscabrielbefore adoption4c1a571
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313import { describe, expect, it } from "@effect/vitest";import { RefName, RepoHost } from "@repo/artifacts/repo-host";import { EvidenceStore, LogRef, logRefOf } from "@repo/checks/evidence-store";import { EvidenceStoreMemory } from "@repo/checks/evidence-store/memory";import { CheckSpec } from "@repo/protocol/domain/check";import { AskId, LandedSha, LaneName, ProjectName, RepoName, RepoPath, keyOf, RequestId,} from "@repo/protocol/ids";import { Anonymous, Person } from "@repo/protocol/principal";import { NotDecider, UnknownProject } from "@repo/protocol/refusal";import { advanceTo } from "@repo/testkit/clock";import type { Files } from "@repo/testkit/git-client";import { aProject, withAsk, withVouches } from "@repo/testkit/scenarios";import { Array, Effect, Layer, Option } from "effect";
import { projectService } from "../src/project.ts";import { CheckScript, composedAt, Pusher, worldOf } from "./world.ts";
const scenario = aProject({ people: ["bob", "carol"] }).pipe( withVouches(["bob", "carol"]), withAsk({ id: "parse-iso", territory: "packages/parser" }));
const { project } = scenario;
const repo = RepoName.make(keyOf(project));
const person = (name: string) => Person.make({ handle: scenario.people.get(name) ?? scenario.initializer });
const alice = person("alice");
const bob = person("bob");
const carol = person("carol");
const parseIso = AskId.make("parse-iso");
const unit = LaneName.make("unit");
const requestId = RequestId.make("req-fix-0001");
const mainRef = RefName.make("refs/heads/main");
const start: Files = [ [ "package.json", '{ "name": "alice/datekit", "workspaces": ["packages/*"] }\n', ], ["packages/parser/src/parse.ts", "export const parse = (s) => ({});\n"],];
const askFiles = () => { const ask = Option.getOrThrow( Array.findFirst(scenario.policy.asks, ({ id }) => id === parseIso) );
return Array.make( { content: `export default "${parseIso}";\n`, path: ask.file }, { content: [ `import { expect, it } from "vitest";`, `import { parser } from "../src/parser.ts";`, ``, `it("${parseIso}", () => {`, ` expect(parser("2026-10-07")).toStrictEqual({ year: 2026 });`, `});`, ``, ].join("\n"), path: CheckSpec.guards.RedThenGreen(ask.check) ? ask.check.path : RepoPath.make("test/parse-iso.test.ts"), } );};
const failing = [ "FAIL packages/parser/test/parse.test.ts > parses ISO weeks", "AssertionError: expected 52 to be 53",].join("\n");
const bobsLog = logRefOf({ attempt: 1, lane: unit, project, requestId });
const mainTip = Effect.gen(function* () { const refs = yield* (yield* RepoHost).refs(repo).pipe(Effect.orDie);
return LandedSha.make( Option.getOrThrow(Option.fromNullishOr(refs.get(mainRef))) );});
const answered = (text: string = failing) => Effect.gen(function* () { yield* advanceTo("2026-10-07T12:00:00Z");
const api = yield* projectService(project);
yield* api.initProject(alice, {});
const first = yield* Pusher.use((pusher) => pusher.push(repo, Option.none(), start, "Start") );
yield* api.recordFirstPush(alice, first);
const files = askFiles(); const composed = yield* composedAt(repo, yield* mainTip, files);
yield* CheckScript.use((checker) => checker.script(composed, LaneName.make("redThenGreen"), [ "failed", "failed", ]) );
const { landed } = yield* api.openAsk(alice, { ask: parseIso, files }); const { claim } = yield* api.claim(bob, parseIso);
const head = yield* Pusher.use((pusher) => pusher.push(claim.fork, Option.some(landed), [ ...start.filter(([path]) => !path.startsWith("packages/parser/")), ["packages/parser/src/parse.ts", "export const parse = () => 2026;\n"], ...files.map(({ content, path }) => [path, content] as const), ]) );
yield* api.submitAnswer(bob, { ask: parseIso, base: landed, head, requestId, });
yield* (yield* EvidenceStore).put({ attempt: 1, lane: unit, project, requestId, text, });
return api; });
const world = Layer.merge(worldOf(scenario, "memory"), EvidenceStoreMemory);
describe("reading a failing lane's log", () => { it.effect( "gives the answer's claimant and the project's maintainers the log, whole", () => Effect.gen(function* () { const api = yield* answered();
expect({ alice: yield* api.evidence(alice, bobsLog), bob: yield* api.evidence(bob, bobsLog), }).toStrictEqual({ alice: { lane: unit, text: failing, truncated: false }, bob: { lane: unit, text: failing, truncated: false }, }); }).pipe(Effect.provide(world)) );
it.effect( "refuses everyone else NotDecider, whether or not a log is kept under the name", () => Effect.gen(function* () { const api = yield* answered();
const never = logRefOf({ attempt: 7, lane: unit, project, requestId: RequestId.make("req-none-0001"), });
expect( yield* Effect.forEach( [ api.evidence(carol, bobsLog), api.evidence(carol, never), api.evidence(Anonymous.make({}), bobsLog), api.evidence(bob, never), ], Effect.flip ) ).toStrictEqual([ new NotDecider({}), new NotDecider({}), new NotDecider({}), new NotDecider({}), ]); }).pipe(Effect.provide(world)) );
it.effect( "refuses a name that points at another project as that project unknown, for anyone", () => Effect.gen(function* () { const api = yield* answered(); const other = ProjectName.make("alice/burrow");
const elsewhere = logRefOf({ attempt: 1, lane: unit, project: other, requestId, });
expect( yield* Effect.forEach( [ api.evidence(alice, elsewhere), api.evidence(bob, elsewhere), api.evidence(carol, elsewhere), ], Effect.flip ) ).toStrictEqual([ new UnknownProject({ project: other }), new UnknownProject({ project: other }), new UnknownProject({ project: other }), ]); }).pipe(Effect.provide(world)) );
it.effect( "refuses a name that is no log's, or names no kept log, as nothing in the project", () => Effect.gen(function* () { const api = yield* answered();
const passed = logRefOf({ attempt: 1, lane: LaneName.make("lint"), project, requestId, });
// bob's own log's name, spelled as `logRefOf` never spells one. const respelled = LogRef.make(bobsLog.replace(/#1$/u, "#01"));
expect( yield* Effect.forEach( [ api.evidence(alice, LogRef.make("not a log's name")), api.evidence(carol, LogRef.make("not a log's name")), api.evidence(alice, respelled), api.evidence(carol, respelled), api.evidence(alice, passed), ], Effect.flip ) ).toStrictEqual([ new UnknownProject({ project }), new UnknownProject({ project }), new UnknownProject({ project }), new UnknownProject({ project }), new UnknownProject({ project }), ]); }).pipe(Effect.provide(world)) );
it.effect( "gives a log over 256 KiB as its last 256 KiB, where the failure prints", () => Effect.gen(function* () { const tail = "AssertionError: expected 52 to be 53\n"; const long = `${"x".repeat(300 * 1024)}${tail}`; const api = yield* answered(long); const read = yield* api.evidence(bob, bobsLog);
expect({ bytes: new TextEncoder().encode(read.text).length, endsWithFailure: read.text.endsWith(tail), isTail: long.endsWith(read.text), truncated: read.truncated, }).toStrictEqual({ bytes: 256 * 1024, endsWithFailure: true, isTail: true, truncated: true, }); }).pipe(Effect.provide(world)) );
it.effect("cuts a long log between characters, never inside one", () => Effect.gen(function* () { // Each "é" is two bytes, so 256 KiB from the end falls inside one. const long = `${"é".repeat(150 * 1024)}x`; const api = yield* answered(long); const read = yield* api.evidence(alice, bobsLog);
expect({ bytes: new TextEncoder().encode(read.text).length, isTail: long.endsWith(read.text), truncated: read.truncated, }).toStrictEqual({ bytes: 256 * 1024 - 1, isTail: true, truncated: true, }); }).pipe(Effect.provide(world)) );});