Files
oh-my-pi/packages/coding-agent/test/model-registry-command-values.test.ts
T
oldschoola a2854ba768 fix: migrate coding-agent tests from fs.rm to removeWithRetries
Migrate 203 test files (356 call sites) from fs.rm/fs.rmSync to
removeWithRetries/removeSyncWithRetries to reduce EBUSY test failures
on Windows. removeWithRetries is now exported from @oh-my-pi/pi-utils.

The migration uses a regex-based approach that:
- Replaces fs.rm(path, { recursive, force }) → removeWithRetries(path)
- Replaces fs.rmSync(path, { recursive, force }) → removeSyncWithRetries(path)
- Replaces fs.rm(path) → removeWithRetries(path) (no options)
- Skips fs.rm/fs.rmSync inside template literals (bun --eval scripts)
- Adds imports to existing @oh-my-pi/pi-utils import or creates new one
- Removes unused fs imports where fs.rm was the only fs usage (4 files)
2026-06-23 15:28:05 -07:00

88 lines
2.8 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, test } from "bun:test";
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import { ModelRegistry } from "@oh-my-pi/pi-coding-agent/config/model-registry";
import { AuthStorage } from "@oh-my-pi/pi-coding-agent/session/auth-storage";
import { removeSyncWithRetries, Snowflake } from "@oh-my-pi/pi-utils";
function stdoutCommand(value: string): string {
return `${JSON.stringify(process.execPath)} -e ${JSON.stringify(`process.stdout.write(${JSON.stringify(value)})`)}`;
}
describe("ModelRegistry command-resolved models.yml values", () => {
let tempDir = "";
let authStorage: AuthStorage;
let modelsPath = "";
beforeEach(async () => {
tempDir = path.join(os.tmpdir(), `pi-test-model-command-values-${Snowflake.next()}`);
fs.mkdirSync(tempDir, { recursive: true });
modelsPath = path.join(tempDir, "models.json");
authStorage = await AuthStorage.create(":memory:");
});
afterEach(() => {
authStorage.close();
if (!tempDir || !fs.existsSync(tempDir)) return;
try {
removeSyncWithRetries(tempDir);
} catch (error) {
if ((error as NodeJS.ErrnoException).code !== "EBUSY") throw error;
}
});
test("provider apiKey and headers resolve from command stdout", async () => {
fs.writeFileSync(
modelsPath,
JSON.stringify({
providers: {
anthropic: {
baseUrl: "https://anthropic-proxy.example.com/v1",
apiKey: `!${stdoutCommand("cmd-api-key")}`,
authHeader: true,
headers: { "X-Api-Key": `!${stdoutCommand("cmd-header")}` },
},
},
}),
);
const registry = new ModelRegistry(authStorage, modelsPath);
const models = registry.getAll().filter(model => model.provider === "anthropic");
expect(models.length).toBeGreaterThan(1);
for (const model of models) {
expect(model.headers?.Authorization).toBe("Bearer cmd-api-key");
expect(model.headers?.["X-Api-Key"]).toBe("cmd-header");
}
expect(await registry.getApiKey(models[0])).toBe("cmd-api-key");
});
test("modelOverrides headers resolve from command stdout", async () => {
fs.writeFileSync(
modelsPath,
JSON.stringify({
providers: {
"custom-proxy": {
baseUrl: "https://custom-proxy.example.com/v1",
api: "openai-completions",
apiKey: `!${stdoutCommand("cmd-api-key")}`,
authHeader: true,
models: [{ id: "custom-model", name: "Custom Model" }],
modelOverrides: {
"custom-model": { headers: { "X-Model-Key": `!${stdoutCommand("cmd-model-header")}` } },
},
},
},
}),
);
const registry = new ModelRegistry(authStorage, modelsPath);
const model = registry.find("custom-proxy", "custom-model");
expect(model).toBeDefined();
expect(model?.headers?.["X-Model-Key"]).toBe("cmd-model-header");
expect(model?.headers?.Authorization).toBe("Bearer cmd-api-key");
});
});