File size: 4,610 Bytes
e249c6d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | import { describe, expect, it } from "vitest";
import type { OpenClawConfig } from "../../config/types.openclaw.js";
import { withPluginMetadataSnapshotScope } from "../../plugins/current-plugin-metadata-snapshot.js";
import { createPluginMetadataSnapshotFixture } from "../../plugins/plugin-metadata.test-support.js";
import {
createApiKeyCredential,
createAuthProfileStoreFixture,
} from "../auth-profiles/credential-fixtures.test-support.js";
import { prepareAgentRuntimeAuth } from "./prepare-auth.js";
function metadata(owner: string) {
return createPluginMetadataSnapshotFixture({
plugins: [{ id: owner, providerAuthAliases: { "fixture-alias": owner } }],
});
}
describe("prepared auth metadata ownership", () => {
it.each([true, false])("uses selected environment evidence (available: %s)", (available) => {
const snapshot = (envVar: string) =>
createPluginMetadataSnapshotFixture({
plugins: [
{
id: "fixture-owner",
providers: ["fixture-provider"],
setup: {
requiresRuntime: false,
providers: [{ id: "fixture-provider", envVars: [envVar] }],
},
},
],
});
const config = {};
const prepared = withPluginMetadataSnapshotScope(
snapshot("AMBIENT_PROVIDER_KEY"),
() =>
prepareAgentRuntimeAuth({
provider: "fixture-provider",
modelId: "model",
config,
env: available
? { SELECTED_PROVIDER_KEY: "synthetic" }
: { AMBIENT_PROVIDER_KEY: "synthetic" },
metadataSnapshot: snapshot("SELECTED_PROVIDER_KEY"),
authProfileStore: { version: 1, profiles: {} },
}),
{ config, trustConfigIdentity: true },
);
expect(prepared.attempts[0]?.kind).toBe(available ? "direct" : "implicit");
expect(prepared.plan.credentialSource).toEqual(
available
? { kind: "direct", evidence: "environment", authorization: "ambient" }
: { kind: "none" },
);
});
it.each(["user", "user-link", "auto", "binding"] as const)(
"selects the prepared owner for %s profiles despite ambient aliases",
(selection) => {
const selected = metadata("selected-auth");
const ambient = metadata("ambient-auth");
const config: OpenClawConfig =
selection === "binding"
? {
models: {
providers: {
"fixture-alias": { baseUrl: "", models: [], apiKey: "fixture:selected" },
},
},
}
: {};
const prepare = () =>
prepareAgentRuntimeAuth({
provider: "fixture-alias",
modelId: "model",
config,
env: {},
metadataSnapshot: selected,
authProfileStore: createAuthProfileStoreFixture({
"fixture:ambient": createApiKeyCredential("ambient-auth", "synthetic-ambient"),
"fixture:selected": createApiKeyCredential("selected-auth", "synthetic-selected"),
}),
...(selection === "user" || selection === "user-link"
? { sessionAuthProfileId: "fixture:selected", sessionAuthProfileSource: selection }
: {}),
});
const prepared = withPluginMetadataSnapshotScope(ambient, prepare, {
config,
trustConfigIdentity: true,
});
expect(prepared.plan).toMatchObject({
providerForAuth: "selected-auth",
forwardedAuthProfileId: "fixture:selected",
forwardedAuthProfileCandidateIds: ["fixture:selected"],
});
expect(prepared.attempts.map((attempt) => attempt.profileId)).toEqual(["fixture:selected"]);
},
);
it("treats an empty prepared selection as authoritative", () => {
const config = {};
const prepared = withPluginMetadataSnapshotScope(
metadata("ambient-auth"),
() =>
prepareAgentRuntimeAuth({
provider: "fixture-alias",
modelId: "model",
config,
env: {},
metadataSnapshot: createPluginMetadataSnapshotFixture(),
authProfileStore: createAuthProfileStoreFixture({
"fixture:exact": createApiKeyCredential("fixture-alias", "synthetic-exact"),
"fixture:ambient": createApiKeyCredential("ambient-auth", "synthetic-ambient"),
}),
}),
{ config, trustConfigIdentity: true },
);
expect(prepared.attempts.map((attempt) => attempt.profileId)).toEqual(["fixture:exact"]);
expect(prepared.plan.forwardedAuthProfileCandidateIds).toEqual(["fixture:exact"]);
});
});
|