@terreno/syncdb 57.10.1 → 57.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client.d.ts +20 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +53 -1
- package/dist/client.js.map +1 -1
- package/dist/codegen/emitSdk.d.ts.map +1 -1
- package/dist/codegen/emitSdk.js +2 -0
- package/dist/codegen/emitSdk.js.map +1 -1
- package/dist/codegen/generate.test.js +2 -0
- package/dist/codegen/generate.test.js.map +1 -1
- package/dist/codegen/hookNames.d.ts +2 -1
- package/dist/codegen/hookNames.d.ts.map +1 -1
- package/dist/codegen/hookNames.js +2 -1
- package/dist/codegen/hookNames.js.map +1 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/query/localQuery.d.ts +22 -0
- package/dist/query/localQuery.d.ts.map +1 -0
- package/dist/query/localQuery.js +38 -0
- package/dist/query/localQuery.js.map +1 -0
- package/dist/query/queryWindows.d.ts +128 -0
- package/dist/query/queryWindows.d.ts.map +1 -0
- package/dist/query/queryWindows.edge.test.d.ts +2 -0
- package/dist/query/queryWindows.edge.test.d.ts.map +1 -0
- package/dist/query/queryWindows.edge.test.js +196 -0
- package/dist/query/queryWindows.edge.test.js.map +1 -0
- package/dist/query/queryWindows.js +621 -0
- package/dist/query/queryWindows.js.map +1 -0
- package/dist/query/queryWindows.test.d.ts +2 -0
- package/dist/query/queryWindows.test.d.ts.map +1 -0
- package/dist/query/queryWindows.test.js +565 -0
- package/dist/query/queryWindows.test.js.map +1 -0
- package/dist/query/rangePlan.d.ts +23 -0
- package/dist/query/rangePlan.d.ts.map +1 -0
- package/dist/query/rangePlan.js +20 -0
- package/dist/query/rangePlan.js.map +1 -0
- package/dist/query/sort.d.ts +13 -0
- package/dist/query/sort.d.ts.map +1 -0
- package/dist/query/sort.js +84 -0
- package/dist/query/sort.js.map +1 -0
- package/dist/query/where.d.ts +41 -0
- package/dist/query/where.d.ts.map +1 -0
- package/dist/query/where.js +243 -0
- package/dist/query/where.js.map +1 -0
- package/dist/query/where.test.d.ts +2 -0
- package/dist/query/where.test.d.ts.map +1 -0
- package/dist/query/where.test.js +130 -0
- package/dist/query/where.test.js.map +1 -0
- package/dist/react/collectionHooks.d.ts +6 -1
- package/dist/react/collectionHooks.d.ts.map +1 -1
- package/dist/react/collectionHooks.js +3 -1
- package/dist/react/collectionHooks.js.map +1 -1
- package/dist/react/hooks.d.ts +66 -2
- package/dist/react/hooks.d.ts.map +1 -1
- package/dist/react/hooks.js +91 -24
- package/dist/react/hooks.js.map +1 -1
- package/dist/react/windowHooks.test.d.ts +2 -0
- package/dist/react/windowHooks.test.d.ts.map +1 -0
- package/dist/react/windowHooks.test.js +132 -0
- package/dist/react/windowHooks.test.js.map +1 -0
- package/dist/storage/schema.d.ts.map +1 -1
- package/dist/storage/schema.js +13 -1
- package/dist/storage/schema.js.map +1 -1
- package/dist/storage/types.d.ts +5 -0
- package/dist/storage/types.d.ts.map +1 -1
- package/dist/storage/types.js +5 -0
- package/dist/storage/types.js.map +1 -1
- package/dist/sync/httpChannel.d.ts +40 -1
- package/dist/sync/httpChannel.d.ts.map +1 -1
- package/dist/sync/httpChannel.js +89 -0
- package/dist/sync/httpChannel.js.map +1 -1
- package/dist/sync/httpChannel.test.js +45 -1
- package/dist/sync/httpChannel.test.js.map +1 -1
- package/dist/types.d.ts +10 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/client.ts +70 -1
- package/src/codegen/emitSdk.ts +2 -0
- package/src/codegen/generate.test.ts +2 -0
- package/src/codegen/hookNames.ts +3 -1
- package/src/index.ts +18 -0
- package/src/query/localQuery.ts +60 -0
- package/src/query/queryWindows.edge.test.ts +250 -0
- package/src/query/queryWindows.test.ts +665 -0
- package/src/query/queryWindows.ts +848 -0
- package/src/query/rangePlan.ts +39 -0
- package/src/query/sort.ts +101 -0
- package/src/query/where.test.ts +155 -0
- package/src/query/where.ts +290 -0
- package/src/react/collectionHooks.ts +16 -2
- package/src/react/hooks.ts +184 -26
- package/src/react/windowHooks.test.tsx +172 -0
- package/src/storage/schema.ts +13 -0
- package/src/storage/types.ts +6 -0
- package/src/sync/httpChannel.test.ts +65 -1
- package/src/sync/httpChannel.ts +142 -0
- package/src/types.ts +11 -0
|
@@ -0,0 +1,665 @@
|
|
|
1
|
+
import {describe, expect, it} from "bun:test";
|
|
2
|
+
|
|
3
|
+
import {createSyncDb, type SyncDb} from "../client";
|
|
4
|
+
import {memoryPersisterFactory} from "../persisters/memoryPersister";
|
|
5
|
+
import {QUERY_WINDOWS_TABLE} from "../storage/types";
|
|
6
|
+
import {type HttpChannel, QueryFieldNotAllowedError} from "../sync/httpChannel";
|
|
7
|
+
import {createFakeTransport, type FakeTransport} from "../testing/fakeTransport";
|
|
8
|
+
import type {AuthProvider, ListResponse, SyncDelta} from "../types";
|
|
9
|
+
import {selectWindowView, type WindowQuery} from "./queryWindows";
|
|
10
|
+
import {planRange} from "./rangePlan";
|
|
11
|
+
import {compileWhere} from "./where";
|
|
12
|
+
|
|
13
|
+
interface Message {
|
|
14
|
+
_id: string;
|
|
15
|
+
_syncSeq?: number;
|
|
16
|
+
threadId: string;
|
|
17
|
+
pinned: boolean;
|
|
18
|
+
text: string;
|
|
19
|
+
created: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
let nameCounter = 0;
|
|
23
|
+
|
|
24
|
+
const auth: AuthProvider = {
|
|
25
|
+
getToken: async () => "token",
|
|
26
|
+
getUserId: async () => "u1",
|
|
27
|
+
onAuthChange: () => () => {},
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const message = (id: string, overrides: Partial<Message> = {}): Message => ({
|
|
31
|
+
_id: id,
|
|
32
|
+
_syncSeq: 1,
|
|
33
|
+
created: `2026-01-0${id.replace(/\D/g, "")}T00:00:00.000Z`,
|
|
34
|
+
pinned: false,
|
|
35
|
+
text: `text ${id}`,
|
|
36
|
+
threadId: "a",
|
|
37
|
+
...overrides,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
/** Server rows; list requests are answered by filtering these with the same matcher. */
|
|
41
|
+
const DB: Message[] = [
|
|
42
|
+
message("m1", {threadId: "a"}),
|
|
43
|
+
message("m2", {pinned: true, threadId: "a"}),
|
|
44
|
+
message("m3", {pinned: true, threadId: "a"}),
|
|
45
|
+
message("m4", {pinned: true, threadId: "b"}),
|
|
46
|
+
message("m5", {threadId: "b"}),
|
|
47
|
+
];
|
|
48
|
+
|
|
49
|
+
interface ListCall {
|
|
50
|
+
path: string;
|
|
51
|
+
params: Record<string, unknown>;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
interface Harness {
|
|
55
|
+
client: SyncDb;
|
|
56
|
+
transport: FakeTransport;
|
|
57
|
+
listCalls: ListCall[];
|
|
58
|
+
snapshotStreams: string[];
|
|
59
|
+
entityFetches: string[][];
|
|
60
|
+
rows: Message[];
|
|
61
|
+
/** Hold list responses until released (concurrency tests). */
|
|
62
|
+
gate?: Promise<void>;
|
|
63
|
+
setGate: (gate: Promise<void> | undefined) => void;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Mirrors modelRouter: clamps limit to maxLimit and echoes the applied limit. */
|
|
67
|
+
const respond = (
|
|
68
|
+
rows: Message[],
|
|
69
|
+
params: Record<string, unknown>,
|
|
70
|
+
maxLimit = 500
|
|
71
|
+
): ListResponse => {
|
|
72
|
+
const {limit: requested, page, sort, ...where} = params;
|
|
73
|
+
const limit = Math.min(Number(requested), maxLimit);
|
|
74
|
+
const {match} = compileWhere(where);
|
|
75
|
+
let matched = rows.filter((row) => match?.(row));
|
|
76
|
+
if (sort === "-created") {
|
|
77
|
+
matched = [...matched].sort((a, b) => (a.created < b.created ? 1 : -1));
|
|
78
|
+
}
|
|
79
|
+
const size = Number(limit);
|
|
80
|
+
const start = page ? (Number(page) - 1) * size : 0;
|
|
81
|
+
const data = matched.slice(start, start + size);
|
|
82
|
+
return {data, limit: size, more: start + size < matched.length, total: matched.length};
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const setup = async ({
|
|
86
|
+
rows = DB,
|
|
87
|
+
maxLimit,
|
|
88
|
+
}: {
|
|
89
|
+
rows?: Message[];
|
|
90
|
+
maxLimit?: number;
|
|
91
|
+
} = {}): Promise<Harness> => {
|
|
92
|
+
nameCounter += 1;
|
|
93
|
+
const transport = createFakeTransport();
|
|
94
|
+
const harness = {
|
|
95
|
+
entityFetches: [] as string[][],
|
|
96
|
+
listCalls: [] as ListCall[],
|
|
97
|
+
rows: rows.map((row) => ({...row})),
|
|
98
|
+
snapshotStreams: [] as string[],
|
|
99
|
+
} as unknown as Harness;
|
|
100
|
+
harness.setGate = (gate): void => {
|
|
101
|
+
harness.gate = gate;
|
|
102
|
+
};
|
|
103
|
+
const channel: HttpChannel = {
|
|
104
|
+
fetchEntities: async ({ids}) => {
|
|
105
|
+
harness.entityFetches.push(ids);
|
|
106
|
+
return {
|
|
107
|
+
entities: harness.rows
|
|
108
|
+
.filter((row) => ids.includes(row._id))
|
|
109
|
+
.map((row) => ({data: row, deleted: false, id: row._id, seq: 7})),
|
|
110
|
+
};
|
|
111
|
+
},
|
|
112
|
+
fetchKeyMaterial: async () => "",
|
|
113
|
+
fetchList: async ({path, params}) => {
|
|
114
|
+
harness.listCalls.push({params, path});
|
|
115
|
+
await (harness.gate ?? Promise.resolve());
|
|
116
|
+
return respond(harness.rows, params, maxLimit);
|
|
117
|
+
},
|
|
118
|
+
fetchSnapshotPage: async ({stream}) => {
|
|
119
|
+
harness.snapshotStreams.push(stream);
|
|
120
|
+
return {
|
|
121
|
+
cursor: 0,
|
|
122
|
+
entities: [],
|
|
123
|
+
frontierSeq: 0,
|
|
124
|
+
hasMore: false,
|
|
125
|
+
oldestRetainedSeq: 0,
|
|
126
|
+
stream,
|
|
127
|
+
};
|
|
128
|
+
},
|
|
129
|
+
fetchStreams: async () => [{collection: "messages", stream: "messages|owner:u1"}],
|
|
130
|
+
sendMutation: async () => {
|
|
131
|
+
throw new Error("not expected");
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
const client = createSyncDb({
|
|
135
|
+
authProvider: auth,
|
|
136
|
+
collections: ["messages"],
|
|
137
|
+
httpChannel: channel,
|
|
138
|
+
name: `query-windows-test-${nameCounter}`,
|
|
139
|
+
persisterFactory: memoryPersisterFactory,
|
|
140
|
+
queryCollections: [{collection: "messages", path: "/api/messages"}],
|
|
141
|
+
reconcileIntervalMs: 0,
|
|
142
|
+
transport,
|
|
143
|
+
});
|
|
144
|
+
await client.start();
|
|
145
|
+
await client.reconcile();
|
|
146
|
+
harness.client = client;
|
|
147
|
+
harness.transport = transport;
|
|
148
|
+
return harness;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
const THREAD_A: WindowQuery = {collection: "messages", where: {threadId: "a"}};
|
|
152
|
+
const PINNED: WindowQuery = {collection: "messages", where: {pinned: true}};
|
|
153
|
+
|
|
154
|
+
const view = (client: SyncDb, query: WindowQuery): string[] =>
|
|
155
|
+
selectWindowView<Message>({
|
|
156
|
+
query,
|
|
157
|
+
store: client.store,
|
|
158
|
+
window: client.queryWindows.getWindow(query),
|
|
159
|
+
}).map((entity) => entity.id);
|
|
160
|
+
|
|
161
|
+
const delta = (data: Message, seq: number): SyncDelta => ({
|
|
162
|
+
collection: "messages",
|
|
163
|
+
data,
|
|
164
|
+
id: data._id,
|
|
165
|
+
method: "update",
|
|
166
|
+
seq,
|
|
167
|
+
stream: "messages|owner:u1",
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
const flush = async (): Promise<void> => {
|
|
171
|
+
await new Promise((resolve) => setTimeout(resolve, 5));
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
describe("query windows", () => {
|
|
175
|
+
it("keeps overlapping windows as separate views over one copy of each row", async () => {
|
|
176
|
+
const {client} = await setup();
|
|
177
|
+
await client.queryWindows.fetchWindow({query: THREAD_A});
|
|
178
|
+
await client.queryWindows.fetchWindow({query: PINNED});
|
|
179
|
+
|
|
180
|
+
expect(view(client, THREAD_A)).toEqual(["m1", "m2", "m3"]);
|
|
181
|
+
expect(view(client, PINNED)).toEqual(["m2", "m3", "m4"]);
|
|
182
|
+
// m2 and m3 are in both windows but stored once.
|
|
183
|
+
expect(Object.keys(client.store.raw.getTable("messages")).sort()).toEqual([
|
|
184
|
+
"m1",
|
|
185
|
+
"m2",
|
|
186
|
+
"m3",
|
|
187
|
+
"m4",
|
|
188
|
+
]);
|
|
189
|
+
await client.stop();
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it("applies a delta for an overlapping row once and both views see it", async () => {
|
|
193
|
+
const {client, transport} = await setup();
|
|
194
|
+
await client.queryWindows.fetchWindow({query: THREAD_A});
|
|
195
|
+
await client.queryWindows.fetchWindow({query: PINNED});
|
|
196
|
+
|
|
197
|
+
let writes = 0;
|
|
198
|
+
const listenerId = client.store.raw.addRowListener("messages", "m2", () => {
|
|
199
|
+
writes += 1;
|
|
200
|
+
});
|
|
201
|
+
transport.deliverDelta(delta(message("m2", {pinned: true, text: "edited"}), 2));
|
|
202
|
+
|
|
203
|
+
expect(writes).toBe(1);
|
|
204
|
+
const read = (query: WindowQuery): string | undefined =>
|
|
205
|
+
selectWindowView<Message>({
|
|
206
|
+
query,
|
|
207
|
+
store: client.store,
|
|
208
|
+
window: client.queryWindows.getWindow(query),
|
|
209
|
+
}).find((entity) => entity.id === "m2")?.data.text;
|
|
210
|
+
expect(read(THREAD_A)).toBe("edited");
|
|
211
|
+
expect(read(PINNED)).toBe("edited");
|
|
212
|
+
client.store.raw.delListener(listenerId);
|
|
213
|
+
await client.stop();
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it("moves rows between views as their fields change, without leaking across filters", async () => {
|
|
217
|
+
const {client, transport} = await setup();
|
|
218
|
+
await client.queryWindows.fetchWindow({query: THREAD_A});
|
|
219
|
+
await client.queryWindows.fetchWindow({query: PINNED});
|
|
220
|
+
|
|
221
|
+
// m3 is unpinned: it leaves PINNED but stays in THREAD_A.
|
|
222
|
+
transport.deliverDelta(delta(message("m3", {pinned: false}), 2));
|
|
223
|
+
expect(view(client, PINNED)).toEqual(["m2", "m4"]);
|
|
224
|
+
expect(view(client, THREAD_A)).toEqual(["m1", "m2", "m3"]);
|
|
225
|
+
|
|
226
|
+
// A brand-new thread-a message arrives live: THREAD_A shows it, PINNED does not.
|
|
227
|
+
transport.deliverDelta(delta(message("m6", {threadId: "a"}), 3));
|
|
228
|
+
expect(view(client, THREAD_A)).toEqual(["m1", "m2", "m3", "m6"]);
|
|
229
|
+
expect(view(client, PINNED)).toEqual(["m2", "m4"]);
|
|
230
|
+
await client.stop();
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
it("sends where/sort/limit as list params and appends pages", async () => {
|
|
234
|
+
const harness = await setup();
|
|
235
|
+
const query: WindowQuery = {
|
|
236
|
+
collection: "messages",
|
|
237
|
+
pageSize: 2,
|
|
238
|
+
sort: "-created",
|
|
239
|
+
where: {threadId: {$in: ["a", "b"]}},
|
|
240
|
+
};
|
|
241
|
+
const first = await harness.client.queryWindows.fetchWindow({query});
|
|
242
|
+
expect(harness.listCalls[0]).toEqual({
|
|
243
|
+
params: {limit: 2, page: 1, sort: "-created", threadId: {$in: ["a", "b"]}},
|
|
244
|
+
path: "/api/messages",
|
|
245
|
+
});
|
|
246
|
+
expect(first.ids).toEqual(["m5", "m4"]);
|
|
247
|
+
expect(first.hasMore).toBe(true);
|
|
248
|
+
expect(first.total).toBe(5);
|
|
249
|
+
|
|
250
|
+
const second = await harness.client.queryWindows.fetchWindow({nextPage: true, query});
|
|
251
|
+
expect(harness.listCalls[1].params.page).toBe(2);
|
|
252
|
+
expect(second.ids).toEqual(["m5", "m4", "m3", "m2"]);
|
|
253
|
+
expect(second.pages).toBe(2);
|
|
254
|
+
|
|
255
|
+
// A refetch reloads every loaded page in one request.
|
|
256
|
+
await harness.client.queryWindows.fetchWindow({query});
|
|
257
|
+
expect(harness.listCalls[2].params).toEqual({
|
|
258
|
+
limit: 4,
|
|
259
|
+
page: 1,
|
|
260
|
+
sort: "-created",
|
|
261
|
+
threadId: {$in: ["a", "b"]},
|
|
262
|
+
});
|
|
263
|
+
await harness.client.stop();
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
it("only admits live matches that sort inside the loaded range", async () => {
|
|
267
|
+
const {client, transport} = await setup();
|
|
268
|
+
const query: WindowQuery = {
|
|
269
|
+
collection: "messages",
|
|
270
|
+
pageSize: 2,
|
|
271
|
+
sort: "-created",
|
|
272
|
+
where: {threadId: "a"},
|
|
273
|
+
};
|
|
274
|
+
await client.queryWindows.fetchWindow({query});
|
|
275
|
+
expect(view(client, query)).toEqual(["m3", "m2"]);
|
|
276
|
+
|
|
277
|
+
// Newer than the loaded range → shown at the top.
|
|
278
|
+
transport.deliverDelta(delta(message("m9", {threadId: "a"}), 2));
|
|
279
|
+
// Older than the last loaded row while more pages exist → left for the next page.
|
|
280
|
+
transport.deliverDelta(delta(message("m0", {threadId: "a"}), 3));
|
|
281
|
+
expect(view(client, query)).toEqual(["m9", "m3", "m2"]);
|
|
282
|
+
await client.stop();
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it("fetches canonical seq from /sync/entities when list rows omit _syncSeq", async () => {
|
|
286
|
+
const harness = await setup({rows: DB.map(({_syncSeq, ...rest}) => rest as Message)});
|
|
287
|
+
await harness.client.queryWindows.fetchWindow({query: THREAD_A});
|
|
288
|
+
expect(harness.entityFetches).toEqual([["m1", "m2", "m3"]]);
|
|
289
|
+
expect(harness.client.store.getEntity({collection: "messages", id: "m1"})?.seq).toBe(7);
|
|
290
|
+
await harness.client.stop();
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
it("never overwrites an optimistic row or one a newer delta wrote", async () => {
|
|
294
|
+
const {client, transport} = await setup();
|
|
295
|
+
await client.queryWindows.fetchWindow({query: THREAD_A});
|
|
296
|
+
transport.deliverDelta(delta(message("m1", {text: "newer"}), 5));
|
|
297
|
+
// Keep the mutation queued (unacked) so m2 stays pending-protected.
|
|
298
|
+
client.goOffline();
|
|
299
|
+
client.mutate({collection: "messages", data: {text: "mine"}, id: "m2", operation: "update"});
|
|
300
|
+
await client.queryWindows.fetchWindow({query: THREAD_A});
|
|
301
|
+
const data = (rowId: string): Message | undefined =>
|
|
302
|
+
client.store.getEntity<Message>({collection: "messages", id: rowId})?.data;
|
|
303
|
+
expect(data("m1")?.text).toBe("newer");
|
|
304
|
+
expect(data("m2")?.text).toBe("mine");
|
|
305
|
+
expect(view(client, THREAD_A)).toEqual(["m1", "m2", "m3"]);
|
|
306
|
+
await client.stop();
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
it("skips snapshot paging for query collections", async () => {
|
|
310
|
+
const {client, snapshotStreams, transport} = await setup();
|
|
311
|
+
transport.confirmSubscribed({collection: "messages", streams: ["messages|owner:u1"]});
|
|
312
|
+
await client.reconcile();
|
|
313
|
+
await flush();
|
|
314
|
+
expect(snapshotStreams).toEqual([]);
|
|
315
|
+
await client.stop();
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
it("shares one request between concurrent retains and fetches once per session", async () => {
|
|
319
|
+
const harness = await setup();
|
|
320
|
+
let release = (): void => {};
|
|
321
|
+
harness.setGate(
|
|
322
|
+
new Promise<void>((resolve) => {
|
|
323
|
+
release = resolve;
|
|
324
|
+
})
|
|
325
|
+
);
|
|
326
|
+
const releaseA = harness.client.queryWindows.retain(THREAD_A);
|
|
327
|
+
const releaseB = harness.client.queryWindows.retain({...THREAD_A, where: {threadId: "a"}});
|
|
328
|
+
expect(harness.listCalls).toHaveLength(1);
|
|
329
|
+
expect(harness.client.queryWindows.getWindow(THREAD_A)?.status).toBe("loading");
|
|
330
|
+
release();
|
|
331
|
+
harness.setGate(undefined);
|
|
332
|
+
await flush();
|
|
333
|
+
expect(harness.client.queryWindows.getWindow(THREAD_A)?.status).toBe("idle");
|
|
334
|
+
|
|
335
|
+
releaseA();
|
|
336
|
+
releaseB();
|
|
337
|
+
harness.client.queryWindows.retain(THREAD_A)();
|
|
338
|
+
expect(harness.listCalls).toHaveLength(1);
|
|
339
|
+
await harness.client.stop();
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
it("refetches retained windows on reconnect", async () => {
|
|
343
|
+
const harness = await setup();
|
|
344
|
+
const release = harness.client.queryWindows.retain(THREAD_A);
|
|
345
|
+
await flush();
|
|
346
|
+
expect(harness.listCalls).toHaveLength(1);
|
|
347
|
+
harness.transport.setConnected(false);
|
|
348
|
+
harness.transport.setConnected(true);
|
|
349
|
+
await flush();
|
|
350
|
+
expect(harness.listCalls).toHaveLength(2);
|
|
351
|
+
release();
|
|
352
|
+
await harness.client.stop();
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
it("refetches off-screen windows on their next retain after a reconnect", async () => {
|
|
356
|
+
const harness = await setup();
|
|
357
|
+
harness.client.queryWindows.retain(THREAD_A)();
|
|
358
|
+
await flush();
|
|
359
|
+
expect(harness.listCalls).toHaveLength(1);
|
|
360
|
+
harness.client.queryWindows.retain(THREAD_A)();
|
|
361
|
+
await flush();
|
|
362
|
+
expect(harness.listCalls).toHaveLength(1);
|
|
363
|
+
|
|
364
|
+
harness.transport.setConnected(false);
|
|
365
|
+
harness.transport.setConnected(true);
|
|
366
|
+
await flush();
|
|
367
|
+
// Not retained during the reconnect, so nothing refetched yet…
|
|
368
|
+
expect(harness.listCalls).toHaveLength(1);
|
|
369
|
+
// …but it is stale now, so the next mount refetches it.
|
|
370
|
+
const release = harness.client.queryWindows.retain(THREAD_A);
|
|
371
|
+
await flush();
|
|
372
|
+
expect(harness.listCalls).toHaveLength(2);
|
|
373
|
+
release();
|
|
374
|
+
await harness.client.stop();
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
it("discards a fetch that resolves after stop()", async () => {
|
|
378
|
+
const harness = await setup();
|
|
379
|
+
let release = (): void => {};
|
|
380
|
+
harness.setGate(
|
|
381
|
+
new Promise<void>((resolve) => {
|
|
382
|
+
release = resolve;
|
|
383
|
+
})
|
|
384
|
+
);
|
|
385
|
+
const pending = harness.client.queryWindows.fetchWindow({query: THREAD_A});
|
|
386
|
+
await harness.client.stop();
|
|
387
|
+
release();
|
|
388
|
+
await pending;
|
|
389
|
+
expect(harness.client.store.raw.getTable("messages")).toEqual({});
|
|
390
|
+
expect(harness.client.store.raw.getTable(QUERY_WINDOWS_TABLE)).toEqual({});
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
it("records an error status when the list request fails", async () => {
|
|
394
|
+
const harness = await setup();
|
|
395
|
+
const query: WindowQuery = {collection: "messages", where: {threadId: "a"}};
|
|
396
|
+
const failing = harness.client.queryWindows;
|
|
397
|
+
harness.listCalls.length = 0;
|
|
398
|
+
harness.rows = undefined as unknown as Message[];
|
|
399
|
+
await expect(failing.fetchWindow({query})).rejects.toThrow();
|
|
400
|
+
expect(failing.getWindow(query)?.status).toBe("error");
|
|
401
|
+
await harness.client.stop();
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
it("pipelines a refetch that exceeds the route's maxLimit and warns", async () => {
|
|
405
|
+
const harness = await setup({maxLimit: 3});
|
|
406
|
+
const warnings: string[] = [];
|
|
407
|
+
const originalWarn = console.warn;
|
|
408
|
+
console.warn = (message: string): void => {
|
|
409
|
+
warnings.push(message);
|
|
410
|
+
};
|
|
411
|
+
try {
|
|
412
|
+
const query: WindowQuery = {collection: "messages", pageSize: 2, sort: "-created"};
|
|
413
|
+
await harness.client.queryWindows.fetchWindow({query});
|
|
414
|
+
await harness.client.queryWindows.fetchWindow({nextPage: true, query});
|
|
415
|
+
const loaded = await harness.client.queryWindows.fetchWindow({nextPage: true, query});
|
|
416
|
+
expect(loaded.ids).toEqual(["m5", "m4", "m3", "m2", "m1"]);
|
|
417
|
+
expect(warnings).toEqual([]);
|
|
418
|
+
|
|
419
|
+
harness.listCalls.length = 0;
|
|
420
|
+
const refetched = await harness.client.queryWindows.fetchWindow({query});
|
|
421
|
+
// 3 pages × 2 = 6 rows: the first request learns maxLimit 3, then 2 pipelined pages of 3.
|
|
422
|
+
expect(harness.listCalls.map((call) => [call.params.limit, call.params.page])).toEqual([
|
|
423
|
+
[6, 1],
|
|
424
|
+
[3, 1],
|
|
425
|
+
[3, 2],
|
|
426
|
+
]);
|
|
427
|
+
expect(refetched.ids).toEqual(["m5", "m4", "m3", "m2", "m1"]);
|
|
428
|
+
expect(refetched.pages).toBe(3);
|
|
429
|
+
expect(warnings).toHaveLength(1);
|
|
430
|
+
expect(warnings[0]).toContain("pipelining 2 requests of 3");
|
|
431
|
+
|
|
432
|
+
// The learned cap is reused: the next refetch pipelines immediately.
|
|
433
|
+
harness.listCalls.length = 0;
|
|
434
|
+
await harness.client.queryWindows.fetchWindow({query});
|
|
435
|
+
expect(harness.listCalls.map((call) => [call.params.limit, call.params.page])).toEqual([
|
|
436
|
+
[3, 1],
|
|
437
|
+
[3, 2],
|
|
438
|
+
]);
|
|
439
|
+
} finally {
|
|
440
|
+
console.warn = originalWarn;
|
|
441
|
+
await harness.client.stop();
|
|
442
|
+
}
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
it("keeps page offsets exact when pageSize exceeds maxLimit", async () => {
|
|
446
|
+
const harness = await setup({maxLimit: 2});
|
|
447
|
+
const originalWarn = console.warn;
|
|
448
|
+
console.warn = (): void => {};
|
|
449
|
+
try {
|
|
450
|
+
const query: WindowQuery = {collection: "messages", pageSize: 4, sort: "-created"};
|
|
451
|
+
const first = await harness.client.queryWindows.fetchWindow({query});
|
|
452
|
+
expect(first.ids).toEqual(["m5", "m4", "m3", "m2"]);
|
|
453
|
+
const second = await harness.client.queryWindows.fetchWindow({nextPage: true, query});
|
|
454
|
+
expect(second.ids).toEqual(["m5", "m4", "m3", "m2", "m1"]);
|
|
455
|
+
expect(second.hasMore).toBe(false);
|
|
456
|
+
} finally {
|
|
457
|
+
console.warn = originalWarn;
|
|
458
|
+
await harness.client.stop();
|
|
459
|
+
}
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
it("surfaces a missing queryFields entry loudly with its error code", async () => {
|
|
463
|
+
const harness = await setup();
|
|
464
|
+
const errors: string[] = [];
|
|
465
|
+
const originalError = console.error;
|
|
466
|
+
console.error = (message: string): void => {
|
|
467
|
+
errors.push(message);
|
|
468
|
+
};
|
|
469
|
+
const channelError = new QueryFieldNotAllowedError({
|
|
470
|
+
allowedQueryFields: ["threadId"],
|
|
471
|
+
field: "text",
|
|
472
|
+
model: "Message",
|
|
473
|
+
path: "/api/messages",
|
|
474
|
+
});
|
|
475
|
+
harness.rows = new Proxy([], {
|
|
476
|
+
get: (): never => {
|
|
477
|
+
throw channelError;
|
|
478
|
+
},
|
|
479
|
+
}) as unknown as Message[];
|
|
480
|
+
try {
|
|
481
|
+
const query: WindowQuery = {collection: "messages", where: {text: "x"}};
|
|
482
|
+
await expect(harness.client.queryWindows.fetchWindow({query})).rejects.toBe(channelError);
|
|
483
|
+
const state = harness.client.queryWindows.getWindow(query);
|
|
484
|
+
expect(state?.status).toBe("error");
|
|
485
|
+
expect(state?.errorCode).toBe("query-param-not-allowed");
|
|
486
|
+
expect(state?.error).toContain('Add "text" to queryFields');
|
|
487
|
+
expect(errors).toHaveLength(1);
|
|
488
|
+
expect(errors[0]).toContain("[syncdb]");
|
|
489
|
+
} finally {
|
|
490
|
+
console.error = originalError;
|
|
491
|
+
await harness.client.stop();
|
|
492
|
+
}
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
it("keeps snapshot sync for fullSync query collections", async () => {
|
|
496
|
+
nameCounter += 1;
|
|
497
|
+
const snapshotStreams: string[] = [];
|
|
498
|
+
const transport = createFakeTransport();
|
|
499
|
+
const client = createSyncDb({
|
|
500
|
+
authProvider: auth,
|
|
501
|
+
collections: ["messages"],
|
|
502
|
+
httpChannel: {
|
|
503
|
+
fetchEntities: async () => ({entities: []}),
|
|
504
|
+
fetchKeyMaterial: async () => "",
|
|
505
|
+
fetchList: async ({params}) => respond(DB, params),
|
|
506
|
+
fetchSnapshotPage: async ({stream}) => {
|
|
507
|
+
snapshotStreams.push(stream);
|
|
508
|
+
return {
|
|
509
|
+
cursor: 0,
|
|
510
|
+
entities: [],
|
|
511
|
+
frontierSeq: 0,
|
|
512
|
+
hasMore: false,
|
|
513
|
+
oldestRetainedSeq: 0,
|
|
514
|
+
stream,
|
|
515
|
+
};
|
|
516
|
+
},
|
|
517
|
+
fetchStreams: async () => [{collection: "messages", stream: "messages|owner:u1"}],
|
|
518
|
+
sendMutation: async () => {
|
|
519
|
+
throw new Error("not expected");
|
|
520
|
+
},
|
|
521
|
+
},
|
|
522
|
+
name: `query-windows-full-${nameCounter}`,
|
|
523
|
+
persisterFactory: memoryPersisterFactory,
|
|
524
|
+
queryCollections: [{collection: "messages", fullSync: true}],
|
|
525
|
+
reconcileIntervalMs: 0,
|
|
526
|
+
transport,
|
|
527
|
+
});
|
|
528
|
+
await client.start();
|
|
529
|
+
await client.reconcile();
|
|
530
|
+
expect(snapshotStreams).toContain("messages|owner:u1");
|
|
531
|
+
const window = await client.queryWindows.fetchWindow({query: PINNED});
|
|
532
|
+
expect(window.ids).toEqual(["m2", "m3", "m4"]);
|
|
533
|
+
await client.stop();
|
|
534
|
+
});
|
|
535
|
+
|
|
536
|
+
it("does not resurrect a cached row the server stopped returning", async () => {
|
|
537
|
+
const harness = await setup();
|
|
538
|
+
const {client, transport} = harness;
|
|
539
|
+
await client.queryWindows.fetchWindow({query: THREAD_A});
|
|
540
|
+
expect(view(client, THREAD_A)).toEqual(["m1", "m2", "m3"]);
|
|
541
|
+
|
|
542
|
+
// Deleted on the server while this device missed the delta (offline).
|
|
543
|
+
harness.rows = harness.rows.filter((row) => row._id !== "m1");
|
|
544
|
+
await client.queryWindows.fetchWindow({query: THREAD_A});
|
|
545
|
+
expect(client.store.getEntity({collection: "messages", id: "m1"})).toBeDefined();
|
|
546
|
+
expect(view(client, THREAD_A)).toEqual(["m2", "m3"]);
|
|
547
|
+
|
|
548
|
+
// A newer delta for the row proves it changed, so live matching may admit it again.
|
|
549
|
+
transport.deliverDelta(delta(message("m1", {text: "restored"}), 9));
|
|
550
|
+
// Unsorted windows list live matches after the server members.
|
|
551
|
+
expect(view(client, THREAD_A)).toEqual(["m2", "m3", "m1"]);
|
|
552
|
+
await client.stop();
|
|
553
|
+
});
|
|
554
|
+
|
|
555
|
+
it("keeps a same-seq row written by sync instead of the list row", async () => {
|
|
556
|
+
const {client, transport} = await setup();
|
|
557
|
+
transport.deliverDelta(delta(message("m1", {text: "sync shape"}), 1));
|
|
558
|
+
await client.queryWindows.fetchWindow({query: THREAD_A});
|
|
559
|
+
expect(client.store.getEntity<Message>({collection: "messages", id: "m1"})?.data.text).toBe(
|
|
560
|
+
"sync shape"
|
|
561
|
+
);
|
|
562
|
+
await client.stop();
|
|
563
|
+
});
|
|
564
|
+
|
|
565
|
+
it("runs a refetch and a next-page load for one window one at a time", async () => {
|
|
566
|
+
const harness = await setup();
|
|
567
|
+
const query: WindowQuery = {collection: "messages", pageSize: 2, sort: "-created"};
|
|
568
|
+
await harness.client.queryWindows.fetchWindow({query});
|
|
569
|
+
let release = (): void => {};
|
|
570
|
+
harness.setGate(
|
|
571
|
+
new Promise<void>((resolve) => {
|
|
572
|
+
release = resolve;
|
|
573
|
+
})
|
|
574
|
+
);
|
|
575
|
+
const refetch = harness.client.queryWindows.fetchWindow({query});
|
|
576
|
+
const nextPage = harness.client.queryWindows.fetchWindow({nextPage: true, query});
|
|
577
|
+
expect(harness.listCalls).toHaveLength(2);
|
|
578
|
+
release();
|
|
579
|
+
harness.setGate(undefined);
|
|
580
|
+
await Promise.all([refetch, nextPage]);
|
|
581
|
+
const state = harness.client.queryWindows.getWindow(query);
|
|
582
|
+
expect(state?.ids).toEqual(["m5", "m4", "m3", "m2"]);
|
|
583
|
+
expect(state?.pages).toBe(2);
|
|
584
|
+
expect(state?.status).toBe("idle");
|
|
585
|
+
await harness.client.stop();
|
|
586
|
+
});
|
|
587
|
+
|
|
588
|
+
it("returns an empty window for an empty top-level $in without a request", async () => {
|
|
589
|
+
const harness = await setup();
|
|
590
|
+
const query: WindowQuery = {collection: "messages", where: {threadId: {$in: []}}};
|
|
591
|
+
const state = await harness.client.queryWindows.fetchWindow({query});
|
|
592
|
+
expect(harness.listCalls).toHaveLength(0);
|
|
593
|
+
expect(state.ids).toEqual([]);
|
|
594
|
+
expect(state.total).toBe(0);
|
|
595
|
+
await harness.client.stop();
|
|
596
|
+
});
|
|
597
|
+
|
|
598
|
+
it("rejects null filters that list params cannot express", async () => {
|
|
599
|
+
const harness = await setup();
|
|
600
|
+
const query: WindowQuery = {collection: "messages", where: {threadId: null}};
|
|
601
|
+
await expect(harness.client.queryWindows.fetchWindow({query})).rejects.toThrow(
|
|
602
|
+
/where\.threadId is null/
|
|
603
|
+
);
|
|
604
|
+
expect(harness.listCalls).toHaveLength(0);
|
|
605
|
+
expect(harness.client.queryWindows.getWindow(query)?.status).toBe("error");
|
|
606
|
+
await harness.client.stop();
|
|
607
|
+
});
|
|
608
|
+
|
|
609
|
+
it("prunes unretained windows older than the retention window", async () => {
|
|
610
|
+
const {client} = await setup();
|
|
611
|
+
await client.queryWindows.fetchWindow({query: THREAD_A});
|
|
612
|
+
const release = client.queryWindows.retain(PINNED);
|
|
613
|
+
await flush();
|
|
614
|
+
expect(client.queryWindows.pruneStale({olderThanMs: 60_000})).toBe(0);
|
|
615
|
+
expect(client.queryWindows.pruneStale({olderThanMs: -1})).toBe(1);
|
|
616
|
+
expect(client.queryWindows.getWindow(THREAD_A)).toBeUndefined();
|
|
617
|
+
expect(client.queryWindows.getWindow(PINNED)?.pages).toBe(1);
|
|
618
|
+
release();
|
|
619
|
+
await client.stop();
|
|
620
|
+
});
|
|
621
|
+
|
|
622
|
+
it("rejects a query collection missing from collections", () => {
|
|
623
|
+
expect(() =>
|
|
624
|
+
createSyncDb({
|
|
625
|
+
authProvider: auth,
|
|
626
|
+
collections: ["todos"],
|
|
627
|
+
name: "bad-config",
|
|
628
|
+
persisterFactory: memoryPersisterFactory,
|
|
629
|
+
queryCollections: ["messages"],
|
|
630
|
+
transport: createFakeTransport(),
|
|
631
|
+
})
|
|
632
|
+
).toThrow(/must also be listed in collections/);
|
|
633
|
+
});
|
|
634
|
+
});
|
|
635
|
+
|
|
636
|
+
describe("planRange", () => {
|
|
637
|
+
it("uses one exact request when the range fits under the cap", () => {
|
|
638
|
+
expect(planRange({cap: 100, count: 20, start: 40})).toEqual({
|
|
639
|
+
chunk: 20,
|
|
640
|
+
firstPage: 3,
|
|
641
|
+
offset: 0,
|
|
642
|
+
requests: 1,
|
|
643
|
+
});
|
|
644
|
+
});
|
|
645
|
+
|
|
646
|
+
it("splits a range larger than the cap into cap-sized pages", () => {
|
|
647
|
+
expect(planRange({cap: 3, count: 6, start: 0})).toEqual({
|
|
648
|
+
chunk: 3,
|
|
649
|
+
firstPage: 1,
|
|
650
|
+
offset: 0,
|
|
651
|
+
requests: 2,
|
|
652
|
+
});
|
|
653
|
+
expect(planRange({cap: 100, count: 101, start: 0}).requests).toBe(2);
|
|
654
|
+
});
|
|
655
|
+
|
|
656
|
+
it("aligns unaligned starts to cap-sized pages instead of tiny chunks", () => {
|
|
657
|
+
// pageSize 53 over maxLimit 50: page two starts at row 53.
|
|
658
|
+
expect(planRange({cap: 50, count: 53, start: 53})).toEqual({
|
|
659
|
+
chunk: 50,
|
|
660
|
+
firstPage: 2,
|
|
661
|
+
offset: 3,
|
|
662
|
+
requests: 2,
|
|
663
|
+
});
|
|
664
|
+
});
|
|
665
|
+
});
|