|
| 1 | +import { resolve } from "node:path"; |
| 2 | +import { afterAll, beforeAll, describe, it } from "vitest"; |
| 3 | +import { runWranglerDev } from "../../shared/src/run-wrangler-long-lived"; |
| 4 | + |
| 5 | +describe("d1-sessions-api - getBookmark", () => { |
| 6 | + describe("with wrangler dev", () => { |
| 7 | + let ip: string, port: number, stop: (() => Promise<unknown>) | undefined; |
| 8 | + |
| 9 | + beforeAll(async () => { |
| 10 | + ({ ip, port, stop } = await runWranglerDev(resolve(__dirname, ".."), [ |
| 11 | + "--port=0", |
| 12 | + "--inspector-port=0", |
| 13 | + ])); |
| 14 | + }); |
| 15 | + |
| 16 | + afterAll(async () => { |
| 17 | + await stop?.(); |
| 18 | + }); |
| 19 | + |
| 20 | + it("should respond with bookmarks before and after a session query", async ({ |
| 21 | + expect, |
| 22 | + }) => { |
| 23 | + let response = await fetch(`http://${ip}:${port}`); |
| 24 | + let parsed = await response.json(); |
| 25 | + expect(response.status).toBe(200); |
| 26 | + expect(parsed).toMatchObject({ |
| 27 | + bookmarkBefore: expect.stringMatching(/\w{8}-\w{8}-\w{8}-\w{32}/), |
| 28 | + bookmarkAfter: expect.stringMatching(/\w{8}-\w{8}-\w{8}-\w{32}/), |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + it("should progress the bookmark after a write", async ({ expect }) => { |
| 33 | + let response = await fetch( |
| 34 | + `http://${ip}:${port}?q=${encodeURIComponent("create table if not exists users1(id text);")}` |
| 35 | + ); |
| 36 | + let parsed = (await response.json()) as { |
| 37 | + bookmarkAfter: string; |
| 38 | + bookmarkBefore: string; |
| 39 | + }; |
| 40 | + expect(response.status).toBe(200); |
| 41 | + expect(parsed).toMatchObject({ |
| 42 | + bookmarkBefore: expect.stringMatching(/\w{8}-\w{8}-\w{8}-\w{32}/), |
| 43 | + bookmarkAfter: expect.stringMatching(/\w{8}-\w{8}-\w{8}-\w{32}/), |
| 44 | + }); |
| 45 | + expect( |
| 46 | + parsed.bookmarkAfter > parsed.bookmarkBefore, |
| 47 | + `before[${parsed.bookmarkBefore}] !== after[${parsed.bookmarkAfter}]` |
| 48 | + ).toEqual(true); |
| 49 | + }); |
| 50 | + |
| 51 | + it("should maintain the latest bookmark after many queries", async ({ |
| 52 | + expect, |
| 53 | + }) => { |
| 54 | + let responses = []; |
| 55 | + |
| 56 | + for (let i = 0; i < 10; i++) { |
| 57 | + const resp = await fetch( |
| 58 | + `http://${ip}:${port}?q=${encodeURIComponent(`create table if not exists users${i}(id text);`)}` |
| 59 | + ); |
| 60 | + let parsed = (await resp.json()) as { |
| 61 | + bookmarkAfter: string; |
| 62 | + bookmarkBefore: string; |
| 63 | + }; |
| 64 | + expect(resp.status).toBe(200); |
| 65 | + responses.push(parsed); |
| 66 | + |
| 67 | + expect( |
| 68 | + parsed.bookmarkAfter > parsed.bookmarkBefore, |
| 69 | + `before[${parsed.bookmarkBefore}] !== after[${parsed.bookmarkAfter}]` |
| 70 | + ).toEqual(true); |
| 71 | + } |
| 72 | + |
| 73 | + const lastBookmark = responses.at(-1)?.bookmarkAfter; |
| 74 | + responses.slice(0, -1).forEach((parsed) => { |
| 75 | + expect( |
| 76 | + parsed.bookmarkAfter < lastBookmark!, |
| 77 | + `previous after[${parsed.bookmarkAfter}] !< lastBookmark[${lastBookmark}]` |
| 78 | + ).toEqual(true); |
| 79 | + }); |
| 80 | + }); |
| 81 | + }); |
| 82 | +}); |
0 commit comments