| 1 | import type { D1Like, D1Statement, SqlRunner } from "../src/types"; |
| 2 | |
| 3 | export const INSTALL_A = "3f2b1c4e-5a6d-4e7f-8a9b-0c1d2e3f4a5b"; |
| 4 | export const INSTALL_B = "9a8b7c6d-5e4f-4a3b-9c2d-1e0f9a8b7c6d"; |
| 5 | |
| 6 | export function fakeRunner(respond: (sql: string) => unknown[], source: SqlRunner["source"] = "binding") { |
| 7 | const seen: string[] = []; |
| 8 | const runner: SqlRunner = { |
| 9 | source, |
| 10 | async run(sql) { |
| 11 | seen.push(sql); |
| 12 | return respond(sql); |
| 13 | }, |
| 14 | }; |
| 15 | return { runner, seen }; |
| 16 | } |
| 17 | |
| 18 | export function answer(sql: string): unknown[] { |
| 19 | if (sql.includes("blob1 = 'session_start'")) { |
| 20 | const key = sql.includes("blob6 AS key") ? "0.10.3" : sql.includes("blob2 AS key") ? "tui" : sql.includes("blob3 AS key") ? "macos" : "all"; |
| 21 | return [ |
| 22 | { day: "2026-11-10", key, installs: "4", sessions: "10", index1: INSTALL_A, install_id: INSTALL_A }, |
| 23 | { day: "2026-11-11", key, installs: "5", sessions: "22" }, |
| 24 | ]; |
| 25 | } |
| 26 | if (sql.includes("blob1 = 'session_end'")) return [{ day: "2026-11-10", surface: "tui", turns: "30", tool_calls: "90" }, { day: "2026-11-10", surface: "exec", turns: "5", tool_calls: "1" }]; |
| 27 | if (sql.includes("blob1 = 'tool_usage'")) return [{ day: "2026-11-10", tool: "bash", calls: "70", errors: "3" }]; |
| 28 | if (sql.includes("blob1 = 'model_usage' AND double1")) return [{ day: "2026-11-10", installs: "2" }]; |
| 29 | if (sql.includes("blob1 = 'model_usage'")) return [{ day: "2026-11-10", provider: "deepseek", model: "other", turns_ok: "8", turns_failed: "1", input_tokens: "1000", output_tokens: "200" }, { day: "2026-11-10", provider: "deepseek", model: "deepseek-v4", turns_ok: "2", turns_failed: "0", input_tokens: "50", output_tokens: "5" }]; |
| 30 | if (sql.includes("'install_or_upgrade'")) return [{ day: "2026-11-10", installs: "3" }]; |
| 31 | if (sql.includes("'funnel_usage'")) return [ |
| 32 | { day: "2026-11-10", counters: JSON.stringify({ home_view: 4, install_view: 2 }), weight: "2" }, |
| 33 | { day: "2026-11-10", counters: JSON.stringify({ home_view: 1, install_view: 0 }), weight: "1" }, |
| 34 | ]; |
| 35 | return []; |
| 36 | } |
| 37 | |
| 38 | export class FakeD1 implements D1Like { |
| 39 | rows = new Map<string, { day: string; metric: string; dim: string; key: string; value: number }>(); |
| 40 | prepare(sql: string): D1Statement { |
| 41 | const db = this; |
| 42 | let args: unknown[] = []; |
| 43 | const stmt: D1Statement & { run(): void } = { |
| 44 | bind(...values: unknown[]) { |
| 45 | args = values; |
| 46 | return stmt; |
| 47 | }, |
| 48 | async all() { |
| 49 | if (sql.includes("FROM daily_totals")) { |
| 50 | const [from, ...metrics] = args as string[]; |
| 51 | return { results: [...db.rows.values()].filter((r) => r.dim === "all" && r.day >= from && metrics.includes(r.metric)) as never[] }; |
| 52 | } |
| 53 | return { results: [] }; |
| 54 | }, |
| 55 | async first() { |
| 56 | const days = [...db.rows.values()].map((r) => r.day).sort(); |
| 57 | return { day: days.length ? days[days.length - 1] : null } as never; |
| 58 | }, |
| 59 | run() { |
| 60 | const [day, metric, dim, key, value] = args as [string, string, string, string, number]; |
| 61 | db.rows.set(`${day}|${metric}|${dim}|${key}`, { day, metric, dim, key, value }); |
| 62 | }, |
| 63 | }; |
| 64 | return stmt; |
| 65 | } |
| 66 | async batch(statements: D1Statement[]) { |
| 67 | for (const s of statements) (s as unknown as { run(): void }).run(); |
| 68 | return []; |
| 69 | } |
| 70 | } |
| 71 |