|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { listJobs, getJob, createJob, updateJob, deleteJob, type DownloadShard } from "./download-jobs-store"; |
| 3 | + |
| 4 | +function shard(overrides: Partial<DownloadShard> = {}): DownloadShard { |
| 5 | + return { |
| 6 | + filename: "model.gguf", |
| 7 | + path: "/models/model.gguf", |
| 8 | + expectedBytes: 1000, |
| 9 | + receivedBytes: 0, |
| 10 | + state: "queued", |
| 11 | + ...overrides, |
| 12 | + }; |
| 13 | +} |
| 14 | + |
| 15 | +describe("download-jobs-store", () => { |
| 16 | + it("starts with an empty list", () => { |
| 17 | + expect(listJobs()).toEqual([]); |
| 18 | + }); |
| 19 | + |
| 20 | + it("creates a job with generated id, default state, and timestamps", () => { |
| 21 | + const job = createJob({ |
| 22 | + kind: "huggingface", |
| 23 | + modelName: "Test Model", |
| 24 | + publisher: "test-org", |
| 25 | + backend: "llamacpp", |
| 26 | + destinationDir: "/models", |
| 27 | + modelId: "test-org/test-model", |
| 28 | + shards: [shard()], |
| 29 | + }); |
| 30 | + expect(job.id).toBeTruthy(); |
| 31 | + expect(job.state).toBe("queued"); |
| 32 | + expect(job.retryCount).toBe(0); |
| 33 | + expect(job.createdAt).toBeTruthy(); |
| 34 | + expect(job.updatedAt).toBe(job.createdAt); |
| 35 | + expect(listJobs().map((j) => j.id)).toContain(job.id); |
| 36 | + }); |
| 37 | + |
| 38 | + it("round-trips a job through getJob", () => { |
| 39 | + const created = createJob({ |
| 40 | + kind: "huggingface", |
| 41 | + modelName: "Another Model", |
| 42 | + publisher: "org", |
| 43 | + backend: "llamacpp", |
| 44 | + destinationDir: "/models", |
| 45 | + modelId: "org/another-model", |
| 46 | + shards: [shard()], |
| 47 | + }); |
| 48 | + expect(getJob(created.id)).toEqual(created); |
| 49 | + }); |
| 50 | + |
| 51 | + it("returns null for an unknown job id", () => { |
| 52 | + expect(getJob("does-not-exist")).toBeNull(); |
| 53 | + }); |
| 54 | + |
| 55 | + it("updates state, error, retryCount, and shards while bumping updatedAt", async () => { |
| 56 | + const created = createJob({ |
| 57 | + kind: "huggingface", |
| 58 | + modelName: "M", |
| 59 | + publisher: "org", |
| 60 | + backend: "llamacpp", |
| 61 | + destinationDir: "/models", |
| 62 | + modelId: "org/m", |
| 63 | + shards: [shard()], |
| 64 | + }); |
| 65 | + await new Promise((r) => setTimeout(r, 5)); |
| 66 | + const updatedShards = [shard({ receivedBytes: 500 })]; |
| 67 | + const updated = updateJob(created.id, { state: "downloading", shards: updatedShards, retryCount: 1 }); |
| 68 | + expect(updated?.state).toBe("downloading"); |
| 69 | + expect(updated?.shards).toEqual(updatedShards); |
| 70 | + expect(updated?.retryCount).toBe(1); |
| 71 | + expect(updated?.updatedAt).not.toBe(created.updatedAt); |
| 72 | + }); |
| 73 | + |
| 74 | + it("returns null when updating an unknown job", () => { |
| 75 | + expect(updateJob("does-not-exist", { state: "ready" })).toBeNull(); |
| 76 | + }); |
| 77 | + |
| 78 | + it("sets a typed error with kind and retryable flags", () => { |
| 79 | + const created = createJob({ |
| 80 | + kind: "huggingface", |
| 81 | + modelName: "M", |
| 82 | + publisher: "org", |
| 83 | + backend: "llamacpp", |
| 84 | + destinationDir: "/models", |
| 85 | + modelId: "org/m", |
| 86 | + shards: [shard()], |
| 87 | + }); |
| 88 | + const updated = updateJob(created.id, { |
| 89 | + state: "failed", |
| 90 | + error: { message: "Gated repo — accept the license first.", kind: "license_required", retryable: false }, |
| 91 | + }); |
| 92 | + expect(updated?.error).toEqual({ message: "Gated repo — accept the license first.", kind: "license_required", retryable: false }); |
| 93 | + }); |
| 94 | + |
| 95 | + it("deletes a job", () => { |
| 96 | + const created = createJob({ |
| 97 | + kind: "huggingface", |
| 98 | + modelName: "M", |
| 99 | + publisher: "org", |
| 100 | + backend: "llamacpp", |
| 101 | + destinationDir: "/models", |
| 102 | + modelId: "org/m", |
| 103 | + shards: [shard()], |
| 104 | + }); |
| 105 | + deleteJob(created.id); |
| 106 | + expect(getJob(created.id)).toBeNull(); |
| 107 | + }); |
| 108 | + |
| 109 | + it("deleting an unknown job is a harmless no-op", () => { |
| 110 | + expect(() => deleteJob("does-not-exist")).not.toThrow(); |
| 111 | + }); |
| 112 | +}); |
0 commit comments