fileChangeFindSearch.test.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { describe, expect, it } from "vitest";
  2. import { getFileChangeFindState } from "@/GitPane/fileChangeFindSearch.js";
  3. import { getGitPaneDiffFindContent } from "@/GitPane/helpers.js";
  4. const contentA = [
  5. " const first = needle;",
  6. "-const oldValue = nope;",
  7. "+const newValue = Needle;",
  8. ].join("\n");
  9. const contentB = [
  10. " const value = needle;",
  11. ].join("\n");
  12. describe("fileChangeFindSearch", () => {
  13. it("collects matches across diff contents in file order", () => {
  14. const state = getFileChangeFindState(
  15. [
  16. { path: "/workspace/src/App.tsx", content: contentA },
  17. { path: "/workspace/src/Other.ts", content: contentB },
  18. ],
  19. "needle",
  20. 0,
  21. );
  22. expect(state.total).toBe(3);
  23. expect(state.matches.map((match) => [match.path, match.matchIndexInFile])).toEqual([
  24. ["/workspace/src/App.tsx", 0],
  25. ["/workspace/src/App.tsx", 1],
  26. ["/workspace/src/Other.ts", 0],
  27. ]);
  28. });
  29. it("resolves the preferred global match to an active file match", () => {
  30. const state = getFileChangeFindState(
  31. [
  32. { path: "/workspace/src/App.tsx", content: contentA },
  33. { path: "/workspace/src/Other.ts", content: contentB },
  34. ],
  35. "needle",
  36. 2,
  37. );
  38. expect(state.currentIndex).toBe(2);
  39. expect(state.activeMatch).toEqual({
  40. path: "/workspace/src/Other.ts",
  41. globalIndex: 2,
  42. matchIndexInFile: 0,
  43. });
  44. });
  45. it("returns an empty state before a diff is available", () => {
  46. const state = getFileChangeFindState([{ path: "/workspace/src/App.tsx", content: null }], "needle", 0);
  47. expect(state.total).toBe(0);
  48. expect(state.currentIndex).toBe(-1);
  49. expect(state.activeMatch).toBeNull();
  50. });
  51. it("searches only visible Git patch contents when the complete content pair is unavailable", () => {
  52. const path = "/workspace/src/App.tsx";
  53. const content = getGitPaneDiffFindContent({
  54. path,
  55. availability: "patch",
  56. patch: [
  57. "diff --git a/src/App.tsx b/src/App.tsx",
  58. "index 1111111..2222222 100644",
  59. "--- a/src/App.tsx",
  60. "+++ b/src/App.tsx",
  61. "@@ -1 +1,2 @@",
  62. " const app = true;",
  63. "+const needle = true;",
  64. ].join("\n"),
  65. beforeContent: null,
  66. afterContent: null,
  67. summary: null,
  68. });
  69. expect(content).toBe("const app = true;\nconst needle = true;");
  70. expect(getFileChangeFindState([{ path, content }], "needle", 0)).toMatchObject({
  71. total: 1,
  72. currentIndex: 0,
  73. activeMatch: {
  74. path,
  75. globalIndex: 0,
  76. matchIndexInFile: 0,
  77. },
  78. });
  79. expect(getFileChangeFindState([{ path, content }], "App.tsx", 0).total).toBe(0);
  80. expect(getFileChangeFindState([{ path, content }], "index", 0).total).toBe(0);
  81. expect(getFileChangeFindState([{ path, content }], "@@", 0).total).toBe(0);
  82. });
  83. });