The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { MssqlClient } from "@effect/sql-mssql" |
| 2 | import { assert, it } from "@effect/vitest" |
| 3 | import { Effect } from "effect" |
| 4 | import * as Digest from "effect/crypto/Digest" |
| 5 | import type * as EventJournal from "effect/eventlog/EventJournal" |
| 6 | import * as EventLogEncryption from "effect/eventlog/EventLogEncryption" |
| 7 | import type { StoreId } from "effect/eventlog/EventLogMessage" |
| 8 | import * as SqlEventLogServer from "effect/eventlog/SqlEventLogServerEncrypted" |
| 9 | import * as Reactivity from "effect/reactivity/Reactivity" |
| 10 | import * as SqlClient from "effect/sql/SqlClient" |
| 11 | import { Buffer } from "node:buffer" |
| 12 | import type * as Tedious from "tedious" |
| 13 | import { vi } from "vitest" |
| 14 | |
| 15 | vi.mock("tedious", async (importOriginal) => { |
| 16 | const original = await importOriginal<typeof Tedious>() |
| 17 | |
| 18 | class MockConnection extends original.Connection { |
| 19 | readonly entryIds = new Set<string>() |
| 20 | |
| 21 | override connect(callback?: (error?: Error) => void) { |
| 22 | callback?.() |
| 23 | } |
| 24 | override close() {} |
| 25 | override beginTransaction(callback: (error?: Error | null) => void) { |
| 26 | callback() |
| 27 | } |
| 28 | override commitTransaction(callback: (error?: Error | null) => void) { |
| 29 | callback() |
| 30 | } |
| 31 | override rollbackTransaction(callback: (error?: Error | null) => void) { |
| 32 | callback() |
| 33 | } |
| 34 | override cancel() { |
| 35 | return false |
| 36 | } |
| 37 | override makeRequest(request: Tedious.Request | Tedious.BulkLoad) { |
| 38 | if (!(request instanceof original.Request)) { |
| 39 | throw new Error("Unexpected bulk load") |
| 40 | } |
| 41 | const query = request.sqlTextOrProcedure?.trimStart() ?? "" |
| 42 | if (query.startsWith("MERGE")) { |
| 43 | const rows = [] |
| 44 | for (const parameter of request.parameters) { |
| 45 | const id = parameter.value as Uint8Array |
| 46 | const key = Array.from(id).join(",") |
| 47 | if (this.entryIds.has(key)) continue |
| 48 | this.entryIds.add(key) |
| 49 | rows.push([{ metadata: { colName: "entry_id" }, value: id }]) |
| 50 | } |
| 51 | request.callback(null, rows.length, rows) |
| 52 | return |
| 53 | } |
| 54 | if (!query.startsWith("SELECT")) { |
| 55 | request.callback(null, 0, []) |
| 56 | return |
| 57 | } |
| 58 | // The store row the storage locks before each write. |
| 59 | if (query.startsWith("SELECT next_sequence")) { |
| 60 | const store = { |
| 61 | next_sequence: 1, |
| 62 | active_activation: "originals", |
| 63 | active_batch_set: null, |
| 64 | base_sequence: 0, |
| 65 | active_kept_count: 0, |
| 66 | no_op_at: null, |
| 67 | no_op_count: 0, |
| 68 | open_stage: null, |
| 69 | next_sweep_at: null, |
| 70 | sweep_lease_until: null, |
| 71 | sweep_lease_owner: null, |
| 72 | pruned_through: 0 |
| 73 | } |
| 74 | request.callback(null, 1, [Object.entries(store).map(([colName, value]) => ({ metadata: { colName }, value }))]) |
| 75 | return |
| 76 | } |
| 77 | if (query.startsWith("SELECT MAX(last_sequence)")) { |
| 78 | request.callback(null, 1, [[{ metadata: { colName: "last_sequence" }, value: null }]]) |
| 79 | return |
| 80 | } |
| 81 | const rows = request.parameters.map((parameter) => [ |
| 82 | { metadata: { colName: "value" }, value: parameter.value } |
| 83 | ]) |
| 84 | request.callback(null, rows.length, rows) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return { ...original, Connection: MockConnection } |
| 89 | }) |
| 90 | |
| 91 | it.effect("binds an interpolated Uint8Array as VarBinary", () => |
| 92 | Effect.gen(function*() { |
| 93 | const client = yield* MssqlClient.make({ |
| 94 | server: "mock.invalid", |
| 95 | minConnections: 0, |
| 96 | maxConnections: 1 |
| 97 | }) |
| 98 | const rows = yield* client<{ value: Uint8Array }>`SELECT ${new Uint8Array([0, 128, 255])} AS value` |
| 99 | |
| 100 | assert.deepStrictEqual(Array.from(rows[0].value), [0, 128, 255]) |
| 101 | }).pipe(Effect.provide(Reactivity.layer))) |
| 102 | |
| 103 | for ( |
| 104 | const [name, ids, expected] of [ |
| 105 | ["preserves distinct Buffer entry IDs", [Buffer.alloc(16, 0xff), Buffer.alloc(16, 0xfe)], [0xff, 0xfe]], |
| 106 | [ |
| 107 | "deduplicates the same entry ID across Buffer and Uint8Array", |
| 108 | [Buffer.alloc(16, 0xff), new Uint8Array(16).fill(0xff)], |
| 109 | [0xff] |
| 110 | ] |
| 111 | ] as const |
| 112 | ) { |
| 113 | it.effect(name, () => |
| 114 | Effect.gen(function*() { |
| 115 | const client = yield* MssqlClient.make({ |
| 116 | server: "mock.invalid", |
| 117 | minConnections: 0, |
| 118 | maxConnections: 1 |
| 119 | }) |
| 120 | const storage = yield* SqlEventLogServer.makeStorage().pipe( |
| 121 | Effect.provideService(SqlClient.SqlClient, client) |
| 122 | ) |
| 123 | const written = yield* storage.write( |
| 124 | "public-key", |
| 125 | "store-a" as StoreId, |
| 126 | ids.map((id, index): EventLogEncryption.EncryptedBatch => ({ |
| 127 | entryIds: [id as EventJournal.EntryId], |
| 128 | iv: new Uint8Array(12), |
| 129 | encryptedBatch: Uint8Array.of(index) |
| 130 | })) |
| 131 | ) |
| 132 | |
| 133 | assert.deepStrictEqual( |
| 134 | written.flatMap((batch) => batch.entryIds.map((id) => Array.from(id))), |
| 135 | expected.map((byte) => Array(16).fill(byte)) |
| 136 | ) |
| 137 | }).pipe(Effect.provide([Reactivity.layer, EventLogEncryption.layerSubtle, Digest.layerWebCrypto]))) |
| 138 | } |