Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

13.6 KB
Raw Blame
import { describe, expect, it, vi } from "vitest";
import { fireEvent, render, screen, within } from "@testing-library/react";
import {
  DECISION_LANES,
  DecisionReviewLanes,
  type DecisionLane,
} from "../../src/decisions/decision-review-lanes";
import type { DecisionRecord } from "@repowise-dev/types/decisions";

function record(overrides: Partial<DecisionRecord> = {}): DecisionRecord {
  return {
    id: "d1",
    repository_id: "r1",
    title: "Use JWT for authentication",
    status: "proposed",
    context: "",
    decision: "Issue signed JWTs",
    // Carries what the acceptance contract needs, because that is the ordinary
    // candidate: reason, scope and an evidence reference. The cases that are
    // missing one say so explicitly.
    rationale: "sessions did not survive a restart",
    alternatives: [],
    consequences: [],
    affected_files: ["src/auth/service.py"],
    affected_modules: [],
    tags: [],
    source: "pr",
    evidence_commits: ["abc1234"],
    evidence_file: null,
    evidence_line: null,
    confidence: 0.6,
    staleness_score: 0,
    superseded_by: null,
    last_code_change: null,
    created_at: "2026-01-01T00:00:00Z",
    updated_at: "2026-01-01T00:00:00Z",
    ...overrides,
  };
}

function renderLanes(
  lane: DecisionLane,
  props: Partial<React.ComponentProps<typeof DecisionReviewLanes>> = {},
) {
  return render(
    <DecisionReviewLanes
      lane={lane}
      onLaneChange={vi.fn()}
      decisions={[record()]}
      repoId="r1"
      {...props}
    />,
  );
}

describe("lane structure", () => {
  it("partitions the repository: five lanes, no overlap", () => {
    expect([...DECISION_LANES]).toEqual([
      "active",
      "candidates",
      "needs_review",
      "uncheckable",
      "history",
    ]);
    expect(new Set(DECISION_LANES).size).toBe(DECISION_LANES.length);
  });

  it("renders one tab per lane, with the measured counts", () => {
    renderLanes("active", {
      counts: {
        active: 122,
        candidates: 381,
        needs_review: 4,
        uncheckable: 57,
        history: 2,
      },
    });

    const tablist = screen.getByRole("tablist");
    expect(within(tablist).getAllByRole("tab")).toHaveLength(5);
    expect(within(tablist).getByText("381")).toBeInTheDocument();
    expect(within(tablist).getByText("57")).toBeInTheDocument();
  });

  it("omits a badge for a lane nobody counted", () => {
    renderLanes("active", { counts: { active: 3 } });

    const tablist = screen.getByRole("tablist");
    // One badge rendered, not five zeroes: a number nobody measured is worse
    // than no number.
    expect(within(tablist).queryByText("0")).not.toBeInTheDocument();
    expect(within(tablist).getByText("3")).toBeInTheDocument();
  });

  it("reports the lane change rather than switching itself", () => {
    const onLaneChange = vi.fn();
    renderLanes("active", { onLaneChange });

    fireEvent.click(screen.getByRole("tab", { name: /Candidates/ }));

    expect(onLaneChange).toHaveBeenCalledWith("candidates");
  });
});

