|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * A keyset walk by `id` visits every row once, in the driver's own order |
| 5 | + * (#29398 item 1) — the query shape of the organization-membership completion |
| 6 | + * read in `@objectstack/core` (`readOrganizationMembersToCompletion`): pages |
| 7 | + * of `{ organization_id, id: { $gt: after } }`, `orderBy id asc`, a `limit`, |
| 8 | + * the columns `id` and `user_id`, resuming after each full page's last id. |
| 9 | + * |
| 10 | + * The resolver cannot check the order itself, because the order is the |
| 11 | + * driver's (a SQL database's collation, not JavaScript's code points); the |
| 12 | + * walk is correct exactly when the cursor and the order share one comparison. |
| 13 | + * This pins that for this driver: the pages, joined, are the sequence of one |
| 14 | + * unpaged ordered read, holding every row of the organization once and no row |
| 15 | + * of another. The ids are mixed-case nanoid-alphabet values, no two equal |
| 16 | + * after case folding. The same pin lives in driver-memory, driver-sql (per dialect cell), |
| 17 | + * driver-sqlite-wasm and driver-turso; the drivers' shared |
| 18 | + * conformance cases live in `@objectstack/spec/data`, which this change does |
| 19 | + * not edit. |
| 20 | + * |
| 21 | + * Like this package's other live suites it needs a real mongod and is opt-in |
| 22 | + * (`test-mongod.ts`): without one it is a named skip. |
| 23 | + */ |
| 24 | + |
| 25 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 26 | +import type { MongoMemoryServer } from 'mongodb-memory-server'; |
| 27 | +import { MongoDBDriver } from './mongodb-driver.js'; |
| 28 | +import { createTestMongod } from './test-mongod.js'; |
| 29 | + |
| 30 | +const TABLE = 'sys_member'; |
| 31 | +const ORG = 'org_walked'; |
| 32 | +const OTHER_ORG = 'org_other'; |
| 33 | + |
| 34 | +/** The organization walked: sixteen ids, no two equal after case folding. */ |
| 35 | +const MEMBER_IDS = [ |
| 36 | + 'AC', 'Ab9', 'B-2', 'a-c', 'aB3', 'ab', 'b_1', '0Zz', |
| 37 | + 'Z0', 'z_9', '_x', '-y', 'Mm', 'mN1', 'n', 'Q-q', |
| 38 | +] as const; |
| 39 | +/** Another organization's rows, interleaved with the walked ones. */ |
| 40 | +const OTHER_IDS = ['Aa', 'b', 'zZ'] as const; |
| 41 | + |
| 42 | +/** Page sizes: one row, a mid-run boundary, an exact multiple (16 = 4 x 4), the whole set. */ |
| 43 | +const PAGE_SIZES = [1, 3, 4, 16] as const; |
| 44 | + |
| 45 | +const userIdOf = (id: string) => `user_${id}`; |
| 46 | + |
| 47 | +const sharedMongod: MongoMemoryServer | undefined = await createTestMongod('member keyset walk conformance'); |
| 48 | + |
| 49 | +describe.skipIf(!sharedMongod)("driver-mongodb — a keyset walk by id visits every member once, in the driver's order (#29398)", () => { |
| 50 | + const mongod = sharedMongod as MongoMemoryServer; |
| 51 | + let driver: MongoDBDriver; |
| 52 | + |
| 53 | + beforeAll(async () => { |
| 54 | + driver = new MongoDBDriver({ url: mongod.getUri(), database: 'member_keyset_walk_conformance' }); |
| 55 | + await driver.connect(); |
| 56 | + const rows = [ |
| 57 | + ...MEMBER_IDS.map((id) => ({ id, organization_id: ORG, user_id: userIdOf(id) })), |
| 58 | + ...OTHER_IDS.map((id) => ({ id, organization_id: OTHER_ORG, user_id: userIdOf(id) })), |
| 59 | + ]; |
| 60 | + for (const row of rows) await driver.create(TABLE, row); |
| 61 | + }, 90_000); |
| 62 | + |
| 63 | + afterAll(async () => { |
| 64 | + if (driver) await driver.disconnect(); |
| 65 | + if (sharedMongod) await sharedMongod.stop(); |
| 66 | + }); |
| 67 | + |
| 68 | + /** The completion read's loop: resume after the last id of each full page. */ |
| 69 | + const walk = async (pageSize: number) => { |
| 70 | + const visited: Array<Record<string, unknown>> = []; |
| 71 | + let after: unknown; |
| 72 | + let reads = 0; |
| 73 | + for (;;) { |
| 74 | + const where = after === undefined |
| 75 | + ? { organization_id: ORG } |
| 76 | + : { organization_id: ORG, id: { $gt: after } }; |
| 77 | + const page: Array<Record<string, unknown>> = await driver.find( |
| 78 | + TABLE, |
| 79 | + { where, orderBy: [{ field: 'id', order: 'asc' }], limit: pageSize, fields: ['id', 'user_id'] } as any, |
| 80 | + ); |
| 81 | + reads += 1; |
| 82 | + // A driver that ignores the cursor would page forever: fail instead of hanging. |
| 83 | + expect(reads, 'the walk did not end; the cursor is not narrowing the read').toBeLessThanOrEqual(MEMBER_IDS.length + 1); |
| 84 | + visited.push(...page); |
| 85 | + if (page.length < pageSize) break; |
| 86 | + after = page[page.length - 1]!.id; |
| 87 | + } |
| 88 | + return { visited, reads }; |
| 89 | + }; |
| 90 | + |
| 91 | + /** The driver's own order, read once without a page boundary to cross. */ |
| 92 | + const ordered = async () => { |
| 93 | + const rows: Array<Record<string, unknown>> = await driver.find( |
| 94 | + TABLE, |
| 95 | + { where: { organization_id: ORG }, orderBy: [{ field: 'id', order: 'asc' }], fields: ['id', 'user_id'] } as any, |
| 96 | + ); |
| 97 | + return rows.map((r) => String(r.id)); |
| 98 | + }; |
| 99 | + |
| 100 | + for (const pageSize of PAGE_SIZES) { |
| 101 | + it(`pages of ${pageSize}: every member once, no other organization's row, in one ordered read's sequence`, async () => { |
| 102 | + const { visited, reads } = await walk(pageSize); |
| 103 | + const ids = visited.map((r) => String(r.id)); |
| 104 | + |
| 105 | + expect(ids).toEqual(await ordered()); |
| 106 | + expect([...ids].sort()).toEqual([...MEMBER_IDS].sort()); |
| 107 | + expect(new Set(ids).size).toBe(MEMBER_IDS.length); |
| 108 | + for (const row of visited) expect(row.user_id).toBe(userIdOf(String(row.id))); |
| 109 | + expect(reads).toBe(Math.floor(MEMBER_IDS.length / pageSize) + 1); |
| 110 | + }); |
| 111 | + } |
| 112 | +}); |
0 commit comments