@cardonjs/testing 0.0.0-stage → 0.6.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/LICENSE +21 -0
- package/README.md +504 -2
- package/THIRD_PARTY_NOTICES.md +72 -0
- package/dist/access-token-contract.d.ts +26 -0
- package/dist/access-token-contract.d.ts.map +1 -0
- package/dist/access-token-contract.js +252 -0
- package/dist/access-token-contract.js.map +1 -0
- package/dist/assertion.d.ts +7 -0
- package/dist/assertion.d.ts.map +1 -0
- package/dist/assertion.js +11 -0
- package/dist/assertion.js.map +1 -0
- package/dist/contract.d.ts +14 -0
- package/dist/contract.d.ts.map +1 -0
- package/dist/contract.js +8 -0
- package/dist/contract.js.map +1 -0
- package/dist/envelopes.d.ts +33 -0
- package/dist/envelopes.d.ts.map +1 -0
- package/dist/envelopes.js +121 -0
- package/dist/envelopes.js.map +1 -0
- package/dist/idempotency-contract.d.ts +19 -0
- package/dist/idempotency-contract.d.ts.map +1 -0
- package/dist/idempotency-contract.js +234 -0
- package/dist/idempotency-contract.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/logs.d.ts +18 -0
- package/dist/logs.d.ts.map +1 -0
- package/dist/logs.js +20 -0
- package/dist/logs.js.map +1 -0
- package/dist/miniflare/bindings.d.ts +43 -0
- package/dist/miniflare/bindings.d.ts.map +1 -0
- package/dist/miniflare/bindings.js +90 -0
- package/dist/miniflare/bindings.js.map +1 -0
- package/dist/miniflare/broken.d.ts +12 -0
- package/dist/miniflare/broken.d.ts.map +1 -0
- package/dist/miniflare/broken.js +27 -0
- package/dist/miniflare/broken.js.map +1 -0
- package/dist/miniflare/executor-channel.d.ts +37 -0
- package/dist/miniflare/executor-channel.d.ts.map +1 -0
- package/dist/miniflare/executor-channel.js +144 -0
- package/dist/miniflare/executor-channel.js.map +1 -0
- package/dist/miniflare/executor.d.ts +42 -0
- package/dist/miniflare/executor.d.ts.map +1 -0
- package/dist/miniflare/executor.js +121 -0
- package/dist/miniflare/executor.js.map +1 -0
- package/dist/miniflare/index.d.ts +10 -0
- package/dist/miniflare/index.d.ts.map +1 -0
- package/dist/miniflare/index.js +10 -0
- package/dist/miniflare/index.js.map +1 -0
- package/dist/miniflare/migrations.d.ts +23 -0
- package/dist/miniflare/migrations.d.ts.map +1 -0
- package/dist/miniflare/migrations.js +43 -0
- package/dist/miniflare/migrations.js.map +1 -0
- package/dist/miniflare/one-call-d1.d.ts +12 -0
- package/dist/miniflare/one-call-d1.d.ts.map +1 -0
- package/dist/miniflare/one-call-d1.js +223 -0
- package/dist/miniflare/one-call-d1.js.map +1 -0
- package/dist/miniflare/statements.d.ts +18 -0
- package/dist/miniflare/statements.d.ts.map +1 -0
- package/dist/miniflare/statements.js +175 -0
- package/dist/miniflare/statements.js.map +1 -0
- package/dist/miniflare/storage.d.ts +58 -0
- package/dist/miniflare/storage.d.ts.map +1 -0
- package/dist/miniflare/storage.js +129 -0
- package/dist/miniflare/storage.js.map +1 -0
- package/dist/miniflare/types.d.ts +25 -0
- package/dist/miniflare/types.d.ts.map +1 -0
- package/dist/miniflare/types.js +2 -0
- package/dist/miniflare/types.js.map +1 -0
- package/dist/miniflare/worker.d.ts +70 -0
- package/dist/miniflare/worker.d.ts.map +1 -0
- package/dist/miniflare/worker.js +137 -0
- package/dist/miniflare/worker.js.map +1 -0
- package/dist/push-subscription-contract.d.ts +54 -0
- package/dist/push-subscription-contract.d.ts.map +1 -0
- package/dist/push-subscription-contract.js +735 -0
- package/dist/push-subscription-contract.js.map +1 -0
- package/dist/routes.d.ts +72 -0
- package/dist/routes.d.ts.map +1 -0
- package/dist/routes.js +196 -0
- package/dist/routes.js.map +1 -0
- package/llms.txt +225 -0
- package/package.json +69 -3
|
@@ -0,0 +1,735 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shared contract every `PushSubscriptionStore` (from `@cardonjs/notify-webpush`) must pass.
|
|
3
|
+
* Run each case with the store's own runner against a harness over its real backend (e.g. D1
|
|
4
|
+
* from Miniflare). The store types are repeated here structurally, so this package does not
|
|
5
|
+
* depend on `@cardonjs/notify-webpush` (whose tests depend on this one). Failure messages name
|
|
6
|
+
* subscriptions by a label, never by their endpoint or keys.
|
|
7
|
+
*/
|
|
8
|
+
import { fail } from "./assertion.js";
|
|
9
|
+
import { defineContract } from "./contract.js";
|
|
10
|
+
/** `MAX_OWNERS_PER_ENDPOINT` of `@cardonjs/notify-webpush`. */
|
|
11
|
+
const OWNERS_PER_ENDPOINT = 50;
|
|
12
|
+
const T0 = Date.UTC(2026, 10, 18, 12, 0, 0, 0);
|
|
13
|
+
/** A clock the cases move forward by hand. */
|
|
14
|
+
function clock() {
|
|
15
|
+
let ms = T0;
|
|
16
|
+
return {
|
|
17
|
+
now: () => new Date(ms),
|
|
18
|
+
at: () => ms,
|
|
19
|
+
tick: (by = 1000) => {
|
|
20
|
+
ms += by;
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/** base64url of `length` characters from a seed, the shape of real keys. */
|
|
25
|
+
function b64(seed, length) {
|
|
26
|
+
return seed.repeat(length).slice(0, length);
|
|
27
|
+
}
|
|
28
|
+
/** A subscription named `label`: its endpoint and keys hold the label, `auth` unless given. */
|
|
29
|
+
function browser(label, auth = `${label}a`) {
|
|
30
|
+
return {
|
|
31
|
+
endpoint: `https://fcm.googleapis.com/fcm/send/${label}`,
|
|
32
|
+
keys: { p256dh: `B${b64(`${label}p`, 86)}`, auth: b64(auth, 22) },
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/** The browser `label` with other keys (`seed`): someone else posting its endpoint. */
|
|
36
|
+
function otherKeys(label, seed = "evil") {
|
|
37
|
+
return { endpoint: browser(label).endpoint, keys: browser(seed).keys };
|
|
38
|
+
}
|
|
39
|
+
/** The browser `label` with a new `p256dh` and the same `auth`. */
|
|
40
|
+
function newP256dh(label) {
|
|
41
|
+
return {
|
|
42
|
+
endpoint: browser(label).endpoint,
|
|
43
|
+
keys: { p256dh: `B${b64(`${label}renewed`, 86)}`, auth: browser(label).keys.auth },
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
const PREFIX = "https://fcm.googleapis.com/fcm/send/";
|
|
47
|
+
const ANA = { type: "user", id: "019a8a3c-0000-7000-8000-00000000a0a0" };
|
|
48
|
+
const BOB = { type: "user", id: "019a8a3c-0000-7000-8000-00000000b0b0" };
|
|
49
|
+
const NOTE = { type: "note", id: "019a8a3c-0000-7000-8000-00000000c0c0" };
|
|
50
|
+
const NOTE2 = { type: "note", id: "019a8a3c-0000-7000-8000-00000000c0c1" };
|
|
51
|
+
/** A subscription as a label (the endpoint without its prefix). */
|
|
52
|
+
function label(value) {
|
|
53
|
+
const endpoint = typeof value?.endpoint === "string" ? value.endpoint : "";
|
|
54
|
+
return endpoint.startsWith(PREFIX) ? endpoint.slice(PREFIX.length) : "<unexpected endpoint>";
|
|
55
|
+
}
|
|
56
|
+
function expectEqual(actual, expected, title) {
|
|
57
|
+
const a = JSON.stringify(actual);
|
|
58
|
+
const e = JSON.stringify(expected);
|
|
59
|
+
if (a !== e)
|
|
60
|
+
fail(title, [`expected ${e}`, `got ${a}`]);
|
|
61
|
+
}
|
|
62
|
+
/** A result as text without anything secret (it holds none, but a broken store might). */
|
|
63
|
+
function outcome(result) {
|
|
64
|
+
if (typeof result !== "object" || result === null)
|
|
65
|
+
return typeof result;
|
|
66
|
+
const { saved, reason } = result;
|
|
67
|
+
return saved === true
|
|
68
|
+
? "saved"
|
|
69
|
+
: saved === false && typeof reason === "string" && /^[a-z_]{1,20}$/.test(reason)
|
|
70
|
+
? reason
|
|
71
|
+
: "<malformed result>";
|
|
72
|
+
}
|
|
73
|
+
function expectSaved(result, title) {
|
|
74
|
+
const got = outcome(result);
|
|
75
|
+
if (got !== "saved")
|
|
76
|
+
fail(title, ["expected saved", `got ${got}`]);
|
|
77
|
+
}
|
|
78
|
+
function expectRefused(result, reason, title) {
|
|
79
|
+
const got = outcome(result);
|
|
80
|
+
if (got !== reason)
|
|
81
|
+
fail(title, [`expected ${reason}`, `got ${got}`]);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* The owner's subscriptions as labels, newest first; fails (without printing keys) when one
|
|
85
|
+
* comes back with other keys than `expected` gives for its label (default: `browser(label)`).
|
|
86
|
+
*/
|
|
87
|
+
async function listed(store, owner, expected = {}) {
|
|
88
|
+
const rows = await store.listForOwner(owner);
|
|
89
|
+
if (!Array.isArray(rows))
|
|
90
|
+
fail("listForOwner must resolve an array");
|
|
91
|
+
return rows.map((row) => {
|
|
92
|
+
const name = label(row);
|
|
93
|
+
const want = expected[name] ?? browser(name);
|
|
94
|
+
if (typeof row.keys !== "object" ||
|
|
95
|
+
row.keys === null ||
|
|
96
|
+
row.keys.p256dh !== want.keys.p256dh ||
|
|
97
|
+
row.keys.auth !== want.keys.auth) {
|
|
98
|
+
fail("a listed subscription must come back with its endpoint's current keys", [
|
|
99
|
+
`subscription ${name}: keys differ`,
|
|
100
|
+
]);
|
|
101
|
+
}
|
|
102
|
+
return name;
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
async function expectList(store, owner, expected, title, keys = {}) {
|
|
106
|
+
expectEqual(await listed(store, owner, keys), expected, title);
|
|
107
|
+
}
|
|
108
|
+
/** Everything the owners list, keys included, as one comparable value (never printed). */
|
|
109
|
+
async function snapshot(store, owners) {
|
|
110
|
+
const all = [];
|
|
111
|
+
for (const owner of owners)
|
|
112
|
+
all.push(await store.listForOwner(owner));
|
|
113
|
+
return JSON.stringify(all);
|
|
114
|
+
}
|
|
115
|
+
async function expectUnchanged(store, owners, before, title) {
|
|
116
|
+
if ((await snapshot(store, owners)) !== before) {
|
|
117
|
+
fail(title, ["a refused save must change nothing: no subscription, key or order moved"]);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
async function rejectsWithTypeError(call, title, secrets) {
|
|
121
|
+
let error;
|
|
122
|
+
try {
|
|
123
|
+
await call();
|
|
124
|
+
}
|
|
125
|
+
catch (caught) {
|
|
126
|
+
error = caught;
|
|
127
|
+
}
|
|
128
|
+
if (!(error instanceof TypeError))
|
|
129
|
+
fail(title, ["expected a TypeError"]);
|
|
130
|
+
const message = error.message;
|
|
131
|
+
for (const secret of secrets) {
|
|
132
|
+
if (message.includes(secret))
|
|
133
|
+
fail(title, ["the error message must not hold the value"]);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
/** `count` distinct owners of `type`, for the caps. */
|
|
137
|
+
function owners(type, count, from = 0) {
|
|
138
|
+
return Array.from({ length: count }, (_, i) => ({ type, id: `${type}-${from + i}` }));
|
|
139
|
+
}
|
|
140
|
+
export const pushSubscriptionStoreContract = defineContract([
|
|
141
|
+
{
|
|
142
|
+
name: "save then listForOwner returns the subscriptions newest first; another owner has none",
|
|
143
|
+
async run(harness) {
|
|
144
|
+
const time = clock();
|
|
145
|
+
const store = await harness.create({ now: time.now });
|
|
146
|
+
expectSaved(await store.save(ANA, browser("phone")), "a new endpoint is saved");
|
|
147
|
+
time.tick();
|
|
148
|
+
expectSaved(await store.save(ANA, browser("laptop")), "a new endpoint is saved");
|
|
149
|
+
await expectList(store, ANA, ["laptop", "phone"], "newest first, with their keys");
|
|
150
|
+
await expectList(store, BOB, [], "an owner without subscriptions lists none");
|
|
151
|
+
time.tick();
|
|
152
|
+
expectSaved(await store.save(ANA, browser("phone")), "the same save again is saved");
|
|
153
|
+
await expectList(store, ANA, ["phone", "laptop"], "saving again makes it the newest");
|
|
154
|
+
// Saved in the same millisecond: the larger endpoint counts as newer.
|
|
155
|
+
const tied = await harness.create({ now: time.now });
|
|
156
|
+
for (const name of ["t-b", "t-c", "t-a"])
|
|
157
|
+
await tied.save(NOTE, browser(name));
|
|
158
|
+
await expectList(tied, NOTE, ["t-c", "t-b", "t-a"], "ties are ordered by endpoint");
|
|
159
|
+
},
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
name: "one endpoint under two owners: both list it, deleteForOwner leaves the other, delete removes both",
|
|
163
|
+
async run(harness) {
|
|
164
|
+
const time = clock();
|
|
165
|
+
const store = await harness.create({ now: time.now });
|
|
166
|
+
expectSaved(await store.save(ANA, browser("phone")), "first owner");
|
|
167
|
+
time.tick();
|
|
168
|
+
expectSaved(await store.save(NOTE, browser("phone")), "the same browser for a second owner");
|
|
169
|
+
time.tick();
|
|
170
|
+
expectSaved(await store.save(BOB, browser("phone")), "and a third, of the first's type");
|
|
171
|
+
for (const owner of [ANA, NOTE, BOB]) {
|
|
172
|
+
await expectList(store, owner, ["phone"], "every owner lists the browser");
|
|
173
|
+
}
|
|
174
|
+
await store.deleteForOwner(NOTE, browser("phone").endpoint);
|
|
175
|
+
await expectList(store, NOTE, [], "deleteForOwner removes that owner's subscription");
|
|
176
|
+
await expectList(store, ANA, ["phone"], "and leaves the other owners'");
|
|
177
|
+
await expectList(store, BOB, ["phone"], "and leaves the other owners'");
|
|
178
|
+
await store.save(NOTE, browser("phone"));
|
|
179
|
+
await store.delete(browser("phone").endpoint);
|
|
180
|
+
for (const owner of [ANA, NOTE, BOB]) {
|
|
181
|
+
await expectList(store, owner, [], "delete removes the browser for every owner");
|
|
182
|
+
}
|
|
183
|
+
await store.delete(browser("unknown").endpoint); // nothing stored: no error
|
|
184
|
+
expectSaved(await store.save(BOB, otherKeys("phone", "fresh")), "a deleted endpoint is free again: other keys may save it");
|
|
185
|
+
},
|
|
186
|
+
},
|
|
187
|
+
{
|
|
188
|
+
name: "another auth for a stored endpoint is refused (taken) under any owner, the same one included, changing nothing",
|
|
189
|
+
async run(harness) {
|
|
190
|
+
const time = clock();
|
|
191
|
+
const store = await harness.create({ now: time.now });
|
|
192
|
+
await store.save(ANA, browser("phone"));
|
|
193
|
+
time.tick();
|
|
194
|
+
await store.save(NOTE, browser("phone"));
|
|
195
|
+
time.tick();
|
|
196
|
+
await store.save(ANA, browser("tablet"));
|
|
197
|
+
const everyone = [ANA, BOB, NOTE, NOTE2];
|
|
198
|
+
const before = await snapshot(store, everyone);
|
|
199
|
+
const upper = browser("phone").keys.auth.toUpperCase();
|
|
200
|
+
const attempts = [
|
|
201
|
+
["other keys", otherKeys("phone")],
|
|
202
|
+
[
|
|
203
|
+
"the same p256dh with another auth",
|
|
204
|
+
{
|
|
205
|
+
endpoint: browser("phone").endpoint,
|
|
206
|
+
keys: { p256dh: browser("phone").keys.p256dh, auth: browser("evil").keys.auth },
|
|
207
|
+
},
|
|
208
|
+
],
|
|
209
|
+
[
|
|
210
|
+
"an auth differing only in letter case (other bytes)",
|
|
211
|
+
{
|
|
212
|
+
endpoint: browser("phone").endpoint,
|
|
213
|
+
keys: { ...browser("phone").keys, auth: upper },
|
|
214
|
+
},
|
|
215
|
+
],
|
|
216
|
+
];
|
|
217
|
+
const intruders = [
|
|
218
|
+
ANA, // the same owner: a shared owner proves nothing about the browser
|
|
219
|
+
NOTE,
|
|
220
|
+
BOB,
|
|
221
|
+
NOTE2,
|
|
222
|
+
{ type: "note", id: ANA.id }, // another type, the same id
|
|
223
|
+
{ type: "user", id: ANA.id.toUpperCase() }, // the id in other letter case
|
|
224
|
+
];
|
|
225
|
+
for (const intruder of intruders) {
|
|
226
|
+
for (const [what, subscription] of attempts) {
|
|
227
|
+
time.tick();
|
|
228
|
+
for (const options of [{}, { exclusive: true }, { max: 1 }]) {
|
|
229
|
+
expectRefused(await store.save(intruder, subscription, options), "taken", `${what} for a stored endpoint must be refused as taken (${intruder.type})`);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
await expectUnchanged(store, everyone, before, "a taken save changes nothing anywhere");
|
|
234
|
+
await expectList(store, ANA, ["tablet", "phone"], "the owner keeps its subscriptions");
|
|
235
|
+
await expectList(store, BOB, [], "the intruder owns nothing");
|
|
236
|
+
},
|
|
237
|
+
},
|
|
238
|
+
{
|
|
239
|
+
name: "the same auth with a new p256dh updates the keys every owner lists; = padding is the same secret",
|
|
240
|
+
async run(harness) {
|
|
241
|
+
const time = clock();
|
|
242
|
+
const store = await harness.create({ now: time.now });
|
|
243
|
+
await store.save(ANA, browser("phone"));
|
|
244
|
+
time.tick();
|
|
245
|
+
await store.save(NOTE, browser("phone"));
|
|
246
|
+
time.tick();
|
|
247
|
+
await store.save(ANA, browser("laptop"));
|
|
248
|
+
time.tick();
|
|
249
|
+
expectSaved(await store.save(NOTE, newP256dh("phone")), "the same auth, new p256dh");
|
|
250
|
+
const renewed = { phone: newP256dh("phone") };
|
|
251
|
+
await expectList(store, NOTE, ["phone"], "the saving owner lists the new key", renewed);
|
|
252
|
+
await expectList(store, ANA, ["laptop", "phone"], "every other owner lists the new key too, its order unchanged", renewed);
|
|
253
|
+
time.tick();
|
|
254
|
+
const padded = {
|
|
255
|
+
endpoint: browser("phone").endpoint,
|
|
256
|
+
keys: {
|
|
257
|
+
p256dh: `${newP256dh("phone").keys.p256dh}=`,
|
|
258
|
+
auth: `${browser("phone").keys.auth}==`,
|
|
259
|
+
},
|
|
260
|
+
};
|
|
261
|
+
expectSaved(await store.save(BOB, padded), "keys are compared unpadded: the padded auth is the same secret");
|
|
262
|
+
await expectList(store, BOB, ["phone"], "and listed in canonical (unpadded) form", renewed);
|
|
263
|
+
},
|
|
264
|
+
},
|
|
265
|
+
{
|
|
266
|
+
name: "whenFull evict keeps the newest max of the owner (default 10), the saved one always, never another owner's",
|
|
267
|
+
async run(harness) {
|
|
268
|
+
const time = clock();
|
|
269
|
+
const store = await harness.create({ now: time.now });
|
|
270
|
+
await store.save(BOB, browser("bob"));
|
|
271
|
+
await store.save(NOTE, browser("d00"));
|
|
272
|
+
const names = Array.from({ length: 12 }, (_, i) => `d${String(i).padStart(2, "0")}`);
|
|
273
|
+
for (const name of names) {
|
|
274
|
+
time.tick();
|
|
275
|
+
expectSaved(await store.save(ANA, browser(name)), "evict never refuses for the owner cap");
|
|
276
|
+
}
|
|
277
|
+
await expectList(store, ANA, names.slice(2).reverse(), "the default keeps the newest 10, the two oldest evicted");
|
|
278
|
+
await expectList(store, NOTE, ["d00"], "an evicted browser stays with its other owners");
|
|
279
|
+
time.tick();
|
|
280
|
+
await store.save(ANA, browser("d02")); // saved again: now the newest
|
|
281
|
+
time.tick();
|
|
282
|
+
await store.save(ANA, browser("late"), { max: 3, whenFull: "evict" });
|
|
283
|
+
await expectList(store, ANA, ["late", "d02", "d11"], "max 3 keeps the 3 newest");
|
|
284
|
+
time.tick();
|
|
285
|
+
await store.save(ANA, browser("solo"), { max: 1 });
|
|
286
|
+
await expectList(store, ANA, ["solo"], "max 1 keeps only the one just saved");
|
|
287
|
+
await expectList(store, BOB, ["bob"], "another owner's subscriptions are never evicted");
|
|
288
|
+
expectSaved(await store.save(BOB, otherKeys("d03", "fresh")), "an evicted browser no owner uses any more is free again");
|
|
289
|
+
// Saved in the same millisecond: ties evict the smaller endpoints, never the saved one.
|
|
290
|
+
const tied = await harness.create({ now: time.now });
|
|
291
|
+
for (const name of ["t-b", "t-c", "t-a"])
|
|
292
|
+
await tied.save(NOTE, browser(name));
|
|
293
|
+
await tied.save(NOTE, browser("t-0"), { max: 2 });
|
|
294
|
+
await expectList(tied, NOTE, ["t-c", "t-0"], "the saved one stays, with the newest other");
|
|
295
|
+
},
|
|
296
|
+
},
|
|
297
|
+
{
|
|
298
|
+
name: "whenFull refuse answers owner_full at the cap and changes nothing; re-saving a subscription is never refused",
|
|
299
|
+
async run(harness) {
|
|
300
|
+
const time = clock();
|
|
301
|
+
const store = await harness.create({ now: time.now });
|
|
302
|
+
for (const name of ["n1", "n2", "n3"]) {
|
|
303
|
+
time.tick();
|
|
304
|
+
expectSaved(await store.save(NOTE, browser(name), { max: 3, whenFull: "refuse" }), "below the cap a save succeeds");
|
|
305
|
+
}
|
|
306
|
+
await store.save(ANA, browser("ana-phone"));
|
|
307
|
+
const everyone = [NOTE, ANA];
|
|
308
|
+
const before = await snapshot(store, everyone);
|
|
309
|
+
time.tick();
|
|
310
|
+
expectRefused(await store.save(NOTE, browser("n4"), { max: 3, whenFull: "refuse" }), "owner_full", "a new subscription past the cap is refused");
|
|
311
|
+
expectRefused(await store.save(NOTE, newP256dh("ana-phone"), { max: 3, whenFull: "refuse" }), "owner_full", "a browser another owner has is refused too, without updating its keys");
|
|
312
|
+
await expectUnchanged(store, everyone, before, "owner_full changes nothing anywhere");
|
|
313
|
+
expectSaved(await store.save(BOB, browser("n4")), "a refused new endpoint was not stored: anyone may save it");
|
|
314
|
+
time.tick();
|
|
315
|
+
expectSaved(await store.save(NOTE, browser("n1"), { max: 3, whenFull: "refuse" }), "re-saving one of the owner's own subscriptions is not refused");
|
|
316
|
+
await expectList(store, NOTE, ["n1", "n3", "n2"], "and makes it the newest");
|
|
317
|
+
},
|
|
318
|
+
},
|
|
319
|
+
{
|
|
320
|
+
name: "a refused save never evicts or changes anything when the owner is at its cap",
|
|
321
|
+
async run(harness) {
|
|
322
|
+
const time = clock();
|
|
323
|
+
const store = await harness.create({ now: time.now });
|
|
324
|
+
await store.save(BOB, browser("bob-phone"));
|
|
325
|
+
for (const name of ["a1", "a2"]) {
|
|
326
|
+
time.tick();
|
|
327
|
+
await store.save(ANA, browser(name), { max: 2 });
|
|
328
|
+
}
|
|
329
|
+
const everyone = [ANA, BOB];
|
|
330
|
+
const before = await snapshot(store, everyone);
|
|
331
|
+
time.tick();
|
|
332
|
+
expectRefused(await store.save(ANA, otherKeys("bob-phone"), { max: 2, whenFull: "evict" }), "taken", "someone else's endpoint is refused");
|
|
333
|
+
await expectUnchanged(store, everyone, before, "a taken save evicts nothing");
|
|
334
|
+
await expectList(store, ANA, ["a2", "a1"], "the owner keeps both subscriptions");
|
|
335
|
+
},
|
|
336
|
+
},
|
|
337
|
+
{
|
|
338
|
+
name: "owner_full with exclusive changes nothing: the endpoint's other owners of the same type keep it",
|
|
339
|
+
async run(harness) {
|
|
340
|
+
const time = clock();
|
|
341
|
+
const store = await harness.create({ now: time.now });
|
|
342
|
+
for (const name of ["n1", "n2"]) {
|
|
343
|
+
time.tick();
|
|
344
|
+
await store.save(NOTE, browser(name), { max: 2, whenFull: "refuse" });
|
|
345
|
+
}
|
|
346
|
+
time.tick();
|
|
347
|
+
await store.save(NOTE2, browser("shared"));
|
|
348
|
+
await store.save(ANA, browser("shared"));
|
|
349
|
+
const everyone = [NOTE, NOTE2, ANA];
|
|
350
|
+
const before = await snapshot(store, everyone);
|
|
351
|
+
time.tick();
|
|
352
|
+
expectRefused(await store.save(NOTE, browser("shared"), {
|
|
353
|
+
max: 2,
|
|
354
|
+
whenFull: "refuse",
|
|
355
|
+
exclusive: true,
|
|
356
|
+
}), "owner_full", "an owner at its cap is refused, exclusive or not");
|
|
357
|
+
await expectUnchanged(store, everyone, before, "owner_full runs no exclusive delete");
|
|
358
|
+
await expectList(store, NOTE2, ["shared"], "the other owner of the same type keeps it");
|
|
359
|
+
},
|
|
360
|
+
},
|
|
361
|
+
{
|
|
362
|
+
name: `an endpoint under ${OWNERS_PER_ENDPOINT} owners refuses another (endpoint_full) and evicts nobody`,
|
|
363
|
+
async run(harness) {
|
|
364
|
+
const time = clock();
|
|
365
|
+
const store = await harness.create({ now: time.now });
|
|
366
|
+
const many = owners("note", OWNERS_PER_ENDPOINT);
|
|
367
|
+
for (const owner of many) {
|
|
368
|
+
time.tick();
|
|
369
|
+
expectSaved(await store.save(owner, browser("phone")), "below the endpoint cap");
|
|
370
|
+
}
|
|
371
|
+
const extra = owners("note", 1, OWNERS_PER_ENDPOINT)[0];
|
|
372
|
+
await store.save(extra, browser("extra-1"), { source: "link-x" });
|
|
373
|
+
const watched = [...many, extra, ANA];
|
|
374
|
+
const before = await snapshot(store, watched);
|
|
375
|
+
time.tick();
|
|
376
|
+
expectRefused(await store.save(extra, newP256dh("phone")), "endpoint_full", "one owner more than the cap is refused");
|
|
377
|
+
expectRefused(await store.save(extra, browser("phone"), { source: "link-x", maxPerSource: 1 }), "endpoint_full", "endpoint_full is answered before source_full");
|
|
378
|
+
expectRefused(await store.save(ANA, browser("phone"), { exclusive: true }), "endpoint_full", "exclusive only discounts owners of its own type");
|
|
379
|
+
await expectUnchanged(store, watched, before, "endpoint_full changes nothing anywhere");
|
|
380
|
+
time.tick();
|
|
381
|
+
expectSaved(await store.save(many[0], browser("phone")), "an owner it already has may save it again");
|
|
382
|
+
expectSaved(await store.save(extra, browser("phone"), { exclusive: true }), "exclusive under the same type replaces those owners instead");
|
|
383
|
+
await expectList(store, extra, ["phone", "extra-1"], "the exclusive owner has it");
|
|
384
|
+
await expectList(store, many[0], [], "the others lost it");
|
|
385
|
+
},
|
|
386
|
+
},
|
|
387
|
+
{
|
|
388
|
+
name: "endpoint_full with exclusive and max 1 changes nothing: the owner's other subscriptions stay",
|
|
389
|
+
async run(harness) {
|
|
390
|
+
const time = clock();
|
|
391
|
+
const store = await harness.create({ now: time.now });
|
|
392
|
+
for (const name of ["a1", "a2", "a3"]) {
|
|
393
|
+
time.tick();
|
|
394
|
+
await store.save(ANA, browser(name));
|
|
395
|
+
}
|
|
396
|
+
const many = owners("note", OWNERS_PER_ENDPOINT);
|
|
397
|
+
for (const owner of many) {
|
|
398
|
+
time.tick();
|
|
399
|
+
await store.save(owner, browser("phone"));
|
|
400
|
+
}
|
|
401
|
+
// The refused save could only touch Ana's rows (eviction) or the endpoint's (exclusive).
|
|
402
|
+
const watched = [
|
|
403
|
+
ANA,
|
|
404
|
+
many[0],
|
|
405
|
+
many[OWNERS_PER_ENDPOINT - 1],
|
|
406
|
+
];
|
|
407
|
+
const before = await snapshot(store, watched);
|
|
408
|
+
time.tick();
|
|
409
|
+
expectRefused(await store.save(ANA, browser("phone"), { max: 1, exclusive: true }), "endpoint_full", "a full endpoint of another type is refused, exclusive or not");
|
|
410
|
+
await expectUnchanged(store, watched, before, "endpoint_full evicts nothing");
|
|
411
|
+
await expectList(store, ANA, ["a3", "a2", "a1"], "the owner keeps every subscription");
|
|
412
|
+
},
|
|
413
|
+
},
|
|
414
|
+
{
|
|
415
|
+
name: "maxPerSource refuses source_full at the cap and changes nothing, whatever whenFull, exclusive or max, the owner not full",
|
|
416
|
+
async run(harness) {
|
|
417
|
+
const time = clock();
|
|
418
|
+
const store = await harness.create({ now: time.now });
|
|
419
|
+
const capped = { source: "link-a", maxPerSource: 2 };
|
|
420
|
+
for (const name of ["s1", "s2"]) {
|
|
421
|
+
time.tick();
|
|
422
|
+
expectSaved(await store.save(NOTE, browser(name), { ...capped, whenFull: "refuse" }), "below the per-source cap a save succeeds");
|
|
423
|
+
}
|
|
424
|
+
time.tick();
|
|
425
|
+
await store.save(NOTE, browser("b1"), { source: "link-b" });
|
|
426
|
+
await store.save(NOTE, browser("plain"));
|
|
427
|
+
time.tick();
|
|
428
|
+
await store.save(NOTE2, browser("shared"));
|
|
429
|
+
await store.save(ANA, browser("shared"));
|
|
430
|
+
await store.save(ANA, browser("ana-phone"));
|
|
431
|
+
const everyone = [NOTE, NOTE2, ANA];
|
|
432
|
+
const before = await snapshot(store, everyone);
|
|
433
|
+
const attempts = [
|
|
434
|
+
["a new browser, refuse", browser("s3"), { ...capped, whenFull: "refuse" }],
|
|
435
|
+
["a new browser, evict", browser("s3"), capped],
|
|
436
|
+
["a new browser, max 1 (evict)", browser("s3"), { ...capped, max: 1 }],
|
|
437
|
+
["exclusive", browser("shared"), { ...capped, exclusive: true }],
|
|
438
|
+
["exclusive and max 1", browser("shared"), { ...capped, exclusive: true, max: 1 }],
|
|
439
|
+
["a browser another owner has, new p256dh", newP256dh("ana-phone"), capped],
|
|
440
|
+
];
|
|
441
|
+
for (const [what, subscription, options] of attempts) {
|
|
442
|
+
time.tick();
|
|
443
|
+
expectRefused(await store.save(NOTE, subscription, options), "source_full", `a third subscription from one source is refused (${what})`);
|
|
444
|
+
}
|
|
445
|
+
time.tick();
|
|
446
|
+
expectRefused(await store.save(NOTE, browser("s3"), { ...capped, max: 4, whenFull: "refuse" }), "source_full", "source_full is answered before owner_full");
|
|
447
|
+
expectRefused(await store.save(NOTE, otherKeys("shared"), capped), "taken", "taken is answered before source_full");
|
|
448
|
+
await expectUnchanged(store, everyone, before, "source_full changes nothing anywhere");
|
|
449
|
+
await expectList(store, NOTE, ["plain", "b1", "s2", "s1"], "nothing evicted, no key updated");
|
|
450
|
+
await expectList(store, NOTE2, ["shared"], "no exclusive delete ran");
|
|
451
|
+
expectSaved(await store.save(BOB, otherKeys("s3", "fresh")), "a refused new endpoint was not stored: anyone may save it");
|
|
452
|
+
},
|
|
453
|
+
},
|
|
454
|
+
{
|
|
455
|
+
name: "maxPerSource counts only the owner's live subscriptions with that source, not the one saved again",
|
|
456
|
+
async run(harness) {
|
|
457
|
+
const time = clock();
|
|
458
|
+
const store = await harness.create({ now: time.now });
|
|
459
|
+
const a = { source: "link-a", maxPerSource: 2 };
|
|
460
|
+
const b = { source: "link-b", maxPerSource: 2 };
|
|
461
|
+
await store.save(NOTE, browser("a1"), a);
|
|
462
|
+
time.tick();
|
|
463
|
+
await store.save(NOTE, browser("a2"), { ...a, expiresAt: new Date(time.at() + 5000) });
|
|
464
|
+
time.tick();
|
|
465
|
+
await store.save(NOTE, browser("b1"), b);
|
|
466
|
+
await store.save(NOTE, browser("p1"));
|
|
467
|
+
await store.save(NOTE, browser("p2"));
|
|
468
|
+
// The same source under other owners, of the same type and of another.
|
|
469
|
+
const alsoA = { source: "link-a" };
|
|
470
|
+
await store.save(NOTE2, browser("n1"), alsoA);
|
|
471
|
+
await store.save(NOTE2, browser("n2"), alsoA);
|
|
472
|
+
await store.save({ type: "user", id: NOTE.id }, browser("u1"), alsoA);
|
|
473
|
+
time.tick();
|
|
474
|
+
expectRefused(await store.save(NOTE, browser("a3"), a), "source_full", "two live subscriptions with the source: the cap");
|
|
475
|
+
expectSaved(await store.save(NOTE, browser("c1"), { source: "link-c", maxPerSource: 1 }), "other sources, source-less subscriptions and other owners are not counted");
|
|
476
|
+
time.tick(5000); // a2 expires
|
|
477
|
+
expectSaved(await store.save(NOTE, browser("a3"), a), "an expired subscription is not counted");
|
|
478
|
+
time.tick();
|
|
479
|
+
expectSaved(await store.save(NOTE, browser("a1"), a), "saving again an endpoint already under the source is accepted at the cap");
|
|
480
|
+
time.tick();
|
|
481
|
+
expectSaved(await store.save(NOTE, browser("a3"), b), "a re-save under another source counts there");
|
|
482
|
+
time.tick();
|
|
483
|
+
expectSaved(await store.save(NOTE, browser("a4"), a), "and frees its slot under the first source");
|
|
484
|
+
time.tick();
|
|
485
|
+
expectRefused(await store.save(NOTE, browser("b2"), b), "source_full", "the moved subscription counts under its new source");
|
|
486
|
+
time.tick();
|
|
487
|
+
await store.save(NOTE, browser("a3"));
|
|
488
|
+
time.tick();
|
|
489
|
+
expectSaved(await store.save(NOTE, browser("b2"), b), "a save without a source clears it");
|
|
490
|
+
await expectList(store, NOTE, ["b2", "a3", "a4", "a1", "c1", "p2", "p1", "b1"], "every saved subscription is listed");
|
|
491
|
+
const listed = JSON.stringify(await store.listForOwner(NOTE));
|
|
492
|
+
if (/link-[a-c]/.test(listed)) {
|
|
493
|
+
fail("listForOwner must never return the source", ["a listed subscription has it"]);
|
|
494
|
+
}
|
|
495
|
+
},
|
|
496
|
+
},
|
|
497
|
+
{
|
|
498
|
+
name: "exclusive removes the endpoint's subscriptions under other owners of the same type only",
|
|
499
|
+
async run(harness) {
|
|
500
|
+
const time = clock();
|
|
501
|
+
const store = await harness.create({ now: time.now });
|
|
502
|
+
await store.save(ANA, browser("phone"));
|
|
503
|
+
await store.save(ANA, browser("laptop"));
|
|
504
|
+
time.tick();
|
|
505
|
+
await store.save(NOTE, browser("phone"));
|
|
506
|
+
await store.save(NOTE2, browser("phone"));
|
|
507
|
+
time.tick();
|
|
508
|
+
expectSaved(await store.save(BOB, browser("phone")), "without exclusive, both users have it");
|
|
509
|
+
await expectList(store, ANA, ["phone", "laptop"], "Ana keeps it without exclusive");
|
|
510
|
+
time.tick();
|
|
511
|
+
expectSaved(await store.save(BOB, browser("phone"), { exclusive: true }), "exclusive save");
|
|
512
|
+
await expectList(store, ANA, ["laptop"], "the other user of the same type lost it");
|
|
513
|
+
await expectList(store, BOB, ["phone"], "the saving owner has it");
|
|
514
|
+
await expectList(store, NOTE, ["phone"], "owners of another type keep it");
|
|
515
|
+
await expectList(store, NOTE2, ["phone"], "owners of another type keep it");
|
|
516
|
+
time.tick();
|
|
517
|
+
await store.save(NOTE, browser("phone"), { exclusive: true });
|
|
518
|
+
await expectList(store, NOTE2, [], "exclusive under note removes the other note");
|
|
519
|
+
await expectList(store, BOB, ["phone"], "and never the user");
|
|
520
|
+
},
|
|
521
|
+
},
|
|
522
|
+
{
|
|
523
|
+
name: "an expired subscription is skipped by listForOwner, not counted against max, and deleted by purgeExpired",
|
|
524
|
+
async run(harness) {
|
|
525
|
+
const time = clock();
|
|
526
|
+
const store = await harness.create({ now: time.now });
|
|
527
|
+
const soon = new Date(time.at() + 5000);
|
|
528
|
+
await store.save(NOTE, browser("short"), { expiresAt: soon });
|
|
529
|
+
time.tick();
|
|
530
|
+
await store.save(NOTE, browser("long"), { expiresAt: new Date(time.at() + 60_000) });
|
|
531
|
+
time.tick();
|
|
532
|
+
await store.save(NOTE, browser("forever"));
|
|
533
|
+
await store.save(ANA, browser("short"));
|
|
534
|
+
await expectList(store, NOTE, ["forever", "long", "short"], "before expiry all are listed");
|
|
535
|
+
time.tick(3000); // exactly at `soon`: expired
|
|
536
|
+
await expectList(store, NOTE, ["forever", "long"], "an expired subscription is skipped");
|
|
537
|
+
expectSaved(await store.save(NOTE, browser("new"), { max: 3, whenFull: "refuse" }), "an expired subscription does not count against max");
|
|
538
|
+
expectEqual(await store.purgeExpired(new Date(soon.getTime() - 1)), 0, "not yet expired");
|
|
539
|
+
expectEqual(await store.purgeExpired(soon), 1, "purgeExpired deletes up to `before`");
|
|
540
|
+
await expectList(store, ANA, ["short"], "other owners of the browser keep it");
|
|
541
|
+
time.tick(60_000);
|
|
542
|
+
await expectList(store, NOTE, ["new", "forever"], "the other one expired too");
|
|
543
|
+
expectEqual(await store.purgeExpired(time.now()), 1, "and is purged");
|
|
544
|
+
expectSaved(await store.save(BOB, otherKeys("long", "fresh")), "a purged browser no owner uses any more is free again");
|
|
545
|
+
expectEqual(await store.purgeExpired(time.now()), 0, "a second purge deletes nothing");
|
|
546
|
+
time.tick();
|
|
547
|
+
await store.save(NOTE, browser("short"), { expiresAt: new Date(time.at() + 1000) });
|
|
548
|
+
time.tick();
|
|
549
|
+
await store.save(NOTE, browser("short"));
|
|
550
|
+
time.tick(5000);
|
|
551
|
+
await expectList(store, NOTE, ["short", "new", "forever"], "saving again replaces the expiry (here: never)");
|
|
552
|
+
},
|
|
553
|
+
},
|
|
554
|
+
{
|
|
555
|
+
name: "purgeExpired deletes at most limit per call, earliest expiry first, and keeps a browser another owner uses",
|
|
556
|
+
async run(harness) {
|
|
557
|
+
const time = clock();
|
|
558
|
+
const store = await harness.create({ now: time.now });
|
|
559
|
+
const names = ["p0", "p1", "p2", "p3", "p4"];
|
|
560
|
+
// Saved latest expiry first (p4 expires last), so the save order is not the expiry order.
|
|
561
|
+
for (const [i, name] of [...names].reverse().entries()) {
|
|
562
|
+
time.tick();
|
|
563
|
+
await store.save(NOTE, browser(name), { expiresAt: new Date(T0 + 10_000 - i * 1000) });
|
|
564
|
+
}
|
|
565
|
+
await store.save(ANA, browser("p0"));
|
|
566
|
+
time.tick(10_000);
|
|
567
|
+
expectEqual(await store.purgeExpired(time.now(), { limit: 2 }), 2, "at most limit");
|
|
568
|
+
await expectList(store, ANA, ["p0"], "a purged subscription's browser stays its other owner's");
|
|
569
|
+
expectSaved(await store.save(BOB, otherKeys("p1", "fresh")), "a purged browser no owner uses any more is free again");
|
|
570
|
+
expectRefused(await store.save(BOB, otherKeys("p2", "fresh")), "taken", "a later expiry is not purged yet: its browser keeps its keys");
|
|
571
|
+
expectEqual(await store.purgeExpired(time.now(), { limit: 2 }), 2, "the next two");
|
|
572
|
+
expectEqual(await store.purgeExpired(time.now(), { limit: 2 }), 1, "the last one");
|
|
573
|
+
expectEqual(await store.purgeExpired(time.now()), 0, "nothing left");
|
|
574
|
+
await expectList(store, ANA, ["p0"], "the other owner's subscription is never purged");
|
|
575
|
+
},
|
|
576
|
+
},
|
|
577
|
+
{
|
|
578
|
+
name: "deleteForOwner and delete match the endpoint exactly; a browser no owner uses is free again",
|
|
579
|
+
async run(harness) {
|
|
580
|
+
const time = clock();
|
|
581
|
+
const store = await harness.create({ now: time.now });
|
|
582
|
+
await store.save(ANA, browser("phone"));
|
|
583
|
+
await store.save(ANA, browser("phone2"));
|
|
584
|
+
await store.save(ANA, browser("Phone"));
|
|
585
|
+
const endpoint = browser("phone").endpoint;
|
|
586
|
+
for (const lookalike of [
|
|
587
|
+
endpoint.toUpperCase(),
|
|
588
|
+
`${endpoint}2`.slice(0, -2),
|
|
589
|
+
`${endpoint}/`,
|
|
590
|
+
`${endpoint} `,
|
|
591
|
+
endpoint.replace("fcm.", "FCM."),
|
|
592
|
+
]) {
|
|
593
|
+
await store.deleteForOwner(ANA, lookalike);
|
|
594
|
+
await store.delete(lookalike);
|
|
595
|
+
}
|
|
596
|
+
await store.deleteForOwner(BOB, endpoint);
|
|
597
|
+
await store.deleteForOwner({ type: "note", id: ANA.id }, endpoint);
|
|
598
|
+
await expectList(store, ANA, ["phone2", "phone", "Phone"], "look-alike endpoints and other owners delete nothing");
|
|
599
|
+
await store.deleteForOwner(ANA, endpoint);
|
|
600
|
+
await expectList(store, ANA, ["phone2", "Phone"], "the exact endpoint is deleted");
|
|
601
|
+
await store.deleteForOwner(ANA, browser("unknown").endpoint); // nothing stored: no error
|
|
602
|
+
expectSaved(await store.save(BOB, otherKeys("phone", "fresh")), "a browser whose last subscription was deleted is free again");
|
|
603
|
+
},
|
|
604
|
+
},
|
|
605
|
+
{
|
|
606
|
+
name: "deleteAllForOwner deletes every subscription of exactly that owner and counts them",
|
|
607
|
+
async run(harness) {
|
|
608
|
+
const time = clock();
|
|
609
|
+
const store = await harness.create({ now: time.now });
|
|
610
|
+
for (const name of ["n1", "n2", "n3"])
|
|
611
|
+
await store.save(NOTE, browser(name));
|
|
612
|
+
await store.save(NOTE, browser("old"), { expiresAt: new Date(time.at() + 10) });
|
|
613
|
+
await store.save({ type: "note", id: `${NOTE.id}x` }, browser("other-note"));
|
|
614
|
+
await store.save({ type: "user", id: NOTE.id }, browser("same-id"));
|
|
615
|
+
await store.save(ANA, browser("n2"));
|
|
616
|
+
time.tick();
|
|
617
|
+
expectEqual(await store.deleteAllForOwner(NOTE), 4, "it counts what it deleted, expired too");
|
|
618
|
+
await expectList(store, NOTE, [], "the owner has none left");
|
|
619
|
+
await expectList(store, ANA, ["n2"], "a browser shared with another owner stays theirs");
|
|
620
|
+
await expectList(store, { type: "note", id: `${NOTE.id}x` }, ["other-note"], "a look-alike id keeps its subscriptions");
|
|
621
|
+
await expectList(store, { type: "user", id: NOTE.id }, ["same-id"], "another type with the same id keeps its subscriptions");
|
|
622
|
+
expectEqual(await store.deleteAllForOwner(NOTE), 0, "a second call deletes nothing");
|
|
623
|
+
expectSaved(await store.save(BOB, otherKeys("n1", "fresh")), "a browser no owner uses any more is free again");
|
|
624
|
+
expectRefused(await store.save(BOB, otherKeys("n2", "fresh")), "taken", "a browser another owner still uses keeps its keys");
|
|
625
|
+
},
|
|
626
|
+
},
|
|
627
|
+
{
|
|
628
|
+
name: "no leak between owners of different types with the same id",
|
|
629
|
+
async run(harness) {
|
|
630
|
+
const time = clock();
|
|
631
|
+
const store = await harness.create({ now: time.now });
|
|
632
|
+
const id = "42";
|
|
633
|
+
const all = [
|
|
634
|
+
{ type: "user", id },
|
|
635
|
+
{ type: "note", id },
|
|
636
|
+
{ type: "order", id },
|
|
637
|
+
{ type: "note", id: "420" },
|
|
638
|
+
{ type: "note.item", id },
|
|
639
|
+
];
|
|
640
|
+
for (const [i, owner] of all.entries()) {
|
|
641
|
+
time.tick();
|
|
642
|
+
await store.save(owner, browser(`o${i}`), { max: 1 });
|
|
643
|
+
}
|
|
644
|
+
for (const [i, owner] of all.entries()) {
|
|
645
|
+
await expectList(store, owner, [`o${i}`], `${owner.type}:${owner.id} lists only its own`);
|
|
646
|
+
}
|
|
647
|
+
await store.deleteForOwner({ type: "user", id }, browser("o1").endpoint);
|
|
648
|
+
await expectList(store, { type: "note", id }, ["o1"], "delete compares the type too");
|
|
649
|
+
expectEqual(await store.deleteAllForOwner({ type: "note", id }), 1, "deleteAllForOwner matches type and id exactly");
|
|
650
|
+
for (const [i, owner] of all.entries()) {
|
|
651
|
+
if (i === 1)
|
|
652
|
+
continue;
|
|
653
|
+
await expectList(store, owner, [`o${i}`], `${owner.type}:${owner.id} keeps its own`);
|
|
654
|
+
}
|
|
655
|
+
},
|
|
656
|
+
},
|
|
657
|
+
{
|
|
658
|
+
name: "rejects a malformed owner, subscription, option or date with a TypeError that never echoes it",
|
|
659
|
+
async run(harness) {
|
|
660
|
+
const store = await harness.create({ now: clock().now });
|
|
661
|
+
const secret = "s3cr3t-value";
|
|
662
|
+
const badOwners = [
|
|
663
|
+
undefined,
|
|
664
|
+
{ type: "User", id: "1" },
|
|
665
|
+
{ type: "", id: "1" },
|
|
666
|
+
{ type: `a${"b".repeat(64)}`, id: "1" },
|
|
667
|
+
{ type: "user", id: "" },
|
|
668
|
+
{ type: "user", id: `has space ${secret}` },
|
|
669
|
+
{ type: "user", id: "x".repeat(256) },
|
|
670
|
+
{ type: `bad ${secret}`, id: "1" },
|
|
671
|
+
];
|
|
672
|
+
for (const owner of badOwners) {
|
|
673
|
+
const o = owner;
|
|
674
|
+
await rejectsWithTypeError(() => store.save(o, browser("x")), "save, bad owner", [
|
|
675
|
+
secret,
|
|
676
|
+
]);
|
|
677
|
+
await rejectsWithTypeError(() => store.listForOwner(o), "listForOwner, bad owner", [
|
|
678
|
+
secret,
|
|
679
|
+
]);
|
|
680
|
+
await rejectsWithTypeError(() => store.deleteAllForOwner(o), "deleteAllForOwner", [
|
|
681
|
+
secret,
|
|
682
|
+
]);
|
|
683
|
+
await rejectsWithTypeError(() => store.deleteForOwner(o, browser("x").endpoint), "deleteForOwner, bad owner", [secret]);
|
|
684
|
+
}
|
|
685
|
+
const endpoint = `${PREFIX}${secret}`;
|
|
686
|
+
const subscriptions = [
|
|
687
|
+
undefined,
|
|
688
|
+
{ endpoint },
|
|
689
|
+
{ endpoint: "", keys: browser("x").keys },
|
|
690
|
+
{ endpoint, keys: { p256dh: "", auth: secret } },
|
|
691
|
+
{ endpoint, keys: { p256dh: secret, auth: 7 } },
|
|
692
|
+
{ endpoint, keys: { p256dh: secret, auth: "==" } },
|
|
693
|
+
{ endpoint: `${PREFIX}${"x".repeat(2048)}`, keys: browser("x").keys },
|
|
694
|
+
];
|
|
695
|
+
for (const subscription of subscriptions) {
|
|
696
|
+
await rejectsWithTypeError(() => store.save(ANA, subscription), "save, bad subscription", [secret]);
|
|
697
|
+
}
|
|
698
|
+
const badOptions = [
|
|
699
|
+
{ max: 0 },
|
|
700
|
+
{ max: 101 },
|
|
701
|
+
{ max: 1.5 },
|
|
702
|
+
{ max: Number.NaN },
|
|
703
|
+
{ max: "10" },
|
|
704
|
+
{ whenFull: "drop" },
|
|
705
|
+
{ whenFull: secret },
|
|
706
|
+
{ expiresAt: Date.now() + 1000 },
|
|
707
|
+
{ expiresAt: new Date(Number.NaN) },
|
|
708
|
+
{ exclusive: "yes" },
|
|
709
|
+
{ maxPerSource: 2 },
|
|
710
|
+
{ source: "" },
|
|
711
|
+
{ source: `has space ${secret}` },
|
|
712
|
+
{ source: `${secret}${"x".repeat(129)}` },
|
|
713
|
+
{ source: `\u00e9${secret}` },
|
|
714
|
+
{ source: 7 },
|
|
715
|
+
{ source: secret, maxPerSource: 0 },
|
|
716
|
+
{ source: secret, maxPerSource: 101 },
|
|
717
|
+
{ source: secret, maxPerSource: 1.5 },
|
|
718
|
+
{ source: secret, maxPerSource: "2" },
|
|
719
|
+
"options",
|
|
720
|
+
];
|
|
721
|
+
for (const options of badOptions) {
|
|
722
|
+
await rejectsWithTypeError(() => store.save(ANA, browser(secret), options), "save, bad options", [secret]);
|
|
723
|
+
}
|
|
724
|
+
await rejectsWithTypeError(() => store.delete(""), "delete, empty endpoint", []);
|
|
725
|
+
for (const before of [undefined, new Date(Number.NaN), T0]) {
|
|
726
|
+
await rejectsWithTypeError(() => store.purgeExpired(before), "purgeExpired, not a valid Date", []);
|
|
727
|
+
}
|
|
728
|
+
for (const options of [{ limit: 0 }, { limit: 1001 }, { limit: 1.5 }, { limit: "5" }, 5]) {
|
|
729
|
+
await rejectsWithTypeError(() => store.purgeExpired(new Date(T0), options), "purgeExpired, bad limit", []);
|
|
730
|
+
}
|
|
731
|
+
await expectList(store, ANA, [], "nothing rejected was stored");
|
|
732
|
+
},
|
|
733
|
+
},
|
|
734
|
+
]);
|
|
735
|
+
//# sourceMappingURL=push-subscription-contract.js.map
|