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