import { afterEach, describe, expect, test } from "bun:test"; import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { loadOrCreateConfig } from "./config"; const tempDirs: string[] = []; afterEach(async () => { await Promise.all(tempDirs.splice(0).map((path) => rm(path, { recursive: true, force: true }))); }); describe("embedder config", () => { test("creates a commented JSONC config by default", async () => { const directory = await tempDir(); const path = join(directory, "embedder.jsonc"); const config = await loadOrCreateConfig(path); const written = await readFile(path, "utf8"); expect(config.vulkanDevice).toBeUndefined(); expect(written).toContain('// "vulkanDevice": "Vulkan0",'); expect(written).toContain("Optional Vulkan device pin"); }); test("parses JSONC comments, trailing commas, and a Vulkan device pin", async () => { const directory = await tempDir(); const path = join(directory, "embedder.jsonc"); await writeFile( path, `{ // Pin the AMD card. "backend": "llama_cpp_vulkan", "vulkanDevice": "Vulkan1", "runtimeTag": "b8215", "runtimeRepo": "ggml-org/llama.cpp", "runtimeAsset": "llama-b8215-bin-ubuntu-vulkan-x64.tar.gz", "runtimeBinary": "llama-server", "modelRepo": "ggml-org/embeddinggemma-300m-qat-q8_0-GGUF", "modelRevision": "main", "modelFile": "embeddinggemma-300m-qat-Q8_0.gguf", } `, ); await expect(loadOrCreateConfig(path)).resolves.toMatchObject({ backend: "llama_cpp_vulkan", vulkanDevice: "Vulkan1", }); }); test("migrates a legacy JSON config to JSONC", async () => { const directory = await tempDir(); const path = join(directory, "embedder.jsonc"); const legacyPath = join(directory, "embedder.json"); await writeFile( legacyPath, JSON.stringify({ backend: "llama_cpp_vulkan", runtimeTag: "b8215", runtimeRepo: "ggml-org/llama.cpp", runtimeAsset: "llama-b8215-bin-ubuntu-vulkan-x64.tar.gz", runtimeBinary: "llama-server", modelRepo: "ggml-org/embeddinggemma-300m-qat-q8_0-GGUF", modelRevision: "main", modelFile: "embeddinggemma-300m-qat-Q8_0.gguf", vulkanDevice: "Vulkan0", }), ); const config = await loadOrCreateConfig(path, legacyPath); const written = await readFile(path, "utf8"); expect(config.vulkanDevice).toBe("Vulkan0"); expect(written).toContain('"vulkanDevice": "Vulkan0"'); }); test("rejects invalid Vulkan device names", async () => { const directory = await tempDir(); const path = join(directory, "embedder.jsonc"); await writeFile( path, JSON.stringify({ backend: "llama_cpp_vulkan", runtimeTag: "b8215", runtimeRepo: "ggml-org/llama.cpp", runtimeAsset: "llama-b8215-bin-ubuntu-vulkan-x64.tar.gz", runtimeBinary: "llama-server", modelRepo: "ggml-org/embeddinggemma-300m-qat-q8_0-GGUF", modelRevision: "main", modelFile: "embeddinggemma-300m-qat-Q8_0.gguf", vulkanDevice: "AMD0", }), ); await expect(loadOrCreateConfig(path)).rejects.toThrow( "embedder config field `vulkanDevice` must look like `Vulkan0`", ); }); }); async function tempDir(): Promise { const directory = await mkdtemp(join(tmpdir(), "tool-query-config-")); tempDirs.push(directory); return directory; }