test: share oauth profile fixtures

This commit is contained in:
Peter Steinberger 2026-03-14 02:38:55 +00:00
parent 6720bf5be0
commit c5d905871f
1 changed files with 41 additions and 33 deletions

View File

@ -32,6 +32,20 @@ function tokenStore(params: {
}; };
} }
function githubCopilotTokenStore(profileId: string, includeInlineToken = true): AuthProfileStore {
return {
version: 1,
profiles: {
[profileId]: {
type: "token",
provider: "github-copilot",
...(includeInlineToken ? { token: "" } : {}),
tokenRef: { source: "env", provider: "default", id: "GITHUB_TOKEN" },
},
},
};
}
async function resolveWithConfig(params: { async function resolveWithConfig(params: {
profileId: string; profileId: string;
provider: string; provider: string;
@ -59,6 +73,25 @@ async function withEnvVar<T>(key: string, value: string, run: () => Promise<T>):
} }
} }
async function expectResolvedApiKey(params: {
profileId: string;
provider: string;
mode: "api_key" | "token" | "oauth";
store: AuthProfileStore;
expectedApiKey: string;
}) {
const result = await resolveApiKeyForProfile({
cfg: cfgFor(params.profileId, params.provider, params.mode),
store: params.store,
profileId: params.profileId,
});
expect(result).toEqual({
apiKey: params.expectedApiKey, // pragma: allowlist secret
provider: params.provider,
email: undefined,
});
}
describe("resolveApiKeyForProfile config compatibility", () => { describe("resolveApiKeyForProfile config compatibility", () => {
it("accepts token credentials when config mode is oauth", async () => { it("accepts token credentials when config mode is oauth", async () => {
const profileId = "anthropic:token"; const profileId = "anthropic:token";
@ -278,25 +311,12 @@ describe("resolveApiKeyForProfile secret refs", () => {
it("resolves token tokenRef from env", async () => { it("resolves token tokenRef from env", async () => {
const profileId = "github-copilot:default"; const profileId = "github-copilot:default";
await withEnvVar("GITHUB_TOKEN", "gh-ref-token", async () => { await withEnvVar("GITHUB_TOKEN", "gh-ref-token", async () => {
const result = await resolveApiKeyForProfile({ await expectResolvedApiKey({
cfg: cfgFor(profileId, "github-copilot", "token"),
store: {
version: 1,
profiles: {
[profileId]: {
type: "token",
provider: "github-copilot",
token: "",
tokenRef: { source: "env", provider: "default", id: "GITHUB_TOKEN" },
},
},
},
profileId, profileId,
});
expect(result).toEqual({
apiKey: "gh-ref-token", // pragma: allowlist secret
provider: "github-copilot", provider: "github-copilot",
email: undefined, mode: "token",
store: githubCopilotTokenStore(profileId),
expectedApiKey: "gh-ref-token", // pragma: allowlist secret
}); });
}); });
}); });
@ -304,24 +324,12 @@ describe("resolveApiKeyForProfile secret refs", () => {
it("resolves token tokenRef without inline token when expires is absent", async () => { it("resolves token tokenRef without inline token when expires is absent", async () => {
const profileId = "github-copilot:no-inline-token"; const profileId = "github-copilot:no-inline-token";
await withEnvVar("GITHUB_TOKEN", "gh-ref-token", async () => { await withEnvVar("GITHUB_TOKEN", "gh-ref-token", async () => {
const result = await resolveApiKeyForProfile({ await expectResolvedApiKey({
cfg: cfgFor(profileId, "github-copilot", "token"),
store: {
version: 1,
profiles: {
[profileId]: {
type: "token",
provider: "github-copilot",
tokenRef: { source: "env", provider: "default", id: "GITHUB_TOKEN" },
},
},
},
profileId, profileId,
});
expect(result).toEqual({
apiKey: "gh-ref-token", // pragma: allowlist secret
provider: "github-copilot", provider: "github-copilot",
email: undefined, mode: "token",
store: githubCopilotTokenStore(profileId, false),
expectedApiKey: "gh-ref-token", // pragma: allowlist secret
}); });
}); });
}); });