@zveltio/sdk 3.0.0-beta.1 → 3.0.0-beta.101
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,164 @@
|
|
|
1
|
+
import { describe, it, expect, afterEach } from 'bun:test';
|
|
2
|
+
import { createZveltioClient } from '../client.js';
|
|
3
|
+
const realFetch = globalThis.fetch;
|
|
4
|
+
afterEach(() => {
|
|
5
|
+
globalThis.fetch = realFetch;
|
|
6
|
+
});
|
|
7
|
+
/** Records the URL each call is given and answers `{}` without a network hop. */
|
|
8
|
+
function captureUrls() {
|
|
9
|
+
const urls = [];
|
|
10
|
+
globalThis.fetch = (async (input) => {
|
|
11
|
+
urls.push(String(input));
|
|
12
|
+
return new Response('{}', { status: 200, headers: { 'content-type': 'application/json' } });
|
|
13
|
+
});
|
|
14
|
+
return urls;
|
|
15
|
+
}
|
|
16
|
+
describe('ZveltioClient URL building', () => {
|
|
17
|
+
it('encodes a record id into a single path segment', async () => {
|
|
18
|
+
const urls = captureUrls();
|
|
19
|
+
const client = createZveltioClient({ baseUrl: 'https://engine.test' });
|
|
20
|
+
const products = client.collection('products');
|
|
21
|
+
await products.get('a/b');
|
|
22
|
+
await products.update('a/b', { name: 'x' });
|
|
23
|
+
await products.delete('a/b');
|
|
24
|
+
expect(urls).toEqual([
|
|
25
|
+
'https://engine.test/api/data/products/a%2Fb',
|
|
26
|
+
'https://engine.test/api/data/products/a%2Fb',
|
|
27
|
+
'https://engine.test/api/data/products/a%2Fb',
|
|
28
|
+
]);
|
|
29
|
+
});
|
|
30
|
+
it('asks the engine who its key is at the route that answers it', async () => {
|
|
31
|
+
const urls = captureUrls();
|
|
32
|
+
const client = createZveltioClient({ baseUrl: 'https://engine.test', apiKey: 'zvk_x' });
|
|
33
|
+
await client.auth.apiKey();
|
|
34
|
+
expect(urls).toEqual(['https://engine.test/api/api-keys/self']);
|
|
35
|
+
});
|
|
36
|
+
it("reaches the member's own keys at /api/me/api-keys", async () => {
|
|
37
|
+
const urls = captureUrls();
|
|
38
|
+
const client = createZveltioClient({ baseUrl: 'https://engine.test' });
|
|
39
|
+
await client.auth.apiKeys.list();
|
|
40
|
+
await client.auth.apiKeys.revoke('a/b');
|
|
41
|
+
expect(urls).toEqual([
|
|
42
|
+
'https://engine.test/api/me/api-keys',
|
|
43
|
+
'https://engine.test/api/me/api-keys/a%2Fb',
|
|
44
|
+
]);
|
|
45
|
+
});
|
|
46
|
+
it('encodes the storage folder into the query string', async () => {
|
|
47
|
+
const urls = captureUrls();
|
|
48
|
+
const client = createZveltioClient({ baseUrl: 'https://engine.test' });
|
|
49
|
+
await client.storage.list('invoices&limit=9999');
|
|
50
|
+
expect(urls).toEqual(['https://engine.test/api/storage?folder_id=invoices%26limit%3D9999']);
|
|
51
|
+
});
|
|
52
|
+
it('sends the upload folder under the field name the engine reads', async () => {
|
|
53
|
+
let body;
|
|
54
|
+
globalThis.fetch = (async (_input, init) => {
|
|
55
|
+
body = init?.body;
|
|
56
|
+
return new Response('{}', { status: 200, headers: { 'content-type': 'application/json' } });
|
|
57
|
+
});
|
|
58
|
+
const client = createZveltioClient({ baseUrl: 'https://engine.test' });
|
|
59
|
+
await client.storage.upload(new File(['x'], 'a.txt'), 'folder-uuid');
|
|
60
|
+
expect(body?.get('folder_id')).toBe('folder-uuid');
|
|
61
|
+
expect(body?.has('folder')).toBe(false);
|
|
62
|
+
});
|
|
63
|
+
it('leaves an ordinary id and collection name untouched', async () => {
|
|
64
|
+
const urls = captureUrls();
|
|
65
|
+
const client = createZveltioClient({ baseUrl: 'https://engine.test/' });
|
|
66
|
+
await client.collection('order_items').get('018f2a1e-0000-7000-8000-000000000000');
|
|
67
|
+
expect(urls).toEqual([
|
|
68
|
+
'https://engine.test/api/data/order_items/018f2a1e-0000-7000-8000-000000000000',
|
|
69
|
+
]);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
describe('ZveltioClient session token (bearer mode)', () => {
|
|
73
|
+
function engine(issue) {
|
|
74
|
+
const sent = [];
|
|
75
|
+
globalThis.fetch = (async (_input, init) => {
|
|
76
|
+
sent.push({
|
|
77
|
+
auth: new Headers(init?.headers).get('Authorization'),
|
|
78
|
+
credentials: init?.credentials,
|
|
79
|
+
});
|
|
80
|
+
const headers = { 'content-type': 'application/json' };
|
|
81
|
+
if (issue)
|
|
82
|
+
headers['set-auth-token'] = issue;
|
|
83
|
+
return new Response('{}', { status: 200, headers });
|
|
84
|
+
});
|
|
85
|
+
return sent;
|
|
86
|
+
}
|
|
87
|
+
it('captures the token at sign-in, sends it as a bearer, and forgets it on logout', async () => {
|
|
88
|
+
const seen = [];
|
|
89
|
+
const sent = engine('tok.sig');
|
|
90
|
+
const client = createZveltioClient({
|
|
91
|
+
baseUrl: 'https://engine.test',
|
|
92
|
+
onSessionToken: (t) => seen.push(t),
|
|
93
|
+
});
|
|
94
|
+
await client.auth.login('a@b.c', 'pw');
|
|
95
|
+
const next = engine(null);
|
|
96
|
+
await client.collection('products').list();
|
|
97
|
+
await client.auth.logout();
|
|
98
|
+
await client.collection('products').list();
|
|
99
|
+
expect(sent[0]).toEqual({ auth: null, credentials: 'omit' });
|
|
100
|
+
expect(next.map((s) => s.auth)).toEqual(['Bearer tok.sig', 'Bearer tok.sig', null]);
|
|
101
|
+
expect(seen).toEqual(['tok.sig', null]);
|
|
102
|
+
expect(client.getSessionToken()).toBeNull();
|
|
103
|
+
});
|
|
104
|
+
it('a browser client keeps cookies and ignores an issued token', async () => {
|
|
105
|
+
const sent = engine('tok.sig');
|
|
106
|
+
const client = createZveltioClient({ baseUrl: 'https://engine.test' });
|
|
107
|
+
await client.auth.login('a@b.c', 'pw');
|
|
108
|
+
await client.collection('products').list();
|
|
109
|
+
expect(sent.map((s) => [s.auth, s.credentials])).toEqual([
|
|
110
|
+
[null, 'include'],
|
|
111
|
+
[null, 'include'],
|
|
112
|
+
]);
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
describe('ZveltioClient step-up', () => {
|
|
116
|
+
function engine() {
|
|
117
|
+
const sent = [];
|
|
118
|
+
globalThis.fetch = (async (_input, init) => {
|
|
119
|
+
const token = new Headers(init?.headers).get('X-Zveltio-Step-Up');
|
|
120
|
+
sent.push(token);
|
|
121
|
+
if (token === 'su-1')
|
|
122
|
+
return new Response('{"ok":true}', { status: 200 });
|
|
123
|
+
return new Response(JSON.stringify({
|
|
124
|
+
type: 'about:blank',
|
|
125
|
+
title: 'Forbidden',
|
|
126
|
+
status: 403,
|
|
127
|
+
code: 'step_up_required',
|
|
128
|
+
errors: { action: 'config' },
|
|
129
|
+
}), { status: 403, headers: { 'content-type': 'application/problem+json' } });
|
|
130
|
+
});
|
|
131
|
+
return sent;
|
|
132
|
+
}
|
|
133
|
+
it('asks for a step-up on 403 step_up_required and sends once more with the token', async () => {
|
|
134
|
+
const sent = engine();
|
|
135
|
+
const asked = [];
|
|
136
|
+
const client = createZveltioClient({
|
|
137
|
+
baseUrl: 'https://engine.test',
|
|
138
|
+
onStepUpRequired: async (action) => {
|
|
139
|
+
asked.push(action);
|
|
140
|
+
return 'su-1';
|
|
141
|
+
},
|
|
142
|
+
});
|
|
143
|
+
expect(await client.post('/api/collections', { name: 'x' })).toEqual({
|
|
144
|
+
ok: true,
|
|
145
|
+
});
|
|
146
|
+
expect(asked).toEqual(['config']);
|
|
147
|
+
expect(sent).toEqual([null, 'su-1']);
|
|
148
|
+
});
|
|
149
|
+
it('throws the 403 when the hook gives up, and without a hook', async () => {
|
|
150
|
+
const sent = engine();
|
|
151
|
+
const giveUp = createZveltioClient({
|
|
152
|
+
baseUrl: 'https://engine.test',
|
|
153
|
+
onStepUpRequired: async () => null,
|
|
154
|
+
});
|
|
155
|
+
await expect(giveUp.post('/api/collections', {})).rejects.toMatchObject({
|
|
156
|
+
code: 'step_up_required',
|
|
157
|
+
});
|
|
158
|
+
const plain = createZveltioClient({ baseUrl: 'https://engine.test' });
|
|
159
|
+
await expect(plain.post('/api/collections', {})).rejects.toMatchObject({
|
|
160
|
+
code: 'step_up_required',
|
|
161
|
+
});
|
|
162
|
+
expect(sent).toEqual([null, null]);
|
|
163
|
+
});
|
|
164
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crdt-orset.test.d.ts","sourceRoot":"","sources":["../../src/tests/crdt-orset.test.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import './setup';
|
|
2
|
+
import { describe, it, expect } from 'bun:test';
|
|
3
|
+
import { orSetAdd, orSetRemove, orSetMerge, orSetValues } from '../crdt.js';
|
|
4
|
+
describe('OR-Set — removal survives a merge with a stale replica', () => {
|
|
5
|
+
it('does not resurrect an element that one replica removed', () => {
|
|
6
|
+
const added = orSetAdd([], 'x');
|
|
7
|
+
const uid = added[0].uid;
|
|
8
|
+
const stale = [...added]; // a replica that never saw the removal
|
|
9
|
+
const removed = orSetRemove(added, uid);
|
|
10
|
+
const first = orSetMerge(removed, stale);
|
|
11
|
+
expect(orSetValues(first)).toEqual([]);
|
|
12
|
+
// The stale replica syncs again. Without its tombstone the merged set has
|
|
13
|
+
// no record of the removal, so the element reads as a fresh addition.
|
|
14
|
+
const second = orSetMerge(first, stale);
|
|
15
|
+
expect(orSetValues(second), 'the removed element came back').toEqual([]);
|
|
16
|
+
});
|
|
17
|
+
it('an element added after a removal is kept', () => {
|
|
18
|
+
const first = orSetAdd([], 'x');
|
|
19
|
+
const removed = orSetRemove(first, first[0].uid);
|
|
20
|
+
const readded = orSetAdd(removed, 'x');
|
|
21
|
+
expect(orSetValues(orSetMerge(readded, removed))).toEqual(['x']);
|
|
22
|
+
});
|
|
23
|
+
});
|
|
@@ -74,3 +74,47 @@ describe('LocalStore — conflict detection', () => {
|
|
|
74
74
|
expect(rec?._conflictData).toBeUndefined();
|
|
75
75
|
});
|
|
76
76
|
});
|
|
77
|
+
describe('LocalStore — what the SyncManager actually passes', () => {
|
|
78
|
+
let store;
|
|
79
|
+
beforeEach(async () => {
|
|
80
|
+
store = new LocalStore();
|
|
81
|
+
await store.open();
|
|
82
|
+
await store.clear();
|
|
83
|
+
});
|
|
84
|
+
afterEach(async () => {
|
|
85
|
+
await store.close();
|
|
86
|
+
});
|
|
87
|
+
it('flags a conflict when the confirmed server version is an epoch timestamp', async () => {
|
|
88
|
+
// `syncNow()` and `pull()` both pass `Date.now()` as the server version —
|
|
89
|
+
// the collections API returns no version column. From the first ACK on,
|
|
90
|
+
// `_serverVersion` is ~1.79e12 and `_localVersion` is still a small counter,
|
|
91
|
+
// so the version comparison alone can never be true again and every local
|
|
92
|
+
// edit was overwritten in silence.
|
|
93
|
+
await store.put('posts', 'rec-ts', { title: 'v1' });
|
|
94
|
+
const [op] = await store.getPendingOps();
|
|
95
|
+
await store.markSynced(op.id, 'posts', 'rec-ts', Date.now());
|
|
96
|
+
await store.put('posts', 'rec-ts', { title: 'local edit' });
|
|
97
|
+
await store.applyServerUpdate('posts', 'rec-ts', { title: 'server wins' }, Date.now());
|
|
98
|
+
const after = await store.get('posts', 'rec-ts');
|
|
99
|
+
expect(after?._syncStatus).toBe('conflict');
|
|
100
|
+
expect(after?.data).toEqual({ title: 'local edit' });
|
|
101
|
+
expect(after?._conflictData).toEqual({ title: 'server wins' });
|
|
102
|
+
});
|
|
103
|
+
it('queues the resolved row, so a resolution reaches the server', async () => {
|
|
104
|
+
await store.applyServerUpdate('posts', 'rec-res', { title: 'server' }, Date.now());
|
|
105
|
+
await store.resolveConflict('posts', 'rec-res', { title: 'resolved' });
|
|
106
|
+
const ops = await store.getPendingOps();
|
|
107
|
+
const mine = ops.filter((o) => o.recordId === 'rec-res');
|
|
108
|
+
expect(mine, 'the resolution was never queued').toHaveLength(1);
|
|
109
|
+
expect(mine[0].payload).toEqual({ title: 'resolved' });
|
|
110
|
+
expect(mine[0].operation).toBe('update');
|
|
111
|
+
});
|
|
112
|
+
it('keeps the CRDT document across a clean server update', async () => {
|
|
113
|
+
await store.put('posts', 'rec-crdt', { title: 'local' });
|
|
114
|
+
const [op] = await store.getPendingOps();
|
|
115
|
+
await store.markSynced(op.id, 'posts', 'rec-crdt', Date.now());
|
|
116
|
+
await store.applyServerUpdate('posts', 'rec-crdt', { title: 'server' }, Date.now());
|
|
117
|
+
const after = await store.get('posts', 'rec-crdt');
|
|
118
|
+
expect(after?._crdtDoc, 'the CRDT document was dropped, so no later merge can run').toBeDefined();
|
|
119
|
+
});
|
|
120
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migration-parse.test.d.ts","sourceRoot":"","sources":["../../src/tests/migration-parse.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import { parseMigrationSql } from '../validate/migration-parse.js';
|
|
3
|
+
describe('parseMigrationSql', () => {
|
|
4
|
+
it('splits on a line that is exactly -- DOWN', () => {
|
|
5
|
+
expect(parseMigrationSql('CREATE TABLE a (id int);\n-- DOWN\nDROP TABLE a;')).toEqual({
|
|
6
|
+
up: 'CREATE TABLE a (id int);',
|
|
7
|
+
down: 'DROP TABLE a;',
|
|
8
|
+
});
|
|
9
|
+
});
|
|
10
|
+
it('does not end UP at a comment that only starts with -- DOWN', () => {
|
|
11
|
+
const raw = 'CREATE TABLE a (id int);\n-- DOWN: manual rollback required\nCREATE TABLE b (id int);';
|
|
12
|
+
expect(parseMigrationSql(raw)).toEqual({ up: raw, down: null });
|
|
13
|
+
});
|
|
14
|
+
it('accepts case and trailing spaces, like the engine', () => {
|
|
15
|
+
expect(parseMigrationSql('SELECT 1;\n-- down \nSELECT 2;').down).toBe('SELECT 2;');
|
|
16
|
+
});
|
|
17
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"multipart.test.d.ts","sourceRoot":"","sources":["../../src/tests/multipart.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import { MULTIPART_REQUIRED, readMultipart } from '../extension/multipart.js';
|
|
3
|
+
/**
|
|
4
|
+
* Six upload handlers opened with a bare `await c.req.formData()` and put their
|
|
5
|
+
* "no file provided" 400 on the line after it. `formData()` throws when the body
|
|
6
|
+
* is not multipart, so that 400 was unreachable for exactly the requests it was
|
|
7
|
+
* written for, and they came back as a bare 500 instead.
|
|
8
|
+
*
|
|
9
|
+
* Both directions are asserted here. A guard that rejects everything would also
|
|
10
|
+
* make the suite green, and would break every upload in the product.
|
|
11
|
+
*/
|
|
12
|
+
const ctxWith = (body, headers = {}) => {
|
|
13
|
+
const req = new Request('http://x/upload', { method: 'POST', body, headers });
|
|
14
|
+
return { req: { formData: () => req.formData() } };
|
|
15
|
+
};
|
|
16
|
+
describe('readMultipart', () => {
|
|
17
|
+
it('parses a real multipart body, fields and files alike', async () => {
|
|
18
|
+
const fd = new FormData();
|
|
19
|
+
fd.append('file', new File(['salut'], 'a.txt', { type: 'text/plain' }));
|
|
20
|
+
fd.append('path', '/docs');
|
|
21
|
+
const form = await readMultipart(ctxWith(fd));
|
|
22
|
+
expect(form).not.toBeNull();
|
|
23
|
+
expect(form?.get('file')?.name).toBe('a.txt');
|
|
24
|
+
expect(form?.get('path')).toBe('/docs');
|
|
25
|
+
});
|
|
26
|
+
it('answers null for every body shape that used to throw', async () => {
|
|
27
|
+
expect(await readMultipart(ctxWith('{}', { 'content-type': 'application/json' }))).toBeNull();
|
|
28
|
+
expect(await readMultipart(ctxWith(null))).toBeNull();
|
|
29
|
+
expect(await readMultipart(ctxWith('salut', { 'content-type': 'text/plain' }))).toBeNull();
|
|
30
|
+
});
|
|
31
|
+
it('does not leak the parser own message to the caller', async () => {
|
|
32
|
+
// The body parser's error reads like "FormData parse error" — nothing a
|
|
33
|
+
// caller can act on, and echoing a parser's internals over HTTP is how
|
|
34
|
+
// request contents end up in error strings.
|
|
35
|
+
expect(MULTIPART_REQUIRED).toEqual({ error: 'Expected a multipart/form-data body.' });
|
|
36
|
+
});
|
|
37
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"offline-provider-crdt.test.d.ts","sourceRoot":"","sources":["../../src/tests/offline-provider-crdt.test.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,CAAC"}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import './setup';
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, it } from 'bun:test';
|
|
3
|
+
import { LocalStore } from '../local-store.js';
|
|
4
|
+
import { createOfflineProvider } from '../offline/index.js';
|
|
5
|
+
/**
|
|
6
|
+
* The CRDT provider is the DEFAULT (`config.provider ?? 'crdt'`) and it did
|
|
7
|
+
* nothing at all: `pull()` was an empty body, `subscribe()` returned an
|
|
8
|
+
* unsubscribe for nothing, and `push()` returned `0` — which reads as "there
|
|
9
|
+
* was nothing to send" rather than "I did not look". An application that called
|
|
10
|
+
* `createOfflineProvider({ engineUrl })` and pushed on a timer got a clean run
|
|
11
|
+
* and an empty server, forever, with nothing in any log.
|
|
12
|
+
*
|
|
13
|
+
* The test that existed asserted exactly that: `await expect(p.push())
|
|
14
|
+
* .resolves.toBe(0)`, under the title "builds a working stub". It passed for the
|
|
15
|
+
* whole life of the defect.
|
|
16
|
+
*
|
|
17
|
+
* So these assert the crossing — what ends up in the local store, and what the
|
|
18
|
+
* count means — rather than that the methods are callable.
|
|
19
|
+
*/
|
|
20
|
+
const ENGINE = 'http://engine.test';
|
|
21
|
+
/** Rows the fake engine holds, by collection. */
|
|
22
|
+
let served = {};
|
|
23
|
+
/** Ids the fake engine reports deleted (tombstones), by collection. */
|
|
24
|
+
let tombstones = {};
|
|
25
|
+
/** Collections the next pull answers with `resync`. */
|
|
26
|
+
let resync = {};
|
|
27
|
+
/** `/api/sync/pull` answers 403, as it does to an API key. */
|
|
28
|
+
let pullForbidden = false;
|
|
29
|
+
/** Bodies `/api/sync/pull` received. */
|
|
30
|
+
let pullBodies = [];
|
|
31
|
+
/** Every request the provider made, so "did it even ask" is answerable. */
|
|
32
|
+
let requests = [];
|
|
33
|
+
const realFetch = globalThis.fetch;
|
|
34
|
+
function json(body, status = 200) {
|
|
35
|
+
return new Response(JSON.stringify(body), {
|
|
36
|
+
status,
|
|
37
|
+
headers: { 'content-type': 'application/json' },
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
beforeEach(async () => {
|
|
41
|
+
served = {};
|
|
42
|
+
tombstones = {};
|
|
43
|
+
resync = {};
|
|
44
|
+
pullForbidden = false;
|
|
45
|
+
pullBodies = [];
|
|
46
|
+
requests = [];
|
|
47
|
+
globalThis.fetch = (async (input, init) => {
|
|
48
|
+
const url = typeof input === 'string' ? input : input.toString();
|
|
49
|
+
requests.push(`${init?.method ?? 'GET'} ${url.replace(ENGINE, '')}`);
|
|
50
|
+
if (url.endsWith('/api/sync/pull')) {
|
|
51
|
+
if (pullForbidden)
|
|
52
|
+
return json({ error: 'Session required' }, 403);
|
|
53
|
+
const body = JSON.parse(String(init?.body));
|
|
54
|
+
pullBodies.push(body);
|
|
55
|
+
const changes = [];
|
|
56
|
+
const cursors = {};
|
|
57
|
+
for (const table of body.collections) {
|
|
58
|
+
const name = table.replace(/^zvd_/, '');
|
|
59
|
+
// A cursor means rows already sent: only tombstones follow it, unless
|
|
60
|
+
// the collection restarts (`resync`) or has none.
|
|
61
|
+
if (body.cursors?.[table] && !resync[table]) {
|
|
62
|
+
for (const id of tombstones[name] ?? []) {
|
|
63
|
+
changes.push({ collection: table, id, data: null, operation: 'delete' });
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
for (const row of served[name] ?? []) {
|
|
68
|
+
changes.push({ collection: table, id: row.id, data: row, operation: 'upsert' });
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
cursors[table] = `pos-${pullBodies.length}`;
|
|
72
|
+
}
|
|
73
|
+
const answer = { changes, hasMore: false, cursors, resync };
|
|
74
|
+
resync = {};
|
|
75
|
+
return json(answer);
|
|
76
|
+
}
|
|
77
|
+
const match = url.match(/\/api\/data\/([^/?]+)/);
|
|
78
|
+
const name = match?.[1] ?? '';
|
|
79
|
+
const page = Number(new URL(url).searchParams.get('page') ?? '1');
|
|
80
|
+
// One page only: page 2 comes back empty, which is how `pull` stops.
|
|
81
|
+
// The shape `listRecords` sends. This fake answered `{ data, … }`, the shape
|
|
82
|
+
// the SDK's type wrongly declared, so the test agreed with the bug.
|
|
83
|
+
const records = page === 1 ? (served[name] ?? []) : [];
|
|
84
|
+
return json({ records, pagination: { total: records.length, page, limit: 200 } });
|
|
85
|
+
});
|
|
86
|
+
const store = new LocalStore();
|
|
87
|
+
await store.open();
|
|
88
|
+
await store.clear();
|
|
89
|
+
await store.close();
|
|
90
|
+
});
|
|
91
|
+
afterEach(() => {
|
|
92
|
+
globalThis.fetch = realFetch;
|
|
93
|
+
});
|
|
94
|
+
async function localRows(collection) {
|
|
95
|
+
const store = new LocalStore();
|
|
96
|
+
await store.open();
|
|
97
|
+
const rows = await store.list(collection);
|
|
98
|
+
await store.close();
|
|
99
|
+
return rows;
|
|
100
|
+
}
|
|
101
|
+
describe('createOfflineProvider — the crdt path actually syncs', () => {
|
|
102
|
+
it('is the default provider, which is why the no-op mattered', async () => {
|
|
103
|
+
const p = await createOfflineProvider({ engineUrl: ENGINE });
|
|
104
|
+
expect(p.kind).toBe('crdt');
|
|
105
|
+
await p.close();
|
|
106
|
+
});
|
|
107
|
+
it('pull writes the server rows into the local store', async () => {
|
|
108
|
+
served.orders = [
|
|
109
|
+
{ id: 'o-1', total: 10, status: 'new' },
|
|
110
|
+
{ id: 'o-2', total: 20, status: 'paid' },
|
|
111
|
+
];
|
|
112
|
+
const p = await createOfflineProvider({ engineUrl: ENGINE, tables: ['orders'] });
|
|
113
|
+
await p.pull();
|
|
114
|
+
await p.close();
|
|
115
|
+
const rows = await localRows('orders');
|
|
116
|
+
expect(rows.map((r) => r.id).sort()).toEqual(['o-1', 'o-2']);
|
|
117
|
+
expect(rows.find((r) => r.id === 'o-1')?.data.total).toBe(10);
|
|
118
|
+
// The old implementation made no request at all. This is the assertion that
|
|
119
|
+
// would have caught it.
|
|
120
|
+
expect(requests).toContain('POST /api/sync/pull');
|
|
121
|
+
});
|
|
122
|
+
it('pull refuses an empty table list instead of quietly doing nothing', async () => {
|
|
123
|
+
// Silence here is exactly what the no-op was. A provider asked to replicate
|
|
124
|
+
// nothing is a configuration mistake, and an empty local database looks the
|
|
125
|
+
// same as an empty server.
|
|
126
|
+
const p = await createOfflineProvider({ engineUrl: ENGINE });
|
|
127
|
+
await expect(p.pull()).rejects.toThrow(/tables.*is empty/i);
|
|
128
|
+
await p.close();
|
|
129
|
+
});
|
|
130
|
+
it('push reports how many queued operations it actually sent', async () => {
|
|
131
|
+
const p = await createOfflineProvider({ engineUrl: ENGINE, tables: ['orders'] });
|
|
132
|
+
// Queue two local writes the way an offline application does.
|
|
133
|
+
const store = new LocalStore();
|
|
134
|
+
await store.open();
|
|
135
|
+
await store.put('orders', 'o-9', { total: 99 });
|
|
136
|
+
await store.put('orders', 'o-10', { total: 100 });
|
|
137
|
+
const queued = await store.getPendingOps();
|
|
138
|
+
await store.close();
|
|
139
|
+
expect(queued.length).toBe(2);
|
|
140
|
+
const sent = await p.push();
|
|
141
|
+
await p.close();
|
|
142
|
+
expect(sent).toBe(2);
|
|
143
|
+
// Not just a number: the writes have to have left the machine.
|
|
144
|
+
expect(requests.filter((r) => r.startsWith('POST /api/data/orders')).length).toBe(2);
|
|
145
|
+
});
|
|
146
|
+
it('subscribe delivers the local rows, and the unsubscribe stops delivery', async () => {
|
|
147
|
+
served.orders = [{ id: 'o-1', total: 10 }];
|
|
148
|
+
const p = await createOfflineProvider({ engineUrl: ENGINE, tables: ['orders'] });
|
|
149
|
+
await p.pull();
|
|
150
|
+
const seen = [];
|
|
151
|
+
const off = p.subscribe('orders', (rows) => seen.push(rows));
|
|
152
|
+
await Bun.sleep(20);
|
|
153
|
+
expect(seen.length).toBeGreaterThan(0);
|
|
154
|
+
expect(seen.at(-1).map((r) => r.id)).toEqual(['o-1']);
|
|
155
|
+
off();
|
|
156
|
+
const countAtUnsubscribe = seen.length;
|
|
157
|
+
// A second pull notifies listeners; an unsubscribed callback must not hear it.
|
|
158
|
+
await p.pull();
|
|
159
|
+
await Bun.sleep(20);
|
|
160
|
+
expect(seen.length).toBe(countAtUnsubscribe);
|
|
161
|
+
await p.close();
|
|
162
|
+
});
|
|
163
|
+
it('a row deleted on the server leaves the local store on the next pull', async () => {
|
|
164
|
+
served.orders = [
|
|
165
|
+
{ id: 'o-1', total: 10 },
|
|
166
|
+
{ id: 'o-2', total: 20 },
|
|
167
|
+
];
|
|
168
|
+
const p = await createOfflineProvider({ engineUrl: ENGINE, tables: ['orders'] });
|
|
169
|
+
await p.pull();
|
|
170
|
+
served.orders = [];
|
|
171
|
+
tombstones.orders = ['o-2'];
|
|
172
|
+
await p.pull();
|
|
173
|
+
await p.close();
|
|
174
|
+
// The second pull resumed from the cursor the first one saved.
|
|
175
|
+
expect(pullBodies[1]?.cursors).toEqual({ zvd_orders: 'pos-1' });
|
|
176
|
+
expect((await localRows('orders')).map((r) => r.id)).toEqual(['o-1']);
|
|
177
|
+
});
|
|
178
|
+
it('resync drops synced rows the server no longer sends, keeping unpushed edits', async () => {
|
|
179
|
+
served.orders = [
|
|
180
|
+
{ id: 'o-1', total: 10 },
|
|
181
|
+
{ id: 'o-2', total: 20 },
|
|
182
|
+
];
|
|
183
|
+
const p = await createOfflineProvider({ engineUrl: ENGINE, tables: ['orders'] });
|
|
184
|
+
await p.pull();
|
|
185
|
+
const store = new LocalStore();
|
|
186
|
+
await store.open();
|
|
187
|
+
await store.put('orders', 'o-local', { total: 1 });
|
|
188
|
+
await store.close();
|
|
189
|
+
served.orders = [{ id: 'o-1', total: 11 }];
|
|
190
|
+
resync = { zvd_orders: true };
|
|
191
|
+
await p.pull();
|
|
192
|
+
await p.close();
|
|
193
|
+
const rows = await localRows('orders');
|
|
194
|
+
expect(rows.map((r) => r.id).sort()).toEqual(['o-1', 'o-local']);
|
|
195
|
+
expect(rows.find((r) => r.id === 'o-1')?.data.total).toBe(11);
|
|
196
|
+
});
|
|
197
|
+
it('a client the sync endpoint refuses (API key, 403) still pulls through the data API', async () => {
|
|
198
|
+
served.orders = [{ id: 'o-1', total: 10 }];
|
|
199
|
+
pullForbidden = true;
|
|
200
|
+
const p = await createOfflineProvider({ engineUrl: ENGINE, tables: ['orders'] });
|
|
201
|
+
await p.pull();
|
|
202
|
+
await p.close();
|
|
203
|
+
expect((await localRows('orders')).map((r) => r.id)).toEqual(['o-1']);
|
|
204
|
+
expect(requests.some((r) => r.startsWith('GET /api/data/orders'))).toBe(true);
|
|
205
|
+
});
|
|
206
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"realtime.test.d.ts","sourceRoot":"","sources":["../../src/tests/realtime.test.ts"],"names":[],"mappings":""}
|