session-diff.test.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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:
  36. "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",
  37. })
  38. expect(fileDiff.isPartial).toBe(false)
  39. expect(fileDiff.additionLines).toEqual(["one\n", "new\n"])
  40. })
  41. test("keeps ordinary leading tool patches partial", () => {
  42. const fileDiff = resolveFileDiff({
  43. file: "a.ts",
  44. patch:
  45. "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",
  46. })
  47. expect(fileDiff.isPartial).toBe(true)
  48. expect(fileDiff.additionLines).toEqual(["new\n", "two\n", "three\n", "four\n", "five\n"])
  49. })
  50. test("keeps separated patch hunks partial without complete file contents", () => {
  51. const fileDiff = resolveFileDiff({
  52. file: "project.ts",
  53. patch:
  54. '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',
  55. })
  56. expect(fileDiff.isPartial).toBe(true)
  57. expect(fileDiff.hunks).toHaveLength(2)
  58. expect(fileDiff.hunks[1]?.collapsedBefore).toBeGreaterThan(0)
  59. })
  60. test("renders headerless persisted patches", () => {
  61. const view = normalize({
  62. file: "a.ts",
  63. patch: "@@ -1 +1 @@\n-old\n+new\n",
  64. additions: 1,
  65. deletions: 1,
  66. status: "modified" as const,
  67. })
  68. expect(view.fileDiff.name).toBe("a.ts")
  69. expect(view.fileDiff.isPartial).toBe(true)
  70. expect(text(view, "deletions")).toBe("old\n")
  71. expect(text(view, "additions")).toBe("new\n")
  72. })
  73. test("does not share headerless patch metadata between files", () => {
  74. const patch = "@@ -1 +1 @@\n-old\n+new\n"
  75. expect(resolveFileDiff({ file: "a.ts", patch }).name).toBe("a.ts")
  76. expect(resolveFileDiff({ file: "b.ts", patch }).name).toBe("b.ts")
  77. })
  78. test("keeps capped header-only patches partial", () => {
  79. const fileDiff = resolveFileDiff({
  80. file: "a.ts",
  81. patch:
  82. "Index: a.ts\n===================================================================\n--- a.ts\t\n+++ a.ts\t\n",
  83. })
  84. expect(fileDiff.name).toBe("a.ts")
  85. expect(fileDiff.isPartial).toBe(true)
  86. expect(fileDiff.hunks).toEqual([])
  87. })
  88. test("keeps full legacy content as a complete diff", () => {
  89. const diff = {
  90. file: "a.ts",
  91. before: "one\n",
  92. after: "two\n",
  93. additions: 1,
  94. deletions: 1,
  95. status: "modified" as const,
  96. }
  97. const view = normalize(diff)
  98. expect(view.fileDiff.isPartial).toBe(false)
  99. expect(text(view, "deletions")).toBe("one\n")
  100. expect(text(view, "additions")).toBe("two\n")
  101. })
  102. test("ignores malformed persisted patches", () => {
  103. const diff = {
  104. file: "a.ts",
  105. patch:
  106. "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",
  107. additions: 1,
  108. deletions: 1,
  109. status: "modified" as const,
  110. }
  111. const view = normalize(diff)
  112. expect(text(view, "deletions")).toBe("")
  113. expect(text(view, "additions")).toBe("")
  114. })
  115. })