Files
Kaname/src/worker/worker.test.ts
T
Pascal LinxweilerandClaude Opus 5.5 49440154a2 Milestone (c): worker and downloads with live progress
- Worker queue with per-model exclusivity and MAX_CONCURRENT_DOWNLOADS,
  spawns `hf download --revision <sha> --local-dir ... --include <exact paths>`
  with HF_TOKEN via env only
- Progress from bytes on disk (final + .incomplete files matched by etag),
  per job and per file, with speed and ETA; post-download size/etag check
- Job logs (sanitized), cancel, graceful interrupt on shutdown,
  partial-file cleanup (hf 2.1.1 does not resume partials)
- Single-worker DB lease, free-space check
- SSE live updates for library, model and job pages
- Dockerfile + compose.yaml (web, worker, data/models volumes)
- HF_HUB_DISABLE_XET default on for measurable progress

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 16:57:12 +02:00

95 lines
3.8 KiB
TypeScript

import assert from "node:assert/strict";
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { after, describe, it } from "node:test";
import {
bytesOnDisk,
etagFromIncompleteName,
listIncomplete,
modelDir,
removeStaleIncomplete,
} from "./hf-layout";
import { sanitizeLine } from "./job-log";
import { SpeedMeter } from "./speed";
const SHA256 = "e150ed544dfe6016930c026a93913a5e3184181ebfe6ab2223ae01dd0491784c";
const SHA1 = "de077778adaa4aa6e706209cd95a2a0d6049bc59";
describe("hf-layout", () => {
const dirs: string[] = [];
after(async () => {
for (const d of dirs) await rm(d, { recursive: true, force: true });
});
async function fixture() {
const dir = await mkdtemp(join(tmpdir(), "kaname-test-"));
dirs.push(dir);
const dl = join(dir, ".cache", "huggingface", "download");
await mkdir(join(dl, "Q4_K_M"), { recursive: true });
return { dir, dl };
}
it("parses the etag from real .incomplete names", () => {
assert.equal(etagFromIncompleteName(`3nJsnUdcCmdBOp4dLn_dEgZ50vw=.${SHA256}.cd150ce3.incomplete`), SHA256);
assert.equal(etagFromIncompleteName(`abc=.${SHA1}.0fa2cc3c.incomplete`), SHA1);
assert.equal(etagFromIncompleteName("model.gguf.metadata"), null);
});
it("modelDir refuses paths outside MODELS_DIR", () => {
assert.ok(modelDir("/models", "org/name").endsWith(join("org", "name")));
assert.throws(() => modelDir("/models", "../etc"));
assert.throws(() => modelDir("/models", "."));
});
it("counts partial bytes by etag, complete files by size + metadata", async () => {
const { dir, dl } = await fixture();
await writeFile(join(dl, `x=.${SHA256}.aaaa1111.incomplete`), Buffer.alloc(300));
await writeFile(join(dl, "Q4_K_M", `y=.${SHA1}.bbbb2222.incomplete`), Buffer.alloc(10));
const incomplete = await listIncomplete(dir);
assert.equal(incomplete.length, 2);
const big = { path: "big.gguf", size: 1000, etags: [SHA256] };
assert.deepEqual(await bytesOnDisk(dir, big, incomplete), { done: 300, complete: false });
// Final file present with matching metadata → complete
await writeFile(join(dir, "big.gguf"), Buffer.alloc(1000));
await writeFile(join(dl, "big.gguf.metadata"), `abc\n${SHA256}\n123.4\n`);
assert.deepEqual(await bytesOnDisk(dir, big, []), { done: 1000, complete: true });
// Same size but metadata from a different file version → not complete
await writeFile(join(dl, "big.gguf.metadata"), `abc\n${"0".repeat(64)}\n123.4\n`);
assert.equal((await bytesOnDisk(dir, big, [])).complete, false);
});
it("removes stale partial files", async () => {
const { dir, dl } = await fixture();
await writeFile(join(dl, `x=.${SHA256}.aaaa1111.incomplete`), Buffer.alloc(50));
await writeFile(join(dl, "keep.metadata"), "x");
assert.deepEqual(await removeStaleIncomplete(dir), { count: 1, bytes: 50 });
assert.equal((await listIncomplete(dir)).length, 0);
});
});
describe("SpeedMeter", () => {
it("measures deltas over the window and estimates ETA", () => {
const m = new SpeedMeter(10_000);
m.add(0, 5_000_000); // bytes already on disk at start don't count
assert.equal(m.bytesPerSecond(), null);
m.add(2_000, 7_000_000);
assert.equal(m.bytesPerSecond(), 1_000_000);
assert.equal(m.eta(10_000_000), 10);
m.add(30_000, 7_000_000); // stalled
assert.equal(m.bytesPerSecond(), 0);
assert.equal(m.eta(10_000_000), null);
});
});
describe("sanitizeLine", () => {
it("strips ANSI codes and hides tokens", () => {
assert.equal(sanitizeLine("\x1b[1m\x1b[31mError: boom\x1b[0m"), "Error: boom");
assert.equal(sanitizeLine("token hf_abcdefghijklmnopqrstuvwxyz123 used"), "token hf_*** used");
assert.equal(sanitizeLine("secret=s3cr3t-value", ["s3cr3t-value"]), "secret=***");
});
});