git-snapshot.test.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { beforeEach, describe, expect, it, vi } from "vitest";
  2. interface GitResponse {
  3. code: number;
  4. stdout?: string;
  5. stderr?: string;
  6. }
  7. interface GitCall {
  8. args: string[];
  9. command: string;
  10. options: {
  11. cwd?: string;
  12. maxBuffer?: number;
  13. timeout?: number;
  14. windowsHide?: boolean;
  15. };
  16. }
  17. const { execFileMock, gitCalls, resetGitMock, setGitResponse } = vi.hoisted(
  18. () => {
  19. const calls: GitCall[] = [];
  20. const responses = new Map<string, GitResponse>();
  21. const commandKey = (args: string[]) => args.join("\0");
  22. const runGitMock = (command: string, args: string[], options: GitCall["options"]) => {
  23. calls.push({ args: [...args], command, options });
  24. const response = responses.get(commandKey(args)) ?? { code: 1, stdout: "", stderr: "" };
  25. const stdout = response.stdout ?? "";
  26. const stderr = response.stderr ?? "";
  27. if (response.code === 0) {
  28. return { stdout, stderr };
  29. }
  30. const error = new Error(`git exited with ${response.code}`) as Error & {
  31. code?: number;
  32. stderr?: string;
  33. stdout?: string;
  34. };
  35. error.code = response.code;
  36. error.stdout = stdout;
  37. error.stderr = stderr;
  38. throw error;
  39. };
  40. const mock = vi.fn(
  41. (
  42. command: string,
  43. args: string[],
  44. options: GitCall["options"],
  45. callback: (error: Error | null, stdout: string, stderr: string) => void,
  46. ) => {
  47. try {
  48. const result = runGitMock(command, args, options);
  49. callback(null, result.stdout, result.stderr);
  50. } catch (error) {
  51. const execError = error as Error & { stderr?: string; stdout?: string };
  52. callback(execError, execError.stdout ?? "", execError.stderr ?? "");
  53. }
  54. },
  55. );
  56. const customPromisify = Symbol.for("nodejs.util.promisify.custom");
  57. Object.defineProperty(mock, customPromisify, {
  58. value: (command: string, args: string[], options: GitCall["options"]) =>
  59. Promise.resolve().then(() => runGitMock(command, args, options)),
  60. });
  61. return {
  62. execFileMock: mock,
  63. gitCalls: calls,
  64. resetGitMock: () => {
  65. calls.length = 0;
  66. mock.mockClear();
  67. responses.clear();
  68. },
  69. setGitResponse: (args: string[], response: GitResponse) => {
  70. responses.set(commandKey(args), response);
  71. },
  72. };
  73. },
  74. );
  75. vi.mock("node:child_process", () => ({
  76. execFile: execFileMock,
  77. }));
  78. describe("resolveGitSnapshot command semantics", () => {
  79. beforeEach(() => {
  80. resetGitMock();
  81. });
  82. it("keeps the local three second timeout for every git snapshot command", async () => {
  83. setGitResponse(["rev-parse", "--is-inside-work-tree"], { code: 0, stdout: "true\n" });
  84. setGitResponse(["rev-parse", "--abbrev-ref", "HEAD"], { code: 0, stdout: "feature\n" });
  85. setGitResponse(["symbolic-ref", "--short", "refs/remotes/origin/HEAD"], {
  86. code: 0,
  87. stdout: "origin/main\n",
  88. });
  89. setGitResponse(["show-ref", "--verify", "--quiet", "refs/remotes/origin/main"], {
  90. code: 0,
  91. });
  92. setGitResponse(["config", "user.name"], { code: 0, stdout: "ZCode Tester\n" });
  93. setGitResponse(["--no-optional-locks", "status", "--short"], {
  94. code: 0,
  95. stdout: " M tracked.txt\n",
  96. });
  97. setGitResponse(["--no-optional-locks", "log", "--oneline", "-n", "5"], {
  98. code: 0,
  99. stdout: "abc123 first commit\n",
  100. });
  101. const { resolveGitSnapshot } = await import("../src/context/git-snapshot.js");
  102. const snapshot = await resolveGitSnapshot("/workspace");
  103. expect(snapshot.isGitRepository).toBe(true);
  104. expect(gitCalls).not.toHaveLength(0);
  105. expect(gitCalls.every((call) => call.command === "git")).toBe(true);
  106. expect(gitCalls.every((call) => call.options.cwd === "/workspace")).toBe(true);
  107. expect(gitCalls.every((call) => call.options.maxBuffer === 1024 * 1024)).toBe(true);
  108. expect(gitCalls.every((call) => call.options.timeout === 3000)).toBe(true);
  109. expect(gitCalls.every((call) => call.options.windowsHide === true)).toBe(true);
  110. });
  111. it("drops stdout from failed optional git commands and keeps the snapshot usable", async () => {
  112. setGitResponse(["rev-parse", "--is-inside-work-tree"], { code: 0, stdout: "true\n" });
  113. setGitResponse(["rev-parse", "--abbrev-ref", "HEAD"], { code: 0, stdout: "feature\n" });
  114. setGitResponse(["symbolic-ref", "--short", "refs/remotes/origin/HEAD"], {
  115. code: 1,
  116. stdout: "origin/should-not-leak\n",
  117. });
  118. setGitResponse(["show-ref", "--verify", "--quiet", "refs/remotes/origin/main"], {
  119. code: 1,
  120. });
  121. setGitResponse(["show-ref", "--verify", "--quiet", "refs/remotes/origin/master"], {
  122. code: 1,
  123. });
  124. setGitResponse(["config", "user.name"], { code: 1, stdout: "Leaked User\n" });
  125. setGitResponse(["--no-optional-locks", "status", "--short"], {
  126. code: 128,
  127. stdout: " M leaked.txt\n",
  128. });
  129. setGitResponse(["--no-optional-locks", "log", "--oneline", "-n", "5"], {
  130. code: 128,
  131. stdout: "abc123 leaked commit\n",
  132. });
  133. const { resolveGitSnapshot } = await import("../src/context/git-snapshot.js");
  134. const snapshot = await resolveGitSnapshot("/workspace");
  135. expect(snapshot).toMatchObject({
  136. isGitRepository: true,
  137. gitBranch: "feature",
  138. gitMainBranch: "main",
  139. gitStatus: "clean",
  140. gitStatusLines: [],
  141. recentCommits: [],
  142. });
  143. expect(snapshot.gitUser).toBeUndefined();
  144. });
  145. });