outcome.test.tsx
This file
Path
apps/web/test/outcome.test.tsx
283 lines · 8.2 KiB · TypeScript
Last changed by
Merge build/maintainer-wording into mainoscabrielbefore adoption4c1a571
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284import { describe, expect, it } from "@effect/vitest";import { Brand } from "@repo/brand";import { Outcome } from "@repo/protocol/domain/outcome";import type { StageName } from "@repo/protocol/domain/outcome";import { AskId, DecisionId, Handle, LandedSha, RepoPath,} from "@repo/protocol/ids";import { OutOfScope, Weakened } from "@repo/protocol/refusal";import { explain } from "@repo/protocol/render";import { Schema } from "effect";import type { ReactElement } from "react";
import { OutcomeView, StageStrip, stageStates,} from "../src/features/shared/outcome.tsx";import { RefusalView } from "../src/features/shared/refusal.tsx";import { allInOrder, markupOf, textOf } from "./markup.ts";
const stagesOf = (element: ReactElement) => [...markupOf(element).matchAll(/<li\b[^>]*>/gu)].flatMap(([tag]) => { const stage = /data-stage="(?<value>[^"]+)"/u.exec(tag)?.groups?.value; const state = /data-state="(?<value>[^"]+)"/u.exec(tag)?.groups?.value;
return stage === undefined || state === undefined ? [] : [[stage, state]]; });
const handle = Schema.decodeSync(Handle);
const repoPath = Schema.decodeSync(RepoPath);
const outOfScope = new OutOfScope({ paths: [repoPath("packages/format/src/format.ts"), repoPath("README.md")],});
const weakened = new Weakened({ at: { line: 12, path: repoPath("packages/parse/test/parse.test.ts") }, weakening: { marker: ".skip(" },});
const refusals = [outOfScope, weakened] as const;
const sha = Schema.decodeSync(LandedSha)( "9b1c3e5d7f9a1b3c5d7e9f1a3b5c7d9e1f3a5b7c");
const landed = Outcome.cases.Landed.make({ sha });
describe(RefusalView, () => { it("shows every line explain gives, in order, each refusal's tag, and then its next command", () => { const text = textOf(<RefusalView refusals={refusals} />);
const expected = refusals.flatMap((refusal) => [ ...explain([refusal]).map((line) => line.trim()), refusal.next, ]);
expect(allInOrder(text, expected)).toBeTruthy(); expect(text).toContain("OutOfScope"); expect(text).toContain("Weakened");
for (const refusal of refusals) { expect(refusal.next.startsWith(`${Brand.cli} `)).toBeTruthy(); } });
it("points at the path and line of a refusal that has one", () => { const text = textOf(<RefusalView refusals={[weakened]} />);
expect(text).toContain("packages/parse/test/parse.test.ts:12"); });
it("says whether retrying can help, without rewording the refusal", () => { const markup = markupOf(<RefusalView refusals={refusals} />);
expect(markup).toContain('data-refusal="OutOfScope"'); expect(markup).toContain('data-retryable="false"'); });});
describe(OutcomeView, () => { const waiting = [ [Outcome.cases.Checking.make({ stage: "checks" }), "Checking", "checks"], [ Outcome.cases.AwaitingDecision.make({ decision: Schema.decodeSync(DecisionId)("dec-000001"), }), "Waiting on decision", "dec-000001", ], [ Outcome.cases.AwaitingFinish.make({ decider: handle("okonkwo") }), "waiting for a maintainer's finish", "okonkwo", ], [ Outcome.cases.WaitingOnParent.make({ ask: Schema.decodeSync(AskId)("split-format"), }), "waiting for", "split-format", ], ] as const;
it.each(waiting)( "reads %o as waiting, never as a refusal", (outcome, wording, names) => { const element = <OutcomeView outcome={outcome} />;
expect(markupOf(element)).toContain(`data-outcome="${outcome._tag}"`); expect(markupOf(element)).toContain('data-state="waiting"'); expect(markupOf(element)).not.toContain("text-refused"); expect(textOf(element)).toContain(wording); expect(textOf(element)).toContain(names); } );
it("reads a landing as final, with its short sha", () => { const element = <OutcomeView outcome={landed} />;
expect(markupOf(element)).toContain('data-state="final"'); expect(textOf(element)).toContain("Landed as 9b1c3e5"); });
it("shows a refusal through RefusalView", () => { const element = ( <OutcomeView outcome={Outcome.cases.Refused.make({ refusals })} /> );
expect(markupOf(element)).toContain('data-state="final"'); expect(markupOf(element)).toContain( markupOf(<RefusalView refusals={refusals} />) ); });});
const strip = (stage: StageName, outcome: Outcome) => stagesOf(<StageStrip outcome={outcome} stage={stage} />);
describe(StageStrip, () => { it("marks the stages before the current one passed, and the rest pending", () => { expect( strip("checks", Outcome.cases.Checking.make({ stage: "checks" })) ).toStrictEqual([ ["parse", "passed"], ["shape", "passed"], ["merge", "passed"], ["cheap", "passed"], ["checks", "current"], ["heldBack", "pending"], ["human", "pending"], ]); });
it("marks the stage that refused, and leaves the rest unreached", () => { expect( strip("shape", Outcome.cases.Refused.make({ refusals })) ).toStrictEqual([ ["parse", "passed"], ["shape", "refused"], ["merge", "pending"], ["cheap", "pending"], ["checks", "pending"], ["heldBack", "pending"], ["human", "pending"], ]); });
it("marks a landed answer's later stages as not needed", () => { expect(strip("checks", landed)).toStrictEqual([ ["parse", "passed"], ["shape", "passed"], ["merge", "passed"], ["cheap", "passed"], ["checks", "passed"], ["heldBack", "skipped"], ["human", "skipped"], ]); });
it("marks the stage the gate is at as the current step, and gives each state a word at the floor size", () => { const markup = markupOf( <StageStrip outcome={Outcome.cases.Checking.make({ stage: "checks" })} stage="checks" /> );
expect(markup).toMatch( /<li\b[^>]*aria-current="step"[^>]*data-stage="checks"/u ); expect(markup.match(/aria-current=/gu)).toHaveLength(1); expect(textOf(<StageStrip outcome={landed} stage="checks" />)).toContain( "not needed" ); expect(markup).not.toContain("text-xs"); });
it("draws a stage the answer never needed as not needed, given what the verdict holds", () => { expect( stagesOf( <StageStrip holds={{ decided: false, heldBack: false }} outcome={landed} stage="human" /> ) ).toStrictEqual([ ["parse", "passed"], ["shape", "passed"], ["merge", "passed"], ["cheap", "passed"], ["checks", "passed"], ["heldBack", "skipped"], ["human", "skipped"], ]); });});
describe("each stage's state", () => { it("marks the maintainer and held-back stages of a landing that needed neither as not needed", () => { expect( stageStates("human", landed, { decided: false, heldBack: false }) ).toStrictEqual([ ["parse", "passed"], ["shape", "passed"], ["merge", "passed"], ["cheap", "passed"], ["checks", "passed"], ["heldBack", "skipped"], ["human", "skipped"], ]); });
it("marks them passed when the verdict holds a decision and held-back results", () => { expect( stageStates("human", landed, { decided: true, heldBack: true }) ).toStrictEqual([ ["parse", "passed"], ["shape", "passed"], ["merge", "passed"], ["cheap", "passed"], ["checks", "passed"], ["heldBack", "passed"], ["human", "passed"], ]); });
it("keeps a stage the answer still waits at current, whatever the verdict holds", () => { expect( stageStates( "human", Outcome.cases.AwaitingFinish.make({ decider: handle("okonkwo") }), { decided: false, heldBack: false } ) ).toStrictEqual([ ["parse", "passed"], ["shape", "passed"], ["merge", "passed"], ["cheap", "passed"], ["checks", "passed"], ["heldBack", "skipped"], ["human", "current"], ]); });
it("reads as before when it is not told what the verdict holds", () => { expect(stageStates("human", landed)).toStrictEqual([ ["parse", "passed"], ["shape", "passed"], ["merge", "passed"], ["cheap", "passed"], ["checks", "passed"], ["heldBack", "passed"], ["human", "passed"], ]); });});