@zveltio/sdk 3.0.0-beta.1 → 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/client.d.ts +103 -7
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +117 -27
- 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 +20 -0
- package/dist/ddl/index.d.ts.map +1 -1
- 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 +1086 -38
- package/dist/extension/index.d.ts.map +1 -1
- package/dist/extension/index.js +16 -0
- 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 +15 -0
- package/dist/extension/permission-gate.d.ts.map +1 -1
- package/dist/extension/permission-gate.js +33 -1
- 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 +3 -1
- 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 +92 -13
- package/dist/offline/index.d.ts +25 -55
- package/dist/offline/index.d.ts.map +1 -1
- package/dist/offline/index.js +173 -157
- package/dist/publish/index.d.ts.map +1 -1
- package/dist/publish/index.js +6 -1
- package/dist/realtime.d.ts +25 -1
- package/dist/realtime.d.ts.map +1 -1
- package/dist/realtime.js +47 -3
- package/dist/rpc/index.d.ts.map +1 -1
- package/dist/rpc/index.js +2 -0
- package/dist/schema-codegen.d.ts +25 -1
- package/dist/schema-codegen.d.ts.map +1 -1
- package/dist/schema-codegen.js +141 -7
- package/dist/schema-watcher.d.ts +10 -2
- package/dist/schema-watcher.d.ts.map +1 -1
- package/dist/schema-watcher.js +25 -10
- package/dist/studio/index.d.ts +1 -0
- package/dist/studio/index.d.ts.map +1 -1
- package/dist/studio/index.js +3 -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 +173 -15
- package/dist/testing/index.d.ts.map +1 -1
- package/dist/testing/index.js +23 -1
- package/dist/testing/with-test-db.d.ts +1 -1
- package/dist/testing/with-test-db.d.ts.map +1 -1
- package/dist/testing/with-test-db.js +22 -15
- 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/validate/index.d.ts +53 -0
- package/dist/validate/index.d.ts.map +1 -1
- package/dist/validate/index.js +693 -7
- package/dist/validate/migration-parse.d.ts +10 -5
- package/dist/validate/migration-parse.d.ts.map +1 -1
- package/dist/validate/migration-parse.js +1 -1
- 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 +8 -8
- 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 -85
- package/dist/client/RealtimeClient.d.ts +0 -22
- package/dist/client/RealtimeClient.d.ts.map +0 -1
- package/dist/client/RealtimeClient.js +0 -98
- package/dist/client/ZveltioClient.d.ts +0 -20
- package/dist/client/ZveltioClient.d.ts.map +0 -1
- package/dist/client/ZveltioClient.js +0 -78
- package/dist/generate-types.d.ts +0 -37
- package/dist/generate-types.d.ts.map +0 -1
- package/dist/generate-types.js +0 -157
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { describe, it, expect, afterEach, jest, spyOn } from 'bun:test';
|
|
2
|
+
import { rm } from 'node:fs/promises';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
4
|
+
import { ZveltioRealtime } from '../realtime.js';
|
|
5
|
+
import { watchSchema } from '../schema-watcher.js';
|
|
6
|
+
const RealWebSocket = globalThis.WebSocket;
|
|
7
|
+
afterEach(() => {
|
|
8
|
+
globalThis.WebSocket = RealWebSocket;
|
|
9
|
+
});
|
|
10
|
+
/** Records the constructor arguments instead of dialling out. */
|
|
11
|
+
function captureSockets() {
|
|
12
|
+
const calls = [];
|
|
13
|
+
globalThis.WebSocket = class {
|
|
14
|
+
static OPEN = 1;
|
|
15
|
+
readyState = 0;
|
|
16
|
+
constructor(...args) {
|
|
17
|
+
calls.push(args);
|
|
18
|
+
}
|
|
19
|
+
close() { }
|
|
20
|
+
send() { }
|
|
21
|
+
};
|
|
22
|
+
return calls;
|
|
23
|
+
}
|
|
24
|
+
describe('ZveltioRealtime upgrade headers', () => {
|
|
25
|
+
// `/api/ws` accepts an API key in `X-API-Key`; a server-side client had no
|
|
26
|
+
// way to send one, so it could read a collection over REST and never
|
|
27
|
+
// subscribe to it.
|
|
28
|
+
it('passes headers to the socket when given', () => {
|
|
29
|
+
const calls = captureSockets();
|
|
30
|
+
const rt = new ZveltioRealtime('https://engine.test', { headers: { 'X-API-Key': 'zvk_x' } });
|
|
31
|
+
rt.connect();
|
|
32
|
+
expect(calls[0]).toEqual(['wss://engine.test/api/ws', { headers: { 'X-API-Key': 'zvk_x' } }]);
|
|
33
|
+
rt.disconnect();
|
|
34
|
+
});
|
|
35
|
+
it('uses the one-argument constructor without them', () => {
|
|
36
|
+
const calls = captureSockets();
|
|
37
|
+
const rt = new ZveltioRealtime('http://engine.test');
|
|
38
|
+
rt.connect();
|
|
39
|
+
expect(calls[0]).toEqual(['ws://engine.test/api/ws']);
|
|
40
|
+
rt.disconnect();
|
|
41
|
+
});
|
|
42
|
+
// A browser reads the second argument as subprotocols and throws a
|
|
43
|
+
// SyntaxError on an object. There the socket opens on the session cookie.
|
|
44
|
+
it('opens without headers where the constructor refuses them (a browser)', () => {
|
|
45
|
+
const calls = [];
|
|
46
|
+
globalThis.WebSocket = class {
|
|
47
|
+
static CONNECTING = 0;
|
|
48
|
+
static OPEN = 1;
|
|
49
|
+
readyState = 0;
|
|
50
|
+
constructor(...args) {
|
|
51
|
+
if (args.length > 1 && typeof args[1] === 'object' && !Array.isArray(args[1]))
|
|
52
|
+
throw new SyntaxError('An invalid or illegal string was specified');
|
|
53
|
+
calls.push(args);
|
|
54
|
+
}
|
|
55
|
+
close() { }
|
|
56
|
+
send() { }
|
|
57
|
+
};
|
|
58
|
+
const rt = new ZveltioRealtime('https://engine.test', { headers: { 'X-API-Key': 'zvk_x' } });
|
|
59
|
+
rt.connect();
|
|
60
|
+
expect(calls).toEqual([['wss://engine.test/api/ws']]);
|
|
61
|
+
rt.disconnect();
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
/** Fake sockets that record themselves, so a test can open or close one. */
|
|
65
|
+
function fakeSockets() {
|
|
66
|
+
const made = [];
|
|
67
|
+
globalThis.WebSocket = class {
|
|
68
|
+
static CONNECTING = 0;
|
|
69
|
+
static OPEN = 1;
|
|
70
|
+
readyState = 0;
|
|
71
|
+
sent = [];
|
|
72
|
+
args;
|
|
73
|
+
onopen;
|
|
74
|
+
onclose;
|
|
75
|
+
constructor(...args) {
|
|
76
|
+
this.args = args;
|
|
77
|
+
made.push(this);
|
|
78
|
+
}
|
|
79
|
+
close() { }
|
|
80
|
+
send(p) {
|
|
81
|
+
this.sent.push(p);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
return made;
|
|
85
|
+
}
|
|
86
|
+
/** Close a fake socket as the runtime does: CLOSED first, then the event. */
|
|
87
|
+
function closeWith(ws, code) {
|
|
88
|
+
ws.readyState = 3;
|
|
89
|
+
ws.onclose?.({ code });
|
|
90
|
+
}
|
|
91
|
+
/** Drive a fake socket to OPEN, as the engine accepting the upgrade would. */
|
|
92
|
+
function accept(ws) {
|
|
93
|
+
ws.readyState = 1;
|
|
94
|
+
ws.onopen?.();
|
|
95
|
+
}
|
|
96
|
+
describe('a socket the engine closed for revoked credentials', () => {
|
|
97
|
+
// The engine closes with 4001 when the session or key is revoked, and every
|
|
98
|
+
// reconnect is then refused with 401 — retrying only hammered the engine.
|
|
99
|
+
afterEach(() => jest.useRealTimers());
|
|
100
|
+
it('ZveltioRealtime stops reconnecting on 4001 and says so', () => {
|
|
101
|
+
jest.useFakeTimers();
|
|
102
|
+
const made = fakeSockets();
|
|
103
|
+
const rt = new ZveltioRealtime('http://engine.test');
|
|
104
|
+
let told = 0;
|
|
105
|
+
rt.onUnauthorized(() => told++);
|
|
106
|
+
rt.connect();
|
|
107
|
+
closeWith(made[0], 4001);
|
|
108
|
+
jest.advanceTimersByTime(60_000);
|
|
109
|
+
expect(made).toHaveLength(1);
|
|
110
|
+
expect(told).toBe(1);
|
|
111
|
+
rt.disconnect();
|
|
112
|
+
});
|
|
113
|
+
it('ZveltioRealtime still reconnects on any other close', () => {
|
|
114
|
+
jest.useFakeTimers();
|
|
115
|
+
const made = fakeSockets();
|
|
116
|
+
const rt = new ZveltioRealtime('http://engine.test');
|
|
117
|
+
let told = 0;
|
|
118
|
+
rt.onUnauthorized(() => told++);
|
|
119
|
+
rt.connect();
|
|
120
|
+
closeWith(made[0], 1006);
|
|
121
|
+
jest.advanceTimersByTime(1_000);
|
|
122
|
+
expect(made).toHaveLength(2);
|
|
123
|
+
expect(told).toBe(0);
|
|
124
|
+
rt.disconnect();
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
describe('watchSchema', () => {
|
|
128
|
+
// It dialled the socket with no credentials, so the engine refused every
|
|
129
|
+
// upgrade (401) and the watcher reconnected every few seconds, forever.
|
|
130
|
+
it('sends its API key with the upgrade', async () => {
|
|
131
|
+
let upgradeKey;
|
|
132
|
+
const server = Bun.serve({
|
|
133
|
+
port: 0,
|
|
134
|
+
fetch(req, srv) {
|
|
135
|
+
const url = new URL(req.url);
|
|
136
|
+
if (url.pathname === '/api/collections')
|
|
137
|
+
return Response.json({ collections: [] });
|
|
138
|
+
if (url.pathname === '/api/ws') {
|
|
139
|
+
upgradeKey = req.headers.get('x-api-key');
|
|
140
|
+
if (upgradeKey !== 'zvk_watch')
|
|
141
|
+
return new Response('Unauthorized', { status: 401 });
|
|
142
|
+
return srv.upgrade(req) ? undefined : new Response('no upgrade', { status: 426 });
|
|
143
|
+
}
|
|
144
|
+
return new Response('not found', { status: 404 });
|
|
145
|
+
},
|
|
146
|
+
websocket: { message() { } },
|
|
147
|
+
});
|
|
148
|
+
const out = `${tmpdir()}/zveltio-watch-${Date.now()}.d.ts`;
|
|
149
|
+
const log = spyOn(console, 'log').mockImplementation(() => { });
|
|
150
|
+
const stop = await watchSchema(`http://127.0.0.1:${server.port}`, out, { apiKey: 'zvk_watch' });
|
|
151
|
+
try {
|
|
152
|
+
for (let i = 0; i < 100 && upgradeKey === undefined; i++)
|
|
153
|
+
await Bun.sleep(10);
|
|
154
|
+
expect(upgradeKey).toBe('zvk_watch');
|
|
155
|
+
for (let i = 0; i < 100 && !log.mock.calls.some((c) => String(c[0]).includes('connected')); i++) {
|
|
156
|
+
await Bun.sleep(10);
|
|
157
|
+
}
|
|
158
|
+
expect(log.mock.calls.some((c) => String(c[0]).includes('Schema watcher connected'))).toBe(true);
|
|
159
|
+
}
|
|
160
|
+
finally {
|
|
161
|
+
stop();
|
|
162
|
+
log.mockRestore();
|
|
163
|
+
server.stop(true);
|
|
164
|
+
await rm(out, { force: true });
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
// The engine publishes schema changes on `$schema` as `schema:changed`, to a
|
|
168
|
+
// schema admin's session only — so the watcher must be able to send one.
|
|
169
|
+
it('regenerates on a schema:changed event, and reports a refused subscribe', async () => {
|
|
170
|
+
let upgradeCookie = null;
|
|
171
|
+
const subscribed = [];
|
|
172
|
+
const server = Bun.serve({
|
|
173
|
+
port: 0,
|
|
174
|
+
fetch(req, srv) {
|
|
175
|
+
const url = new URL(req.url);
|
|
176
|
+
if (url.pathname === '/api/collections')
|
|
177
|
+
return Response.json({ collections: [] });
|
|
178
|
+
if (url.pathname === '/api/ws') {
|
|
179
|
+
upgradeCookie = req.headers.get('cookie');
|
|
180
|
+
return srv.upgrade(req) ? undefined : new Response('no upgrade', { status: 426 });
|
|
181
|
+
}
|
|
182
|
+
return new Response('not found', { status: 404 });
|
|
183
|
+
},
|
|
184
|
+
websocket: {
|
|
185
|
+
message(ws, raw) {
|
|
186
|
+
const msg = JSON.parse(String(raw));
|
|
187
|
+
subscribed.push(msg);
|
|
188
|
+
if (msg.channel !== '$schema')
|
|
189
|
+
return;
|
|
190
|
+
ws.send(JSON.stringify({ type: 'subscribed', channel: '$schema' }));
|
|
191
|
+
ws.send(JSON.stringify({ type: 'schema:changed', collection: 'orders', action: 'alter' }));
|
|
192
|
+
ws.send(JSON.stringify({ type: 'error', message: 'No read permission for "$schema"' }));
|
|
193
|
+
},
|
|
194
|
+
},
|
|
195
|
+
});
|
|
196
|
+
const out = `${tmpdir()}/zveltio-watch-evt-${Date.now()}.d.ts`;
|
|
197
|
+
const log = spyOn(console, 'log').mockImplementation(() => { });
|
|
198
|
+
let updates = 0;
|
|
199
|
+
const errors = [];
|
|
200
|
+
const stop = await watchSchema(`http://127.0.0.1:${server.port}`, out, {
|
|
201
|
+
headers: { cookie: 'session=abc' },
|
|
202
|
+
onUpdate: () => updates++,
|
|
203
|
+
onError: (e) => errors.push(e.message),
|
|
204
|
+
});
|
|
205
|
+
try {
|
|
206
|
+
for (let i = 0; i < 200 && (updates < 2 || errors.length === 0); i++)
|
|
207
|
+
await Bun.sleep(10);
|
|
208
|
+
expect(upgradeCookie).toBe('session=abc');
|
|
209
|
+
expect(subscribed).toEqual([{ type: 'subscribe', channel: '$schema' }]);
|
|
210
|
+
expect(updates).toBe(2); // the initial generation, then the event
|
|
211
|
+
expect(errors).toEqual(['Schema watcher: No read permission for "$schema"']);
|
|
212
|
+
}
|
|
213
|
+
finally {
|
|
214
|
+
stop();
|
|
215
|
+
log.mockRestore();
|
|
216
|
+
server.stop(true);
|
|
217
|
+
await rm(out, { force: true });
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reserved-paths.test.d.ts","sourceRoot":"","sources":["../../src/tests/reserved-paths.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import { isReservedPath } from '../extension/index.js';
|
|
3
|
+
describe('isReservedPath', () => {
|
|
4
|
+
it('refuses a path whose first segment the engine or the App owns', () => {
|
|
5
|
+
for (const p of [
|
|
6
|
+
'admin',
|
|
7
|
+
'/admin',
|
|
8
|
+
'app/crm',
|
|
9
|
+
'/API/x',
|
|
10
|
+
'auth',
|
|
11
|
+
'.well-known/x',
|
|
12
|
+
'favicon.ico',
|
|
13
|
+
'robots.txt',
|
|
14
|
+
])
|
|
15
|
+
expect(isReservedPath(p)).toBe(true);
|
|
16
|
+
});
|
|
17
|
+
it('accepts a path that only starts with the same letters', () => {
|
|
18
|
+
for (const p of ['apps', 'about', 'administrator', 'blog/admin', '', '/'])
|
|
19
|
+
expect(isReservedPath(p)).toBe(false);
|
|
20
|
+
});
|
|
21
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sync-manager.test.d.ts","sourceRoot":"","sources":["../../src/tests/sync-manager.test.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,CAAC"}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import './setup';
|
|
2
|
+
import { describe, it, expect, afterEach } from 'bun:test';
|
|
3
|
+
import { SyncManager } from '../sync-manager.js';
|
|
4
|
+
import { LocalStore } from '../local-store.js';
|
|
5
|
+
/**
|
|
6
|
+
* A client whose `get` answers the server's copy of a record — the only call the
|
|
7
|
+
* realtime handler makes.
|
|
8
|
+
*/
|
|
9
|
+
function fakeClient() {
|
|
10
|
+
return {
|
|
11
|
+
collection: () => ({ get: async (id) => ({ id, title: 'from server' }) }),
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
/** Stands in for `ZveltioRealtime`; `push` delivers a frame as the socket would. */
|
|
15
|
+
function fakeRealtime() {
|
|
16
|
+
let handler;
|
|
17
|
+
return {
|
|
18
|
+
subscribe: (_collection, cb) => {
|
|
19
|
+
handler = cb;
|
|
20
|
+
return () => { };
|
|
21
|
+
},
|
|
22
|
+
disconnect: () => { },
|
|
23
|
+
push: async (msg) => handler?.(msg),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
let sync;
|
|
27
|
+
afterEach(async () => {
|
|
28
|
+
await sync?.stop();
|
|
29
|
+
sync = undefined;
|
|
30
|
+
});
|
|
31
|
+
describe('SyncManager — realtime frames as the engine sends them', () => {
|
|
32
|
+
// The frame shape is `broadcastEvent` in packages/engine/src/routes/ws.ts.
|
|
33
|
+
// The handler used to wait for `record.created` / `record_id`, names the
|
|
34
|
+
// socket never sends, so no server push ever reached the local store.
|
|
35
|
+
it('applies an insert and drops a delete without queueing one back', async () => {
|
|
36
|
+
sync = new SyncManager(fakeClient(), { syncInterval: 60_000 });
|
|
37
|
+
await sync.start();
|
|
38
|
+
const store = sync.store;
|
|
39
|
+
await store.clear();
|
|
40
|
+
const rt = fakeRealtime();
|
|
41
|
+
sync.realtime = rt;
|
|
42
|
+
let latest = [];
|
|
43
|
+
sync.collection('posts').subscribe((records) => {
|
|
44
|
+
latest = records;
|
|
45
|
+
});
|
|
46
|
+
await rt.push({ type: 'event', collection: 'posts', event: 'insert', data: { id: 'p1' } });
|
|
47
|
+
expect((await store.get('posts', 'p1'))?.data).toMatchObject({ title: 'from server' });
|
|
48
|
+
expect(latest.map((r) => r.id)).toEqual(['p1']);
|
|
49
|
+
await rt.push({ type: 'event', collection: 'posts', event: 'delete', data: { id: 'p1' } });
|
|
50
|
+
expect(await store.get('posts', 'p1')).toBeUndefined();
|
|
51
|
+
// A queued delete here would send the server's own deletion back to it.
|
|
52
|
+
expect(await store.getPendingOps()).toEqual([]);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
describe('SyncManager — stop() before start() settles', () => {
|
|
56
|
+
// A React effect cleanup (and StrictMode, on every mount in development) calls
|
|
57
|
+
// `stop()` while `start()` is still awaiting the store. `start()` then armed
|
|
58
|
+
// the sync interval anyway, and nothing was left to clear it.
|
|
59
|
+
it('leaves no timer armed', async () => {
|
|
60
|
+
sync = new SyncManager(fakeClient(), { syncInterval: 60_000 });
|
|
61
|
+
const starting = sync.start();
|
|
62
|
+
await sync.stop();
|
|
63
|
+
await starting;
|
|
64
|
+
expect(sync.syncTimer).toBeNull();
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
describe('SyncManager — clearLocalData', () => {
|
|
68
|
+
// The next user of the browser must neither see this user's records nor send
|
|
69
|
+
// this user's queued writes under their own session.
|
|
70
|
+
it('drops records and pending writes, open or not', async () => {
|
|
71
|
+
sync = new SyncManager(fakeClient(), { syncInterval: 60_000 });
|
|
72
|
+
await sync.start();
|
|
73
|
+
const store = sync.store;
|
|
74
|
+
await sync.collection('posts').create({ title: 'mine' });
|
|
75
|
+
expect(await store.getPendingOps()).not.toEqual([]);
|
|
76
|
+
await sync.clearLocalData();
|
|
77
|
+
expect(await store.list('posts')).toEqual([]);
|
|
78
|
+
expect(await store.getPendingOps()).toEqual([]);
|
|
79
|
+
});
|
|
80
|
+
it('clears a store this manager never opened', async () => {
|
|
81
|
+
const other = new LocalStore();
|
|
82
|
+
await other.open();
|
|
83
|
+
await other.put('posts', 'p9', { title: 'left behind' });
|
|
84
|
+
await other.close();
|
|
85
|
+
sync = new SyncManager(fakeClient(), { syncInterval: 60_000 });
|
|
86
|
+
await sync.clearLocalData();
|
|
87
|
+
await other.open();
|
|
88
|
+
expect(await other.list('posts')).toEqual([]);
|
|
89
|
+
expect(await other.getPendingOps()).toEqual([]);
|
|
90
|
+
await other.close();
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
describe('SyncManager — paged pull reads the list the engine sends', () => {
|
|
94
|
+
// A client the sync routes refuse (an API key, 403) pages `list()` instead.
|
|
95
|
+
// That path read `result.data`, which `GET /api/data/:collection` has never
|
|
96
|
+
// sent — it answers `{ records, pagination }` — so it applied nothing.
|
|
97
|
+
it('applies the records of each page', async () => {
|
|
98
|
+
const realFetch = globalThis.fetch;
|
|
99
|
+
globalThis.fetch = (async (input) => {
|
|
100
|
+
const url = String(input instanceof Request ? input.url : input);
|
|
101
|
+
if (url.endsWith('/api/sync/pull')) {
|
|
102
|
+
return Response.json({ status: 403, title: 'Forbidden' }, { status: 403 });
|
|
103
|
+
}
|
|
104
|
+
const page = Number(new URL(url).searchParams.get('page'));
|
|
105
|
+
const records = page === 1 ? [{ id: 'p1' }, { id: 'p2' }] : [{ id: 'p3' }];
|
|
106
|
+
return Response.json({ records, pagination: { total: 3, page, limit: 2, pages: 2 } });
|
|
107
|
+
});
|
|
108
|
+
try {
|
|
109
|
+
const { ZveltioClient } = await import('../client.js');
|
|
110
|
+
sync = new SyncManager(new ZveltioClient({ baseUrl: 'http://engine.test' }), {
|
|
111
|
+
syncInterval: 60_000,
|
|
112
|
+
});
|
|
113
|
+
await sync.start();
|
|
114
|
+
const store = sync.store;
|
|
115
|
+
await store.clear();
|
|
116
|
+
expect(await sync.pull(['posts'], 2)).toBe(3);
|
|
117
|
+
expect((await store.list('posts')).map((r) => r.id).sort()).toEqual(['p1', 'p2', 'p3']);
|
|
118
|
+
}
|
|
119
|
+
finally {
|
|
120
|
+
globalThis.fetch = realFetch;
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"today-in.test.d.ts","sourceRoot":"","sources":["../../src/tests/today-in.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import { todayIn } from '../extension/dates.js';
|
|
3
|
+
describe('todayIn', () => {
|
|
4
|
+
// 22:30 UTC on the 18th is already the 19th in Bucharest (UTC+3 in October).
|
|
5
|
+
const lateUtc = new Date('2026-10-18T22:30:00Z');
|
|
6
|
+
it('is the tenant’s date, not the UTC one, across midnight', () => {
|
|
7
|
+
expect(todayIn('Europe/Bucharest', lateUtc)).toBe('2026-10-19');
|
|
8
|
+
expect(todayIn('UTC', lateUtc)).toBe('2026-10-18');
|
|
9
|
+
expect(todayIn('America/New_York', new Date('2026-10-19T02:00:00Z'))).toBe('2026-10-18');
|
|
10
|
+
});
|
|
11
|
+
it('formats as ISO YYYY-MM-DD', () => {
|
|
12
|
+
expect(todayIn('UTC', new Date('2026-01-05T12:00:00Z'))).toBe('2026-01-05');
|
|
13
|
+
});
|
|
14
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate-manifest-actions.test.d.ts","sourceRoot":"","sources":["../../src/tests/validate-manifest-actions.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// `zveltio extension validate` refuses the actions and jobs the engine would refuse at load.
|
|
2
|
+
import { describe, expect, it } from 'bun:test';
|
|
3
|
+
import { validateManifest } from '../validate/index.js';
|
|
4
|
+
const base = {
|
|
5
|
+
name: 'a',
|
|
6
|
+
displayName: 'A',
|
|
7
|
+
category: 'custom',
|
|
8
|
+
description: 'd',
|
|
9
|
+
version: '1.0.0',
|
|
10
|
+
resources: ['ext:a'],
|
|
11
|
+
};
|
|
12
|
+
const input = { type: 'object' };
|
|
13
|
+
const action = {
|
|
14
|
+
name: 'convertLead',
|
|
15
|
+
permission: { resource: 'ext:a' },
|
|
16
|
+
input,
|
|
17
|
+
enqueues: ['notify'],
|
|
18
|
+
};
|
|
19
|
+
const job = { name: 'notify', input, retry: { limit: 3 } };
|
|
20
|
+
const errors = (extra) => validateManifest({ manifest: { ...base, ...extra } })
|
|
21
|
+
.filter((e) => e.code === 'MANIFEST_BAD_ACTION' || e.code === 'MANIFEST_BAD_JOB')
|
|
22
|
+
.map((e) => e.message);
|
|
23
|
+
describe('manifest actions and jobs', () => {
|
|
24
|
+
it('accepts a well-formed action that enqueues a declared job', () => {
|
|
25
|
+
expect(errors({ actions: [action], jobs: [job] })).toEqual([]);
|
|
26
|
+
});
|
|
27
|
+
it('refuses what the engine schema refuses', () => {
|
|
28
|
+
expect(errors({ actions: [action] })).toEqual([
|
|
29
|
+
'action "convertLead" enqueues "notify", which is not in jobs',
|
|
30
|
+
]);
|
|
31
|
+
expect(errors({ actions: [{ ...action, permission: { resource: 'ext:b' } }], jobs: [job] })).toHaveLength(1);
|
|
32
|
+
expect(errors({ actions: [action, action], jobs: [job] })).toHaveLength(1);
|
|
33
|
+
expect(errors({ actions: [{ ...action, name: 'Convert' }], jobs: [job] })).toHaveLength(1);
|
|
34
|
+
expect(errors({ actions: [{ ...action, input: 'x' }], jobs: [job] })).toHaveLength(1);
|
|
35
|
+
expect(errors({ actions: [{ ...action, writes: 'contacts' }], jobs: [job] })).toHaveLength(1);
|
|
36
|
+
expect(errors({ actions: {}, jobs: [job] })).toHaveLength(1);
|
|
37
|
+
expect(errors({ jobs: [{ ...job, retry: { limit: 11 } }] })).toHaveLength(1);
|
|
38
|
+
expect(errors({ jobs: [{ ...job, retry: { backoff: 'yes' } }] })).toHaveLength(1);
|
|
39
|
+
});
|
|
40
|
+
it('takes fromPublicRoutes naming publicRoutes entries only (ADR 0018)', () => {
|
|
41
|
+
const hooked = { ...job, fromPublicRoutes: ['/webhooks/stripe'] };
|
|
42
|
+
expect(errors({ jobs: [hooked], publicRoutes: ['/webhooks/stripe'] })).toEqual([]);
|
|
43
|
+
expect(errors({ jobs: [hooked] })).toEqual([
|
|
44
|
+
'job "notify" names "/webhooks/stripe" in fromPublicRoutes, which is not a publicRoutes entry',
|
|
45
|
+
]);
|
|
46
|
+
expect(errors({ jobs: [{ ...job, fromPublicRoutes: '/webhooks/stripe' }] })).toHaveLength(1);
|
|
47
|
+
expect(errors({
|
|
48
|
+
jobs: [{ ...job, fromPublicRoutes: ['/webhooks/*'] }],
|
|
49
|
+
publicRoutes: ['/webhooks/*'],
|
|
50
|
+
})).toEqual([
|
|
51
|
+
'job "notify" names "/webhooks/*" in fromPublicRoutes; name one route, without "*"',
|
|
52
|
+
]);
|
|
53
|
+
});
|
|
54
|
+
it('takes calls of a required dependency’s actions, as <extension>.<action> (ADR 0014)', () => {
|
|
55
|
+
const calling = { ...action, calls: ['finance/invoicing.recordPayment'] };
|
|
56
|
+
const deps = [{ name: 'finance/invoicing' }];
|
|
57
|
+
expect(errors({ actions: [calling], jobs: [job], dependencies: deps })).toEqual([]);
|
|
58
|
+
expect(errors({ actions: [calling], jobs: [job] })).toEqual([
|
|
59
|
+
'action "convertLead" calls "finance/invoicing.recordPayment", but "finance/invoicing" is not in dependencies or optionalDependencies',
|
|
60
|
+
]);
|
|
61
|
+
expect(errors({ actions: [calling], jobs: [job], optionalDependencies: deps })).toEqual([]);
|
|
62
|
+
const bad = (calls) => errors({ actions: [{ ...action, calls }], jobs: [job], dependencies: deps });
|
|
63
|
+
expect(bad(['finance/invoicing'])).toHaveLength(1);
|
|
64
|
+
expect(bad(['finance/invoicing.Pay'])).toHaveLength(1);
|
|
65
|
+
expect(bad('finance/invoicing.recordPayment')).toHaveLength(1);
|
|
66
|
+
});
|
|
67
|
+
it('accepts an action gated on a collection the actions or jobs read or write', () => {
|
|
68
|
+
const gated = { ...action, permission: { resource: 'leads' } };
|
|
69
|
+
expect(errors({ actions: [{ ...gated, writes: ['leads'] }], jobs: [job] })).toEqual([]);
|
|
70
|
+
expect(errors({ actions: [gated], jobs: [{ ...job, reads: ['leads'] }] })).toEqual([]);
|
|
71
|
+
expect(errors({ actions: [gated], jobs: [job] })).toHaveLength(1);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
describe('manifest collections', () => {
|
|
75
|
+
const bad = (collections) => validateManifest({ manifest: { ...base, collections } }).filter((e) => e.code === 'MANIFEST_BAD_COLLECTIONS');
|
|
76
|
+
it('takes collection names, once each', () => {
|
|
77
|
+
expect(bad(['tasks', 'task_lists'])).toEqual([]);
|
|
78
|
+
expect(bad('tasks')).toHaveLength(1);
|
|
79
|
+
expect(bad(['Tasks'])).toHaveLength(1);
|
|
80
|
+
expect(bad(['tasks', 'tasks'])).toHaveLength(1);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate-manifest-dependencies.test.d.ts","sourceRoot":"","sources":["../../src/tests/validate-manifest-dependencies.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// `zveltio extension validate` checks both dependency lists the broker reads.
|
|
2
|
+
import { describe, expect, it } from 'bun:test';
|
|
3
|
+
import { validateManifest } from '../validate/index.js';
|
|
4
|
+
const base = {
|
|
5
|
+
name: 'a',
|
|
6
|
+
displayName: 'A',
|
|
7
|
+
category: 'custom',
|
|
8
|
+
description: 'd',
|
|
9
|
+
version: '1.0.0',
|
|
10
|
+
};
|
|
11
|
+
const codes = (extra) => validateManifest({ manifest: { ...base, ...extra } })
|
|
12
|
+
.filter((e) => e.code === 'MANIFEST_BAD_DEPENDENCY')
|
|
13
|
+
.map((e) => e.message);
|
|
14
|
+
describe('manifest dependencies / optionalDependencies', () => {
|
|
15
|
+
it('accepts both lists', () => {
|
|
16
|
+
expect(codes({
|
|
17
|
+
dependencies: [{ name: 'crm', minVersion: '1.0.0' }],
|
|
18
|
+
optionalDependencies: [{ name: 'ai' }],
|
|
19
|
+
})).toEqual([]);
|
|
20
|
+
});
|
|
21
|
+
it('refuses a malformed entry, a bad minVersion and a name in both lists', () => {
|
|
22
|
+
expect(codes({ optionalDependencies: 'ai' })).toHaveLength(1);
|
|
23
|
+
expect(codes({ optionalDependencies: [{ minVersion: '1.0.0' }] })[0]).toContain('has no name');
|
|
24
|
+
expect(codes({ optionalDependencies: [{ name: 'ai', minVersion: 'x' }] })[0]).toContain('not semver');
|
|
25
|
+
expect(codes({ dependencies: [{ name: 'ai' }], optionalDependencies: [{ name: 'ai' }] })[0]).toContain('"ai" is listed in "dependencies" and "optionalDependencies"');
|
|
26
|
+
});
|
|
27
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate-manifest-forward-credentials.test.d.ts","sourceRoot":"","sources":["../../src/tests/validate-manifest-forward-credentials.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// `zveltio extension validate` refuses the forwardCredentials the engine would refuse.
|
|
2
|
+
import { describe, expect, it } from 'bun:test';
|
|
3
|
+
import { validateManifest } from '../validate/index.js';
|
|
4
|
+
const base = {
|
|
5
|
+
name: 'a',
|
|
6
|
+
displayName: 'A',
|
|
7
|
+
category: 'custom',
|
|
8
|
+
description: 'd',
|
|
9
|
+
version: '1.0.0',
|
|
10
|
+
publicRoutes: ['/scim/*'],
|
|
11
|
+
};
|
|
12
|
+
const errors = (forwardCredentials) => validateManifest({ manifest: { ...base, forwardCredentials } }).filter((e) => e.code === 'MANIFEST_BAD_FORWARD_CREDENTIALS');
|
|
13
|
+
describe('manifest forwardCredentials', () => {
|
|
14
|
+
it('accepts a credential header on a public route', () => {
|
|
15
|
+
expect(errors({ '/scim/*': ['authorization'] })).toEqual([]);
|
|
16
|
+
});
|
|
17
|
+
it('refuses an unknown header, a non-public route and a non-object', () => {
|
|
18
|
+
expect(errors({ '/scim/*': ['stripe-signature'] })).toHaveLength(1);
|
|
19
|
+
expect(errors({ '/admin/*': ['cookie'] })).toHaveLength(1);
|
|
20
|
+
expect(errors(['authorization'])).toHaveLength(1);
|
|
21
|
+
});
|
|
22
|
+
// The engine's schema takes arrays only, and refused at load what this passed.
|
|
23
|
+
it('refuses a bare header name where the engine wants a list', () => {
|
|
24
|
+
expect(errors({ '/scim/*': 'authorization' })).toHaveLength(1);
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
describe('manifest apiKeyRoutes', () => {
|
|
28
|
+
const routeErrors = (apiKeyRoutes) => validateManifest({ manifest: { ...base, apiKeyRoutes } }).filter((e) => e.code === 'MANIFEST_BAD_API_KEY_ROUTE');
|
|
29
|
+
it('accepts a list of method + pattern', () => {
|
|
30
|
+
expect(routeErrors(['GET /items'])).toEqual([]);
|
|
31
|
+
});
|
|
32
|
+
it('refuses a bare string where the engine wants a list', () => {
|
|
33
|
+
expect(routeErrors('GET /items')).toHaveLength(1);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate-sdui-data-endpoints.test.d.ts","sourceRoot":"","sources":["../../src/tests/validate-sdui-data-endpoints.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// A declarative page may name the data API of a collection its extension declares
|
|
2
|
+
// (ADR 0012), and of no other; every other core endpoint stays foreign.
|
|
3
|
+
import { describe, expect, it } from 'bun:test';
|
|
4
|
+
import { validateSduiSchema } from '../validate/index.js';
|
|
5
|
+
const foreign = (endpoint, collections) => validateSduiSchema({
|
|
6
|
+
schema: { title: 't', resources: [{ name: 'r', dataSource: endpoint }] },
|
|
7
|
+
extName: 'projects/management',
|
|
8
|
+
collections,
|
|
9
|
+
})
|
|
10
|
+
.filter((e) => e.code === 'SDUI_ENDPOINT_FOREIGN')
|
|
11
|
+
.map((e) => e.message.split('"')[1]);
|
|
12
|
+
describe('SDUI data API endpoints', () => {
|
|
13
|
+
it('allows its own collection and one of its records', () => {
|
|
14
|
+
for (const e of [
|
|
15
|
+
'/api/data/projects',
|
|
16
|
+
'/api/data/projects?limit=5',
|
|
17
|
+
'/api/data/projects/{id}',
|
|
18
|
+
'/api/data/projects/:id',
|
|
19
|
+
'/api/data/tasks/0b9c5c3e-1d2a-4f6b-9a1e-2c3d4e5f6a7b',
|
|
20
|
+
]) {
|
|
21
|
+
expect(foreign(e, ['projects', 'tasks'])).toEqual([]);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
it('refuses a collection it does not declare, and any data path without a declaration', () => {
|
|
25
|
+
expect(foreign('/api/data/invoices', ['projects'])).toEqual(['/api/data/invoices']);
|
|
26
|
+
expect(foreign('/api/data/projects')).toEqual(['/api/data/projects']);
|
|
27
|
+
});
|
|
28
|
+
it('refuses every other path, a deeper one and a dot segment included', () => {
|
|
29
|
+
for (const e of [
|
|
30
|
+
'/api/users',
|
|
31
|
+
'/api/data',
|
|
32
|
+
'/api/data/projects/{id}/history',
|
|
33
|
+
'/api/data/projects/../users',
|
|
34
|
+
'/api/data/projects/..',
|
|
35
|
+
'/api/collections/projects',
|
|
36
|
+
'/api/data/Projects',
|
|
37
|
+
]) {
|
|
38
|
+
expect(foreign(e, ['projects'])).toEqual([e]);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"with-test-db.test.d.ts","sourceRoot":"","sources":["../../src/tests/with-test-db.test.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,CAAC"}
|