@object-ui/core 0.3.1 → 2.0.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.
Files changed (118) hide show
  1. package/.turbo/turbo-build.log +4 -0
  2. package/CHANGELOG.md +11 -0
  3. package/dist/actions/ActionRunner.d.ts +228 -4
  4. package/dist/actions/ActionRunner.js +397 -45
  5. package/dist/actions/TransactionManager.d.ts +193 -0
  6. package/dist/actions/TransactionManager.js +410 -0
  7. package/dist/actions/index.d.ts +2 -1
  8. package/dist/actions/index.js +2 -1
  9. package/dist/adapters/ApiDataSource.d.ts +69 -0
  10. package/dist/adapters/ApiDataSource.js +293 -0
  11. package/dist/adapters/ValueDataSource.d.ts +55 -0
  12. package/dist/adapters/ValueDataSource.js +287 -0
  13. package/dist/adapters/index.d.ts +3 -0
  14. package/dist/adapters/index.js +5 -2
  15. package/dist/adapters/resolveDataSource.d.ts +40 -0
  16. package/dist/adapters/resolveDataSource.js +59 -0
  17. package/dist/data-scope/DataScopeManager.d.ts +127 -0
  18. package/dist/data-scope/DataScopeManager.js +229 -0
  19. package/dist/data-scope/index.d.ts +10 -0
  20. package/dist/data-scope/index.js +10 -0
  21. package/dist/evaluator/ExpressionCache.d.ts +101 -0
  22. package/dist/evaluator/ExpressionCache.js +135 -0
  23. package/dist/evaluator/ExpressionEvaluator.d.ts +30 -2
  24. package/dist/evaluator/ExpressionEvaluator.js +60 -16
  25. package/dist/evaluator/FormulaFunctions.d.ts +58 -0
  26. package/dist/evaluator/FormulaFunctions.js +350 -0
  27. package/dist/evaluator/index.d.ts +4 -2
  28. package/dist/evaluator/index.js +4 -2
  29. package/dist/index.d.ts +14 -7
  30. package/dist/index.js +13 -9
  31. package/dist/query/index.d.ts +6 -0
  32. package/dist/query/index.js +6 -0
  33. package/dist/query/query-ast.d.ts +32 -0
  34. package/dist/query/query-ast.js +268 -0
  35. package/dist/registry/PluginScopeImpl.d.ts +80 -0
  36. package/dist/registry/PluginScopeImpl.js +243 -0
  37. package/dist/registry/PluginSystem.d.ts +66 -0
  38. package/dist/registry/PluginSystem.js +142 -0
  39. package/dist/registry/Registry.d.ts +83 -4
  40. package/dist/registry/Registry.js +113 -7
  41. package/dist/registry/WidgetRegistry.d.ts +120 -0
  42. package/dist/registry/WidgetRegistry.js +275 -0
  43. package/dist/theme/ThemeEngine.d.ts +82 -0
  44. package/dist/theme/ThemeEngine.js +400 -0
  45. package/dist/theme/index.d.ts +8 -0
  46. package/dist/theme/index.js +8 -0
  47. package/dist/validation/index.d.ts +9 -0
  48. package/dist/validation/index.js +9 -0
  49. package/dist/validation/validation-engine.d.ts +88 -0
  50. package/dist/validation/validation-engine.js +428 -0
  51. package/dist/validation/validators/index.d.ts +16 -0
  52. package/dist/validation/validators/index.js +16 -0
  53. package/dist/validation/validators/object-validation-engine.d.ts +118 -0
  54. package/dist/validation/validators/object-validation-engine.js +538 -0
  55. package/package.json +14 -5
  56. package/src/actions/ActionRunner.ts +577 -55
  57. package/src/actions/TransactionManager.ts +521 -0
  58. package/src/actions/__tests__/ActionRunner.params.test.ts +134 -0
  59. package/src/actions/__tests__/ActionRunner.test.ts +711 -0
  60. package/src/actions/__tests__/TransactionManager.test.ts +447 -0
  61. package/src/actions/index.ts +2 -1
  62. package/src/adapters/ApiDataSource.ts +349 -0
  63. package/src/adapters/ValueDataSource.ts +332 -0
  64. package/src/adapters/__tests__/ApiDataSource.test.ts +418 -0
  65. package/src/adapters/__tests__/ValueDataSource.test.ts +325 -0
  66. package/src/adapters/__tests__/resolveDataSource.test.ts +144 -0
  67. package/src/adapters/index.ts +6 -1
  68. package/src/adapters/resolveDataSource.ts +79 -0
  69. package/src/builder/__tests__/schema-builder.test.ts +235 -0
  70. package/src/data-scope/DataScopeManager.ts +269 -0
  71. package/src/data-scope/__tests__/DataScopeManager.test.ts +211 -0
  72. package/src/data-scope/index.ts +16 -0
  73. package/src/evaluator/ExpressionCache.ts +192 -0
  74. package/src/evaluator/ExpressionEvaluator.ts +61 -16
  75. package/src/evaluator/FormulaFunctions.ts +398 -0
  76. package/src/evaluator/__tests__/ExpressionCache.test.ts +135 -0
  77. package/src/evaluator/__tests__/ExpressionContext.test.ts +110 -0
  78. package/src/evaluator/__tests__/FormulaFunctions.test.ts +447 -0
  79. package/src/evaluator/index.ts +4 -2
  80. package/src/index.ts +14 -10
  81. package/src/query/__tests__/query-ast.test.ts +211 -0
  82. package/src/query/__tests__/window-functions.test.ts +275 -0
  83. package/src/query/index.ts +7 -0
  84. package/src/query/query-ast.ts +341 -0
  85. package/src/registry/PluginScopeImpl.ts +259 -0
  86. package/src/registry/PluginSystem.ts +161 -0
  87. package/src/registry/Registry.ts +136 -8
  88. package/src/registry/WidgetRegistry.ts +316 -0
  89. package/src/registry/__tests__/PluginSystem.test.ts +226 -0
  90. package/src/registry/__tests__/Registry.test.ts +293 -0
  91. package/src/registry/__tests__/WidgetRegistry.test.ts +321 -0
  92. package/src/registry/__tests__/plugin-scope-integration.test.ts +283 -0
  93. package/src/theme/ThemeEngine.ts +452 -0
  94. package/src/theme/__tests__/ThemeEngine.test.ts +606 -0
  95. package/src/theme/index.ts +22 -0
  96. package/src/validation/__tests__/object-validation-engine.test.ts +567 -0
  97. package/src/validation/__tests__/schema-validator.test.ts +118 -0
  98. package/src/validation/__tests__/validation-engine.test.ts +102 -0
  99. package/src/validation/index.ts +10 -0
  100. package/src/validation/validation-engine.ts +520 -0
  101. package/src/validation/validators/index.ts +25 -0
  102. package/src/validation/validators/object-validation-engine.ts +722 -0
  103. package/tsconfig.tsbuildinfo +1 -1
  104. package/vitest.config.ts +2 -0
  105. package/src/adapters/index.d.ts +0 -8
  106. package/src/adapters/index.js +0 -10
  107. package/src/builder/schema-builder.d.ts +0 -294
  108. package/src/builder/schema-builder.js +0 -503
  109. package/src/index.d.ts +0 -13
  110. package/src/index.js +0 -16
  111. package/src/registry/Registry.d.ts +0 -56
  112. package/src/registry/Registry.js +0 -43
  113. package/src/types/index.d.ts +0 -19
  114. package/src/types/index.js +0 -8
  115. package/src/utils/filter-converter.d.ts +0 -57
  116. package/src/utils/filter-converter.js +0 -100
  117. package/src/validation/schema-validator.d.ts +0 -94
  118. package/src/validation/schema-validator.js +0 -278
