|
| 1 | +import { describe, expect, it } from "vitest" |
| 2 | + |
| 3 | +import type { ChangeCardData } from "@roo-code/types" |
| 4 | + |
| 5 | +import { |
| 6 | + buildChangeCard, |
| 7 | + buildChangeCardPayload, |
| 8 | + isAutoApprovedStep, |
| 9 | + resolveChangeCardDetail, |
| 10 | + type ChangeCardWrite, |
| 11 | +} from "../changeCard" |
| 12 | + |
| 13 | +describe("changeCard (B3a)", () => { |
| 14 | + function write(overrides: Partial<ChangeCardWrite> = {}): ChangeCardWrite { |
| 15 | + return { |
| 16 | + path: "src/a.ts", |
| 17 | + diffStats: { additions: 2, deletions: 1 }, |
| 18 | + diff: "--- a/src/a.ts\n+++ b/src/a.ts\n@@ -1 +1,2 @@\n-old\n+new-1\n+new-2", |
| 19 | + ...overrides, |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + describe("isAutoApprovedStep", () => { |
| 24 | + it("returns false for an empty step", () => { |
| 25 | + expect(isAutoApprovedStep([])).toBe(false) |
| 26 | + }) |
| 27 | + |
| 28 | + it("returns true only when every write was auto-approved", () => { |
| 29 | + expect(isAutoApprovedStep([write({ autoApproved: true }), write({ autoApproved: true })])).toBe(true) |
| 30 | + expect(isAutoApprovedStep([write({ autoApproved: true }), write()])).toBe(false) |
| 31 | + expect(isAutoApprovedStep([write()])).toBe(false) |
| 32 | + }) |
| 33 | + }) |
| 34 | + |
| 35 | + describe("resolveChangeCardDetail", () => { |
| 36 | + it("forces summary for auto-approved steps even when the setting is full", () => { |
| 37 | + const writes = [write({ autoApproved: true })] |
| 38 | + expect(resolveChangeCardDetail(writes, "full")).toBe("summary") |
| 39 | + expect(resolveChangeCardDetail(writes, undefined)).toBe("summary") |
| 40 | + }) |
| 41 | + |
| 42 | + it("follows the setting for interactive steps, defaulting to summary when unset", () => { |
| 43 | + const writes = [write()] |
| 44 | + expect(resolveChangeCardDetail(writes, "full")).toBe("full") |
| 45 | + expect(resolveChangeCardDetail(writes, "summary")).toBe("summary") |
| 46 | + expect(resolveChangeCardDetail(writes, undefined)).toBe("summary") |
| 47 | + }) |
| 48 | + }) |
| 49 | + |
| 50 | + describe("buildChangeCard", () => { |
| 51 | + it("carries the inline diff per file for full detail on a multi-file step", () => { |
| 52 | + const card = buildChangeCard( |
| 53 | + "sha-1", |
| 54 | + [write(), write({ path: "src/b.ts", diffStats: { additions: 1, deletions: 0 }, diff: "+b" })], |
| 55 | + "full", |
| 56 | + ) |
| 57 | + |
| 58 | + expect(card).toEqual({ |
| 59 | + checkpointIds: ["sha-1"], |
| 60 | + files: [ |
| 61 | + { |
| 62 | + path: "src/a.ts", |
| 63 | + additions: 2, |
| 64 | + deletions: 1, |
| 65 | + diff: "--- a/src/a.ts\n+++ b/src/a.ts\n@@ -1 +1,2 @@\n-old\n+new-1\n+new-2", |
| 66 | + }, |
| 67 | + { path: "src/b.ts", additions: 1, deletions: 0, diff: "+b" }, |
| 68 | + ], |
| 69 | + totalFiles: 2, |
| 70 | + detail: "full", |
| 71 | + }) |
| 72 | + }) |
| 73 | + |
| 74 | + it("omits the diff per file for summary detail (lazy fetch is B3b)", () => { |
| 75 | + const card = buildChangeCard("sha-1", [write()], "summary") |
| 76 | + |
| 77 | + expect(card.files).toEqual([{ path: "src/a.ts", additions: 2, deletions: 1 }]) |
| 78 | + expect(card.files[0]).not.toHaveProperty("diff") |
| 79 | + expect(card.detail).toBe("summary") |
| 80 | + expect(card.totalFiles).toBe(1) |
| 81 | + }) |
| 82 | + |
| 83 | + it("defaults missing diffStats to zero counts and keeps full detail without diff for a write without one", () => { |
| 84 | + const card = buildChangeCard("sha-1", [write({ diffStats: undefined, diff: undefined })], "full") |
| 85 | + |
| 86 | + expect(card.files[0]).toEqual({ path: "src/a.ts", additions: 0, deletions: 0 }) |
| 87 | + }) |
| 88 | + }) |
| 89 | + |
| 90 | + describe("buildChangeCardPayload", () => { |
| 91 | + it("resolves the detail level and builds the payload in one call", () => { |
| 92 | + // The expectations are typed against the shared ChangeCardData |
| 93 | + // contract in @roo-code/types, so the builder's output is checked |
| 94 | + // against the same single source of truth the webview consumes. |
| 95 | + // Interactive step with the full setting: diff inline. |
| 96 | + const full: ChangeCardData = buildChangeCardPayload("sha-1", [write()], "full") |
| 97 | + expect(full.detail).toBe("full") |
| 98 | + expect(full.files[0].diff).toBe("--- a/src/a.ts\n+++ b/src/a.ts\n@@ -1 +1,2 @@\n-old\n+new-1\n+new-2") |
| 99 | + |
| 100 | + // Auto-approved step with the full setting: compact summary, no diff. |
| 101 | + const compact: ChangeCardData = buildChangeCardPayload("sha-1", [write({ autoApproved: true })], "full") |
| 102 | + expect(compact.detail).toBe("summary") |
| 103 | + expect(compact.files[0]).not.toHaveProperty("diff") |
| 104 | + expect(compact.checkpointIds).toEqual(["sha-1"]) |
| 105 | + expect(compact.totalFiles).toBe(1) |
| 106 | + }) |
| 107 | + }) |
| 108 | +}) |
0 commit comments