@objectstack/runtime 4.0.4 → 4.1.0

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.
@@ -1,1317 +0,0 @@
1
-
2
- import { describe, it, expect, vi, beforeEach } from 'vitest';
3
- import { HttpDispatcher } from './http-dispatcher.js';
4
- import { ObjectKernel } from '@objectstack/core';
5
-
6
- describe('HttpDispatcher', () => {
7
- let kernel: ObjectKernel;
8
- let dispatcher: HttpDispatcher;
9
- let mockProtocol: any;
10
- let mockObjectQL: any;
11
-
12
- beforeEach(() => {
13
- // Mock Kernel
14
- mockProtocol = {
15
- saveMetaItem: vi.fn().mockResolvedValue({ success: true, message: 'Saved' }),
16
- getMetaItem: vi.fn().mockResolvedValue({ success: true, item: { foo: 'bar' } }),
17
- findData: vi.fn().mockResolvedValue({ object: 'test', records: [], total: 0 }),
18
- getData: vi.fn().mockResolvedValue({ object: 'test', id: '1', record: {} }),
19
- };
20
-
21
- mockObjectQL = {
22
- insert: vi.fn().mockResolvedValue({ id: 'new_1' }),
23
- find: vi.fn().mockResolvedValue([]),
24
- update: vi.fn().mockResolvedValue({}),
25
- delete: vi.fn().mockResolvedValue({}),
26
- getObjects: vi.fn().mockReturnValue({}),
27
- registry: {
28
- getObject: vi.fn().mockReturnValue({ name: 'test_obj' }),
29
- getRegisteredTypes: vi.fn().mockReturnValue([]),
30
- getAllPackages: vi.fn().mockReturnValue([]),
31
- },
32
- };
33
-
34
- kernel = {
35
- context: {
36
- getService: (name: string) => {
37
- if (name === 'protocol') return mockProtocol;
38
- if (name === 'objectql') return mockObjectQL;
39
- return null;
40
- }
41
- }
42
- } as any;
43
-
44
- dispatcher = new HttpDispatcher(kernel);
45
- });
46
-
47
- describe('handleMetadata', () => {
48
- it('should handle PUT /metadata/:type/:name by calling protocol.saveMetaItem', async () => {
49
- const context = { request: {} };
50
- const body = { label: 'New Label' };
51
- const path = '/objects/my_obj';
52
-
53
- const result = await dispatcher.handleMetadata(path, context, 'PUT', body);
54
-
55
- expect(result.handled).toBe(true);
56
- expect(result.response?.status).toBe(200);
57
- expect(mockProtocol.saveMetaItem).toHaveBeenCalledWith({
58
- type: 'objects',
59
- name: 'my_obj',
60
- item: body
61
- });
62
- expect(result.response?.body).toEqual({
63
- success: true,
64
- data: { success: true, message: 'Saved' },
65
- meta: undefined
66
- });
67
- });
68
-
69
- it('should fallback to MetadataService when protocol is missing saveMetaItem', async () => {
70
- // Mock protocol without saveMetaItem, but MetadataService with saveItem
71
- const mockMetaSvc = {
72
- saveItem: vi.fn().mockResolvedValue({ success: true, fromMetaSvc: true }),
73
- };
74
- (kernel as any).context.getService = (name: string) => {
75
- if (name === 'protocol') return {};
76
- if (name === 'metadata') return mockMetaSvc;
77
- if (name === 'objectql') return mockObjectQL;
78
- return null;
79
- };
80
-
81
- const context = { request: {} };
82
- const body = { label: 'Fallback' };
83
- const path = '/objects/my_obj';
84
-
85
- const result = await dispatcher.handleMetadata(path, context, 'PUT', body);
86
-
87
- expect(result.handled).toBe(true);
88
- expect(mockMetaSvc.saveItem).toHaveBeenCalledWith('objects', 'my_obj', body);
89
- expect(result.response?.body?.data).toEqual({ success: true, fromMetaSvc: true });
90
- });
91
-
92
- it('should return error if save fails', async () => {
93
- mockProtocol.saveMetaItem.mockRejectedValue(new Error('Save failed'));
94
-
95
- const context = { request: {} };
96
- const body = {};
97
- const path = '/objects/bad_obj';
98
-
99
- const result = await dispatcher.handleMetadata(path, context, 'PUT', body);
100
-
101
- expect(result.handled).toBe(true);
102
- expect(result.response?.status).toBe(400);
103
- expect(result.response?.body?.error?.message).toBe('Save failed');
104
- });
105
-
106
- it('should handle READ operations via ObjectQL registry', async () => {
107
- mockObjectQL.registry.getObject.mockReturnValue({ name: 'my_obj', fields: {} });
108
-
109
- const context = { request: {} };
110
- const result = await dispatcher.handleMetadata('/objects/my_obj', context, 'GET');
111
-
112
- expect(result.handled).toBe(true);
113
- expect(mockObjectQL.registry.getObject).toHaveBeenCalledWith('my_obj');
114
- });
115
- });
116
-
117
- describe('handleAutomation', () => {
118
- let mockAutomationService: any;
119
-
120
- beforeEach(() => {
121
- mockAutomationService = {
122
- listFlows: vi.fn().mockResolvedValue(['flow_a', 'flow_b']),
123
- getFlow: vi.fn().mockResolvedValue({ name: 'flow_a', label: 'Flow A' }),
124
- registerFlow: vi.fn(),
125
- unregisterFlow: vi.fn(),
126
- execute: vi.fn().mockResolvedValue({ success: true, output: {} }),
127
- toggleFlow: vi.fn().mockResolvedValue(undefined),
128
- listRuns: vi.fn().mockResolvedValue([{ id: 'run_1', status: 'completed' }]),
129
- getRun: vi.fn().mockResolvedValue({ id: 'run_1', status: 'completed' }),
130
- trigger: vi.fn().mockResolvedValue({ success: true }),
131
- };
132
-
133
- // Set up kernel services to include automation
134
- (kernel as any).services = new Map([
135
- ['automation', mockAutomationService],
136
- ]);
137
- });
138
-
139
- it('should list flows via GET /', async () => {
140
- const result = await dispatcher.handleAutomation('', 'GET', {}, { request: {} });
141
- expect(result.handled).toBe(true);
142
- expect(result.response?.body?.data?.flows).toEqual(['flow_a', 'flow_b']);
143
- });
144
-
145
- it('should get a flow via GET /:name', async () => {
146
- const result = await dispatcher.handleAutomation('flow_a', 'GET', {}, { request: {} });
147
- expect(result.handled).toBe(true);
148
- expect(result.response?.body?.data?.name).toBe('flow_a');
149
- });
150
-
151
- it('should return 404 for non-existent flow via GET /:name', async () => {
152
- mockAutomationService.getFlow.mockResolvedValue(null);
153
- const result = await dispatcher.handleAutomation('missing', 'GET', {}, { request: {} });
154
- expect(result.handled).toBe(true);
155
- expect(result.response?.status).toBe(404);
156
- });
157
-
158
- it('should create a flow via POST /', async () => {
159
- const body = { name: 'new_flow', label: 'New Flow' };
160
- const result = await dispatcher.handleAutomation('', 'POST', body, { request: {} });
161
- expect(result.handled).toBe(true);
162
- expect(mockAutomationService.registerFlow).toHaveBeenCalledWith('new_flow', body);
163
- });
164
-
165
- it('should update a flow via PUT /:name', async () => {
166
- const body = { definition: { label: 'Updated' } };
167
- const result = await dispatcher.handleAutomation('flow_a', 'PUT', body, { request: {} });
168
- expect(result.handled).toBe(true);
169
- expect(mockAutomationService.registerFlow).toHaveBeenCalledWith('flow_a', { label: 'Updated' });
170
- });
171
-
172
- it('should delete a flow via DELETE /:name', async () => {
173
- const result = await dispatcher.handleAutomation('flow_a', 'DELETE', {}, { request: {} });
174
- expect(result.handled).toBe(true);
175
- expect(mockAutomationService.unregisterFlow).toHaveBeenCalledWith('flow_a');
176
- expect(result.response?.body?.data?.deleted).toBe(true);
177
- });
178
-
179
- it('should trigger a flow via POST /:name/trigger', async () => {
180
- const result = await dispatcher.handleAutomation('flow_a/trigger', 'POST', { key: 'val' }, { request: {} });
181
- expect(result.handled).toBe(true);
182
- expect(mockAutomationService.execute).toHaveBeenCalledWith('flow_a', { key: 'val' });
183
- });
184
-
185
- it('should toggle a flow via POST /:name/toggle', async () => {
186
- const result = await dispatcher.handleAutomation('flow_a/toggle', 'POST', { enabled: false }, { request: {} });
187
- expect(result.handled).toBe(true);
188
- expect(mockAutomationService.toggleFlow).toHaveBeenCalledWith('flow_a', false);
189
- });
190
-
191
- it('should list runs via GET /:name/runs', async () => {
192
- const result = await dispatcher.handleAutomation('flow_a/runs', 'GET', {}, { request: {} });
193
- expect(result.handled).toBe(true);
194
- expect(result.response?.body?.data?.runs).toHaveLength(1);
195
- });
196
-
197
- it('should get a run via GET /:name/runs/:runId', async () => {
198
- const result = await dispatcher.handleAutomation('flow_a/runs/run_1', 'GET', {}, { request: {} });
199
- expect(result.handled).toBe(true);
200
- expect(result.response?.body?.data?.id).toBe('run_1');
201
- });
202
-
203
- it('should return 404 for non-existent run', async () => {
204
- mockAutomationService.getRun.mockResolvedValue(null);
205
- const result = await dispatcher.handleAutomation('flow_a/runs/missing', 'GET', {}, { request: {} });
206
- expect(result.handled).toBe(true);
207
- expect(result.response?.status).toBe(404);
208
- });
209
-
210
- it('should handle legacy trigger path POST /trigger/:name', async () => {
211
- const result = await dispatcher.handleAutomation('trigger/flow_a', 'POST', { data: 1 }, { request: {} });
212
- expect(result.handled).toBe(true);
213
- expect(mockAutomationService.trigger).toHaveBeenCalledWith('flow_a', { data: 1 }, { request: {} });
214
- });
215
- });
216
-
217
- // ═══════════════════════════════════════════════════════════════
218
- // Async Service Resolution Tests
219
- // Covers: getService awaits Promise-based (async factory) services
220
- // ═══════════════════════════════════════════════════════════════
221
-
222
- describe('Async service resolution (Promise-based injection)', () => {
223
-
224
- describe('handleAnalytics with async service', () => {
225
- it('should resolve analytics service from Promise (async factory)', async () => {
226
- const mockAnalytics = {
227
- query: vi.fn().mockResolvedValue({ rows: [{ id: 1 }], total: 1 }),
228
- getMeta: vi.fn().mockResolvedValue({ tables: ['t1'] }),
229
- generateSql: vi.fn().mockResolvedValue({ sql: 'SELECT 1' }),
230
- };
231
- // Inject as Promise (simulates async factory registration)
232
- (kernel as any).getService = vi.fn().mockImplementation((name: string) => {
233
- if (name === 'analytics') return Promise.resolve(mockAnalytics);
234
- return null;
235
- });
236
-
237
- const result = await dispatcher.handleAnalytics('query', 'POST', { sql: 'SELECT 1' }, { request: {} });
238
- expect(result.handled).toBe(true);
239
- expect(result.response?.status).toBe(200);
240
- expect(mockAnalytics.query).toHaveBeenCalled();
241
- });
242
-
243
- it('should handle POST /analytics/sql with async service', async () => {
244
- const mockAnalytics = {
245
- generateSql: vi.fn().mockResolvedValue({ sql: 'SELECT * FROM t' }),
246
- };
247
- (kernel as any).getService = vi.fn().mockResolvedValue(mockAnalytics);
248
-
249
- const result = await dispatcher.handleAnalytics('sql', 'POST', { object: 'test' }, { request: {} });
250
- expect(result.handled).toBe(true);
251
- expect(result.response?.status).toBe(200);
252
- expect(mockAnalytics.generateSql).toHaveBeenCalled();
253
- });
254
-
255
- it('should handle GET /analytics/meta with async service', async () => {
256
- const mockAnalytics = {
257
- getMeta: vi.fn().mockResolvedValue({ tables: ['users', 'orders'] }),
258
- };
259
- (kernel as any).getService = vi.fn().mockResolvedValue(mockAnalytics);
260
-
261
- const result = await dispatcher.handleAnalytics('meta', 'GET', {}, { request: {} });
262
- expect(result.handled).toBe(true);
263
- expect(result.response?.status).toBe(200);
264
- expect(result.response?.body?.data?.tables).toEqual(['users', 'orders']);
265
- });
266
-
267
- it('should return unhandled when analytics service is not registered', async () => {
268
- (kernel as any).getService = vi.fn().mockResolvedValue(null);
269
- (kernel as any).services = new Map();
270
-
271
- const result = await dispatcher.handleAnalytics('query', 'POST', {}, { request: {} });
272
- expect(result.handled).toBe(false);
273
- });
274
-
275
- it('should return unhandled for unknown analytics sub-path', async () => {
276
- const mockAnalytics = { query: vi.fn() };
277
- (kernel as any).getService = vi.fn().mockResolvedValue(mockAnalytics);
278
-
279
- const result = await dispatcher.handleAnalytics('unknown', 'POST', {}, { request: {} });
280
- expect(result.handled).toBe(false);
281
- });
282
- });
283
-
284
- describe('handleAuth with async service', () => {
285
- it('should resolve auth service from Promise', async () => {
286
- const mockAuth = {
287
- handler: vi.fn().mockResolvedValue({ user: { id: '1' } }),
288
- };
289
- (kernel as any).getService = vi.fn().mockImplementation((name: string) => {
290
- if (name === 'auth') return Promise.resolve(mockAuth);
291
- return null;
292
- });
293
-
294
- const result = await dispatcher.handleAuth('', 'POST', {}, { request: {}, response: {} });
295
- expect(result.handled).toBe(true);
296
- expect(mockAuth.handler).toHaveBeenCalled();
297
- });
298
-
299
- it('should fallback to mock auth when async auth service has no handler', async () => {
300
- (kernel as any).getService = vi.fn().mockResolvedValue({});
301
-
302
- const result = await dispatcher.handleAuth('/login', 'POST', { email: 'test@example.com' }, { request: {} });
303
- expect(result.handled).toBe(true);
304
- // Falls through to mock auth fallback (sign-in behavior)
305
- expect(result.response?.status).toBe(200);
306
- expect(result.response?.body?.user).toBeDefined();
307
- });
308
-
309
- it('should return unhandled when auth service not registered and no legacy match', async () => {
310
- (kernel as any).getService = vi.fn().mockResolvedValue(null);
311
- (kernel as any).services = new Map();
312
-
313
- const result = await dispatcher.handleAuth('/profile', 'GET', {}, { request: {} });
314
- expect(result.handled).toBe(false);
315
- });
316
- });
317
-
318
- describe('handleAuth mock fallback (MSW/test mode)', () => {
319
- beforeEach(() => {
320
- // No auth service — simulates MSW/mock mode
321
- (kernel as any).getService = vi.fn().mockResolvedValue(null);
322
- (kernel as any).services = new Map();
323
- });
324
-
325
- it('should mock sign-up/email endpoint', async () => {
326
- const result = await dispatcher.handleAuth('/sign-up/email', 'POST', { email: 'test@example.com', name: 'Test' }, { 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.user.email).toBe('test@example.com');
331
- expect(result.response?.body.session).toBeDefined();
332
- });
333
-
334
- it('should mock sign-in/email endpoint', async () => {
335
- const result = await dispatcher.handleAuth('/sign-in/email', 'POST', { email: 'test@example.com' }, { request: {} });
336
- expect(result.handled).toBe(true);
337
- expect(result.response?.status).toBe(200);
338
- expect(result.response?.body.user).toBeDefined();
339
- expect(result.response?.body.session).toBeDefined();
340
- });
341
-
342
- it('should mock get-session endpoint', async () => {
343
- const result = await dispatcher.handleAuth('/get-session', 'GET', {}, { request: {} });
344
- expect(result.handled).toBe(true);
345
- expect(result.response?.status).toBe(200);
346
- expect(result.response?.body).toEqual({ session: null, user: null });
347
- });
348
-
349
- it('should mock sign-out endpoint', async () => {
350
- const result = await dispatcher.handleAuth('/sign-out', 'POST', {}, { request: {} });
351
- expect(result.handled).toBe(true);
352
- expect(result.response?.status).toBe(200);
353
- expect(result.response?.body).toEqual({ success: true });
354
- });
355
-
356
- it('should mock login fallback when no auth service registered', async () => {
357
- const result = await dispatcher.handleAuth('/login', 'POST', { email: 'test@example.com' }, { request: {} });
358
- expect(result.handled).toBe(true);
359
- expect(result.response?.status).toBe(200);
360
- expect(result.response?.body.user).toBeDefined();
361
- expect(result.response?.body.session).toBeDefined();
362
- });
363
-
364
- it('should return unhandled for unknown auth path in mock mode', async () => {
365
- const result = await dispatcher.handleAuth('/unknown', 'GET', {}, { request: {} });
366
- expect(result.handled).toBe(false);
367
- });
368
- });
369
-
370
- describe('handleStorage with async service', () => {
371
- it('should resolve storage service from Promise', async () => {
372
- const mockStorage = {
373
- upload: vi.fn().mockResolvedValue({ id: 'file_1', url: '/files/1' }),
374
- };
375
- (kernel as any).getService = vi.fn().mockImplementation((name: string) => {
376
- if (name === 'file-storage') return Promise.resolve(mockStorage);
377
- return null;
378
- });
379
-
380
- const result = await dispatcher.handleStorage('/upload', 'POST', { name: 'test.txt' }, { request: {} });
381
- expect(result.handled).toBe(true);
382
- expect(result.response?.status).toBe(200);
383
- expect(mockStorage.upload).toHaveBeenCalled();
384
- });
385
-
386
- it('should return 501 when storage service is not registered (async null)', async () => {
387
- (kernel as any).getService = vi.fn().mockResolvedValue(null);
388
- (kernel as any).services = new Map();
389
-
390
- const result = await dispatcher.handleStorage('/upload', 'POST', {}, { request: {} });
391
- expect(result.handled).toBe(true);
392
- expect(result.response?.status).toBe(501);
393
- expect(result.response?.body?.error?.message).toBe('File storage not configured');
394
- });
395
-
396
- it('should handle GET /storage/file/:id with async service', async () => {
397
- const mockStorage = {
398
- download: vi.fn().mockResolvedValue({ data: 'content', mimeType: 'text/plain' }),
399
- };
400
- (kernel as any).getService = vi.fn().mockImplementation((name: string) => {
401
- if (name === 'file-storage') return Promise.resolve(mockStorage);
402
- return null;
403
- });
404
-
405
- const result = await dispatcher.handleStorage('/file/abc123', 'GET', null, { request: {} });
406
- expect(result.handled).toBe(true);
407
- expect(mockStorage.download).toHaveBeenCalledWith('abc123', { request: {} });
408
- });
409
-
410
- it('should return 400 when upload has no file', async () => {
411
- const mockStorage = { upload: vi.fn() };
412
- (kernel as any).getService = vi.fn().mockResolvedValue(mockStorage);
413
-
414
- const result = await dispatcher.handleStorage('/upload', 'POST', null, { request: {} });
415
- expect(result.handled).toBe(true);
416
- expect(result.response?.status).toBe(400);
417
- expect(result.response?.body?.error?.message).toBe('No file provided');
418
- });
419
- });
420
-
421
- describe('handleAutomation with async service', () => {
422
- it('should resolve automation service from Promise (async factory)', async () => {
423
- const mockAuto = {
424
- listFlows: vi.fn().mockResolvedValue(['f1']),
425
- };
426
- (kernel as any).getService = vi.fn().mockImplementation((name: string) => {
427
- if (name === 'automation') return Promise.resolve(mockAuto);
428
- return null;
429
- });
430
-
431
- const result = await dispatcher.handleAutomation('', 'GET', {}, { request: {} });
432
- expect(result.handled).toBe(true);
433
- expect(result.response?.body?.data?.flows).toEqual(['f1']);
434
- });
435
-
436
- it('should return unhandled when automation service not registered', async () => {
437
- (kernel as any).getService = vi.fn().mockResolvedValue(null);
438
- (kernel as any).services = new Map();
439
-
440
- const result = await dispatcher.handleAutomation('', 'GET', {}, { request: {} });
441
- expect(result.handled).toBe(false);
442
- });
443
- });
444
-
445
- describe('handleMetadata with async protocol service', () => {
446
- it('should resolve protocol service from async getService', async () => {
447
- const asyncProtocol = {
448
- saveMetaItem: vi.fn().mockResolvedValue({ success: true }),
449
- };
450
- (kernel as any).context.getService = vi.fn().mockImplementation((name: string) => {
451
- if (name === 'protocol') return Promise.resolve(asyncProtocol);
452
- return null;
453
- });
454
-
455
- const result = await dispatcher.handleMetadata('/objects/my_obj', { request: {} }, 'PUT', { label: 'Test' });
456
- expect(result.handled).toBe(true);
457
- expect(result.response?.status).toBe(200);
458
- expect(asyncProtocol.saveMetaItem).toHaveBeenCalled();
459
- });
460
-
461
- it('should fallback to ObjectQL registry when async protocol returns null', async () => {
462
- (kernel as any).context.getService = vi.fn().mockImplementation((name: string) => {
463
- if (name === 'objectql') return mockObjectQL;
464
- return null;
465
- });
466
- mockObjectQL.registry.getObject.mockReturnValue({ name: 'my_obj', fields: {} });
467
-
468
- const result = await dispatcher.handleMetadata('/objects/my_obj', { request: {} }, 'GET');
469
- expect(result.handled).toBe(true);
470
- expect(mockObjectQL.registry.getObject).toHaveBeenCalledWith('my_obj');
471
- });
472
- });
473
- });
474
-
475
- // ═══════════════════════════════════════════════════════════════
476
- // Synchronous service resolution (backward compatibility)
477
- // ═══════════════════════════════════════════════════════════════
478
-
479
- describe('Synchronous service resolution (backward compat)', () => {
480
- it('should work with synchronous service from services Map', async () => {
481
- const syncAnalytics = {
482
- query: vi.fn().mockResolvedValue({ rows: [], total: 0 }),
483
- };
484
- (kernel as any).services = new Map([['analytics', syncAnalytics]]);
485
-
486
- const result = await dispatcher.handleAnalytics('query', 'POST', {}, { request: {} });
487
- expect(result.handled).toBe(true);
488
- expect(syncAnalytics.query).toHaveBeenCalled();
489
- });
490
-
491
- it('should work with synchronous getService returning service directly', async () => {
492
- const syncAuto = {
493
- listFlows: vi.fn().mockResolvedValue(['flow_x']),
494
- };
495
- (kernel as any).getService = vi.fn().mockReturnValue(syncAuto);
496
-
497
- const result = await dispatcher.handleAutomation('', 'GET', {}, { request: {} });
498
- expect(result.handled).toBe(true);
499
- expect(result.response?.body?.data?.flows).toEqual(['flow_x']);
500
- });
501
- });
502
-
503
- // ═══════════════════════════════════════════════════════════════
504
- // getServiceAsync preferred path
505
- // ═══════════════════════════════════════════════════════════════
506
-
507
- describe('getServiceAsync preferred path', () => {
508
- it('should prefer getServiceAsync over getService for analytics', async () => {
509
- const asyncAnalytics = {
510
- query: vi.fn().mockResolvedValue({ rows: [1], total: 1 }),
511
- };
512
- (kernel as any).getServiceAsync = vi.fn().mockResolvedValue(asyncAnalytics);
513
- (kernel as any).getService = vi.fn().mockImplementation(() => {
514
- throw new Error("Service 'analytics' is async - use await");
515
- });
516
-
517
- const result = await dispatcher.handleAnalytics('query', 'POST', {}, { request: {} });
518
- expect(result.handled).toBe(true);
519
- expect(asyncAnalytics.query).toHaveBeenCalled();
520
- expect((kernel as any).getServiceAsync).toHaveBeenCalledWith('analytics');
521
- });
522
-
523
- it('should prefer getServiceAsync over getService for auth', async () => {
524
- const asyncAuth = {
525
- handler: vi.fn().mockResolvedValue({ user: { id: '1' } }),
526
- };
527
- (kernel as any).getServiceAsync = vi.fn().mockResolvedValue(asyncAuth);
528
- (kernel as any).getService = vi.fn().mockImplementation(() => {
529
- throw new Error("Service 'auth' is async - use await");
530
- });
531
-
532
- const result = await dispatcher.handleAuth('', 'POST', {}, { request: {}, response: {} });
533
- expect(result.handled).toBe(true);
534
- expect(asyncAuth.handler).toHaveBeenCalled();
535
- expect((kernel as any).getServiceAsync).toHaveBeenCalledWith('auth');
536
- });
537
-
538
- it('should prefer getServiceAsync over getService for automation', async () => {
539
- const asyncAuto = {
540
- listFlows: vi.fn().mockResolvedValue(['flow_async']),
541
- };
542
- (kernel as any).getServiceAsync = vi.fn().mockResolvedValue(asyncAuto);
543
-
544
- const result = await dispatcher.handleAutomation('', 'GET', {}, { request: {} });
545
- expect(result.handled).toBe(true);
546
- expect(result.response?.body?.data?.flows).toEqual(['flow_async']);
547
- expect((kernel as any).getServiceAsync).toHaveBeenCalledWith('automation');
548
- });
549
-
550
- it('should prefer getServiceAsync over getService for file-storage', async () => {
551
- const asyncStorage = {
552
- upload: vi.fn().mockResolvedValue({ id: 'file_1', url: '/files/1' }),
553
- };
554
- (kernel as any).getServiceAsync = vi.fn().mockResolvedValue(asyncStorage);
555
-
556
- const result = await dispatcher.handleStorage('/upload', 'POST', { name: 'test.txt' }, { request: {} });
557
- expect(result.handled).toBe(true);
558
- expect(result.response?.status).toBe(200);
559
- expect((kernel as any).getServiceAsync).toHaveBeenCalledWith('file-storage');
560
- });
561
-
562
- it('should resolve protocol service via getServiceAsync for handleMetadata', async () => {
563
- const asyncProtocol = {
564
- saveMetaItem: vi.fn().mockResolvedValue({ success: true }),
565
- };
566
- (kernel as any).getServiceAsync = vi.fn().mockImplementation((name: string) => {
567
- if (name === 'protocol') return Promise.resolve(asyncProtocol);
568
- return Promise.resolve(null);
569
- });
570
- // Remove context.getService to ensure getServiceAsync is used
571
- (kernel as any).context = {};
572
-
573
- const result = await dispatcher.handleMetadata('/objects/my_obj', { request: {} }, 'PUT', { label: 'Test' });
574
- expect(result.handled).toBe(true);
575
- expect(result.response?.status).toBe(200);
576
- expect(asyncProtocol.saveMetaItem).toHaveBeenCalled();
577
- expect((kernel as any).getServiceAsync).toHaveBeenCalledWith('protocol');
578
- });
579
-
580
- it('should fall through when getServiceAsync returns null', async () => {
581
- (kernel as any).getServiceAsync = vi.fn().mockResolvedValue(null);
582
- const syncAnalytics = {
583
- query: vi.fn().mockResolvedValue({ rows: [], total: 0 }),
584
- };
585
- (kernel as any).services = new Map([['analytics', syncAnalytics]]);
586
-
587
- const result = await dispatcher.handleAnalytics('query', 'POST', {}, { request: {} });
588
- expect(result.handled).toBe(true);
589
- expect(syncAnalytics.query).toHaveBeenCalled();
590
- });
591
-
592
- it('should fall through when getServiceAsync throws', async () => {
593
- (kernel as any).getServiceAsync = vi.fn().mockRejectedValue(new Error('not found'));
594
- const syncAnalytics = {
595
- query: vi.fn().mockResolvedValue({ rows: [], total: 0 }),
596
- };
597
- (kernel as any).services = new Map([['analytics', syncAnalytics]]);
598
-
599
- const result = await dispatcher.handleAnalytics('query', 'POST', {}, { request: {} });
600
- expect(result.handled).toBe(true);
601
- expect(syncAnalytics.query).toHaveBeenCalled();
602
- });
603
- });
604
-
605
- // ═══════════════════════════════════════════════════════════════
606
- // handleData — expand/populate parameter flow
607
- // ═══════════════════════════════════════════════════════════════
608
-
609
- describe('handleData', () => {
610
- it('should pass expand and select to protocol for GET /data/:object/:id', async () => {
611
- mockProtocol.getData.mockResolvedValue({ object: 'order_item', id: 'oi_1', record: { id: 'oi_1' } });
612
-
613
- const result = await dispatcher.handleData(
614
- '/order_item/oi_1', 'GET', {},
615
- { expand: 'order,product', select: 'name,total' },
616
- { request: {} }
617
- );
618
-
619
- expect(result.handled).toBe(true);
620
- expect(result.response?.status).toBe(200);
621
- expect(mockProtocol.getData).toHaveBeenCalledWith(
622
- { object: 'order_item', id: 'oi_1', expand: 'order,product', select: 'name,total' }
623
- );
624
- });
625
-
626
- it('should NOT pass non-allowlisted params for GET /data/:object/:id', async () => {
627
- mockProtocol.getData.mockResolvedValue({ object: 'task', id: 't1', record: {} });
628
-
629
- await dispatcher.handleData(
630
- '/task/t1', 'GET', {},
631
- { expand: 'assignee', malicious: 'drop_table', filter: 'hack' },
632
- { request: {} }
633
- );
634
-
635
- // Only expand is passed; malicious and filter are dropped
636
- expect(mockProtocol.getData).toHaveBeenCalledWith(
637
- { object: 'task', id: 't1', expand: 'assignee' }
638
- );
639
- });
640
-
641
- it('should pass full query (with expand/populate) for GET /data/:object list', async () => {
642
- mockProtocol.findData.mockResolvedValue({ object: 'task', records: [], total: 0 });
643
-
644
- const query = { populate: 'assignee,project', top: '10', skip: '0' };
645
- const result = await dispatcher.handleData(
646
- '/task', 'GET', {},
647
- query,
648
- { request: {} }
649
- );
650
-
651
- expect(result.handled).toBe(true);
652
- // top → limit and skip → offset are normalized by the dispatcher
653
- expect(mockProtocol.findData).toHaveBeenCalledWith(
654
- { object: 'task', query: { populate: 'assignee,project', limit: '10', offset: '0' } }
655
- );
656
- });
657
-
658
- it('should pass expand in query for GET /data/:object list', async () => {
659
- mockProtocol.findData.mockResolvedValue({ object: 'order', records: [], total: 0 });
660
-
661
- const query = { expand: 'customer,products' };
662
- await dispatcher.handleData('/order', 'GET', {}, query, { request: {} });
663
-
664
- expect(mockProtocol.findData).toHaveBeenCalledWith(
665
- { object: 'order', query: { expand: 'customer,products' } }
666
- );
667
- });
668
-
669
- it('should return error if object name is missing', async () => {
670
- const result = await dispatcher.handleData('/', 'GET', {}, {}, { request: {} });
671
- expect(result.handled).toBe(true);
672
- expect(result.response?.status).toBe(400);
673
- });
674
-
675
- it('should handle POST /data/:object/query with body containing expand', async () => {
676
- mockProtocol.findData.mockResolvedValue({ object: 'task', records: [] });
677
-
678
- await dispatcher.handleData(
679
- '/task/query', 'POST',
680
- { filter: { status: 'active' }, populate: ['assignee'] },
681
- {},
682
- { request: {} }
683
- );
684
-
685
- expect(mockProtocol.findData).toHaveBeenCalledWith(
686
- { object: 'task', query: { filter: { status: 'active' }, populate: ['assignee'] } }
687
- );
688
- });
689
- });
690
-
691
- // ═══════════════════════════════════════════════════════════════
692
- // Error handling for service method failures
693
- // ═══════════════════════════════════════════════════════════════
694
-
695
- describe('Service method error handling', () => {
696
- it('should propagate analytics query error', async () => {
697
- const badAnalytics = {
698
- query: vi.fn().mockRejectedValue(new Error('Query timeout')),
699
- };
700
- (kernel as any).getService = vi.fn().mockResolvedValue(badAnalytics);
701
-
702
- await expect(
703
- dispatcher.handleAnalytics('query', 'POST', {}, { request: {} })
704
- ).rejects.toThrow('Query timeout');
705
- });
706
-
707
- it('should propagate storage upload error', async () => {
708
- const badStorage = {
709
- upload: vi.fn().mockRejectedValue(new Error('Disk full')),
710
- };
711
- (kernel as any).getService = vi.fn().mockImplementation((name: string) => {
712
- if (name === 'file-storage') return Promise.resolve(badStorage);
713
- return null;
714
- });
715
-
716
- await expect(
717
- dispatcher.handleStorage('/upload', 'POST', { data: 'file' }, { request: {} })
718
- ).rejects.toThrow('Disk full');
719
- });
720
- });
721
-
722
- // ═══════════════════════════════════════════════════════════════
723
- // Package Publish / Revert Endpoints
724
- // ═══════════════════════════════════════════════════════════════
725
-
726
- describe('Package publish/revert endpoints', () => {
727
- it('should handle POST /packages/:id/publish via metadata service', async () => {
728
- const mockMetadata = {
729
- publishPackage: vi.fn().mockResolvedValue({
730
- success: true,
731
- packageId: 'com.acme.crm',
732
- version: 2,
733
- publishedAt: '2025-06-01T00:00:00Z',
734
- itemsPublished: 3,
735
- }),
736
- };
737
- const mockRegistry = {
738
- getAllPackages: vi.fn().mockReturnValue([]),
739
- enablePackage: vi.fn(),
740
- disablePackage: vi.fn(),
741
- };
742
- (kernel as any).getService = vi.fn().mockImplementation((name: string) => {
743
- if (name === 'metadata') return Promise.resolve(mockMetadata);
744
- if (name === 'objectql') return Promise.resolve({ registry: mockRegistry });
745
- return null;
746
- });
747
-
748
- const result = await dispatcher.handlePackages('/com.acme.crm/publish', 'POST', { publishedBy: 'admin' }, {}, { request: {} });
749
- expect(result.handled).toBe(true);
750
- expect(result.response?.status).toBe(200);
751
- expect(mockMetadata.publishPackage).toHaveBeenCalledWith('com.acme.crm', { publishedBy: 'admin' });
752
- });
753
-
754
- it('should handle POST /packages/:id/revert via metadata service', async () => {
755
- const mockMetadata = {
756
- revertPackage: vi.fn().mockResolvedValue(undefined),
757
- };
758
- const mockRegistry = {
759
- getAllPackages: vi.fn().mockReturnValue([]),
760
- enablePackage: vi.fn(),
761
- disablePackage: vi.fn(),
762
- };
763
- (kernel as any).getService = vi.fn().mockImplementation((name: string) => {
764
- if (name === 'metadata') return Promise.resolve(mockMetadata);
765
- if (name === 'objectql') return Promise.resolve({ registry: mockRegistry });
766
- return null;
767
- });
768
-
769
- const result = await dispatcher.handlePackages('/com.acme.crm/revert', 'POST', {}, {}, { request: {} });
770
- expect(result.handled).toBe(true);
771
- expect(result.response?.status).toBe(200);
772
- expect(mockMetadata.revertPackage).toHaveBeenCalledWith('com.acme.crm');
773
- });
774
-
775
- it('should return 503 for publish when metadata service unavailable', async () => {
776
- const mockRegistry = {
777
- getAllPackages: vi.fn().mockReturnValue([]),
778
- };
779
- (kernel as any).getService = vi.fn().mockImplementation((name: string) => {
780
- if (name === 'metadata') return Promise.resolve(null);
781
- if (name === 'objectql') return Promise.resolve({ registry: mockRegistry });
782
- return null;
783
- });
784
-
785
- const result = await dispatcher.handlePackages('/crm/publish', 'POST', {}, {}, { request: {} });
786
- expect(result.handled).toBe(true);
787
- expect(result.response?.status).toBe(503);
788
- });
789
- });
790
-
791
- // ═══════════════════════════════════════════════════════════════
792
- // Metadata getPublished Endpoint
793
- // ═══════════════════════════════════════════════════════════════
794
-
795
- describe('Metadata getPublished endpoint', () => {
796
- it('should handle GET /metadata/:type/:name/published via metadata service', async () => {
797
- const mockMetadata = {
798
- getPublished: vi.fn().mockResolvedValue({ name: 'account', label: 'Account' }),
799
- };
800
- (kernel as any).getService = vi.fn().mockImplementation((name: string) => {
801
- if (name === 'metadata') return Promise.resolve(mockMetadata);
802
- return null;
803
- });
804
-
805
- const result = await dispatcher.handleMetadata('/object/account/published', { request: {} }, 'GET');
806
- expect(result.handled).toBe(true);
807
- expect(result.response?.status).toBe(200);
808
- expect(result.response?.body?.data).toEqual({ name: 'account', label: 'Account' });
809
- expect(mockMetadata.getPublished).toHaveBeenCalledWith('object', 'account');
810
- });
811
-
812
- it('should return 404 when published item not found', async () => {
813
- const mockMetadata = {
814
- getPublished: vi.fn().mockResolvedValue(undefined),
815
- };
816
- (kernel as any).getService = vi.fn().mockImplementation((name: string) => {
817
- if (name === 'metadata') return Promise.resolve(mockMetadata);
818
- return null;
819
- });
820
-
821
- const result = await dispatcher.handleMetadata('/object/nonexistent/published', { request: {} }, 'GET');
822
- expect(result.handled).toBe(true);
823
- expect(result.response?.status).toBe(404);
824
- });
825
-
826
- it('should fallback to resolveService for getPublished when metadata service unavailable', async () => {
827
- const metaSvc = {
828
- getPublished: vi.fn().mockResolvedValue({ name: 'account', fields: ['name'] }),
829
- };
830
- (kernel as any).getService = vi.fn().mockImplementation((name: string) => {
831
- if (name === 'metadata') return Promise.resolve(metaSvc);
832
- if (name === 'objectql') return Promise.resolve(mockObjectQL);
833
- return null;
834
- });
835
- (kernel as any).context = {
836
- getService: (name: string) => {
837
- if (name === 'metadata') return metaSvc;
838
- if (name === 'objectql') return mockObjectQL;
839
- return null;
840
- }
841
- };
842
-
843
- const result = await dispatcher.handleMetadata('/object/account/published', { request: {} }, 'GET');
844
- expect(result.handled).toBe(true);
845
- expect(result.response?.status).toBe(200);
846
- expect(metaSvc.getPublished).toHaveBeenCalledWith('object', 'account');
847
- });
848
- });
849
-
850
- // ═══════════════════════════════════════════════════════════════
851
- // handleI18n — i18n route dispatching
852
- // ═══════════════════════════════════════════════════════════════
853
-
854
- describe('handleI18n', () => {
855
- let mockI18nService: any;
856
-
857
- beforeEach(() => {
858
- mockI18nService = {
859
- getLocales: vi.fn().mockReturnValue(['en', 'zh-CN', 'ja']),
860
- getTranslations: vi.fn().mockReturnValue({ 'o.account.label': '客户', 'o.account.fields.name': '名称' }),
861
- getFieldLabels: vi.fn().mockReturnValue({ name: '名称', industry: '行业' }),
862
- };
863
-
864
- (kernel as any).getService = vi.fn().mockImplementation((name: string) => {
865
- if (name === 'i18n') return mockI18nService;
866
- return null;
867
- });
868
- });
869
-
870
- it('should list locales via GET /locales', async () => {
871
- const result = await dispatcher.handleI18n('/locales', 'GET', {}, { request: {} });
872
- expect(result.handled).toBe(true);
873
- expect(result.response?.status).toBe(200);
874
- expect(result.response?.body?.data?.locales).toEqual(['en', 'zh-CN', 'ja']);
875
- expect(mockI18nService.getLocales).toHaveBeenCalled();
876
- });
877
-
878
- it('should get translations via GET /translations/:locale', async () => {
879
- const result = await dispatcher.handleI18n('/translations/zh-CN', 'GET', {}, { request: {} });
880
- expect(result.handled).toBe(true);
881
- expect(result.response?.status).toBe(200);
882
- expect(result.response?.body?.data?.locale).toBe('zh-CN');
883
- expect(result.response?.body?.data?.translations).toEqual({ 'o.account.label': '客户', 'o.account.fields.name': '名称' });
884
- expect(mockI18nService.getTranslations).toHaveBeenCalledWith('zh-CN');
885
- });
886
-
887
- it('should get translations via GET /translations?locale=zh-CN (query param)', async () => {
888
- const result = await dispatcher.handleI18n('/translations', 'GET', { locale: 'zh-CN' }, { request: {} });
889
- expect(result.handled).toBe(true);
890
- expect(result.response?.status).toBe(200);
891
- expect(result.response?.body?.data?.locale).toBe('zh-CN');
892
- expect(mockI18nService.getTranslations).toHaveBeenCalledWith('zh-CN');
893
- });
894
-
895
- it('should return 400 when translations requested without locale', async () => {
896
- const result = await dispatcher.handleI18n('/translations', 'GET', {}, { request: {} });
897
- expect(result.handled).toBe(true);
898
- expect(result.response?.status).toBe(400);
899
- expect(result.response?.body?.error?.message).toBe('Missing locale parameter');
900
- });
901
-
902
- it('should get field labels via GET /labels/:object/:locale', async () => {
903
- const result = await dispatcher.handleI18n('/labels/account/zh-CN', 'GET', {}, { request: {} });
904
- expect(result.handled).toBe(true);
905
- expect(result.response?.status).toBe(200);
906
- expect(result.response?.body?.data?.object).toBe('account');
907
- expect(result.response?.body?.data?.locale).toBe('zh-CN');
908
- expect(result.response?.body?.data?.labels).toEqual({ name: '名称', industry: '行业' });
909
- expect(mockI18nService.getFieldLabels).toHaveBeenCalledWith('account', 'zh-CN');
910
- });
911
-
912
- it('should get field labels via GET /labels/:object?locale=zh-CN (query param)', async () => {
913
- const result = await dispatcher.handleI18n('/labels/account', 'GET', { locale: 'zh-CN' }, { request: {} });
914
- expect(result.handled).toBe(true);
915
- expect(result.response?.status).toBe(200);
916
- expect(result.response?.body?.data?.object).toBe('account');
917
- expect(mockI18nService.getFieldLabels).toHaveBeenCalledWith('account', 'zh-CN');
918
- });
919
-
920
- it('should return 400 when labels requested without locale', async () => {
921
- const result = await dispatcher.handleI18n('/labels/account', 'GET', {}, { request: {} });
922
- expect(result.handled).toBe(true);
923
- expect(result.response?.status).toBe(400);
924
- expect(result.response?.body?.error?.message).toBe('Missing locale parameter');
925
- });
926
-
927
- it('should fallback to deriving labels from translations when getFieldLabels is missing', async () => {
928
- delete mockI18nService.getFieldLabels;
929
- mockI18nService.getTranslations.mockReturnValue({
930
- 'o.contact.fields.first_name': 'First Name',
931
- 'o.contact.fields.email': 'Email',
932
- 'o.contact.label': 'Contact',
933
- });
934
-
935
- const result = await dispatcher.handleI18n('/labels/contact/en', 'GET', {}, { request: {} });
936
- expect(result.handled).toBe(true);
937
- expect(result.response?.status).toBe(200);
938
- expect(result.response?.body?.data?.labels).toEqual({
939
- first_name: 'First Name',
940
- email: 'Email',
941
- });
942
- });
943
-
944
- it('should return 501 when i18n service is not available', async () => {
945
- (kernel as any).getService = vi.fn().mockResolvedValue(null);
946
- (kernel as any).services = new Map();
947
-
948
- const result = await dispatcher.handleI18n('/locales', 'GET', {}, { request: {} });
949
- expect(result.handled).toBe(true);
950
- expect(result.response?.status).toBe(501);
951
- });
952
-
953
- it('should return unhandled for non-GET methods', async () => {
954
- const result = await dispatcher.handleI18n('/locales', 'POST', {}, { request: {} });
955
- expect(result.handled).toBe(false);
956
- });
957
-
958
- it('should dispatch /i18n routes via dispatch()', async () => {
959
- const result = await dispatcher.dispatch('GET', '/i18n/locales', undefined, {}, { request: {} });
960
- expect(result.handled).toBe(true);
961
- expect(result.response?.body?.data?.locales).toEqual(['en', 'zh-CN', 'ja']);
962
- });
963
-
964
- it('should resolve locale via fallback (zh → zh-CN) for translations', async () => {
965
- // Override mock to be locale-aware: only 'zh-CN' has data, 'zh' returns empty
966
- mockI18nService.getTranslations = vi.fn().mockImplementation((locale: string) => {
967
- if (locale === 'zh-CN') return { 'o.task.label': '任务' };
968
- return {};
969
- });
970
-
971
- const result = await dispatcher.handleI18n('/translations/zh', 'GET', {}, { request: {} });
972
- expect(result.handled).toBe(true);
973
- expect(result.response?.status).toBe(200);
974
- const data = result.response?.body?.data;
975
- expect(data.locale).toBe('zh-CN');
976
- expect(data.requestedLocale).toBe('zh');
977
- expect(data.translations).toEqual({ 'o.task.label': '任务' });
978
- });
979
-
980
- it('should resolve locale via case-insensitive fallback (ZH-CN → zh-CN) for translations', async () => {
981
- // Override mock to be locale-aware: 'ZH-CN' returns empty, 'zh-CN' has data
982
- mockI18nService.getTranslations = vi.fn().mockImplementation((locale: string) => {
983
- if (locale === 'zh-CN') return { 'o.task.label': '任务' };
984
- return {};
985
- });
986
-
987
- const result = await dispatcher.handleI18n('/translations/ZH-CN', 'GET', {}, { request: {} });
988
- expect(result.handled).toBe(true);
989
- expect(result.response?.status).toBe(200);
990
- const data = result.response?.body?.data;
991
- expect(data.locale).toBe('zh-CN');
992
- expect(data.translations).toEqual({ 'o.task.label': '任务' });
993
- });
994
- });
995
-
996
- // ═══════════════════════════════════════════════════════════════
997
- // Discovery ↔ Handler i18n consistency
998
- // ═══════════════════════════════════════════════════════════════
999
-
1000
- describe('discovery-handler i18n consistency', () => {
1001
- it('should report i18n as available in discovery when service is registered', async () => {
1002
- const mockI18nService = {
1003
- getLocales: vi.fn().mockReturnValue(['en', 'zh-CN', 'ja']),
1004
- getTranslations: vi.fn().mockReturnValue({}),
1005
- getDefaultLocale: vi.fn().mockReturnValue('en'),
1006
- };
1007
-
1008
- (kernel as any).getService = vi.fn().mockImplementation((name: string) => {
1009
- if (name === 'i18n') return mockI18nService;
1010
- return null;
1011
- });
1012
-
1013
- const info = await dispatcher.getDiscoveryInfo('/api/v1');
1014
- expect(info.services.i18n.enabled).toBe(true);
1015
- expect(info.services.i18n.status).toBe('available');
1016
- expect(info.routes.i18n).toBe('/api/v1/i18n');
1017
- expect(info.features.i18n).toBe(true);
1018
- });
1019
-
1020
- it('should report i18n as unavailable in discovery when service is not registered', async () => {
1021
- (kernel as any).getService = vi.fn().mockResolvedValue(null);
1022
- (kernel as any).services = new Map();
1023
-
1024
- const info = await dispatcher.getDiscoveryInfo('/api/v1');
1025
- expect(info.services.i18n.enabled).toBe(false);
1026
- expect(info.services.i18n.status).toBe('unavailable');
1027
- expect(info.routes.i18n).toBeUndefined();
1028
- expect(info.features.i18n).toBe(false);
1029
- });
1030
-
1031
- it('should detect i18n via getServiceAsync (async factory) in discovery', async () => {
1032
- const mockI18nService = {
1033
- getLocales: vi.fn().mockReturnValue(['en', 'fr']),
1034
- getTranslations: vi.fn().mockReturnValue({}),
1035
- getDefaultLocale: vi.fn().mockReturnValue('fr'),
1036
- };
1037
-
1038
- // Service NOT in sync map, only accessible via async factory
1039
- (kernel as any).services = new Map();
1040
- (kernel as any).getServiceAsync = vi.fn().mockImplementation(async (name: string) => {
1041
- if (name === 'i18n') return mockI18nService;
1042
- return null;
1043
- });
1044
-
1045
- const info = await dispatcher.getDiscoveryInfo('/api/v1');
1046
- expect(info.services.i18n.enabled).toBe(true);
1047
- expect(info.services.i18n.status).toBe('available');
1048
-
1049
- // Handler should also find it
1050
- const result = await dispatcher.handleI18n('/locales', 'GET', {}, { request: {} });
1051
- expect(result.handled).toBe(true);
1052
- expect(result.response?.status).toBe(200);
1053
- expect(result.response?.body?.data?.locales).toEqual(['en', 'fr']);
1054
- });
1055
-
1056
- it('should populate locale from actual i18n service', async () => {
1057
- const mockI18nService = {
1058
- getLocales: vi.fn().mockReturnValue(['en', 'zh-CN', 'ja']),
1059
- getTranslations: vi.fn().mockReturnValue({}),
1060
- getDefaultLocale: vi.fn().mockReturnValue('zh-CN'),
1061
- };
1062
-
1063
- (kernel as any).getService = vi.fn().mockImplementation((name: string) => {
1064
- if (name === 'i18n') return mockI18nService;
1065
- return null;
1066
- });
1067
-
1068
- const info = await dispatcher.getDiscoveryInfo('/api/v1');
1069
- expect(info.locale.default).toBe('zh-CN');
1070
- expect(info.locale.supported).toEqual(['en', 'zh-CN', 'ja']);
1071
- });
1072
-
1073
- it('should use default locale when i18n service is not available', async () => {
1074
- (kernel as any).getService = vi.fn().mockResolvedValue(null);
1075
- (kernel as any).services = new Map();
1076
-
1077
- const info = await dispatcher.getDiscoveryInfo('/api/v1');
1078
- expect(info.locale.default).toBe('en');
1079
- expect(info.locale.supported).toEqual(['en']);
1080
- expect(info.locale.timezone).toBe('UTC');
1081
- });
1082
-
1083
- it('should ensure discovery and dispatch are consistent for root path', async () => {
1084
- const mockI18nService = {
1085
- getLocales: vi.fn().mockReturnValue(['en']),
1086
- getTranslations: vi.fn().mockReturnValue({}),
1087
- getDefaultLocale: vi.fn().mockReturnValue('en'),
1088
- };
1089
-
1090
- (kernel as any).getService = vi.fn().mockImplementation((name: string) => {
1091
- if (name === 'i18n') return mockI18nService;
1092
- return null;
1093
- });
1094
-
1095
- // Dispatch to root should return the same discovery data
1096
- const result = await dispatcher.dispatch('GET', '', undefined, {}, { request: {} });
1097
- expect(result.handled).toBe(true);
1098
- const data = result.response?.body?.data;
1099
- expect(data.services.i18n.enabled).toBe(true);
1100
- expect(data.locale.default).toBe('en');
1101
- });
1102
- });
1103
-
1104
- // ═══════════════════════════════════════════════════════════════
1105
- // i18n across server/dev/mock environments
1106
- // ═══════════════════════════════════════════════════════════════
1107
-
1108
- describe('i18n environment consistency', () => {
1109
- it('should work with dev stub i18n service (in-memory translations)', async () => {
1110
- // Simulate dev plugin i18n stub — Map-backed, all sync
1111
- const translations = new Map<string, Record<string, unknown>>();
1112
- let defaultLocale = 'en';
1113
- const devI18nStub = {
1114
- t: (key: string, locale: string) => {
1115
- const t = translations.get(locale);
1116
- return (t?.[key] as string) ?? key;
1117
- },
1118
- getTranslations: (locale: string) => translations.get(locale) ?? {},
1119
- loadTranslations: (locale: string, data: Record<string, unknown>) => {
1120
- translations.set(locale, { ...translations.get(locale), ...data });
1121
- },
1122
- getLocales: () => [...translations.keys()],
1123
- getDefaultLocale: () => defaultLocale,
1124
- setDefaultLocale: (locale: string) => { defaultLocale = locale; },
1125
- };
1126
-
1127
- // Load data like AppPlugin would
1128
- devI18nStub.loadTranslations('en', { 'o.task.label': 'Task' });
1129
- devI18nStub.loadTranslations('zh-CN', { 'o.task.label': '任务' });
1130
-
1131
- (kernel as any).getService = vi.fn().mockImplementation((name: string) => {
1132
- if (name === 'i18n') return devI18nStub;
1133
- return null;
1134
- });
1135
-
1136
- // Discovery should reflect loaded locales
1137
- const info = await dispatcher.getDiscoveryInfo('/api/v1');
1138
- expect(info.services.i18n.enabled).toBe(true);
1139
- expect(info.locale.supported).toEqual(['en', 'zh-CN']);
1140
-
1141
- // Handler should serve translations
1142
- const result = await dispatcher.handleI18n('/translations/zh-CN', 'GET', {}, { request: {} });
1143
- expect(result.response?.status).toBe(200);
1144
- expect(result.response?.body?.data?.translations['o.task.label']).toBe('任务');
1145
- });
1146
-
1147
- it('should handle MSW catch-all dispatch pattern for i18n', async () => {
1148
- // MSW routes all requests through dispatcher.dispatch()
1149
- const mockI18nService = {
1150
- getLocales: vi.fn().mockReturnValue(['en', 'de']),
1151
- getTranslations: vi.fn().mockReturnValue({ 'o.account.label': 'Konto' }),
1152
- getDefaultLocale: vi.fn().mockReturnValue('de'),
1153
- };
1154
-
1155
- (kernel as any).getService = vi.fn().mockImplementation((name: string) => {
1156
- if (name === 'i18n') return mockI18nService;
1157
- return null;
1158
- });
1159
-
1160
- // MSW-style dispatch: full path stripped to relative
1161
- const localesResult = await dispatcher.dispatch('GET', '/i18n/locales', undefined, {}, { request: {} });
1162
- expect(localesResult.handled).toBe(true);
1163
- expect(localesResult.response?.body?.data?.locales).toEqual(['en', 'de']);
1164
-
1165
- const translationsResult = await dispatcher.dispatch('GET', '/i18n/translations/de', undefined, {}, { request: {} });
1166
- expect(translationsResult.handled).toBe(true);
1167
- expect(translationsResult.response?.body?.data?.translations['o.account.label']).toBe('Konto');
1168
-
1169
- // Discovery and handler agree
1170
- const discovery = await dispatcher.getDiscoveryInfo('/api/v1');
1171
- expect(discovery.services.i18n.enabled).toBe(true);
1172
- expect(discovery.locale.default).toBe('de');
1173
- });
1174
-
1175
- it('should return 501 consistently when i18n is unavailable in both discovery and handler', async () => {
1176
- (kernel as any).getService = vi.fn().mockResolvedValue(null);
1177
- (kernel as any).services = new Map();
1178
-
1179
- // Discovery: unavailable
1180
- const info = await dispatcher.getDiscoveryInfo('/api/v1');
1181
- expect(info.services.i18n.enabled).toBe(false);
1182
- expect(info.services.i18n.status).toBe('unavailable');
1183
-
1184
- // Handler: 501
1185
- const result = await dispatcher.handleI18n('/locales', 'GET', {}, { request: {} });
1186
- expect(result.response?.status).toBe(501);
1187
-
1188
- // Dispatch: also 501
1189
- const dispatchResult = await dispatcher.dispatch('GET', '/i18n/locales', undefined, {}, { request: {} });
1190
- expect(dispatchResult.response?.status).toBe(501);
1191
- });
1192
-
1193
- it('should handle context-based service resolution (mock kernel)', async () => {
1194
- // Simulate a kernel that only provides i18n through context.getService
1195
- const mockI18n = {
1196
- getLocales: vi.fn().mockReturnValue(['en']),
1197
- getTranslations: vi.fn().mockReturnValue({}),
1198
- getDefaultLocale: vi.fn().mockReturnValue('en'),
1199
- };
1200
-
1201
- (kernel as any).services = new Map();
1202
- (kernel as any).getService = undefined;
1203
- (kernel as any).getServiceAsync = undefined;
1204
- (kernel as any).context = {
1205
- getService: vi.fn().mockImplementation((name: string) => {
1206
- if (name === 'i18n') return mockI18n;
1207
- return null;
1208
- }),
1209
- };
1210
-
1211
- const info = await dispatcher.getDiscoveryInfo('/api/v1');
1212
- expect(info.services.i18n.enabled).toBe(true);
1213
-
1214
- const result = await dispatcher.handleI18n('/locales', 'GET', {}, { request: {} });
1215
- expect(result.response?.status).toBe(200);
1216
- });
1217
- });
1218
-
1219
- describe('handleMetadata with minimal kernel (serverless/lightweight)', () => {
1220
- let minimalKernel: any;
1221
- let minimalDispatcher: HttpDispatcher;
1222
-
1223
- beforeEach(() => {
1224
- // Minimal kernel — simulates a lightweight/serverless setup
1225
- // where only the protocol service and/or ObjectQL registry are available.
1226
- minimalKernel = {
1227
- context: {
1228
- getService: vi.fn().mockReturnValue(null),
1229
- },
1230
- };
1231
- minimalDispatcher = new HttpDispatcher(minimalKernel);
1232
- });
1233
-
1234
- it('GET /meta should return default types with minimal kernel', async () => {
1235
- const context = { request: {} };
1236
- const result = await minimalDispatcher.handleMetadata('', context, 'GET');
1237
- expect(result.handled).toBe(true);
1238
- expect(result.response?.status).toBe(200);
1239
- expect(result.response?.body?.data?.types).toContain('object');
1240
- });
1241
-
1242
- it('GET /meta/types should return default types with minimal kernel', async () => {
1243
- const context = { request: {} };
1244
- const result = await minimalDispatcher.handleMetadata('/types', context, 'GET');
1245
- expect(result.handled).toBe(true);
1246
- expect(result.response?.status).toBe(200);
1247
- expect(result.response?.body?.data?.types).toContain('object');
1248
- });
1249
-
1250
- it('GET /meta/objects should use ObjectQL registry', async () => {
1251
- const mockRegistry = {
1252
- getAllObjects: vi.fn().mockReturnValue([{ name: 'account' }]),
1253
- getObject: vi.fn(),
1254
- };
1255
- minimalKernel.context.getService = vi.fn().mockImplementation((name: string) => {
1256
- if (name === 'objectql') return { registry: mockRegistry };
1257
- return null;
1258
- });
1259
-
1260
- const context = { request: {} };
1261
- const result = await minimalDispatcher.handleMetadata('/objects', context, 'GET');
1262
- expect(result.handled).toBe(true);
1263
- expect(result.response?.status).toBe(200);
1264
- expect(mockRegistry.getAllObjects).toHaveBeenCalled();
1265
- });
1266
-
1267
- it('GET /meta/objects/:name should use ObjectQL registry', async () => {
1268
- const mockRegistry = {
1269
- registry: {
1270
- getObject: vi.fn().mockReturnValue({ name: 'account', fields: {} }),
1271
- },
1272
- };
1273
- minimalKernel.context.getService = vi.fn().mockImplementation((name: string) => {
1274
- if (name === 'objectql') return mockRegistry;
1275
- return null;
1276
- });
1277
-
1278
- const context = { request: {} };
1279
- const result = await minimalDispatcher.handleMetadata('/objects/account', context, 'GET');
1280
- expect(result.handled).toBe(true);
1281
- expect(result.response?.status).toBe(200);
1282
- expect(mockRegistry.registry.getObject).toHaveBeenCalledWith('account');
1283
- });
1284
-
1285
- it('GET /meta/:type/:name/published should return 404 when metadata service is unavailable', async () => {
1286
- const context = { request: {} };
1287
- const result = await minimalDispatcher.handleMetadata('/object/my_obj/published', context, 'GET');
1288
- expect(result.handled).toBe(true);
1289
- expect(result.response?.status).toBe(404);
1290
- });
1291
-
1292
- it('PUT /meta/:type/:name should return 501 when protocol is unavailable', async () => {
1293
- const context = { request: {} };
1294
- const body = { label: 'Test' };
1295
- const result = await minimalDispatcher.handleMetadata('/objects/my_obj', context, 'PUT', body);
1296
- expect(result.handled).toBe(true);
1297
- expect(result.response?.status).toBe(501);
1298
- });
1299
-
1300
- it('should use protocol service with minimal kernel', async () => {
1301
- const mockProtocolLocal = {
1302
- getMetaTypes: vi.fn().mockResolvedValue({ types: ['custom_type'] }),
1303
- };
1304
- minimalKernel.context.getService = vi.fn().mockImplementation((name: string) => {
1305
- if (name === 'protocol') return mockProtocolLocal;
1306
- return null;
1307
- });
1308
-
1309
- const context = { request: {} };
1310
- const result = await minimalDispatcher.handleMetadata('/types', context, 'GET');
1311
- expect(result.handled).toBe(true);
1312
- expect(result.response?.status).toBe(200);
1313
- expect(mockProtocolLocal.getMetaTypes).toHaveBeenCalled();
1314
- expect(result.response?.body?.data?.types).toContain('custom_type');
1315
- });
1316
- });
1317
- });