@midsummer-ai/sdk 0.1.0-next.900 → 0.1.0-staging.3100
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/auth-1W-SzpD3.mjs +441 -0
- package/build-DQo-JrlN.d.mts +120 -0
- package/bytes-C2pcyl9u.mjs +280 -0
- package/{connection-CDk7H1FL.mjs → connection-Cu2GSOaR.mjs} +171 -31
- package/database/build.d.mts +2 -2
- package/database/build.mjs +1404 -117
- package/database/client.d.mts +85 -9
- package/database/client.mjs +550 -32
- package/database/contract.d.mts +92 -2
- package/database/contract.mjs +2179 -3
- package/database/errors.d.mts +1 -22
- package/database/react.d.mts +133 -0
- package/database/react.mjs +488 -0
- package/database/server.d.mts +2 -3
- package/database/server.mjs +3 -320
- package/database/stand-in.d.mts +95 -9
- package/database/stand-in.mjs +1436 -116
- package/errors-x2vXKi0u.d.mts +25 -0
- package/index-DNNXNny7.d.mts +410 -0
- package/key-BrRdFY-n.mjs +11 -0
- package/package.json +114 -1
- package/schema-CTHQMRsW.mjs +217 -0
- package/server-B5HK2Ou9.d.mts +574 -0
- package/server-EDRbF2u-.mjs +1893 -0
- package/{store-BzI9at5x.mjs → store-lIZ5GxVv.mjs} +1 -1
- package/{tabs-D-P8_ci_.mjs → tabs-zCh8kceo.mjs} +1 -1
- package/vector/contract.d.mts +1 -1
- package/vector/contract.mjs +773 -1
- package/vector/index.d.mts +2 -2
- package/vector/index.mjs +71 -3
- package/vector/stand-in.d.mts +1 -1
- package/vector/stand-in.mjs +326 -23
- package/vector/wire.d.mts +11 -2
- package/vector/wire.mjs +179 -14
- package/build-SQ44jwSa.d.mts +0 -41
- package/index-DdoK0y7D.d.mts +0 -192
- package/schema-BF-Y1EVu.mjs +0 -142
- package/schema-DS8x1Dn8.mjs +0 -115
- package/schema-f7E9uzUn.d.mts +0 -74
- package/server-C7O7CICU.d.mts +0 -182
package/database/contract.mjs
CHANGED
|
@@ -11,8 +11,19 @@ import { DatabaseError } from "./errors.mjs";
|
|
|
11
11
|
*
|
|
12
12
|
* for (const item of databaseContract) test(item.name, () => item.run(client))
|
|
13
13
|
*/
|
|
14
|
-
/**
|
|
14
|
+
/**
|
|
15
|
+
* The fixture's folder, served by every Database the cases run against: `schema.ts`, the functions in
|
|
16
|
+
* the files beside it and in `admin/tools.ts`, the helper `lib/text.ts`, and `zod` 4.1.12 from the
|
|
17
|
+
* installed packages. A Database is given its build (`buildDatabase` with `installed`), as a deploy uploads it.
|
|
18
|
+
* The sign-in cases (`databaseAuthContract`) serve it with an `auth.config.ts` beside it, from `testIssuer`.
|
|
19
|
+
*/
|
|
15
20
|
const contractFolder = new URL("./contract-db/", import.meta.url);
|
|
21
|
+
/**
|
|
22
|
+
* The fixture of what the Stand-in serves ahead of the Database: `schema.ts` with a vector index, and
|
|
23
|
+
* `places.ts` and `nearest.ts`. A build refuses it unless built with `ahead`; its cases are
|
|
24
|
+
* `databaseAheadContract`.
|
|
25
|
+
*/
|
|
26
|
+
const contractAheadFolder = new URL("./contract-ahead/", import.meta.url);
|
|
16
27
|
function eq(got, want, what) {
|
|
17
28
|
if (JSON.stringify(got) !== JSON.stringify(want)) throw new Error(`${what}: got ${JSON.stringify(got)}, want ${JSON.stringify(want)}`);
|
|
18
29
|
}
|
|
@@ -25,6 +36,23 @@ async function refusedWith(call, code, what) {
|
|
|
25
36
|
}
|
|
26
37
|
throw new Error(`${what}: answered, want refused ${code}`);
|
|
27
38
|
}
|
|
39
|
+
/** As `refusedWith`, and the refusal's message is `message`, as the function threw it. */
|
|
40
|
+
async function refusedSaying(call, code, message, what) {
|
|
41
|
+
try {
|
|
42
|
+
await call();
|
|
43
|
+
} catch (error) {
|
|
44
|
+
if (error instanceof DatabaseError && error.code === code && error.message === message) return;
|
|
45
|
+
throw new Error(`${what}: refused ${error?.code} ${JSON.stringify(error?.message)}, want ${code} ${JSON.stringify(message)}`);
|
|
46
|
+
}
|
|
47
|
+
throw new Error(`${what}: returned, want refused ${code}`);
|
|
48
|
+
}
|
|
49
|
+
/** `value` with each `ArrayBuffer` in it as `{ bytes: [...] }`, so `eq` can tell bytes from anything else. */
|
|
50
|
+
function plain(value) {
|
|
51
|
+
if (value instanceof ArrayBuffer) return { bytes: [...new Uint8Array(value)] };
|
|
52
|
+
if (Array.isArray(value)) return value.map(plain);
|
|
53
|
+
if (value !== null && typeof value === "object") return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, plain(item)]));
|
|
54
|
+
return value;
|
|
55
|
+
}
|
|
28
56
|
function caller(db) {
|
|
29
57
|
const subject = `c${Date.now().toString(36)}${Math.random().toString(36).slice(2, 8)}`;
|
|
30
58
|
return {
|
|
@@ -36,7 +64,620 @@ async function until(test, ms, what) {
|
|
|
36
64
|
for (const started = Date.now(); Date.now() - started < ms; await new Promise((resolve) => setTimeout(resolve, 50))) if (test()) return;
|
|
37
65
|
throw new Error(`${what}: not within ${ms} ms`);
|
|
38
66
|
}
|
|
67
|
+
/** Query chains, cursor pages and document metadata, runnable through any database client. */
|
|
68
|
+
const databaseQueryContract = [
|
|
69
|
+
{
|
|
70
|
+
name: "public int64 index values sort numerically across the full signed range",
|
|
71
|
+
async run(db) {
|
|
72
|
+
const { me } = caller(db);
|
|
73
|
+
await me.mutation("integers.seed");
|
|
74
|
+
eq(await me.query("integers.order"), {
|
|
75
|
+
asc: [
|
|
76
|
+
"min",
|
|
77
|
+
"below-safe",
|
|
78
|
+
"negative-ten",
|
|
79
|
+
"negative-two",
|
|
80
|
+
"zero",
|
|
81
|
+
"zero",
|
|
82
|
+
"two",
|
|
83
|
+
"two",
|
|
84
|
+
"two",
|
|
85
|
+
"ten",
|
|
86
|
+
"above-safe",
|
|
87
|
+
"max"
|
|
88
|
+
],
|
|
89
|
+
desc: [
|
|
90
|
+
"max",
|
|
91
|
+
"above-safe",
|
|
92
|
+
"ten",
|
|
93
|
+
"two",
|
|
94
|
+
"two",
|
|
95
|
+
"two",
|
|
96
|
+
"zero",
|
|
97
|
+
"zero",
|
|
98
|
+
"negative-two",
|
|
99
|
+
"negative-ten",
|
|
100
|
+
"below-safe",
|
|
101
|
+
"min"
|
|
102
|
+
],
|
|
103
|
+
first: "min",
|
|
104
|
+
last: "max"
|
|
105
|
+
}, "integer ordering");
|
|
106
|
+
eq(await caller(db).me.mutation("integers.adjacent"), {
|
|
107
|
+
asc: [
|
|
108
|
+
"min",
|
|
109
|
+
"min-plus-one",
|
|
110
|
+
"min-plus-two",
|
|
111
|
+
"below-safe",
|
|
112
|
+
"negative-boundary",
|
|
113
|
+
"positive-boundary",
|
|
114
|
+
"above-safe",
|
|
115
|
+
"max-minus-two",
|
|
116
|
+
"max-minus-one",
|
|
117
|
+
"max"
|
|
118
|
+
],
|
|
119
|
+
desc: [
|
|
120
|
+
"max",
|
|
121
|
+
"max-minus-one",
|
|
122
|
+
"max-minus-two",
|
|
123
|
+
"above-safe",
|
|
124
|
+
"positive-boundary",
|
|
125
|
+
"negative-boundary",
|
|
126
|
+
"below-safe",
|
|
127
|
+
"min-plus-two",
|
|
128
|
+
"min-plus-one",
|
|
129
|
+
"min"
|
|
130
|
+
]
|
|
131
|
+
}, "adjacent integers keep all 64 bits");
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
name: "public int64 range bounds compare by value including equivalent representations",
|
|
136
|
+
async run(db) {
|
|
137
|
+
const { me } = caller(db);
|
|
138
|
+
await me.mutation("integers.seed");
|
|
139
|
+
eq(await me.query("integers.ranges"), {
|
|
140
|
+
eq: [
|
|
141
|
+
"two",
|
|
142
|
+
"two",
|
|
143
|
+
"two"
|
|
144
|
+
],
|
|
145
|
+
between: ["ten", "above-safe"],
|
|
146
|
+
inclusive: [
|
|
147
|
+
"negative-ten",
|
|
148
|
+
"negative-two",
|
|
149
|
+
"zero",
|
|
150
|
+
"zero",
|
|
151
|
+
"two",
|
|
152
|
+
"two",
|
|
153
|
+
"two"
|
|
154
|
+
],
|
|
155
|
+
legacy: ["ten", "above-safe"]
|
|
156
|
+
}, "integer bounds");
|
|
157
|
+
}
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
name: "public int64 cursor pages preserve numeric bounds in both directions",
|
|
161
|
+
async run(db) {
|
|
162
|
+
const { me } = caller(db);
|
|
163
|
+
await me.mutation("integers.seed");
|
|
164
|
+
for (const desc of [false, true]) {
|
|
165
|
+
const first = await me.query("integers.page", {
|
|
166
|
+
cursor: null,
|
|
167
|
+
desc
|
|
168
|
+
});
|
|
169
|
+
eq([first.page, first.isDone], [desc ? ["above-safe"] : ["ten"], false], "first integer page");
|
|
170
|
+
const last = await me.query("integers.page", {
|
|
171
|
+
cursor: first.continueCursor,
|
|
172
|
+
desc
|
|
173
|
+
});
|
|
174
|
+
eq([last.page, last.isDone], [desc ? ["ten"] : ["above-safe"], true], "last integer page");
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
},
|
|
178
|
+
{
|
|
179
|
+
name: "public int64 values reject values outside the signed 64-bit range",
|
|
180
|
+
async run(db) {
|
|
181
|
+
const { me } = caller(db);
|
|
182
|
+
for (const value of [
|
|
183
|
+
"9223372036854775808",
|
|
184
|
+
"-9223372036854775809",
|
|
185
|
+
9007199254740992
|
|
186
|
+
]) {
|
|
187
|
+
await refusedWith(() => me.mutation("integers.add", { value }), "invalid", "integer argument outside range");
|
|
188
|
+
await refusedWith(() => me.mutation("integers.addRaw", { value }), "invalid", "integer document outside range");
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
},
|
|
192
|
+
{
|
|
193
|
+
name: "public int64 union values follow value type order without converting string fields",
|
|
194
|
+
async run(db) {
|
|
195
|
+
const { me } = caller(db);
|
|
196
|
+
eq(await me.mutation("integers.seedMixed"), [
|
|
197
|
+
"missing",
|
|
198
|
+
"null",
|
|
199
|
+
"min",
|
|
200
|
+
"two",
|
|
201
|
+
"ten",
|
|
202
|
+
"max",
|
|
203
|
+
"float",
|
|
204
|
+
"false",
|
|
205
|
+
"true",
|
|
206
|
+
"string"
|
|
207
|
+
], "mixed integer ordering");
|
|
208
|
+
eq(await me.mutation("integers.strings"), ["ten", "two"], "ordinary strings keep string order");
|
|
209
|
+
}
|
|
210
|
+
},
|
|
211
|
+
{
|
|
212
|
+
name: "ambiguous public integer union representations are refused instead of guessed",
|
|
213
|
+
async run(db) {
|
|
214
|
+
const { me } = caller(db);
|
|
215
|
+
for (const value of ["2", 2]) await refusedWith(() => me.mutation("integers.ambiguous", { value }), "refused", "ambiguous integer type");
|
|
216
|
+
}
|
|
217
|
+
},
|
|
218
|
+
{
|
|
219
|
+
name: "a query builder's filter is applied before a page is counted, so each page has numItems rows that pass; every terminal and for await filter too",
|
|
220
|
+
async run(db) {
|
|
221
|
+
const { me } = caller(db);
|
|
222
|
+
for (let score = 1; score <= 7; score += 1) await me.mutation("audit.add", {
|
|
223
|
+
score,
|
|
224
|
+
label: String(score)
|
|
225
|
+
});
|
|
226
|
+
const first = await me.query("items.oddPage", { numItems: 2 });
|
|
227
|
+
eq([first.page, first.isDone], [[1, 3], false], "the first page");
|
|
228
|
+
const second = await me.query("items.oddPage", {
|
|
229
|
+
numItems: 2,
|
|
230
|
+
cursor: first.continueCursor
|
|
231
|
+
});
|
|
232
|
+
eq([second.page, second.isDone], [[5, 7], true], "the last page");
|
|
233
|
+
eq(await me.query("items.filtered"), {
|
|
234
|
+
first: 3,
|
|
235
|
+
taken: [6, 4],
|
|
236
|
+
unique: 4,
|
|
237
|
+
none: 0,
|
|
238
|
+
sum: 25
|
|
239
|
+
}, "each terminal, filtered, and for await");
|
|
240
|
+
}
|
|
241
|
+
},
|
|
242
|
+
{
|
|
243
|
+
name: "a page stops where its read budget is spent, short and not done, and the next page goes on from there",
|
|
244
|
+
async run(db) {
|
|
245
|
+
const { me } = caller(db);
|
|
246
|
+
for (let score = 1; score <= 7; score += 1) await me.mutation("audit.add", {
|
|
247
|
+
score,
|
|
248
|
+
label: String(score)
|
|
249
|
+
});
|
|
250
|
+
const pages = [];
|
|
251
|
+
let cursor;
|
|
252
|
+
for (let k = 0; k < 6; k += 1) {
|
|
253
|
+
const page = await me.query("items.budgetPage", {
|
|
254
|
+
cursor,
|
|
255
|
+
numItems: 2,
|
|
256
|
+
rows: 2,
|
|
257
|
+
odd: true
|
|
258
|
+
});
|
|
259
|
+
pages.push([page.page, page.isDone]);
|
|
260
|
+
if (page.isDone) break;
|
|
261
|
+
cursor = page.continueCursor;
|
|
262
|
+
}
|
|
263
|
+
eq(pages, [
|
|
264
|
+
[[1], false],
|
|
265
|
+
[[3], false],
|
|
266
|
+
[[5], false],
|
|
267
|
+
[[7], true]
|
|
268
|
+
], "pages of at most two rows read");
|
|
269
|
+
const bytes = await me.query("items.budgetPage", {
|
|
270
|
+
numItems: 5,
|
|
271
|
+
bytes: 1,
|
|
272
|
+
odd: true
|
|
273
|
+
});
|
|
274
|
+
eq([bytes.page, bytes.isDone], [[1], false], "a page of at most one byte read: the first row only");
|
|
275
|
+
}
|
|
276
|
+
},
|
|
277
|
+
{
|
|
278
|
+
name: "a byte budget ends a page at the row that spends it, however small the rows before it",
|
|
279
|
+
async run(db) {
|
|
280
|
+
const { me } = caller(db);
|
|
281
|
+
await me.mutation("items.seedSized", { sizes: [
|
|
282
|
+
10,
|
|
283
|
+
2e3,
|
|
284
|
+
2e3,
|
|
285
|
+
2e3
|
|
286
|
+
] });
|
|
287
|
+
const first = await me.query("items.budgetPage", {
|
|
288
|
+
numItems: 5,
|
|
289
|
+
bytes: 1e3,
|
|
290
|
+
odd: false
|
|
291
|
+
});
|
|
292
|
+
eq([first.page, first.isDone], [[2, 4], false], "the small row, then the row that spends the budget");
|
|
293
|
+
const next = await me.query("items.budgetPage", {
|
|
294
|
+
cursor: first.continueCursor,
|
|
295
|
+
numItems: 5,
|
|
296
|
+
bytes: 1e3,
|
|
297
|
+
odd: false
|
|
298
|
+
});
|
|
299
|
+
eq([next.page, next.isDone], [[6], false], "the next page goes on from there, one large row");
|
|
300
|
+
}
|
|
301
|
+
},
|
|
302
|
+
{
|
|
303
|
+
name: "a query builder's terminals are promises, as Convex's are: then works on each",
|
|
304
|
+
async run(db) {
|
|
305
|
+
const { me } = caller(db);
|
|
306
|
+
await me.mutation("items.seed");
|
|
307
|
+
eq(await me.query("items.thenable"), [
|
|
308
|
+
4,
|
|
309
|
+
1,
|
|
310
|
+
"first",
|
|
311
|
+
1
|
|
312
|
+
], "collect, take, first and paginate through then");
|
|
313
|
+
}
|
|
314
|
+
},
|
|
315
|
+
{
|
|
316
|
+
name: "query builders preserve compound prefixes, bounds and terminal results",
|
|
317
|
+
async run(db) {
|
|
318
|
+
const { me } = caller(db);
|
|
319
|
+
await me.mutation("items.seed");
|
|
320
|
+
eq(await me.query("items.terminals"), {
|
|
321
|
+
collected: [
|
|
322
|
+
"first",
|
|
323
|
+
"second",
|
|
324
|
+
"second",
|
|
325
|
+
"third"
|
|
326
|
+
],
|
|
327
|
+
taken: ["third", "second"],
|
|
328
|
+
first: "first",
|
|
329
|
+
unique: "third",
|
|
330
|
+
empty: null,
|
|
331
|
+
emptyUnique: null,
|
|
332
|
+
bounded: ["second", "second"],
|
|
333
|
+
prefix: ["third"]
|
|
334
|
+
}, "the query results");
|
|
335
|
+
await refusedWith(() => me.query("items.nonUnique"), "refused", "multiple rows for unique");
|
|
336
|
+
}
|
|
337
|
+
},
|
|
338
|
+
{
|
|
339
|
+
name: "cursor pages resume across equal keys in both orders, even after the last row is deleted",
|
|
340
|
+
async run(db) {
|
|
341
|
+
for (const desc of [false, true]) {
|
|
342
|
+
const { me } = caller(db);
|
|
343
|
+
await me.mutation("items.seed");
|
|
344
|
+
const first = await me.query("items.page", {
|
|
345
|
+
cursor: null,
|
|
346
|
+
numItems: 2,
|
|
347
|
+
desc
|
|
348
|
+
});
|
|
349
|
+
eq(first.page.map((d) => d.label), desc ? ["third", "second"] : ["first", "second"], "the first page");
|
|
350
|
+
eq([
|
|
351
|
+
first.isDone,
|
|
352
|
+
typeof first.continueCursor,
|
|
353
|
+
first.continueCursor.length > 0
|
|
354
|
+
], [
|
|
355
|
+
false,
|
|
356
|
+
"string",
|
|
357
|
+
true
|
|
358
|
+
], "a continuing page");
|
|
359
|
+
await me.mutation("items.remove", { id: first.page[1].id });
|
|
360
|
+
const last = await me.query("items.page", {
|
|
361
|
+
cursor: first.continueCursor,
|
|
362
|
+
numItems: 2,
|
|
363
|
+
desc
|
|
364
|
+
});
|
|
365
|
+
eq(last.page.map((d) => d.label), desc ? ["second", "first"] : ["second", "third"], "the last page");
|
|
366
|
+
eq(last.isDone, true, "the end");
|
|
367
|
+
const end = await me.query("items.page", {
|
|
368
|
+
cursor: last.continueCursor,
|
|
369
|
+
numItems: 2,
|
|
370
|
+
desc
|
|
371
|
+
});
|
|
372
|
+
eq([end.page, end.isDone], [[], true], "after the end");
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
},
|
|
376
|
+
{
|
|
377
|
+
name: "a page pinned to its end cursor keeps its rows: one inserted inside it appears there, and no row repeats or is skipped across pages",
|
|
378
|
+
async run(db) {
|
|
379
|
+
for (const desc of [false, true]) {
|
|
380
|
+
const { me } = caller(db);
|
|
381
|
+
const ids = {};
|
|
382
|
+
for (const score of [
|
|
383
|
+
10,
|
|
384
|
+
20,
|
|
385
|
+
30,
|
|
386
|
+
40,
|
|
387
|
+
50
|
|
388
|
+
]) ids[score] = (await me.mutation("audit.add", {
|
|
389
|
+
score,
|
|
390
|
+
label: String(score)
|
|
391
|
+
})).id;
|
|
392
|
+
const read = async (cursor, endCursor) => await me.query("items.pinnedPage", {
|
|
393
|
+
cursor: cursor ?? void 0,
|
|
394
|
+
endCursor,
|
|
395
|
+
numItems: 2,
|
|
396
|
+
desc
|
|
397
|
+
});
|
|
398
|
+
const scores = (page) => page.page.map((d) => d.score);
|
|
399
|
+
const first = await read(null), second = await read(first.continueCursor), third = await read(second.continueCursor);
|
|
400
|
+
eq([
|
|
401
|
+
scores(first),
|
|
402
|
+
scores(second),
|
|
403
|
+
scores(third),
|
|
404
|
+
third.isDone
|
|
405
|
+
], desc ? [
|
|
406
|
+
[50, 40],
|
|
407
|
+
[30, 20],
|
|
408
|
+
[10],
|
|
409
|
+
true
|
|
410
|
+
] : [
|
|
411
|
+
[10, 20],
|
|
412
|
+
[30, 40],
|
|
413
|
+
[50],
|
|
414
|
+
true
|
|
415
|
+
], "the pages as first read");
|
|
416
|
+
for (const score of [
|
|
417
|
+
15,
|
|
418
|
+
35,
|
|
419
|
+
5,
|
|
420
|
+
60
|
|
421
|
+
]) await me.mutation("audit.add", {
|
|
422
|
+
score,
|
|
423
|
+
label: String(score)
|
|
424
|
+
});
|
|
425
|
+
await me.mutation("items.remove", { id: ids[desc ? 20 : 40] });
|
|
426
|
+
const pinned = [
|
|
427
|
+
await read(null, first.continueCursor),
|
|
428
|
+
await read(first.continueCursor, second.continueCursor),
|
|
429
|
+
await read(second.continueCursor, third.continueCursor)
|
|
430
|
+
];
|
|
431
|
+
eq(pinned.map(scores), desc ? [
|
|
432
|
+
[
|
|
433
|
+
60,
|
|
434
|
+
50,
|
|
435
|
+
40
|
|
436
|
+
],
|
|
437
|
+
[35, 30],
|
|
438
|
+
[
|
|
439
|
+
15,
|
|
440
|
+
10,
|
|
441
|
+
5
|
|
442
|
+
]
|
|
443
|
+
] : [
|
|
444
|
+
[
|
|
445
|
+
5,
|
|
446
|
+
10,
|
|
447
|
+
15,
|
|
448
|
+
20
|
|
449
|
+
],
|
|
450
|
+
[30, 35],
|
|
451
|
+
[50, 60]
|
|
452
|
+
], "each page pinned to its end");
|
|
453
|
+
eq(pinned.map((page) => page.isDone), [
|
|
454
|
+
false,
|
|
455
|
+
false,
|
|
456
|
+
true
|
|
457
|
+
], "only the last page is done");
|
|
458
|
+
eq(pinned.map((page) => page.continueCursor), [
|
|
459
|
+
first.continueCursor,
|
|
460
|
+
second.continueCursor,
|
|
461
|
+
third.continueCursor
|
|
462
|
+
], "each page continues where it is pinned");
|
|
463
|
+
eq(pinned.flatMap(scores), desc ? [
|
|
464
|
+
60,
|
|
465
|
+
50,
|
|
466
|
+
40,
|
|
467
|
+
35,
|
|
468
|
+
30,
|
|
469
|
+
15,
|
|
470
|
+
10,
|
|
471
|
+
5
|
|
472
|
+
] : [
|
|
473
|
+
5,
|
|
474
|
+
10,
|
|
475
|
+
15,
|
|
476
|
+
20,
|
|
477
|
+
30,
|
|
478
|
+
35,
|
|
479
|
+
50,
|
|
480
|
+
60
|
|
481
|
+
], "every row once, in order");
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
},
|
|
485
|
+
{
|
|
486
|
+
name: "documents expose their id and creation time, and default reads follow creation order",
|
|
487
|
+
async run(db) {
|
|
488
|
+
const { me } = caller(db);
|
|
489
|
+
const ids = await me.mutation("items.seed");
|
|
490
|
+
eq(await me.query("items.metadata", { id: ids[0] }), {
|
|
491
|
+
valid: true,
|
|
492
|
+
defaultOrder: [
|
|
493
|
+
"third",
|
|
494
|
+
"first",
|
|
495
|
+
"second",
|
|
496
|
+
"second"
|
|
497
|
+
],
|
|
498
|
+
creationOrder: [
|
|
499
|
+
"third",
|
|
500
|
+
"first",
|
|
501
|
+
"second",
|
|
502
|
+
"second"
|
|
503
|
+
],
|
|
504
|
+
metadataOnAll: true
|
|
505
|
+
}, "the metadata and order");
|
|
506
|
+
eq(await me.action("item_actions.metadata", { id: ids[0] }), {
|
|
507
|
+
get: true,
|
|
508
|
+
range: true
|
|
509
|
+
}, "action read metadata");
|
|
510
|
+
eq(await me.mutation("items.rewrite", { id: ids[0] }), {
|
|
511
|
+
patched: true,
|
|
512
|
+
replaced: true,
|
|
513
|
+
id: true
|
|
514
|
+
}, "metadata after writes");
|
|
515
|
+
}
|
|
516
|
+
},
|
|
517
|
+
{
|
|
518
|
+
name: "empty pages finish and pagination refuses invalid sizes and cursors",
|
|
519
|
+
async run(db) {
|
|
520
|
+
const { me } = caller(db);
|
|
521
|
+
const empty = await me.query("items.page", {
|
|
522
|
+
cursor: null,
|
|
523
|
+
numItems: 2,
|
|
524
|
+
desc: false
|
|
525
|
+
});
|
|
526
|
+
eq([
|
|
527
|
+
empty.page,
|
|
528
|
+
empty.isDone,
|
|
529
|
+
typeof empty.continueCursor
|
|
530
|
+
], [
|
|
531
|
+
[],
|
|
532
|
+
true,
|
|
533
|
+
"string"
|
|
534
|
+
], "an empty page");
|
|
535
|
+
const end = await me.query("items.page", {
|
|
536
|
+
cursor: empty.continueCursor,
|
|
537
|
+
numItems: 2,
|
|
538
|
+
desc: false
|
|
539
|
+
});
|
|
540
|
+
eq([
|
|
541
|
+
end.page,
|
|
542
|
+
end.isDone,
|
|
543
|
+
typeof end.continueCursor
|
|
544
|
+
], [
|
|
545
|
+
[],
|
|
546
|
+
true,
|
|
547
|
+
"string"
|
|
548
|
+
], "the end cursor");
|
|
549
|
+
await refusedWith(() => me.query("items.page", {
|
|
550
|
+
cursor: "invalid",
|
|
551
|
+
numItems: 2,
|
|
552
|
+
desc: false
|
|
553
|
+
}), "refused", "an invalid cursor");
|
|
554
|
+
await refusedWith(() => me.query("items.page", {
|
|
555
|
+
cursor: null,
|
|
556
|
+
numItems: 0,
|
|
557
|
+
desc: false
|
|
558
|
+
}), "refused", "a zero page size");
|
|
559
|
+
await refusedWith(() => me.query("items.invalidField", { field: "score" }), "refused", "an equality that skips the first index field");
|
|
560
|
+
}
|
|
561
|
+
},
|
|
562
|
+
{
|
|
563
|
+
name: "equal-key inserts between cursor pages follow creation time without skips or duplicates",
|
|
564
|
+
async run(db) {
|
|
565
|
+
const { me } = caller(db);
|
|
566
|
+
const old = await me.mutation("audit.add", {
|
|
567
|
+
score: 1,
|
|
568
|
+
label: "old"
|
|
569
|
+
});
|
|
570
|
+
await me.mutation("audit.add", {
|
|
571
|
+
score: 3,
|
|
572
|
+
label: "tail"
|
|
573
|
+
});
|
|
574
|
+
const first = await me.query("items.page", {
|
|
575
|
+
cursor: null,
|
|
576
|
+
numItems: 1,
|
|
577
|
+
desc: false
|
|
578
|
+
});
|
|
579
|
+
eq(first.page.map((d) => d.label), ["old"], "the first page");
|
|
580
|
+
await until(() => Date.now() > old.time, 5e3, "a later insertion time");
|
|
581
|
+
await me.mutation("audit.add", {
|
|
582
|
+
score: 1,
|
|
583
|
+
label: "new"
|
|
584
|
+
});
|
|
585
|
+
const next = await me.query("items.page", {
|
|
586
|
+
cursor: first.continueCursor,
|
|
587
|
+
numItems: 2,
|
|
588
|
+
desc: false
|
|
589
|
+
});
|
|
590
|
+
eq([next.page.map((d) => d.label), next.isDone], [["new", "tail"], true], "the remaining documents");
|
|
591
|
+
eq(await me.query("audit.createdAfter", { time: old.time }), ["new"], "the implicit creation-time range");
|
|
592
|
+
eq(await me.query("audit.legacyId", { id: old.id }), ["old"], "the legacy id bound");
|
|
593
|
+
eq(await me.action("item_actions.legacyId", { id: old.id }), ["old"], "the action id bound");
|
|
594
|
+
}
|
|
595
|
+
},
|
|
596
|
+
{
|
|
597
|
+
name: "a table named query supports its old methods and the callable builder",
|
|
598
|
+
async run(db) {
|
|
599
|
+
eq(await caller(db).me.mutation("audit.queryTable"), {
|
|
600
|
+
legacy: "patched",
|
|
601
|
+
builder: ["patched"],
|
|
602
|
+
explicit: "patched"
|
|
603
|
+
}, "the query table");
|
|
604
|
+
}
|
|
605
|
+
},
|
|
606
|
+
{
|
|
607
|
+
name: "missing and null remain distinct in index equality and cursor pages",
|
|
608
|
+
async run(db) {
|
|
609
|
+
const { me } = caller(db);
|
|
610
|
+
await me.mutation("audit.seedValues");
|
|
611
|
+
eq(await me.query("audit.missingAndNull"), {
|
|
612
|
+
missing: ["missing"],
|
|
613
|
+
nulls: ["null"],
|
|
614
|
+
all: [
|
|
615
|
+
"missing",
|
|
616
|
+
"null",
|
|
617
|
+
"number",
|
|
618
|
+
"boolean",
|
|
619
|
+
"string"
|
|
620
|
+
]
|
|
621
|
+
}, "value order and equality");
|
|
622
|
+
for (const desc of [false, true]) {
|
|
623
|
+
let cursor = null;
|
|
624
|
+
const labels = [];
|
|
625
|
+
for (let i = 0; i < 5; i += 1) {
|
|
626
|
+
const result = await me.query("audit.valuePage", {
|
|
627
|
+
cursor,
|
|
628
|
+
desc,
|
|
629
|
+
missingOnly: false
|
|
630
|
+
});
|
|
631
|
+
labels.push(...result.page);
|
|
632
|
+
cursor = result.continueCursor;
|
|
633
|
+
eq(result.isDone, i === 4, "the final page flag");
|
|
634
|
+
}
|
|
635
|
+
eq(labels, desc ? [
|
|
636
|
+
"string",
|
|
637
|
+
"boolean",
|
|
638
|
+
"number",
|
|
639
|
+
"null",
|
|
640
|
+
"missing"
|
|
641
|
+
] : [
|
|
642
|
+
"missing",
|
|
643
|
+
"null",
|
|
644
|
+
"number",
|
|
645
|
+
"boolean",
|
|
646
|
+
"string"
|
|
647
|
+
], "the paged value order");
|
|
648
|
+
}
|
|
649
|
+
const missing = await me.query("audit.valuePage", {
|
|
650
|
+
cursor: null,
|
|
651
|
+
desc: false,
|
|
652
|
+
missingOnly: true
|
|
653
|
+
});
|
|
654
|
+
eq([missing.page, missing.isDone], [["missing"], true], "missing-only equality");
|
|
655
|
+
const end = await me.query("audit.valuePage", {
|
|
656
|
+
cursor: missing.continueCursor,
|
|
657
|
+
desc: false,
|
|
658
|
+
missingOnly: true
|
|
659
|
+
});
|
|
660
|
+
eq([end.page, end.isDone], [[], true], "a cursor scoped to missing equality");
|
|
661
|
+
}
|
|
662
|
+
},
|
|
663
|
+
{
|
|
664
|
+
name: "inserted creation times follow call order and fall within independent clock bounds",
|
|
665
|
+
async run(db) {
|
|
666
|
+
const { me } = caller(db);
|
|
667
|
+
const before = Date.now();
|
|
668
|
+
const first = await me.mutation("audit.insertTimes");
|
|
669
|
+
const second = await me.mutation("audit.insertTimes");
|
|
670
|
+
const after = Date.now() + 1;
|
|
671
|
+
const times = [...first, ...second];
|
|
672
|
+
eq([first.length, second.length], [2, 2], "inserted documents");
|
|
673
|
+
eq(times.every((t) => Number.isFinite(t) && t >= before && t < after), true, "independent timestamp bounds");
|
|
674
|
+
eq(times.every((t, i) => i === 0 || t >= times[i - 1]), true, "insertion order");
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
];
|
|
678
|
+
/** Every database guarantee, including the query and metadata cases. */
|
|
39
679
|
const databaseContract = [
|
|
680
|
+
...databaseQueryContract,
|
|
40
681
|
{
|
|
41
682
|
name: "a mutation's row reads back with what it wrote, owned by its caller",
|
|
42
683
|
async run(db) {
|
|
@@ -126,7 +767,140 @@ const databaseContract = [
|
|
|
126
767
|
{
|
|
127
768
|
name: "an internal function is refused to a client",
|
|
128
769
|
async run(db) {
|
|
129
|
-
|
|
770
|
+
const { me } = caller(db);
|
|
771
|
+
await refusedWith(() => me.query("notes.secret", {}), "internal", "the internal query");
|
|
772
|
+
await refusedWith(() => me.mutation("notes.addInternal", { text: "sneaked" }), "internal", "the internal mutation");
|
|
773
|
+
await refusedWith(() => me.action("jobs.addThenCount", { text: "sneaked" }), "internal", "the internal action");
|
|
774
|
+
eq(await me.query("notes.mine", {}), [], "the rows");
|
|
775
|
+
}
|
|
776
|
+
},
|
|
777
|
+
{
|
|
778
|
+
name: "a function runs a nested helper module and an installed package",
|
|
779
|
+
async run(db) {
|
|
780
|
+
const { me } = caller(db);
|
|
781
|
+
await me.mutation("notes.addLoud", { text: "hello" });
|
|
782
|
+
await refusedWith(() => me.mutation("notes.addLoud", { text: "hi" }), "refused", "a text zod refuses");
|
|
783
|
+
eq(await me.query("notes.mine", {}), ["HELLO!"], "the rows");
|
|
784
|
+
}
|
|
785
|
+
},
|
|
786
|
+
{
|
|
787
|
+
name: "a mutation's nested calls are one transaction with it: a throw after them writes nothing",
|
|
788
|
+
async run(db) {
|
|
789
|
+
const { me } = caller(db);
|
|
790
|
+
await refusedWith(() => me.mutation("notes.addVia", {
|
|
791
|
+
text: "a",
|
|
792
|
+
fail: true
|
|
793
|
+
}), "refused", "the throw");
|
|
794
|
+
eq(await me.query("notes.mine", {}), [], "after the throw");
|
|
795
|
+
eq(await me.mutation("notes.addVia", {
|
|
796
|
+
text: "b",
|
|
797
|
+
fail: false
|
|
798
|
+
}), ["b inner", "b outer"], "what its nested query saw");
|
|
799
|
+
eq(await me.query("notes.mine", {}), ["b inner", "b outer"], "after the return");
|
|
800
|
+
}
|
|
801
|
+
},
|
|
802
|
+
{
|
|
803
|
+
name: "a nested mutation that throws undoes only its own writes when its caller catches it",
|
|
804
|
+
async run(db) {
|
|
805
|
+
const { me } = caller(db);
|
|
806
|
+
await me.mutation("notes.addCaught", { text: "c" });
|
|
807
|
+
eq(await me.query("notes.mine", {}), ["c outer"], "the rows");
|
|
808
|
+
}
|
|
809
|
+
},
|
|
810
|
+
{
|
|
811
|
+
name: "nested mutations run at once: one that throws undoes only its own writes, never a sibling's",
|
|
812
|
+
async run(db) {
|
|
813
|
+
const { me } = caller(db);
|
|
814
|
+
await me.mutation("notes.addPair", {});
|
|
815
|
+
eq(await me.query("notes.mine", {}), ["keep"], "the rows");
|
|
816
|
+
}
|
|
817
|
+
},
|
|
818
|
+
{
|
|
819
|
+
name: "a mutation's scheduling and canceling happen only if it commits, nested or not",
|
|
820
|
+
async run(db) {
|
|
821
|
+
const { me } = caller(db);
|
|
822
|
+
const id = await me.mutation("notes.schedule", {});
|
|
823
|
+
eq(await me.mutation("notes.scheduled", { id }), "pending", "once scheduled");
|
|
824
|
+
await me.mutation("notes.cancelCaught", { id });
|
|
825
|
+
eq(await me.mutation("notes.scheduled", { id }), "pending", "after a nested cancel that threw");
|
|
826
|
+
await refusedWith(() => me.mutation("notes.cancel", {
|
|
827
|
+
id,
|
|
828
|
+
fail: true
|
|
829
|
+
}), "refused", "a cancel that throws");
|
|
830
|
+
eq(await me.mutation("notes.scheduled", { id }), "pending", "after a cancel that threw");
|
|
831
|
+
await me.mutation("notes.cancel", {
|
|
832
|
+
id,
|
|
833
|
+
fail: false
|
|
834
|
+
});
|
|
835
|
+
eq(await me.mutation("notes.scheduled", { id }), "canceled", "after a cancel that returned");
|
|
836
|
+
}
|
|
837
|
+
},
|
|
838
|
+
{
|
|
839
|
+
name: "nested mutations that update one row at once each see the others' updates, the caller's too",
|
|
840
|
+
async run(db) {
|
|
841
|
+
const { me } = caller(db);
|
|
842
|
+
const id = await me.mutation("notes.add", { text: "count" });
|
|
843
|
+
await me.mutation("notes.bumpThree", { id });
|
|
844
|
+
eq((await me.query("notes.get", { id })).n, 3, "the count");
|
|
845
|
+
}
|
|
846
|
+
},
|
|
847
|
+
{
|
|
848
|
+
name: "a run scheduled and canceled in one transaction is canceled after it",
|
|
849
|
+
async run(db) {
|
|
850
|
+
const { me } = caller(db);
|
|
851
|
+
const id = await me.mutation("notes.scheduleCanceled", {});
|
|
852
|
+
eq(await me.mutation("notes.scheduled", { id }), "canceled", "its state");
|
|
853
|
+
}
|
|
854
|
+
},
|
|
855
|
+
{
|
|
856
|
+
name: "a nested mutation whose answer doesn't fit its returns is refused, and its writes are undone",
|
|
857
|
+
async run(db) {
|
|
858
|
+
const { me } = caller(db);
|
|
859
|
+
await me.mutation("notes.addCaughtBadReturn", { text: "r" });
|
|
860
|
+
eq(await me.query("notes.mine", {}), ["r outer"], "the rows");
|
|
861
|
+
}
|
|
862
|
+
},
|
|
863
|
+
{
|
|
864
|
+
name: "a nested call of the wrong kind is refused, by reference or by name",
|
|
865
|
+
async run(db) {
|
|
866
|
+
const { me } = caller(db);
|
|
867
|
+
await refusedWith(() => me.mutation("notes.wrongKind", { by: "reference" }), "refused", "a query's reference");
|
|
868
|
+
await refusedWith(() => me.mutation("notes.wrongKind", { by: "name" }), "no function", "a query's name");
|
|
869
|
+
}
|
|
870
|
+
},
|
|
871
|
+
{
|
|
872
|
+
name: "a function sees none of the host's process: no environment, no require, no Bun",
|
|
873
|
+
async run(db) {
|
|
874
|
+
eq(await caller(db).me.query("notes.host", {}), [
|
|
875
|
+
null,
|
|
876
|
+
"undefined",
|
|
877
|
+
"undefined"
|
|
878
|
+
], "what it sees");
|
|
879
|
+
}
|
|
880
|
+
},
|
|
881
|
+
{
|
|
882
|
+
name: "a function in a folder of db/ is named by its folders, and runs a query given no arguments",
|
|
883
|
+
async run(db) {
|
|
884
|
+
const { me } = caller(db);
|
|
885
|
+
eq(await me.query("admin.tools.count", {}), 0, "before");
|
|
886
|
+
await me.mutation("notes.add", { text: "one" });
|
|
887
|
+
eq(await me.query("admin.tools.count", {}), 1, "after");
|
|
888
|
+
}
|
|
889
|
+
},
|
|
890
|
+
{
|
|
891
|
+
name: "an action's nested calls are calls of their own: a throw after them keeps what they wrote",
|
|
892
|
+
async run(db) {
|
|
893
|
+
const { me } = caller(db);
|
|
894
|
+
await refusedWith(() => me.action("jobs.relay", {
|
|
895
|
+
text: "a",
|
|
896
|
+
fail: true
|
|
897
|
+
}), "refused", "the throw");
|
|
898
|
+
eq(await me.query("notes.mine", {}), ["a"], "after the throw");
|
|
899
|
+
eq(await me.action("jobs.relay", {
|
|
900
|
+
text: "b",
|
|
901
|
+
fail: false
|
|
902
|
+
}), 2, "its answer");
|
|
903
|
+
eq(await me.query("notes.mine", {}), ["a", "b"], "after the return");
|
|
130
904
|
}
|
|
131
905
|
},
|
|
132
906
|
{
|
|
@@ -158,7 +932,1409 @@ const databaseContract = [
|
|
|
158
932
|
stop();
|
|
159
933
|
}
|
|
160
934
|
}
|
|
935
|
+
},
|
|
936
|
+
{
|
|
937
|
+
name: "a search finds the rows with the search's words that match its filter, more of the words first",
|
|
938
|
+
async run(db) {
|
|
939
|
+
const { me } = caller(db);
|
|
940
|
+
await me.mutation("posts.add", { body: "a red car" });
|
|
941
|
+
await me.mutation("posts.add", { body: "a red apple" });
|
|
942
|
+
await me.mutation("posts.add", { body: "a green pear" });
|
|
943
|
+
await me.mutation("posts.add", {
|
|
944
|
+
body: "a red apple",
|
|
945
|
+
owner: "someone else"
|
|
946
|
+
});
|
|
947
|
+
eq(await me.query("posts.search", { words: "red apple" }), ["a red apple", "a red car"], "the posts");
|
|
948
|
+
}
|
|
949
|
+
},
|
|
950
|
+
{
|
|
951
|
+
name: "a search's last word matches the words it begins",
|
|
952
|
+
async run(db) {
|
|
953
|
+
const { me } = caller(db);
|
|
954
|
+
await me.mutation("posts.add", { body: "rabbit stew" });
|
|
955
|
+
await me.mutation("posts.add", { body: "carrot stew" });
|
|
956
|
+
await me.mutation("posts.add", { body: "a reddish sky" });
|
|
957
|
+
eq(await me.query("posts.search", { words: "rab" }), ["rabbit stew"], "the posts");
|
|
958
|
+
eq(await me.query("posts.search", { words: "red blue red" }), ["a reddish sky"], "a last word typed before too");
|
|
959
|
+
}
|
|
960
|
+
},
|
|
961
|
+
{
|
|
962
|
+
name: "a search's pages go on after their last row, and read again pinned to their end cursors they split the ranking with every row once, a newer row too",
|
|
963
|
+
async run(db) {
|
|
964
|
+
const { me } = caller(db);
|
|
965
|
+
const ids = [];
|
|
966
|
+
for (let n = 0; n < 5; n += 1) {
|
|
967
|
+
ids.push(await me.mutation("posts.add", { body: "stew" }));
|
|
968
|
+
await new Promise((resolve) => setTimeout(resolve, 2));
|
|
969
|
+
}
|
|
970
|
+
const [p1, p2, p3, p4, p5] = ids;
|
|
971
|
+
const read = async (cursor, endCursor) => await me.query("posts.searchPage", {
|
|
972
|
+
words: "stew",
|
|
973
|
+
cursor: cursor ?? void 0,
|
|
974
|
+
endCursor,
|
|
975
|
+
numItems: 2
|
|
976
|
+
});
|
|
977
|
+
const first = await read(null), second = await read(first.continueCursor), third = await read(second.continueCursor);
|
|
978
|
+
eq([
|
|
979
|
+
first.page,
|
|
980
|
+
second.page,
|
|
981
|
+
third.page,
|
|
982
|
+
third.isDone
|
|
983
|
+
], [
|
|
984
|
+
[p5, p4],
|
|
985
|
+
[p3, p2],
|
|
986
|
+
[p1],
|
|
987
|
+
true
|
|
988
|
+
], "the pages as first read");
|
|
989
|
+
const p6 = await me.mutation("posts.add", { body: "stew" });
|
|
990
|
+
const pinned = [
|
|
991
|
+
await read(null, first.continueCursor),
|
|
992
|
+
await read(first.continueCursor, second.continueCursor),
|
|
993
|
+
await read(second.continueCursor, third.continueCursor)
|
|
994
|
+
];
|
|
995
|
+
eq(pinned.flatMap((page) => page.page), [
|
|
996
|
+
p6,
|
|
997
|
+
p5,
|
|
998
|
+
p4,
|
|
999
|
+
p3,
|
|
1000
|
+
p2,
|
|
1001
|
+
p1
|
|
1002
|
+
], "every row once, in rank order");
|
|
1003
|
+
eq(pinned.map((page) => page.continueCursor), [
|
|
1004
|
+
first.continueCursor,
|
|
1005
|
+
second.continueCursor,
|
|
1006
|
+
third.continueCursor
|
|
1007
|
+
], "each page continues where it is pinned");
|
|
1008
|
+
eq(pinned.map((page) => page.isDone), [
|
|
1009
|
+
false,
|
|
1010
|
+
false,
|
|
1011
|
+
true
|
|
1012
|
+
], "only the last page is done");
|
|
1013
|
+
}
|
|
1014
|
+
},
|
|
1015
|
+
{
|
|
1016
|
+
name: "a search page pinned to its end keeps its rows when over a thousand newer rows rank before them",
|
|
1017
|
+
async run(db) {
|
|
1018
|
+
const { me } = caller(db);
|
|
1019
|
+
const older = await me.mutation("posts.add", { body: "broth" });
|
|
1020
|
+
await new Promise((resolve) => setTimeout(resolve, 2));
|
|
1021
|
+
const newer = await me.mutation("posts.add", { body: "broth" });
|
|
1022
|
+
await new Promise((resolve) => setTimeout(resolve, 2));
|
|
1023
|
+
const first = await me.query("posts.searchPage", {
|
|
1024
|
+
words: "broth",
|
|
1025
|
+
numItems: 2
|
|
1026
|
+
});
|
|
1027
|
+
eq([first.page, first.isDone], [[newer, older], true], "the page as first read");
|
|
1028
|
+
await me.mutation("posts.addMany", {
|
|
1029
|
+
body: "broth",
|
|
1030
|
+
count: 1028
|
|
1031
|
+
});
|
|
1032
|
+
const pinned = await me.query("posts.searchPage", {
|
|
1033
|
+
words: "broth",
|
|
1034
|
+
numItems: 2,
|
|
1035
|
+
endCursor: first.continueCursor
|
|
1036
|
+
});
|
|
1037
|
+
eq([
|
|
1038
|
+
pinned.page.length,
|
|
1039
|
+
pinned.page.slice(-2),
|
|
1040
|
+
pinned.isDone
|
|
1041
|
+
], [
|
|
1042
|
+
1030,
|
|
1043
|
+
[newer, older],
|
|
1044
|
+
true
|
|
1045
|
+
], "every row, the first two last");
|
|
1046
|
+
eq(new Set(pinned.page).size, 1030, "no row twice");
|
|
1047
|
+
}
|
|
1048
|
+
},
|
|
1049
|
+
{
|
|
1050
|
+
name: "a search's pinned pages split its ranking with no row twice, when a boundary row now ranks last and when one is deleted",
|
|
1051
|
+
async run(db) {
|
|
1052
|
+
const { me } = caller(db);
|
|
1053
|
+
const made = [];
|
|
1054
|
+
for (let n = 0; n < 5; n += 1) {
|
|
1055
|
+
made.push(await me.mutation("posts.add", { body: "gravy" }));
|
|
1056
|
+
await new Promise((resolve) => setTimeout(resolve, 2));
|
|
1057
|
+
}
|
|
1058
|
+
const [e, d, c, b, a] = made;
|
|
1059
|
+
const read = async (cursor, endCursor) => await me.query("posts.searchPage", {
|
|
1060
|
+
words: "gravy",
|
|
1061
|
+
cursor: cursor ?? void 0,
|
|
1062
|
+
endCursor,
|
|
1063
|
+
numItems: 2
|
|
1064
|
+
});
|
|
1065
|
+
const first = await read(null), second = await read(first.continueCursor), third = await read(second.continueCursor);
|
|
1066
|
+
eq([
|
|
1067
|
+
first.page,
|
|
1068
|
+
second.page,
|
|
1069
|
+
third.page,
|
|
1070
|
+
third.isDone
|
|
1071
|
+
], [
|
|
1072
|
+
[a, b],
|
|
1073
|
+
[c, d],
|
|
1074
|
+
[e],
|
|
1075
|
+
true
|
|
1076
|
+
], "the pages as first read");
|
|
1077
|
+
const pinned = async () => [
|
|
1078
|
+
await read(null, first.continueCursor),
|
|
1079
|
+
await read(first.continueCursor, second.continueCursor),
|
|
1080
|
+
await read(second.continueCursor, third.continueCursor)
|
|
1081
|
+
];
|
|
1082
|
+
await me.mutation("posts.edit", {
|
|
1083
|
+
id: b,
|
|
1084
|
+
body: "gravy with a long tail of other words after it"
|
|
1085
|
+
});
|
|
1086
|
+
eq((await pinned()).flatMap((page) => page.page), [
|
|
1087
|
+
a,
|
|
1088
|
+
c,
|
|
1089
|
+
d,
|
|
1090
|
+
e,
|
|
1091
|
+
b
|
|
1092
|
+
], "every row once, in rank order, B last");
|
|
1093
|
+
await me.mutation("posts.remove", { id: d });
|
|
1094
|
+
eq((await pinned()).flatMap((page) => page.page), [
|
|
1095
|
+
a,
|
|
1096
|
+
c,
|
|
1097
|
+
e,
|
|
1098
|
+
b
|
|
1099
|
+
], "every row once without D");
|
|
1100
|
+
}
|
|
1101
|
+
},
|
|
1102
|
+
{
|
|
1103
|
+
name: "a search ranks by BM25: a word said more often in as short a text first, equal scores newest first",
|
|
1104
|
+
async run(db) {
|
|
1105
|
+
const { me } = caller(db);
|
|
1106
|
+
const once = await me.mutation("posts.add", { body: "fox" });
|
|
1107
|
+
await new Promise((resolve) => setTimeout(resolve, 2));
|
|
1108
|
+
const thrice = await me.mutation("posts.add", { body: "fox fox fox" });
|
|
1109
|
+
await new Promise((resolve) => setTimeout(resolve, 2));
|
|
1110
|
+
const again = await me.mutation("posts.add", { body: "fox" });
|
|
1111
|
+
eq(await me.query("posts.searchIds", { words: "fox" }), [
|
|
1112
|
+
thrice,
|
|
1113
|
+
again,
|
|
1114
|
+
once
|
|
1115
|
+
], "the posts");
|
|
1116
|
+
}
|
|
1117
|
+
},
|
|
1118
|
+
{
|
|
1119
|
+
name: "a search's filters all hold: two values for one field find nothing",
|
|
1120
|
+
async run(db) {
|
|
1121
|
+
const { me } = caller(db);
|
|
1122
|
+
await me.mutation("posts.add", { body: "plum mine" });
|
|
1123
|
+
await me.mutation("posts.add", {
|
|
1124
|
+
body: "plum theirs",
|
|
1125
|
+
owner: "someone else"
|
|
1126
|
+
});
|
|
1127
|
+
eq(await me.query("posts.searchAs", {
|
|
1128
|
+
words: "plum",
|
|
1129
|
+
owner: "someone else"
|
|
1130
|
+
}), [], "two owners");
|
|
1131
|
+
}
|
|
1132
|
+
},
|
|
1133
|
+
{
|
|
1134
|
+
name: "a search filter on a missing value finds the rows without it, not those with null",
|
|
1135
|
+
async run(db) {
|
|
1136
|
+
const { me } = caller(db);
|
|
1137
|
+
await me.mutation("posts.add", {
|
|
1138
|
+
body: "pear one",
|
|
1139
|
+
kind: "x"
|
|
1140
|
+
});
|
|
1141
|
+
await me.mutation("posts.add", { body: "pear two" });
|
|
1142
|
+
await me.mutation("posts.add", {
|
|
1143
|
+
body: "pear three",
|
|
1144
|
+
kind: null
|
|
1145
|
+
});
|
|
1146
|
+
eq(await me.query("posts.searchKind", {
|
|
1147
|
+
words: "pear",
|
|
1148
|
+
kind: "x"
|
|
1149
|
+
}), ["pear one"], "kind x");
|
|
1150
|
+
eq(await me.query("posts.searchKind", { words: "pear" }), ["pear two"], "no kind");
|
|
1151
|
+
}
|
|
1152
|
+
},
|
|
1153
|
+
{
|
|
1154
|
+
name: "a table's second search index searches its own field, each index its rows",
|
|
1155
|
+
async run(db) {
|
|
1156
|
+
const { me } = caller(db);
|
|
1157
|
+
await me.mutation("posts.add", {
|
|
1158
|
+
body: "quince tart",
|
|
1159
|
+
kind: "recipe"
|
|
1160
|
+
});
|
|
1161
|
+
await me.mutation("posts.add", {
|
|
1162
|
+
body: "recipe for quince",
|
|
1163
|
+
kind: "note"
|
|
1164
|
+
});
|
|
1165
|
+
await me.mutation("posts.add", { body: "fig jam" });
|
|
1166
|
+
eq(await me.query("posts.searchByKind", { words: "recipe" }), ["quince tart"], "by kind");
|
|
1167
|
+
eq(await me.query("posts.search", { words: "recipe" }), ["recipe for quince"], "by body");
|
|
1168
|
+
eq(await me.query("posts.searchByKind", { words: "fig" }), [], "a row without the field is in no search of it");
|
|
1169
|
+
}
|
|
1170
|
+
},
|
|
1171
|
+
{
|
|
1172
|
+
name: "a watched query that is refused is told why, and is given no value",
|
|
1173
|
+
async run(db) {
|
|
1174
|
+
const { me } = caller(db);
|
|
1175
|
+
let seen = "nothing", why = null;
|
|
1176
|
+
const stop = me.watch("notes.secret", {}, (value) => {
|
|
1177
|
+
seen = value;
|
|
1178
|
+
}, (error) => {
|
|
1179
|
+
why = error;
|
|
1180
|
+
});
|
|
1181
|
+
try {
|
|
1182
|
+
await until(() => why !== null, 5e3, "its refusal");
|
|
1183
|
+
eq([
|
|
1184
|
+
why instanceof DatabaseError,
|
|
1185
|
+
why.code,
|
|
1186
|
+
seen
|
|
1187
|
+
], [
|
|
1188
|
+
true,
|
|
1189
|
+
"internal",
|
|
1190
|
+
"nothing"
|
|
1191
|
+
], "the refusal and the value");
|
|
1192
|
+
} finally {
|
|
1193
|
+
stop();
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
1196
|
+
},
|
|
1197
|
+
{
|
|
1198
|
+
name: "a second watch of a query already refused is told why too",
|
|
1199
|
+
async run(db) {
|
|
1200
|
+
const { me } = caller(db);
|
|
1201
|
+
const told = [];
|
|
1202
|
+
const first = me.watch("notes.secret", {}, () => {
|
|
1203
|
+
told.push("a value");
|
|
1204
|
+
}, (error) => {
|
|
1205
|
+
told.push(`first ${error.code}`);
|
|
1206
|
+
});
|
|
1207
|
+
try {
|
|
1208
|
+
await until(() => told.length === 1, 5e3, "the first refusal");
|
|
1209
|
+
const second = me.watch("notes.secret", {}, () => {
|
|
1210
|
+
told.push("a value");
|
|
1211
|
+
}, (error) => {
|
|
1212
|
+
told.push(`second ${error.code}`);
|
|
1213
|
+
});
|
|
1214
|
+
try {
|
|
1215
|
+
await until(() => told.length === 2, 5e3, "the second refusal");
|
|
1216
|
+
eq(told, ["first internal", "second internal"], "what the watches were told");
|
|
1217
|
+
} finally {
|
|
1218
|
+
second();
|
|
1219
|
+
}
|
|
1220
|
+
} finally {
|
|
1221
|
+
first();
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
},
|
|
1225
|
+
{
|
|
1226
|
+
name: "a watched query refused after a change is told why, and is given its value again once the change is undone",
|
|
1227
|
+
async run(db) {
|
|
1228
|
+
const { me } = caller(db);
|
|
1229
|
+
const told = [];
|
|
1230
|
+
const stop = me.watch("notes.clean", {}, (value) => {
|
|
1231
|
+
told.push(value);
|
|
1232
|
+
}, (error) => {
|
|
1233
|
+
told.push(error instanceof DatabaseError ? `refused ${error.code}` : String(error));
|
|
1234
|
+
});
|
|
1235
|
+
try {
|
|
1236
|
+
await until(() => told.length === 1, 5e3, "its first value");
|
|
1237
|
+
const id = await me.mutation("notes.add", { text: "forbidden" });
|
|
1238
|
+
await until(() => told.length === 2, 5e3, "its refusal");
|
|
1239
|
+
await me.mutation("notes.remove", { id });
|
|
1240
|
+
await until(() => told.length === 3, 5e3, "its value again");
|
|
1241
|
+
eq(told, [
|
|
1242
|
+
[],
|
|
1243
|
+
"refused refused",
|
|
1244
|
+
[]
|
|
1245
|
+
], "what the watch was told");
|
|
1246
|
+
} finally {
|
|
1247
|
+
stop();
|
|
1248
|
+
}
|
|
1249
|
+
}
|
|
1250
|
+
},
|
|
1251
|
+
{
|
|
1252
|
+
name: "an id carries its table: get, patch, replace and delete take it alone or after the table, insert takes the table first, and normalizeId checks an id's table",
|
|
1253
|
+
async run(db) {
|
|
1254
|
+
const { me } = caller(db);
|
|
1255
|
+
eq(await me.mutation("shapes.byId", { label: "a" }), {
|
|
1256
|
+
read: "a",
|
|
1257
|
+
patched: 2,
|
|
1258
|
+
replaced: ["a!", 3],
|
|
1259
|
+
named: "named",
|
|
1260
|
+
namedGone: null,
|
|
1261
|
+
normalized: true,
|
|
1262
|
+
elsewhere: null,
|
|
1263
|
+
junk: null,
|
|
1264
|
+
gone: null
|
|
1265
|
+
}, "the calls by id");
|
|
1266
|
+
}
|
|
1267
|
+
},
|
|
1268
|
+
{
|
|
1269
|
+
name: "an id the Database mints has the id form and round-trips: get, patch, normalizeId, a cursor and delete",
|
|
1270
|
+
async run(db) {
|
|
1271
|
+
const { me } = caller(db);
|
|
1272
|
+
const made = [await me.mutation("shapes.minted", { label: "minted" }), await me.mutation("shapes.minted", { label: "minted" })];
|
|
1273
|
+
let hash = 2166136261;
|
|
1274
|
+
for (const unit of "shapes") hash = Math.imul(hash ^ unit.charCodeAt(0), 16777619) >>> 0;
|
|
1275
|
+
const code = hash.toString(36).padStart(7, "0");
|
|
1276
|
+
for (const m of made) {
|
|
1277
|
+
eq(/^[0-9a-z]{16,256}$/u.test(m.id) && m.id.startsWith(code), true, `${m.id}: its table's code ${code}, then base 36, 16 to 256 characters`);
|
|
1278
|
+
eq([
|
|
1279
|
+
m.n,
|
|
1280
|
+
m.normalized,
|
|
1281
|
+
m.elsewhere,
|
|
1282
|
+
m.punctuated
|
|
1283
|
+
], [
|
|
1284
|
+
2,
|
|
1285
|
+
m.id,
|
|
1286
|
+
null,
|
|
1287
|
+
null
|
|
1288
|
+
], "read after its patch, normalized for its table, not for another nor once punctuated");
|
|
1289
|
+
}
|
|
1290
|
+
await refusedWith(() => me.mutation("shapes.dropMinted", { id: `${made[0].id.slice(0, -1)}~` }), "invalid", "a punctuated id for v.id(\"shapes\")");
|
|
1291
|
+
const first = await me.query("shapes.mintedPage", {
|
|
1292
|
+
label: "minted",
|
|
1293
|
+
cursor: null
|
|
1294
|
+
});
|
|
1295
|
+
const second = await me.query("shapes.mintedPage", {
|
|
1296
|
+
label: "minted",
|
|
1297
|
+
cursor: first.cursor
|
|
1298
|
+
});
|
|
1299
|
+
eq([
|
|
1300
|
+
first.ids.length,
|
|
1301
|
+
first.isDone,
|
|
1302
|
+
second.ids.length,
|
|
1303
|
+
second.isDone
|
|
1304
|
+
], [
|
|
1305
|
+
1,
|
|
1306
|
+
false,
|
|
1307
|
+
1,
|
|
1308
|
+
true
|
|
1309
|
+
], "two pages of one row");
|
|
1310
|
+
eq([...first.ids, ...second.ids].sort(), made.map((m) => m.id).sort(), "each row once, the second after the first's cursor");
|
|
1311
|
+
for (const m of made) eq(await me.mutation("shapes.dropMinted", { id: m.id }), true, "deleted by it");
|
|
1312
|
+
}
|
|
1313
|
+
},
|
|
1314
|
+
{
|
|
1315
|
+
name: "an id of another table is refused, by v.id and by a call that names its table, and nothing is written",
|
|
1316
|
+
async run(db) {
|
|
1317
|
+
const { me } = caller(db);
|
|
1318
|
+
const note = await me.mutation("notes.add", { text: "kept" });
|
|
1319
|
+
await refusedWith(() => me.mutation("shapes.removeShape", { id: note }), "invalid", "a notes id for v.id(\"shapes\")");
|
|
1320
|
+
await refusedSaying(() => me.mutation("shapes.patchNamed", { id: note }), "refused", `ctx.db.patch("shapes", ${JSON.stringify(note)}): not an id of shapes`, "a notes id for patch(\"shapes\", id)");
|
|
1321
|
+
eq(await me.query("notes.mine", {}), ["kept"], "the note");
|
|
1322
|
+
}
|
|
1323
|
+
},
|
|
1324
|
+
{
|
|
1325
|
+
name: "Convex's calls by id return promises, so .then works on them",
|
|
1326
|
+
async run(db) {
|
|
1327
|
+
const { me } = caller(db);
|
|
1328
|
+
eq(await me.mutation("shapes.thenable", {}), "then", "insert(...).then(get).then(label)");
|
|
1329
|
+
}
|
|
1330
|
+
},
|
|
1331
|
+
{
|
|
1332
|
+
name: "a filter is applied before a page is counted, so each page has numItems rows that pass, in index order",
|
|
1333
|
+
async run(db) {
|
|
1334
|
+
const { me } = caller(db);
|
|
1335
|
+
await me.mutation("shapes.seven", {});
|
|
1336
|
+
const first = await me.query("shapes.oddPage", { paginationOpts: {
|
|
1337
|
+
numItems: 2,
|
|
1338
|
+
cursor: null,
|
|
1339
|
+
id: 1
|
|
1340
|
+
} });
|
|
1341
|
+
eq([first.ns, first.isDone], [[1, 3], false], "the first page");
|
|
1342
|
+
const second = await me.query("shapes.oddPage", { paginationOpts: {
|
|
1343
|
+
numItems: 2,
|
|
1344
|
+
cursor: first.continueCursor,
|
|
1345
|
+
endCursor: null
|
|
1346
|
+
} });
|
|
1347
|
+
eq([second.ns, second.isDone], [[5, 7], true], "the last page");
|
|
1348
|
+
const end = await me.query("shapes.oddPage", { paginationOpts: {
|
|
1349
|
+
numItems: 2,
|
|
1350
|
+
cursor: second.continueCursor
|
|
1351
|
+
} });
|
|
1352
|
+
eq([end.ns, end.isDone], [[], true], "after the end");
|
|
1353
|
+
eq(await me.query("shapes.filtered", {}), {
|
|
1354
|
+
first: 3,
|
|
1355
|
+
taken: [6, 2],
|
|
1356
|
+
unique: 4,
|
|
1357
|
+
none: 0,
|
|
1358
|
+
counted: 25
|
|
1359
|
+
}, "each terminal, filtered, and for await");
|
|
1360
|
+
}
|
|
1361
|
+
},
|
|
1362
|
+
{
|
|
1363
|
+
name: "a page stops where its read budget is spent, short and not done, and the next page goes on from there",
|
|
1364
|
+
async run(db) {
|
|
1365
|
+
const { me } = caller(db);
|
|
1366
|
+
await me.mutation("shapes.seven", {});
|
|
1367
|
+
const pages = [];
|
|
1368
|
+
let cursor = null;
|
|
1369
|
+
for (let k = 0; k < 6; k += 1) {
|
|
1370
|
+
const page = await me.query("shapes.oddPage", { paginationOpts: {
|
|
1371
|
+
numItems: 2,
|
|
1372
|
+
cursor,
|
|
1373
|
+
maximumRowsRead: 2
|
|
1374
|
+
} });
|
|
1375
|
+
pages.push([page.ns, page.isDone]);
|
|
1376
|
+
if (page.isDone) break;
|
|
1377
|
+
cursor = page.continueCursor;
|
|
1378
|
+
}
|
|
1379
|
+
eq(pages, [
|
|
1380
|
+
[[1], false],
|
|
1381
|
+
[[3], false],
|
|
1382
|
+
[[5], false],
|
|
1383
|
+
[[7], true]
|
|
1384
|
+
], "pages of at most two rows read");
|
|
1385
|
+
const bytes = await me.query("shapes.oddPage", { paginationOpts: {
|
|
1386
|
+
numItems: 5,
|
|
1387
|
+
cursor: null,
|
|
1388
|
+
maximumBytesRead: 1
|
|
1389
|
+
} });
|
|
1390
|
+
eq([bytes.ns, bytes.isDone], [[1], false], "a page of at most one byte read: the first row only");
|
|
1391
|
+
}
|
|
1392
|
+
},
|
|
1393
|
+
{
|
|
1394
|
+
name: "a filter that passes few rows reads on past many of them to fill its page",
|
|
1395
|
+
async run(db) {
|
|
1396
|
+
const { me } = caller(db);
|
|
1397
|
+
await me.mutation("shapes.many", {});
|
|
1398
|
+
const first = await me.query("shapes.hundreds", { paginationOpts: {
|
|
1399
|
+
numItems: 2,
|
|
1400
|
+
cursor: null
|
|
1401
|
+
} });
|
|
1402
|
+
eq([first.ns, first.isDone], [[100, 200], false], "the first page");
|
|
1403
|
+
const last = await me.query("shapes.hundreds", { paginationOpts: {
|
|
1404
|
+
numItems: 2,
|
|
1405
|
+
cursor: first.continueCursor
|
|
1406
|
+
} });
|
|
1407
|
+
eq([last.ns, last.isDone], [[300], true], "the last page");
|
|
1408
|
+
}
|
|
1409
|
+
},
|
|
1410
|
+
{
|
|
1411
|
+
name: "v.null() takes only null and v.any() any value, and an index orders a null field before an id",
|
|
1412
|
+
async run(db) {
|
|
1413
|
+
const { me } = caller(db);
|
|
1414
|
+
eq(await me.mutation("shapes.family", {
|
|
1415
|
+
kind: { deep: [1, "two"] },
|
|
1416
|
+
none: null
|
|
1417
|
+
}), {
|
|
1418
|
+
order: ["root", "child"],
|
|
1419
|
+
kind: { deep: [1, "two"] }
|
|
1420
|
+
}, "the rows in index order");
|
|
1421
|
+
await refusedWith(() => me.mutation("shapes.family", {
|
|
1422
|
+
kind: 1,
|
|
1423
|
+
none: 0
|
|
1424
|
+
}), "invalid", "a value for v.null()");
|
|
1425
|
+
}
|
|
1426
|
+
},
|
|
1427
|
+
{
|
|
1428
|
+
name: "a refused one-shot query keeps the message the function threw",
|
|
1429
|
+
async run(db) {
|
|
1430
|
+
const { me } = caller(db);
|
|
1431
|
+
await me.mutation("shapes.seven", {});
|
|
1432
|
+
await refusedSaying(() => me.query("shapes.twoOfThem", {}), "refused", "unique: the query returned more than one document", "unique over seven rows");
|
|
1433
|
+
}
|
|
1434
|
+
},
|
|
1435
|
+
{
|
|
1436
|
+
name: "bytes from a client are an ArrayBuffer or a typed array, never a base64 string, and a function is given an ArrayBuffer, also when an action passes it on",
|
|
1437
|
+
async run(db) {
|
|
1438
|
+
const { me } = caller(db);
|
|
1439
|
+
eq(await me.mutation("shapes.bytes", {
|
|
1440
|
+
data: new Uint8Array([
|
|
1441
|
+
0,
|
|
1442
|
+
255,
|
|
1443
|
+
7
|
|
1444
|
+
]).buffer,
|
|
1445
|
+
more: [new Uint8Array([1, 2])]
|
|
1446
|
+
}), {
|
|
1447
|
+
buffer: true,
|
|
1448
|
+
data: [
|
|
1449
|
+
0,
|
|
1450
|
+
255,
|
|
1451
|
+
7
|
|
1452
|
+
],
|
|
1453
|
+
more: [[1, 2]]
|
|
1454
|
+
}, "an ArrayBuffer and a typed array");
|
|
1455
|
+
await refusedWith(() => me.mutation("shapes.bytes", { data: "AP8H" }), "invalid", "a base64 string under v.bytes()");
|
|
1456
|
+
eq(await me.action("shapes.bytesOnward", { data: new Uint8Array([5, 6]) }), {
|
|
1457
|
+
buffer: true,
|
|
1458
|
+
data: [5, 6],
|
|
1459
|
+
more: [[9]]
|
|
1460
|
+
}, "from an action in the same file");
|
|
1461
|
+
}
|
|
1462
|
+
}
|
|
1463
|
+
];
|
|
1464
|
+
/** A request's body may be at most this many bytes: 20 MiB. A larger one is refused 413 before an HTTP action runs. */
|
|
1465
|
+
const HTTP_BODY_LIMIT = 20971520;
|
|
1466
|
+
const bytesOf = async (response) => [...new Uint8Array(await response.arrayBuffer())];
|
|
1467
|
+
const ALL_BYTES = Array.from({ length: 256 }, (_, k) => k);
|
|
1468
|
+
const BROWSER = "http://app.example";
|
|
1469
|
+
/**
|
|
1470
|
+
* What a Database's functions reach beyond its rows: its files, its HTTP actions, the environment's
|
|
1471
|
+
* values and the network. Each case is given a client, as above, and the Database's `DatabaseWeb`.
|
|
1472
|
+
*
|
|
1473
|
+
* for (const item of databaseServerContract) test(item.name, () => item.run(client, web))
|
|
1474
|
+
*/
|
|
1475
|
+
const databaseServerContract = [
|
|
1476
|
+
{
|
|
1477
|
+
name: "a file posted to an upload URL reads back at its URL and as a Blob, and once deleted is gone",
|
|
1478
|
+
async run(db, web) {
|
|
1479
|
+
const { me } = caller(db);
|
|
1480
|
+
const upload = await me.mutation("files.uploadUrl", {});
|
|
1481
|
+
const posted = await web.fetch(new Request(upload, {
|
|
1482
|
+
method: "POST",
|
|
1483
|
+
headers: { "content-type": "application/x-contract" },
|
|
1484
|
+
body: "hello"
|
|
1485
|
+
}));
|
|
1486
|
+
eq(posted.status, 200, "the upload's status");
|
|
1487
|
+
const { storageId } = await posted.json();
|
|
1488
|
+
eq(typeof storageId, "string", "the upload's storageId");
|
|
1489
|
+
eq((await web.fetch(new Request(upload, {
|
|
1490
|
+
method: "POST",
|
|
1491
|
+
headers: { "content-type": "application/x-contract" },
|
|
1492
|
+
body: "again"
|
|
1493
|
+
}))).status, 404, "a second upload to the same URL");
|
|
1494
|
+
const url = await me.query("files.url", { id: storageId });
|
|
1495
|
+
const read = await web.fetch(new Request(url));
|
|
1496
|
+
eq([
|
|
1497
|
+
read.status,
|
|
1498
|
+
read.headers.get("content-type"),
|
|
1499
|
+
await read.text()
|
|
1500
|
+
], [
|
|
1501
|
+
200,
|
|
1502
|
+
"application/x-contract",
|
|
1503
|
+
"hello"
|
|
1504
|
+
], "the file at its URL");
|
|
1505
|
+
eq(await me.action("actions.read", { id: storageId }), {
|
|
1506
|
+
type: "application/x-contract",
|
|
1507
|
+
bytes: [
|
|
1508
|
+
104,
|
|
1509
|
+
101,
|
|
1510
|
+
108,
|
|
1511
|
+
108,
|
|
1512
|
+
111
|
|
1513
|
+
]
|
|
1514
|
+
}, "the file as a Blob");
|
|
1515
|
+
await me.mutation("files.remove", { id: storageId });
|
|
1516
|
+
eq(await me.query("files.url", { id: storageId }), null, "its URL after the delete");
|
|
1517
|
+
eq((await web.fetch(new Request(url))).status, 404, "the old URL after the delete");
|
|
1518
|
+
eq(await me.action("actions.read", { id: storageId }), null, "the Blob after the delete");
|
|
1519
|
+
}
|
|
1520
|
+
},
|
|
1521
|
+
{
|
|
1522
|
+
name: "a browser on another origin may upload to an upload URL and read the storageId and the file",
|
|
1523
|
+
async run(db, web) {
|
|
1524
|
+
const { me } = caller(db);
|
|
1525
|
+
const upload = await me.mutation("files.uploadUrl", {});
|
|
1526
|
+
const preflight = await web.fetch(new Request(upload, {
|
|
1527
|
+
method: "OPTIONS",
|
|
1528
|
+
headers: {
|
|
1529
|
+
origin: BROWSER,
|
|
1530
|
+
"access-control-request-method": "POST",
|
|
1531
|
+
"access-control-request-headers": "content-type"
|
|
1532
|
+
}
|
|
1533
|
+
}));
|
|
1534
|
+
eq([preflight.status >= 200 && preflight.status < 300, preflight.headers.get("access-control-allow-origin")], [true, "*"], "the preflight");
|
|
1535
|
+
eq((preflight.headers.get("access-control-allow-methods") ?? "").split(/\s*,\s*/u).includes("POST"), true, "the preflight's methods include POST");
|
|
1536
|
+
eq((preflight.headers.get("access-control-allow-headers") ?? "").toLowerCase().split(/\s*,\s*/u).includes("content-type"), true, "the preflight's headers include content-type");
|
|
1537
|
+
const posted = await web.fetch(new Request(upload, {
|
|
1538
|
+
method: "POST",
|
|
1539
|
+
headers: {
|
|
1540
|
+
origin: BROWSER,
|
|
1541
|
+
"content-type": "image/png"
|
|
1542
|
+
},
|
|
1543
|
+
body: new Uint8Array([
|
|
1544
|
+
137,
|
|
1545
|
+
80,
|
|
1546
|
+
78,
|
|
1547
|
+
71
|
|
1548
|
+
])
|
|
1549
|
+
}));
|
|
1550
|
+
eq([posted.status, posted.headers.get("access-control-allow-origin")], [200, "*"], "the upload");
|
|
1551
|
+
const { storageId } = await posted.json();
|
|
1552
|
+
const read = await web.fetch(new Request(await me.query("files.url", { id: storageId }), { headers: { origin: BROWSER } }));
|
|
1553
|
+
eq([
|
|
1554
|
+
read.status,
|
|
1555
|
+
read.headers.get("access-control-allow-origin"),
|
|
1556
|
+
await bytesOf(read)
|
|
1557
|
+
], [
|
|
1558
|
+
200,
|
|
1559
|
+
"*",
|
|
1560
|
+
[
|
|
1561
|
+
137,
|
|
1562
|
+
80,
|
|
1563
|
+
78,
|
|
1564
|
+
71
|
|
1565
|
+
]
|
|
1566
|
+
], "the file read from the browser");
|
|
1567
|
+
}
|
|
1568
|
+
},
|
|
1569
|
+
{
|
|
1570
|
+
name: "a file an action stores keeps its bytes and type exactly, and an action's delete removes it",
|
|
1571
|
+
async run(db, web) {
|
|
1572
|
+
const { me } = caller(db);
|
|
1573
|
+
const id = await me.action("actions.store", {
|
|
1574
|
+
bytes: [
|
|
1575
|
+
0,
|
|
1576
|
+
255,
|
|
1577
|
+
128,
|
|
1578
|
+
10,
|
|
1579
|
+
13
|
|
1580
|
+
],
|
|
1581
|
+
type: "application/x-contract"
|
|
1582
|
+
});
|
|
1583
|
+
eq(await me.action("actions.read", { id }), {
|
|
1584
|
+
type: "application/x-contract",
|
|
1585
|
+
bytes: [
|
|
1586
|
+
0,
|
|
1587
|
+
255,
|
|
1588
|
+
128,
|
|
1589
|
+
10,
|
|
1590
|
+
13
|
|
1591
|
+
]
|
|
1592
|
+
}, "the Blob read back");
|
|
1593
|
+
const read = await web.fetch(new Request(await me.query("files.url", { id })));
|
|
1594
|
+
eq([
|
|
1595
|
+
read.status,
|
|
1596
|
+
read.headers.get("content-type"),
|
|
1597
|
+
await bytesOf(read)
|
|
1598
|
+
], [
|
|
1599
|
+
200,
|
|
1600
|
+
"application/x-contract",
|
|
1601
|
+
[
|
|
1602
|
+
0,
|
|
1603
|
+
255,
|
|
1604
|
+
128,
|
|
1605
|
+
10,
|
|
1606
|
+
13
|
|
1607
|
+
]
|
|
1608
|
+
], "the file at its URL");
|
|
1609
|
+
await me.action("actions.remove", { id });
|
|
1610
|
+
eq(await me.query("files.url", { id }), null, "its URL after the delete");
|
|
1611
|
+
}
|
|
1612
|
+
},
|
|
1613
|
+
{
|
|
1614
|
+
name: "a store with a sha256 keeps the file only when its bytes have that hex SHA-256",
|
|
1615
|
+
async run(db) {
|
|
1616
|
+
const { me } = caller(db);
|
|
1617
|
+
const id = await me.action("actions.storeChecked", {
|
|
1618
|
+
bytes: [
|
|
1619
|
+
1,
|
|
1620
|
+
2,
|
|
1621
|
+
3
|
|
1622
|
+
],
|
|
1623
|
+
sha256: "039058c6f2c0cb492c533b0a4d14ef77cc0f78abccced5287d84a1a2011cfb81"
|
|
1624
|
+
});
|
|
1625
|
+
eq((await me.action("actions.read", { id })).bytes, [
|
|
1626
|
+
1,
|
|
1627
|
+
2,
|
|
1628
|
+
3
|
|
1629
|
+
], "the file stored");
|
|
1630
|
+
await refusedWith(() => me.action("actions.storeChecked", {
|
|
1631
|
+
bytes: [
|
|
1632
|
+
1,
|
|
1633
|
+
2,
|
|
1634
|
+
3
|
|
1635
|
+
],
|
|
1636
|
+
sha256: "d4b29a968c40173638ded8d174c86957afa211be479cee020dba5dfe127d91ca"
|
|
1637
|
+
}), "invalid", "another file's SHA-256");
|
|
1638
|
+
}
|
|
1639
|
+
},
|
|
1640
|
+
{
|
|
1641
|
+
name: "a file deleted in a mutation is gone to the rest of it, and a watched query's URL turns null after any delete",
|
|
1642
|
+
async run(db) {
|
|
1643
|
+
const { me } = caller(db);
|
|
1644
|
+
const first = await me.action("actions.store", {
|
|
1645
|
+
bytes: [1],
|
|
1646
|
+
type: "application/x-contract"
|
|
1647
|
+
});
|
|
1648
|
+
eq(await me.mutation("files.removeThenUrl", { id: first }), null, "its URL in the mutation that deleted it");
|
|
1649
|
+
for (const remove of [(id) => me.action("actions.remove", { id }), (id) => me.mutation("files.remove", { id })]) {
|
|
1650
|
+
const id = await me.action("actions.store", {
|
|
1651
|
+
bytes: [2],
|
|
1652
|
+
type: "application/x-contract"
|
|
1653
|
+
});
|
|
1654
|
+
let seen;
|
|
1655
|
+
const stop = me.watch("files.url", { id }, (value) => {
|
|
1656
|
+
seen = value;
|
|
1657
|
+
});
|
|
1658
|
+
try {
|
|
1659
|
+
await until(() => typeof seen === "string", 5e3, "the watched URL");
|
|
1660
|
+
await remove(id);
|
|
1661
|
+
await until(() => seen === null, 5e3, "the watched URL after the delete");
|
|
1662
|
+
} finally {
|
|
1663
|
+
stop();
|
|
1664
|
+
}
|
|
1665
|
+
}
|
|
1666
|
+
}
|
|
1667
|
+
},
|
|
1668
|
+
{
|
|
1669
|
+
name: "an HTTP action is given a POST whole and its own Response is returned, status and headers included",
|
|
1670
|
+
async run(_db, web) {
|
|
1671
|
+
const response = await web.fetch(new Request(`${web.httpUrl}/echo?x=1`, {
|
|
1672
|
+
method: "POST",
|
|
1673
|
+
headers: {
|
|
1674
|
+
"content-type": "application/json",
|
|
1675
|
+
"x-contract": "yes"
|
|
1676
|
+
},
|
|
1677
|
+
body: JSON.stringify({ text: "posted" })
|
|
1678
|
+
}));
|
|
1679
|
+
eq([
|
|
1680
|
+
response.status,
|
|
1681
|
+
response.headers.get("x-answer"),
|
|
1682
|
+
response.headers.get("content-type")?.split(";")[0]
|
|
1683
|
+
], [
|
|
1684
|
+
201,
|
|
1685
|
+
"42",
|
|
1686
|
+
"application/json"
|
|
1687
|
+
], "the status and headers");
|
|
1688
|
+
eq(await response.json(), {
|
|
1689
|
+
method: "POST",
|
|
1690
|
+
x: "1",
|
|
1691
|
+
header: "yes",
|
|
1692
|
+
text: "posted"
|
|
1693
|
+
}, "the body");
|
|
1694
|
+
}
|
|
1695
|
+
},
|
|
1696
|
+
{
|
|
1697
|
+
name: "an HTTP action's caller is anonymous, never the server, unless the request carries a verified token",
|
|
1698
|
+
async run(db, web) {
|
|
1699
|
+
const { subject } = caller(db);
|
|
1700
|
+
eq(await (await web.fetch(new Request(`${web.httpUrl}/whoami`))).json(), {
|
|
1701
|
+
subject: null,
|
|
1702
|
+
server: false,
|
|
1703
|
+
nested: null
|
|
1704
|
+
}, "an anonymous request");
|
|
1705
|
+
eq(await (await web.fetch(new Request(`${web.httpUrl}/whoami`, { headers: { authorization: `Bearer ${await web.token(subject)}` } }))).json(), {
|
|
1706
|
+
subject,
|
|
1707
|
+
server: false,
|
|
1708
|
+
nested: subject
|
|
1709
|
+
}, "a signed-in request");
|
|
1710
|
+
eq(await (await web.fetch(new Request(`${web.httpUrl}/whoami`, { headers: { authorization: "Bearer not-a-token" } }))).json(), {
|
|
1711
|
+
subject: null,
|
|
1712
|
+
server: false,
|
|
1713
|
+
nested: null
|
|
1714
|
+
}, "a token that does not verify: anonymous");
|
|
1715
|
+
}
|
|
1716
|
+
},
|
|
1717
|
+
{
|
|
1718
|
+
name: "routes: an exact path before a prefix, the longest prefix first, HEAD takes GET, the root, and bytes exactly",
|
|
1719
|
+
async run(_db, web) {
|
|
1720
|
+
const text = async (path) => {
|
|
1721
|
+
const r = await web.fetch(new Request(`${web.httpUrl}${path}`));
|
|
1722
|
+
return [r.status, await r.text()];
|
|
1723
|
+
};
|
|
1724
|
+
eq(await text("/"), [200, "root"], "the root");
|
|
1725
|
+
eq(await text("/bytes/exact"), [200, "exact"], "an exact path inside a prefix");
|
|
1726
|
+
eq(await text("/bytes/deep/x"), [200, "deep"], "the longer prefix");
|
|
1727
|
+
const response = await web.fetch(new Request(`${web.httpUrl}/bytes/7`));
|
|
1728
|
+
eq([
|
|
1729
|
+
response.status,
|
|
1730
|
+
response.headers.get("content-type"),
|
|
1731
|
+
await bytesOf(response)
|
|
1732
|
+
], [
|
|
1733
|
+
200,
|
|
1734
|
+
"application/octet-stream",
|
|
1735
|
+
[
|
|
1736
|
+
0,
|
|
1737
|
+
255,
|
|
1738
|
+
128,
|
|
1739
|
+
7
|
|
1740
|
+
]
|
|
1741
|
+
], "the bytes");
|
|
1742
|
+
const head = await web.fetch(new Request(`${web.httpUrl}/bytes/7`, { method: "HEAD" }));
|
|
1743
|
+
eq([head.status, (await head.arrayBuffer()).byteLength], [200, 0], "HEAD of a GET route");
|
|
1744
|
+
eq((await web.fetch(new Request(`${web.httpUrl}/echo`))).status, 404, "a GET of a POST route");
|
|
1745
|
+
eq((await web.fetch(new Request(`${web.httpUrl}/nothing`, {
|
|
1746
|
+
method: "POST",
|
|
1747
|
+
body: "{}"
|
|
1748
|
+
}))).status, 404, "a path with no route");
|
|
1749
|
+
}
|
|
1750
|
+
},
|
|
1751
|
+
{
|
|
1752
|
+
name: "an HTTP action's repeated response headers arrive apart",
|
|
1753
|
+
async run(_db, web) {
|
|
1754
|
+
const response = await web.fetch(new Request(`${web.httpUrl}/cookies`));
|
|
1755
|
+
eq([response.status, response.headers.getSetCookie()], [200, ["a=1", "b=2"]], "the set-cookie headers");
|
|
1756
|
+
}
|
|
1757
|
+
},
|
|
1758
|
+
{
|
|
1759
|
+
name: "an HTTP action takes a body of exactly 20 MiB, and a larger one is refused 413 before it runs",
|
|
1760
|
+
async run(_db, web) {
|
|
1761
|
+
const at = await web.fetch(new Request(`${web.httpUrl}/size`, {
|
|
1762
|
+
method: "POST",
|
|
1763
|
+
body: new Uint8Array(HTTP_BODY_LIMIT)
|
|
1764
|
+
}));
|
|
1765
|
+
eq([at.status, await at.text()], [200, "20971520"], "20 MiB");
|
|
1766
|
+
eq((await web.fetch(new Request(`${web.httpUrl}/size`, {
|
|
1767
|
+
method: "POST",
|
|
1768
|
+
body: /* @__PURE__ */ new Uint8Array(20971521)
|
|
1769
|
+
}))).status, 413, "a byte more");
|
|
1770
|
+
}
|
|
1771
|
+
},
|
|
1772
|
+
{
|
|
1773
|
+
name: "an action reads the environment's values from process.env; a name not set is undefined and not listed",
|
|
1774
|
+
async run(db) {
|
|
1775
|
+
const { me } = caller(db);
|
|
1776
|
+
eq(await me.action("actions.env", {}), {
|
|
1777
|
+
greeting: "hello from the environment",
|
|
1778
|
+
unsetIsUndefined: true,
|
|
1779
|
+
unsetListed: false
|
|
1780
|
+
}, "the values");
|
|
1781
|
+
}
|
|
1782
|
+
},
|
|
1783
|
+
{
|
|
1784
|
+
name: "an action's fetch sends bytes and returns the whole response: status, headers, arrayBuffer() and a stream",
|
|
1785
|
+
async run(db, web) {
|
|
1786
|
+
const { me } = caller(db);
|
|
1787
|
+
eq(await me.action("actions.echo", { url: web.echoUrl }), {
|
|
1788
|
+
status: 200,
|
|
1789
|
+
type: "application/octet-stream",
|
|
1790
|
+
bytes: ALL_BYTES,
|
|
1791
|
+
streamed: ALL_BYTES
|
|
1792
|
+
}, "the echo");
|
|
1793
|
+
}
|
|
1794
|
+
},
|
|
1795
|
+
{
|
|
1796
|
+
name: "an action's fetch reads back a response of 20 MiB, and a larger one is refused",
|
|
1797
|
+
async run(db, web) {
|
|
1798
|
+
const { me } = caller(db);
|
|
1799
|
+
eq(await me.action("actions.fetchSized", {
|
|
1800
|
+
url: web.echoUrl,
|
|
1801
|
+
bytes: 20971520
|
|
1802
|
+
}), 20971520, "exactly 20 MiB");
|
|
1803
|
+
await refusedWith(() => me.action("actions.fetchSized", {
|
|
1804
|
+
url: web.echoUrl,
|
|
1805
|
+
bytes: 20971521
|
|
1806
|
+
}), "invalid", "a byte over");
|
|
1807
|
+
}
|
|
1808
|
+
},
|
|
1809
|
+
{
|
|
1810
|
+
name: "an action's global fetch reaches the network as ctx.fetch does",
|
|
1811
|
+
async run(db, web) {
|
|
1812
|
+
const { me } = caller(db);
|
|
1813
|
+
eq(await me.action("actions.globalFetch", { url: web.echoUrl }), [
|
|
1814
|
+
7,
|
|
1815
|
+
8,
|
|
1816
|
+
9
|
|
1817
|
+
], "the echo");
|
|
1818
|
+
}
|
|
1819
|
+
},
|
|
1820
|
+
{
|
|
1821
|
+
name: "runQuery, runMutation and runAction refuse a function of another kind, and nothing is written",
|
|
1822
|
+
async run(db) {
|
|
1823
|
+
const { me } = caller(db);
|
|
1824
|
+
await refusedWith(() => me.action("actions.wrongKind", { via: "runQuery" }), "no function", "a mutation handed to runQuery");
|
|
1825
|
+
await refusedWith(() => me.action("actions.wrongKind", { via: "runMutation" }), "no function", "a query handed to runMutation");
|
|
1826
|
+
await refusedWith(() => me.action("actions.wrongKind", { via: "runAction" }), "no function", "a mutation handed to runAction");
|
|
1827
|
+
eq(await me.query("notes.mine", {}), [], "the notes");
|
|
1828
|
+
}
|
|
1829
|
+
},
|
|
1830
|
+
{
|
|
1831
|
+
name: "bytes a function is given are kept in a v.bytes() field and come back to the caller as bytes",
|
|
1832
|
+
async run(db) {
|
|
1833
|
+
const { me } = caller(db);
|
|
1834
|
+
eq(plain(await me.mutation("shapes.keepBytes", { data: new Uint8Array([
|
|
1835
|
+
0,
|
|
1836
|
+
255,
|
|
1837
|
+
7
|
|
1838
|
+
]).buffer })), {
|
|
1839
|
+
stored: { bytes: [
|
|
1840
|
+
0,
|
|
1841
|
+
255,
|
|
1842
|
+
7
|
|
1843
|
+
] },
|
|
1844
|
+
echoed: { bytes: [
|
|
1845
|
+
0,
|
|
1846
|
+
255,
|
|
1847
|
+
7
|
|
1848
|
+
] }
|
|
1849
|
+
}, "sent as an ArrayBuffer");
|
|
1850
|
+
}
|
|
1851
|
+
},
|
|
1852
|
+
{
|
|
1853
|
+
name: "bytes stay bytes and strings stay strings, under v.any() and in a union with v.string()",
|
|
1854
|
+
async run(db) {
|
|
1855
|
+
const { me } = caller(db);
|
|
1856
|
+
eq(plain(await me.mutation("shapes.anyBytes", {
|
|
1857
|
+
x: new Uint8Array([1, 2]).buffer,
|
|
1858
|
+
s: "AP8H"
|
|
1859
|
+
})), {
|
|
1860
|
+
x: { bytes: [1, 2] },
|
|
1861
|
+
s: "AP8H",
|
|
1862
|
+
xIsBytes: true,
|
|
1863
|
+
sIsBytes: false
|
|
1864
|
+
}, "bytes under v.any(), a string in the union");
|
|
1865
|
+
eq(plain(await me.mutation("shapes.anyBytes", {
|
|
1866
|
+
x: "AP8H",
|
|
1867
|
+
s: new Uint8Array([3]).buffer
|
|
1868
|
+
})), {
|
|
1869
|
+
x: "AP8H",
|
|
1870
|
+
s: { bytes: [3] },
|
|
1871
|
+
xIsBytes: false,
|
|
1872
|
+
sIsBytes: true
|
|
1873
|
+
}, "a string under v.any(), bytes in the union");
|
|
1874
|
+
}
|
|
1875
|
+
},
|
|
1876
|
+
{
|
|
1877
|
+
name: "bytes a nested call returns reach its caller as bytes, and a v.string() field refuses bytes",
|
|
1878
|
+
async run(db) {
|
|
1879
|
+
const { me } = caller(db);
|
|
1880
|
+
eq(await me.action("shapes.nestedBytes", { data: new Uint8Array([4]).buffer }), {
|
|
1881
|
+
stored: true,
|
|
1882
|
+
echoed: true
|
|
1883
|
+
}, "runMutation's answer");
|
|
1884
|
+
await refusedWith(() => me.mutation("shapes.bytesAsLabel", { data: new Uint8Array([5]).buffer }), "invalid", "bytes for a string field");
|
|
1885
|
+
}
|
|
1886
|
+
},
|
|
1887
|
+
{
|
|
1888
|
+
name: "an object whose only key is $bytes is reserved for bytes, and is refused as data",
|
|
1889
|
+
async run(db) {
|
|
1890
|
+
const { me } = caller(db);
|
|
1891
|
+
let why = "";
|
|
1892
|
+
try {
|
|
1893
|
+
await me.mutation("shapes.anyBytes", {
|
|
1894
|
+
x: { $bytes: "AP8H" },
|
|
1895
|
+
s: ""
|
|
1896
|
+
});
|
|
1897
|
+
} catch (error) {
|
|
1898
|
+
why = error.message;
|
|
1899
|
+
}
|
|
1900
|
+
eq(why, "an object whose only key is $bytes is how bytes travel, so one can't be sent as data", "why a hand-made tag is refused");
|
|
1901
|
+
}
|
|
1902
|
+
},
|
|
1903
|
+
{
|
|
1904
|
+
name: "a watch of a query whose answer is bytes is told when only the bytes change",
|
|
1905
|
+
async run(db) {
|
|
1906
|
+
const { me } = caller(db);
|
|
1907
|
+
await me.mutation("shapes.setBlob", { data: new Uint8Array([1]).buffer });
|
|
1908
|
+
const told = [];
|
|
1909
|
+
const stop = me.watch("shapes.blobOf", {}, (value) => {
|
|
1910
|
+
told.push(plain(value));
|
|
1911
|
+
});
|
|
1912
|
+
try {
|
|
1913
|
+
await until(() => told.length === 1, 5e3, "the first value");
|
|
1914
|
+
await me.mutation("shapes.setBlob", { data: new Uint8Array([2]).buffer });
|
|
1915
|
+
await until(() => told.length === 2, 5e3, "the changed bytes");
|
|
1916
|
+
eq(told, [{ bytes: [1] }, { bytes: [2] }], "what the watch was told");
|
|
1917
|
+
} finally {
|
|
1918
|
+
stop();
|
|
1919
|
+
}
|
|
1920
|
+
}
|
|
1921
|
+
},
|
|
1922
|
+
{
|
|
1923
|
+
name: "bytes written as a typed array, or under v.any(), read back as ArrayBuffers",
|
|
1924
|
+
async run(db) {
|
|
1925
|
+
const { me } = caller(db);
|
|
1926
|
+
eq(await me.mutation("shapes.keepTyped", {}), {
|
|
1927
|
+
blobIsBuffer: true,
|
|
1928
|
+
kindIsBuffer: true,
|
|
1929
|
+
blob: [7, 8],
|
|
1930
|
+
kind: [9]
|
|
1931
|
+
}, "the row read back");
|
|
1932
|
+
}
|
|
1933
|
+
},
|
|
1934
|
+
{
|
|
1935
|
+
name: "two watches of one query with different bytes are two queries",
|
|
1936
|
+
async run(db) {
|
|
1937
|
+
const { me } = caller(db);
|
|
1938
|
+
const seen = {};
|
|
1939
|
+
const one = me.watch("shapes.bytesOf", { data: new Uint8Array([1]).buffer }, (value) => {
|
|
1940
|
+
seen.one = value;
|
|
1941
|
+
});
|
|
1942
|
+
const two = me.watch("shapes.bytesOf", { data: new Uint8Array([2]).buffer }, (value) => {
|
|
1943
|
+
seen.two = value;
|
|
1944
|
+
});
|
|
1945
|
+
try {
|
|
1946
|
+
await until(() => seen.one !== void 0 && seen.two !== void 0, 5e3, "both values");
|
|
1947
|
+
eq([seen.one, seen.two], [[1], [2]], "each watch's value");
|
|
1948
|
+
} finally {
|
|
1949
|
+
one();
|
|
1950
|
+
two();
|
|
1951
|
+
}
|
|
1952
|
+
}
|
|
1953
|
+
},
|
|
1954
|
+
{
|
|
1955
|
+
name: "a stored file's metadata, from getMetadata and from db.system.get, is its size, SHA-256 and type, and null once deleted",
|
|
1956
|
+
async run(db) {
|
|
1957
|
+
const { me } = caller(db);
|
|
1958
|
+
const id = await me.action("actions.store", {
|
|
1959
|
+
bytes: [
|
|
1960
|
+
104,
|
|
1961
|
+
101,
|
|
1962
|
+
108,
|
|
1963
|
+
108,
|
|
1964
|
+
111
|
|
1965
|
+
],
|
|
1966
|
+
type: "application/x-contract"
|
|
1967
|
+
});
|
|
1968
|
+
const sha256 = "LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ=";
|
|
1969
|
+
eq(await me.query("shapes.metadata", { id }), {
|
|
1970
|
+
normalized: true,
|
|
1971
|
+
metadata: {
|
|
1972
|
+
storageId: id,
|
|
1973
|
+
sha256,
|
|
1974
|
+
size: 5,
|
|
1975
|
+
contentType: "application/x-contract"
|
|
1976
|
+
},
|
|
1977
|
+
system: {
|
|
1978
|
+
id: true,
|
|
1979
|
+
sha256,
|
|
1980
|
+
size: 5,
|
|
1981
|
+
contentType: "application/x-contract",
|
|
1982
|
+
time: "number"
|
|
1983
|
+
}
|
|
1984
|
+
}, "the metadata of \"hello\"");
|
|
1985
|
+
await me.mutation("files.remove", { id });
|
|
1986
|
+
eq(await me.query("shapes.metadata", { id }), {
|
|
1987
|
+
normalized: true,
|
|
1988
|
+
metadata: null,
|
|
1989
|
+
system: null
|
|
1990
|
+
}, "the metadata after the delete");
|
|
1991
|
+
}
|
|
1992
|
+
}
|
|
1993
|
+
];
|
|
1994
|
+
/**
|
|
1995
|
+
* What the Stand-in serves ahead of the Database, which refuses vector indexes today: the cases the
|
|
1996
|
+
* engine passes once it serves them, run with `contractAheadFolder`'s build.
|
|
1997
|
+
*/
|
|
1998
|
+
const databaseAheadContract = [
|
|
1999
|
+
{
|
|
2000
|
+
name: "a vector search gives the nearest rows first, each its id and cosine score",
|
|
2001
|
+
async run(db) {
|
|
2002
|
+
const { me } = caller(db);
|
|
2003
|
+
const east = await me.mutation("places.add", { at: [1, 0] });
|
|
2004
|
+
const north = await me.mutation("places.add", { at: [0, 1] });
|
|
2005
|
+
await caller(db).me.mutation("places.add", { at: [1, 0] });
|
|
2006
|
+
eq(await me.action("nearest.places", {
|
|
2007
|
+
at: [1, 0],
|
|
2008
|
+
limit: 10
|
|
2009
|
+
}), [{
|
|
2010
|
+
_id: east,
|
|
2011
|
+
_score: 1
|
|
2012
|
+
}, {
|
|
2013
|
+
_id: north,
|
|
2014
|
+
_score: 0
|
|
2015
|
+
}], "both places");
|
|
2016
|
+
eq(await me.action("nearest.places", {
|
|
2017
|
+
at: [0, 2],
|
|
2018
|
+
limit: 1
|
|
2019
|
+
}), [{
|
|
2020
|
+
_id: north,
|
|
2021
|
+
_score: 1
|
|
2022
|
+
}], "the nearest");
|
|
2023
|
+
}
|
|
2024
|
+
},
|
|
2025
|
+
{
|
|
2026
|
+
name: "a vector search finds a row where its vector is now, and nothing for a filter that allows nothing",
|
|
2027
|
+
async run(db) {
|
|
2028
|
+
const { subject, me } = caller(db);
|
|
2029
|
+
const east = await me.mutation("places.add", { at: [1, 0] });
|
|
2030
|
+
const north = await me.mutation("places.add", { at: [0, 1] });
|
|
2031
|
+
await me.mutation("places.move", {
|
|
2032
|
+
id: north,
|
|
2033
|
+
at: [-1, 0]
|
|
2034
|
+
});
|
|
2035
|
+
eq(await me.action("nearest.places", {
|
|
2036
|
+
at: [1, 0],
|
|
2037
|
+
limit: 10
|
|
2038
|
+
}), [{
|
|
2039
|
+
_id: east,
|
|
2040
|
+
_score: 1
|
|
2041
|
+
}, {
|
|
2042
|
+
_id: north,
|
|
2043
|
+
_score: -1
|
|
2044
|
+
}], "after the move");
|
|
2045
|
+
await me.mutation("places.remove", { id: east });
|
|
2046
|
+
eq(await me.action("nearest.among", {
|
|
2047
|
+
at: [1, 0],
|
|
2048
|
+
owners: [subject]
|
|
2049
|
+
}), [{
|
|
2050
|
+
_id: north,
|
|
2051
|
+
_score: -1
|
|
2052
|
+
}], "after the delete");
|
|
2053
|
+
eq(await me.action("nearest.among", {
|
|
2054
|
+
at: [1, 0],
|
|
2055
|
+
owners: []
|
|
2056
|
+
}), [], "no owner allowed");
|
|
2057
|
+
}
|
|
2058
|
+
},
|
|
2059
|
+
{
|
|
2060
|
+
name: "a vector's score is its direction's, however long or short it is",
|
|
2061
|
+
async run(db) {
|
|
2062
|
+
const { me } = caller(db);
|
|
2063
|
+
const long = await me.mutation("places.add", { at: [1e100, 0] });
|
|
2064
|
+
const short = await me.mutation("places.add", { at: [0, 1e-100] });
|
|
2065
|
+
eq(await me.action("nearest.places", {
|
|
2066
|
+
at: [1, 0],
|
|
2067
|
+
limit: 10
|
|
2068
|
+
}), [{
|
|
2069
|
+
_id: long,
|
|
2070
|
+
_score: 1
|
|
2071
|
+
}, {
|
|
2072
|
+
_id: short,
|
|
2073
|
+
_score: 0
|
|
2074
|
+
}], "the places");
|
|
2075
|
+
}
|
|
2076
|
+
}
|
|
2077
|
+
];
|
|
2078
|
+
const MIB = 1048576;
|
|
2079
|
+
/** A module of exactly `bytes` bytes. */
|
|
2080
|
+
const sized = (bytes) => `export const s = "${"x".repeat(bytes - 21)}";\n`;
|
|
2081
|
+
const admitted = async (deploy, files, what) => {
|
|
2082
|
+
const why = await deploy(files);
|
|
2083
|
+
if (why !== null) throw new Error(`${what}: refused (${why}), want it taken`);
|
|
2084
|
+
};
|
|
2085
|
+
const refused = async (deploy, files, what) => {
|
|
2086
|
+
if (await deploy(files) === null) throw new Error(`${what}: taken, want it refused`);
|
|
2087
|
+
};
|
|
2088
|
+
/** The limits a deploy keeps, each at its edge: 1 MiB a file, 8 MiB in all and 4000 files. */
|
|
2089
|
+
const databaseDeployContract = [
|
|
2090
|
+
{
|
|
2091
|
+
name: "a deploy takes a file of exactly 1 MiB, and refuses one a byte larger",
|
|
2092
|
+
async run(deploy) {
|
|
2093
|
+
await admitted(deploy, { "big.mjs": sized(MIB) }, "1 MiB");
|
|
2094
|
+
await refused(deploy, { "big.mjs": sized(1048577) }, "1 MiB and a byte");
|
|
2095
|
+
}
|
|
2096
|
+
},
|
|
2097
|
+
{
|
|
2098
|
+
name: "a deploy takes 8 MiB in all, and refuses a byte more",
|
|
2099
|
+
async run(deploy) {
|
|
2100
|
+
const eight = Object.fromEntries(Array.from({ length: 8 }, (_, i) => [`part${i}.mjs`, sized(MIB)]));
|
|
2101
|
+
await admitted(deploy, eight, "8 MiB");
|
|
2102
|
+
await refused(deploy, {
|
|
2103
|
+
...eight,
|
|
2104
|
+
"more.mjs": "\n"
|
|
2105
|
+
}, "8 MiB and a byte");
|
|
2106
|
+
}
|
|
2107
|
+
},
|
|
2108
|
+
{
|
|
2109
|
+
name: "a deploy takes 4000 files, and refuses 4001",
|
|
2110
|
+
async run(deploy) {
|
|
2111
|
+
const files = (n) => Object.fromEntries(Array.from({ length: n }, (_, i) => [`m${i}.mjs`, "export {};\n"]));
|
|
2112
|
+
await admitted(deploy, files(4e3), "4000 files");
|
|
2113
|
+
await refused(deploy, files(4001), "4001 files");
|
|
2114
|
+
}
|
|
2115
|
+
}
|
|
2116
|
+
];
|
|
2117
|
+
const base64url = (bytes) => btoa(String.fromCharCode(...bytes)).replace(/\+/gu, "-").replace(/\//gu, "_").replace(/=+$/u, "");
|
|
2118
|
+
const encoded = (value) => base64url(new TextEncoder().encode(JSON.stringify(value)));
|
|
2119
|
+
async function testIssuer() {
|
|
2120
|
+
const algorithm = {
|
|
2121
|
+
name: "RSASSA-PKCS1-v1_5",
|
|
2122
|
+
modulusLength: 2048,
|
|
2123
|
+
publicExponent: new Uint8Array([
|
|
2124
|
+
1,
|
|
2125
|
+
0,
|
|
2126
|
+
1
|
|
2127
|
+
]),
|
|
2128
|
+
hash: "SHA-256"
|
|
2129
|
+
};
|
|
2130
|
+
const [own, other] = await Promise.all([crypto.subtle.generateKey(algorithm, true, ["sign", "verify"]), crypto.subtle.generateKey(algorithm, true, ["sign", "verify"])]);
|
|
2131
|
+
const { kty, n, e } = await crypto.subtle.exportKey("jwk", own.publicKey);
|
|
2132
|
+
const kid = "contract-1";
|
|
2133
|
+
const issuer = `https://contract-${crypto.randomUUID().slice(0, 8)}.issuer.test`, audience = "contract";
|
|
2134
|
+
const jwks = `data:application/json;base64,${btoa(JSON.stringify({ keys: [{
|
|
2135
|
+
kty,
|
|
2136
|
+
n,
|
|
2137
|
+
e,
|
|
2138
|
+
kid,
|
|
2139
|
+
alg: "RS256",
|
|
2140
|
+
use: "sig"
|
|
2141
|
+
}] }))}`;
|
|
2142
|
+
const provider = `{ type: "customJwt", issuer: ${JSON.stringify(issuer)}, jwks: ${JSON.stringify(jwks)}, algorithm: "RS256", applicationID: ${JSON.stringify(audience)} }`;
|
|
2143
|
+
return {
|
|
2144
|
+
issuer,
|
|
2145
|
+
audience,
|
|
2146
|
+
provider,
|
|
2147
|
+
authConfig: ({ anonymous }) => `export default {\n providers: [${provider}],\n anonymous: ${anonymous},\n}\n`,
|
|
2148
|
+
async sign(claims, { forged = false } = {}) {
|
|
2149
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
2150
|
+
const payload = Object.fromEntries(Object.entries({
|
|
2151
|
+
iss: issuer,
|
|
2152
|
+
aud: audience,
|
|
2153
|
+
iat: now,
|
|
2154
|
+
exp: now + 600,
|
|
2155
|
+
...claims
|
|
2156
|
+
}).filter(([, value]) => value !== void 0));
|
|
2157
|
+
const signing = `${encoded({
|
|
2158
|
+
alg: "RS256",
|
|
2159
|
+
typ: "JWT",
|
|
2160
|
+
kid
|
|
2161
|
+
})}.${encoded(payload)}`;
|
|
2162
|
+
const signature = new Uint8Array(await crypto.subtle.sign({ name: "RSASSA-PKCS1-v1_5" }, (forged ? other : own).privateKey, new TextEncoder().encode(signing)));
|
|
2163
|
+
return `${signing}.${base64url(signature)}`;
|
|
2164
|
+
}
|
|
2165
|
+
};
|
|
2166
|
+
}
|
|
2167
|
+
/** A value with its object keys in order, so two identities compare by what they hold. */
|
|
2168
|
+
const sorted = (value) => Array.isArray(value) ? value.map(sorted) : value && typeof value === "object" ? Object.fromEntries(Object.entries(value).sort(([a], [b]) => a < b ? -1 : 1).map(([key, item]) => [key, sorted(item)])) : value;
|
|
2169
|
+
const databaseAuthContract = [
|
|
2170
|
+
{
|
|
2171
|
+
name: "a trusted provider's token reaches getUserIdentity with its verified claims",
|
|
2172
|
+
anonymous: false,
|
|
2173
|
+
async run(db, issuer) {
|
|
2174
|
+
const me = db.withToken(await issuer.sign({
|
|
2175
|
+
sub: "user_ada",
|
|
2176
|
+
name: "Ada Lovelace",
|
|
2177
|
+
given_name: "Ada",
|
|
2178
|
+
email: "ada@example.com",
|
|
2179
|
+
email_verified: true,
|
|
2180
|
+
picture: "https://example.com/ada.png",
|
|
2181
|
+
org: "acme"
|
|
2182
|
+
}));
|
|
2183
|
+
eq(sorted(await me.query("notes.identity", {})), sorted({
|
|
2184
|
+
tokenIdentifier: `${issuer.issuer}|user_ada`,
|
|
2185
|
+
subject: "user_ada",
|
|
2186
|
+
issuer: issuer.issuer,
|
|
2187
|
+
name: "Ada Lovelace",
|
|
2188
|
+
givenName: "Ada",
|
|
2189
|
+
email: "ada@example.com",
|
|
2190
|
+
emailVerified: true,
|
|
2191
|
+
pictureUrl: "https://example.com/ada.png",
|
|
2192
|
+
org: "acme"
|
|
2193
|
+
}), "the identity");
|
|
2194
|
+
eq(await me.query("notes.whoami", {}), "user_ada", "the subject");
|
|
2195
|
+
}
|
|
2196
|
+
},
|
|
2197
|
+
{
|
|
2198
|
+
name: "a token signed by a key its issuer does not publish is refused",
|
|
2199
|
+
anonymous: false,
|
|
2200
|
+
async run(db, issuer) {
|
|
2201
|
+
const token = await issuer.sign({ sub: "mallory" }, { forged: true });
|
|
2202
|
+
await refusedWith(() => db.withToken(token).query("notes.whoami", {}), "unauthenticated", "the forged token");
|
|
2203
|
+
}
|
|
2204
|
+
},
|
|
2205
|
+
{
|
|
2206
|
+
name: "an unsigned token is refused",
|
|
2207
|
+
anonymous: false,
|
|
2208
|
+
async run(db, issuer) {
|
|
2209
|
+
const unsigned = `${encoded({
|
|
2210
|
+
alg: "none",
|
|
2211
|
+
typ: "JWT"
|
|
2212
|
+
})}.${encoded({
|
|
2213
|
+
iss: issuer.issuer,
|
|
2214
|
+
aud: issuer.audience,
|
|
2215
|
+
sub: "mallory",
|
|
2216
|
+
exp: Math.floor(Date.now() / 1e3) + 600
|
|
2217
|
+
})}.`;
|
|
2218
|
+
await refusedWith(() => db.withToken(unsigned).query("notes.whoami", {}), "unauthenticated", "the unsigned token");
|
|
2219
|
+
}
|
|
2220
|
+
},
|
|
2221
|
+
{
|
|
2222
|
+
name: "an expired token is refused",
|
|
2223
|
+
anonymous: false,
|
|
2224
|
+
async run(db, issuer) {
|
|
2225
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
2226
|
+
const token = await issuer.sign({
|
|
2227
|
+
sub: "user_ada",
|
|
2228
|
+
iat: now - 7200,
|
|
2229
|
+
exp: now - 3600
|
|
2230
|
+
});
|
|
2231
|
+
await refusedWith(() => db.withToken(token).query("notes.whoami", {}), "unauthenticated", "the expired token");
|
|
2232
|
+
}
|
|
2233
|
+
},
|
|
2234
|
+
{
|
|
2235
|
+
name: "a token not valid yet is refused",
|
|
2236
|
+
anonymous: false,
|
|
2237
|
+
async run(db, issuer) {
|
|
2238
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
2239
|
+
const token = await issuer.sign({
|
|
2240
|
+
sub: "user_ada",
|
|
2241
|
+
nbf: now + 3600,
|
|
2242
|
+
exp: now + 7200
|
|
2243
|
+
});
|
|
2244
|
+
await refusedWith(() => db.withToken(token).query("notes.whoami", {}), "unauthenticated", "the token from the future");
|
|
2245
|
+
}
|
|
2246
|
+
},
|
|
2247
|
+
{
|
|
2248
|
+
name: "a token for another audience is refused",
|
|
2249
|
+
anonymous: false,
|
|
2250
|
+
async run(db, issuer) {
|
|
2251
|
+
const token = await issuer.sign({
|
|
2252
|
+
sub: "user_ada",
|
|
2253
|
+
aud: "another-app"
|
|
2254
|
+
});
|
|
2255
|
+
await refusedWith(() => db.withToken(token).query("notes.whoami", {}), "unauthenticated", "the other audience's token");
|
|
2256
|
+
}
|
|
2257
|
+
},
|
|
2258
|
+
{
|
|
2259
|
+
name: "a token from an issuer the Database does not trust is refused",
|
|
2260
|
+
anonymous: false,
|
|
2261
|
+
async run(db, issuer) {
|
|
2262
|
+
const token = await issuer.sign({
|
|
2263
|
+
sub: "user_ada",
|
|
2264
|
+
iss: "https://someone-else.issuer.test"
|
|
2265
|
+
});
|
|
2266
|
+
await refusedWith(() => db.withToken(token).query("notes.whoami", {}), "unauthenticated", "the untrusted issuer's token");
|
|
2267
|
+
}
|
|
2268
|
+
},
|
|
2269
|
+
{
|
|
2270
|
+
name: "a session ends when its token expires: a call after is refused, though the connection was made before",
|
|
2271
|
+
anonymous: false,
|
|
2272
|
+
async run(db, issuer) {
|
|
2273
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
2274
|
+
const me = db.withToken(await issuer.sign({
|
|
2275
|
+
sub: "user_ada",
|
|
2276
|
+
iat: now - 120,
|
|
2277
|
+
exp: now - 57
|
|
2278
|
+
}));
|
|
2279
|
+
eq(await me.query("notes.whoami", {}), "user_ada", "the subject while the token holds");
|
|
2280
|
+
await new Promise((resolve) => setTimeout(resolve, 4500));
|
|
2281
|
+
await refusedWith(() => me.query("notes.whoami", {}), "unauthenticated", "a call once the token has expired");
|
|
2282
|
+
}
|
|
2283
|
+
},
|
|
2284
|
+
{
|
|
2285
|
+
name: "a watch is given nothing more once its session's token has expired",
|
|
2286
|
+
anonymous: false,
|
|
2287
|
+
async run(db, issuer) {
|
|
2288
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
2289
|
+
const sub = `user_${Math.random().toString(36).slice(2, 10)}`;
|
|
2290
|
+
const me = db.withToken(await issuer.sign({
|
|
2291
|
+
sub,
|
|
2292
|
+
iat: now - 120,
|
|
2293
|
+
exp: now - 57
|
|
2294
|
+
}));
|
|
2295
|
+
const writer = db.withToken(await issuer.sign({ sub }));
|
|
2296
|
+
let seen = null;
|
|
2297
|
+
const stop = me.watch("notes.mine", {}, (value) => {
|
|
2298
|
+
seen = value;
|
|
2299
|
+
});
|
|
2300
|
+
try {
|
|
2301
|
+
await until(() => Array.isArray(seen), 5e3, "its first value");
|
|
2302
|
+
await writer.mutation("notes.add", { text: "early" });
|
|
2303
|
+
await until(() => JSON.stringify(seen) === JSON.stringify(["early"]), 5e3, "a value while the token holds");
|
|
2304
|
+
await new Promise((resolve) => setTimeout(resolve, 4500));
|
|
2305
|
+
await writer.mutation("notes.add", { text: "late" });
|
|
2306
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
2307
|
+
eq(seen, ["early"], "the value after the token expired");
|
|
2308
|
+
} finally {
|
|
2309
|
+
stop();
|
|
2310
|
+
}
|
|
2311
|
+
}
|
|
2312
|
+
},
|
|
2313
|
+
{
|
|
2314
|
+
name: "no token is refused where anonymous callers are not admitted",
|
|
2315
|
+
anonymous: false,
|
|
2316
|
+
async run(db) {
|
|
2317
|
+
await refusedWith(() => db.withToken(null).query("notes.whoami", {}), "unauthenticated", "no token");
|
|
2318
|
+
}
|
|
2319
|
+
},
|
|
2320
|
+
{
|
|
2321
|
+
name: "no token calls as no one where anonymous callers are admitted",
|
|
2322
|
+
anonymous: true,
|
|
2323
|
+
async run(db) {
|
|
2324
|
+
const nobody = db.withToken(null);
|
|
2325
|
+
eq(await nobody.query("notes.identity", {}), null, "the identity");
|
|
2326
|
+
eq(await nobody.query("notes.whoami", {}), null, "the subject");
|
|
2327
|
+
}
|
|
2328
|
+
},
|
|
2329
|
+
{
|
|
2330
|
+
name: "a refused token is refused where anonymous callers are admitted, never taken as no one",
|
|
2331
|
+
anonymous: true,
|
|
2332
|
+
async run(db, issuer) {
|
|
2333
|
+
const token = await issuer.sign({ sub: "mallory" }, { forged: true });
|
|
2334
|
+
await refusedWith(() => db.withToken(token).query("notes.whoami", {}), "unauthenticated", "the forged token");
|
|
2335
|
+
eq(await db.withToken(await issuer.sign({ sub: "user_ada" })).query("notes.whoami", {}), "user_ada", "a good token's subject");
|
|
2336
|
+
}
|
|
161
2337
|
}
|
|
162
2338
|
];
|
|
163
2339
|
//#endregion
|
|
164
|
-
export { contractFolder, databaseContract };
|
|
2340
|
+
export { HTTP_BODY_LIMIT, contractAheadFolder, contractFolder, databaseAheadContract, databaseAuthContract, databaseContract, databaseDeployContract, databaseQueryContract, databaseServerContract, testIssuer };
|