mirror of
https://github.com/Komediruzecki/token-circles.git
synced 2026-10-20 09:46:43 +02:00
* fix(worker): make transaction balance mutations atomic via D1 batch
The Cloudflare Worker (production data plane) applied each account-balance
mutation as a separate awaited D1 round-trip: create/update/delete issued
the row change plus its reversal and re-apply as up to five independent
`db.run()` calls. A transient error or Worker eviction between them left
the row and the account balances permanently out of sync — creating or
destroying money.
Each money-mutating handler (create, PUT /:id, DELETE /:id, bulk delete,
delete-all) now collects its row change and balance side effects into a
single `c.env.DB.batch([...])`, which D1 runs as one all-or-nothing
transaction — mirroring the pattern already used in imports.ts, tags.ts
and account.ts. `PUT /:id` and `DELETE /:id` pre-check the already-fetched
row for 404 (its WHERE clause matches the mutation), which lets the old,
non-atomic "undo the reversal on 0 changes" compensation blocks go away.
SQL, bind order and balance arithmetic are unchanged — only the execution
boundary moved from N sequential writes to one atomic batch.
Verified with `tsc --noEmit` and a full statement-by-statement review;
the worker has no automated test harness, so exercise on the dev D1 before
promoting to prod.
* test(worker): D1-backed balance-integrity suite for transactions
Runs the real worker (src/index.ts) in workerd via Miniflare against a
local D1 built from worker/migrations/, using @cloudflare/vitest-pool-workers.
Tests drive the transaction handlers over genuine authenticated HTTP (a
real minted session cookie) and assert account balances read from D1.
Covers create / income / expense / transfer, destination-only transfer,
multi-profile create (X-Profile-Ids), amount update, expense->income type
flip, PUT-404 leaves balances untouched, single delete, bulk delete,
delete-all, and D1 batch atomicity (a failing statement rolls back the
whole batch) — 14 tests. Run: pnpm -C worker test
The toolchain is version-sensitive (pool-workers 0.8.19 pairs with vitest
3.1.x) so both are pinned exactly; workerd/esbuild build scripts are
allowlisted via pnpm.onlyBuiltDependencies. The worker stays outside the
pnpm workspace (installs with --ignore-workspace; no committed lockfile,
matching the existing convention).
* chore(worker): bump test toolchain to newest vitest-3 pairing (pool-workers 0.8.57 + vitest 3.2.6)
@cloudflare/vitest-pool-workers is tightly coupled to a vitest major and
changed its config API across the v3->v4 boundary. The latest line
(0.16.x/0.17.0) targets vitest 4 but removed the /config entry and its
pool wiring is rejected by vitest 4 ("Runner ... is not supported") — it's
the newest but currently rough/under-documented. This pins the newest
STABLE pairing on the vitest-3 line, which uses the documented
defineWorkersConfig API and passes all 14 tests. Exact pins because the
pairing is version-sensitive and the worker has no committed lockfile.
* chore(worker): move test toolchain to vitest 4 via the cloudflareTest plugin
Supersedes the temporary vitest-3 pin. The vitest-4 line of
@cloudflare/vitest-pool-workers (0.17.0) does work — it just dropped the
old `test.pool` / `poolOptions.workers` wiring (which vitest 4 reports as
"Runner not supported") in favour of a Vite plugin: `cloudflareTest()`
from the package's main entry, taking the same wrangler/miniflare/
singleWorker/isolatedStorage options. Rewired vitest.config.ts to the
plugin API and pinned pool-workers 0.17.0 + vitest 4.1.9 (exact).
All 14 D1-backed tests pass, and the vitest-3 "chai optimizeDeps" warning
is gone (the v4 plugin fixes the SSR/module-resolution issues).
10 lines
269 B
TypeScript
10 lines
269 B
TypeScript
import type { Env } from '../src/index';
|
|
|
|
// Bindings injected by vitest.config.ts (miniflare.bindings) on top of wrangler.jsonc.
|
|
declare module 'cloudflare:test' {
|
|
interface ProvidedEnv extends Env {
|
|
JWT_SECRET: string;
|
|
TEST_MIGRATIONS: D1Migration[];
|
|
}
|
|
}
|