describe("a candidate is a review request, not an instruction", () => {
  it("carries the evidence it was drawn from", () => {
    renderLanes("candidates", {
      decisions: [
        record({
          evidence_count: 3,
          evidence_preview: {
            source: "pr",
            source_quote: "we settled on JWT because sessions did not survive",
            verification: "exact",
            evidence_file: "docs/auth.md",
            evidence_line: 12,
          },
        }),
      ],
    });

    expect(
      screen.getByText(/we settled on JWT because sessions did not survive/),
    ).toBeInTheDocument();
    expect(screen.getByText(/docs\/auth\.md:12/)).toBeInTheDocument();
    expect(screen.getByText(/2 more/)).toBeInTheDocument();
  });

  it("says it governs nothing and nobody has accepted it", () => {
    renderLanes("candidates");

    expect(screen.getByText(/nobody has accepted/i)).toBeInTheDocument();
    expect(screen.getByText(/govern nothing and reach no agent/i)).toBeInTheDocument();
  });

  it("offers the review verbs only in the candidates lane", () => {
    const onAccept = vi.fn();
    renderLanes("active", { onAccept, decisions: [record({ currency: "active" })] });
    expect(screen.queryByRole("button", { name: "Accept" })).not.toBeInTheDocument();

    renderLanes("candidates", { onAccept });
    expect(screen.getByRole("button", { name: "Accept" })).toBeInTheDocument();
  });

  it("hands the record back rather than writing", () => {
    const onAccept = vi.fn();
    const onDismiss = vi.fn();
    renderLanes("candidates", { onAccept, onDismiss });

    fireEvent.click(screen.getByRole("button", { name: "Accept" }));
    expect(onAccept).toHaveBeenCalledWith(expect.objectContaining({ id: "d1" }));

    fireEvent.click(screen.getByRole("button", { name: "Dismiss" }));
    expect(onDismiss).toHaveBeenCalledWith(expect.objectContaining({ id: "d1" }));
  });

  it("disables the verbs while that row's write is in flight, and only that row", () => {
    renderLanes("candidates", {
      onAccept: vi.fn(),
      onDismiss: vi.fn(),
      pendingIds: new Set(["d1"]),
    });

    expect(screen.getByRole("button", { name: "Accepting…" })).toBeDisabled();
    expect(screen.getByRole("button", { name: "Dismiss" })).toBeDisabled();
  });

  it("leaves a second row usable while the first is in flight", () => {
    renderLanes("candidates", {
      decisions: [record({ id: "d1" }), record({ id: "d2", title: "Other" })],
      onAccept: vi.fn(),
      pendingIds: new Set(["d1"]),
    });

    // One write in flight must not freeze the queue: a single pending id used
    // to disable every row, and re-enable them all when the first resolved.
    expect(screen.getByRole("button", { name: "Accepting…" })).toBeDisabled();
    expect(screen.getByRole("button", { name: "Accept" })).toBeEnabled();
  });

  it("disables Accept, with the reason, on a candidate the engine would refuse", () => {
    renderLanes("candidates", {
      decisions: [
        record({
          affected_files: [],
          affected_modules: [],
          rationale: "",
          decision: "",
        }),
      ],
      onAccept: vi.fn(),
      onDismiss: vi.fn(),
    });

    // The engine refuses an acceptance with no scope, reason or evidence. The
    // row already knows all three, so the button says so before it is pressed
    // rather than reporting it in an error toast afterwards.
    expect(screen.getByRole("button", { name: "Accept" })).toBeDisabled();
    expect(screen.getByText(/no scope: name the files or modules it governs/))
      .toBeInTheDocument();
    // Dismissing stays available: an unacceptable candidate is exactly the one
    // worth tombstoning.
    expect(screen.getByRole("button", { name: "Dismiss" })).toBeEnabled();
  });

  it("disables Accept when the server filled a blank decision with the title", () => {
    renderLanes("candidates", {
      decisions: [record({ decision: "Use JWTs", decision_text_missing: true })],
      onAccept: vi.fn(),
      onDismiss: vi.fn(),
    });

    // The row reads the title fallback, but acceptance reads the stored text.
    expect(screen.getByRole("button", { name: "Accept" })).toBeDisabled();
    expect(screen.getByText(/no decision text: state what was chosen/))
      .toBeInTheDocument();
  });

  it("leaves Accept enabled on a candidate that carries what the contract needs", () => {
    renderLanes("candidates", {
      decisions: [
        record({
          rationale: "sessions did not survive a restart",
          affected_files: ["src/auth/service.py"],
          evidence_commits: ["abc1234"],
        }),
      ],
      onAccept: vi.fn(),
    });

    expect(screen.getByRole("button", { name: "Accept" })).toBeEnabled();
    expect(screen.queryByText(/Cannot accept/)).not.toBeInTheDocument();
  });

  it("explains itself instead of rendering a control it cannot use", () => {
    renderLanes("candidates", {
      onAccept: vi.fn(),
      readOnlyReason: "Read-only snapshot",
    });

    expect(screen.queryByRole("button", { name: "Accept" })).not.toBeInTheDocument();
    expect(screen.getByText("Read-only snapshot")).toBeInTheDocument();
  });
});

describe("currency marks the exception, not the default", () => {
  // Scoped to the row list: every lane name is also a tab label, so an
  // unscoped query would find the tab and pass whatever the row rendered.
  const rows = () => within(screen.getByRole("list"));

  it("says nothing extra about a decision that still describes its code", () => {
    renderLanes("active", { decisions: [record({ currency: "active" })] });

    expect(rows().queryByText("Needs review")).not.toBeInTheDocument();
    expect(rows().queryByText("Uncheckable")).not.toBeInTheDocument();
    expect(rows().queryByText("Active")).not.toBeInTheDocument();
  });

  it("names a currency the reader cannot guess", () => {
    renderLanes("uncheckable", {
      decisions: [
        record({ currency: "uncheckable", affected_files: [], affected_modules: [] }),
      ],
    });

    expect(rows().getByText("Uncheckable")).toBeInTheDocument();
    // And the row says the same thing in its own words rather than only in a
    // colour: this record names nothing at all.
    expect(rows().getByText("names nothing")).toBeInTheDocument();
  });
});

