@objectstack/runtime 3.2.0 → 3.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@objectstack/runtime",
3
- "version": "3.2.0",
3
+ "version": "3.2.2",
4
4
  "license": "Apache-2.0",
5
5
  "description": "ObjectStack Core Runtime & Query Engine",
6
6
  "type": "module",
@@ -15,10 +15,10 @@
15
15
  },
16
16
  "dependencies": {
17
17
  "zod": "^4.3.6",
18
- "@objectstack/core": "3.2.0",
19
- "@objectstack/rest": "3.2.0",
20
- "@objectstack/spec": "3.2.0",
21
- "@objectstack/types": "3.2.0"
18
+ "@objectstack/core": "3.2.2",
19
+ "@objectstack/rest": "3.2.2",
20
+ "@objectstack/spec": "3.2.2",
21
+ "@objectstack/types": "3.2.2"
22
22
  },
23
23
  "devDependencies": {
24
24
  "typescript": "^5.0.0",
@@ -216,7 +216,7 @@ describe('HttpDispatcher', () => {
216
216
  it('should resolve analytics service from Promise (async factory)', async () => {
217
217
  const mockAnalytics = {
218
218
  query: vi.fn().mockResolvedValue({ rows: [{ id: 1 }], total: 1 }),
219
- getMetadata: vi.fn().mockResolvedValue({ tables: ['t1'] }),
219
+ getMeta: vi.fn().mockResolvedValue({ tables: ['t1'] }),
220
220
  generateSql: vi.fn().mockResolvedValue({ sql: 'SELECT 1' }),
221
221
  };
222
222
  // Inject as Promise (simulates async factory registration)
@@ -245,7 +245,7 @@ describe('HttpDispatcher', () => {
245
245
 
246
246
  it('should handle GET /analytics/meta with async service', async () => {
247
247
  const mockAnalytics = {
248
- getMetadata: vi.fn().mockResolvedValue({ tables: ['users', 'orders'] }),
248
+ getMeta: vi.fn().mockResolvedValue({ tables: ['users', 'orders'] }),
249
249
  };
250
250
  (kernel as any).getService = vi.fn().mockResolvedValue(mockAnalytics);
251
251
 
@@ -305,6 +305,59 @@ describe('HttpDispatcher', () => {
305
305
  });
306
306
  });
307
307
 
308
+ describe('handleAuth mock fallback (MSW/test mode)', () => {
309
+ beforeEach(() => {
310
+ // No auth service, no broker — simulates MSW/mock mode
311
+ (kernel as any).getService = vi.fn().mockResolvedValue(null);
312
+ (kernel as any).services = new Map();
313
+ (kernel as any).broker = null;
314
+ });
315
+
316
+ it('should mock sign-up/email endpoint', async () => {
317
+ const result = await dispatcher.handleAuth('/sign-up/email', 'POST', { email: 'test@example.com', name: 'Test' }, { request: {} });
318
+ expect(result.handled).toBe(true);
319
+ expect(result.response?.status).toBe(200);
320
+ expect(result.response?.body.user).toBeDefined();
321
+ expect(result.response?.body.user.email).toBe('test@example.com');
322
+ expect(result.response?.body.session).toBeDefined();
323
+ });
324
+
325
+ it('should mock sign-in/email endpoint', async () => {
326
+ const result = await dispatcher.handleAuth('/sign-in/email', 'POST', { email: 'test@example.com' }, { request: {} });
327
+ expect(result.handled).toBe(true);
328
+ expect(result.response?.status).toBe(200);
329
+ expect(result.response?.body.user).toBeDefined();
330
+ expect(result.response?.body.session).toBeDefined();
331
+ });
332
+
333
+ it('should mock get-session endpoint', async () => {
334
+ const result = await dispatcher.handleAuth('/get-session', 'GET', {}, { request: {} });
335
+ expect(result.handled).toBe(true);
336
+ expect(result.response?.status).toBe(200);
337
+ expect(result.response?.body).toEqual({ session: null, user: null });
338
+ });
339
+
340
+ it('should mock sign-out endpoint', async () => {
341
+ const result = await dispatcher.handleAuth('/sign-out', 'POST', {}, { request: {} });
342
+ expect(result.handled).toBe(true);
343
+ expect(result.response?.status).toBe(200);
344
+ expect(result.response?.body).toEqual({ success: true });
345
+ });
346
+
347
+ it('should mock login fallback when broker unavailable', async () => {
348
+ const result = await dispatcher.handleAuth('/login', 'POST', { email: 'test@example.com' }, { request: {} });
349
+ expect(result.handled).toBe(true);
350
+ expect(result.response?.status).toBe(200);
351
+ expect(result.response?.body.user).toBeDefined();
352
+ expect(result.response?.body.session).toBeDefined();
353
+ });
354
+
355
+ it('should return unhandled for unknown auth path in mock mode', async () => {
356
+ const result = await dispatcher.handleAuth('/unknown', 'GET', {}, { request: {} });
357
+ expect(result.handled).toBe(false);
358
+ });
359
+ });
360
+
308
361
  describe('handleStorage with async service', () => {
309
362
  it('should resolve storage service from Promise', async () => {
310
363
  const mockStorage = {
@@ -439,13 +492,115 @@ describe('HttpDispatcher', () => {
439
492
  });
440
493
  });
441
494
 
495
+ // ═══════════════════════════════════════════════════════════════
496
+ // getServiceAsync preferred path
497
+ // ═══════════════════════════════════════════════════════════════
498
+
499
+ describe('getServiceAsync preferred path', () => {
500
+ it('should prefer getServiceAsync over getService for analytics', async () => {
501
+ const asyncAnalytics = {
502
+ query: vi.fn().mockResolvedValue({ rows: [1], total: 1 }),
503
+ };
504
+ (kernel as any).getServiceAsync = vi.fn().mockResolvedValue(asyncAnalytics);
505
+ (kernel as any).getService = vi.fn().mockImplementation(() => {
506
+ throw new Error("Service 'analytics' is async - use await");
507
+ });
508
+
509
+ const result = await dispatcher.handleAnalytics('query', 'POST', {}, { request: {} });
510
+ expect(result.handled).toBe(true);
511
+ expect(asyncAnalytics.query).toHaveBeenCalled();
512
+ expect((kernel as any).getServiceAsync).toHaveBeenCalledWith('analytics');
513
+ });
514
+
515
+ it('should prefer getServiceAsync over getService for auth', async () => {
516
+ const asyncAuth = {
517
+ handler: vi.fn().mockResolvedValue({ user: { id: '1' } }),
518
+ };
519
+ (kernel as any).getServiceAsync = vi.fn().mockResolvedValue(asyncAuth);
520
+ (kernel as any).getService = vi.fn().mockImplementation(() => {
521
+ throw new Error("Service 'auth' is async - use await");
522
+ });
523
+
524
+ const result = await dispatcher.handleAuth('', 'POST', {}, { request: {}, response: {} });
525
+ expect(result.handled).toBe(true);
526
+ expect(asyncAuth.handler).toHaveBeenCalled();
527
+ expect((kernel as any).getServiceAsync).toHaveBeenCalledWith('auth');
528
+ });
529
+
530
+ it('should prefer getServiceAsync over getService for automation', async () => {
531
+ const asyncAuto = {
532
+ listFlows: vi.fn().mockResolvedValue(['flow_async']),
533
+ };
534
+ (kernel as any).getServiceAsync = vi.fn().mockResolvedValue(asyncAuto);
535
+
536
+ const result = await dispatcher.handleAutomation('', 'GET', {}, { request: {} });
537
+ expect(result.handled).toBe(true);
538
+ expect(result.response?.body?.data?.flows).toEqual(['flow_async']);
539
+ expect((kernel as any).getServiceAsync).toHaveBeenCalledWith('automation');
540
+ });
541
+
542
+ it('should prefer getServiceAsync over getService for file-storage', async () => {
543
+ const asyncStorage = {
544
+ upload: vi.fn().mockResolvedValue({ id: 'file_1', url: '/files/1' }),
545
+ };
546
+ (kernel as any).getServiceAsync = vi.fn().mockResolvedValue(asyncStorage);
547
+
548
+ const result = await dispatcher.handleStorage('/upload', 'POST', { name: 'test.txt' }, { request: {} });
549
+ expect(result.handled).toBe(true);
550
+ expect(result.response?.status).toBe(200);
551
+ expect((kernel as any).getServiceAsync).toHaveBeenCalledWith('file-storage');
552
+ });
553
+
554
+ it('should resolve protocol service via getServiceAsync for handleMetadata', async () => {
555
+ const asyncProtocol = {
556
+ saveMetaItem: vi.fn().mockResolvedValue({ success: true }),
557
+ };
558
+ (kernel as any).getServiceAsync = vi.fn().mockImplementation((name: string) => {
559
+ if (name === 'protocol') return Promise.resolve(asyncProtocol);
560
+ return Promise.resolve(null);
561
+ });
562
+ // Remove context.getService to ensure getServiceAsync is used
563
+ (kernel as any).context = {};
564
+
565
+ const result = await dispatcher.handleMetadata('/objects/my_obj', { request: {} }, 'PUT', { label: 'Test' });
566
+ expect(result.handled).toBe(true);
567
+ expect(result.response?.status).toBe(200);
568
+ expect(asyncProtocol.saveMetaItem).toHaveBeenCalled();
569
+ expect((kernel as any).getServiceAsync).toHaveBeenCalledWith('protocol');
570
+ });
571
+
572
+ it('should fall through when getServiceAsync returns null', async () => {
573
+ (kernel as any).getServiceAsync = vi.fn().mockResolvedValue(null);
574
+ const syncAnalytics = {
575
+ query: vi.fn().mockResolvedValue({ rows: [], total: 0 }),
576
+ };
577
+ (kernel as any).services = new Map([['analytics', syncAnalytics]]);
578
+
579
+ const result = await dispatcher.handleAnalytics('query', 'POST', {}, { request: {} });
580
+ expect(result.handled).toBe(true);
581
+ expect(syncAnalytics.query).toHaveBeenCalled();
582
+ });
583
+
584
+ it('should fall through when getServiceAsync throws', async () => {
585
+ (kernel as any).getServiceAsync = vi.fn().mockRejectedValue(new Error('not found'));
586
+ const syncAnalytics = {
587
+ query: vi.fn().mockResolvedValue({ rows: [], total: 0 }),
588
+ };
589
+ (kernel as any).services = new Map([['analytics', syncAnalytics]]);
590
+
591
+ const result = await dispatcher.handleAnalytics('query', 'POST', {}, { request: {} });
592
+ expect(result.handled).toBe(true);
593
+ expect(syncAnalytics.query).toHaveBeenCalled();
594
+ });
595
+ });
596
+
442
597
  // ═══════════════════════════════════════════════════════════════
443
598
  // handleData — expand/populate parameter flow
444
599
  // ═══════════════════════════════════════════════════════════════
445
600
 
446
601
  describe('handleData', () => {
447
602
  it('should pass expand and select to broker for GET /data/:object/:id', async () => {
448
- mockBroker.call.mockResolvedValue({ object: 'order_item', id: 'oi_1', record: { _id: 'oi_1' } });
603
+ mockBroker.call.mockResolvedValue({ object: 'order_item', id: 'oi_1', record: { id: 'oi_1' } });
449
604
 
450
605
  const result = await dispatcher.handleData(
451
606
  '/order_item/oi_1', 'GET', {},
@@ -684,4 +839,119 @@ describe('HttpDispatcher', () => {
684
839
  );
685
840
  });
686
841
  });
842
+
843
+ // ═══════════════════════════════════════════════════════════════
844
+ // handleI18n — i18n route dispatching
845
+ // ═══════════════════════════════════════════════════════════════
846
+
847
+ describe('handleI18n', () => {
848
+ let mockI18nService: any;
849
+
850
+ beforeEach(() => {
851
+ mockI18nService = {
852
+ getLocales: vi.fn().mockReturnValue(['en', 'zh-CN', 'ja']),
853
+ getTranslations: vi.fn().mockReturnValue({ 'o.account.label': '客户', 'o.account.fields.name': '名称' }),
854
+ getFieldLabels: vi.fn().mockReturnValue({ name: '名称', industry: '行业' }),
855
+ };
856
+
857
+ (kernel as any).getService = vi.fn().mockImplementation((name: string) => {
858
+ if (name === 'i18n') return mockI18nService;
859
+ return null;
860
+ });
861
+ });
862
+
863
+ it('should list locales via GET /locales', async () => {
864
+ const result = await dispatcher.handleI18n('/locales', 'GET', {}, { request: {} });
865
+ expect(result.handled).toBe(true);
866
+ expect(result.response?.status).toBe(200);
867
+ expect(result.response?.body?.data?.locales).toEqual(['en', 'zh-CN', 'ja']);
868
+ expect(mockI18nService.getLocales).toHaveBeenCalled();
869
+ });
870
+
871
+ it('should get translations via GET /translations/:locale', async () => {
872
+ const result = await dispatcher.handleI18n('/translations/zh-CN', 'GET', {}, { request: {} });
873
+ expect(result.handled).toBe(true);
874
+ expect(result.response?.status).toBe(200);
875
+ expect(result.response?.body?.data?.locale).toBe('zh-CN');
876
+ expect(result.response?.body?.data?.translations).toEqual({ 'o.account.label': '客户', 'o.account.fields.name': '名称' });
877
+ expect(mockI18nService.getTranslations).toHaveBeenCalledWith('zh-CN');
878
+ });
879
+
880
+ it('should get translations via GET /translations?locale=zh-CN (query param)', async () => {
881
+ const result = await dispatcher.handleI18n('/translations', 'GET', { locale: 'zh-CN' }, { request: {} });
882
+ expect(result.handled).toBe(true);
883
+ expect(result.response?.status).toBe(200);
884
+ expect(result.response?.body?.data?.locale).toBe('zh-CN');
885
+ expect(mockI18nService.getTranslations).toHaveBeenCalledWith('zh-CN');
886
+ });
887
+
888
+ it('should return 400 when translations requested without locale', async () => {
889
+ const result = await dispatcher.handleI18n('/translations', 'GET', {}, { request: {} });
890
+ expect(result.handled).toBe(true);
891
+ expect(result.response?.status).toBe(400);
892
+ expect(result.response?.body?.error?.message).toBe('Missing locale parameter');
893
+ });
894
+
895
+ it('should get field labels via GET /labels/:object/:locale', async () => {
896
+ const result = await dispatcher.handleI18n('/labels/account/zh-CN', 'GET', {}, { request: {} });
897
+ expect(result.handled).toBe(true);
898
+ expect(result.response?.status).toBe(200);
899
+ expect(result.response?.body?.data?.object).toBe('account');
900
+ expect(result.response?.body?.data?.locale).toBe('zh-CN');
901
+ expect(result.response?.body?.data?.labels).toEqual({ name: '名称', industry: '行业' });
902
+ expect(mockI18nService.getFieldLabels).toHaveBeenCalledWith('account', 'zh-CN');
903
+ });
904
+
905
+ it('should get field labels via GET /labels/:object?locale=zh-CN (query param)', async () => {
906
+ const result = await dispatcher.handleI18n('/labels/account', 'GET', { locale: 'zh-CN' }, { request: {} });
907
+ expect(result.handled).toBe(true);
908
+ expect(result.response?.status).toBe(200);
909
+ expect(result.response?.body?.data?.object).toBe('account');
910
+ expect(mockI18nService.getFieldLabels).toHaveBeenCalledWith('account', 'zh-CN');
911
+ });
912
+
913
+ it('should return 400 when labels requested without locale', async () => {
914
+ const result = await dispatcher.handleI18n('/labels/account', 'GET', {}, { request: {} });
915
+ expect(result.handled).toBe(true);
916
+ expect(result.response?.status).toBe(400);
917
+ expect(result.response?.body?.error?.message).toBe('Missing locale parameter');
918
+ });
919
+
920
+ it('should fallback to deriving labels from translations when getFieldLabels is missing', async () => {
921
+ delete mockI18nService.getFieldLabels;
922
+ mockI18nService.getTranslations.mockReturnValue({
923
+ 'o.contact.fields.first_name': 'First Name',
924
+ 'o.contact.fields.email': 'Email',
925
+ 'o.contact.label': 'Contact',
926
+ });
927
+
928
+ const result = await dispatcher.handleI18n('/labels/contact/en', 'GET', {}, { request: {} });
929
+ expect(result.handled).toBe(true);
930
+ expect(result.response?.status).toBe(200);
931
+ expect(result.response?.body?.data?.labels).toEqual({
932
+ first_name: 'First Name',
933
+ email: 'Email',
934
+ });
935
+ });
936
+
937
+ it('should return 501 when i18n service is not available', async () => {
938
+ (kernel as any).getService = vi.fn().mockResolvedValue(null);
939
+ (kernel as any).services = new Map();
940
+
941
+ const result = await dispatcher.handleI18n('/locales', 'GET', {}, { request: {} });
942
+ expect(result.handled).toBe(true);
943
+ expect(result.response?.status).toBe(501);
944
+ });
945
+
946
+ it('should return unhandled for non-GET methods', async () => {
947
+ const result = await dispatcher.handleI18n('/locales', 'POST', {}, { request: {} });
948
+ expect(result.handled).toBe(false);
949
+ });
950
+
951
+ it('should dispatch /i18n routes via dispatch()', async () => {
952
+ const result = await dispatcher.dispatch('GET', '/i18n/locales', undefined, {}, { request: {} });
953
+ expect(result.handled).toBe(true);
954
+ expect(result.response?.body?.data?.locales).toEqual(['en', 'zh-CN', 'ja']);
955
+ });
956
+ });
687
957
  });