client-today.test.ts
This file
Path
apps/web/test/client-today.test.ts
131 lines · 4.2 KiB · TypeScript
Last changed by
Merge build/maintainer-wording into mainoscabrielbefore adoption4c1a571
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132import { describe, expect, it } from "@effect/vitest";import { DecisionSubject } from "@repo/protocol/domain/decision";import { Unauthenticated } from "@repo/protocol/principal";import { Array, DateTime, Effect, Exit, Option, Schema } from "effect";
import { keys } from "../src/client/keys.ts";import { ContractsClient } from "../src/surfaces.ts";import { DECIDER, registryAs, seededWorld, settled } from "./dev-browser.ts";
const todayAtom = ContractsClient.query( "today", {}, { reactivityKeys: keys.today() });
describe("Today, through the web's atoms", seededWorld, () => { it.live("keeps Today from an anonymous browser", () => Effect.gen(function* () { const anonymous = yield* settled(registryAs(), todayAtom);
const refused = Exit.isFailure(anonymous) ? Option.getOrUndefined(Exit.findErrorOption(anonymous)) : undefined;
expect(Schema.is(Unauthenticated)(refused)).toBeTruthy(); }) );
it.live("shows the maintainer their Today, with the seed's decisions", () => Effect.gen(function* () { const today = yield* settled(registryAs(DECIDER), todayAtom);
expect( Exit.isSuccess(today) ? { budget: today.value.budget, decider: today.value.decider, newcomers: today.value.newcomers, projects: Array.dedupe( today.value.decisions.map(({ project }) => project) ), subjects: today.value.decisions .map(({ subject }) => subject._tag) .toSorted(), unattended: today.value.unattended.map(({ ask }) => ask), } : undefined ).toStrictEqual({ budget: { finishes: 0, total: 5, used: 2 }, decider: "ines-v", newcomers: ["bram"], projects: ["oscabriel/datekit"], subjects: ["OpenProposal", "OpenProposal", "StrayPush"], unattended: ["diff-in-days-dst"], }); }) );
// The page's atoms live in a registry of the page's own (`SeededAtoms`), // the rail's in the root's: a decision taken on the page refetches both. it.live( "closes a decision the maintainer declines, and Today refetches without it, on the page and in the rail", () => Effect.gen(function* () { const registry = registryAs(DECIDER); const rail = registryAs(DECIDER); const unmount = registry.mount(todayAtom); const unmountRail = rail.mount(todayAtom); const before = yield* settled(registry, todayAtom); // Read in the rail before the decision, so a refetch is what changes it. yield* settled(rail, todayAtom);
const proposal = Exit.isSuccess(before) ? before.value.decisions.find(({ subject }) => DecisionSubject.guards.OpenProposal(subject) ) : undefined;
expect(proposal).toBeDefined();
if (proposal === undefined) { return; }
const decide = ContractsClient.mutation("decide");
registry.set(decide, { payload: { choice: "decline", decision: proposal.id, project: proposal.project, }, reactivityKeys: [...keys.today(), ...keys.project(proposal.project)], });
const decided = yield* settled(registry, decide);
const taken = Exit.isSuccess(decided) ? decided.value.decision.decided : undefined;
expect(taken?.choice).toBe("decline"); expect( taken === undefined ? undefined : DateTime.formatIso(taken.at) ).toBe("2026-10-07T09:21:00.000Z");
const todayIn = (where: typeof registry) => Effect.map(settled(where, todayAtom), (today) => Exit.isSuccess(today) ? { holds: today.value.decisions.some( ({ id }) => id === proposal.id ), used: today.value.budget.used, } : undefined );
expect({ page: yield* todayIn(registry), rail: yield* todayIn(rail), }).toStrictEqual({ page: { holds: false, used: 3 }, rail: { holds: false, used: 3 }, });
unmount(); unmountRail(); }) );});