Skip to content

Commit dde7b3a

Browse files
committed
test(drivers): pin the member keyset walk by id on memory, sqlite-wasm, turso and mongodb (WIP)
1 parent 7fcdbf3 commit dde7b3a

5 files changed

Lines changed: 431 additions & 0 deletions

File tree

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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-sql (per dialect cell),
17+
* driver-sqlite-wasm, driver-turso and driver-mongodb; the drivers' shared
18+
* conformance cases live in `@objectstack/spec/data`, which this change does
19+
* not edit.
20+
*/
21+
22+
import { describe, it, expect, beforeAll } from 'vitest';
23+
import { InMemoryDriver } from './memory-driver.js';
24+
25+
const TABLE = 'sys_member';
26+
const ORG = 'org_walked';
27+
const OTHER_ORG = 'org_other';
28+
29+
/** The organization walked: sixteen ids, no two equal after case folding. */
30+
const MEMBER_IDS = [
31+
'AC', 'Ab9', 'B-2', 'a-c', 'aB3', 'ab', 'b_1', '0Zz',
32+
'Z0', 'z_9', '_x', '-y', 'Mm', 'mN1', 'n', 'Q-q',
33+
] as const;
34+
/** Another organization's rows, interleaved with the walked ones. */
35+
const OTHER_IDS = ['Aa', 'b', 'zZ'] as const;
36+
37+
/** Page sizes: one row, a mid-run boundary, an exact multiple (16 = 4 x 4), the whole set. */
38+
const PAGE_SIZES = [1, 3, 4, 16] as const;
39+
40+
const userIdOf = (id: string) => `user_${id}`;
41+
42+
describe("InMemoryDriver — a keyset walk by id visits every member once, in the driver's order (#29398)", () => {
43+
let driver: InMemoryDriver;
44+
45+
beforeAll(async () => {
46+
driver = new InMemoryDriver({ persistence: false });
47+
await driver.connect();
48+
const rows = [
49+
...MEMBER_IDS.map((id) => ({ id, organization_id: ORG, user_id: userIdOf(id) })),
50+
...OTHER_IDS.map((id) => ({ id, organization_id: OTHER_ORG, user_id: userIdOf(id) })),
51+
];
52+
for (const row of rows) await driver.create(TABLE, row);
53+
});
54+
55+
/** The completion read's loop: resume after the last id of each full page. */
56+
const walk = async (pageSize: number) => {
57+
const visited: Array<Record<string, unknown>> = [];
58+
let after: unknown;
59+
let reads = 0;
60+
for (;;) {
61+
const where = after === undefined
62+
? { organization_id: ORG }
63+
: { organization_id: ORG, id: { $gt: after } };
64+
const page: Array<Record<string, unknown>> = await driver.find(
65+
TABLE,
66+
{ where, orderBy: [{ field: 'id', order: 'asc' }], limit: pageSize, fields: ['id', 'user_id'] } as any,
67+
);
68+
reads += 1;
69+
// A driver that ignores the cursor would page forever: fail instead of hanging.
70+
expect(reads, 'the walk did not end; the cursor is not narrowing the read').toBeLessThanOrEqual(MEMBER_IDS.length + 1);
71+
visited.push(...page);
72+
if (page.length < pageSize) break;
73+
after = page[page.length - 1]!.id;
74+
}
75+
return { visited, reads };
76+
};
77+
78+
/** The driver's own order, read once without a page boundary to cross. */
79+
const ordered = async () => {
80+
const rows: Array<Record<string, unknown>> = await driver.find(
81+
TABLE,
82+
{ where: { organization_id: ORG }, orderBy: [{ field: 'id', order: 'asc' }], fields: ['id', 'user_id'] } as any,
83+
);
84+
return rows.map((r) => String(r.id));
85+
};
86+
87+
for (const pageSize of PAGE_SIZES) {
88+
it(`pages of ${pageSize}: every member once, no other organization's row, in one ordered read's sequence`, async () => {
89+
const { visited, reads } = await walk(pageSize);
90+
const ids = visited.map((r) => String(r.id));
91+
92+
expect(ids).toEqual(await ordered());
93+
expect([...ids].sort()).toEqual([...MEMBER_IDS].sort());
94+
expect(new Set(ids).size).toBe(MEMBER_IDS.length);
95+
for (const row of visited) expect(row.user_id).toBe(userIdOf(String(row.id)));
96+
expect(reads).toBe(Math.floor(MEMBER_IDS.length / pageSize) + 1);
97+
});
98+
}
99+
});
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
});

