

























@@ -31,6 +31,37 @@ async function readAuthJson(agentDir: string) {
3131return JSON.parse(await fs.readFile(authPath, "utf8")) as Record<string, unknown>;
3232}
333334+function requireAuthEntry(
35+auth: Record<string, unknown>,
36+provider: string,
37+): Record<string, unknown> {
38+const entry = auth[provider];
39+if (!entry || typeof entry !== "object") {
40+throw new Error(`expected auth entry ${provider}`);
41+}
42+return entry as Record<string, unknown>;
43+}
44+45+function expectApiKeyAuth(auth: Record<string, unknown>, provider: string, key: string): void {
46+const entry = requireAuthEntry(auth, provider);
47+expect(entry.type).toBe("api_key");
48+expect(entry.key).toBe(key);
49+}
50+51+function expectOAuthAuth(
52+auth: Record<string, unknown>,
53+provider: string,
54+access: string,
55+refresh?: string,
56+): void {
57+const entry = requireAuthEntry(auth, provider);
58+expect(entry.type).toBe("oauth");
59+expect(entry.access).toBe(access);
60+if (refresh !== undefined) {
61+expect(entry.refresh).toBe(refresh);
62+}
63+}
64+3465describe("ensurePiAuthJsonFromAuthProfiles", () => {
3566it("writes openai-codex oauth credentials into auth.json for pi-coding-agent discovery", async () => {
3667const agentDir = await createAgentDir();
@@ -49,11 +80,7 @@ describe("ensurePiAuthJsonFromAuthProfiles", () => {
4980expect(first.wrote).toBe(true);
50815182const auth = await readAuthJson(agentDir);
52-expect(auth["openai-codex"]).toMatchObject({
53-type: "oauth",
54-access: "access-token",
55-refresh: "refresh-token",
56-});
83+expectOAuthAuth(auth, "openai-codex", "access-token", "refresh-token");
57845885const second = await ensurePiAuthJsonFromAuthProfiles(agentDir);
5986expect(second.wrote).toBe(false);
@@ -74,10 +101,7 @@ describe("ensurePiAuthJsonFromAuthProfiles", () => {
74101expect(result.wrote).toBe(true);
7510276103const auth = await readAuthJson(agentDir);
77-expect(auth["openrouter"]).toMatchObject({
78-type: "api_key",
79-key: "sk-or-v1-test-key",
80-});
104+expectApiKeyAuth(auth, "openrouter", "sk-or-v1-test-key");
81105});
8210683107it("writes token credentials as api_key into auth.json", async () => {
@@ -95,10 +119,7 @@ describe("ensurePiAuthJsonFromAuthProfiles", () => {
95119expect(result.wrote).toBe(true);
9612097121const auth = await readAuthJson(agentDir);
98-expect(auth["anthropic"]).toMatchObject({
99-type: "api_key",
100-key: "sk-ant-test-token",
101-});
122+expectApiKeyAuth(auth, "anthropic", "sk-ant-test-token");
102123});
103124104125it("syncs multiple providers at once", async () => {
@@ -129,9 +150,9 @@ describe("ensurePiAuthJsonFromAuthProfiles", () => {
129150130151const auth = await readAuthJson(agentDir);
131152132-expect(auth["openrouter"]).toMatchObject({ type: "api_key", key: "sk-or-key" });
133-expect(auth["anthropic"]).toMatchObject({ type: "api_key", key: "sk-ant-token" });
134-expect(auth["openai-codex"]).toMatchObject({ type: "oauth", access: "access" });
153+expectApiKeyAuth(auth, "openrouter", "sk-or-key");
154+expectApiKeyAuth(auth, "anthropic", "sk-ant-token");
155+expectOAuthAuth(auth, "openai-codex", "access");
135156});
136157137158it("skips profiles with empty keys", async () => {
@@ -180,7 +201,7 @@ describe("ensurePiAuthJsonFromAuthProfiles", () => {
180201expect(result.wrote).toBe(true);
181202182203const auth = await readAuthJson(agentDir);
183-expect(auth["zai"]).toMatchObject({ type: "api_key", key: "sk-zai" });
204+expectApiKeyAuth(auth, "zai", "sk-zai");
184205expect(auth["z.ai"]).toBeUndefined();
185206});
186207@@ -205,8 +226,8 @@ describe("ensurePiAuthJsonFromAuthProfiles", () => {
205226await ensurePiAuthJsonFromAuthProfiles(agentDir);
206227207228const auth = await readAuthJson(agentDir);
208-expect(auth["legacy-provider"]).toMatchObject({ type: "api_key", key: "legacy-key" });
209-expect(auth["openrouter"]).toMatchObject({ type: "api_key", key: "new-key" });
229+expectApiKeyAuth(auth, "legacy-provider", "legacy-key");
230+expectApiKeyAuth(auth, "openrouter", "new-key");
210231});
211232212233it("treats malformed existing provider entries as stale and replaces them", async () => {
@@ -228,6 +249,6 @@ describe("ensurePiAuthJsonFromAuthProfiles", () => {
228249expect(result.wrote).toBe(true);
229250230251const auth = await readAuthJson(agentDir);
231-expect(auth["openrouter"]).toMatchObject({ type: "api_key", key: "new-key" });
252+expectApiKeyAuth(auth, "openrouter", "new-key");
232253});
233254});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。