Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283import { expect, test } from "vite-plus/test";
import { deriveStatus, formatStatus, type Facts, type Proposal, type Step } from "./status.ts";
let openPullRequest = { number: 1, isDraft: true, url: "https://github.com/owner/repo/pull/1", title: "Feature", isMerged: false,};
function facts(overrides: Partial<Facts> = {}): Facts { return { branch: "feature", isDefaultBranch: false, hasUpstream: true, unpushedCommits: 0, pullRequest: openPullRequest, pullRequestState: "known", proposal: null, ...overrides, };}function proposal(status: string, clarificationCount = 0, overrides: Partial<Proposal> = {}) { return { id: "proposal.0001", title: "Feature", status, clarificationCount, ...overrides, };}
// Asserts the derived routing — phase, who acts, which skill. The `next` sentence is human-facing// copy that will be reworded; pinning it here would make every wording change a test failure.function assertPhase(input: Facts, expected: Pick<Step, "phase" | "title" | "actor" | "skill">) { let status = deriveStatus(input);
expect(status).toMatchObject(expected); expect(status.next.length, "every phase must say what happens next").toBeGreaterThan(0); expect(status.situation.length, "every phase must describe the situation").toBeGreaterThan(0); return status;}
test("derives preparation on the default branch", () => { assertPhase(facts({ branch: "main", isDefaultBranch: true, pullRequest: null }), { phase: "1", title: "Preparation", actor: "agent", skill: null, });});
test("derives preparation for a branch without a pull request", () => { assertPhase(facts({ pullRequest: null }), { phase: "1", title: "Preparation", actor: "agent", skill: null, });});
test("derives proposal work for a draft pull request without a proposal", () => { assertPhase(facts(), { phase: "2", title: "Proposal", actor: "agent", skill: ".agents/skills/writing-a-proposal/", });});
test.each([ { where: "on the default branch", state: facts({ branch: "main", isDefaultBranch: true, pullRequest: null }), }, { where: "on a branch without a pull request", state: facts({ pullRequest: null }) }, { where: "on a pull request without a proposal", state: facts() },])("asks before opening a proposal workspace or drafting a proposal $where", ({ state }) => { let { actor, next } = deriveStatus(state);
expect(actor).toBe("agent"); expect(next).toMatch(/ask the human whether they want a proposal/i); expect(next).toMatch(/before (creating|pushing|writing)/i);});
test("keeps a draft proposal in proposal work", () => { assertPhase(facts({ proposal: proposal("draft") }), { phase: "2", title: "Proposal", actor: "you", skill: null, });});
test("derives implementation for an awaiting proposal", () => { assertPhase(facts({ proposal: proposal("awaiting-implementation") }), { phase: "3", title: "Implementation", actor: "agent", skill: ".agents/skills/implementing-a-proposal/", });});
test("derives human review for an active-review proposal", () => { assertPhase(facts({ proposal: proposal("active-review") }), { phase: "4", title: "Human review", actor: "you", skill: null, });});
test("derives revisions for a returned proposal", () => { assertPhase(facts({ proposal: proposal("returned-for-revisions") }), { phase: "4 → 3", title: "Human review → Implementation", actor: "agent", skill: ".agents/skills/implementing-a-proposal/", });});
test("derives completion for an accepted proposal", () => { assertPhase(facts({ proposal: proposal("accepted") }), { phase: "5", title: "Completion", actor: "agent", skill: ".agents/skills/completing-a-feature/", });});
test("derives done for an implemented merged pull request", () => { assertPhase( facts({ proposal: proposal("implemented"), pullRequest: { ...openPullRequest, isMerged: true }, }), { phase: "done", title: "Done", actor: "you", skill: null, }, );});
test("derives a closed state for a rejected proposal", () => { assertPhase(facts({ proposal: proposal("rejected") }), { phase: "done", title: "Closed", actor: "you", skill: null, });});
test("derives a closed state for a withdrawn proposal", () => { assertPhase(facts({ proposal: proposal("withdrawn") }), { phase: "done", title: "Closed", actor: "you", skill: null, });});
test("derives a closed state with its successor for a superseded proposal", () => { let input = facts({ proposal: proposal("superseded"), proposalSuccessor: { id: "proposal.0002", title: "Replacement" }, }); let status = assertPhase(input, { phase: "done", title: "Closed", actor: "you", skill: null, });
expect(formatStatus(input, status)).toMatch(/proposal\.0002/);});
test("names an implemented proposal with an unmerged pull request as inconsistent", () => { let status = assertPhase(facts({ proposal: proposal("implemented") }), { phase: "3 → 4", title: "Inconsistent record", actor: "you", skill: null, });
expect(status.notices).toStrictEqual([{ kind: "implemented-unmerged" }]);});
test("flags a draft pull request when the proposal is in active review", () => { let status = deriveStatus(facts({ proposal: proposal("active-review") }));
expect(status.notices).toStrictEqual([{ kind: "draft-active-review" }]);});
test("surfaces a heuristic proposal selection and its other candidates", () => { let input = facts({ proposal: proposal("draft", 0, { pullRequest: "2" }), proposalSelection: { otherCandidates: [{ id: "proposal.0002", title: "Other feature" }], pullRequestMismatch: true, }, }); let status = deriveStatus(input);
expect(status.notices).toStrictEqual([ { kind: "ambiguous-proposal-selection", selected: "proposal.0001", otherCandidates: [{ id: "proposal.0002", title: "Other feature" }], pullRequestMismatch: true, proposalPullRequest: "2", pullRequestNumber: 1, }, ]); expect(formatStatus(input, status)).toMatch(/proposal\.0002/);});
test("surfaces unresolved clarification markers and unpushed commits", () => { let status = deriveStatus(facts({ proposal: proposal("draft", 2), unpushedCommits: 3 }));
expect(status.notices).toStrictEqual([ { kind: "clarifications", count: 2 }, { kind: "unpushed-commits", count: 3 }, ]);});
test("reports an unavailable pull request state instead of assuming no pull request", () => { let status = assertPhase( facts({ pullRequest: null, pullRequestState: "unknown", pullRequestReason: "gh is unavailable", }), { phase: "1", title: "Preparation", actor: "agent", skill: null, }, );
expect(status.reason).toBe("gh is unavailable");});
test("reports an unauthenticated pull request state", () => { let status = assertPhase( facts({ pullRequest: null, pullRequestState: "unknown", pullRequestReason: "gh is not authenticated", }), { phase: "1", title: "Preparation", actor: "agent", skill: null, }, );
expect(status.reason).toBe("gh is not authenticated");});
test("reports an unknown branch role instead of recommending a pull request", () => { assertPhase(facts({ pullRequest: null, isDefaultBranch: null }), { phase: "unknown", title: "Branch role unknown", actor: "agent", skill: null, });});
test("reserves unknown routing for an invalid proposal status", () => { assertPhase(facts({ proposal: proposal("invalid") }), { phase: "2", title: "Proposal", actor: "you", skill: null, });});