@zveltio/sdk 2.0.8 → 3.0.0-beta.100
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 +17 -4
- package/dist/build/index.d.ts +77 -0
- package/dist/build/index.d.ts.map +1 -0
- package/dist/build/index.js +157 -0
- package/dist/client.d.ts +103 -7
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +129 -28
- package/dist/core.d.ts.map +1 -1
- package/dist/core.js +21 -3
- package/dist/crdt.d.ts.map +1 -1
- package/dist/crdt.js +11 -2
- package/dist/ddl/index.d.ts +173 -0
- package/dist/ddl/index.d.ts.map +1 -0
- package/dist/ddl/index.js +33 -0
- package/dist/errors.d.ts +58 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +124 -0
- package/dist/extension/action-refusal.d.ts +27 -0
- package/dist/extension/action-refusal.d.ts.map +1 -0
- package/dist/extension/action-refusal.js +29 -0
- package/dist/extension/capabilities.d.ts +71 -0
- package/dist/extension/capabilities.d.ts.map +1 -0
- package/dist/extension/capabilities.js +79 -0
- package/dist/extension/dates.d.ts +7 -0
- package/dist/extension/dates.d.ts.map +1 -0
- package/dist/extension/dates.js +14 -0
- package/dist/extension/index.d.ts +1570 -10
- package/dist/extension/index.d.ts.map +1 -1
- package/dist/extension/index.js +17 -1
- package/dist/extension/jsonb.d.ts +62 -0
- package/dist/extension/jsonb.d.ts.map +1 -0
- package/dist/extension/jsonb.js +64 -0
- package/dist/extension/multipart.d.ts +58 -0
- package/dist/extension/multipart.d.ts.map +1 -0
- package/dist/extension/multipart.js +61 -0
- package/dist/extension/numeric.d.ts +94 -0
- package/dist/extension/numeric.d.ts.map +1 -0
- package/dist/extension/numeric.js +162 -0
- package/dist/extension/permission-gate.d.ts +52 -0
- package/dist/extension/permission-gate.d.ts.map +1 -0
- package/dist/extension/permission-gate.js +125 -0
- package/dist/extension/reserved-paths.d.ts +10 -0
- package/dist/extension/reserved-paths.d.ts.map +1 -0
- package/dist/extension/reserved-paths.js +23 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/local-store.d.ts +14 -1
- package/dist/local-store.d.ts.map +1 -1
- package/dist/local-store.js +93 -16
- package/dist/offline/index.d.ts +75 -0
- package/dist/offline/index.d.ts.map +1 -0
- package/dist/offline/index.js +251 -0
- package/dist/publish/index.d.ts +94 -0
- package/dist/publish/index.d.ts.map +1 -0
- package/dist/publish/index.js +153 -0
- package/dist/realtime.d.ts +25 -1
- package/dist/realtime.d.ts.map +1 -1
- package/dist/realtime.js +53 -5
- package/dist/rpc/index.d.ts +68 -0
- package/dist/rpc/index.d.ts.map +1 -0
- package/dist/rpc/index.js +63 -0
- package/dist/schema-codegen.d.ts +84 -0
- package/dist/schema-codegen.d.ts.map +1 -0
- package/dist/schema-codegen.js +503 -0
- package/dist/schema-watcher.d.ts +10 -2
- package/dist/schema-watcher.d.ts.map +1 -1
- package/dist/schema-watcher.js +26 -13
- package/dist/studio/index.d.ts +85 -0
- package/dist/studio/index.d.ts.map +1 -0
- package/dist/studio/index.js +183 -0
- package/dist/studio/slot-owner-registry.d.ts +13 -0
- package/dist/studio/slot-owner-registry.d.ts.map +1 -0
- package/dist/studio/slot-owner-registry.js +21 -0
- package/dist/svelte.d.ts +1 -1
- package/dist/svelte.d.ts.map +1 -1
- package/dist/svelte.js +4 -2
- package/dist/sync-manager.d.ts +49 -4
- package/dist/sync-manager.d.ts.map +1 -1
- package/dist/sync-manager.js +175 -24
- package/dist/testing/index.d.ts +121 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +305 -0
- package/dist/testing/with-test-db.d.ts +119 -0
- package/dist/testing/with-test-db.d.ts.map +1 -0
- package/dist/testing/with-test-db.js +320 -0
- package/dist/tests/action-refusal.test.d.ts +2 -0
- package/dist/tests/action-refusal.test.d.ts.map +1 -0
- package/dist/tests/action-refusal.test.js +13 -0
- package/dist/tests/client.test.d.ts +2 -0
- package/dist/tests/client.test.d.ts.map +1 -0
- package/dist/tests/client.test.js +164 -0
- package/dist/tests/crdt-orset.test.d.ts +2 -0
- package/dist/tests/crdt-orset.test.d.ts.map +1 -0
- package/dist/tests/crdt-orset.test.js +23 -0
- package/dist/tests/local-store.test.js +44 -0
- package/dist/tests/migration-parse.test.d.ts +2 -0
- package/dist/tests/migration-parse.test.d.ts.map +1 -0
- package/dist/tests/migration-parse.test.js +17 -0
- package/dist/tests/multipart.test.d.ts +2 -0
- package/dist/tests/multipart.test.d.ts.map +1 -0
- package/dist/tests/multipart.test.js +37 -0
- package/dist/tests/offline-provider-crdt.test.d.ts +2 -0
- package/dist/tests/offline-provider-crdt.test.d.ts.map +1 -0
- package/dist/tests/offline-provider-crdt.test.js +206 -0
- package/dist/tests/realtime.test.d.ts +2 -0
- package/dist/tests/realtime.test.d.ts.map +1 -0
- package/dist/tests/realtime.test.js +220 -0
- package/dist/tests/reserved-paths.test.d.ts +2 -0
- package/dist/tests/reserved-paths.test.d.ts.map +1 -0
- package/dist/tests/reserved-paths.test.js +21 -0
- package/dist/tests/sync-manager.test.d.ts +2 -0
- package/dist/tests/sync-manager.test.d.ts.map +1 -0
- package/dist/tests/sync-manager.test.js +123 -0
- package/dist/tests/today-in.test.d.ts +2 -0
- package/dist/tests/today-in.test.d.ts.map +1 -0
- package/dist/tests/today-in.test.js +14 -0
- package/dist/tests/validate-manifest-actions.test.d.ts +2 -0
- package/dist/tests/validate-manifest-actions.test.d.ts.map +1 -0
- package/dist/tests/validate-manifest-actions.test.js +82 -0
- package/dist/tests/validate-manifest-dependencies.test.d.ts +2 -0
- package/dist/tests/validate-manifest-dependencies.test.d.ts.map +1 -0
- package/dist/tests/validate-manifest-dependencies.test.js +27 -0
- package/dist/tests/validate-manifest-forward-credentials.test.d.ts +2 -0
- package/dist/tests/validate-manifest-forward-credentials.test.d.ts.map +1 -0
- package/dist/tests/validate-manifest-forward-credentials.test.js +35 -0
- package/dist/tests/validate-sdui-data-endpoints.test.d.ts +2 -0
- package/dist/tests/validate-sdui-data-endpoints.test.d.ts.map +1 -0
- package/dist/tests/validate-sdui-data-endpoints.test.js +41 -0
- package/dist/tests/with-test-db.test.d.ts +2 -0
- package/dist/tests/with-test-db.test.d.ts.map +1 -0
- package/dist/tests/with-test-db.test.js +57 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +3 -1
- package/dist/validate/index.d.ts +148 -0
- package/dist/validate/index.d.ts.map +1 -0
- package/dist/validate/index.js +887 -0
- package/dist/validate/migration-parse.d.ts +22 -0
- package/dist/validate/migration-parse.d.ts.map +1 -0
- package/dist/validate/migration-parse.js +11 -0
- package/dist/validate/shared-message-keys.d.ts +16 -0
- package/dist/validate/shared-message-keys.d.ts.map +1 -0
- package/dist/validate/shared-message-keys.js +227 -0
- package/package.json +56 -4
- package/dist/client/Auth.d.ts +0 -19
- package/dist/client/Auth.d.ts.map +0 -1
- package/dist/client/Auth.js +0 -45
- package/dist/client/QueryBuilder.d.ts +0 -17
- package/dist/client/QueryBuilder.d.ts.map +0 -1
- package/dist/client/QueryBuilder.js +0 -76
- package/dist/client/RealtimeClient.d.ts +0 -22
- package/dist/client/RealtimeClient.d.ts.map +0 -1
- package/dist/client/RealtimeClient.js +0 -96
- package/dist/client/ZveltioClient.d.ts +0 -20
- package/dist/client/ZveltioClient.d.ts.map +0 -1
- package/dist/client/ZveltioClient.js +0 -78
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Testing helpers for Zveltio extensions (S4-06).
|
|
3
|
+
*
|
|
4
|
+
* Goal: give extension authors a one-liner to spin up a fake
|
|
5
|
+
* `ExtensionContext` and a Hono test app, so unit tests run without a
|
|
6
|
+
* real database or auth setup. For integration tests against a real
|
|
7
|
+
* Postgres + engine, use Bun's `testcontainers` directly — this module
|
|
8
|
+
* is the offline-fast path.
|
|
9
|
+
*
|
|
10
|
+
* Three primitives:
|
|
11
|
+
* - `createTestContext(overrides?)` — minimal `ExtensionContext` with
|
|
12
|
+
* no-op stubs for everything (db, auth, events, services,
|
|
13
|
+
* queryAlter, entityAccess, registerPublicRoute). Override per-test.
|
|
14
|
+
* - `createTestApp(extension, ctx?)` — runs `extension.register()`
|
|
15
|
+
* against a fresh Hono and returns the app, ready for `.request()`.
|
|
16
|
+
* - `mockDb(presets?)` — records query-builder calls; returns presets
|
|
17
|
+
* when an `.execute()` / `.executeTakeFirst()` lands. Enough to
|
|
18
|
+
* fake CRUD paths; falls back to empty arrays / undefined.
|
|
19
|
+
*/
|
|
20
|
+
import { Hono } from 'hono';
|
|
21
|
+
/**
|
|
22
|
+
* Build a recording mock Kysely-shaped object. Any method chain is
|
|
23
|
+
* accepted; the terminal `.execute()` / `.executeTakeFirst()` /
|
|
24
|
+
* `.executeTakeFirstOrThrow()` returns a preset if matched, otherwise
|
|
25
|
+
* an empty list (for execute) or undefined.
|
|
26
|
+
*/
|
|
27
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
28
|
+
export function mockDb(presets = {}) {
|
|
29
|
+
const calls = [];
|
|
30
|
+
const presetMap = new Map(Object.entries(presets));
|
|
31
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
32
|
+
function buildChain(prefix) {
|
|
33
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
34
|
+
const proxy = new Proxy(() => { }, {
|
|
35
|
+
get(_, prop) {
|
|
36
|
+
if (typeof prop === 'symbol')
|
|
37
|
+
return undefined;
|
|
38
|
+
if (prop === 'calls')
|
|
39
|
+
return calls;
|
|
40
|
+
if (prop === 'reset')
|
|
41
|
+
return () => {
|
|
42
|
+
calls.length = 0;
|
|
43
|
+
};
|
|
44
|
+
if (prop === 'preset')
|
|
45
|
+
return (chain, value) => {
|
|
46
|
+
presetMap.set(chain, value);
|
|
47
|
+
};
|
|
48
|
+
if (prop === 'then')
|
|
49
|
+
return undefined;
|
|
50
|
+
// Build next link in the chain.
|
|
51
|
+
return buildChain([...prefix, prop]);
|
|
52
|
+
},
|
|
53
|
+
// NOTE: synchronous apply. Terminal calls return a Promise; non-
|
|
54
|
+
// terminal calls return the proxy itself so further chaining works
|
|
55
|
+
// without awaiting. (Async apply would wrap every step in a Promise
|
|
56
|
+
// and break `.selectFrom().selectAll().execute()` chains.)
|
|
57
|
+
apply(_t, _this, args) {
|
|
58
|
+
const chain = prefix.join('.');
|
|
59
|
+
calls.push({ chain, args });
|
|
60
|
+
const terminal = prefix[prefix.length - 1];
|
|
61
|
+
const isTerminal = terminal === 'execute' ||
|
|
62
|
+
terminal === 'executeTakeFirst' ||
|
|
63
|
+
terminal === 'executeTakeFirstOrThrow';
|
|
64
|
+
if (isTerminal) {
|
|
65
|
+
const preset = matchPreset(chain, presetMap);
|
|
66
|
+
if (preset !== undefined) {
|
|
67
|
+
const resolved = typeof preset === 'function' ? preset(...args) : preset;
|
|
68
|
+
return Promise.resolve(resolved);
|
|
69
|
+
}
|
|
70
|
+
if (terminal === 'execute')
|
|
71
|
+
return Promise.resolve([]);
|
|
72
|
+
if (terminal === 'executeTakeFirst')
|
|
73
|
+
return Promise.resolve(undefined);
|
|
74
|
+
// executeTakeFirstOrThrow with no preset → reject
|
|
75
|
+
return Promise.reject(new Error(`mockDb: no preset for chain "${chain}" and executeTakeFirstOrThrow has no fallback`));
|
|
76
|
+
}
|
|
77
|
+
// Non-terminal: keep chaining on the same proxy (its prefix is
|
|
78
|
+
// unchanged; the next property access will extend via get()).
|
|
79
|
+
return proxy;
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
return proxy;
|
|
83
|
+
}
|
|
84
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
85
|
+
return buildChain([]);
|
|
86
|
+
}
|
|
87
|
+
function matchPreset(chain, presets) {
|
|
88
|
+
// Exact match wins.
|
|
89
|
+
if (presets.has(chain))
|
|
90
|
+
return presets.get(chain);
|
|
91
|
+
// Suffix match: preset 'foo.execute' matches 'selectFrom.x.foo.execute'.
|
|
92
|
+
for (const [key, val] of presets) {
|
|
93
|
+
if (chain.endsWith(key))
|
|
94
|
+
return val;
|
|
95
|
+
}
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
export function mockEventBus() {
|
|
99
|
+
const emitted = [];
|
|
100
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
101
|
+
const listeners = new Map();
|
|
102
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
103
|
+
const beforeHandlers = new Map();
|
|
104
|
+
return {
|
|
105
|
+
emit(event, payload) {
|
|
106
|
+
emitted.push({ event, payload });
|
|
107
|
+
for (const h of listeners.get(event) ?? [])
|
|
108
|
+
h(payload);
|
|
109
|
+
},
|
|
110
|
+
on(event, handler) {
|
|
111
|
+
const list = listeners.get(event) ?? [];
|
|
112
|
+
list.push(handler);
|
|
113
|
+
listeners.set(event, list);
|
|
114
|
+
return () => {
|
|
115
|
+
const cur = listeners.get(event);
|
|
116
|
+
if (cur) {
|
|
117
|
+
const idx = cur.indexOf(handler);
|
|
118
|
+
if (idx >= 0)
|
|
119
|
+
cur.splice(idx, 1);
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
},
|
|
123
|
+
onBefore(event, handler) {
|
|
124
|
+
const list = beforeHandlers.get(event) ?? [];
|
|
125
|
+
list.push(handler);
|
|
126
|
+
beforeHandlers.set(event, list);
|
|
127
|
+
return () => {
|
|
128
|
+
const cur = beforeHandlers.get(event);
|
|
129
|
+
if (cur) {
|
|
130
|
+
const idx = cur.indexOf(handler);
|
|
131
|
+
if (idx >= 0)
|
|
132
|
+
cur.splice(idx, 1);
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
},
|
|
136
|
+
async runBefore(event, seed) {
|
|
137
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
138
|
+
const payload = { ...seed };
|
|
139
|
+
let aborted = false;
|
|
140
|
+
let abortReason = '';
|
|
141
|
+
payload.abort = (reason) => {
|
|
142
|
+
aborted = true;
|
|
143
|
+
abortReason = reason;
|
|
144
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
145
|
+
const err = new Error(`Aborted: ${reason}`);
|
|
146
|
+
err.name = 'AbortHookError';
|
|
147
|
+
err.reason = reason;
|
|
148
|
+
throw err;
|
|
149
|
+
};
|
|
150
|
+
payload.mutate = (patch) => {
|
|
151
|
+
const key = event === 'record.beforeUpdate' ? 'patch' : 'data';
|
|
152
|
+
payload[key] = { ...payload[key], ...patch };
|
|
153
|
+
};
|
|
154
|
+
for (const h of beforeHandlers.get(event) ?? []) {
|
|
155
|
+
await h(payload);
|
|
156
|
+
}
|
|
157
|
+
return payload;
|
|
158
|
+
},
|
|
159
|
+
get emitted() {
|
|
160
|
+
return emitted;
|
|
161
|
+
},
|
|
162
|
+
reset() {
|
|
163
|
+
emitted.length = 0;
|
|
164
|
+
listeners.clear();
|
|
165
|
+
beforeHandlers.clear();
|
|
166
|
+
},
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
// ─── Mock service registry ─────────────────────────────────────────────────
|
|
170
|
+
export function mockServiceRegistry() {
|
|
171
|
+
const services = new Map();
|
|
172
|
+
return {
|
|
173
|
+
register(name, value) {
|
|
174
|
+
services.set(name, value);
|
|
175
|
+
},
|
|
176
|
+
unregister(name) {
|
|
177
|
+
services.delete(name);
|
|
178
|
+
},
|
|
179
|
+
get(name) {
|
|
180
|
+
return services.get(name) ?? null;
|
|
181
|
+
},
|
|
182
|
+
has(name) {
|
|
183
|
+
return services.has(name);
|
|
184
|
+
},
|
|
185
|
+
list() {
|
|
186
|
+
return [...services.keys()];
|
|
187
|
+
},
|
|
188
|
+
async waitFor(name) {
|
|
189
|
+
const v = services.get(name);
|
|
190
|
+
if (v === undefined)
|
|
191
|
+
throw new Error(`mockServiceRegistry: no service "${name}" registered`);
|
|
192
|
+
return v;
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
// ─── Mock queryAlter / entityAccess scoped registries ──────────────────────
|
|
197
|
+
function mockQueryAlterScope() {
|
|
198
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
199
|
+
const entries = [];
|
|
200
|
+
return {
|
|
201
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
202
|
+
register(def) {
|
|
203
|
+
entries.push(def);
|
|
204
|
+
},
|
|
205
|
+
list() {
|
|
206
|
+
return entries.map((e) => ({ table: e.table }));
|
|
207
|
+
},
|
|
208
|
+
unregisterAll() {
|
|
209
|
+
entries.length = 0;
|
|
210
|
+
},
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
function mockEntityAccessScope() {
|
|
214
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
215
|
+
const entries = [];
|
|
216
|
+
return {
|
|
217
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
218
|
+
register(def) {
|
|
219
|
+
entries.push(def);
|
|
220
|
+
},
|
|
221
|
+
list() {
|
|
222
|
+
return entries.map((e) => ({ table: e.table }));
|
|
223
|
+
},
|
|
224
|
+
unregisterAll() {
|
|
225
|
+
entries.length = 0;
|
|
226
|
+
},
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
230
|
+
export function mockAuth(opts = {}) {
|
|
231
|
+
const user = opts.user === undefined ? { id: 'test-user', email: 'test@example.com', roles: [] } : opts.user;
|
|
232
|
+
return {
|
|
233
|
+
api: {
|
|
234
|
+
async getSession() {
|
|
235
|
+
return user === null ? null : { user };
|
|
236
|
+
},
|
|
237
|
+
},
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
241
|
+
export function createTestContext(opts = {}) {
|
|
242
|
+
const db = opts.db ?? mockDb();
|
|
243
|
+
const events = opts.events ?? mockEventBus();
|
|
244
|
+
return {
|
|
245
|
+
db,
|
|
246
|
+
auth: opts.auth ?? mockAuth(),
|
|
247
|
+
fieldTypeRegistry: {
|
|
248
|
+
register: () => { },
|
|
249
|
+
get: () => undefined,
|
|
250
|
+
has: () => false,
|
|
251
|
+
list: () => [],
|
|
252
|
+
getAll: () => [],
|
|
253
|
+
deserialize: (_t, v) => v,
|
|
254
|
+
serialize: (_t, v) => v,
|
|
255
|
+
},
|
|
256
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
257
|
+
events: events,
|
|
258
|
+
checkPermission: async () => true,
|
|
259
|
+
getUserRoles: async () => ['admin'],
|
|
260
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
261
|
+
DDLManager: {},
|
|
262
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
263
|
+
services: mockServiceRegistry(),
|
|
264
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
265
|
+
queryAlter: mockQueryAlterScope(),
|
|
266
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
267
|
+
entityAccess: mockEntityAccessScope(),
|
|
268
|
+
registerPublicRoute: () => { },
|
|
269
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
270
|
+
internals: {},
|
|
271
|
+
...(opts.extra ?? {}),
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Spin up a Hono test app with the extension's routes registered. Returns
|
|
276
|
+
* the outer Hono so callers can use `.request(path, init)` for assertions.
|
|
277
|
+
*
|
|
278
|
+
* Honors the extension's `mountStrategy`. For `'subapp'`, the outer app
|
|
279
|
+
* mounts the sub-app at `/ext/<extension.name>` by default — same shape
|
|
280
|
+
* the engine produces in production.
|
|
281
|
+
*/
|
|
282
|
+
export async function createTestApp(
|
|
283
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy any; tracked in hardening plan item H-01
|
|
284
|
+
extension, opts = {}) {
|
|
285
|
+
const ctx = opts.ctx ?? createTestContext();
|
|
286
|
+
const strategy = extension.mountStrategy ?? 'global';
|
|
287
|
+
if (strategy === 'subapp') {
|
|
288
|
+
const outer = new Hono();
|
|
289
|
+
const sub = new Hono();
|
|
290
|
+
await extension.register(sub, ctx);
|
|
291
|
+
const mountPath = opts.mountSubappAt === false ? '/' : (opts.mountSubappAt ?? `/ext/${extension.name}`);
|
|
292
|
+
outer.route(mountPath, sub);
|
|
293
|
+
return outer;
|
|
294
|
+
}
|
|
295
|
+
const app = new Hono();
|
|
296
|
+
await extension.register(app, ctx);
|
|
297
|
+
return app;
|
|
298
|
+
}
|
|
299
|
+
// ─── Integration test helpers (Postgres) ─────────────────────────────────
|
|
300
|
+
//
|
|
301
|
+
// `withTestDb` + friends live in a separate module so the testcontainers
|
|
302
|
+
// dynamic import doesn't fan out on every consumer of `mockDb` /
|
|
303
|
+
// `createTestContext`. Re-exported here for the
|
|
304
|
+
// `import { withTestDb } from '@zveltio/sdk/testing'` ergonomics.
|
|
305
|
+
export { withTestDb, startTestDb, applyMigrationStrings, applyMigrationFiles, stopReusedTestDb, splitStatements, } from './with-test-db.js';
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Postgres-backed integration test helper (S4-06 follow-up).
|
|
3
|
+
*
|
|
4
|
+
* `withTestDb(fn)` spins up a real Postgres container via `testcontainers`,
|
|
5
|
+
* yields a Kysely instance connected to it, runs the user's callback, then
|
|
6
|
+
* tears the container down. Use this when `mockDb` is insufficient — e.g.
|
|
7
|
+
* when an extension hook chains a real SELECT through `ctx.db`, or when you
|
|
8
|
+
* want to verify migrations apply cleanly.
|
|
9
|
+
*
|
|
10
|
+
* Requires:
|
|
11
|
+
* - Docker / Podman running on the test host.
|
|
12
|
+
* - `testcontainers` installed (peer dep — declare as devDep in the
|
|
13
|
+
* extension's package.json). We import it dynamically so unit-test
|
|
14
|
+
* workflows that never call `withTestDb` don't take the heavy hit.
|
|
15
|
+
*
|
|
16
|
+
* Cost model:
|
|
17
|
+
* - First call: ~3-5s (image pull + container start). Subsequent calls
|
|
18
|
+
* reuse the cached image, ~1-2s. Use one container across a `describe()`
|
|
19
|
+
* block when you can — the wrapper supports nesting via `reuse`.
|
|
20
|
+
*
|
|
21
|
+
* Why a peer dep + dynamic import:
|
|
22
|
+
* - `testcontainers` pulls `dockerode` + a bunch of Node-ish modules. We
|
|
23
|
+
* don't want every consumer of `@zveltio/sdk/testing` to inherit that
|
|
24
|
+
* weight just to use `mockDb`. By importing lazily, the cost is
|
|
25
|
+
* opt-in at the call site.
|
|
26
|
+
*/
|
|
27
|
+
import type { Kysely } from 'kysely';
|
|
28
|
+
export interface WithTestDbOptions {
|
|
29
|
+
/** Postgres image. Default: `pgvector/pgvector:pg18` (the image CI and docker-compose use). */
|
|
30
|
+
image?: string;
|
|
31
|
+
/** DB name to create. Default: `zveltio_test`. */
|
|
32
|
+
database?: string;
|
|
33
|
+
/** Optional SQL strings to run after the container is ready. */
|
|
34
|
+
migrations?: string[];
|
|
35
|
+
/**
|
|
36
|
+
* Container start timeout, ms. Default: 60_000. Cold pulls can be slow
|
|
37
|
+
* on the first run; CI environments may want 120_000.
|
|
38
|
+
*/
|
|
39
|
+
startupTimeoutMs?: number;
|
|
40
|
+
/**
|
|
41
|
+
* If passed, the container is started once and reused. Pass `false`
|
|
42
|
+
* (default) to start + stop per call.
|
|
43
|
+
*/
|
|
44
|
+
reuse?: boolean;
|
|
45
|
+
}
|
|
46
|
+
export interface TestDb {
|
|
47
|
+
/** Kysely instance — same shape as `ctx.db` in production. */
|
|
48
|
+
db: Kysely<any>;
|
|
49
|
+
/** Connection string the container exposed. */
|
|
50
|
+
connectionString: string;
|
|
51
|
+
/** Stop + remove the container (also closes the Kysely pool). */
|
|
52
|
+
cleanup(): Promise<void>;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Spin up Postgres, optionally run migrations, return a `TestDb` handle.
|
|
56
|
+
* The caller is responsible for calling `cleanup()` (typically inside an
|
|
57
|
+
* `afterAll`/`afterEach`).
|
|
58
|
+
*
|
|
59
|
+
* For the more common "scope the lifecycle to a test/describe block"
|
|
60
|
+
* pattern, prefer `withTestDb(fn)` below.
|
|
61
|
+
*/
|
|
62
|
+
export declare function startTestDb(opts?: WithTestDbOptions): Promise<TestDb>;
|
|
63
|
+
/**
|
|
64
|
+
* Convenience wrapper: starts a TestDb, runs `fn`, always cleans up. The
|
|
65
|
+
* 95% case for integration tests. Returns whatever `fn` returns.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* import { withTestDb } from '@zveltio/sdk/testing';
|
|
69
|
+
* import myExtension from '../engine';
|
|
70
|
+
*
|
|
71
|
+
* test('extension creates contact rows', async () => {
|
|
72
|
+
* await withTestDb(async (db) => {
|
|
73
|
+
* await db.schema.createTable('zvd_contacts').addColumn(...).execute();
|
|
74
|
+
* const ctx = createTestContext({ db });
|
|
75
|
+
* const app = await createTestApp(myExtension, { ctx });
|
|
76
|
+
* const res = await app.request('/ext/my/contacts', { method: 'POST', ... });
|
|
77
|
+
* expect(res.status).toBe(201);
|
|
78
|
+
* const rows = await db.selectFrom('zvd_contacts').selectAll().execute();
|
|
79
|
+
* expect(rows).toHaveLength(1);
|
|
80
|
+
* });
|
|
81
|
+
* });
|
|
82
|
+
*/
|
|
83
|
+
export declare function withTestDb<T>(fnOrOpts: ((db: Kysely<any>) => Promise<T>) | WithTestDbOptions, maybeFn?: (db: Kysely<any>) => Promise<T>): Promise<T>;
|
|
84
|
+
/**
|
|
85
|
+
* Apply an ordered list of SQL strings against the db. Each entry runs in
|
|
86
|
+
* its own statement; semicolons in the source string split into multiple
|
|
87
|
+
* statements. Useful for replaying engine + extension migrations against
|
|
88
|
+
* the test db.
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* import { readFileSync } from 'fs';
|
|
92
|
+
* import { sync as glob } from 'glob';
|
|
93
|
+
* const files = glob('engine/migrations/*.sql').map((p) => readFileSync(p, 'utf8'));
|
|
94
|
+
* await applyMigrationStrings(db, files);
|
|
95
|
+
*/
|
|
96
|
+
export declare function applyMigrationStrings(db: Kysely<any>, sqlStrings: string[]): Promise<void>;
|
|
97
|
+
/**
|
|
98
|
+
* Same as `applyMigrationStrings` but takes file paths. Reads each file
|
|
99
|
+
* synchronously, applies in order. Bun-native — uses `Bun.file()`.
|
|
100
|
+
*/
|
|
101
|
+
export declare function applyMigrationFiles(db: Kysely<any>, paths: string[]): Promise<void>;
|
|
102
|
+
/**
|
|
103
|
+
* Stop any reuse-cached container. Call this from a `globalTeardown` /
|
|
104
|
+
* `afterAll` so test runs don't leave dangling containers. No-op when no
|
|
105
|
+
* reuse container is active.
|
|
106
|
+
*/
|
|
107
|
+
export declare function stopReusedTestDb(): Promise<void>;
|
|
108
|
+
/**
|
|
109
|
+
* Naive SQL statement splitter: splits on `;` outside of single-quoted
|
|
110
|
+
* strings and dollar-quoted blocks (`$$...$$`). Handles the migration-file
|
|
111
|
+
* styles Zveltio ships; not a full SQL parser.
|
|
112
|
+
*
|
|
113
|
+
* Public so tests can pin its behavior. Not part of the documented surface.
|
|
114
|
+
*/
|
|
115
|
+
export declare function splitStatements(src: string): string[];
|
|
116
|
+
export declare const _internalForTests: {
|
|
117
|
+
splitStatements: typeof splitStatements;
|
|
118
|
+
};
|
|
119
|
+
//# sourceMappingURL=with-test-db.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"with-test-db.d.ts","sourceRoot":"","sources":["../../src/testing/with-test-db.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAGrC,MAAM,WAAW,iBAAiB;IAChC,+FAA+F;IAC/F,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,kDAAkD;IAClD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,gEAAgE;IAChE,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;IACtB;;;OAGG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B;;;OAGG;IACH,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAED,MAAM,WAAW,MAAM;IACrB,8DAA8D;IAE9D,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC;IAChB,+CAA+C;IAC/C,gBAAgB,EAAE,MAAM,CAAC;IACzB,iEAAiE;IACjE,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1B;AA4BD;;;;;;;GAOG;AACH,wBAAsB,WAAW,CAAC,IAAI,GAAE,iBAAsB,GAAG,OAAO,CAAC,MAAM,CAAC,CA8E/E;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAsB,UAAU,CAAC,CAAC,EAEhC,QAAQ,EAAE,CAAC,CAAC,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC,GAAG,iBAAiB,EAE/D,OAAO,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,GACxC,OAAO,CAAC,CAAC,CAAC,CAYZ;AAED;;;;;;;;;;;GAWG;AAGH,wBAAsB,qBAAqB,CAAC,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,EAAE,UAAU,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAgBhG;AAED;;;GAGG;AAGH,wBAAsB,mBAAmB,CAAC,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAIzF;AAED;;;;GAIG;AACH,wBAAsB,gBAAgB,IAAI,OAAO,CAAC,IAAI,CAAC,CAStD;AAkBD;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,CAuErD;AAGD,eAAO,MAAM,iBAAiB;;CAAsB,CAAC"}
|