From 7fa895889c6879563087da9bbd40cc0a4d8816a9 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 10 May 2026 14:30:29 +0100 Subject: [PATCH] test: clear onboard auth broad matchers --- src/commands/onboard-auth.test.ts | 72 +++++++++++++++++++------------ 1 file changed, 44 insertions(+), 28 deletions(-) diff --git a/src/commands/onboard-auth.test.ts b/src/commands/onboard-auth.test.ts index 2118f9fbf9a..b356ecd65d7 100644 --- a/src/commands/onboard-auth.test.ts +++ b/src/commands/onboard-auth.test.ts @@ -81,6 +81,30 @@ vi.mock("../secrets/provider-env-vars.js", () => ({ getProviderEnvVars: vi.fn((provider: string) => providerEnvVarsById[provider] ?? []), })); +function requireRecord(value: unknown, label: string): Record { + expect(typeof value, label).toBe("object"); + expect(value, label).not.toBeNull(); + return value as Record; +} + +function expectFields(value: unknown, expected: Record, label = "record") { + const record = requireRecord(value, label); + for (const [key, expectedValue] of Object.entries(expected)) { + expect(record[key], key).toEqual(expectedValue); + } + return record; +} + +async function expectMissingFile(readPromise: Promise) { + try { + await readPromise; + } catch (error) { + expectFields(error, { code: "ENOENT" }, "read error"); + return; + } + throw new Error("Expected file read to fail with ENOENT"); +} + describe("writeOAuthCredentials", () => { const lifecycle = createAuthTestLifecycle([ "OPENCLAW_STATE_DIR", @@ -112,15 +136,13 @@ describe("writeOAuthCredentials", () => { const parsed = await readAuthProfilesForAgent<{ profiles?: Record; }>(defaultAgentDir); - expect(parsed.profiles?.["openai-codex:default"]).toMatchObject({ + expectFields(parsed.profiles?.["openai-codex:default"], { refresh: "refresh-token", access: "access-token", type: "oauth", }); - await expect( - fs.readFile(path.join(env.agentDir, "auth-profiles.json"), "utf8"), - ).rejects.toMatchObject({ code: "ENOENT" }); + await expectMissingFile(fs.readFile(path.join(env.agentDir, "auth-profiles.json"), "utf8")); }); it("writes OAuth credentials to all sibling agent dirs when syncSiblingAgents=true", async () => { @@ -152,7 +174,7 @@ describe("writeOAuthCredentials", () => { const parsed = JSON.parse(raw) as { profiles?: Record; }; - expect(parsed.profiles?.["openai-codex:default"]).toMatchObject({ + expectFields(parsed.profiles?.["openai-codex:default"], { refresh: "refresh-sync", access: "access-sync", type: "oauth", @@ -184,14 +206,12 @@ describe("writeOAuthCredentials", () => { const kidParsed = JSON.parse(kidRaw) as { profiles?: Record; }; - expect(kidParsed.profiles?.["openai-codex:default"]).toMatchObject({ + expectFields(kidParsed.profiles?.["openai-codex:default"], { access: "access-kid", type: "oauth", }); - await expect(fs.readFile(authProfilePathFor(mainAgentDir), "utf8")).rejects.toMatchObject({ - code: "ENOENT", - }); + await expectMissingFile(fs.readFile(authProfilePathFor(mainAgentDir), "utf8")); }); it("syncs siblings from explicit agentDir outside OPENCLAW_STATE_DIR", async () => { @@ -223,7 +243,7 @@ describe("writeOAuthCredentials", () => { const parsed = JSON.parse(raw) as { profiles?: Record; }; - expect(parsed.profiles?.["openai-codex:default"]).toMatchObject({ + expectFields(parsed.profiles?.["openai-codex:default"], { refresh: "refresh-ext", access: "access-ext", type: "oauth", @@ -232,9 +252,7 @@ describe("writeOAuthCredentials", () => { // Global state dir should NOT have credentials written const globalMain = path.join(tempStateDir, "agents", "main", "agent"); - await expect(fs.readFile(authProfilePathFor(globalMain), "utf8")).rejects.toMatchObject({ - code: "ENOENT", - }); + await expectMissingFile(fs.readFile(authProfilePathFor(globalMain), "utf8")); }); }); @@ -280,11 +298,11 @@ describe("upsertApiKeyProfile secret refs", () => { }); upsertApiKeyProfile({ provider: "openai", input: "sk-openai-env", agentDir: env.agentDir }); - expect(await readProfile(env.agentDir, "moonshot:default")).toMatchObject({ + expectFields(await readProfile(env.agentDir, "moonshot:default"), { key: "sk-moonshot-env", }); expect((await readProfile(env.agentDir, "moonshot:default"))?.keyRef).toBeUndefined(); - expect(await readProfile(env.agentDir, "openai:default")).toMatchObject({ + expectFields(await readProfile(env.agentDir, "openai:default"), { key: "sk-openai-env", }); expect((await readProfile(env.agentDir, "openai:default"))?.keyRef).toBeUndefined(); @@ -315,18 +333,18 @@ describe("upsertApiKeyProfile secret refs", () => { profileId: "moonshot:plain", }); - expect(await readProfile(env.agentDir, "moonshot:default")).toMatchObject({ + expectFields(await readProfile(env.agentDir, "moonshot:default"), { keyRef: { source: "env", provider: "default", id: "MOONSHOT_API_KEY" }, }); expect((await readProfile(env.agentDir, "moonshot:default"))?.key).toBeUndefined(); - expect(await readProfile(env.agentDir, "openai:default")).toMatchObject({ + expectFields(await readProfile(env.agentDir, "openai:default"), { keyRef: { source: "env", provider: "default", id: "OPENAI_API_KEY" }, }); expect((await readProfile(env.agentDir, "openai:default"))?.key).toBeUndefined(); - expect(await readProfile(env.agentDir, "moonshot:inline")).toMatchObject({ + expectFields(await readProfile(env.agentDir, "moonshot:inline"), { keyRef: { source: "env", provider: "default", id: "MOONSHOT_API_KEY" }, }); - expect(await readProfile(env.agentDir, "moonshot:plain")).toMatchObject({ + expectFields(await readProfile(env.agentDir, "moonshot:plain"), { key: "sk-moonshot-plaintext", }); expect((await readProfile(env.agentDir, "moonshot:plain"))?.keyRef).toBeUndefined(); @@ -364,21 +382,21 @@ describe("upsertApiKeyProfile secret refs", () => { }); } - expect(await readProfile(env.agentDir, "cloudflare-ai-gateway:default")).toMatchObject({ + expectFields(await readProfile(env.agentDir, "cloudflare-ai-gateway:default"), { keyRef: { source: "env", provider: "default", id: "CLOUDFLARE_AI_GATEWAY_API_KEY" }, metadata: { accountId: "account-1", gatewayId: "gateway-1" }, }); expect((await readProfile(env.agentDir, "cloudflare-ai-gateway:default"))?.key).toBeUndefined(); - expect(await readProfile(env.agentDir, "volcengine:default")).toMatchObject({ + expectFields(await readProfile(env.agentDir, "volcengine:default"), { keyRef: { source: "env", provider: "default", id: "VOLCANO_ENGINE_API_KEY" }, }); - expect(await readProfile(env.agentDir, "byteplus:default")).toMatchObject({ + expectFields(await readProfile(env.agentDir, "byteplus:default"), { keyRef: { source: "env", provider: "default", id: "BYTEPLUS_API_KEY" }, }); - expect(await readProfile(env.agentDir, "opencode:default")).toMatchObject({ + expectFields(await readProfile(env.agentDir, "opencode:default"), { keyRef: { source: "env", provider: "default", id: "OPENCODE_API_KEY" }, }); - expect(await readProfile(env.agentDir, "opencode-go:default")).toMatchObject({ + expectFields(await readProfile(env.agentDir, "opencode-go:default"), { keyRef: { source: "env", provider: "default", id: "OPENCODE_API_KEY" }, }); }); @@ -405,15 +423,13 @@ describe("upsertApiKeyProfile", () => { const parsed = await readAuthProfilesForAgent<{ profiles?: Record; }>(defaultAgentDir); - expect(parsed.profiles?.["minimax:default"]).toMatchObject({ + expectFields(parsed.profiles?.["minimax:default"], { type: "api_key", provider: "minimax", key: "sk-minimax-test", }); - await expect( - fs.readFile(path.join(env.agentDir, "auth-profiles.json"), "utf8"), - ).rejects.toMatchObject({ code: "ENOENT" }); + await expectMissingFile(fs.readFile(path.join(env.agentDir, "auth-profiles.json"), "utf8")); }); });