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.

7.4 KB
Raw Blame
import { describe, expect, it } from "vitest";
import { PERFORMANCE_HOME_BIOMARKERS } from "../../src/health/biomarker-glossary";

import {
  affectedSummary,
  agentHandoffCall,
  boundaryLabel,
  causeStandingText,
  facetValueLabel,
  humanizeToken,
  moduleScopeLabel,
  opportunityEvidenceLine,
  opportunityTitle,
  planPresentation,
  siblingFixLabel,
  whyRankedLabel,
  whyRankedPhrase,
} from "../../src/health/performance/presentation";
import { opportunity } from "./fixtures/performance";

describe("performance presentation", () => {
  it("titles a cause in words, then names where by its short symbol", () => {
    const title = opportunityTitle(opportunity());
    expect(title).toBe("Database call inside a loop in load");
    expect(title).not.toContain("::");
    expect(opportunityTitle(opportunity({ intervention_symbol: null }))).toBe(
      "Database call inside a loop in run",
    );
  });

  it("reads rank factors as reasons, not points", () => {
    expect(whyRankedPhrase({ factor: "affected_call_sites", value: 8, points: 6 })).toBe(
      "8 call sites",
    );
    expect(whyRankedPhrase({ factor: "boundary_kind", value: "db", points: 4 })).toBe(
      "a database call",
    );
    expect(whyRankedPhrase({ factor: "multiplier_shape", value: "io_in_loop", points: 4 })).toBe(
      "runs once per loop iteration",
    );
    expect(whyRankedPhrase({ factor: "entry_reachability", value: true, points: 3 })).toBe(
      "reachable from an entry point",
    );
  });

  it("gives every performance marker a non-empty title", () => {
    for (const marker of PERFORMANCE_HOME_BIOMARKERS) {
      const title = opportunityTitle(opportunity({ biomarker_type: marker, boundary_kind: null }));
      expect(title.length, marker).toBeGreaterThan(0);
      expect(title[0], marker).toBe(title[0]!.toUpperCase());
    }
  });

  it("names an absent boundary rather than leaving it blank", () => {
    expect(boundaryLabel(null)).toBe("In-process");
    expect(boundaryLabel("none")).toBe("In-process");
    expect(boundaryLabel("db")).toBe("Database");
    expect(boundaryLabel("something_new")).toBe("Something new");
  });

  it("falls back from sink to intervention symbol to file for the evidence line", () => {
    expect(opportunityEvidenceLine(opportunity())).toBe("src/db.py::fetch");
    expect(opportunityEvidenceLine(opportunity({ terminal_sink: null }))).toBe(
      "src/shared.py::load",
    );
    expect(
      opportunityEvidenceLine(
        opportunity({ terminal_sink: null, intervention_symbol: null }),
      ),
    ).toBe("src/shared.py::run");
    expect(
      opportunityEvidenceLine(
        opportunity({ terminal_sink: null, intervention_symbol: null, evidence: [] }),
      ),
    ).toBe("src/shared.py");
  });

  it("keeps singular and plural counts honest", () => {
    expect(affectedSummary(opportunity())).toBe("2 call sites across 2 files");
    expect(
      affectedSummary(opportunity({ affected_call_sites_total: 1, affected_files_total: 1 })),
    ).toBe("1 call site across 1 file");
  });

  it("reads a rank factor as words with its signed contribution", () => {
    expect(whyRankedLabel({ factor: "boundary_kind", value: "db", points: 4 })).toBe(
      "Boundary kind: Db (+4)",
    );
    expect(whyRankedLabel({ factor: "entry_reachability", value: true, points: 0 })).toBe(
      "Entry reachability (+0)",
    );
  });

  it("labels each facet in its own vocabulary", () => {
    expect(facetValueLabel("context", "unknown")).toBe("Unclassified");
    expect(facetValueLabel("actionability", "investigate")).toBe("Needs investigation");
    expect(facetValueLabel("confidence", "high")).toBe("High");
    expect(facetValueLabel("plan_state", "no_safe_plan")).toBe("No safe plan");
    expect(facetValueLabel("boundary", "none")).toBe("In-process");
  });

  it("separates a plan that exists from one that needs a refresh and from none", () => {
    expect(planPresentation(opportunity()).actionable).toBe(true);
    const refresh = planPresentation(
      opportunity({ plan_id: null, plan_status: "not_persisted" }),
    );
    expect(refresh.actionable).toBe(false);
    expect(refresh.label).toBe("Needs an index refresh");
    const none = planPresentation(opportunity({ plan_id: null, plan_status: "no_safe_plan" }));
    expect(none.label).toBe("No safe plan");
  });

  it("names a stored plan apart from the plan-ready actionability state", () => {
    // The row mark counts with the "With a stored plan" tile; "Plan ready" is
    // the actionability state its own tile counts.
    expect(planPresentation(opportunity()).label).toBe("Stored plan");
    expect(facetValueLabel("plan_state", "available")).toBe("Stored plan");
    expect(facetValueLabel("actionability", "plan_ready")).toBe("Plan ready");
  });

  it("never calls a plan actionable without an id to fetch it with", () => {
    expect(planPresentation(opportunity({ plan_id: null })).actionable).toBe(false);
  });

  it("quotes the opportunity id in the agent drill-down", () => {
    expect(agentHandoffCall("perf2_abc")).toBe('get_health(opportunity_id="perf2_abc")');
  });

  it("prefers a sibling's strategy over its biomarker for the fix label", () => {
    expect(
      siblingFixLabel({
        opportunity_id: "perf2_sib",
        biomarker_type: "nested_loop_with_io",
        strategy: "batch_or_prefetch_io",
        relation: "preferred",
      }),
    ).toBe("Batch or prefetch io");
    expect(
      siblingFixLabel({
        opportunity_id: "perf2_sib",
        biomarker_type: "nested_loop_with_io",
        strategy: null,
        relation: "same_site",
      }),
    ).toBe("Nested loop with io");
  });

  it("reads a new actionability reason and prerequisite tokens honestly", () => {
    expect(humanizeToken("loop_already_chunked")).toBe("Loop already chunked");
    expect(humanizeToken("bounded_concurrency")).toBe("Bounded concurrency");
    expect(humanizeToken("query_supports_group_selection")).toBe(
      "Query supports group selection",
    );
  });
});

