@zyncbase/testing 0.4.1 → 0.4.2
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/README.md +9 -5
- package/dist/index.js +53 -32
- package/dist/runtime.d.ts +4 -1
- package/dist/wait.d.ts +5 -1
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -51,9 +51,9 @@ explicit resource management.
|
|
|
51
51
|
| `rt.serviceAuth({ sub, claims?, ttlSeconds? })` | Mint service auth (`sub` starts with `service:`) |
|
|
52
52
|
| `rt.anonymousAuth()` | Anonymous auth when the config enables it |
|
|
53
53
|
| `rt.uniqueNamespace(prefix)` | `prefix:<uuid>` for isolated namespaces |
|
|
54
|
-
| `rt.stop()` |
|
|
55
|
-
| `subscribeRows(client, collection, query?)` | Live-query handle with `rows`, `next()`, `unsubscribe()` |
|
|
56
|
-
| `nextPresence(client, predicate?, options?)` | Resolve on the next matching presence snapshot |
|
|
54
|
+
| `rt.stop()` | Run `onStop` callbacks, disconnect clients, then stop the server (idempotent) |
|
|
55
|
+
| `subscribeRows(client, collection, query?, { timeoutMs }?)` | Live-query handle with `rows`, `next()`, `unsubscribe()`; `timeoutMs` is the default for its `next()` calls |
|
|
56
|
+
| `nextPresence(client, predicate?, options?)` | Resolve on the current or next matching presence snapshot from the server |
|
|
57
57
|
| `once(client, event, options?)` | Resolve with a lifecycle event's args |
|
|
58
58
|
| `eventually(label, check, timeout?)` | Poll until truthy |
|
|
59
59
|
| `freePort()` | Reserve a free localhost port for an app listener |
|
|
@@ -61,8 +61,12 @@ explicit resource management.
|
|
|
61
61
|
| `recordWebSockets()` / `dropSockets(recorder)` | Capture and close a client's transport |
|
|
62
62
|
| `decodeJwt(token)` | Read a token payload (diagnostics only) |
|
|
63
63
|
|
|
64
|
-
|
|
65
|
-
`{ timeoutMs }`.
|
|
64
|
+
`rows.next`, `nextPresence`, and `once` default to 10 seconds and take
|
|
65
|
+
`{ timeoutMs }`. `subscribeRows`' fourth argument, `{ timeoutMs }`, changes
|
|
66
|
+
that default for the handle's `next()` calls; subscribing itself has no
|
|
67
|
+
timeout. `eventually` also defaults to 10 seconds but takes
|
|
68
|
+
the timeout as a number, `eventually(label, check, 5_000)`. `waitForHttp`
|
|
69
|
+
defaults to 30 seconds and takes `{ timeoutMs, intervalMs }`.
|
|
66
70
|
|
|
67
71
|
## Recipes
|
|
68
72
|
|
package/dist/index.js
CHANGED
|
@@ -14,15 +14,24 @@ async function freePort() {
|
|
|
14
14
|
return address.port;
|
|
15
15
|
}
|
|
16
16
|
async function waitForHttp(url, options = {}) {
|
|
17
|
-
const
|
|
17
|
+
const timeoutMs = options.timeoutMs ?? 30000;
|
|
18
|
+
const deadline = Date.now() + timeoutMs;
|
|
18
19
|
const interval = options.intervalMs ?? 100;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
20
|
+
let timer;
|
|
21
|
+
const expired = new Promise((resolve) => {
|
|
22
|
+
timer = setTimeout(() => resolve(undefined), timeoutMs);
|
|
23
|
+
});
|
|
24
|
+
try {
|
|
25
|
+
while (Date.now() < deadline) {
|
|
26
|
+
try {
|
|
27
|
+
const response = await Promise.race([fetch(url), expired]);
|
|
28
|
+
if (response?.ok)
|
|
29
|
+
return;
|
|
30
|
+
} catch {}
|
|
31
|
+
await sleep(interval);
|
|
32
|
+
}
|
|
33
|
+
} finally {
|
|
34
|
+
clearTimeout(timer);
|
|
26
35
|
}
|
|
27
36
|
throw new Error(`Timed out waiting for ${options.label ?? url} at ${url}`);
|
|
28
37
|
}
|
|
@@ -69,15 +78,12 @@ async function createTestRuntime(options) {
|
|
|
69
78
|
overrides: options.overrides
|
|
70
79
|
});
|
|
71
80
|
const clients = new Set;
|
|
72
|
-
let
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
})();
|
|
79
|
-
return stopping;
|
|
80
|
-
};
|
|
81
|
+
let closed = false;
|
|
82
|
+
inner.onStop(() => {
|
|
83
|
+
closed = true;
|
|
84
|
+
for (const client of [...clients].reverse())
|
|
85
|
+
client.disconnect();
|
|
86
|
+
});
|
|
81
87
|
inner.stopOnSignals();
|
|
82
88
|
return {
|
|
83
89
|
get url() {
|
|
@@ -101,6 +107,8 @@ async function createTestRuntime(options) {
|
|
|
101
107
|
serviceAuth: (auth) => mintServiceAuth({ secret: inner.serviceSecret, ...auth }),
|
|
102
108
|
anonymousAuth: () => ({ anonymous: true }),
|
|
103
109
|
async connect(connectOptions) {
|
|
110
|
+
if (closed)
|
|
111
|
+
throw new Error("createTestRuntime: connect() after stop()");
|
|
104
112
|
const { auth, ...rest } = connectOptions;
|
|
105
113
|
const client = createClient({
|
|
106
114
|
url: inner.databaseUrl,
|
|
@@ -119,21 +127,30 @@ async function createTestRuntime(options) {
|
|
|
119
127
|
},
|
|
120
128
|
uniqueNamespace: (prefix) => `${prefix}:${crypto.randomUUID()}`,
|
|
121
129
|
onStop: (callback) => inner.onStop(callback),
|
|
122
|
-
stop,
|
|
123
|
-
[Symbol.asyncDispose]: stop
|
|
130
|
+
stop: () => inner.stop(),
|
|
131
|
+
[Symbol.asyncDispose]: () => inner.stop()
|
|
124
132
|
};
|
|
125
133
|
}
|
|
126
134
|
// src/wait.ts
|
|
127
135
|
var DEFAULT_TIMEOUT_MS = 1e4;
|
|
128
136
|
var POLL_INTERVAL_MS = 50;
|
|
129
137
|
var sleep2 = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
138
|
+
var toError = (error) => error instanceof Error ? error : new Error(String(error));
|
|
130
139
|
async function eventually(label, check, timeoutMs = DEFAULT_TIMEOUT_MS) {
|
|
131
140
|
const deadline = Date.now() + timeoutMs;
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
141
|
+
let timer;
|
|
142
|
+
const expired = new Promise((resolve) => {
|
|
143
|
+
timer = setTimeout(() => resolve(undefined), timeoutMs);
|
|
144
|
+
});
|
|
145
|
+
try {
|
|
146
|
+
while (Date.now() < deadline) {
|
|
147
|
+
const value = await Promise.race([check(), expired]);
|
|
148
|
+
if (value)
|
|
149
|
+
return value;
|
|
150
|
+
await sleep2(POLL_INTERVAL_MS);
|
|
151
|
+
}
|
|
152
|
+
} finally {
|
|
153
|
+
clearTimeout(timer);
|
|
137
154
|
}
|
|
138
155
|
throw new Error(`Timed out: ${label}`);
|
|
139
156
|
}
|
|
@@ -150,7 +167,7 @@ async function subscribeRows(client, collection, options = {}, wait = {}) {
|
|
|
150
167
|
} catch (error) {
|
|
151
168
|
clearTimeout(waiter.timer);
|
|
152
169
|
waiters.delete(waiter);
|
|
153
|
-
waiter.reject(
|
|
170
|
+
waiter.reject(toError(error));
|
|
154
171
|
continue;
|
|
155
172
|
}
|
|
156
173
|
if (!matches)
|
|
@@ -200,11 +217,9 @@ async function subscribeRows(client, collection, options = {}, wait = {}) {
|
|
|
200
217
|
}
|
|
201
218
|
};
|
|
202
219
|
}
|
|
203
|
-
|
|
220
|
+
function nextPresence(client, predicate = () => true, options = {}) {
|
|
204
221
|
const timeout = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
205
|
-
let users =
|
|
206
|
-
if (predicate(users))
|
|
207
|
-
return users;
|
|
222
|
+
let users = [];
|
|
208
223
|
return new Promise((resolve, reject) => {
|
|
209
224
|
let unsubscribe;
|
|
210
225
|
let settled = false;
|
|
@@ -224,14 +239,20 @@ async function nextPresence(client, predicate = () => true, options = {}) {
|
|
|
224
239
|
finish(new Error(`Timed out after ${timeout}ms waiting for presence (last: ${users.length} users)`));
|
|
225
240
|
}, timeout);
|
|
226
241
|
client.presence.subscribe((next) => {
|
|
242
|
+
if (settled)
|
|
243
|
+
return;
|
|
227
244
|
users = next;
|
|
228
|
-
|
|
229
|
-
|
|
245
|
+
try {
|
|
246
|
+
if (predicate(users))
|
|
247
|
+
finish();
|
|
248
|
+
} catch (error) {
|
|
249
|
+
finish(toError(error));
|
|
250
|
+
}
|
|
230
251
|
}).then((stop) => {
|
|
231
252
|
unsubscribe = stop;
|
|
232
253
|
if (settled)
|
|
233
254
|
stop().catch(() => {});
|
|
234
|
-
}, (error) => finish(
|
|
255
|
+
}, (error) => finish(toError(error)));
|
|
235
256
|
});
|
|
236
257
|
}
|
|
237
258
|
function once(client, event, options = {}) {
|
package/dist/runtime.d.ts
CHANGED
|
@@ -39,7 +39,10 @@ export interface TestRuntime {
|
|
|
39
39
|
/** A namespace unique to this call: `room:<uuid>` for prefix `room`. */
|
|
40
40
|
uniqueNamespace(prefix: string): string;
|
|
41
41
|
onStop(callback: () => void | Promise<void>): void;
|
|
42
|
-
/**
|
|
42
|
+
/**
|
|
43
|
+
* Runs `onStop` callbacks, disconnects tracked clients, then stops the
|
|
44
|
+
* server. Idempotent; `connect()` rejects once the clients are disconnected.
|
|
45
|
+
*/
|
|
43
46
|
stop(): Promise<void>;
|
|
44
47
|
[Symbol.asyncDispose](): Promise<void>;
|
|
45
48
|
}
|
package/dist/wait.d.ts
CHANGED
|
@@ -13,7 +13,11 @@ export interface RowsHandle<Row> {
|
|
|
13
13
|
}
|
|
14
14
|
/** Subscribes to a live query and exposes awaitable snapshots. */
|
|
15
15
|
export declare function subscribeRows<S extends object, C extends CollectionsOf<S>, A, P>(client: ZyncBaseClient<S, A, P>, collection: C, options?: QueryOptionsFor<S, C>, wait?: WaitOptions): Promise<RowsHandle<DocumentRowOf<S, C>>>;
|
|
16
|
-
/**
|
|
16
|
+
/**
|
|
17
|
+
* Resolves with the next server presence snapshot matching `predicate` (or
|
|
18
|
+
* the current one). Only subscription snapshots count: the local cache is
|
|
19
|
+
* empty until the server answers, so it is never checked.
|
|
20
|
+
*/
|
|
17
21
|
export declare function nextPresence<S extends object, A, P>(client: ZyncBaseClient<S, A, P>, predicate?: (users: PresenceEntry<P>[]) => boolean, options?: WaitOptions): Promise<PresenceEntry<P>[]>;
|
|
18
22
|
/** Resolves with the args of the next lifecycle event. */
|
|
19
23
|
export declare function once<E extends LifecycleEvent>(client: Pick<ZyncBaseClient, "on" | "off">, event: E, options?: WaitOptions): Promise<Parameters<ClientEvents[E]>>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zyncbase/testing",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.2",
|
|
4
4
|
"description": "Test helpers for ZyncBase apps: ephemeral runtime, clients, auth, and waiters.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
@@ -31,15 +31,15 @@
|
|
|
31
31
|
"build": "rm -rf dist && bun build --root src src/index.ts --outdir dist --format esm --target node --external zyncbase --external 'zyncbase/*' --external @zyncbase/client --external '@zyncbase/client/*' --external @zyncbase/backend --external '@zyncbase/backend/*' && tsc --emitDeclarationOnly --outDir dist"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
|
-
"@zyncbase/backend": "^0.4.
|
|
35
|
-
"@zyncbase/client": "^0.4.
|
|
36
|
-
"zyncbase": "^0.4.
|
|
34
|
+
"@zyncbase/backend": "^0.4.2",
|
|
35
|
+
"@zyncbase/client": "^0.4.2",
|
|
36
|
+
"zyncbase": "^0.4.2"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@types/bun": "^1.3.11",
|
|
40
|
-
"@zyncbase/backend": "
|
|
41
|
-
"@zyncbase/client": "
|
|
40
|
+
"@zyncbase/backend": "workspace:*",
|
|
41
|
+
"@zyncbase/client": "workspace:*",
|
|
42
42
|
"typescript": "^5.0.0",
|
|
43
|
-
"zyncbase": "
|
|
43
|
+
"zyncbase": "workspace:*"
|
|
44
44
|
}
|
|
45
45
|
}
|