|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import type { VisibleAgentNote } from "../src/ui/lib/agentAnnotations"; |
| 3 | +import { measureDiffSectionMetrics } from "../src/ui/lib/sectionHeights"; |
| 4 | +import { resolveTheme } from "../src/ui/themes"; |
| 5 | +import { createDiffFile, createHeaderOnlyDiffFile, lines } from "./fixtures/diff-helpers"; |
| 6 | + |
| 7 | +describe("measureDiffSectionMetrics", () => { |
| 8 | + const theme = resolveTheme("midnight", null); |
| 9 | + |
| 10 | + test("measures split and stack layouts from the render plan", () => { |
| 11 | + const file = createDiffFile(); |
| 12 | + |
| 13 | + const split = measureDiffSectionMetrics(file, "split", true, theme); |
| 14 | + const stack = measureDiffSectionMetrics(file, "stack", true, theme); |
| 15 | + |
| 16 | + expect(split.bodyHeight).toBeGreaterThan(0); |
| 17 | + expect(stack.bodyHeight).toBeGreaterThan(split.bodyHeight); |
| 18 | + expect(split.hunkBounds.get(0)?.height).toBeGreaterThan(0); |
| 19 | + expect(stack.hunkBounds.get(0)?.height).toBeGreaterThan(split.hunkBounds.get(0)?.height ?? 0); |
| 20 | + }); |
| 21 | + |
| 22 | + test("accounts for visible inline notes without moving the hunk anchor", () => { |
| 23 | + const file = createDiffFile(); |
| 24 | + const visibleAgentNotes: VisibleAgentNote[] = [ |
| 25 | + { |
| 26 | + id: "annotation:example:0", |
| 27 | + annotation: { |
| 28 | + newRange: [1, 1], |
| 29 | + rationale: "Keep note height in section metrics.", |
| 30 | + summary: "Explain the change", |
| 31 | + }, |
| 32 | + }, |
| 33 | + ]; |
| 34 | + |
| 35 | + const baseMetrics = measureDiffSectionMetrics(file, "split", true, theme, [], 120); |
| 36 | + const noteMetrics = measureDiffSectionMetrics( |
| 37 | + file, |
| 38 | + "split", |
| 39 | + true, |
| 40 | + theme, |
| 41 | + visibleAgentNotes, |
| 42 | + 120, |
| 43 | + ); |
| 44 | + |
| 45 | + expect(noteMetrics.bodyHeight).toBeGreaterThan(baseMetrics.bodyHeight); |
| 46 | + expect(noteMetrics.hunkAnchorRows.get(0)).toBe(baseMetrics.hunkAnchorRows.get(0)); |
| 47 | + expect(noteMetrics.rowMetrics.some((row) => row.key.startsWith("inline-note:"))).toBe(true); |
| 48 | + }); |
| 49 | + |
| 50 | + test("wraps long rows into taller section metrics when wrapping is enabled", () => { |
| 51 | + const file = createDiffFile({ |
| 52 | + before: lines("const alpha = 1;", "const beta = 2;"), |
| 53 | + after: lines( |
| 54 | + "const alpha = 1;", |
| 55 | + "const beta = 'this is a deliberately long line that should wrap in a narrow viewport';", |
| 56 | + ), |
| 57 | + id: "wrapped", |
| 58 | + path: "wrapped.ts", |
| 59 | + }); |
| 60 | + |
| 61 | + const nowrapMetrics = measureDiffSectionMetrics( |
| 62 | + file, |
| 63 | + "stack", |
| 64 | + true, |
| 65 | + theme, |
| 66 | + [], |
| 67 | + 32, |
| 68 | + true, |
| 69 | + false, |
| 70 | + ); |
| 71 | + const wrappedMetrics = measureDiffSectionMetrics( |
| 72 | + file, |
| 73 | + "stack", |
| 74 | + true, |
| 75 | + theme, |
| 76 | + [], |
| 77 | + 32, |
| 78 | + true, |
| 79 | + true, |
| 80 | + ); |
| 81 | + |
| 82 | + expect(wrappedMetrics.bodyHeight).toBeGreaterThan(nowrapMetrics.bodyHeight); |
| 83 | + expect(wrappedMetrics.hunkBounds.get(0)?.height).toBeGreaterThan( |
| 84 | + nowrapMetrics.hunkBounds.get(0)?.height ?? 0, |
| 85 | + ); |
| 86 | + }); |
| 87 | + |
| 88 | + test("returns a one-row placeholder for files with no visible hunks", () => { |
| 89 | + const file = createDiffFile({ |
| 90 | + after: "const stable = true;\n", |
| 91 | + before: "const stable = true;\n", |
| 92 | + id: "empty", |
| 93 | + path: "empty.ts", |
| 94 | + }); |
| 95 | + |
| 96 | + const metrics = measureDiffSectionMetrics(file, "split", true, theme); |
| 97 | + |
| 98 | + expect(file.metadata.hunks).toHaveLength(0); |
| 99 | + expect(metrics.bodyHeight).toBe(1); |
| 100 | + expect(metrics.hunkBounds.size).toBe(0); |
| 101 | + expect(metrics.rowMetrics).toEqual([]); |
| 102 | + }); |
| 103 | + |
| 104 | + test("can measure a header-only hunk stream without line rows", () => { |
| 105 | + const file = createHeaderOnlyDiffFile(); |
| 106 | + |
| 107 | + const metrics = measureDiffSectionMetrics(file, "split", true, theme); |
| 108 | + |
| 109 | + expect(file.metadata.hunks).toHaveLength(1); |
| 110 | + expect(metrics.bodyHeight).toBe(1); |
| 111 | + expect(metrics.hunkAnchorRows.size).toBe(1); |
| 112 | + expect(metrics.hunkAnchorRows.get(0)).toBe(0); |
| 113 | + expect(metrics.hunkBounds.get(0)).toMatchObject({ height: 1, top: 0 }); |
| 114 | + expect(metrics.rowMetrics).toHaveLength(1); |
| 115 | + expect(metrics.rowMetrics[0]?.key).toContain(":header:"); |
| 116 | + }); |
| 117 | +}); |
0 commit comments