mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-12 07:20:45 +00:00
refactor(test): dedupe gemini oauth fixture setup
This commit is contained in:
@@ -35,6 +35,67 @@ describe("extractGeminiCliCredentials", () => {
|
||||
|
||||
let originalPath: string | undefined;
|
||||
|
||||
function makeFakeLayout() {
|
||||
const binDir = join(rootDir, "fake", "bin");
|
||||
const geminiPath = join(binDir, "gemini");
|
||||
const resolvedPath = join(
|
||||
rootDir,
|
||||
"fake",
|
||||
"lib",
|
||||
"node_modules",
|
||||
"@google",
|
||||
"gemini-cli",
|
||||
"dist",
|
||||
"index.js",
|
||||
);
|
||||
const oauth2Path = join(
|
||||
rootDir,
|
||||
"fake",
|
||||
"lib",
|
||||
"node_modules",
|
||||
"@google",
|
||||
"gemini-cli",
|
||||
"node_modules",
|
||||
"@google",
|
||||
"gemini-cli-core",
|
||||
"dist",
|
||||
"src",
|
||||
"code_assist",
|
||||
"oauth2.js",
|
||||
);
|
||||
|
||||
return { binDir, geminiPath, resolvedPath, oauth2Path };
|
||||
}
|
||||
|
||||
function installGeminiLayout(params: {
|
||||
oauth2Exists?: boolean;
|
||||
oauth2Content?: string;
|
||||
readdir?: string[];
|
||||
}) {
|
||||
const layout = makeFakeLayout();
|
||||
process.env.PATH = layout.binDir;
|
||||
|
||||
mockExistsSync.mockImplementation((p: string) => {
|
||||
const normalized = normalizePath(p);
|
||||
if (normalized === normalizePath(layout.geminiPath)) {
|
||||
return true;
|
||||
}
|
||||
if (params.oauth2Exists && normalized === normalizePath(layout.oauth2Path)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
mockRealpathSync.mockReturnValue(layout.resolvedPath);
|
||||
if (params.oauth2Content !== undefined) {
|
||||
mockReadFileSync.mockReturnValue(params.oauth2Content);
|
||||
}
|
||||
if (params.readdir) {
|
||||
mockReaddirSync.mockReturnValue(params.readdir);
|
||||
}
|
||||
|
||||
return layout;
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
vi.clearAllMocks();
|
||||
originalPath = process.env.PATH;
|
||||
@@ -54,48 +115,7 @@ describe("extractGeminiCliCredentials", () => {
|
||||
});
|
||||
|
||||
it("extracts credentials from oauth2.js in known path", async () => {
|
||||
const fakeBinDir = join(rootDir, "fake", "bin");
|
||||
const fakeGeminiPath = join(fakeBinDir, "gemini");
|
||||
const fakeResolvedPath = join(
|
||||
rootDir,
|
||||
"fake",
|
||||
"lib",
|
||||
"node_modules",
|
||||
"@google",
|
||||
"gemini-cli",
|
||||
"dist",
|
||||
"index.js",
|
||||
);
|
||||
const fakeOauth2Path = join(
|
||||
rootDir,
|
||||
"fake",
|
||||
"lib",
|
||||
"node_modules",
|
||||
"@google",
|
||||
"gemini-cli",
|
||||
"node_modules",
|
||||
"@google",
|
||||
"gemini-cli-core",
|
||||
"dist",
|
||||
"src",
|
||||
"code_assist",
|
||||
"oauth2.js",
|
||||
);
|
||||
|
||||
process.env.PATH = fakeBinDir;
|
||||
|
||||
mockExistsSync.mockImplementation((p: string) => {
|
||||
const normalized = normalizePath(p);
|
||||
if (normalized === normalizePath(fakeGeminiPath)) {
|
||||
return true;
|
||||
}
|
||||
if (normalized === normalizePath(fakeOauth2Path)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
mockRealpathSync.mockReturnValue(fakeResolvedPath);
|
||||
mockReadFileSync.mockReturnValue(FAKE_OAUTH2_CONTENT);
|
||||
installGeminiLayout({ oauth2Exists: true, oauth2Content: FAKE_OAUTH2_CONTENT });
|
||||
|
||||
const { extractGeminiCliCredentials, clearCredentialsCache } = await import("./oauth.js");
|
||||
clearCredentialsCache();
|
||||
@@ -108,26 +128,7 @@ describe("extractGeminiCliCredentials", () => {
|
||||
});
|
||||
|
||||
it("returns null when oauth2.js cannot be found", async () => {
|
||||
const fakeBinDir = join(rootDir, "fake", "bin");
|
||||
const fakeGeminiPath = join(fakeBinDir, "gemini");
|
||||
const fakeResolvedPath = join(
|
||||
rootDir,
|
||||
"fake",
|
||||
"lib",
|
||||
"node_modules",
|
||||
"@google",
|
||||
"gemini-cli",
|
||||
"dist",
|
||||
"index.js",
|
||||
);
|
||||
|
||||
process.env.PATH = fakeBinDir;
|
||||
|
||||
mockExistsSync.mockImplementation(
|
||||
(p: string) => normalizePath(p) === normalizePath(fakeGeminiPath),
|
||||
);
|
||||
mockRealpathSync.mockReturnValue(fakeResolvedPath);
|
||||
mockReaddirSync.mockReturnValue([]); // Empty directory for recursive search
|
||||
installGeminiLayout({ oauth2Exists: false, readdir: [] });
|
||||
|
||||
const { extractGeminiCliCredentials, clearCredentialsCache } = await import("./oauth.js");
|
||||
clearCredentialsCache();
|
||||
@@ -135,48 +136,7 @@ describe("extractGeminiCliCredentials", () => {
|
||||
});
|
||||
|
||||
it("returns null when oauth2.js lacks credentials", async () => {
|
||||
const fakeBinDir = join(rootDir, "fake", "bin");
|
||||
const fakeGeminiPath = join(fakeBinDir, "gemini");
|
||||
const fakeResolvedPath = join(
|
||||
rootDir,
|
||||
"fake",
|
||||
"lib",
|
||||
"node_modules",
|
||||
"@google",
|
||||
"gemini-cli",
|
||||
"dist",
|
||||
"index.js",
|
||||
);
|
||||
const fakeOauth2Path = join(
|
||||
rootDir,
|
||||
"fake",
|
||||
"lib",
|
||||
"node_modules",
|
||||
"@google",
|
||||
"gemini-cli",
|
||||
"node_modules",
|
||||
"@google",
|
||||
"gemini-cli-core",
|
||||
"dist",
|
||||
"src",
|
||||
"code_assist",
|
||||
"oauth2.js",
|
||||
);
|
||||
|
||||
process.env.PATH = fakeBinDir;
|
||||
|
||||
mockExistsSync.mockImplementation((p: string) => {
|
||||
const normalized = normalizePath(p);
|
||||
if (normalized === normalizePath(fakeGeminiPath)) {
|
||||
return true;
|
||||
}
|
||||
if (normalized === normalizePath(fakeOauth2Path)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
mockRealpathSync.mockReturnValue(fakeResolvedPath);
|
||||
mockReadFileSync.mockReturnValue("// no credentials here");
|
||||
installGeminiLayout({ oauth2Exists: true, oauth2Content: "// no credentials here" });
|
||||
|
||||
const { extractGeminiCliCredentials, clearCredentialsCache } = await import("./oauth.js");
|
||||
clearCredentialsCache();
|
||||
@@ -184,48 +144,7 @@ describe("extractGeminiCliCredentials", () => {
|
||||
});
|
||||
|
||||
it("caches credentials after first extraction", async () => {
|
||||
const fakeBinDir = join(rootDir, "fake", "bin");
|
||||
const fakeGeminiPath = join(fakeBinDir, "gemini");
|
||||
const fakeResolvedPath = join(
|
||||
rootDir,
|
||||
"fake",
|
||||
"lib",
|
||||
"node_modules",
|
||||
"@google",
|
||||
"gemini-cli",
|
||||
"dist",
|
||||
"index.js",
|
||||
);
|
||||
const fakeOauth2Path = join(
|
||||
rootDir,
|
||||
"fake",
|
||||
"lib",
|
||||
"node_modules",
|
||||
"@google",
|
||||
"gemini-cli",
|
||||
"node_modules",
|
||||
"@google",
|
||||
"gemini-cli-core",
|
||||
"dist",
|
||||
"src",
|
||||
"code_assist",
|
||||
"oauth2.js",
|
||||
);
|
||||
|
||||
process.env.PATH = fakeBinDir;
|
||||
|
||||
mockExistsSync.mockImplementation((p: string) => {
|
||||
const normalized = normalizePath(p);
|
||||
if (normalized === normalizePath(fakeGeminiPath)) {
|
||||
return true;
|
||||
}
|
||||
if (normalized === normalizePath(fakeOauth2Path)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
mockRealpathSync.mockReturnValue(fakeResolvedPath);
|
||||
mockReadFileSync.mockReturnValue(FAKE_OAUTH2_CONTENT);
|
||||
installGeminiLayout({ oauth2Exists: true, oauth2Content: FAKE_OAUTH2_CONTENT });
|
||||
|
||||
const { extractGeminiCliCredentials, clearCredentialsCache } = await import("./oauth.js");
|
||||
clearCredentialsCache();
|
||||
|
||||
Reference in New Issue
Block a user