The best way to control Claude Code, Codex and more from any computer or phone

main
ts 71 lines 2.67 KB
Raw
1 // @effect-diagnostics nodeBuiltinImport:off - real SQLite verifies the D1 transaction's deduplication.
2 import * as NodeSqlite from "node:sqlite";
3 import * as NodeFS from "node:fs";
4 import * as Effect from "effect/Effect";
5 import type * as Cloudflare from "@cloudflare/workers-types";
6 import { expect, it } from "@effect/vitest";
7 import * as Savings from "./Savings.ts";
8 import * as Layer from "effect/Layer";
9
10 it.effect(
11 "increments lifetime totals once per receipt, including retries and different hosts",
12 () =>
13 Effect.gen(function* () {
14 const sqlite = new NodeSqlite.DatabaseSync(":memory:");
15 try {
16 sqlite.exec(
17 NodeFS.readFileSync(
18 new URL("../migrations/savings/0001_savings.sql", import.meta.url),
19 "utf8",
20 ),
21 );
22 sqlite.exec(
23 NodeFS.readFileSync(
24 new URL("../migrations/savings/0002_unspecified_tier.sql", import.meta.url),
25 "utf8",
26 ),
27 );
28 const db = {
29 prepare: (sql: string) => ({ bind: (...args: (string | number)[]) => ({ sql, args }) }),
30 batch: async (statements: { sql: string; args: (string | number)[] }[]) => {
31 sqlite.exec("BEGIN");
32 try {
33 for (const statement of statements)
34 sqlite.prepare(statement.sql).run(...statement.args);
35 sqlite.exec("COMMIT");
36 } catch (error) {
37 sqlite.exec("ROLLBACK");
38 throw error;
39 }
40 return [];
41 },
42 } as unknown as Cloudflare.D1Database;
43 const savings = yield* Savings.Savings.pipe(
44 Effect.provide(Savings.layer.pipe(Layer.provide(Layer.succeed(Savings.Database, db)))),
45 );
46 const summary = {
47 id: "a".repeat(64),
48 recordedAt: 1,
49 host: "mbp",
50 platform: "darwin",
51 arch: "arm64",
52 ci: false,
53 runId: "",
54 tasks: [{ package: "server", task: "check", source: "remote" as const, savedMs: 900 }],
55 };
56 yield* savings.record(summary, "alice");
57 yield* savings.record(summary, "alice");
58 yield* savings.record({ ...summary, id: "b".repeat(64), host: "linux" }, "bob");
59 yield* savings.record({ ...summary, id: "c".repeat(64) }, "alice");
60 const rows = sqlite
61 .prepare("SELECT developer, host, remote_ms, remote_hits FROM savings ORDER BY developer")
62 .all();
63 expect(rows).toEqual([
64 { developer: "alice", host: "mbp", remote_ms: 1800, remote_hits: 2 },
65 { developer: "bob", host: "linux", remote_ms: 900, remote_hits: 1 },
66 ]);
67 } finally {
68 sqlite.close();
69 }
70 }),
71 );