Skip to content
Myr

evidence.test.ts

This file

Path

apps/platform/test/evidence.test.ts

373 lines · 10.9 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–15why, one step back
1import { describe, expect, expectTypeOf, it } from "@effect/vitest";
2import { Brand } from "@repo/brand";3import { CheckAt } from "@repo/checks/check-runner";4import { EvidenceStore } from "@repo/checks/evidence-store";5import { EvidenceStoreR2 } from "@repo/checks/evidence-store/r2";6import type { EvidenceBucket } from "@repo/checks/evidence-store/r2";7import { Outcome } from "@repo/protocol/domain/outcome";8import {9 AskId,10 CommitSha,11 Handle,12 LaneName,13 ProjectName,14 RepoPath,15 RequestId,

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 16why, one step back
16 keyOf,

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 17–32why, one step back
17 TreeSha,
18} from "@repo/protocol/ids";19import { Unauthenticated } from "@repo/protocol/principal";20import {21 ChecksFailed,22 NotDecider,23 UnknownProject,24} from "@repo/protocol/refusal";25import { Array, Cause, Effect, Exit, Stream } from "effect";2627import { decodeEnv } from "../src/platform.ts";28import { workerPorts } from "../src/ports.ts";29import { fakeEnv } from "./fake-env.ts";30import { coreLoopWorld } from "./world.ts";31import type { Someone } from "./world.ts";32

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 33why, one step back
33const project = ProjectName.make("dora/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 34–54why, one step back
34
35const parseIso = AskId.make("parse-iso");3637const redThenGreen = LaneName.make("redThenGreen");3839const unit = LaneName.make("unit");4041const sdk = `${Brand.cliPackage}/rules`;4243const start = {44 [`${Brand.configDir}/project.ts`]: [45 `import { kinds, project } from "${sdk}";`,46 ``,47 `export default project({`,48 ` kinds: [kinds.fix],`,49 ` run: { checks: { unit: "bun test" }, test: "bun test {path}" },`,50 `});`,51 ``,52 ].join("\n"),53 [`${Brand.configDir}/vouched.td`]: "alice\nbob\n",54 "README.md": "datekit: dates, parsed\n",

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 55why, one step back
55 "package.json": '{ "name": "dora/datekit", "private": true }\n',

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 56–231why, one step back
56 "src/parse.ts":
57 "export const parse = (iso: string) => ({ month: 0, year: 0 });\n",58};5960const askFiles = Array.make(61 {62 content: [63 `import { kinds, territory } from "${sdk}";`,64 ``,65 `export default kinds.fix.ask({`,66 ` path: "test/${parseIso}.test.ts",`,67 ` territory: territory.paths("src/parse.ts"),`,68 ` want: "parse reads the year of an ISO date",`,69 `});`,70 ``,71 ].join("\n"),72 path: RepoPath.make(`${Brand.configDir}/asks/${parseIso}.ask.ts`),73 },74 {75 content: [76 `import { expect, it } from "bun:test";`,77 `import { parse } from "../src/parse.ts";`,78 ``,79 `it("parse reads the year of an ISO date", () => {`,80 ` expect(parse("2026-10-07").year).toBe(2026);`,81 `});`,82 ``,83 ].join("\n"),84 path: RepoPath.make(`test/${parseIso}.test.ts`),85 }86);8788const unitLog = [89 "unit: failed in 1 ms",90 "$ run unit",91 "unit: scripted failure",92].join("\n");9394interface Kept {95 readonly text: string;96 readonly customMetadata: Readonly<Record<string, string>>;97}9899const fakeBucket = () => {100 const objects = new Map<string, Kept>();101102 const bucket: EvidenceBucket = {103 get: (key) => {104 const kept = objects.get(key);105106 return Promise.resolve(107 kept === undefined ? null : { text: () => Promise.resolve(kept.text) }108 );109 },110 put: (key, text, { customMetadata }) => {111 objects.set(key, { customMetadata, text });112113 return Promise.resolve({ key });114 },115 };116117 return { bucket, objects };118};119120const refused = Effect.gen(function* () {121 const { bucket, objects } = fakeBucket();122 const world = yield* coreLoopWorld({ evidence: EvidenceStoreR2(bucket) });123 const dora = world.rpc("dora");124 const alice = world.rpc("alice");125 const doraGit = world.git("dora");126 const aliceGit = world.git("alice");127128 const made = yield* dora.initProject({ project });129 const home = yield* doraGit.clone(made.remote);130131 yield* doraGit.commit(home, "Start datekit", start);132 yield* doraGit.push(home);133134 yield* world.script(135 CheckAt.cases.Commit.make({136 commit: yield* world.composed(project, askFiles),137 }),138 redThenGreen,139 ["failed", "failed"]140 );141142 const opened = yield* dora.openAsk({143 ask: parseIso,144 files: askFiles,145 project,146 });147148 const { claim } = yield* alice.claim({ ask: parseIso, project });149 const work = yield* aliceGit.clone(claim.remote);150151 const fix = yield* aliceGit.commit(work, "Read the year", {152 "src/parse.ts":153 "export const parse = (iso: string) => ({ month: 0, year: Number(iso.slice(0, 4)) });\n",154 });155156 const pushed = yield* aliceGit.push(work);157158 const tree = TreeSha.make(159 yield* aliceGit.ok(work, "rev-parse", `${fix}^{tree}`)160 );161162 const merged = CheckAt.cases.Tree.make({ tree });163164 yield* world.script(merged, redThenGreen, ["passed"]);165 yield* world.script(merged, unit, ["failed"]);166 yield* world.script(167 CheckAt.cases.Commit.make({ commit: CommitSha.make(opened.landed) }),168 unit,169 ["passed"]170 );171172 yield* world.settle();173174 const { verdict } = yield* alice.verdict({ ask: parseIso, project });175176 const logRef = verdict?.lanes.find(({ lane }) => lane === unit)?.logRef ?? "";177178 return { aliceGit, logRef, objects, pushed, verdict, work, world };179});180181const json = <A, E>(read: Effect.Effect<A, E>) =>182 Effect.map(Effect.exit(read), (exit) => JSON.stringify(exit));183184const everythingElse = (185 world: Effect.Success<typeof refused>["world"],186 who: Someone | "anonymous"187) =>188 Effect.gen(function* () {189 const rpc = world.rpc(who);190191 return [192 yield* json(rpc.listProjects({})),193 yield* json(rpc.frontPage({})),194 yield* json(rpc.listAsks({ project })),195 yield* json(rpc.showAsk({ ask: parseIso, project })),196 yield* json(rpc.verdict({ ask: parseIso, project })),197 yield* json(rpc.credits({ handle: Handle.make("alice") })),198 yield* json(rpc.ledger({ project })),199 yield* json(rpc.envShape({ project })),200 yield* json(rpc.status({ project })),201 yield* json(rpc.today({ project })),202 yield* json(Stream.runCollect(rpc.watch({ project }))),203 ];204 });205206describe("a failing lane's log, through the platform", () => {207 it("takes the Worker's EVIDENCE binding as its bucket", () => {208 expectTypeOf<Env["EVIDENCE"]>().toExtend<EvidenceBucket>();209 });210211 it.effect(212 "keeps the Worker's lane logs in its EVIDENCE bucket, not in memory",213 () =>214 Effect.gen(function* () {215 const config = yield* decodeEnv(fakeEnv()).pipe(Effect.orDie);216217 const exit = yield* Effect.exit(218 EvidenceStore.use((store) =>219 store.put({220 attempt: 1,221 lane: unit,222 project,223 requestId: RequestId.make("req-fix-0001"),224 text: unitLog,225 })226 ).pipe(Effect.provide(workerPorts(config)))227 );228229 // The fake env's EVIDENCE throws when touched: the store reached it.230 expect(231 Exit.isFailure(exit) ? Cause.pretty(exit.cause) : "it kept the log"

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 232why, one step back
232 ).toContain("could not put dora__datekit/req-fix-0001/unit#1");

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 233–246why, one step back
233 })
234 );235236 it.live(237 "keeps the log of a lane that failed, and the refusal names only where",238 () =>239 Effect.gen(function* () {240 const { logRef, objects, verdict } = yield* refused;241242 expect({243 kept: objects.get(logRef),244 outcome: verdict?.outcome,245 }).toStrictEqual({246 kept: {

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 247why, one step back
247 customMetadata: { lane: "unit", project: "dora/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 248–260why, one step back
248 text: unitLog,
249 },250 outcome: Outcome.cases.Refused.make({251 refusals: [252 new ChecksFailed({ lane: unit, logRef, next: "run unit" }),253 ],254 }),255 });256 }),257 60_000258 );259260 it.live(

Merge build/maintainer-wording into mainoscabrielbefore adoption4c1a571

line 261why, one step back
261 "gives the log to the answer's claimant and to a maintainer, and refuses everyone else",

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 262–271why, one step back
262 () =>
263 Effect.gen(function* () {264 const { logRef, objects, world } = yield* refused;265 const ref = { project, ref: logRef };266267 const asked = (who: Someone | "anonymous", payload = ref) =>268 Effect.exit(world.rpc(who).evidence(payload));269270 yield* world271 .rpc("bob")

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 272why, one step back
272 .initProject({ project: ProjectName.make("bob/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 273–275why, one step back
273
274 const elsewhere = (other: string) => ({275 project,

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

lines 276–279why, one step back
276 ref: logRef.replace(
277 `${keyOf(project)}/`,278 `${keyOf(ProjectName.make(other))}/`279 ),

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 280–287why, one step back
280 });
281282 const read = {283 alice: yield* asked("alice"),284 anonymous: yield* asked("anonymous"),285 bob: yield* asked("bob"),286 dora: yield* asked("dora"),287 ghost: yield* asked("dora", {

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

lines 288–289why, one step back
288 project: ProjectName.make("alice/ghost"),
289 ref: logRef.replace(`${keyOf(project)}/`, "alice__ghost/"),

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

line 290why, one step back
290 }),

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

lines 291–292why, one step back
291 inBurrow: yield* asked("dora", elsewhere("bob/burrow")),
292 inGhost: yield* asked("dora", elsewhere("alice/ghost")),

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 293–302why, one step back
293 };
294295 const whole = { lane: unit, text: unitLog, truncated: false };296297 expect(read).toStrictEqual({298 alice: Exit.succeed(whole),299 anonymous: Exit.fail(new Unauthenticated()),300 bob: Exit.fail(new NotDecider({})),301 dora: Exit.succeed(whole),302 ghost: Exit.fail(

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 303why, one step back
303 new UnknownProject({ project: ProjectName.make("alice/ghost") })

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 304–305why, one step back
304 ),
305 inBurrow: Exit.fail(

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

lines 306–308why, one step back
306 new UnknownProject({
307 project: ProjectName.make("bob/burrow"),308 })

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 309–310why, one step back
309 ),
310 inGhost: Exit.fail(

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 311why, one step back
311 new UnknownProject({ project: ProjectName.make("alice/ghost") })

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 312–373why, one step back
312 ),
313 });314315 const long = `${"x".repeat(300 * 1024)}\n${unitLog}`;316 const kept = objects.get(logRef);317318 if (kept !== undefined) {319 objects.set(logRef, { ...kept, text: long });320 }321322 const cut = yield* world.rpc("dora").evidence(ref);323324 expect({325 bytes: new TextEncoder().encode(cut.text).length,326 isTail: long.endsWith(cut.text),327 truncated: cut.truncated,328 }).toStrictEqual({ bytes: 256 * 1024, isTail: true, truncated: true });329 }),330 60_000331 );332333 it.live(334 "never puts the log in another /rpc response or in git's remote: lines",335 () =>336 Effect.gen(function* () {337 const { aliceGit, pushed, work, world } = yield* refused;338339 const read = [340 ...(yield* everythingElse(world, "alice")),341 ...(yield* everythingElse(world, "bob")),342 ...(yield* everythingElse(world, "dora")),343 ...(yield* everythingElse(world, "anonymous")),344 ];345346 yield* aliceGit.commit(work, "Read the year, trimmed", {347 "src/parse.ts":348 "export const parse = (iso: string) => ({ month: 0, year: Number(iso.slice(0, 4).trim()) });\n",349 });350351 const again = yield* aliceGit.push(work);352353 const needles = [354 unitLog,355 JSON.stringify(unitLog).slice(1, -1),356 "unit: scripted failure",357 ];358359 const seen = [pushed.stderr, again.stderr, ...read];360361 expect({362 again: {363 exitCode: again.exitCode,364 reached: again.stderr.includes("remote:"),365 },366 leaks: seen.filter((text) =>367 needles.some((needle) => text.includes(needle))368 ),369 }).toStrictEqual({ again: { exitCode: 0, reached: true }, leaks: [] });370 }),371 60_000372 );373});