Files
oh-my-pi/packages/coding-agent/test/lm-studio-fix.test.ts
T
can1357 005401e82a refactor: extracted fetch mocking into reusable hookFetch utility
- Extracted fetch mocking logic into reusable `hookFetch()` utility function with middleware-style handler pattern.
- Replaced manual `globalThis.fetch` assignment and restoration across 10 test files with `hookFetch()` calls using `using` statement for automatic cleanup.
- Implemented Disposable pattern with Symbol.dispose for fetch hook resource management, eliminating try-finally blocks.
- Exported `hookFetch` from utils public API to enable consistent fetch mocking across packages.
2026-03-08 16:16:54 +01:00

73 lines
2.6 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 { hookFetch, Snowflake } from "@oh-my-pi/pi-utils";
describe("ModelRegistry LM Studio Fixes", () => {
let tempDir: string;
let modelsJsonPath: string;
let authStorage: AuthStorage;
beforeEach(async () => {
tempDir = path.join(os.tmpdir(), `pi-test-lm-studio-fixes-${Snowflake.next()}`);
fs.mkdirSync(tempDir, { recursive: true });
modelsJsonPath = path.join(tempDir, "models.json");
authStorage = await AuthStorage.create(path.join(tempDir, "testauth.db"));
});
afterEach(() => {
if (tempDir && fs.existsSync(tempDir)) {
fs.rmSync(tempDir, { recursive: true });
}
});
test("auto-discovers both ollama and lm-studio models independently", async () => {
using _hook = hookFetch(input => {
const url = String(input);
if (url.includes(":11434/api/tags")) {
return new Response(JSON.stringify({ models: [{ name: "ollama-model" }] }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
}
if (url.includes(":1234/v1/models")) {
return new Response(JSON.stringify({ data: [{ id: "lm-studio-model" }] }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
}
return new Response(null, { status: 404 });
});
const registry = new ModelRegistry(authStorage, modelsJsonPath);
await registry.refresh();
const allModels = registry.getAll();
expect(allModels.some(m => m.provider === "ollama" && m.id === "ollama-model")).toBe(true);
expect(allModels.some(m => m.provider === "lm-studio" && m.id === "lm-studio-model")).toBe(true);
const available = registry.getAvailable();
expect(available.some(m => m.provider === "ollama")).toBe(true);
expect(available.some(m => m.provider === "lm-studio")).toBe(true);
});
test("lm-studio discovery handles trailing slashes in baseUrl correctly", async () => {
let _requestedUrl = "";
using _hook = hookFetch(input => {
const url = String(input);
// Only track URLs from our test endpoints; ignore concurrent built-in provider discovery
if (url.includes("127.0.0.1:1234") || url.includes("127.0.0.1:9999") || url.startsWith("not a url")) {
_requestedUrl = url;
return new Response(JSON.stringify({ data: [{ id: "model-1" }] }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
}
return new Response(null, { status: 404 });
});
});
});