describe("module scope", () => {
  it("reads `<file>::__module__` as the module scope of that file", () => {
    expect(moduleScopeLabel("src/jobs.py::__module__")).toBe("module scope of src/jobs.py");
    expect(moduleScopeLabel("src/jobs.py::__module__", true)).toBe("module scope of jobs.py");
    expect(moduleScopeLabel("src/jobs.py::run")).toBeNull();
  });

  it("never prints __module__ in a title or an evidence line", () => {
    const top = opportunity({ intervention_symbol: "src/jobs.py::__module__", terminal_sink: null });
    expect(opportunityTitle(top)).toBe("Database call inside a loop in module scope of jobs.py");
    expect(opportunityEvidenceLine(top)).toBe("module scope of src/jobs.py");
  });

  it("phrases a row's secondary facts as sentences from the server's values", () => {
    const text = causeStandingText(
      opportunity({
        actionability_state: "advisory",
        actionability_reason: "strategy_requires_validation",
        plan_status: "no_safe_plan",
        plan_id: null,
        plan_reason: "No stored plan addresses this cause",
        confidence: "medium",
      }),
    );
    expect(text).toBe(
      "Advisory: Strategy requires validation. No safe plan: No stored plan addresses this cause. The call path resolved with medium confidence.",
    );
    // A reason that already ends a sentence is not given a second stop.
    expect(causeStandingText(opportunity({ plan_reason: "Stored." }))).toContain("Stored plan: Stored. ");
  });
});