|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import { withTempDir } from "./temp-dir.js"; |
| 5 | + |
| 6 | +const spawnSyncMock = vi.hoisted(() => vi.fn()); |
| 7 | + |
| 8 | +vi.mock("node:child_process", () => ({ |
| 9 | + spawnSync: spawnSyncMock, |
| 10 | +})); |
| 11 | + |
| 12 | +import { listGitTrackedFiles } from "./repo-files.js"; |
| 13 | + |
| 14 | +describe("listGitTrackedFiles", () => { |
| 15 | + beforeEach(() => { |
| 16 | + spawnSyncMock.mockReset(); |
| 17 | + }); |
| 18 | + |
| 19 | + it("bounds Git inventory lookup and returns sorted existing files", async () => { |
| 20 | + await withTempDir("openclaw-repo-files-", async (repoRoot) => { |
| 21 | + await fs.writeFile(path.join(repoRoot, "z.ts"), ""); |
| 22 | + await fs.writeFile(path.join(repoRoot, "a.ts"), ""); |
| 23 | + spawnSyncMock.mockReturnValue({ |
| 24 | + status: 0, |
| 25 | + stdout: "z.ts\nmissing.ts\na.ts\n", |
| 26 | + }); |
| 27 | + |
| 28 | + expect(listGitTrackedFiles({ repoRoot, pathspecs: "src" })).toEqual(["a.ts", "z.ts"]); |
| 29 | + expect(spawnSyncMock).toHaveBeenCalledWith( |
| 30 | + "git", |
| 31 | + ["ls-files", "--", "src"], |
| 32 | + expect.objectContaining({ |
| 33 | + cwd: repoRoot, |
| 34 | + killSignal: "SIGKILL", |
| 35 | + timeout: 5_000, |
| 36 | + }), |
| 37 | + ); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + it("caches a timed-out inventory lookup as unavailable", async () => { |
| 42 | + await withTempDir("openclaw-repo-files-timeout-", async (repoRoot) => { |
| 43 | + spawnSyncMock.mockReturnValue({ |
| 44 | + error: Object.assign(new Error("spawnSync git ETIMEDOUT"), { code: "ETIMEDOUT" }), |
| 45 | + signal: "SIGKILL", |
| 46 | + status: null, |
| 47 | + stdout: "", |
| 48 | + }); |
| 49 | + const params = { repoRoot, pathspecs: ["src", "extensions"] }; |
| 50 | + |
| 51 | + expect(listGitTrackedFiles(params)).toBeNull(); |
| 52 | + expect(listGitTrackedFiles(params)).toBeNull(); |
| 53 | + expect(spawnSyncMock).toHaveBeenCalledOnce(); |
| 54 | + }); |
| 55 | + }); |
| 56 | +}); |
0 commit comments