@@ -0,0 +1,293 @@
1
+ /**
2
+ * ObjectUI
3
+ * Copyright (c) 2024-present ObjectStack Inc.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
10
+ import { Registry } from '../Registry';
11
+
12
+ describe('Registry', () => {
13
+ let registry: Registry;
14
+ let consoleWarnSpy: any;
15
+
16
+ beforeEach(() => {
17
+ registry = new Registry();
18
+ consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
19
+ });
20
+
21
+ afterEach(() => {
22
+ consoleWarnSpy.mockRestore();
23
+ });
24
+
25
+ describe('Basic Registration', () => {
26
+ it('should register a component without namespace', () => {
27
+ const component = () => 'test';
28
+ registry.register('button', component);
29
+
30
+ expect(registry.has('button')).toBe(true);
31
+ expect(registry.get('button')).toBe(component);
32
+ });
33
+
34
+ it('should warn when registering without namespace', () => {
35
+ const component = () => 'test';
36
+ registry.register('button', component);
37
+
38
+ expect(consoleWarnSpy).toHaveBeenCalledWith(
39
+ expect.stringContaining('Registering component "button" without a namespace is deprecated')
40
+ );
41
+ });
42
+
43
+ it('should register a component with namespace', () => {
44
+ const component = () => 'test';
45
+ registry.register('button', component, { namespace: 'ui' });
46
+
47
+ expect(registry.has('button', 'ui')).toBe(true);
48
+ expect(registry.get('button', 'ui')).toBe(component);
49
+ });
50
+
51
+ it('should not warn when registering with namespace', () => {
52
+ const component = () => 'test';
53
+ registry.register('button', component, { namespace: 'ui' });
54
+
55
+ expect(consoleWarnSpy).not.toHaveBeenCalled();
56
+ });
57
+ });
58
+
59
+ describe('Namespaced Registration', () => {
60
+ it('should register components with the same name in different namespaces', () => {
61
+ const gridComponent1 = () => 'grid1';
62
+ const gridComponent2 = () => 'grid2';
63
+
64
+ registry.register('grid', gridComponent1, { namespace: 'plugin-grid' });
65
+ registry.register('grid', gridComponent2, { namespace: 'plugin-aggrid' });
66
+
67
+ expect(registry.get('grid', 'plugin-grid')).toBe(gridComponent1);
68
+ expect(registry.get('grid', 'plugin-aggrid')).toBe(gridComponent2);
69
+ });
70
+
71
+ it('should store full type as namespace:type', () => {
72
+ const component = () => 'test';
73
+ registry.register('button', component, { namespace: 'ui' });
74
+
75
+ const config = registry.getConfig('button', 'ui');
76
+ expect(config?.type).toBe('ui:button');
77
+ });
78
+
79
+ it('should preserve namespace in component config', () => {
80
+ const component = () => 'test';
81
+ registry.register('button', component, {
82
+ namespace: 'ui',
83
+ label: 'Button',
84
+ category: 'form'
85
+ });
86
+
87
+ const config = registry.getConfig('button', 'ui');
88
+ expect(config?.namespace).toBe('ui');
89
+ expect(config?.label).toBe('Button');
90
+ expect(config?.category).toBe('form');
91
+ });
92
+ });
93
+
94
+ describe('Namespace Lookup with Fallback', () => {
95
+ it('should not fallback when namespace is explicitly specified', () => {
96
+ const component = () => 'test';
97
+ registry.register('button', component);
98
+
99
+ // When no namespace is specified, should find it
100
+ expect(registry.get('button')).toBe(component);
101
+
102
+ // When namespace is specified but component isn't in that namespace, should return undefined
103
+ expect(registry.get('button', 'ui')).toBeUndefined();
104
+ });
105
+
106
+ it('should prefer namespaced component over non-namespaced', () => {
107
+ const component1 = () => 'non-namespaced';
108
+ const component2 = () => 'namespaced';
109
+
110
+ registry.register('button', component1);
111
+ registry.register('button', component2, { namespace: 'ui' });
112
+
113
+ // When searching with namespace, should get namespaced version
114
+ expect(registry.get('button', 'ui')).toBe(component2);
115
+
116
+ // When searching without namespace, should get the latest registered (namespaced one due to backward compatibility)
117
+ expect(registry.get('button')).toBe(component2);
118
+ });
119
+
120
+ it('should return undefined when component not found in any namespace', () => {
121
+ expect(registry.get('nonexistent', 'ui')).toBeUndefined();
122
+ expect(registry.get('nonexistent')).toBeUndefined();
123
+ });
124
+ });
125
+
126
+ describe('has() method', () => {
127
+ it('should check existence with namespace', () => {
128
+ const component = () => 'test';
129
+ registry.register('button', component, { namespace: 'ui' });
130
+
131
+ expect(registry.has('button', 'ui')).toBe(true);
132
+ // Due to backward compatibility, non-namespaced lookup also works
133
+ expect(registry.has('button')).toBe(true);
134
+ // Other namespaces should return false
135
+ expect(registry.has('button', 'other')).toBe(false);
136
+ });
137
+
138
+ it('should fallback to non-namespaced check only when no namespace provided', () => {
139
+ const component = () => 'test';
140
+ registry.register('button', component);
141
+
142
+ expect(registry.has('button')).toBe(true);
143
+ // When namespace is explicitly requested, should not find non-namespaced component
144
+ expect(registry.has('button', 'ui')).toBe(false);
145
+ });
146
+ });
147
+
148
+ describe('getConfig() method', () => {
149
+ it('should get config with namespace', () => {
150
+ const component = () => 'test';
151
+ registry.register('button', component, {
152
+ namespace: 'ui',
153
+ label: 'Button'
154
+ });
155
+
156
+ const config = registry.getConfig('button', 'ui');
157
+ expect(config).toBeDefined();
158
+ expect(config?.component).toBe(component);
159
+ expect(config?.label).toBe('Button');
160
+ });
161
+
162
+ it('should not fallback when namespace is explicitly provided', () => {
163
+ const component = () => 'test';
164
+ registry.register('button', component, { label: 'Button' });
165
+
166
+ // When no namespace is provided, should find it
167
+ const config1 = registry.getConfig('button');
168
+ expect(config1).toBeDefined();
169
+
170
+ // When namespace is provided but component isn't in that namespace, should return undefined
171
+ const config2 = registry.getConfig('button', 'ui');
172
+ expect(config2).toBeUndefined();
173
+ });
174
+ });
175
+
176
+ describe('getAllTypes() and getAllConfigs()', () => {
177
+ it('should return all registered types including namespaced ones', () => {
178
+ registry.register('button', () => 'b1');
179
+ registry.register('input', () => 'i1', { namespace: 'ui' });
180
+ registry.register('grid', () => 'g1', { namespace: 'plugin-grid' });
181
+
182
+ const types = registry.getAllTypes();
183
+ // Due to backward compatibility, namespaced components are stored under both keys
184
+ expect(types).toContain('button');
185
+ expect(types).toContain('ui:input');
186
+ expect(types).toContain('input'); // backward compat
187
+ expect(types).toContain('plugin-grid:grid');
188
+ expect(types).toContain('grid'); // backward compat
189
+ });
190
+
191
+ it('should return all configs', () => {
192
+ registry.register('button', () => 'b1', { label: 'Button' });
193
+ registry.register('input', () => 'i1', {
194
+ namespace: 'ui',
195
+ label: 'Input'
196
+ });
197
+
198
+ const configs = registry.getAllConfigs();
199
+ // Due to backward compatibility, namespaced components are stored twice
200
+ expect(configs.length).toBeGreaterThanOrEqual(2);
201
+ expect(configs.map(c => c.type)).toContain('button');
202
+ expect(configs.map(c => c.type)).toContain('ui:input');
203
+ });
204
+ });
205
+
206
+ describe('Conflict Prevention', () => {
207
+ it('should allow same type name in different namespaces', () => {
208
+ const grid1 = () => 'grid-plugin-1';
209
+ const grid2 = () => 'grid-plugin-2';
210
+ const grid3 = () => 'aggrid-plugin';
211
+
212
+ registry.register('grid', grid1, { namespace: 'plugin-grid' });
213
+ registry.register('grid', grid2, { namespace: 'plugin-view' });
214
+ registry.register('grid', grid3, { namespace: 'plugin-aggrid' });
215
+
216
+ expect(registry.get('grid', 'plugin-grid')).toBe(grid1);
217
+ expect(registry.get('grid', 'plugin-view')).toBe(grid2);
218
+ expect(registry.get('grid', 'plugin-aggrid')).toBe(grid3);
219
+ });
220
+
221
+ it('should handle complex namespace names', () => {
222
+ const component = () => 'test';
223
+ registry.register('table', component, { namespace: 'plugin-advanced-grid' });
224
+
225
+ expect(registry.get('table', 'plugin-advanced-grid')).toBe(component);
226
+ expect(registry.getConfig('table', 'plugin-advanced-grid')?.type).toBe('plugin-advanced-grid:table');
227
+ });
228
+ });
229
+
230
+ describe('Backward Compatibility', () => {
231
+ it('should maintain compatibility with existing non-namespaced code', () => {
232
+ const component = () => 'test';
233
+
234
+ // Old-style registration
235
+ registry.register('button', component);
236
+
237
+ // Old-style retrieval should still work
238
+ expect(registry.get('button')).toBe(component);
239
+ expect(registry.has('button')).toBe(true);
240
+ expect(registry.getConfig('button')).toBeDefined();
241
+ });
242
+
243
+ it('should support mixed namespaced and non-namespaced registrations', () => {
244
+ const oldButton = () => 'old';
245
+ const newButton = () => 'new';
246
+
247
+ registry.register('button-old', oldButton);
248
+ registry.register('button-new', newButton, { namespace: 'ui' });
249
+
250
+ expect(registry.get('button-old')).toBe(oldButton);
251
+ expect(registry.get('button-new', 'ui')).toBe(newButton);
252
+ });
253
+
254
+ it('should allow non-namespaced lookup of namespaced components', () => {
255
+ const component = () => 'test';
256
+
257
+ // Register with namespace
258
+ registry.register('button', component, { namespace: 'ui' });
259
+
260
+ // Should be findable both ways for backward compatibility
261
+ expect(registry.get('button')).toBe(component);
262
+ expect(registry.get('button', 'ui')).toBe(component);
263
+
264
+ // The full type should be namespaced
265
+ const config = registry.getConfig('button');
266
+ expect(config?.type).toBe('ui:button');
267
+ });
268
+ });
269
+
270
+ describe('Edge Cases', () => {
271
+ it('should handle empty namespace string', () => {
272
+ const component = () => 'test';
273
+ registry.register('button', component, { namespace: '' });
274
+
275
+ // Empty namespace should be treated as no namespace
276
+ expect(registry.get('button')).toBe(component);
277
+ });
278
+
279
+ it('should handle namespace with special characters', () => {
280
+ const component = () => 'test';
281
+ registry.register('button', component, { namespace: 'plugin-my-custom' });
282
+
283
+ expect(registry.get('button', 'plugin-my-custom')).toBe(component);
284
+ });
285
+
286
+ it('should handle undefined meta', () => {
287
+ const component = () => 'test';
288
+ registry.register('button', component, undefined);
289
+
290
+ expect(registry.get('button')).toBe(component);
291
+ });
292
+ });
293
+ });
@@ -0,0 +1,321 @@
1
+ /**
2
+ * Tests for WidgetRegistry
3
+ */
4
+
5
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
6
+ import { WidgetRegistry } from '../WidgetRegistry';
7
+ import { Registry } from '../Registry';
8
+ import type { WidgetManifest, WidgetRegistryEvent } from '@object-ui/types';
9
+
10
+ function createManifest(overrides: Partial<WidgetManifest> = {}): WidgetManifest {
11
+ return {
12
+ name: 'test-widget',
13
+ version: '1.0.0',
14
+ type: 'test',
15
+ label: 'Test Widget',
16
+ source: { type: 'inline', component: () => null },
17
+ ...overrides,
18
+ };
19
+ }
20
+
21
+ describe('WidgetRegistry', () => {
22
+ let widgetRegistry: WidgetRegistry;
23
+
24
+ beforeEach(() => {
25
+ widgetRegistry = new WidgetRegistry();
26
+ });
27
+
28
+ describe('register / unregister', () => {
29
+ it('registers a widget manifest', () => {
30
+ const manifest = createManifest();
31
+ widgetRegistry.register(manifest);
32
+ expect(widgetRegistry.has('test-widget')).toBe(true);
33
+ expect(widgetRegistry.getManifest('test-widget')).toBe(manifest);
34
+ });
35
+
36
+ it('registers multiple manifests at once', () => {
37
+ widgetRegistry.registerAll([
38
+ createManifest({ name: 'a', type: 'a' }),
39
+ createManifest({ name: 'b', type: 'b' }),
40
+ ]);
41
+ expect(widgetRegistry.has('a')).toBe(true);
42
+ expect(widgetRegistry.has('b')).toBe(true);
43
+ });
44
+
45
+ it('unregisters a widget', () => {
46
+ widgetRegistry.register(createManifest());
47
+ expect(widgetRegistry.unregister('test-widget')).toBe(true);
48
+ expect(widgetRegistry.has('test-widget')).toBe(false);
49
+ });
50
+
51
+ it('returns false when unregistering a non-existent widget', () => {
52
+ expect(widgetRegistry.unregister('nope')).toBe(false);
53
+ });
54
+ });
55
+
56
+ describe('getAllManifests / getByCategory', () => {
57
+ it('returns all manifests', () => {
58
+ widgetRegistry.registerAll([
59
+ createManifest({ name: 'a', type: 'a' }),
60
+ createManifest({ name: 'b', type: 'b' }),
61
+ ]);
62
+ expect(widgetRegistry.getAllManifests()).toHaveLength(2);
63
+ });
64
+
65
+ it('filters by category', () => {
66
+ widgetRegistry.registerAll([
67
+ createManifest({ name: 'chart-1', type: 'c1', category: 'charts' }),
68
+ createManifest({ name: 'form-1', type: 'f1', category: 'forms' }),
69
+ createManifest({ name: 'chart-2', type: 'c2', category: 'charts' }),
70
+ ]);
71
+ expect(widgetRegistry.getByCategory('charts')).toHaveLength(2);
72
+ expect(widgetRegistry.getByCategory('forms')).toHaveLength(1);
73
+ expect(widgetRegistry.getByCategory('unknown')).toHaveLength(0);
74
+ });
75
+ });
76
+
77
+ describe('load', () => {
78
+ it('loads an inline widget', async () => {
79
+ const component = () => null;
80
+ widgetRegistry.register(
81
+ createManifest({ source: { type: 'inline', component } }),
82
+ );
83
+
84
+ const resolved = await widgetRegistry.load('test-widget');
85
+ expect(resolved.component).toBe(component);
86
+ expect(resolved.manifest.name).toBe('test-widget');
87
+ expect(resolved.loadedAt).toBeGreaterThan(0);
88
+ expect(widgetRegistry.isLoaded('test-widget')).toBe(true);
89
+ });
90
+
91
+ it('returns cached resolved widget on subsequent loads', async () => {
92
+ widgetRegistry.register(createManifest());
93
+ const first = await widgetRegistry.load('test-widget');
94
+ const second = await widgetRegistry.load('test-widget');
95
+ expect(first).toBe(second);
96
+ });
97
+
98
+ it('throws when loading an unregistered widget', async () => {
99
+ await expect(widgetRegistry.load('nope')).rejects.toThrow(
100
+ 'Widget "nope" is not registered',
101
+ );
102
+ });
103
+
104
+ it('loads a widget from the component registry', async () => {
105
+ const componentRegistry = new Registry();
106
+ const component = () => null;
107
+ componentRegistry.register('existing-type', component);
108
+
109
+ const reg = new WidgetRegistry({ componentRegistry });
110
+ reg.register(
111
+ createManifest({
112
+ source: { type: 'registry', registryKey: 'existing-type' },
113
+ }),
114
+ );
115
+
116
+ const resolved = await reg.load('test-widget');
117
+ expect(resolved.component).toBe(component);
118
+ });
119
+
120
+ it('throws when registry key is not found', async () => {
121
+ const componentRegistry = new Registry();
122
+ const reg = new WidgetRegistry({ componentRegistry });
123
+ reg.register(
124
+ createManifest({
125
+ source: { type: 'registry', registryKey: 'missing-key' },
126
+ }),
127
+ );
128
+
129
+ await expect(reg.load('test-widget')).rejects.toThrow(
130
+ 'references registry key "missing-key"',
131
+ );
132
+ });
133
+
134
+ it('throws when no component registry is configured for registry source', async () => {
135
+ widgetRegistry.register(
136
+ createManifest({
137
+ source: { type: 'registry', registryKey: 'something' },
138
+ }),
139
+ );
140
+
141
+ await expect(widgetRegistry.load('test-widget')).rejects.toThrow(
142
+ 'no component registry is configured',
143
+ );
144
+ });
145
+
146
+ it('resolves dependencies before loading', async () => {
147
+ const loadOrder: string[] = [];
148
+
149
+ widgetRegistry.register(
150
+ createManifest({
151
+ name: 'dep-a',
152
+ type: 'dep-a',
153
+ source: {
154
+ type: 'inline',
155
+ component: () => {
156
+ loadOrder.push('dep-a');
157
+ return null;
158
+ },
159
+ },
160
+ }),
161
+ );
162
+
163
+ widgetRegistry.register(
164
+ createManifest({
165
+ name: 'main',
166
+ type: 'main',
167
+ dependencies: ['dep-a'],
168
+ source: {
169
+ type: 'inline',
170
+ component: () => {
171
+ loadOrder.push('main');
172
+ return null;
173
+ },
174
+ },
175
+ }),
176
+ );
177
+
178
+ await widgetRegistry.load('main');
179
+ expect(widgetRegistry.isLoaded('dep-a')).toBe(true);
180
+ expect(widgetRegistry.isLoaded('main')).toBe(true);
181
+ });
182
+
183
+ it('syncs loaded widget to component registry', async () => {
184
+ const componentRegistry = new Registry();
185
+ const reg = new WidgetRegistry({ componentRegistry });
186
+ const component = () => null;
187
+
188
+ reg.register(
189
+ createManifest({
190
+ name: 'sync-test',
191
+ type: 'custom-type',
192
+ label: 'Sync Test',
193
+ category: 'testing',
194
+ source: { type: 'inline', component },
195
+ }),
196
+ );
197
+
198
+ await reg.load('sync-test');
199
+
200
+ const synced = componentRegistry.get('custom-type');
201
+ expect(synced).toBe(component);
202
+ });
203
+ });
204
+
205
+ describe('loadAll', () => {
206
+ it('loads all registered widgets', async () => {
207
+ widgetRegistry.registerAll([
208
+ createManifest({ name: 'a', type: 'a' }),
209
+ createManifest({ name: 'b', type: 'b' }),
210
+ ]);
211
+
212
+ const results = await widgetRegistry.loadAll();
213
+ expect(results).toHaveLength(2);
214
+ expect(results.every((r) => !(r.result instanceof Error))).toBe(true);
215
+ });
216
+
217
+ it('captures errors for failed widgets', async () => {
218
+ widgetRegistry.register(createManifest({ name: 'good', type: 'good' }));
219
+ widgetRegistry.register(
220
+ createManifest({
221
+ name: 'bad',
222
+ type: 'bad',
223
+ source: { type: 'registry', registryKey: 'missing' },
224
+ }),
225
+ );
226
+
227
+ const results = await widgetRegistry.loadAll();
228
+ const bad = results.find((r) => r.name === 'bad');
229
+ expect(bad?.result).toBeInstanceOf(Error);
230
+ });
231
+ });
232
+
233
+ describe('events', () => {
234
+ it('emits widget:registered event', () => {
235
+ const events: WidgetRegistryEvent[] = [];
236
+ widgetRegistry.on((e) => events.push(e));
237
+
238
+ widgetRegistry.register(createManifest());
239
+
240
+ expect(events).toHaveLength(1);
241
+ expect(events[0].type).toBe('widget:registered');
242
+ });
243
+
244
+ it('emits widget:unregistered event', () => {
245
+ widgetRegistry.register(createManifest());
246
+
247
+ const events: WidgetRegistryEvent[] = [];
248
+ widgetRegistry.on((e) => events.push(e));
249
+
250
+ widgetRegistry.unregister('test-widget');
251
+
252
+ expect(events).toHaveLength(1);
253
+ expect(events[0].type).toBe('widget:unregistered');
254
+ });
255
+
256
+ it('emits widget:loaded event', async () => {
257
+ widgetRegistry.register(createManifest());
258
+
259
+ const events: WidgetRegistryEvent[] = [];
260
+ widgetRegistry.on((e) => events.push(e));
261
+
262
+ await widgetRegistry.load('test-widget');
263
+
264
+ expect(events.some((e) => e.type === 'widget:loaded')).toBe(true);
265
+ });
266
+
267
+ it('emits widget:error event on load failure', async () => {
268
+ widgetRegistry.register(
269
+ createManifest({
270
+ source: { type: 'registry', registryKey: 'missing' },
271
+ }),
272
+ );
273
+
274
+ const events: WidgetRegistryEvent[] = [];
275
+ widgetRegistry.on((e) => events.push(e));
276
+
277
+ await widgetRegistry.load('test-widget').catch(() => {});
278
+
279
+ expect(events.some((e) => e.type === 'widget:error')).toBe(true);
280
+ });
281
+
282
+ it('supports unsubscribing from events', () => {
283
+ const handler = vi.fn();
284
+ const unsub = widgetRegistry.on(handler);
285
+
286
+ widgetRegistry.register(createManifest({ name: 'a', type: 'a' }));
287
+ expect(handler).toHaveBeenCalledTimes(1);
288
+
289
+ unsub();
290
+ widgetRegistry.register(createManifest({ name: 'b', type: 'b' }));
291
+ expect(handler).toHaveBeenCalledTimes(1); // No new calls
292
+ });
293
+ });
294
+
295
+ describe('clear / getStats', () => {
296
+ it('clears all widgets', async () => {
297
+ widgetRegistry.register(createManifest());
298
+ await widgetRegistry.load('test-widget');
299
+
300
+ widgetRegistry.clear();
301
+
302
+ expect(widgetRegistry.has('test-widget')).toBe(false);
303
+ expect(widgetRegistry.isLoaded('test-widget')).toBe(false);
304
+ });
305
+
306
+ it('returns correct stats', () => {
307
+ widgetRegistry.registerAll([
308
+ createManifest({ name: 'a', type: 'a', category: 'charts' }),
309
+ createManifest({ name: 'b', type: 'b', category: 'forms' }),
310
+ createManifest({ name: 'c', type: 'c', category: 'charts' }),
311
+ ]);
312
+
313
+ const stats = widgetRegistry.getStats();
314
+ expect(stats.registered).toBe(3);
315
+ expect(stats.loaded).toBe(0);
316
+ expect(stats.categories).toContain('charts');
317
+ expect(stats.categories).toContain('forms');
318
+ expect(stats.categories).toHaveLength(2);
319
+ });
320
+ });
321
+ });