mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 05:59:34 +00:00
refactor(agents): dedupe model and tool test helpers
This commit is contained in:
@@ -13,40 +13,40 @@ import { ensureOpenClawModelsJson } from "./models-config.js";
|
||||
|
||||
installModelsConfigTestHooks({ restoreFetch: true });
|
||||
|
||||
async function writeAuthProfiles(agentDir: string, profiles: Record<string, unknown>) {
|
||||
await fs.mkdir(agentDir, { recursive: true });
|
||||
await fs.writeFile(
|
||||
path.join(agentDir, "auth-profiles.json"),
|
||||
JSON.stringify({ version: 1, profiles }, null, 2),
|
||||
);
|
||||
}
|
||||
|
||||
function expectBearerAuthHeader(fetchMock: { mock: { calls: unknown[][] } }, token: string) {
|
||||
const [, opts] = fetchMock.mock.calls[0] as [string, { headers?: Record<string, string> }];
|
||||
expect(opts?.headers?.Authorization).toBe(`Bearer ${token}`);
|
||||
}
|
||||
|
||||
describe("models-config", () => {
|
||||
it("uses the first github-copilot profile when env tokens are missing", async () => {
|
||||
await withTempHome(async (home) => {
|
||||
await withUnsetCopilotTokenEnv(async () => {
|
||||
const fetchMock = mockCopilotTokenExchangeSuccess();
|
||||
const agentDir = path.join(home, "agent-profiles");
|
||||
await fs.mkdir(agentDir, { recursive: true });
|
||||
await fs.writeFile(
|
||||
path.join(agentDir, "auth-profiles.json"),
|
||||
JSON.stringify(
|
||||
{
|
||||
version: 1,
|
||||
profiles: {
|
||||
"github-copilot:alpha": {
|
||||
type: "token",
|
||||
provider: "github-copilot",
|
||||
token: "alpha-token",
|
||||
},
|
||||
"github-copilot:beta": {
|
||||
type: "token",
|
||||
provider: "github-copilot",
|
||||
token: "beta-token",
|
||||
},
|
||||
},
|
||||
},
|
||||
null,
|
||||
2,
|
||||
),
|
||||
);
|
||||
await writeAuthProfiles(agentDir, {
|
||||
"github-copilot:alpha": {
|
||||
type: "token",
|
||||
provider: "github-copilot",
|
||||
token: "alpha-token",
|
||||
},
|
||||
"github-copilot:beta": {
|
||||
type: "token",
|
||||
provider: "github-copilot",
|
||||
token: "beta-token",
|
||||
},
|
||||
});
|
||||
|
||||
await ensureOpenClawModelsJson({ models: { providers: {} } }, agentDir);
|
||||
|
||||
const [, opts] = fetchMock.mock.calls[0] as [string, { headers?: Record<string, string> }];
|
||||
expect(opts?.headers?.Authorization).toBe("Bearer alpha-token");
|
||||
expectBearerAuthHeader(fetchMock, "alpha-token");
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -82,31 +82,21 @@ describe("models-config", () => {
|
||||
await withUnsetCopilotTokenEnv(async () => {
|
||||
const fetchMock = mockCopilotTokenExchangeSuccess();
|
||||
const agentDir = path.join(home, "agent-profiles");
|
||||
await fs.mkdir(agentDir, { recursive: true });
|
||||
process.env.COPILOT_REF_TOKEN = "token-from-ref-env";
|
||||
await fs.writeFile(
|
||||
path.join(agentDir, "auth-profiles.json"),
|
||||
JSON.stringify(
|
||||
{
|
||||
version: 1,
|
||||
profiles: {
|
||||
"github-copilot:default": {
|
||||
type: "token",
|
||||
provider: "github-copilot",
|
||||
tokenRef: { source: "env", provider: "default", id: "COPILOT_REF_TOKEN" },
|
||||
},
|
||||
},
|
||||
try {
|
||||
await writeAuthProfiles(agentDir, {
|
||||
"github-copilot:default": {
|
||||
type: "token",
|
||||
provider: "github-copilot",
|
||||
tokenRef: { source: "env", provider: "default", id: "COPILOT_REF_TOKEN" },
|
||||
},
|
||||
null,
|
||||
2,
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
await ensureOpenClawModelsJson({ models: { providers: {} } }, agentDir);
|
||||
|
||||
const [, opts] = fetchMock.mock.calls[0] as [string, { headers?: Record<string, string> }];
|
||||
expect(opts?.headers?.Authorization).toBe("Bearer token-from-ref-env");
|
||||
delete process.env.COPILOT_REF_TOKEN;
|
||||
await ensureOpenClawModelsJson({ models: { providers: {} } }, agentDir);
|
||||
expectBearerAuthHeader(fetchMock, "token-from-ref-env");
|
||||
} finally {
|
||||
delete process.env.COPILOT_REF_TOKEN;
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user