-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathclient.test.ts
More file actions
1205 lines (1073 loc) · 47.4 KB
/
Copy pathclient.test.ts
File metadata and controls
1205 lines (1073 loc) · 47.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { describe, it, expect, vi } from 'vitest';
import { ObjectStackClient, QueryBuilder, FilterBuilder, createQuery, createFilter } from './index';
/** Helper: create a client with mocked fetch that returns the given response body */
function createMockClient(body: any, status = 200) {
const fetchMock = vi.fn().mockResolvedValue({
ok: status >= 200 && status < 300,
status,
statusText: status === 200 ? 'OK' : 'Error',
json: async () => body,
headers: new Headers()
});
const client = new ObjectStackClient({
baseUrl: 'http://localhost:3000',
fetch: fetchMock
});
return { client, fetchMock };
}
describe('ObjectStackClient', () => {
it('should initialize with correct configuration', () => {
const client = new ObjectStackClient({ baseUrl: 'http://localhost:3000' });
expect(client).toBeDefined();
});
it('should normalize base URL', () => {
const client: any = new ObjectStackClient({ baseUrl: 'http://localhost:3000/' });
expect(client.baseUrl).toBe('http://localhost:3000');
});
it('should make discovery request on connect', async () => {
const fetchMock = vi.fn().mockResolvedValue({
ok: true,
json: async () => ({
version: 'v1',
apiName: 'ObjectStack',
capabilities: ['metadata', 'data', 'ui'],
endpoints: {}
})
});
const client = new ObjectStackClient({
baseUrl: 'http://localhost:3000',
fetch: fetchMock
});
await client.connect();
// connect() tries .well-known first, which succeeds with our mock
expect(fetchMock).toHaveBeenCalled();
});
it('should get metadata types', async () => {
const fetchMock = vi.fn().mockResolvedValue({
ok: true,
json: async () => ({
types: ['object', 'plugin', 'view']
})
});
const client = new ObjectStackClient({
baseUrl: 'http://localhost:3000',
fetch: fetchMock
});
const result = await client.meta.getTypes();
expect(fetchMock).toHaveBeenCalledWith('http://localhost:3000/api/v1/meta', expect.any(Object));
expect(result.types).toEqual(['object', 'plugin', 'view']);
});
it('should get metadata items by type', async () => {
const fetchMock = vi.fn().mockResolvedValue({
ok: true,
json: async () => ({
type: 'object',
items: [{ name: 'customer' }, { name: 'order' }]
})
});
const client = new ObjectStackClient({
baseUrl: 'http://localhost:3000',
fetch: fetchMock
});
const result = await client.meta.getItems('object');
expect(fetchMock).toHaveBeenCalledWith('http://localhost:3000/api/v1/meta/object', expect.any(Object));
expect(result.type).toBe('object');
expect(result.items).toHaveLength(2);
});
it('should get metadata item by type and name', async () => {
const fetchMock = vi.fn().mockResolvedValue({
ok: true,
json: async () => ({
name: 'customer',
fields: []
})
});
const client = new ObjectStackClient({
baseUrl: 'http://localhost:3000',
fetch: fetchMock
});
const result = await client.meta.getItem('object', 'customer');
expect(fetchMock).toHaveBeenCalledWith('http://localhost:3000/api/v1/meta/object/customer', expect.any(Object));
expect(result.name).toBe('customer');
});
});
describe('Permissions namespace', () => {
it('should check permission with all params', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { allowed: true, reason: 'owner' }
});
const result = await client.permissions.check({
object: 'customer',
action: 'read',
recordId: '123',
field: 'email'
});
expect(result).toEqual({ allowed: true, reason: 'owner' });
const url = fetchMock.mock.calls[0][0] as string;
expect(url).toContain('/api/v1/permissions/check');
expect(url).toContain('object=customer');
expect(url).toContain('action=read');
expect(url).toContain('recordId=123');
expect(url).toContain('field=email');
});
it('should check permission without optional params', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { allowed: false }
});
const result = await client.permissions.check({
object: 'order',
action: 'delete'
});
expect(result).toEqual({ allowed: false });
const url = fetchMock.mock.calls[0][0] as string;
expect(url).not.toContain('recordId');
expect(url).not.toContain('field=');
});
it('should get object permissions', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { object: 'customer', permissions: { read: true, create: true } }
});
const result = await client.permissions.getObjectPermissions('customer');
expect(result.object).toBe('customer');
const url = fetchMock.mock.calls[0][0] as string;
expect(url).toContain('/api/v1/permissions/objects/customer');
});
it('should get effective permissions', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { roles: ['admin'], permissions: [] }
});
const result = await client.permissions.getEffectivePermissions();
expect(result.roles).toEqual(['admin']);
const url = fetchMock.mock.calls[0][0] as string;
expect(url).toContain('/api/v1/permissions/effective');
});
});
describe('Realtime namespace', () => {
it('should connect to realtime', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { connectionId: 'conn-1', transport: 'websocket' }
});
const result = await client.realtime.connect({ transport: 'websocket' as any });
expect(result.connectionId).toBe('conn-1');
const [url, opts] = fetchMock.mock.calls[0];
expect(url).toContain('/api/v1/realtime/connect');
expect(opts.method).toBe('POST');
});
it('should disconnect from realtime', async () => {
const { client, fetchMock } = createMockClient({ success: true });
await client.realtime.disconnect();
const [url, opts] = fetchMock.mock.calls[0];
expect(url).toContain('/api/v1/realtime/disconnect');
expect(opts.method).toBe('POST');
});
it('should subscribe to a channel', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { subscriptionId: 'sub-1' }
});
const result = await client.realtime.subscribe({
channel: 'customer.changes',
events: ['create', 'update']
});
expect(result.subscriptionId).toBe('sub-1');
const body = JSON.parse(fetchMock.mock.calls[0][1].body);
expect(body.channel).toBe('customer.changes');
expect(body.events).toEqual(['create', 'update']);
});
it('should unsubscribe from a channel', async () => {
const { client, fetchMock } = createMockClient({ success: true });
await client.realtime.unsubscribe('sub-1');
const body = JSON.parse(fetchMock.mock.calls[0][1].body);
expect(body.subscriptionId).toBe('sub-1');
});
it('should set presence', async () => {
const { client, fetchMock } = createMockClient({ success: true });
await client.realtime.setPresence('room-1', { status: 'online' } as any);
const [url, opts] = fetchMock.mock.calls[0];
expect(url).toContain('/api/v1/realtime/presence');
expect(opts.method).toBe('PUT');
const body = JSON.parse(opts.body);
expect(body.channel).toBe('room-1');
expect(body.state.status).toBe('online');
});
it('should get presence for a channel', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { channel: 'room-1', members: [] }
});
const result = await client.realtime.getPresence('room-1');
expect(result.channel).toBe('room-1');
const url = fetchMock.mock.calls[0][0] as string;
expect(url).toContain('/api/v1/realtime/presence/room-1');
});
});
describe('Workflow namespace', () => {
it('should get workflow config', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { object: 'order', states: ['draft', 'submitted'] }
});
const result = await client.workflow.getConfig('order');
expect(result.object).toBe('order');
const url = fetchMock.mock.calls[0][0] as string;
expect(url).toContain('/api/v1/workflow/order/config');
});
it('should get workflow state', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { state: 'draft', transitions: ['submit'] }
});
const result = await client.workflow.getState('order', 'rec-1');
expect(result.state).toBe('draft');
const url = fetchMock.mock.calls[0][0] as string;
expect(url).toContain('/api/v1/workflow/order/rec-1/state');
});
it('should execute workflow transition', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { success: true, newState: 'submitted' }
});
const result = await client.workflow.transition({
object: 'order',
recordId: 'rec-1',
transition: 'submit',
comment: 'Ready for review'
});
expect(result.newState).toBe('submitted');
const body = JSON.parse(fetchMock.mock.calls[0][1].body);
expect(body.transition).toBe('submit');
expect(body.comment).toBe('Ready for review');
});
// ADR-0019: approve/reject left the workflow namespace — they now live on
// `client.approvals` (approval is a flow node, not a workflow step).
});
describe('Approvals namespace (ADR-0019)', () => {
it('should list approval requests with filters', async () => {
const { client, fetchMock } = createMockClient({
data: [{ id: 'req-1', status: 'pending', object_name: 'order', record_id: 'rec-1', process_name: 'flow:approve' }]
});
const result = await client.approvals.listRequests({ status: 'pending', approverId: 'user-1' });
expect(result).toHaveLength(1);
expect(result[0].id).toBe('req-1');
const url = fetchMock.mock.calls[0][0] as string;
expect(url).toContain('/api/v1/approvals/requests');
expect(url).toContain('status=pending');
expect(url).toContain('approverId=user-1');
});
it('should join array status filters', async () => {
const { client, fetchMock } = createMockClient({ data: [] });
await client.approvals.listRequests({ status: ['approved', 'rejected'] });
const url = decodeURIComponent(fetchMock.mock.calls[0][0] as string);
expect(url).toContain('status=approved,rejected');
});
it('should get a single approval request', async () => {
const { client, fetchMock } = createMockClient({
id: 'req-1', status: 'pending', object_name: 'order', record_id: 'rec-1', process_name: 'flow:approve'
});
const result = await client.approvals.getRequest('req-1');
expect(result.id).toBe('req-1');
const url = fetchMock.mock.calls[0][0] as string;
expect(url).toContain('/api/v1/approvals/requests/req-1');
});
it('should record an approve decision', async () => {
const { client, fetchMock } = createMockClient({
request: { id: 'req-1', status: 'approved' }, finalized: true, decision: 'approve', resumed: true
});
const result = await client.approvals.approve('req-1', { actorId: 'user-1', comment: 'Looks good' });
expect(result.finalized).toBe(true);
expect(result.decision).toBe('approve');
const [url, opts] = fetchMock.mock.calls[0];
expect(url).toContain('/api/v1/approvals/requests/req-1/approve');
expect(opts.method).toBe('POST');
const body = JSON.parse(opts.body);
expect(body.actorId).toBe('user-1');
expect(body.comment).toBe('Looks good');
});
it('should record a reject decision', async () => {
const { client, fetchMock } = createMockClient({
request: { id: 'req-1', status: 'rejected' }, finalized: true, decision: 'reject'
});
const result = await client.approvals.reject('req-1', { comment: 'Missing fields' });
expect(result.decision).toBe('reject');
const [url, opts] = fetchMock.mock.calls[0];
expect(url).toContain('/api/v1/approvals/requests/req-1/reject');
expect(opts.method).toBe('POST');
});
it('should list the action audit trail', async () => {
const { client, fetchMock } = createMockClient({
data: [{ id: 'act-1', request_id: 'req-1', action: 'approve', actor_id: 'user-1' }]
});
const result = await client.approvals.listActions('req-1');
expect(result).toHaveLength(1);
expect(result[0].action).toBe('approve');
const url = fetchMock.mock.calls[0][0] as string;
expect(url).toContain('/api/v1/approvals/requests/req-1/actions');
});
});
describe('Views namespace', () => {
it('should list views for an object', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { views: [{ id: 'v1', name: 'Default' }] }
});
const result = await client.views.list('customer', 'list');
expect(result.views).toHaveLength(1);
const url = fetchMock.mock.calls[0][0] as string;
expect(url).toContain('/api/v1/ui/views/customer');
expect(url).toContain('type=list');
});
it('should list views without type filter', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { views: [] }
});
await client.views.list('order');
const url = fetchMock.mock.calls[0][0] as string;
expect(url).toContain('/api/v1/ui/views/order');
expect(url).not.toContain('type=');
});
it('should get a specific view', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { id: 'v1', name: 'Default', type: 'list' }
});
const result = await client.views.get('customer', 'v1');
expect(result.id).toBe('v1');
const url = fetchMock.mock.calls[0][0] as string;
expect(url).toContain('/api/v1/ui/views/customer/v1');
});
it('should create a view', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { id: 'v2', name: 'Custom View' }
});
const result = await client.views.create('customer', { name: 'Custom View' } as any);
expect(result.id).toBe('v2');
const [url, opts] = fetchMock.mock.calls[0];
expect(url).toContain('/api/v1/ui/views/customer');
expect(opts.method).toBe('POST');
});
it('should update a view', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { id: 'v1', name: 'Updated View' }
});
const result = await client.views.update('customer', 'v1', { name: 'Updated View' } as any);
expect(result.name).toBe('Updated View');
const [url, opts] = fetchMock.mock.calls[0];
expect(url).toContain('/api/v1/ui/views/customer/v1');
expect(opts.method).toBe('PUT');
});
it('should delete a view', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { deleted: true }
});
const result = await client.views.delete('customer', 'v1');
expect(result.deleted).toBe(true);
const [url, opts] = fetchMock.mock.calls[0];
expect(url).toContain('/api/v1/ui/views/customer/v1');
expect(opts.method).toBe('DELETE');
});
});
describe('Auth enhancements', () => {
it('should register a new user', async () => {
const { client, fetchMock } = createMockClient({
data: { token: 'new-token', user: { email: 'test@example.com' } }
});
const result = await client.auth.register({
email: 'test@example.com',
password: 'secret123',
name: 'Test User'
});
expect(result.data.token).toBe('new-token');
const [url, opts] = fetchMock.mock.calls[0];
expect(url).toContain('/api/v1/auth/sign-up/email'); // Updated to better-auth endpoint
expect(opts.method).toBe('POST');
// Token should be auto-set
expect((client as any).token).toBe('new-token');
});
it('should refresh token', async () => {
const { client, fetchMock } = createMockClient({
data: { token: 'refreshed-token' }
});
const result = await client.auth.refreshToken('old-refresh-token');
expect(result.data.token).toBe('refreshed-token');
const [url, opts] = fetchMock.mock.calls[0];
expect(url).toContain('/api/v1/auth/get-session'); // Updated: better-auth uses get-session for refresh
expect(opts.method).toBe('GET'); // Updated: GET instead of POST
// Token should be auto-set
expect((client as any).token).toBe('refreshed-token');
});
it('signInWithProvider defaults callbackURL to the current page (base-path-correct)', async () => {
const assign = vi.fn();
vi.stubGlobal('window', {
location: { href: 'https://app.example.com/_console/login', assign },
});
try {
const { client, fetchMock } = createMockClient({ url: 'https://accounts.google.com/o/oauth2/auth' });
await client.auth.signInWithProvider('google');
const [, opts] = fetchMock.mock.calls[0];
// The SDK can't know the app's mount path, so it returns the user to
// where they started rather than a hard-coded root '/login'.
expect(JSON.parse(opts.body).callbackURL).toBe('https://app.example.com/_console/login');
expect(assign).toHaveBeenCalledWith('https://accounts.google.com/o/oauth2/auth');
} finally {
vi.unstubAllGlobals();
}
});
it('signInWithProvider honours an explicit callbackURL', async () => {
const assign = vi.fn();
vi.stubGlobal('window', {
location: { href: 'https://app.example.com/_console/login', assign },
});
try {
const { client, fetchMock } = createMockClient({ url: 'https://accounts.google.com/o/oauth2/auth' });
await client.auth.signInWithProvider('google', { callbackURL: 'https://app.example.com/_console/home' });
const [, opts] = fetchMock.mock.calls[0];
expect(JSON.parse(opts.body).callbackURL).toBe('https://app.example.com/_console/home');
} finally {
vi.unstubAllGlobals();
}
});
});
describe('Notifications namespace', () => {
it('should register a device', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { deviceId: 'dev-1', registered: true }
});
const result = await client.notifications.registerDevice({
token: 'push-token',
platform: 'web',
deviceId: 'dev-1'
});
expect(result.deviceId).toBe('dev-1');
const [url, opts] = fetchMock.mock.calls[0];
expect(url).toContain('/api/v1/notifications/devices');
expect(opts.method).toBe('POST');
});
it('should unregister a device', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { success: true }
});
await client.notifications.unregisterDevice('dev-1');
const [url, opts] = fetchMock.mock.calls[0];
expect(url).toContain('/api/v1/notifications/devices/dev-1');
expect(opts.method).toBe('DELETE');
});
it('should list notifications with filters', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { notifications: [], unreadCount: 0 }
});
await client.notifications.list({ read: false, limit: 10 });
const url = fetchMock.mock.calls[0][0] as string;
expect(url).toContain('/api/v1/notifications');
expect(url).toContain('read=false');
expect(url).toContain('limit=10');
});
it('should mark notifications as read', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { success: true, readCount: 2 }
});
const result = await client.notifications.markRead(['n1', 'n2']);
expect(result.readCount).toBe(2);
const body = JSON.parse(fetchMock.mock.calls[0][1].body);
expect(body.ids).toEqual(['n1', 'n2']);
});
it('should mark all notifications as read', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { success: true, readCount: 5 }
});
const result = await client.notifications.markAllRead();
expect(result.readCount).toBe(5);
const [url, opts] = fetchMock.mock.calls[0];
expect(url).toContain('/api/v1/notifications/read/all');
expect(opts.method).toBe('POST');
});
});
describe('AI namespace', () => {
it('should execute natural language query', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { query: { object: 'customer', where: {} }, confidence: 0.95 }
});
const result = await client.ai.nlq({ query: 'find all active customers' });
expect(result.confidence).toBe(0.95);
const [url, opts] = fetchMock.mock.calls[0];
expect(url).toContain('/api/v1/ai/nlq');
expect(opts.method).toBe('POST');
});
it('should not expose chat method (use Vercel AI SDK useChat directly)', () => {
const { client } = createMockClient({ success: true, data: {} });
// ai.chat was removed — consumers should use @ai-sdk/react useChat() directly
expect(client.ai).not.toHaveProperty('chat');
});
it('should get AI suggestions', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { suggestions: ['Alice Corp', 'Alpha Inc'] }
});
const result = await client.ai.suggest({
object: 'customer',
field: 'name',
partial: 'Al'
});
expect(result.suggestions).toHaveLength(2);
});
it('should get AI insights', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { type: 'summary', insights: [] }
});
const result = await client.ai.insights({
object: 'order',
type: 'summary'
});
expect(result.type).toBe('summary');
const [url, opts] = fetchMock.mock.calls[0];
expect(url).toContain('/api/v1/ai/insights');
expect(opts.method).toBe('POST');
});
});
describe('i18n namespace', () => {
it('should get available locales', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { locales: ['en', 'zh-CN', 'ja'], default: 'en' }
});
const result = await client.i18n.getLocales();
expect(result.locales).toContain('en');
const url = fetchMock.mock.calls[0][0] as string;
expect(url).toContain('/api/v1/i18n/locales');
});
it('should get translations', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { locale: 'zh-CN', translations: { hello: '你好' } }
});
const result = await client.i18n.getTranslations('zh-CN', { namespace: 'common' });
expect(result.locale).toBe('zh-CN');
const url = fetchMock.mock.calls[0][0] as string;
expect(url).toContain('/api/v1/i18n/translations');
expect(url).toContain('locale=zh-CN');
expect(url).toContain('namespace=common');
});
it('should get field labels', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { object: 'customer', labels: { name: '名前' } }
});
const result = await client.i18n.getFieldLabels('customer', 'ja');
expect(result.object).toBe('customer');
const url = fetchMock.mock.calls[0][0] as string;
expect(url).toContain('/api/v1/i18n/labels/customer');
expect(url).toContain('locale=ja');
});
});
describe('QueryBuilder enhancements', () => {
it('should add expand for nested relation loading', () => {
const q = createQuery('order')
.select('id', 'total')
.expand('customer', { fields: ['name', 'email'] } as any)
.expand('items')
.build();
expect(q.expand).toBeDefined();
expect((q.expand as any).customer).toEqual({ fields: ['name', 'email'] });
expect((q.expand as any).items).toEqual({});
});
it('should add full-text search', () => {
const q = createQuery('customer')
.search('alice', { fields: ['name', 'email'], fuzzy: true })
.build();
expect((q as any).search).toEqual({
query: 'alice',
fields: ['name', 'email'],
fuzzy: true
});
});
it('should set cursor for keyset pagination', () => {
const q = createQuery('customer')
.cursor({ id: 'last-seen-id', created_at: '2024-01-01' })
.build();
expect((q as any).cursor).toEqual({
id: 'last-seen-id',
created_at: '2024-01-01'
});
});
it('should enable distinct', () => {
const q = createQuery('customer')
.select('status')
.distinct()
.build();
expect((q as any).distinct).toBe(true);
});
});
describe('FilterBuilder enhancements', () => {
it('should add between filter', () => {
const f = createFilter<{ age: number }>()
.between('age', 18, 65)
.build();
// between generates: ['and', [field, '>=', min], [field, '<=', max]]
expect(f[0]).toBe('and');
expect(f[1]).toEqual(['age', '>=', 18]);
expect(f[2]).toEqual(['age', '<=', 65]);
});
it('should add contains filter', () => {
const f = createFilter<{ name: string }>()
.contains('name', 'alice')
.build();
expect(f).toEqual(['name', 'like', '%alice%']);
});
it('should add startsWith filter', () => {
const f = createFilter<{ name: string }>()
.startsWith('name', 'A')
.build();
expect(f).toEqual(['name', 'like', 'A%']);
});
it('should add endsWith filter', () => {
const f = createFilter<{ email: string }>()
.endsWith('email', '.com')
.build();
expect(f).toEqual(['email', 'like', '%.com']);
});
it('should add exists filter', () => {
const f = createFilter<{ phone: string }>()
.exists('phone')
.build();
expect(f).toEqual(['phone', 'is_not_null', null]);
});
});
// ==========================================
// Automation Client Tests
// ==========================================
describe('ObjectStackClient.automation', () => {
it('should list flows', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { flows: ['flow_a', 'flow_b'], total: 2, hasMore: false },
});
const result = await client.automation.list();
expect(fetchMock).toHaveBeenCalledWith(
'http://localhost:3000/api/v1/automation',
expect.any(Object),
);
expect(result.flows).toEqual(['flow_a', 'flow_b']);
});
it('should get a flow by name', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { name: 'my_flow', label: 'My Flow' },
});
const result = await client.automation.get('my_flow');
expect(fetchMock).toHaveBeenCalledWith(
'http://localhost:3000/api/v1/automation/my_flow',
expect.any(Object),
);
expect(result.name).toBe('my_flow');
});
it('should create a flow', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { name: 'new_flow' },
});
await client.automation.create('new_flow', { label: 'New' });
expect(fetchMock).toHaveBeenCalledWith(
'http://localhost:3000/api/v1/automation',
expect.objectContaining({ method: 'POST' }),
);
});
it('should update a flow', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { name: 'my_flow', label: 'Updated' },
});
await client.automation.update('my_flow', { label: 'Updated' });
expect(fetchMock).toHaveBeenCalledWith(
'http://localhost:3000/api/v1/automation/my_flow',
expect.objectContaining({ method: 'PUT' }),
);
});
it('should delete a flow', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { name: 'old_flow', deleted: true },
});
const result = await client.automation.delete('old_flow');
expect(fetchMock).toHaveBeenCalledWith(
'http://localhost:3000/api/v1/automation/old_flow',
expect.objectContaining({ method: 'DELETE' }),
);
expect(result.deleted).toBe(true);
});
it('should toggle a flow', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { name: 'my_flow', enabled: false },
});
const result = await client.automation.toggle('my_flow', false);
expect(fetchMock).toHaveBeenCalledWith(
'http://localhost:3000/api/v1/automation/my_flow/toggle',
expect.objectContaining({ method: 'POST' }),
);
expect(result.enabled).toBe(false);
});
it('should list runs for a flow', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { runs: [{ id: 'run_1' }], hasMore: false },
});
const result = await client.automation.runs.list('my_flow');
expect(fetchMock).toHaveBeenCalledWith(
'http://localhost:3000/api/v1/automation/my_flow/runs',
expect.any(Object),
);
expect(result.runs).toHaveLength(1);
});
it('should list runs with pagination options', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { runs: [], hasMore: false },
});
await client.automation.runs.list('my_flow', { limit: 5, cursor: 'abc' });
expect(fetchMock).toHaveBeenCalledWith(
'http://localhost:3000/api/v1/automation/my_flow/runs?limit=5&cursor=abc',
expect.any(Object),
);
});
it('should get a single run', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { id: 'run_1', status: 'completed' },
});
const result = await client.automation.runs.get('my_flow', 'run_1');
expect(fetchMock).toHaveBeenCalledWith(
'http://localhost:3000/api/v1/automation/my_flow/runs/run_1',
expect.any(Object),
);
expect(result.id).toBe('run_1');
});
it('should still support legacy trigger', async () => {
const { client, fetchMock } = createMockClient({ success: true, data: { result: 'ok' } });
await client.automation.trigger('my_flow', { key: 'val' });
expect(fetchMock).toHaveBeenCalledWith(
'http://localhost:3000/api/v1/automation/trigger/my_flow',
expect.objectContaining({ method: 'POST' }),
);
});
// ==========================================
// capabilities getter
// ==========================================
it('should return undefined capabilities before connect', () => {
const client = new ObjectStackClient({ baseUrl: 'http://localhost:3000' });
expect(client.capabilities).toBeUndefined();
});
it('should expose capabilities after connect', async () => {
const caps = {
comments: true,
automation: false,
cron: false,
search: true,
export: false,
chunkedUpload: false,
};
const fetchMock = vi.fn().mockResolvedValue({
ok: true,
json: async () => ({
version: 'v1',
apiName: 'ObjectStack API',
capabilities: caps,
}),
});
const client = new ObjectStackClient({
baseUrl: 'http://localhost:3000',
fetch: fetchMock,
});
await client.connect();
expect(client.capabilities).toBeDefined();
expect(client.capabilities!.comments).toBe(true);
expect(client.capabilities!.automation).toBe(false);
expect(client.capabilities!.search).toBe(true);
});
});
// ==========================================
// QueryOptionsV2 (Canonical Query Syntax) Tests
// ==========================================
describe('QueryOptionsV2 — canonical find()', () => {
it('should accept canonical field names (where, fields, orderBy, limit, offset)', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { object: 'account', records: [], total: 0 }
});
await client.data.find('account', {
where: { status: 'active' },
fields: ['name', 'email'],
orderBy: ['-created_at'],
limit: 10,
offset: 5,
});
const url = fetchMock.mock.calls[0][0] as string;
// V2 canonical options are normalized to HTTP transport params
expect(url).toContain('top=10');
expect(url).toContain('skip=5');
expect(url).toContain('select=name%2Cemail');
expect(url).toContain('sort=-created_at');
// where → filter as JSON
expect(url).toContain('status=active');
});
it('should still accept legacy field names (filter, select, sort, top, skip)', async () => {
const { client, fetchMock } = createMockClient({
success: true,
data: { object: 'account', records: [], total: 0 }
});
await client.data.find('account', {
filter: { industry: 'Tech' },
select: ['name'],
sort: ['-revenue'],
top: 20,
skip: 0,
});
const url = fetchMock.mock.calls[0][0] as string;
expect(url).toContain('top=20');
expect(url).toContain('select=name');
expect(url).toContain('sort=-revenue');
expect(url).toContain('industry=Tech');
});
});
describe('QueryBuilder — offset() alias', () => {
it('should set offset via .offset() method', () => {
const q = createQuery('task')
.limit(10)
.offset(20)
.build();
expect(q.limit).toBe(10);
expect(q.offset).toBe(20);
});
it('should set offset via deprecated .skip() method', () => {
const q = createQuery('task')
.limit(10)
.skip(30)
.build();
expect(q.offset).toBe(30);
});
});
// ----------------------------------------------------------------------
// ScopedProjectClient — project-scoped sub-client (Phase 2)
// ----------------------------------------------------------------------
describe('ScopedProjectClient', () => {
it('prefixes meta.getTypes with /projects/:id', async () => {
const { client, fetchMock } = createMockClient({ types: ['object'] });
const scoped = client.project('proj-123');
await scoped.meta.getTypes();
expect(fetchMock).toHaveBeenCalledWith(
'http://localhost:3000/api/v1/environments/proj-123/meta',
expect.any(Object),
);
});
it('prefixes data.find with /projects/:id', async () => {
const { client, fetchMock } = createMockClient({ records: [] });
const scoped = client.project('proj-123');
await scoped.data.find('task', { top: 5 });
const url = (fetchMock.mock.calls[0] as any[])[0] as string;
expect(url.startsWith('http://localhost:3000/api/v1/environments/proj-123/data/task')).toBe(true);
expect(url).toContain('top=5');
});
it('prefixes data.get / data.create / data.update / data.delete', async () => {
const { client, fetchMock } = createMockClient({ id: 't1' });
const scoped = client.project('proj-xyz');
await scoped.data.get('task', 't1');
expect(fetchMock).toHaveBeenLastCalledWith(
'http://localhost:3000/api/v1/environments/proj-xyz/data/task/t1',
expect.any(Object),
);
await scoped.data.create('task', { title: 'hi' });
expect(fetchMock).toHaveBeenLastCalledWith(