71 lines
3.1 KiB
TypeScript
71 lines
3.1 KiB
TypeScript
import * as NodeServices from "@effect/platform-node/NodeServices";
|
|
import { assert, describe, it } from "@effect/vitest";
|
|
import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
|
|
import * as Context from "effect/Context";
|
|
import * as Effect from "effect/Effect";
|
|
import * as FileSystem from "effect/FileSystem";
|
|
import * as Layer from "effect/Layer";
|
|
import * as Path from "effect/Path";
|
|
import * as SqlClient from "effect/sql/SqlClient";
|
|
|
|
import { readOpenCodeUsage } from "./usage.ts";
|
|
|
|
/** A writable connection that stays open until the test's scope closes. */
|
|
const openDatabase = (filename: string) =>
|
|
Layer.build(NodeSqliteClient.layer({ filename })).pipe(
|
|
Effect.map(Context.get(SqlClient.SqlClient)),
|
|
);
|
|
|
|
describe("readOpenCodeUsage", () => {
|
|
it.effect("counts migrated OpenCode messages once and sees subsequent WAL writes", () =>
|
|
Effect.gen(function* () {
|
|
const fileSystem = yield* FileSystem.FileSystem;
|
|
const path = yield* Path.Path;
|
|
const dir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-reader-test-" });
|
|
const db = yield* openDatabase(path.join(dir, "opencode.db"));
|
|
yield* db.unsafe("PRAGMA journal_mode = WAL");
|
|
yield* db.unsafe("PRAGMA wal_autocheckpoint = 0");
|
|
yield* db.unsafe("CREATE TABLE message (id TEXT, session_id TEXT, data TEXT)");
|
|
const message = {
|
|
id: "msg-1",
|
|
sessionID: "session-1",
|
|
role: "assistant",
|
|
modelID: "claude-sonnet-4-5",
|
|
time: { created: 1780000000000 },
|
|
cost: 0.25,
|
|
tokens: { input: 100, output: 20, reasoning: 5, cache: { read: 30, write: 10 } },
|
|
};
|
|
const insert = (id: string, sessionId: string, data: string) =>
|
|
db.unsafe("INSERT INTO message VALUES (?, ?, ?)", [id, sessionId, data]);
|
|
yield* insert(message.id, message.sessionID, JSON.stringify(message));
|
|
const legacy = path.join(dir, "storage", "message", message.sessionID);
|
|
yield* fileSystem.makeDirectory(legacy, { recursive: true });
|
|
yield* fileSystem.writeFileString(path.join(legacy, "msg-1.json"), JSON.stringify(message));
|
|
const first = yield* readOpenCodeUsage(dir, 0);
|
|
assert.isFalse(first.error);
|
|
const records = first.files.flatMap((file) => file.records);
|
|
assert.strictEqual(records.length, 1);
|
|
assert.deepStrictEqual(records[0]?.totals, {
|
|
uncachedInputTokens: 100,
|
|
cachedInputTokens: 30,
|
|
cacheCreationTokens: 10,
|
|
outputTokens: 25,
|
|
reasoningTokens: 5,
|
|
});
|
|
assert.strictEqual(records[0]?.reportedCostUsd, 0.25);
|
|
yield* insert(
|
|
"msg-2",
|
|
message.sessionID,
|
|
JSON.stringify({ ...message, id: "msg-2", time: { created: 1780000001000 } }),
|
|
);
|
|
const next = yield* readOpenCodeUsage(dir, 1780000001000);
|
|
assert.isFalse(next.error);
|
|
assert.deepStrictEqual(
|
|
next.files.flatMap((file) => file.records).map((record) => record.dedupeKey),
|
|
["opencode:msg-2"],
|
|
);
|
|
assert.isAbove(Number((yield* fileSystem.stat(path.join(dir, "opencode.db-wal"))).size), 0);
|
|
}).pipe(Effect.provide(NodeServices.layer)),
|
|
);
|
|
});
|