|
| 1 | +import fs from "node:fs"; |
| 2 | +import fsp from "node:fs/promises"; |
| 3 | +import os from "node:os"; |
| 4 | +import path from "node:path"; |
| 5 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 6 | +import { |
| 7 | + resolveGeminiImageDirWinPath, |
| 8 | + resolveGeminiProjectIdentifier, |
| 9 | + resolveGeminiProjectTempRootWinPath, |
| 10 | +} from "./projectTemp"; |
| 11 | +import { getDistroHomeSubPathUNCAsync } from "../wsl"; |
| 12 | + |
| 13 | +vi.mock("../wsl", () => ({ |
| 14 | + getDistroHomeSubPathUNCAsync: vi.fn(), |
| 15 | +})); |
| 16 | + |
| 17 | +/** |
| 18 | + * 中文说明:创建临时测试目录,并确保用例结束后清理。 |
| 19 | + */ |
| 20 | +function createTempDir(prefix: string): string { |
| 21 | + return fs.mkdtempSync(path.join(os.tmpdir(), prefix)); |
| 22 | +} |
| 23 | + |
| 24 | +describe("Gemini 项目临时目录解析", () => { |
| 25 | + const tempDirs: string[] = []; |
| 26 | + |
| 27 | + afterEach(async () => { |
| 28 | + try { vi.restoreAllMocks(); } catch {} |
| 29 | + try { vi.unstubAllEnvs(); } catch {} |
| 30 | + delete process.env.GEMINI_CLI_HOME; |
| 31 | + while (tempDirs.length > 0) { |
| 32 | + const dir = tempDirs.pop(); |
| 33 | + if (!dir) continue; |
| 34 | + try { await fsp.rm(dir, { recursive: true, force: true }); } catch {} |
| 35 | + } |
| 36 | + }); |
| 37 | + |
| 38 | + it("优先读取 projects.json 中已存在的 shortId 映射", async () => { |
| 39 | + const geminiHome = createTempDir("gemini-home-"); |
| 40 | + tempDirs.push(geminiHome); |
| 41 | + process.env.GEMINI_CLI_HOME = geminiHome; |
| 42 | + |
| 43 | + const normalizedProjectRoot = "g:\\projects\\demo"; |
| 44 | + await fsp.mkdir(path.join(geminiHome, "tmp"), { recursive: true }); |
| 45 | + await fsp.writeFile( |
| 46 | + path.join(geminiHome, "projects.json"), |
| 47 | + JSON.stringify({ projects: { [normalizedProjectRoot]: "demo-app" } }), |
| 48 | + "utf8", |
| 49 | + ); |
| 50 | + |
| 51 | + const projectId = await resolveGeminiProjectIdentifier({ |
| 52 | + projectWinRoot: "G:\\Projects\\demo", |
| 53 | + runtimeEnv: "windows", |
| 54 | + }); |
| 55 | + |
| 56 | + expect(projectId).toBe("demo-app"); |
| 57 | + }); |
| 58 | + |
| 59 | + it("registry 缺失时会根据 marker 反查 shortId", async () => { |
| 60 | + const geminiHome = createTempDir("gemini-home-"); |
| 61 | + tempDirs.push(geminiHome); |
| 62 | + process.env.GEMINI_CLI_HOME = geminiHome; |
| 63 | + |
| 64 | + const markerDir = path.join(geminiHome, "tmp", "demo-worktree"); |
| 65 | + await fsp.mkdir(markerDir, { recursive: true }); |
| 66 | + await fsp.writeFile(path.join(markerDir, ".project_root"), "/home/lulu/demo", "utf8"); |
| 67 | + |
| 68 | + const projectId = await resolveGeminiProjectIdentifier({ |
| 69 | + projectWslRoot: "/home/lulu/demo", |
| 70 | + runtimeEnv: "wsl", |
| 71 | + }); |
| 72 | + |
| 73 | + expect(projectId).toBe("demo-worktree"); |
| 74 | + }); |
| 75 | + |
| 76 | + it("无 registry/marker 时会按 Gemini slug 规则声明新的 shortId", async () => { |
| 77 | + const geminiHome = createTempDir("gemini-home-"); |
| 78 | + tempDirs.push(geminiHome); |
| 79 | + process.env.GEMINI_CLI_HOME = geminiHome; |
| 80 | + |
| 81 | + const projectId = await resolveGeminiProjectIdentifier({ |
| 82 | + projectWinRoot: "G:\\Projects\\My Demo", |
| 83 | + runtimeEnv: "windows", |
| 84 | + }); |
| 85 | + |
| 86 | + expect(projectId).toBe("my-demo"); |
| 87 | + expect(fs.existsSync(path.join(geminiHome, "tmp", "my-demo", ".project_root"))).toBe(true); |
| 88 | + }); |
| 89 | + |
| 90 | + it("WSL 运行时将 Gemini temp 目录解析为 UNC 路径", async () => { |
| 91 | + vi.spyOn(os, "platform").mockReturnValue("win32" as any); |
| 92 | + vi.mocked(getDistroHomeSubPathUNCAsync).mockResolvedValue("\\\\wsl.localhost\\Ubuntu-24.04\\home\\lulu\\.gemini"); |
| 93 | + const originalReadFile = fsp.readFile.bind(fsp); |
| 94 | + vi.spyOn(fsp, "readFile").mockImplementation(async (targetPath: any, ...args: any[]) => { |
| 95 | + const normalizedPath = String(targetPath || ""); |
| 96 | + if (normalizedPath === "\\\\wsl.localhost\\Ubuntu-24.04\\home\\lulu\\.gemini\\projects.json") |
| 97 | + return JSON.stringify({ projects: { "/home/lulu/demo": "demo" } }) as any; |
| 98 | + return await (originalReadFile as any)(targetPath, ...args); |
| 99 | + }); |
| 100 | + |
| 101 | + const tempRoot = await resolveGeminiProjectTempRootWinPath({ |
| 102 | + projectWslRoot: "/home/lulu/demo", |
| 103 | + runtimeEnv: "wsl", |
| 104 | + distro: "Ubuntu-24.04", |
| 105 | + }); |
| 106 | + const imageRoot = await resolveGeminiImageDirWinPath({ |
| 107 | + projectWslRoot: "/home/lulu/demo", |
| 108 | + runtimeEnv: "wsl", |
| 109 | + distro: "Ubuntu-24.04", |
| 110 | + }); |
| 111 | + |
| 112 | + expect(tempRoot).toBe("\\\\wsl.localhost\\Ubuntu-24.04\\home\\lulu\\.gemini\\tmp\\demo"); |
| 113 | + expect(imageRoot).toBe("\\\\wsl.localhost\\Ubuntu-24.04\\home\\lulu\\.gemini\\tmp\\demo\\images"); |
| 114 | + }); |
| 115 | +}); |
0 commit comments