| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { UserRepo } from "./users"; |
| 3 | |
| 4 | describe("UserRepo.updateProfile", () => { |
| 5 | it("claims a new handle atomically and reports a conflict without a read-then-write race", async () => { |
| 6 | let sql = ""; |
| 7 | let binds: unknown[] = []; |
| 8 | const statement = { |
| 9 | bind(...values: unknown[]) { binds = values; return this; }, |
| 10 | async run() { return { meta: { changes: 0 } }; }, |
| 11 | }; |
| 12 | const db = { |
| 13 | prepare(query: string) { |
| 14 | sql = query; |
| 15 | return statement; |
| 16 | }, |
| 17 | } as unknown as D1Database; |
| 18 | |
| 19 | const row = await new UserRepo(db).updateProfile(7, { handle: "claimed" }); |
| 20 | expect(row).toBeNull(); |
| 21 | expect(sql).toContain("NOT EXISTS"); |
| 22 | expect(sql).toContain("other.id !="); |
| 23 | expect(binds).toContain("claimed"); |
| 24 | expect(binds).toContain(7); |
| 25 | }); |
| 26 | }); |
| 27 |