session-diff.test.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { describe, expect, test } from "bun:test"
  2. import { normalize, resolveFileDiff, text } from "./session-diff"
  3. describe("session diff", () => {
  4. test("renders whole-file unified patches as complete diffs", () => {
  5. const diff = {
  6. file: "a.ts",
  7. patch:
  8. "Index: a.ts\n===================================================================\n--- a.ts\t\n+++ a.ts\t\n@@ -1,2 +1,2 @@\n one\n-two\n+three\n",
  9. additions: 1,
  10. deletions: 1,
  11. status: "modified" as const,
  12. }
  13. const view = normalize(diff)
  14. expect(view.fileDiff.name).toBe("a.ts")
  15. expect(view.fileDiff.isPartial).toBe(false)
  16. expect(text(view, "deletions")).toBe("one\ntwo\n")
  17. expect(text(view, "additions")).toBe("one\nthree\n")
  18. })
  19. test("keeps missing final newlines from unified patches", () => {
  20. const diff = {
  21. file: "a.ts",
  22. patch:
  23. "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",
  24. additions: 1,
  25. deletions: 1,
  26. status: "modified" as const,
  27. }
  28. const view = normalize(diff)
  29. expect(text(view, "deletions")).toBe("one\ntwo")
  30. expect(text(view, "additions")).toBe("one\nthree")
  31. })
  32. test("renders whole-file VCS patches as complete diffs", () => {
  33. const fileDiff = resolveFileDiff({
  34. file: "a.ts",
  35. 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",
  36. })
  37. expect(fileDiff.isPartial).toBe(false)
  38. expect(fileDiff.additionLines).toEqual(["one\n", "new\n"])
  39. })
  40. test("keeps ordinary leading tool patches partial", () => {
  41. const fileDiff = resolveFileDiff({
  42. file: "a.ts",
  43. 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",
  44. })
  45. expect(fileDiff.isPartial).toBe(true)
  46. expect(fileDiff.additionLines).toEqual(["new\n", "two\n", "three\n", "four\n", "five\n"])
  47. })
  48. test("keeps separated patch hunks partial without complete file contents", () => {
  49. const fileDiff = resolveFileDiff({
  50. file: "project.ts",
  51. patch:
  52. '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',
  53. })
  54. expect(fileDiff.isPartial).toBe(true)
  55. expect(fileDiff.hunks).toHaveLength(2)
  56. expect(fileDiff.hunks[1]?.collapsedBefore).toBeGreaterThan(0)
  57. })
  58. test("renders headerless persisted patches", () => {
  59. const view = normalize({
  60. file: "a.ts",
  61. patch: "@@ -1 +1 @@\n-old\n+new\n",
  62. additions: 1,
  63. deletions: 1,
  64. status: "modified" as const,
  65. })
  66. expect(view.fileDiff.name).toBe("a.ts")
  67. expect(view.fileDiff.isPartial).toBe(true)
  68. expect(text(view, "deletions")).toBe("old\n")
  69. expect(text(view, "additions")).toBe("new\n")
  70. })
  71. test("does not share headerless patch metadata between files", () => {
  72. const patch = "@@ -1 +1 @@\n-old\n+new\n"
  73. expect(resolveFileDiff({ file: "a.ts", patch }).name).toBe("a.ts")
  74. expect(resolveFileDiff({ file: "b.ts", patch }).name).toBe("b.ts")
  75. })
  76. test("keeps capped header-only patches partial", () => {
  77. const fileDiff = resolveFileDiff({
  78. file: "a.ts",
  79. patch:
  80. "Index: a.ts\n===================================================================\n--- a.ts\t\n+++ a.ts\t\n",
  81. })
  82. expect(fileDiff.name).toBe("a.ts")
  83. expect(fileDiff.isPartial).toBe(true)
  84. expect(fileDiff.hunks).toEqual([])
  85. })
  86. test("keeps full legacy content as a complete diff", () => {
  87. const diff = {
  88. file: "a.ts",
  89. before: "one\n",
  90. after: "two\n",
  91. additions: 1,
  92. deletions: 1,
  93. status: "modified" as const,
  94. }
  95. const view = normalize(diff)
  96. expect(view.fileDiff.isPartial).toBe(false)
  97. expect(text(view, "deletions")).toBe("one\n")
  98. expect(text(view, "additions")).toBe("two\n")
  99. })
  100. test("ignores malformed persisted patches", () => {
  101. const diff = {
  102. file: "a.ts",
  103. patch:
  104. "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",
  105. additions: 1,
  106. deletions: 1,
  107. status: "modified" as const,
  108. }
  109. const view = normalize(diff)
  110. expect(text(view, "deletions")).toBe("")
  111. expect(text(view, "additions")).toBe("")
  112. })
  113. })