Skip to content
Myr

claim.test.ts

This file

Path

packages/project/test/claim.test.ts

454 lines · 12.3 KiB · TypeScript

Last changed by

Merge build/comments-project into mainoscabrielbefore adoption49efd2a

History of this file

Take it

1import { describe, expect, it } from "@effect/vitest";2import { ClaimView } from "@repo/protocol/domain/claim";3import {4  AskId,5  claimForkName,6  CommitSha,7  Handle,8  LandedSha,9  ProjectName,10  RepoName,11} from "@repo/protocol/ids";12import { Person } from "@repo/protocol/principal";13import {14  AskBlocked,15  AskFull,16  CapReached,17  ClaimLapsed,18  Denounced,19  NotClaimed,20  NotVouched,21} from "@repo/protocol/refusal";22import { DateTime, Option, Result, Schema } from "effect";2324import {25  ClaimCommand,26  ClaimEffect,27  ClaimState,28  toView,29  transition,30} from "../src/claim.ts";31import { ProjectEventInput } from "../src/events.ts";3233const { Claim, Land, Push, Release, Tick } = ClaimCommand.cases;3435const HOUR = 3_600_000;3637const policy = { lapseAfterMs: 24 * HOUR, warnAfterMs: 2 * HOUR };3839const project = ProjectName.make("alice/datekit");4041const ask = AskId.make("parse-iso");4243const blocker = AskId.make("drop-legacy");4445const base = CommitSha.make("1".repeat(40));4647const head = CommitSha.make("2".repeat(40));4849const landing = LandedSha.make("3".repeat(40));5051const bob = Handle.make("Bob");5253const carol = Handle.make("carol");5455const fork = RepoName.make("alice__datekit--parse-iso--bob");5657const start = DateTime.makeUnsafe("2026-10-07T12:00:00Z");5859const at = (hours: number) => DateTime.add(start, { hours });6061const claimBy = (62  handle: Handle,63  standing: Partial<Omit<typeof Claim.Type, "_tag" | "who">> = {}64) =>65  Claim.make({66    base,67    blockedBy: [],68    cap: 3,69    denounced: false,70    heldByWho: 0,71    liveCount: 0,72    project,73    race: 1,74    vouched: true,75    who: Person.make({ handle }),76    ...standing,77  });7879const open = ClaimState.cases.Open.make({ ask });8081const fresh = {82  ask,83  base,84  claimant: bob,85  fork,86  lapsesAt: at(24),87  lastPushAt: Option.none(),88  openedAt: start,89  warnsAt: at(2),90};9192const live = ClaimState.cases.Live.make(fresh);9394const warned = ClaimState.cases.Warned.make(fresh);9596const lapsed = ClaimState.cases.Lapsed.make({ ...fresh, at: at(24) });9798const step = (state: ClaimState, effects: readonly ClaimEffect[] = []) =>99  Result.succeed({ effects, state });100101describe("claiming an ask", () => {102  it("gives a vouched person under their cap a live claim, a fork and a warn alarm", () => {103    expect(transition(open, claimBy(bob), start, policy)).toStrictEqual(104      step(live, [105        ClaimEffect.MakeFork({106          from: RepoName.make("alice__datekit"),107          name: fork,108        }),109        ClaimEffect.SetAlarm({ at: at(2) }),110        ClaimEffect.Emit({111          event: ProjectEventInput.Claimed({ ask, at: start, claimant: bob }),112        }),113      ])114    );115  });116117  it("names the fork after the project, the ask and the handle in lowercase", () => {118    expect(claimForkName(project, ask, bob)).toBe(119      "alice__datekit--parse-iso--bob"120    );121  });122123  it("refuses a second person with AskFull, and forks nothing", () => {124    expect(transition(live, claimBy(carol), at(1), policy)).toStrictEqual(125      Result.fail(new AskFull({ ask }))126    );127  });128129  it("refuses with AskFull when others hold every claim the kind allows", () => {130    expect(131      transition(open, claimBy(carol, { liveCount: 2, race: 2 }), at(1), policy)132    ).toStrictEqual(Result.fail(new AskFull({ ask })));133  });134135  it("lets a racing kind take another claim while it has room", () => {136    const result = transition(137      open,138      claimBy(carol, { liveCount: 1, race: 2 }),139      at(1),140      policy141    );142143    expect(Result.map(result, ({ state }) => state._tag)).toStrictEqual(144      Result.succeed("Live")145    );146  });147148  it("leaves the holder's own claim as it is when they claim again", () => {149    expect(transition(live, claimBy(bob), at(1), policy)).toStrictEqual(150      step(live)151    );152    expect(transition(warned, claimBy(bob), at(3), policy)).toStrictEqual(153      step(warned)154    );155  });156157  it("refuses an ask that waits on others with AskBlocked", () => {158    expect(159      transition(open, claimBy(bob, { blockedBy: [blocker] }), start, policy)160    ).toStrictEqual(Result.fail(AskBlocked.of(ask, [blocker])));161  });162163  it("refuses a person at their cap with CapReached", () => {164    expect(165      transition(open, claimBy(bob, { heldByWho: 3 }), start, policy)166    ).toStrictEqual(Result.fail(new CapReached({ cap: 3 })));167  });168169  it("refuses an unvouched person with NotVouched", () => {170    expect(171      transition(open, claimBy(bob, { vouched: false }), start, policy)172    ).toStrictEqual(Result.fail(new NotVouched({})));173  });174175  it("refuses a denounced person with Denounced, before NotVouched", () => {176    expect(177      transition(178        open,179        claimBy(bob, { denounced: true, vouched: false }),180        start,181        policy182      )183    ).toStrictEqual(Result.fail(new Denounced({})));184  });185186  it("names only the first refusal a claim meets: blockers, then the cap, then standing", () => {187    const troubled = {188      blockedBy: [blocker],189      denounced: true,190      heldByWho: 3,191      vouched: false,192    };193194    expect(195      transition(open, claimBy(bob, troubled), start, policy)196    ).toStrictEqual(Result.fail(AskBlocked.of(ask, [blocker])));197    expect(198      transition(199        open,200        claimBy(bob, { ...troubled, blockedBy: [] }),201        start,202        policy203      )204    ).toStrictEqual(Result.fail(new CapReached({ cap: 3 })));205  });206207  it("says AskFull before anything else, so a full ask never forks", () => {208    expect(209      transition(210        live,211        claimBy(carol, {212          blockedBy: [blocker],213          denounced: true,214          heldByWho: 3,215          vouched: false,216        }),217        at(1),218        policy219      )220    ).toStrictEqual(Result.fail(new AskFull({ ask })));221  });222223  it("gives a lapsed claim to the next person, with their own fork", () => {224    const result = transition(lapsed, claimBy(carol), at(25), policy);225226    expect(Result.map(result, ({ effects }) => effects[0])).toStrictEqual(227      Result.succeed(228        ClaimEffect.MakeFork({229          from: RepoName.make("alice__datekit"),230          name: RepoName.make("alice__datekit--parse-iso--carol"),231        })232      )233    );234  });235236  it("refuses any claim on an answered ask with AskFull", () => {237    const answered = ClaimState.cases.Answered.make({ ...fresh, landing });238239    expect(transition(answered, claimBy(bob), at(3), policy)).toStrictEqual(240      Result.fail(new AskFull({ ask }))241    );242  });243});244245describe("pushing keeps a claim alive", () => {246  it("moves both deadlines on from the push and sets the alarm", () => {247    expect(248      transition(live, Push.make({ head, who: bob }), at(1), policy)249    ).toStrictEqual(250      step(251        ClaimState.cases.Live.make({252          ...fresh,253          lapsesAt: at(25),254          lastPushAt: Option.some(at(1)),255          warnsAt: at(3),256        }),257        [258          ClaimEffect.SetAlarm({ at: at(3) }),259          ClaimEffect.Emit({260            event: ProjectEventInput.Pushed({ ask, at: at(1), head }),261          }),262        ]263      )264    );265  });266267  it("makes a warned claim live again", () => {268    const result = transition(269      warned,270      Push.make({ head, who: bob }),271      at(5),272      policy273    );274275    expect(Result.map(result, ({ state }) => state._tag)).toStrictEqual(276      Result.succeed("Live")277    );278  });279280  it("refuses a push from someone who holds no claim with NotClaimed", () => {281    expect(282      transition(live, Push.make({ head, who: carol }), at(1), policy)283    ).toStrictEqual(Result.fail(NotClaimed.of(ask)));284    expect(285      transition(open, Push.make({ head, who: bob }), at(1), policy)286    ).toStrictEqual(Result.fail(NotClaimed.of(ask)));287  });288289  it("refuses the lapsed claimant's push with ClaimLapsed", () => {290    expect(291      transition(lapsed, Push.make({ head, who: bob }), at(25), policy)292    ).toStrictEqual(Result.fail(ClaimLapsed.of(ask)));293  });294});295296describe("an alarm", () => {297  it("does nothing before the warn deadline", () => {298    expect(transition(live, Tick.make({}), at(1), policy)).toStrictEqual(299      step(live)300    );301  });302303  it("warns a claim at its warn deadline and sets the lapse alarm", () => {304    expect(transition(live, Tick.make({}), at(2), policy)).toStrictEqual(305      step(warned, [306        ClaimEffect.SetAlarm({ at: at(24) }),307        ClaimEffect.Emit({308          event: ProjectEventInput.Warned({ ask, at: at(2) }),309        }),310      ])311    );312  });313314  it("lapses a warned claim at its lapse deadline and revokes the fork", () => {315    expect(transition(warned, Tick.make({}), at(24), policy)).toStrictEqual(316      step(lapsed, [317        ClaimEffect.RevokeAccess({ fork }),318        ClaimEffect.Emit({319          event: ProjectEventInput.Lapsed({ ask, at: at(24) }),320        }),321      ])322    );323  });324325  it("lapses a live claim straight away when the alarm comes after both deadlines", () => {326    const result = transition(live, Tick.make({}), at(30), policy);327328    expect(Result.map(result, ({ state }) => state._tag)).toStrictEqual(329      Result.succeed("Lapsed")330    );331  });332333  it("leaves a warned claim alone before its lapse deadline", () => {334    expect(transition(warned, Tick.make({}), at(23), policy)).toStrictEqual(335      step(warned)336    );337  });338});339340describe("releasing a claim", () => {341  it("ends the holder's claim and revokes the fork", () => {342    expect(343      transition(live, Release.make({ who: bob }), at(1), policy)344    ).toStrictEqual(345      step(ClaimState.cases.Released.make(fresh), [346        ClaimEffect.RevokeAccess({ fork }),347        ClaimEffect.Emit({348          event: ProjectEventInput.Released({ ask, at: at(1) }),349        }),350      ])351    );352  });353354  it("refuses anyone but the holder with NotClaimed", () => {355    expect(356      transition(live, Release.make({ who: carol }), at(1), policy)357    ).toStrictEqual(Result.fail(NotClaimed.of(ask)));358  });359360  it("converges when the holder releases twice", () => {361    const released = ClaimState.cases.Released.make(fresh);362363    expect(364      transition(released, Release.make({ who: bob }), at(2), policy)365    ).toStrictEqual(step(released));366  });367});368369describe("landing the claimant's answer", () => {370  it("answers the claim and revokes the fork", () => {371    expect(372      transition(live, Land.make({ landing }), at(3), policy)373    ).toStrictEqual(374      step(ClaimState.cases.Answered.make({ ...fresh, landing }), [375        ClaimEffect.RevokeAccess({ fork }),376      ])377    );378  });379380  it("converges when the landing is told twice", () => {381    const answered = ClaimState.cases.Answered.make({ ...fresh, landing });382383    expect(384      transition(answered, Land.make({ landing }), at(4), policy)385    ).toStrictEqual(step(answered));386  });387});388389describe("the claim's view", () => {390  const context = {391    checkCommand: "bun test test/parse-iso.test.ts",392    project,393    remote: "https://platform.example/datekit--parse-iso--bob.git",394  };395396  it("shows a live claim with its fork, remote and deadlines", () => {397    expect(toView(live, context)).toStrictEqual(398      Option.some(399        ClaimView.make({400          ask,401          base,402          checkCommand: context.checkCommand,403          claimant: bob,404          fork,405          lapsesAt: at(24),406          openedAt: start,407          project,408          remote: context.remote,409          state: "live",410          warnsAt: at(2),411        })412      )413    );414  });415416  it("names the state the claim ended in, and its last push", () => {417    const pushed = { ...fresh, lastPushAt: Option.some(at(1)) };418419    expect(420      Option.map(421        toView(422          ClaimState.cases.Lapsed.make({ ...pushed, at: at(25) }),423          context424        ),425        ({ lastPushAt, state }) => ({ lastPushAt, state })426      )427    ).toStrictEqual(Option.some({ lastPushAt: at(1), state: "lapsed" }));428  });429430  it("has no view while the ask holds no claim", () => {431    expect(toView(open, context)).toStrictEqual(Option.none());432  });433});434435describe("the persisted state", () => {436  it("round-trips through JSON text", () => {437    const pushed = ClaimState.cases.Warned.make({438      ...fresh,439      lastPushAt: Option.some(at(1)),440    });441442    const Stored = Schema.fromJsonString(ClaimState);443    const text = Schema.encodeSync(Stored)(pushed);444445    expect(Schema.decodeSync(Stored)(text)).toStrictEqual(pushed);446  });447448  it("is JSON with times as text, and leaves out a push that never came", () => {449    const stored = Schema.encodeSync(ClaimState)(live);450451    expect(stored).toHaveProperty("lapsesAt", "2026-10-08T12:00:00.000Z");452    expect(stored).not.toHaveProperty("lastPushAt");453  });454});