refactor(auth): separate browser credential parsing

This commit is contained in:
Ivan Pereira
2026-06-24 17:51:37 +01:00
parent 2b107f13d1
commit f8919e254c
5 changed files with 254 additions and 210 deletions
+41
View File
@@ -1,5 +1,7 @@
import { afterEach, describe, expect, mock, test } from "./test-helpers.js";
import { SearchError } from "../src/search/types.js";
afterEach(() => {
mock.restore();
});
@@ -66,4 +68,43 @@ describe("perplexity_search execute", () => {
expect(result.details.incognito).toBe(false);
expect(result.details.model).toBe("gpt54");
});
test("does not clear cached credentials on Perplexity auth rejection", async () => {
const authenticate = mock(async () => "jwt-token");
const saveBrowserAuthInput = mock(async () => ({ type: "oauth", access: "jwt-token" }));
const clearToken = mock(async () => undefined);
const loadConfig = mock(async () => ({}));
const resolveSearchDefaults = mock(() => ({ model: "pplx_pro_upgraded", incognito: true }));
const searchPerplexity = mock(async () => {
throw new SearchError("AUTH", "Perplexity rejected authentication (401/403).");
});
mock.module("../src/auth/login.js", () => ({ authenticate, saveBrowserAuthInput }));
mock.module("../src/auth/storage.js", () => ({ clearToken }));
mock.module("../src/config.js", () => ({
getConfigPath: () => "/tmp/pi-perplexity-config.json",
loadConfig,
resolveSearchDefaults,
saveConfig: mock(async () => undefined),
}));
mock.module("../src/search/client.js", () => ({ searchPerplexity }));
const { default: registerExtension } = await import(`../src/index.js?test=${crypto.randomUUID()}`);
let execute: ((toolCallId: string, params: any, signal?: AbortSignal, onUpdate?: any, ctx?: any) => Promise<any>) | undefined;
registerExtension({
registerCommand() {
return undefined;
},
registerTool(tool: { execute: typeof execute }) {
execute = tool.execute;
},
} as any);
const result = await execute!("tool-1", { query: "hello" }, undefined, undefined, { ui: {} });
expect(result.details.isError).toBe(true);
expect(String(result.content[0].text)).toContain("Perplexity search failed");
expect(clearToken).toHaveBeenCalledTimes(0);
});
});