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

usage.test.ts

425 lines17.9 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, describe, it } from "@effect/vitest";
3import { DEFAULT_SERVER_SETTINGS, ProviderInstanceId } from "@t3tools/contracts";
4import * as HostProcess from "@t3tools/shared/HostProcess";
5import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
6import * as Context from "effect/Context";
7import * as Effect from "effect/Effect";
8import * as FileSystem from "effect/FileSystem";
9import * as Layer from "effect/Layer";
10import * as Path from "effect/Path";
11import * as SqlClient from "effect/sql/SqlClient";
12import { vi } from "vite-plus/test";
13​
14import { openCodeUsageReader, readOpenCodeUsage } from "./usage.ts";
15​
16const openDatabase = (filename: string) =>
17 Layer.build(NodeSqliteClient.layer({ filename })).pipe(
18 Effect.map(Context.get(SqlClient.SqlClient)),
19 );
20​
21describe("readOpenCodeUsage", () => {
22 it.effect("bounds raw history allocations while counting every eligible message once", () =>
23 Effect.gen(function* () {
24 const fileSystem = yield* FileSystem.FileSystem;
25 const path = yield* Path.Path;
26 const dir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-reader-large-test-" });
27 const db = yield* openDatabase(path.join(dir, "opencode.db"));
28 yield* db.unsafe("PRAGMA journal_mode = WAL");
29 yield* db.unsafe(
30 "CREATE TABLE message (id TEXT, session_id TEXT, data TEXT, time_created INTEGER)",
31 );
32 const sinceMs = 1780000000000;
33 const message = {
34 role: "assistant",
35 modelID: "claude-sonnet-4-5",
36 time: { created: sinceMs },
37 tokens: { input: 100, output: 20 },
38 body: "x".repeat(256 * 1024),
39 };
40 const data = JSON.stringify(message);
41 const insert = (rowId: number, id: string, payload: string, created = sinceMs) =>
42 db.unsafe(
43 "INSERT INTO message (rowid, id, session_id, data, time_created) VALUES (?, ?, ?, ?, ?)",
44 [rowId, id, "session-1", payload, created],
45 );
46 for (let index = 0; index < 130; index++) {
47 yield* insert(index * 2 - 10, `msg-${index}`, data);
48 }
49 yield* insert(300, "old", data, sinceMs - 1);
50 yield* insert(301, "user", JSON.stringify({ ...message, role: "user" }));
51 for (let index = 0; index < 32; index++) {
52 yield* insert(
53 400 + index,
54 `user-${index}`,
55 JSON.stringify({
56 role: "user",
57 summary: { diffs: [{ patch: "x".repeat(1024 * 1024) }] },
58 }),
59 );
60 }
61 yield* insert(302, "invalid", "{invalid");
62 yield* insert(303, "msg-0", data);
63​
64 const writer = yield* Effect.acquireRelease(
65 Effect.sync(() => new NodeSqlite.DatabaseSync(path.join(dir, "opencode.db"))),
66 (database) => Effect.sync(() => database.close()),
67 );
68 let changed = false;
69 let largestRawResult = 0;
70 const originalAll = NodeSqlite.StatementSync.prototype.all;
71 yield* Effect.acquireRelease(
72 Effect.sync(() =>
73 vi.spyOn(NodeSqlite.StatementSync.prototype, "all").mockImplementation(function (
74 this: NodeSqlite.StatementSync,
75 ...params
76 ) {
77 const rows = originalAll.apply(this, params);
78 const rawSize = rows.reduce(
79 (size, row) => size + (typeof row.data === "string" ? row.data.length : 0),
80 0,
81 );
82 largestRawResult = Math.max(largestRawResult, rawSize);
83 if (!changed && rawSize > 0) {
84 changed = true;
85 writer
86 .prepare("UPDATE message SET data = ? WHERE id = ?")
87 .run(JSON.stringify({ ...message, tokens: { input: 900, output: 20 } }), "msg-129");
88 }
89 return rows;
90 }),
91 ),
92 (spy) => Effect.sync(() => spy.mockRestore()),
93 );
94 const result = yield* readOpenCodeUsage(dir, sinceMs);
95 assert.isFalse(result.error);
96 const records = result.files.flatMap((file) => file.records);
97 assert.deepStrictEqual(
98 records.map((record) => record.dedupeKey),
99 Array.from({ length: 130 }, (_, index) => `opencode:msg-${index}`),
100 );
101 assert.strictEqual(
102 records.reduce((total, record) => total + record.totals.uncachedInputTokens, 0),
103 13000,
104 );
105 assert.strictEqual(
106 (yield* readOpenCodeUsage(dir, sinceMs)).files
107 .flatMap((file) => file.records)
108 .find((record) => record.dedupeKey === "opencode:msg-129")?.totals.uncachedInputTokens,
109 900,
110 );
111 assert.isAbove(largestRawResult, 0);
112 assert.isBelow(largestRawResult, 64 * 1024);
113 }).pipe(Effect.provide(NodeServices.layer)),
114 );
115​
116 it.effect("reads statements in proportion to the window despite sparse, unordered rowids", () =>
117 Effect.gen(function* () {
118 const fileSystem = yield* FileSystem.FileSystem;
119 const path = yield* Path.Path;
120 const dir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-window-" });
121 const db = yield* openDatabase(path.join(dir, "opencode.db"));
122 yield* db.unsafe(
123 "CREATE TABLE message (id TEXT, session_id TEXT, data TEXT, time_created INTEGER)",
124 );
125 yield* db.unsafe("CREATE INDEX message_time ON message(time_created)");
126 const sinceMs = 1780000000000;
127 const data = JSON.stringify({ modelID: "m", tokens: { input: 1 } });
128 yield* db.unsafe(
129 `WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < 5000)
130 INSERT INTO message SELECT 'old-' || i, 's', ?, ? FROM n`,
131 [data, sinceMs - 1],
132 );
133 for (const [rowid, id] of [
134 [-10, "a"],
135 [10000, "b"],
136 [20000, "c"],
137 ] as const) {
138 yield* db.unsafe(
139 "INSERT INTO message(rowid, id, session_id, data, time_created) VALUES (?, ?, 's', ?, ?)",
140 [rowid, id, data, sinceMs],
141 );
142 }
143 let statements = 0;
144 const originalAll = NodeSqlite.StatementSync.prototype.all;
145 yield* Effect.acquireRelease(
146 Effect.sync(() =>
147 vi.spyOn(NodeSqlite.StatementSync.prototype, "all").mockImplementation(function (
148 this: NodeSqlite.StatementSync,
149 ...params
150 ) {
151 statements++;
152 return originalAll.apply(this, params);
153 }),
154 ),
155 (spy) => Effect.sync(() => spy.mockRestore()),
156 );
157 const result = yield* readOpenCodeUsage(dir, sinceMs);
158 assert.isFalse(result.error);
159 assert.deepStrictEqual(
160 result.files.flatMap((file) => file.records).map((record) => record.dedupeKey),
161 ["opencode:a", "opencode:b", "opencode:c"],
162 );
163 assert.isBelow(statements, 10);
164 }).pipe(Effect.provide(NodeServices.layer)),
165 );
166​
167 it.effect("counts migrated OpenCode messages once and sees subsequent WAL writes", () =>
168 Effect.gen(function* () {
169 const fileSystem = yield* FileSystem.FileSystem;
170 const path = yield* Path.Path;
171 const dir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-reader-test-" });
172 const db = yield* openDatabase(path.join(dir, "opencode.db"));
173 yield* db.unsafe("PRAGMA journal_mode = WAL");
174 yield* db.unsafe("PRAGMA wal_autocheckpoint = 0");
175 yield* db.unsafe("CREATE TABLE message (id TEXT, session_id TEXT, data TEXT)");
176 const message = {
177 id: "msg-1",
178 sessionID: "session-1",
179 role: "assistant",
180 modelID: "claude-sonnet-4-5",
181 time: { created: 1780000000000 },
182 cost: 0.25,
183 tokens: { input: 100, output: 20, reasoning: 5, cache: { read: 30, write: 10 } },
184 };
185 const insert = (id: string, sessionId: string, data: string) =>
186 db.unsafe("INSERT INTO message VALUES (?, ?, ?)", [id, sessionId, data]);
187 yield* insert(message.id, message.sessionID, JSON.stringify(message));
188 const legacy = path.join(dir, "storage", "message", message.sessionID);
189 yield* fileSystem.makeDirectory(legacy, { recursive: true });
190 yield* fileSystem.writeFileString(path.join(legacy, "msg-1.json"), JSON.stringify(message));
191 const first = yield* readOpenCodeUsage(dir, 0);
192 assert.isFalse(first.error);
193 const records = first.files.flatMap((file) => file.records);
194 assert.strictEqual(records.length, 1);
195 assert.deepStrictEqual(records[0]?.totals, {
196 uncachedInputTokens: 100,
197 cachedInputTokens: 30,
198 cacheCreationTokens: 10,
199 outputTokens: 25,
200 reasoningTokens: 5,
201 });
202 assert.strictEqual(records[0]?.reportedCostUsd, 0.25);
203 yield* insert(
204 "msg-2",
205 message.sessionID,
206 JSON.stringify({ ...message, id: "msg-2", time: { created: 1780000001000 } }),
207 );
208 const next = yield* readOpenCodeUsage(dir, 1780000001000);
209 assert.isFalse(next.error);
210 assert.deepStrictEqual(
211 next.files.flatMap((file) => file.records).map((record) => record.dedupeKey),
212 ["opencode:msg-2"],
213 );
214 assert.isAbove(Number((yield* fileSystem.stat(path.join(dir, "opencode.db-wal"))).size), 0);
215 }).pipe(Effect.provide(NodeServices.layer)),
216 );
217​
218 it.effect(
219 "projects both SQLite formats without changing malformed or optional field handling",
220 () =>
221 Effect.gen(function* () {
222 const fileSystem = yield* FileSystem.FileSystem;
223 const path = yield* Path.Path;
224 const dir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-formats-" });
225 const db = yield* openDatabase(path.join(dir, "opencode.db"));
226 const sinceMs = 1780000000000;
227 const message = { model: { id: "m" }, tokens: { input: 10 }, time: { created: sinceMs } };
228 const cases = [
229 ["assistant", JSON.stringify({ ...message, role: "assistant", cost: 0.25 }), true],
230 ["absent-role", JSON.stringify(message), true],
231 ["null-role", JSON.stringify({ ...message, role: null }), false],
232 ["user", JSON.stringify({ ...message, role: "user" }), false],
233 ["malformed", "{broken", false],
234 ["json5", "{role:'assistant'}", false],
235 ["null", null, false],
236 ["blob", new Uint8Array(Buffer.from(JSON.stringify(message))), false],
237 ["fallback", JSON.stringify({ modelID: "fallback-model", tokens: { output: 5 } }), true],
238 ] as const;
239 for (const table of ["message", "session_message"]) {
240 yield* db.unsafe(
241 `CREATE TABLE ${table} (id TEXT, session_id TEXT, data, type TEXT, time_created INTEGER)`,
242 );
243 for (const [id, data] of cases) {
244 yield* db.unsafe(`INSERT INTO ${table} VALUES (?, 's', ?, 'assistant', ?)`, [
245 `${table}-${id}`,
246 data,
247 sinceMs,
248 ]);
249 }
250 yield* db.unsafe(`INSERT INTO ${table} VALUES (?, 's', ?, 'user', ?)`, [
251 `${table}-type-user`,
252 JSON.stringify(message),
253 sinceMs,
254 ]);
255 }
256 const result = yield* readOpenCodeUsage(dir, sinceMs);
257 assert.isFalse(result.error);
258 const records = result.files.flatMap((file) => file.records);
259 assert.deepStrictEqual(
260 records.map((record) => record.dedupeKey),
261 [
262 ...cases.filter(([, , valid]) => valid).map(([id]) => `opencode:message-${id}`),
263 "opencode:message-type-user",
264 ...cases.filter(([, , valid]) => valid).map(([id]) => `opencode:session_message-${id}`),
265 ],
266 );
267 assert.strictEqual(records[0]?.reportedCostUsd, 0.25);
268 assert.strictEqual(records[2]?.model, "fallback-model");
269 assert.strictEqual(records[2]?.timestampMs, sinceMs);
270 }).pipe(Effect.provide(NodeServices.layer)),
271 );
272​
273 it.effect("reads only legacy OpenCode messages written since the window opened", () =>
274 Effect.gen(function* () {
275 const fileSystem = yield* FileSystem.FileSystem;
276 const path = yield* Path.Path;
277 const dir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-reader-test-" });
278 const sinceMs = 1780000000000;
279 const legacy = path.join(dir, "storage", "message", "session-1");
280 yield* fileSystem.makeDirectory(legacy, { recursive: true });
281 const write = Effect.fn(function* (id: string, mtimeMs: number) {
282 const file = path.join(legacy, `${id}.json`);
283 yield* fileSystem.writeFileString(
284 file,
285 JSON.stringify({
286 id,
287 sessionID: "session-1",
288 role: "assistant",
289 modelID: "claude-sonnet-4-5",
290 time: { created: sinceMs + 1000 },
291 tokens: { input: 100, output: 20 },
292 }),
293 );
294 yield* fileSystem.utimes(file, mtimeMs / 1000, mtimeMs / 1000);
295 return file;
296 });
297 yield* write("msg-old", sinceMs - 60_000);
298​
299 const stale = yield* readOpenCodeUsage(dir, sinceMs);
300 assert.deepStrictEqual(stale, { files: [], missing: false, error: false });
301​
302 const recent = yield* write("msg-new", sinceMs + 60_000);
303 const result = yield* readOpenCodeUsage(dir, sinceMs);
304 assert.isFalse(result.missing);
305 assert.isFalse(result.error);
306 assert.deepStrictEqual(
307 result.files.map((file) => file.path),
308 [recent],
309 );
310 assert.deepStrictEqual(
311 result.files.flatMap((file) => file.records).map((record) => record.dedupeKey),
312 ["opencode:msg-new"],
313 );
314 }).pipe(Effect.provide(NodeServices.layer)),
315 );
316​
317 it.effect("never follows symlinks in the legacy OpenCode store", () =>
318 Effect.gen(function* () {
319 const fileSystem = yield* FileSystem.FileSystem;
320 const path = yield* Path.Path;
321 const dir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-reader-test-" });
322 const outside = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-reader-test-" });
323 const sinceMs = 1780000000000;
324 const store = path.join(dir, "storage", "message");
325 const session = path.join(store, "session-1");
326 yield* fileSystem.makeDirectory(session, { recursive: true });
327 const write = Effect.fn(function* (directory: string, id: string, mtimeMs: number) {
328 const file = path.join(directory, `${id}.json`);
329 yield* fileSystem.writeFileString(
330 file,
331 JSON.stringify({
332 id,
333 sessionID: "session-1",
334 role: "assistant",
335 modelID: "claude-sonnet-4-5",
336 time: { created: sinceMs + 1000 },
337 tokens: { input: 100, output: 20 },
338 }),
339 );
340 yield* fileSystem.utimes(file, mtimeMs / 1000, mtimeMs / 1000);
341 return file;
342 });
343 const target = yield* write(outside, "msg-linked", sinceMs + 60_000);
344 const stale = yield* write(outside, "msg-stale", sinceMs - 60_000);
345 yield* fileSystem.symlink(target, path.join(session, "msg-linked.json"));
346 yield* fileSystem.symlink(stale, path.join(session, "msg-stale.json"));
347 yield* fileSystem.symlink(outside, path.join(store, "session-linked"));
348 yield* fileSystem.symlink(store, path.join(session, "cycle"));
349 yield* fileSystem.symlink(path.join(outside, "gone.json"), path.join(session, "gone.json"));
350 yield* fileSystem.symlink(path.join(session, "loop.json"), path.join(session, "loop.json"));
351​
352 const linksOnly = yield* readOpenCodeUsage(dir, sinceMs);
353 assert.deepStrictEqual(linksOnly, { files: [], missing: true, error: false });
354​
355 const real = yield* write(session, "msg-real", sinceMs + 60_000);
356 const result = yield* readOpenCodeUsage(dir, sinceMs);
357 assert.isFalse(result.missing);
358 assert.isFalse(result.error);
359 assert.deepStrictEqual(
360 result.files.map((file) => file.path),
361 [real],
362 );
363 assert.deepStrictEqual(
364 result.files.flatMap((file) => file.records).map((record) => record.dedupeKey),
365 ["opencode:msg-real"],
366 );
367 }).pipe(Effect.provide(NodeServices.layer)),
368 );
369});
370import * as NodeSqlite from "node:sqlite";
371​
372describe("openCodeUsageReader", () => {
373 it.effect("scans canonical instance roots once, including XDG and home defaults", () =>
374 Effect.gen(function* () {
375 const fileSystem = yield* FileSystem.FileSystem;
376 const path = yield* Path.Path;
377 const temp = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-instances-test-" });
378 const home = yield* fileSystem.realPath(temp);
379 const defaultRoot = path.join(home, ".local", "share", "opencode");
380 const instanceRoot = path.join(home, "instance");
381 const dataHome = path.join(home, "xdg");
382 const xdgRoot = path.join(dataHome, "opencode");
383 const alias = path.join(home, "default-alias");
384 for (const root of [defaultRoot, instanceRoot, xdgRoot]) {
385 yield* fileSystem.makeDirectory(root, { recursive: true });
386 }
387 yield* fileSystem.symlink(defaultRoot, alias);
388 const instances = [
389 { id: "opencode", environment: {}, configured: false },
390 {
391 id: "opencode-list",
392 environment: { OPENCODE_DATA_DIR: ` ${instanceRoot}, ${alias}, ${defaultRoot}, , ` },
393 configured: true,
394 },
395 {
396 id: "opencode-xdg",
397 environment: { XDG_DATA_HOME: dataHome },
398 configured: true,
399 },
400 ];
401 if (openCodeUsageReader.kind !== "scan") throw new Error("Expected an OpenCode scan reader");
402 const sources = yield* openCodeUsageReader
403 .scan({
404 instances: instances.map(({ id, environment, configured }) => ({
405 instanceId: ProviderInstanceId.make(id),
406 config: undefined,
407 environment,
408 configured,
409 })),
410 settings: DEFAULT_SERVER_SETTINGS,
411 windowStartMs: 0,
412 retentionCutoffMs: 0,
413 awaitRefresh: true,
414 })
415 .pipe(
416 Effect.provideService(HostProcess.Environment, {}),
417 Effect.provideService(HostProcess.HomeDirectory, home),
418 );
419 assert.deepStrictEqual(
420 sources.map((source) => source.dir).sort(),
421 [defaultRoot, instanceRoot, xdgRoot].sort(),
422 );
423 }).pipe(Effect.provide(NodeServices.layer)),
424 );
425});