File size: 4,923 Bytes
0dbc9de | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | import { describe, expect, test } from "bun:test"
import { normalize, resolveFileDiff, text } from "./session-diff"
describe("session diff", () => {
test("renders whole-file unified patches as complete diffs", () => {
const diff = {
file: "a.ts",
patch:
"Index: a.ts\n===================================================================\n--- a.ts\t\n+++ a.ts\t\n@@ -1,2 +1,2 @@\n one\n-two\n+three\n",
additions: 1,
deletions: 1,
status: "modified" as const,
}
const view = normalize(diff)
expect(view.fileDiff.name).toBe("a.ts")
expect(view.fileDiff.isPartial).toBe(false)
expect(text(view, "deletions")).toBe("one\ntwo\n")
expect(text(view, "additions")).toBe("one\nthree\n")
})
test("keeps missing final newlines from unified patches", () => {
const diff = {
file: "a.ts",
patch:
"Index: a.ts\n===================================================================\n--- a.ts\t\n+++ a.ts\t\n@@ -1,2 +1,2 @@\n one\n-two\n\\ No newline at end of file\n+three\n\\ No newline at end of file\n",
additions: 1,
deletions: 1,
status: "modified" as const,
}
const view = normalize(diff)
expect(text(view, "deletions")).toBe("one\ntwo")
expect(text(view, "additions")).toBe("one\nthree")
})
test("renders whole-file VCS patches as complete diffs", () => {
const fileDiff = resolveFileDiff({
file: "a.ts",
patch:
"diff --git a/a.ts b/a.ts\nindex 1a2b3c4..5d6e7f8 100644\n--- a/a.ts\n+++ b/a.ts\n@@ -1,2 +1,2 @@\n one\n-old\n+new\n",
})
expect(fileDiff.isPartial).toBe(false)
expect(fileDiff.additionLines).toEqual(["one\n", "new\n"])
})
test("keeps ordinary leading tool patches partial", () => {
const fileDiff = resolveFileDiff({
file: "a.ts",
patch:
"Index: a.ts\n===================================================================\n--- a.ts\n+++ a.ts\n@@ -1,5 +1,5 @@\n-old\n+new\n two\n three\n four\n five\n",
})
expect(fileDiff.isPartial).toBe(true)
expect(fileDiff.additionLines).toEqual(["new\n", "two\n", "three\n", "four\n", "five\n"])
})
test("keeps separated patch hunks partial without complete file contents", () => {
const fileDiff = resolveFileDiff({
file: "project.ts",
patch:
'Index: project.ts\n===================================================================\n--- project.ts\t\n+++ project.ts\t\n@@ -1,3 +1,2 @@\n import { and } from "drizzle-orm"\n-import { sql } from "drizzle-orm"\n import { ProjectTable } from "./project.sql"\n@@ -346,3 +345,3 @@\n import { Database } from "@/storage/db"\n-import { ProjectTable } from "./project.sql"\n+import { ProjectTable } from "../project/project.sql"\n import { SessionTable } from "../session/session.sql"\n',
})
expect(fileDiff.isPartial).toBe(true)
expect(fileDiff.hunks).toHaveLength(2)
expect(fileDiff.hunks[1]?.collapsedBefore).toBeGreaterThan(0)
})
test("renders headerless persisted patches", () => {
const view = normalize({
file: "a.ts",
patch: "@@ -1 +1 @@\n-old\n+new\n",
additions: 1,
deletions: 1,
status: "modified" as const,
})
expect(view.fileDiff.name).toBe("a.ts")
expect(view.fileDiff.isPartial).toBe(true)
expect(text(view, "deletions")).toBe("old\n")
expect(text(view, "additions")).toBe("new\n")
})
test("does not share headerless patch metadata between files", () => {
const patch = "@@ -1 +1 @@\n-old\n+new\n"
expect(resolveFileDiff({ file: "a.ts", patch }).name).toBe("a.ts")
expect(resolveFileDiff({ file: "b.ts", patch }).name).toBe("b.ts")
})
test("keeps capped header-only patches partial", () => {
const fileDiff = resolveFileDiff({
file: "a.ts",
patch:
"Index: a.ts\n===================================================================\n--- a.ts\t\n+++ a.ts\t\n",
})
expect(fileDiff.name).toBe("a.ts")
expect(fileDiff.isPartial).toBe(true)
expect(fileDiff.hunks).toEqual([])
})
test("keeps full legacy content as a complete diff", () => {
const diff = {
file: "a.ts",
before: "one\n",
after: "two\n",
additions: 1,
deletions: 1,
status: "modified" as const,
}
const view = normalize(diff)
expect(view.fileDiff.isPartial).toBe(false)
expect(text(view, "deletions")).toBe("one\n")
expect(text(view, "additions")).toBe("two\n")
})
test("ignores malformed persisted patches", () => {
const diff = {
file: "a.ts",
patch:
"diff --git a/a.ts b/a.ts\nindex ff4ceb2..65a1de0 100644\n--- a/a.ts\n+++ b/a.ts\n@@ -1,3 +1,3 @@\n keep\n+add\n same\r",
additions: 1,
deletions: 1,
status: "modified" as const,
}
const view = normalize(diff)
expect(text(view, "deletions")).toBe("")
expect(text(view, "additions")).toBe("")
})
})
|