project-do.test.ts
This file
Path
apps/platform/test/project-do.test.ts
267 lines · 8.5 KiB · TypeScript
Last changed by
Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8
History of this fileTake 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–250why, one step back1import { describe, expect, it } from "@effect/vitest";2import { AnswerRuns } from "@repo/project/answer-runs";3import type { AnswerQueue } from "@repo/project/answer-runs";4import { ProjectGenerator } from "@repo/project/generated";5import { RequestId } from "@repo/protocol/ids";6import { UnknownProject } from "@repo/protocol/refusal";7import { advanceTo, at } from "@repo/testkit/clock";8import { untouchable } from "@repo/testkit/untouchable";9import { Context, DateTime, Effect, Layer, Option } from "effect";1011import { AnswerRunsWorkflow } from "../src/adapters/answer-runs-workflow.ts";12import { decodeEnv } from "../src/platform.ts";13import { doPorts, Project } from "../src/project-do.ts";14import { generate } from "../src/workflows/generated.ts";15import { fakeEnv } from "./fake-env.ts";16import {17 alice,18 answerWorkflow,19 anyone,20 bob,21 databaseFile,22 fixedFiles,23 forkOf,24 Instances,25 opening,26 outcomeOf,27 parseIso,28 project,29 Pusher,30 SetAlarms,31 shellOver,32 through,33 world,34} from "./project-do.fixtures.ts";3536const noon = "2026-10-07T12:00:00Z";3738const epochOf = (iso: string) => DateTime.toEpochMillis(at(iso));3940describe("a Project DO's shell", () => {41 it.effect(42 "refuses a read of a project never made with UnknownProject, typed across the boundary",43 () =>44 Effect.gen(function* () {45 const call = through(yield* shellOver(":memory:"));4647 expect(yield* outcomeOf(call("listAsks", [anyone]))).toStrictEqual({48 refused: new UnknownProject({ project }),49 });50 }).pipe(Effect.provide(world))51 );5253 it.effect(54 "persists what a call wrote, so a shell over the same SQLite hydrates it",55 () =>56 Effect.gen(function* () {57 yield* advanceTo(noon);5859 const file = yield* databaseFile;60 const call = through(yield* shellOver(file));61 const { ask } = yield* opening(call);62 const listed = yield* call("listAsks", [anyone]);63 const again = through(yield* shellOver(file));6465 expect(listed.asks.map(({ ask: id }) => id)).toStrictEqual([ask.id]);66 expect(yield* call("listAsks", [anyone])).toStrictEqual(listed);67 expect(yield* again("listAsks", [anyone])).toStrictEqual(listed);68 }).pipe(Effect.provide(world))69 );7071 it.effect(72 "sets the DO's alarm from a claim's deadline, and lapses the claim when it fires late",73 () =>74 Effect.gen(function* () {75 yield* advanceTo(noon);7677 const shell = yield* shellOver(":memory:");78 const call = through(shell);7980 yield* opening(call);81 yield* call("claim", [bob, parseIso]);8283 const alarm = yield* SetAlarms.use(({ all }) => all);8485 yield* advanceTo("2026-10-08T12:00:00Z");86 yield* shell.alarm();8788 const { claims } = yield* call("status", [bob]);8990 expect(alarm.at(-1)).toBe(epochOf("2026-10-07T14:00:00Z"));91 expect(claims.map(({ state }) => state)).toStrictEqual(["lapsed"]);92 }).pipe(Effect.provide(world))93 );9495 it.effect(96 "starts one Answer Workflow instance per accepted answer, and none for its replay",97 () =>98 Effect.gen(function* () {99 yield* advanceTo(noon);100101 const call = through(yield* shellOver(":memory:"));102 const { landed } = yield* opening(call);103104 yield* call("claim", [bob, parseIso]);105106 const head = yield* Pusher.use((git) =>107 git.push(forkOf(bob), Option.some(landed), fixedFiles)108 );109110 const requestId = RequestId.make("req-fix0001");111 const answer = { ask: parseIso, base: landed, head, requestId };112 const receipt = yield* call("submitAnswer", [bob, answer]);113 const startedOnce = yield* Instances.use(({ made }) => made);114 const replay = yield* call("submitAnswer", [bob, answer]);115116 expect(receipt.outcome._tag).toBe("Checking");117 expect(startedOnce).toStrictEqual([118 { id: requestId, params: { project, requestId } },119 ]);120 expect(replay).toStrictEqual(receipt);121 expect(yield* Instances.use(({ made }) => made)).toStrictEqual(122 startedOnce123 );124 expect(yield* Instances.use(({ tries }) => tries)).toBe(1);125 }).pipe(Effect.provide(world))126 );127128 it.effect(129 "takes a nameless DO's name from the project its storage holds, and dies with neither",130 () =>131 Effect.gen(function* () {132 yield* advanceTo(noon);133134 const file = yield* databaseFile;135 const empty = through(yield* shellOver(":memory:", Option.none()));136137 expect(yield* outcomeOf(empty("listAsks", [anyone]))).toBe("died");138139 const call = through(yield* shellOver(file));140 const { ask } = yield* opening(call);141 const listed = yield* call("listAsks", [anyone]);142 const nameless = through(yield* shellOver(file, Option.none()));143144 expect(listed.asks.map(({ ask: id }) => id)).toStrictEqual([ask.id]);145 expect(yield* outcomeOf(nameless("listAsks", [anyone]))).toStrictEqual({146 value: listed,147 });148 }).pipe(Effect.provide(world))149 );150151 it.effect("wakes a DO no project was made in without failing", () =>152 Effect.gen(function* () {153 const shell = yield* shellOver(":memory:");154155 yield* shell.alarm();156157 expect(yield* outcomeOf(through(shell)("status", [alice]))).toStrictEqual(158 { refused: new UnknownProject({ project }) }159 );160 }).pipe(Effect.provide(world))161 );162});163164const refused = () => Promise.reject(new Error("Workflows are down"));165166describe("the Answer Workflow as AnswerRuns", () => {167 const queue = untouchable<AnswerQueue>("AnswerQueue");168169 it.effect("makes one instance for a request started twice", () =>170 Effect.gen(function* () {171 const { instances, workflow } = answerWorkflow();172 const requestId = RequestId.make("req-twice0001");173174 const runs = yield* AnswerRuns.pipe(175 Effect.provide(AnswerRunsWorkflow(workflow, project))176 );177178 yield* runs.start(requestId, queue);179 yield* runs.start(requestId, queue);180181 expect(yield* instances.made).toStrictEqual([182 { id: requestId, params: { project, requestId } },183 ]);184 expect(yield* instances.tries).toBe(2);185 })186 );187188 it.effect(189 "runs an instance again when its run has ended, and leaves a live one alone",190 () =>191 Effect.gen(function* () {192 const { instances, workflow } = answerWorkflow();193 const live = RequestId.make("req-live0001");194195 const ended = [196 RequestId.make("req-done0001"),197 RequestId.make("req-fail0001"),198 RequestId.make("req-stop0001"),199 ] as const;200201 const runs = yield* AnswerRuns.pipe(202 Effect.provide(AnswerRunsWorkflow(workflow, project))203 );204205 yield* Effect.forEach([live, ...ended], (id) => runs.start(id, queue));206 yield* instances.end(live, "waiting");207 yield* instances.end(ended[0], "complete");208 yield* instances.end(ended[1], "errored");209 yield* instances.end(ended[2], "terminated");210 yield* Effect.forEach([live, ...ended], (id) => runs.start(id, queue));211212 expect({213 made: (yield* instances.made).length,214 restarts: yield* instances.restarts,215 }).toStrictEqual({ made: 4, restarts: [...ended] });216 })217 );218219 it.effect(220 "dies when the Workflow can neither make nor find the instance",221 () =>222 Effect.gen(function* () {223 const runs = yield* AnswerRuns.pipe(224 Effect.provide(225 AnswerRunsWorkflow({ create: refused, get: refused }, project)226 )227 );228229 expect(230 yield* outcomeOf(runs.start(RequestId.make("req-down0001"), queue))231 ).toBe("died");232 })233 );234});235236describe("the Project DO class", () => {237 it("has call and alarm, and the hibernation API for its watchers: every project call goes through call", () => {238 expect(239 Object.getOwnPropertyNames(Project.prototype).toSorted()240 ).toStrictEqual([241 "alarm",242 "call",243 "constructor",244 "fetch",245 "webSocketClose",246 "webSocketMessage",247 ]);248 });249250 it.effect(Drop the private build plan's citations from the code, and fail verify if one comes backoscabrielbefore adoptiona29cef6
line 251why, one step back251 "runs on ports that carry the platform's generator, so a landing that changes the config is followed",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 252–267why, one step back252 () =>253 Effect.gen(function* () {254 const config = yield* decodeEnv(fakeEnv()).pipe(Effect.orDie);255256 const ports = yield* Layer.build(257 doPorts({258 config,259 name: Option.some(project),260 storage: untouchable("storage"),261 })262 );263264 expect(Context.get(ports, ProjectGenerator)).toBe(generate);265 })266 );267});