Files
Kaname/src/worker/scan.test.ts
T
Pascal LinxweilerandClaude Opus 5.5 d27e79bba0 Local models: the folder's README.md is the model card
The worker reads README.md when it scans the models folder (the web
app can't see it) and stores it on the folder and, after import, on
the model; every scan refreshes it for local models. Relative images
and links are dropped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 00:27:17 +01:00

93 lines
4.0 KiB
TypeScript

// The models-folder scan, on a temporary folder and SQLite database.
// Runs with `--conditions=react-server` (see package.json).
import assert from "node:assert/strict";
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { after, before, describe, it } from "node:test";
const dir = mkdtempSync(join(tmpdir(), "kaname-scan-"));
process.env.DATABASE_URL = `file:${join(dir, "test.db").replace(/\\/g, "/")}`;
const modelsDir = join(dir, "models");
process.env.MODELS_DIR = modelsDir;
let scan: typeof import("./scan");
let D: typeof import("@/lib/db/client");
let S: typeof import("@/lib/db/schema");
before(async () => {
const { runMigrations } = await import("@/lib/db/migrate");
await runMigrations();
scan = await import("./scan");
D = await import("@/lib/db/client");
S = await import("@/lib/db/schema");
});
after(() => {
try {
rmSync(dir, { recursive: true, force: true });
} catch {}
});
function put(path: string, bytes = 3) {
mkdirSync(dirname(path), { recursive: true });
writeFileSync(path, "x".repeat(bytes));
}
const tick = () => new Promise((r) => setTimeout(r, 5));
async function stored() {
const rows = await D.db().select().from(S.localFolders);
return Object.fromEntries(rows.map((r) => [r.path, r]));
}
describe("models folder scan", () => {
it("writes only new or changed folders and removes the ones that are gone", async () => {
put(join(modelsDir, "org", "a", "model.gguf"));
put(join(modelsDir, "org", "b", "shards", "x.safetensors"));
assert.equal(await scan.scanModelsDir(modelsDir), 2);
const first = await stored();
await tick();
assert.equal(await scan.scanModelsDir(modelsDir), 2);
const second = await stored();
assert.equal(second["org/a"].scannedAt.getTime(), first["org/a"].scannedAt.getTime(), "unchanged: not written");
assert.equal(second["org/b"].scannedAt.getTime(), first["org/b"].scannedAt.getTime(), "unchanged: not written");
await tick();
put(join(modelsDir, "org", "a", "extra.json"), 10);
rmSync(join(modelsDir, "org", "b"), { recursive: true });
assert.equal(await scan.scanModelsDir(modelsDir), 1);
const third = await stored();
assert.deepEqual(Object.keys(third), ["org/a"]);
assert.equal(third["org/a"].fileCount, 2);
assert.deepEqual(third["org/a"].files.map((f) => f.path), ["extra.json", "model.gguf"]);
assert.ok(third["org/a"].scannedAt.getTime() > first["org/a"].scannedAt.getTime(), "changed: written");
});
it("compares the file list, not only the counts", () => {
const at = new Date();
const found = { path: "o/m", files: [{ path: "b.gguf", size: 2 }], fileCount: 1, totalBytes: 2, commit: null, scannedAt: at };
const same = { path: "o/m", filesJson: JSON.stringify(found.files), fileCount: 1, totalBytes: 2, commit: null, readme: null };
assert.deepEqual(scan.folderChanges([same], [found]), { insert: [], update: [], remove: [] });
const renamed = { ...same, filesJson: JSON.stringify([{ path: "a.gguf", size: 2 }]) };
assert.deepEqual(scan.folderChanges([renamed], [found]).update, [found]);
});
it("writes a folder again when its README changed", () => {
const at = new Date();
const found = { path: "o/m", files: [{ path: "README.md", size: 3 }], fileCount: 1, totalBytes: 3, commit: null, readme: "new", scannedAt: at };
const stored = { path: "o/m", filesJson: JSON.stringify(found.files), fileCount: 1, totalBytes: 3, commit: null, readme: "old" };
assert.deepEqual(scan.folderChanges([stored], [found]).update, [found]);
});
it("reads README.md in any case, and only at the top level", async () => {
const folder = join(dir, "readme-test"); // outside MODELS_DIR: no other test scans it
put(join(folder, "sub", "README.md"), 1);
assert.equal(await scan.readReadme(folder), null);
writeFileSync(join(folder, "ReadMe.md"), "# Hello\n");
assert.equal(await scan.readReadme(folder), "# Hello\n");
});
});