feat(backend): ephemeral Pi model lister (get_available_models) with TTL cache
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
import { test, expect } from "vitest";
|
||||
import { spawn } from "node:child_process";
|
||||
import { mkdtempSync, writeFileSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import path from "node:path";
|
||||
import { createPiModelLister } from "../src/pi/list-models.js";
|
||||
import { loadConfig } from "../src/config.js";
|
||||
|
||||
const FAKE = path.resolve("../harness/tests/fake_pi/fake_pi_rpc.mjs");
|
||||
|
||||
function scriptWith(models: unknown[]): string {
|
||||
const dir = mkdtempSync(join(tmpdir(), "tht-models-"));
|
||||
const file = join(dir, "models.json");
|
||||
writeFileSync(file, JSON.stringify({ available_models: models }));
|
||||
return file;
|
||||
}
|
||||
|
||||
test("createPiModelLister returns mapped PiModel[] from get_available_models", async () => {
|
||||
const script = scriptWith([
|
||||
{ provider: "zai", id: "glm-5.2", name: "GLM 5.2", reasoning: true, extra: "ignored" },
|
||||
{ provider: "anthropic", id: "claude-opus-4-8", name: "Claude Opus 4.8", reasoning: true },
|
||||
]);
|
||||
try {
|
||||
const lister = createPiModelLister(loadConfig({ THT_HARNESS_DIR: "../harness" }), {
|
||||
spawnFn: () => spawn("node", [FAKE, script]) as any,
|
||||
});
|
||||
const models = await lister();
|
||||
expect(models).toEqual([
|
||||
{ provider: "zai", id: "glm-5.2", name: "GLM 5.2", reasoning: true },
|
||||
{ provider: "anthropic", id: "claude-opus-4-8", name: "Claude Opus 4.8", reasoning: true },
|
||||
]);
|
||||
} finally {
|
||||
rmSync(path.dirname(script), { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("createPiModelLister caches within ttl (spawns once for two calls)", async () => {
|
||||
const script = scriptWith([{ provider: "zai", id: "glm-5.2", name: "GLM 5.2", reasoning: true }]);
|
||||
try {
|
||||
let spawns = 0;
|
||||
const lister = createPiModelLister(loadConfig({ THT_HARNESS_DIR: "../harness" }), {
|
||||
spawnFn: () => { spawns++; return spawn("node", [FAKE, script]) as any; },
|
||||
ttlMs: 10_000,
|
||||
nowMs: () => 1000,
|
||||
});
|
||||
await lister();
|
||||
await lister();
|
||||
expect(spawns).toBe(1);
|
||||
} finally {
|
||||
rmSync(path.dirname(script), { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user