‎packages/drivers/driver-sql/src/sql-driver-member-keyset-walk-conformance.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ function declareWalk(cell: DialectCell): void {
100100
READ_OPTIONS,
101101
);
102102
reads += 1;
103+
// A driver that ignores the cursor would page forever: fail instead of hanging.
104+
expect(reads, 'the walk did not end; the cursor is not narrowing the read').toBeLessThanOrEqual(MEMBER_IDS.length + 1);
103105
visited.push(...page);
104106
if (page.length < pageSize) break;
105107
after = page[page.length - 1]!.id;
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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-turso and driver-mongodb; the drivers' shared
18+
* conformance cases live in `@objectstack/spec/data`, which this change does
19+
* not edit.
20+
*/
21+
22+
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
23+
import { SqliteWasmDriver } from './index.js';
24+
25+
const TABLE = 'sys_member';
26+
const ORG = 'org_walked';
27+
const OTHER_ORG = 'org_other';
28+
29+
/** The organization walked: sixteen ids, no two equal after case folding. */
30+
const MEMBER_IDS = [
31+
'AC', 'Ab9', 'B-2', 'a-c', 'aB3', 'ab', 'b_1', '0Zz',
32+
'Z0', 'z_9', '_x', '-y', 'Mm', 'mN1', 'n', 'Q-q',
33+
] as const;
34+
/** Another organization's rows, interleaved with the walked ones. */
35+
const OTHER_IDS = ['Aa', 'b', 'zZ'] as const;
36+
37+
/** Page sizes: one row, a mid-run boundary, an exact multiple (16 = 4 x 4), the whole set. */
38+
const PAGE_SIZES = [1, 3, 4, 16] as const;
39+
40+
const READ_OPTIONS = { bypassTenantAudit: true } as const;
41+
42+
const userIdOf = (id: string) => `user_${id}`;
43+
44+
describe("SqliteWasmDriver — a keyset walk by id visits every member once, in the driver's order (#29398)", () => {
45+
let driver: SqliteWasmDriver;
46+
47+
beforeAll(async () => {
48+
driver = new SqliteWasmDriver({ filename: ':memory:' });
49+
await driver.initObjects([
50+
{ name: TABLE, fields: { organization_id: { type: 'string' }, user_id: { type: 'string' } } } as any,
51+
]);
52+
const rows = [
53+
...MEMBER_IDS.map((id) => ({ id, organization_id: ORG, user_id: userIdOf(id) })),
54+
...OTHER_IDS.map((id) => ({ id, organization_id: OTHER_ORG, user_id: userIdOf(id) })),
55+
];
56+
for (const row of rows) await driver.create(TABLE, row, READ_OPTIONS);
57+
});
58+
59+
afterAll(async () => {
60+
await driver?.disconnect();
61+
});
62+
63+
/** The completion read's loop: resume after the last id of each full page. */
64+
const walk = async (pageSize: number) => {
65+
const visited: Array<Record<string, unknown>> = [];
66+
let after: unknown;
67+
let reads = 0;
68+
for (;;) {
69+
const where = after === undefined
70+
? { organization_id: ORG }
71+
: { organization_id: ORG, id: { $gt: after } };
72+
const page: Array<Record<string, unknown>> = await driver.find(
73+
TABLE,
74+
{ where, orderBy: [{ field: 'id', order: 'asc' }], limit: pageSize, fields: ['id', 'user_id'] } as any,
75+
READ_OPTIONS,
76+
);
77+
reads += 1;
78+
// A driver that ignores the cursor would page forever: fail instead of hanging.
79+
expect(reads, 'the walk did not end; the cursor is not narrowing the read').toBeLessThanOrEqual(MEMBER_IDS.length + 1);
80+
visited.push(...page);
81+
if (page.length < pageSize) break;
82+
after = page[page.length - 1]!.id;
83+
}
84+
return { visited, reads };
85+
};
86+
87+
/** The driver's own order, read once without a page boundary to cross. */
88+
const ordered = async () => {
89+
const rows: Array<Record<string, unknown>> = await driver.find(
90+
TABLE,
91+
{ where: { organization_id: ORG }, orderBy: [{ field: 'id', order: 'asc' }], fields: ['id', 'user_id'] } as any,
92+
READ_OPTIONS,
93+
);
94+
return rows.map((r) => String(r.id));
95+
};
96+
97+
for (const pageSize of PAGE_SIZES) {
98+
it(`pages of ${pageSize}: every member once, no other organization's row, in one ordered read's sequence`, async () => {
99+
const { visited, reads } = await walk(pageSize);
100+
const ids = visited.map((r) => String(r.id));
101+
102+
expect(ids).toEqual(await ordered());
103+
expect([...ids].sort()).toEqual([...MEMBER_IDS].sort());
104+
expect(new Set(ids).size).toBe(MEMBER_IDS.length);
105+
for (const row of visited) expect(row.user_id).toBe(userIdOf(String(row.id)));
106+
expect(reads).toBe(Math.floor(MEMBER_IDS.length / pageSize) + 1);
107+
});
108+
}
109+
});

0 commit comments

Comments
 (0)