|
| 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`). |
| 7 | + * |
| 8 | + * That read pages `sys_member` with `{ organization_id, id: { $gt: after } }`, |
| 9 | + * `orderBy id asc`, a `limit`, and the columns `id` and `user_id`, resuming |
| 10 | + * after the last id of each full page. Its correctness rests on the driver, |
| 11 | + * and on nothing the resolver can check for itself: the order is the |
| 12 | + * database's collation, not JavaScript's code-point order (PostgreSQL with an |
| 13 | + * ICU `en-US` database sorts `a-c ab aB3 Ab9 AC b_1 B-2`, a `C.UTF-8` one |
| 14 | + * sorts `AC Ab9 B-2 a-c aB3 ab b_1`), so a resolver-side "each id is greater |
| 15 | + * than the last" check would refuse every correct page on such a database. |
| 16 | + * The keyset is collation-safe because `ORDER BY id` and `id > ?` use the |
| 17 | + * same comparison. This file pins exactly that, per dialect cell: the pages, |
| 18 | + * joined, are the same sequence as one unpaged ordered read, and that sequence |
| 19 | + * holds every row of the organization once and no row of another. |
| 20 | + * |
| 21 | + * The ids are mixed-case nanoid-alphabet values (letters of both cases, |
| 22 | + * digits, `-`, `_`), so a cell whose collation folds case or weighs |
| 23 | + * punctuation differently still walks a real non-code-point order. No two |
| 24 | + * ids are equal after case folding: a case-insensitive primary key (MySQL's |
| 25 | + * `utf8mb4_0900_ai_ci`) would refuse such a pair at insert, which is a |
| 26 | + * different fact from the one pinned here. |
| 27 | + * |
| 28 | + * Each driver package carries its own copy of this pin (driver-memory, |
| 29 | + * driver-sqlite-wasm, driver-turso, driver-mongodb): the drivers' shared |
| 30 | + * conformance cases live in `@objectstack/spec/data`, which this change does |
| 31 | + * not edit. |
| 32 | + */ |
| 33 | + |
| 34 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 35 | +import { SqlDriver } from './index.js'; |
| 36 | +import { DIALECT_CELLS, declareUnprovisionedCell, type DialectCell } from './live-dialect-matrix.testkit.js'; |
| 37 | + |
| 38 | +/** Issue-prefixed: the live cells share a database with every other suite in this package. */ |
| 39 | +const TABLE = 'os29398_member_keyset'; |
| 40 | +const ORG = 'org_walked'; |
| 41 | +const OTHER_ORG = 'org_other'; |
| 42 | + |
| 43 | +/** The organization walked: sixteen ids, no two equal after case folding. */ |
| 44 | +const MEMBER_IDS = [ |
| 45 | + 'AC', 'Ab9', 'B-2', 'a-c', 'aB3', 'ab', 'b_1', '0Zz', |
| 46 | + 'Z0', 'z_9', '_x', '-y', 'Mm', 'mN1', 'n', 'Q-q', |
| 47 | +] as const; |
| 48 | +/** Another organization's rows, interleaved with the walked ones in any collation. */ |
| 49 | +const OTHER_IDS = ['Aa', 'b', 'zZ'] as const; |
| 50 | + |
| 51 | +/** Page sizes: one row, a mid-run boundary, an exact multiple (16 = 4 x 4), the whole set. */ |
| 52 | +const PAGE_SIZES = [1, 3, 4, 16] as const; |
| 53 | + |
| 54 | +const READ_OPTIONS = { bypassTenantAudit: true } as const; |
| 55 | + |
| 56 | +const userIdOf = (id: string) => `user_${id}`; |
| 57 | + |
| 58 | +for (const cell of DIALECT_CELLS) { |
| 59 | + if (!cell.available) { |
| 60 | + declareUnprovisionedCell(cell, 'member keyset walk conformance'); |
| 61 | + continue; |
| 62 | + } |
| 63 | + declareWalk(cell); |
| 64 | +} |
| 65 | + |
| 66 | +function declareWalk(cell: DialectCell): void { |
| 67 | + describe(`SqlDriver — a keyset walk by id visits every member once, in the driver's order (#29398) (${cell.label})`, () => { |
| 68 | + let driver: SqlDriver; |
| 69 | + |
| 70 | + beforeAll(async () => { |
| 71 | + driver = new SqlDriver(cell.config()); |
| 72 | + await driver.execute(`drop table if exists ${TABLE}`).catch(() => {}); |
| 73 | + await driver.initObjects([ |
| 74 | + { name: TABLE, fields: { organization_id: { type: 'string' }, user_id: { type: 'string' } } } as any, |
| 75 | + ]); |
| 76 | + const rows = [ |
| 77 | + ...MEMBER_IDS.map((id) => ({ id, organization_id: ORG, user_id: userIdOf(id) })), |
| 78 | + ...OTHER_IDS.map((id) => ({ id, organization_id: OTHER_ORG, user_id: userIdOf(id) })), |
| 79 | + ]; |
| 80 | + for (const row of rows) await driver.create(TABLE, row, READ_OPTIONS); |
| 81 | + }); |
| 82 | + |
| 83 | + afterAll(async () => { |
| 84 | + await driver?.execute(`drop table if exists ${TABLE}`).catch(() => {}); |
| 85 | + await driver?.disconnect?.(); |
| 86 | + }); |
| 87 | + |
| 88 | + /** The completion read's loop: resume after the last id of each full page. */ |
| 89 | + const walk = async (pageSize: number) => { |
| 90 | + const visited: Array<Record<string, unknown>> = []; |
| 91 | + let after: unknown; |
| 92 | + let reads = 0; |
| 93 | + for (;;) { |
| 94 | + const where = after === undefined |
| 95 | + ? { organization_id: ORG } |
| 96 | + : { organization_id: ORG, id: { $gt: after } }; |
| 97 | + const page: Array<Record<string, unknown>> = await driver.find( |
| 98 | + TABLE, |
| 99 | + { where, orderBy: [{ field: 'id', order: 'asc' }], limit: pageSize, fields: ['id', 'user_id'] } as any, |
| 100 | + READ_OPTIONS, |
| 101 | + ); |
| 102 | + reads += 1; |
| 103 | + visited.push(...page); |
| 104 | + if (page.length < pageSize) break; |
| 105 | + after = page[page.length - 1]!.id; |
| 106 | + } |
| 107 | + return { visited, reads }; |
| 108 | + }; |
| 109 | + |
| 110 | + /** The driver's own order, read once without a page boundary to cross. */ |
| 111 | + const ordered = async () => { |
| 112 | + const rows: Array<Record<string, unknown>> = await driver.find( |
| 113 | + TABLE, |
| 114 | + { where: { organization_id: ORG }, orderBy: [{ field: 'id', order: 'asc' }], fields: ['id', 'user_id'] } as any, |
| 115 | + READ_OPTIONS, |
| 116 | + ); |
| 117 | + return rows.map((r) => String(r.id)); |
| 118 | + }; |
| 119 | + |
| 120 | + for (const pageSize of PAGE_SIZES) { |
| 121 | + it(`pages of ${pageSize}: every member once, no other organization's row, in one ordered read's sequence`, async () => { |
| 122 | + const { visited, reads } = await walk(pageSize); |
| 123 | + const ids = visited.map((r) => String(r.id)); |
| 124 | + |
| 125 | + expect(ids).toEqual(await ordered()); |
| 126 | + expect([...ids].sort()).toEqual([...MEMBER_IDS].sort()); |
| 127 | + expect(new Set(ids).size).toBe(MEMBER_IDS.length); |
| 128 | + for (const row of visited) expect(row.user_id).toBe(userIdOf(String(row.id))); |
| 129 | + expect(reads).toBe(Math.floor(MEMBER_IDS.length / pageSize) + 1); |
| 130 | + }); |
| 131 | + } |
| 132 | + }); |
| 133 | +} |
0 commit comments