Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

SpeechModelStore.test.ts

222 lines9.0 KB

1import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { expect, it } from "@effect/vitest";
4import * as Digest from "effect/crypto/Digest";
5import * as Deferred from "effect/Deferred";
6import * as Effect from "effect/Effect";
7import * as Hex from "effect/encoding/Hex";
8import * as Fiber from "effect/Fiber";
9import * as FileSystem from "effect/FileSystem";
10import * as Layer from "effect/Layer";
11import * as Path from "effect/Path";
12import * as Stream from "effect/Stream";
13import { HttpClient, HttpClientError, HttpClientRequest, HttpClientResponse } from "effect/http";
14​
15import * as HostProcess from "@t3tools/shared/HostProcess";
16​
17import * as ServerConfig from "../config.ts";
18import * as SpeechModelStore from "./SpeechModelStore.ts";
19​
20const bytes = Uint8Array.from({ length: 64 }, (_, index) => (index * 7) % 256);
21const SPLIT = 20;
22​
23const harness = Effect.gen(function* () {
24 const digest = yield* Digest.Digest;
25 const model = {
26 id: "fixture/speech-model",
27 revision: "a".repeat(40),
28 filename: "model.gguf",
29 size: bytes.length,
30 sha256: Hex.encode(yield* digest.digest(Digest.sha256, bytes).pipe(Effect.orDie)),
31 };
32 const config = yield* ServerConfig.ServerConfig;
33 const path = yield* Path.Path;
34 const fs = yield* FileSystem.FileSystem;
35 const directory = path.join(config.stateDir, "speech", "models");
36 const stale = path.join(directory, model.filename);
37 yield* fs.makeDirectory(directory, { recursive: true });
38 yield* fs.writeFile(stale, new Uint8Array(bytes.length));
39 const bodyStarted = yield* Deferred.make<void>();
40 const requests: HttpClientRequest.HttpClientRequest[] = [];
41 const transportFailure = (request: HttpClientRequest.HttpClientRequest) =>
42 new HttpClientError.HttpClientError({
43 reason: new HttpClientError.TransportError({ request, cause: new Error("reset") }),
44 });
45 const bodies = {
46 current: (
47 request: HttpClientRequest.HttpClientRequest,
48 ): Stream.Stream<Uint8Array, HttpClientError.HttpClientError> =>
49 Stream.fail(transportFailure(request)),
50 };
51 const client = HttpClient.make((request) =>
52 Effect.sync(() => {
53 requests.push(request);
54 const body = bodies.current(request);
55 const resumed = request.headers["range"] === `bytes=${SPLIT}-`;
56 return Object.defineProperty(
57 HttpClientResponse.fromWeb(
58 request,
59 new Response(null, {
60 status: resumed ? 206 : 200,
61 headers: resumed
62 ? {
63 "content-range": `bytes ${SPLIT}-${bytes.length - 1}/${bytes.length}`,
64 "content-length": String(bytes.length - SPLIT),
65 }
66 : { "content-length": String(bytes.length) },
67 }),
68 ),
69 "stream",
70 {
71 value: Stream.concat(
72 Stream.fromEffect(Deferred.succeed(bodyStarted, undefined)).pipe(Stream.drain),
73 body,
74 ),
75 },
76 );
77 }),
78 );
79 const openStore = SpeechModelStore.SpeechModelStore.pipe(
80 Effect.provide(
81 SpeechModelStore.layer.pipe(Layer.provide(Layer.succeed(HttpClient.HttpClient, client))),
82 ),
83 );
84 const store = yield* openStore;
85 return {
86 model,
87 directory,
88 stale,
89 fs,
90 requests,
91 bodies,
92 transportFailure,
93 bodyStarted,
94 store,
95 openStore,
96 };
97});
98​
99const dependencies = ServerConfig.layerTest("/tmp", { prefix: "speech-model-store-" }).pipe(
100 Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
101);
102​
103it.effect("resumes an interrupted transfer, replaces a corrupt same-size file, and marks it", () =>
104 Effect.gen(function* () {
105 const h = yield* harness;
106 h.bodies.current = (request) =>
107 request.headers["range"]
108 ? Stream.succeed(bytes.subarray(SPLIT))
109 : Stream.concat(
110 Stream.succeed(bytes.subarray(0, SPLIT)),
111 Stream.fail(h.transportFailure(request)),
112 );
113 expect(yield* h.store.isReady(h.model)).toBe(true);
114 expect(yield* h.store.installedPath(h.model)).toBeUndefined();
115 expect(yield* h.store.isReady(h.model)).toBe(false);
116​
117 const progress: number[] = [];
118 const installed = yield* h.store.download(h.model, {
119 onProgress: (downloaded) => progress.push(downloaded),
120 });
121 expect(installed).toBe(h.stale);
122 expect(h.requests.map((request) => request.headers["range"])).toEqual([
123 undefined,
124 `bytes=${SPLIT}-`,
125 ]);
126 const pinnedUrl = `https://huggingface.co/${h.model.id}/resolve/${h.model.revision}/${h.model.filename}`;
127 expect(h.requests.map((request) => request.url)).toEqual([pinnedUrl, pinnedUrl]);
128 expect(progress.at(0)).toBe(0);
129 expect(progress.at(-1)).toBe(bytes.length);
130 expect([...(yield* h.fs.readFile(installed))]).toEqual([...bytes]);
131 expect((yield* h.fs.readDirectory(h.directory)).sort()).toEqual([
132 "model.gguf",
133 "model.gguf.verified",
134 ]);
135 expect(yield* h.store.isReady(h.model)).toBe(true);
136 expect(yield* h.store.installedPath(h.model)).toBe(installed);
137 const requestsBeforeReuse = h.requests.length;
138 expect(yield* h.store.download(h.model)).toBe(installed);
139 expect(h.requests).toHaveLength(requestsBeforeReuse);
140 }).pipe(Effect.provide(dependencies)),
141);
142​
143it.effect("rejects a same-size download whose SHA-256 differs from the catalog", () =>
144 Effect.gen(function* () {
145 const h = yield* harness;
146 h.bodies.current = () => Stream.succeed(new Uint8Array(bytes.length));
147 const result = yield* Effect.result(h.store.download(h.model));
148 expect(result).toMatchObject({ _tag: "Failure", failure: { reason: "verification" } });
149 expect(yield* h.fs.readDirectory(h.directory)).toEqual(["model.gguf"]);
150 expect(yield* h.store.installedPath(h.model)).toBeUndefined();
151 }).pipe(Effect.provide(dependencies)),
152);
153​
154it.effect("cleans only download partials on cancellation or restart", () =>
155 Effect.gen(function* () {
156 const h = yield* harness;
157 h.bodies.current = () => Stream.concat(Stream.succeed(bytes.subarray(0, SPLIT)), Stream.never);
158 const fiber = yield* h.store.download(h.model).pipe(Effect.forkChild);
159 yield* Deferred.await(h.bodyStarted);
160 yield* Fiber.interrupt(fiber);
161 expect(yield* h.fs.readDirectory(h.directory)).toEqual(["model.gguf"]);
162 expect([...(yield* h.fs.readFile(h.stale))]).toEqual([...new Uint8Array(bytes.length)]);
163 const orphan = `${h.stale}.f660de6c-e172-4146-8ffe-d3c32eb48cd4.part`;
164 yield* h.fs.writeFile(orphan, bytes.subarray(0, SPLIT));
165 yield* h.fs.writeFileString(`${h.stale}.notes.part`, "not a download");
166 yield* h.openStore;
167 expect((yield* h.fs.readDirectory(h.directory)).sort()).toEqual([
168 "model.gguf",
169 "model.gguf.notes.part",
170 ]);
171 }).pipe(Effect.provide(dependencies)),
172);
173​
174it.effect("links a verified model file from another folder and never deletes it", () =>
175 Effect.gen(function* () {
176 const h = yield* harness;
177 const path = yield* Path.Path;
178 const elsewhere = yield* h.fs.makeTempDirectoryScoped({ prefix: "speech-other-app-" });
179 const nested = path.join(elsewhere, "hub", "snapshots");
180 yield* h.fs.makeDirectory(nested, { recursive: true });
181 const impostor = path.join(elsewhere, "same-size.gguf");
182 const existing = path.join(nested, "renamed-by-the-other-app.gguf");
183 yield* h.fs.writeFile(impostor, new Uint8Array(bytes.length));
184 yield* h.fs.writeFile(existing, bytes);
185​
186 expect(yield* h.store.link([h.model], impostor)).toEqual([]);
187 expect(yield* h.store.linkedFrom(h.model)).toBeUndefined();
188​
189 expect(yield* h.store.link([h.model], elsewhere)).toEqual([h.model.id]);
190 expect(yield* h.store.linkedFrom(h.model)).toBe(existing);
191 expect(yield* h.store.installedPath(h.model)).toBe(h.stale);
192 expect(h.requests).toHaveLength(0);
193 // Pointing at T3 Code's own folder must not turn the link into a link to itself.
194 expect(yield* h.store.link([h.model], h.directory)).toEqual([]);
195 expect(yield* h.store.installedPath(h.model)).toBe(h.stale);
196​
197 yield* h.store.remove(h.model);
198 expect(yield* h.store.isReady(h.model)).toBe(false);
199 expect([...(yield* h.fs.readFile(existing))]).toEqual([...bytes]);
200 }).pipe(Effect.scoped, Effect.provide(dependencies)),
201);
202​
203it.effect("installs from a Hugging Face cache copy instead of downloading", () =>
204 Effect.gen(function* () {
205 const h = yield* harness;
206 const path = yield* Path.Path;
207 const home = yield* h.fs.makeTempDirectoryScoped({ prefix: "speech-home-" });
208 const snapshot = path.join(
209 home,
210 ".cache/huggingface/hub/models--fixture--speech-model/snapshots",
211 h.model.revision,
212 );
213 yield* h.fs.makeDirectory(snapshot, { recursive: true });
214 const cached = path.join(snapshot, h.model.filename);
215 yield* h.fs.writeFile(cached, bytes);
216 const store = yield* h.openStore.pipe(Effect.provideService(HostProcess.HomeDirectory, home));
217​
218 expect(yield* store.download(h.model)).toBe(h.stale);
219 expect(yield* store.linkedFrom(h.model)).toBe(cached);
220 expect(h.requests).toHaveLength(0);
221 }).pipe(Effect.scoped, Effect.provide(dependencies)),
222);