watch.test.ts
This file
Path
apps/web/test/watch.test.ts
543 lines · 15.2 KiB · TypeScript
Last changed by
Merge build/comments-web-rest into mainoscabrielbefore adoption0eb1248
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–11why, one step back1import { describe, expect, it } from "@effect/vitest";2import { ProjectEvent } from "@repo/protocol/domain/event";3import type { LandingView } from "@repo/protocol/domain/landing";4import { Outcome } from "@repo/protocol/domain/outcome";5import {6 AskId,7 CommitSha,8 Handle,9 KindName,10 LandedSha,11 ProjectName,Give the web its W9 dependencies, components, project nav, line and revision helpers, and a stub for every new pageoscabrielbefore adoption254268a
line 12why, one step back12 ProposalId,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 13why, one step back13} from "@repo/protocol/ids";Fold every page's project events once, keep Markdown off the first load, and cache what never changesoscabrielbefore adoption38e5215
line 14why, one step back14import { UnknownProject } from "@repo/protocol/refusal";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 15why, one step back15import { Clock, DateTime, Effect, Layer, Queue, Ref, Stream } from "effect";Fold every page's project events once, keep Markdown off the first load, and cache what never changesoscabrielbefore adoption38e5215
line 16why, one step back16import { AsyncResult, Atom, AtomRegistry, Reactivity } from "effect/reactivity";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–18why, one step back17import { TestClock } from "effect/testing";18Fold every page's project events once, keep Markdown off the first load, and cache what never changesoscabrielbefore adoption38e5215
line 19why, one step back19import { projectEventsAtom } from "../src/client/events.ts";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 20–27why, one step back20import { keys } from "../src/client/keys.ts";21import {22 POLL_MS,23 ProjectWatch,24 watchAtom,25 watchRuntime,26} from "../src/client/watch.ts";27Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
line 28why, one step back28const datekit = ProjectName.make("oscabriel/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 29–71why, one step back2930const parseIso = AskId.make("parse-iso");3132const at = DateTime.toUtc(DateTime.makeUnsafe("2026-10-07T12:00:00Z"));3334const head = CommitSha.make("a".repeat(40));3536const sha = LandedSha.make("b".repeat(40));3738const landing: LandingView = {39 ask: parseIso,40 assisted: [],41 author: Handle.make("alice"),42 credits: [{ handle: Handle.make("alice"), role: "Signed-off-by" }],43 landedAt: at,44 parent: LandedSha.make("c".repeat(40)),45 project: datekit,46 sha,47};4849const projectKey = "project";5051const askKey = "ask";5253const invalidations = Effect.gen(function* () {54 const reactivity = yield* Reactivity.make;55 const fired: string[] = [];5657 const listen = (key: string, kind: string) =>58 reactivity.registerUnsafe([key], () => {59 fired.push(kind);60 });6162 const [askOwn] = keys.ask(datekit, parseIso);6364 listen(keys.project(datekit).join(","), projectKey);65 listen(askOwn ?? "", askKey);6667 return { fired, reactivity };68});6970const tab = (71 follow: (since: number) => Stream.Stream<ProjectEvent, string>,Fold every page's project events once, keep Markdown off the first load, and cache what never changesoscabrielbefore adoption38e5215
lines 72–75why, one step back72 reactivity: Reactivity.Reactivity,73 replay: Atom.Type<ReturnType<typeof projectEventsAtom>> = AsyncResult.success(74 []75 )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 76–83why, one step back76) =>77 Effect.gen(function* () {78 const clock = yield* Clock.Clock;7980 const registry = yield* Effect.acquireRelease(81 Effect.sync(() =>82 AtomRegistry.make({83 initialValues: [Fold every page's project events once, keep Markdown off the first load, and cache what never changesoscabrielbefore adoption38e5215
line 84why, one step back84 Atom.initialValue(projectEventsAtom(datekit), replay),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 85–176why, one step back85 Atom.initialValue(86 watchRuntime.layer,87 Layer.mergeAll(88 Layer.succeed(ProjectWatch, {89 follow: (_project, since) => follow(since),90 }),91 Layer.succeed(Reactivity.Reactivity, reactivity),92 Layer.succeed(Clock.Clock, clock)93 )94 ),95 ],96 })97 ),98 (made) =>99 Effect.sync(() => {100 made.dispose();101 })102 );103104 registry.mount(watchAtom(datekit));105 });106107const runUntil = (done: () => boolean) =>108 Effect.yieldNow.pipe(Effect.repeat({ times: 200, until: done }));109110const firedNow = (fired: string[]) =>111 Effect.gen(function* () {112 yield* runUntil(() => fired.length > 0);113114 return fired.splice(0).toSorted();115 });116117describe(watchAtom, () => {118 it.effect(119 "invalidates the project's keys on AskOpened, AskClosed, Claimed and Landed, and the ask's on Pushed and Judged",120 () =>121 Effect.gen(function* () {122 const { fired, reactivity } = yield* invalidations;123 const events = yield* Queue.unbounded<ProjectEvent>();124125 yield* tab(() => Stream.fromQueue(events), reactivity);126127 const each = [128 ProjectEvent.cases.AskOpened.make({129 ask: {130 ask: parseIso,131 blockedBy: [],132 claimed: false,133 kind: KindName.make("fix"),134 territory: "src/parse.ts",135 want: "parse reads the year",136 },137 at,138 seq: 1,139 }),140 ProjectEvent.cases.Claimed.make({141 ask: parseIso,142 at,143 claimant: Handle.make("alice"),144 seq: 2,145 }),146 ProjectEvent.cases.Pushed.make({ ask: parseIso, at, head, seq: 3 }),147 ProjectEvent.cases.Judged.make({148 ask: parseIso,149 at,150 head,151 outcome: Outcome.cases.Checking.make({ stage: "merge" }),152 seq: 4,153 }),154 ProjectEvent.cases.Landed.make({ at, landing, seq: 5 }),155 ProjectEvent.cases.AskClosed.make({156 ask: parseIso,157 at,158 landing: sha,159 seq: 6,160 }),161 ];162163 const heard: Record<string, readonly string[]> = {};164165 for (const event of each) {166 yield* Queue.offer(events, event);167 heard[event._tag] = yield* firedNow(fired);168 }169170 expect(heard).toStrictEqual({171 AskClosed: [projectKey],172 AskOpened: [projectKey],173 Claimed: [projectKey],174 Judged: [askKey, projectKey],175 Landed: [projectKey],176 Pushed: [askKey, projectKey],Give the web its W9 dependencies, components, project nav, line and revision helpers, and a stub for every new pageoscabrielbefore adoption254268a
lines 177–222why, one step back177 });178 })179 );180181 it.effect(182 "invalidates the code and policy reads on Landed, and the proposal's on ProposalDrafted",183 () =>184 Effect.gen(function* () {185 const reactivity = yield* Reactivity.make;186 const fired: string[] = [];187 const proposal = ProposalId.make("prop-000001");188189 const listen = (key: string | undefined, kind: string) =>190 reactivity.registerUnsafe([key ?? ""], () => {191 fired.push(kind);192 });193194 listen(keys.code(datekit)[0], "code");195 listen(keys.policy(datekit)[0], "policy");196 listen(keys.proposal(datekit, proposal)[0], "proposal");197 listen(keys.project(datekit)[0], projectKey);198199 const events = yield* Queue.unbounded<ProjectEvent>();200201 yield* tab(() => Stream.fromQueue(events), reactivity);202203 yield* Queue.offer(204 events,205 ProjectEvent.cases.Landed.make({ at, landing, seq: 1 })206 );207208 const landed = yield* firedNow(fired);209210 yield* Queue.offer(211 events,212 ProjectEvent.cases.ProposalDrafted.make({213 at,214 proposal,215 ready: true,216 seq: 2,217 })218 );219220 expect({ drafted: yield* firedNow(fired), landed }).toStrictEqual({221 drafted: [projectKey, "proposal"],222 landed: ["code", "policy", projectKey],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 223–226why, one step back223 });224 })225 );226Merge fix/w12-reviewoscabrielbefore adoption7b8b393
lines 227–288why, one step back227 // The rail lists every project with its open asks and last landing228 // (`projectsAtom`, read under `keys.front()`): an event that changes229 // them reads the list again, as one that leaves them alone does not.230 it.effect(231 "invalidates the project list on AskOpened, AskClosed and Landed, and not on Claimed",232 () =>233 Effect.gen(function* () {234 const reactivity = yield* Reactivity.make;235 const fired: string[] = [];236237 reactivity.registerUnsafe([keys.front()[0] ?? ""], () => {238 fired.push("front");239 });240241 const events = yield* Queue.unbounded<ProjectEvent>();242243 yield* tab(() => Stream.fromQueue(events), reactivity);244245 const each = [246 ProjectEvent.cases.AskOpened.make({247 ask: {248 ask: parseIso,249 blockedBy: [],250 claimed: false,251 kind: KindName.make("fix"),252 territory: "src/parse.ts",253 want: "parse reads the year",254 },255 at,256 seq: 1,257 }),258 ProjectEvent.cases.Claimed.make({259 ask: parseIso,260 at,261 claimant: Handle.make("alice"),262 seq: 2,263 }),264 ProjectEvent.cases.Landed.make({ at, landing, seq: 3 }),265 ProjectEvent.cases.AskClosed.make({266 ask: parseIso,267 at,268 landing: sha,269 seq: 4,270 }),271 ];272273 const heard: Record<string, readonly string[]> = {};274275 for (const event of each) {276 yield* Queue.offer(events, event);277 heard[event._tag] = yield* firedNow(fired);278 }279280 expect(heard).toStrictEqual({281 AskClosed: ["front"],282 AskOpened: ["front"],283 Claimed: [],284 Landed: ["front"],285 });286 })287 );288Merge 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 289–356why, one step back289 it.effect(290 "polls every POLL_MS when the stream fails at connect, invalidating the project's keys",291 () =>292 Effect.gen(function* () {293 const { fired, reactivity } = yield* invalidations;294295 yield* tab(() => Stream.fail("no framed /rpc here"), reactivity);296 yield* runUntil(() => false);297298 const before = [...fired];299300 yield* TestClock.adjust(POLL_MS - 1);301 yield* runUntil(() => false);302303 const early = [...fired];304305 yield* TestClock.adjust(1);306307 const first = yield* firedNow(fired);308309 yield* TestClock.adjust(POLL_MS);310311 expect({312 before,313 early,314 first,315 second: yield* firedNow(fired),316 }).toStrictEqual({317 before: [],318 early: [],319 first: [projectKey],320 second: [projectKey],321 });322 })323 );324325 it.effect(326 "follows again from the last number it heard, POLL_MS after a stream that heard something ends",327 () =>328 Effect.gen(function* () {329 const { fired, reactivity } = yield* invalidations;330 const asked = yield* Ref.make<readonly number[]>([]);331332 yield* tab(333 (since) =>334 Stream.unwrap(335 Effect.as(336 Ref.update(asked, (all) => [...all, since]),337 since === 0338 ? Stream.make(339 ProjectEvent.cases.Claimed.make({340 ask: parseIso,341 at,342 claimant: Handle.make("alice"),343 seq: 7,344 })345 )346 : Stream.never347 )348 ),349 reactivity350 );351352 yield* firedNow(fired);353 yield* TestClock.adjust(POLL_MS);354 yield* runUntil(() => Ref.getUnsafe(asked).length > 1);355356 expect(yield* Ref.get(asked)).toStrictEqual([0, 7]);Fold every page's project events once, keep Markdown off the first load, and cache what never changesoscabrielbefore adoption38e5215
lines 357–417why, one step back357 })358 );359360 it.effect(361 "follows from the last number of the replay the page read, not from the first event",362 () =>363 Effect.gen(function* () {364 const { reactivity } = yield* invalidations;365 const asked = yield* Ref.make<readonly number[]>([]);366367 yield* tab(368 (since) =>369 Stream.unwrap(370 Effect.as(371 Ref.update(asked, (all) => [...all, since]),372 Stream.never373 )374 ),375 reactivity,376 AsyncResult.success(377 Array.from({ length: 7 }, (_, index) =>378 ProjectEvent.cases.Warned.make({379 ask: parseIso,380 at,381 seq: index + 1,382 })383 )384 )385 );386387 yield* runUntil(() => Ref.getUnsafe(asked).length > 0);388389 expect(yield* Ref.get(asked)).toStrictEqual([7]);390 })391 );392393 it.effect(394 "hears once an event that came between the replay and the follow",395 () =>396 Effect.gen(function* () {397 const { fired, reactivity } = yield* invalidations;398399 const kept = Array.from({ length: 8 }, (_, index) =>400 ProjectEvent.cases.Claimed.make({401 ask: parseIso,402 at,403 claimant: Handle.make("alice"),404 seq: index + 1,405 })406 );407408 yield* tab(409 (since) =>410 Stream.concat(411 Stream.fromIterable(kept.filter(({ seq }) => seq > since)),412 Stream.never413 ),414 reactivity,415 AsyncResult.success(kept.slice(0, 7))416 );417Review fixes for 1202oscabrielbefore adoption512af5a
lines 418–460why, one step back418 // Once for the page's read after the replay, once for the event.419 yield* runUntil(() => fired.length > 1);420 yield* runUntil(() => fired.length > 2);421422 expect(fired).toStrictEqual([projectKey, projectKey]);423 })424 );425426 // A page's own reads may be older than the replay (a loader read them on427 // the server; the replay is read in the browser later): what happened in428 // between is in the replay but not on the page, and the stream starts429 // after it.430 it.effect(431 "reads the page once more after the replay, for what came between the page's read and the replay, and never the replay itself",432 () =>433 Effect.gen(function* () {434 const { fired, reactivity } = yield* invalidations;435 const [events] = keys.events(datekit);436437 reactivity.registerUnsafe([events ?? ""], () => {438 fired.push("events");439 });440441 yield* tab(442 () => Stream.never,443 reactivity,444 AsyncResult.success([445 ProjectEvent.cases.Claimed.make({446 ask: parseIso,447 at,448 claimant: Handle.make("alice"),449 seq: 1,450 }),451 ProjectEvent.cases.Judged.make({452 ask: parseIso,453 at,454 head,455 outcome: Outcome.cases.Checking.make({ stage: "merge" }),456 seq: 2,457 }),458 ])459 );460Fold every page's project events once, keep Markdown off the first load, and cache what never changesoscabrielbefore adoption38e5215
lines 461–466why, one step back461 const heard = yield* firedNow(fired);462463 yield* runUntil(() => fired.length > 0);464465 expect({ after: [...fired], heard }).toStrictEqual({466 after: [],Review fixes for 1202oscabrielbefore adoption512af5a
lines 467–514why, one step back467 heard: [askKey, projectKey],468 });469 })470 );471472 it.effect(473 "reads the replay again on each event the stream brings, and on each poll",474 () =>475 Effect.gen(function* () {476 const reactivity = yield* Reactivity.make;477 const fired: string[] = [];478 const [events] = keys.events(datekit);479480 reactivity.registerUnsafe([events ?? ""], () => {481 fired.push("events");482 });483484 const heard = yield* Queue.unbounded<ProjectEvent>();485 const connects = yield* Ref.make(0);486487 yield* tab(488 () =>489 Stream.unwrap(490 Effect.map(491 Ref.getAndUpdate(connects, (count) => count + 1),492 (count) =>493 count === 0494 ? Stream.fromQueue(heard).pipe(Stream.take(1))495 : Stream.fail("no framed /rpc here")496 )497 ),498 reactivity499 );500501 yield* Queue.offer(502 heard,503 ProjectEvent.cases.Warned.make({ ask: parseIso, at, seq: 1 })504 );505506 const onEvent = yield* firedNow(fired);507508 yield* TestClock.adjust(POLL_MS);509 yield* runUntil(() => Ref.getUnsafe(connects) > 1);510 yield* TestClock.adjust(POLL_MS);511512 expect({ onEvent, onPoll: yield* firedNow(fired) }).toStrictEqual({513 onEvent: ["events"],514 onPoll: ["events"],Fold every page's project events once, keep Markdown off the first load, and cache what never changesoscabrielbefore adoption38e5215
lines 515–540why, one step back515 });516 })517 );518519 it.effect(520 "follows from the first event when the replay could not be read",521 () =>522 Effect.gen(function* () {523 const { reactivity } = yield* invalidations;524 const asked = yield* Ref.make<readonly number[]>([]);525526 yield* tab(527 (since) =>528 Stream.unwrap(529 Effect.as(530 Ref.update(asked, (all) => [...all, since]),531 Stream.never532 )533 ),534 reactivity,535 AsyncResult.fail(new UnknownProject({ project: datekit }))536 );537538 yield* runUntil(() => Ref.getUnsafe(asked).length > 0);539540 expect(yield* Ref.get(asked)).toStrictEqual([0]);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 541–543why, one step back541 })542 );543});