-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp-plugin.test.ts
More file actions
274 lines (228 loc) · 10.3 KB
/
app-plugin.test.ts
File metadata and controls
274 lines (228 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { AppPlugin } from './app-plugin';
import { PluginContext } from '@objectstack/core';
describe('AppPlugin', () => {
let mockContext: PluginContext;
beforeEach(() => {
mockContext = {
logger: {
info: vi.fn(),
error: vi.fn(),
warn: vi.fn(),
debug: vi.fn()
},
registerService: vi.fn(),
getService: vi.fn(),
getServices: vi.fn()
} as unknown as PluginContext;
});
it('should initialize with manifest info', () => {
const bundle = {
id: 'com.test.app',
name: 'Test App',
version: '1.0.0'
};
const plugin = new AppPlugin(bundle);
expect(plugin.name).toBe('plugin.app.com.test.app');
expect(plugin.version).toBe('1.0.0');
});
it('should handle nested stack definition manifest', () => {
const bundle = {
manifest: {
id: 'com.test.stack',
version: '2.0.0'
},
objects: []
};
const plugin = new AppPlugin(bundle);
expect(plugin.name).toBe('plugin.app.com.test.stack');
expect(plugin.version).toBe('2.0.0');
});
it('registerService should register raw manifest in init phase', async () => {
const bundle = {
id: 'com.test.simple',
objects: []
};
const plugin = new AppPlugin(bundle);
// Mock the manifest service
const mockManifestService = { register: vi.fn() };
vi.mocked(mockContext.getService).mockReturnValue(mockManifestService);
await plugin.init(mockContext);
expect(mockContext.getService).toHaveBeenCalledWith('manifest');
expect(mockManifestService.register).toHaveBeenCalledWith(bundle);
});
it('start should do nothing if no runtime hooks', async () => {
const bundle = { id: 'com.test.static' };
const plugin = new AppPlugin(bundle);
vi.mocked(mockContext.getService).mockReturnValue({}); // Mock ObjectQL exists
await plugin.start!(mockContext);
// Only logs, no errors
expect(mockContext.logger.debug).toHaveBeenCalled();
});
it('start should invoke onEnable if present', async () => {
const onEnableSpy = vi.fn();
const bundle = {
id: 'com.test.code',
onEnable: onEnableSpy
};
const plugin = new AppPlugin(bundle);
// Mock ObjectQL engine
const mockQL = { registry: {} };
vi.mocked(mockContext.getService).mockReturnValue(mockQL);
await plugin.start!(mockContext);
expect(onEnableSpy).toHaveBeenCalled();
// Check context passed to onEnable
const callArg = onEnableSpy.mock.calls[0][0];
expect(callArg.ql).toBe(mockQL);
});
it('start should warn if objectql not found', async () => {
const bundle = { id: 'com.test.warn' };
const plugin = new AppPlugin(bundle);
vi.mocked(mockContext.getService).mockReturnValue(undefined); // No ObjectQL
await plugin.start!(mockContext);
expect(mockContext.logger.warn).toHaveBeenCalledWith(
expect.stringContaining('ObjectQL engine service not found'),
expect.any(Object)
);
});
it('start should handle getService throwing for objectql', async () => {
const bundle = { id: 'com.test.throw' };
const plugin = new AppPlugin(bundle);
vi.mocked(mockContext.getService).mockImplementation(() => {
throw new Error("[Kernel] Service 'objectql' not found");
});
await plugin.start!(mockContext);
expect(mockContext.logger.warn).toHaveBeenCalledWith(
expect.stringContaining('ObjectQL engine service not found'),
expect.any(Object)
);
});
// ═══════════════════════════════════════════════════════════════
// i18n translation auto-loading
// ═══════════════════════════════════════════════════════════════
describe('i18n translation loading', () => {
let mockI18n: any;
let mockQL: any;
beforeEach(() => {
mockI18n = {
loadTranslations: vi.fn(),
setDefaultLocale: vi.fn(),
getLocales: vi.fn().mockReturnValue([]),
getDefaultLocale: vi.fn().mockReturnValue('en'),
};
mockQL = { registry: {} };
vi.mocked(mockContext.getService).mockImplementation((name: string) => {
if (name === 'objectql') return mockQL;
if (name === 'i18n') return mockI18n;
return undefined;
});
});
it('should auto-load translations from bundle into i18n service', async () => {
const bundle = {
id: 'com.test.i18n',
translations: [
{
en: { objects: { task: { label: 'Task' } } },
'zh-CN': { objects: { task: { label: '任务' } } },
},
],
};
const plugin = new AppPlugin(bundle);
await plugin.start!(mockContext);
expect(mockI18n.loadTranslations).toHaveBeenCalledWith('en', { objects: { task: { label: 'Task' } } });
expect(mockI18n.loadTranslations).toHaveBeenCalledWith('zh-CN', { objects: { task: { label: '任务' } } });
});
it('should set default locale from i18n config', async () => {
const bundle = {
id: 'com.test.locale',
i18n: { defaultLocale: 'zh-CN', supportedLocales: ['en', 'zh-CN'] },
translations: [{ en: { messages: { hello: 'Hello' } } }],
};
const plugin = new AppPlugin(bundle);
await plugin.start!(mockContext);
expect(mockI18n.setDefaultLocale).toHaveBeenCalledWith('zh-CN');
});
it('should skip translation loading when i18n service is not registered', async () => {
vi.mocked(mockContext.getService).mockImplementation((name: string) => {
if (name === 'objectql') return mockQL;
return undefined; // No i18n service
});
const bundle = {
id: 'com.test.noi18n',
translations: [{ en: { messages: { hello: 'Hello' } } }],
};
const plugin = new AppPlugin(bundle);
await plugin.start!(mockContext);
// Should log warning (translations exist but no i18n service) and not throw
expect(mockContext.logger.warn).toHaveBeenCalledWith(
expect.stringContaining('no i18n service is registered')
);
});
it('should skip translation loading when getService throws for i18n', async () => {
vi.mocked(mockContext.getService).mockImplementation((name: string) => {
if (name === 'objectql') return mockQL;
throw new Error("[Kernel] Service 'i18n' not found");
});
const bundle = {
id: 'com.test.i18nthrow',
translations: [{ en: { messages: { hello: 'Hello' } } }],
};
const plugin = new AppPlugin(bundle);
await plugin.start!(mockContext);
// Should log warning (translations exist but no i18n service) and not throw
expect(mockContext.logger.warn).toHaveBeenCalledWith(
expect.stringContaining('no i18n service is registered')
);
});
it('should handle bundle with no translations gracefully', async () => {
const bundle = { id: 'com.test.notrans' };
const plugin = new AppPlugin(bundle);
await plugin.start!(mockContext);
expect(mockI18n.loadTranslations).not.toHaveBeenCalled();
});
it('should load translations from nested manifest.translations', async () => {
const bundle = {
manifest: {
id: 'com.test.nested',
translations: [
{ en: { messages: { save: 'Save' } } },
],
},
};
const plugin = new AppPlugin(bundle);
await plugin.start!(mockContext);
expect(mockI18n.loadTranslations).toHaveBeenCalledWith('en', { messages: { save: 'Save' } });
});
it('should load multiple translation bundles', async () => {
const bundle = {
id: 'com.test.multi',
translations: [
{ en: { objects: { task: { label: 'Task' } } } },
{ en: { objects: { contact: { label: 'Contact' } } }, 'ja-JP': { objects: { contact: { label: '連絡先' } } } },
],
};
const plugin = new AppPlugin(bundle);
await plugin.start!(mockContext);
expect(mockI18n.loadTranslations).toHaveBeenCalledTimes(3);
});
it('should handle errors in loadTranslations gracefully', async () => {
mockI18n.loadTranslations.mockImplementation((locale: string) => {
if (locale === 'zh-CN') throw new Error('Disk read failed');
});
const bundle = {
id: 'com.test.error',
translations: [
{ en: { messages: { save: 'Save' } }, 'zh-CN': { messages: { save: '保存' } } },
],
};
const plugin = new AppPlugin(bundle);
await plugin.start!(mockContext);
// en should still be loaded despite zh-CN failure
expect(mockI18n.loadTranslations).toHaveBeenCalledWith('en', { messages: { save: 'Save' } });
expect(mockContext.logger.warn).toHaveBeenCalledWith(
expect.stringContaining('Failed to load translations'),
expect.objectContaining({ locale: 'zh-CN' })
);
});
});
});