The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { Database } from "bun:sqlite"; |
| 2 | import { |
| 3 | dispatchAlarmCallbacks, |
| 4 | initializeAlarmCallbacks, |
| 5 | makeDurableObjectCallbackFactory, |
| 6 | } from "@alchemy.run/cloudflare/Workers/AlarmCallback"; |
| 7 | import { |
| 8 | DurableObjectState, |
| 9 | fromDurableObjectState, |
| 10 | } from "@alchemy.run/cloudflare/Workers/DurableObjectState"; |
| 11 | import type * as cf from "@cloudflare/workers-types"; |
| 12 | import { RuntimeContext } from "alchemy/RuntimeContext"; |
| 13 | import * as Effect from "effect/Effect"; |
| 14 | import * as TestClock from "effect/testing/TestClock"; |
| 15 | |
| 16 | /** Real SQLite and the production scheduler, with a controllable native alarm. */ |
| 17 | export const makeAlarmScheduler = Effect.gen(function* () { |
| 18 | yield* TestClock.setTime(10_000); |
| 19 | const db = yield* Effect.acquireRelease( |
| 20 | Effect.sync(() => new Database(":memory:")), |
| 21 | (db) => Effect.sync(() => db.close()), |
| 22 | ); |
| 23 | let alarm: number | null = null; |
| 24 | let transactionId = 0; |
| 25 | const sql = { |
| 26 | exec(query: string, ...bindings: cf.SqlStorageValue[]) { |
| 27 | const before = db |
| 28 | .query<{ count: number }, []>("SELECT total_changes() AS count") |
| 29 | .get()!.count; |
| 30 | const statements = query.split(";").filter((statement) => statement.trim()); |
| 31 | let rows: Record<string, cf.SqlStorageValue>[] = []; |
| 32 | const values = bindings.map((value) => |
| 33 | value instanceof ArrayBuffer ? new Uint8Array(value) : value, |
| 34 | ); |
| 35 | for (const statement of statements) { |
| 36 | rows = db |
| 37 | .query<Record<string, cf.SqlStorageValue>, typeof values>(statement) |
| 38 | .all(...values); |
| 39 | } |
| 40 | const after = db.query<{ count: number }, []>("SELECT total_changes() AS count").get()!.count; |
| 41 | const iterator = rows[Symbol.iterator](); |
| 42 | return { |
| 43 | toArray: () => rows, |
| 44 | one: () => { |
| 45 | if (rows.length !== 1) throw new Error(`Expected one row, got ${rows.length}`); |
| 46 | return rows[0]; |
| 47 | }, |
| 48 | next: () => iterator.next(), |
| 49 | [Symbol.iterator]: () => iterator, |
| 50 | rowsRead: rows.length, |
| 51 | rowsWritten: after - before, |
| 52 | }; |
| 53 | }, |
| 54 | }; |
| 55 | const begin = () => { |
| 56 | const name = `alarm_test_${++transactionId}`; |
| 57 | const previousAlarm = alarm; |
| 58 | db.exec(`SAVEPOINT ${name}`); |
| 59 | return { |
| 60 | commit: () => db.exec(`RELEASE ${name}`), |
| 61 | rollback: () => { |
| 62 | db.exec(`ROLLBACK TO ${name}`); |
| 63 | alarm = previousAlarm; |
| 64 | }, |
| 65 | }; |
| 66 | }; |
| 67 | const native = { |
| 68 | sql, |
| 69 | getAlarm: () => Promise.resolve(alarm), |
| 70 | setAlarm: (at: number | Date) => { |
| 71 | alarm = at instanceof Date ? at.getTime() : at; |
| 72 | return Promise.resolve(); |
| 73 | }, |
| 74 | deleteAlarm: () => { |
| 75 | alarm = null; |
| 76 | return Promise.resolve(); |
| 77 | }, |
| 78 | sync: () => Promise.resolve(), |
| 79 | async transaction<T>(closure: (transaction: unknown) => Promise<T>) { |
| 80 | const transaction = begin(); |
| 81 | try { |
| 82 | const result = await closure({ |
| 83 | ...native, |
| 84 | rollback: transaction.rollback, |
| 85 | }); |
| 86 | transaction.commit(); |
| 87 | return result; |
| 88 | } catch (cause) { |
| 89 | transaction.rollback(); |
| 90 | transaction.commit(); |
| 91 | throw cause; |
| 92 | } |
| 93 | }, |
| 94 | transactionSync<T>(closure: () => T) { |
| 95 | const transaction = begin(); |
| 96 | try { |
| 97 | const result = closure(); |
| 98 | transaction.commit(); |
| 99 | return result; |
| 100 | } catch (cause) { |
| 101 | transaction.rollback(); |
| 102 | transaction.commit(); |
| 103 | throw cause; |
| 104 | } |
| 105 | }, |
| 106 | }; |
| 107 | const raw = { |
| 108 | id: { toString: () => "alarm-scheduler-test" }, |
| 109 | storage: native, |
| 110 | } as unknown as cf.DurableObjectState; |
| 111 | const state = fromDurableObjectState(raw); |
| 112 | let seal = initializeAlarmCallbacks(raw); |
| 113 | return { |
| 114 | db, |
| 115 | raw, |
| 116 | state, |
| 117 | get alarm() { |
| 118 | return alarm; |
| 119 | }, |
| 120 | register: makeDurableObjectCallbackFactory(raw), |
| 121 | seal: () => seal(), |
| 122 | restart: () => { |
| 123 | seal = initializeAlarmCallbacks(raw); |
| 124 | }, |
| 125 | fire: Effect.gen(function* () { |
| 126 | if (alarm === null) return yield* Effect.die("No native alarm is scheduled"); |
| 127 | yield* TestClock.setTime(alarm); |
| 128 | alarm = null; |
| 129 | yield* dispatchAlarmCallbacks(raw, false).pipe( |
| 130 | Effect.provideService(DurableObjectState, state), |
| 131 | ); |
| 132 | }), |
| 133 | }; |
| 134 | }).pipe(Effect.provide(RuntimeContext.phantom)); |