session-diff.test.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { describe, expect, test } from "bun:test"
  2. import { normalize, text } from "./session-diff"
  3. describe("session diff", () => {
  4. test("keeps unified patch content", () => {
  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.patch).toBe(diff.patch)
  15. expect(view.fileDiff.name).toBe("a.ts")
  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("converts legacy content into a patch", () => {
  33. const diff = {
  34. file: "a.ts",
  35. before: "one\n",
  36. after: "two\n",
  37. additions: 1,
  38. deletions: 1,
  39. status: "modified" as const,
  40. }
  41. const view = normalize(diff)
  42. expect(view.patch).toContain("@@ -1,1 +1,1 @@")
  43. expect(text(view, "deletions")).toBe("one\n")
  44. expect(text(view, "additions")).toBe("two\n")
  45. })
  46. test("ignores malformed persisted patches", () => {
  47. const diff = {
  48. file: "a.ts",
  49. patch:
  50. "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",
  51. additions: 1,
  52. deletions: 1,
  53. status: "modified" as const,
  54. }
  55. const view = normalize(diff)
  56. expect(view.patch).toBe(diff.patch)
  57. expect(text(view, "deletions")).toBe("")
  58. expect(text(view, "additions")).toBe("")
  59. })
  60. })