describe("who signed is marked where it is not a person", () => {
  const rows = () => within(screen.getByRole("list"));

  it("says nothing about the case the surface was built for", () => {
    renderLanes("active", {
      decisions: [
        record({ currency: "active", accepter: "Raghav", accepter_kind: "person" }),
      ],
    });

    expect(rows().queryByText(/Signed by/)).not.toBeInTheDocument();
  });

  it("names an agent, and the session behind it", () => {
    renderLanes("active", {
      decisions: [
        record({
          currency: "active",
          accepter: "claude_code",
          accepter_kind: "agent",
          accepter_session: "sess-42",
        }),
      ],
    });

    const mark = rows().getByText("Signed by an agent");
    expect(mark).toBeInTheDocument();
    expect(mark).toHaveAttribute(
      "title",
      "Signed by an agent: claude_code, session sess-42",
    );
  });

  it("does not call a machine withdrawal an acceptance", () => {
    // The evolution stage retires records as `agent`, so the badge on a
    // history row is over a revocation, not a grant.
    renderLanes("history", {
      decisions: [
        record({
          currency: "superseded",
          accepter: "evolution",
          accepter_kind: "agent",
        }),
      ],
    });

    expect(rows().queryByText(/Accepted by/)).not.toBeInTheDocument();
    expect(rows().getByText("Signed by an agent")).toBeInTheDocument();
  });

  it("marks a row written before the kind was recorded", () => {
    // '' is not `person`: leaving it unmarked would read as one.
    renderLanes("active", {
      decisions: [
        record({ currency: "active", accepter: "Raghav", accepter_kind: "" }),
      ],
    });

    expect(rows().getByText("Signer not recorded")).toBeInTheDocument();
  });

  it("says nothing about a candidate, which nobody signed", () => {
    renderLanes("candidates", { decisions: [record({ currency: null })] });

    expect(rows().queryByText(/Signed by/)).not.toBeInTheDocument();
    expect(rows().queryByText("Signer not recorded")).not.toBeInTheDocument();
  });
});

describe("the source filter is a second axis, not a second lane control", () => {
  it("offers every live source and no retired one", () => {
    renderLanes("candidates", { onSourceChange: vi.fn() });
    const select = screen.getByLabelText("Filter by source");

    // Built from the shared list, so a source absent from this page's fifty
    // rows is still selectable.
    expect(within(select).getByRole("option", { name: "Commit" })).toBeTruthy();
    expect(within(select).getByRole("option", { name: "Model" })).toBeTruthy();
    expect(within(select).queryByRole("option", { name: "Docs" })).toBeNull();
  });

  it("reports the change rather than filtering itself", () => {
    const onSourceChange = vi.fn();
    renderLanes("candidates", { onSourceChange });

    fireEvent.change(screen.getByLabelText("Filter by source"), {
      target: { value: "pr" },
    });

    expect(onSourceChange).toHaveBeenCalledWith("pr");
  });

  it("renders no control when the host cannot act on it", () => {
    renderLanes("candidates");

    expect(screen.queryByLabelText("Filter by source")).not.toBeInTheDocument();
  });

  it("says an empty result may be the filter rather than the lane", () => {
    renderLanes("candidates", {
      decisions: [],
      source: "pr",
      onSourceChange: vi.fn(),
    });

    expect(screen.getByText(/clear the source filter/)).toBeInTheDocument();
    expect(screen.queryByText(/No candidates are waiting/)).not.toBeInTheDocument();
  });
});

describe("empty and failed lanes", () => {
  it("says what will fill the lane", () => {
    renderLanes("candidates", { decisions: [] });

    expect(screen.getByText(/No candidates are waiting/)).toBeInTheDocument();
  });

  it("says nothing while the first page is still loading", () => {
    renderLanes("candidates", { decisions: undefined, isLoading: true });

    expect(screen.queryByText(/No candidates are waiting/)).not.toBeInTheDocument();
  });

  it("offers a retry when the fetch failed", () => {
    const onRetry = vi.fn();
    renderLanes("candidates", {
      decisions: [],
      error: new Error("boom"),
      onRetry,
    });

    // The error replaces the lane rather than sitting beside an empty state
    // that would read as "there is nothing here".
    expect(screen.queryByText(/No candidates are waiting/)).not.toBeInTheDocument();
    expect(screen.getByText(/Couldn't load this lane/)).toBeInTheDocument();
  });
});