Something went wrong. Try again.
opencode v2 mirror
Something went wrong. Try again.
6.3 kB · 172 lines
TypeScript
at systemd
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173import { describe, expect, test } from "bun:test"import { normalize, resolveFileDiff, text } from "./session-diff"
describe("session diff", () => { test.each([ ["carriage return", "\r"], ["line separator", "\u2028"], ["paragraph separator", "\u2029"], ])( "parses adversarial patch headers containing a %s", (_, separator) => { // GHSA-73rr-hh4g-fpgx: isolate synchronous hangs and memory growth from the test runner. const name = `a${separator}b.ts` const padding = " ".repeat(10_000) const patches = [ `--- ${name}\t\n+++ b.ts\t\n`, `--- a.ts\t\n+++ ${name}\t\n`, `--- ${padding}${name}\t\n+++ b.ts\t\n`, `--- a.ts\t\n+++ ${padding}${name}\t\n`, `Index: ${padding}${name}\n--- a.ts\t\n+++ b.ts\t\n`, `diff -r abc -r def ${padding}${name}\n--- a.ts\t\n+++ b.ts\t\n`, ].map((header) => `${header}@@ -1 +1 @@\n-old\n+new\n`) const result = Bun.spawnSync({ cmd: [ process.execPath, "--eval", `import { completePatchContents } from "./session-diff.ts" console.log(JSON.stringify((await Bun.stdin.json()).map(completePatchContents)))`, ], cwd: import.meta.dir, stdin: Buffer.from(JSON.stringify(patches)), timeout: 5_000, killSignal: "SIGKILL", })
expect(result.exitCode, result.stderr.toString()).toBe(0) expect(JSON.parse(result.stdout.toString())).toEqual(patches.map(() => ({ before: "old\n", after: "new\n" }))) }, 10_000, )
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 preloaded 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("") })})