@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,606 @@
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 } from 'vitest';
10
+ import type { Theme } from '@object-ui/types';
11
+ import {
12
+ hexToHSL,
13
+ toCSSColor,
14
+ generateColorVars,
15
+ generateTypographyVars,
16
+ generateBorderRadiusVars,
17
+ generateShadowVars,
18
+ generateAnimationVars,
19
+ generateZIndexVars,
20
+ generateThemeVars,
21
+ mergeThemes,
22
+ resolveThemeInheritance,
23
+ resolveMode,
24
+ } from '../ThemeEngine';
25
+
26
+ // ============================================================================
27
+ // Test Fixtures
28
+ // ============================================================================
29
+
30
+ const baseTheme: Theme = {
31
+ name: 'default',
32
+ label: 'Default',
33
+ mode: 'auto',
34
+ colors: {
35
+ primary: '#3B82F6',
36
+ secondary: '#64748B',
37
+ accent: '#F59E0B',
38
+ success: '#10B981',
39
+ warning: '#F59E0B',
40
+ error: '#EF4444',
41
+ info: '#3B82F6',
42
+ background: '#FFFFFF',
43
+ surface: '#F8FAFC',
44
+ text: '#0F172A',
45
+ textSecondary: '#64748B',
46
+ border: '#E2E8F0',
47
+ disabled: '#94A3B8',
48
+ },
49
+ typography: {
50
+ fontFamily: {
51
+ base: 'Inter, sans-serif',
52
+ heading: 'Inter, sans-serif',
53
+ mono: 'JetBrains Mono, monospace',
54
+ },
55
+ fontSize: {
56
+ xs: '0.75rem',
57
+ sm: '0.875rem',
58
+ base: '1rem',
59
+ lg: '1.125rem',
60
+ xl: '1.25rem',
61
+ '2xl': '1.5rem',
62
+ },
63
+ fontWeight: {
64
+ light: 300,
65
+ normal: 400,
66
+ medium: 500,
67
+ semibold: 600,
68
+ bold: 700,
69
+ },
70
+ lineHeight: {
71
+ tight: '1.25',
72
+ normal: '1.5',
73
+ relaxed: '1.75',
74
+ },
75
+ letterSpacing: {
76
+ tight: '-0.025em',
77
+ normal: '0',
78
+ wide: '0.025em',
79
+ },
80
+ },
81
+ borderRadius: {
82
+ none: '0',
83
+ sm: '0.125rem',
84
+ base: '0.25rem',
85
+ md: '0.375rem',
86
+ lg: '0.5rem',
87
+ xl: '0.75rem',
88
+ '2xl': '1rem',
89
+ full: '9999px',
90
+ },
91
+ shadows: {
92
+ none: 'none',
93
+ sm: '0 1px 2px 0 rgb(0 0 0 / 0.05)',
94
+ base: '0 1px 3px 0 rgb(0 0 0 / 0.1)',
95
+ md: '0 4px 6px -1px rgb(0 0 0 / 0.1)',
96
+ lg: '0 10px 15px -3px rgb(0 0 0 / 0.1)',
97
+ xl: '0 20px 25px -5px rgb(0 0 0 / 0.1)',
98
+ inner: 'inset 0 2px 4px 0 rgb(0 0 0 / 0.05)',
99
+ },
100
+ animation: {
101
+ duration: { fast: '150ms', base: '300ms', slow: '500ms' },
102
+ timing: { ease: 'cubic-bezier(0.4, 0, 0.2, 1)', linear: 'linear' },
103
+ },
104
+ zIndex: {
105
+ base: 0,
106
+ dropdown: 1000,
107
+ sticky: 1100,
108
+ fixed: 1200,
109
+ modalBackdrop: 1300,
110
+ modal: 1400,
111
+ popover: 1500,
112
+ tooltip: 1600,
113
+ },
114
+ };
115
+
116
+ // ============================================================================
117
+ // hexToHSL
118
+ // ============================================================================
119
+
120
+ describe('hexToHSL', () => {
121
+ it('should convert #000000 to "0 0% 0%"', () => {
122
+ expect(hexToHSL('#000000')).toBe('0 0% 0%');
123
+ });
124
+
125
+ it('should convert #FFFFFF to "0 0% 100%"', () => {
126
+ expect(hexToHSL('#FFFFFF')).toBe('0 0% 100%');
127
+ });
128
+
129
+ it('should convert #FF0000 to red HSL', () => {
130
+ expect(hexToHSL('#FF0000')).toBe('0 100% 50%');
131
+ });
132
+
133
+ it('should convert #3B82F6 (blue)', () => {
134
+ const result = hexToHSL('#3B82F6');
135
+ expect(result).toBeTruthy();
136
+ expect(result).toMatch(/^\d+ \d+% \d+%$/);
137
+ });
138
+
139
+ it('should handle shorthand hex #F00', () => {
140
+ expect(hexToHSL('#F00')).toBe('0 100% 50%');
141
+ });
142
+
143
+ it('should handle hex without #', () => {
144
+ expect(hexToHSL('FF0000')).toBe('0 100% 50%');
145
+ });
146
+
147
+ it('should return null for invalid hex', () => {
148
+ expect(hexToHSL('not-a-color')).toBeNull();
149
+ expect(hexToHSL('#GGHHII')).toBeNull();
150
+ expect(hexToHSL('')).toBeNull();
151
+ });
152
+ });
153
+
154
+ // ============================================================================
155
+ // toCSSColor
156
+ // ============================================================================
157
+
158
+ describe('toCSSColor', () => {
159
+ it('should convert hex to HSL', () => {
160
+ expect(toCSSColor('#FF0000')).toBe('0 100% 50%');
161
+ });
162
+
163
+ it('should pass through rgb() values', () => {
164
+ expect(toCSSColor('rgb(255, 0, 0)')).toBe('rgb(255, 0, 0)');
165
+ });
166
+
167
+ it('should pass through hsl() values', () => {
168
+ expect(toCSSColor('hsl(0, 100%, 50%)')).toBe('hsl(0, 100%, 50%)');
169
+ });
170
+
171
+ it('should pass through oklch() values', () => {
172
+ expect(toCSSColor('oklch(0.7 0.15 30)')).toBe('oklch(0.7 0.15 30)');
173
+ });
174
+ });
175
+
176
+ // ============================================================================
177
+ // generateColorVars
178
+ // ============================================================================
179
+
180
+ describe('generateColorVars', () => {
181
+ it('should generate --primary from colors.primary', () => {
182
+ const vars = generateColorVars({ primary: '#3B82F6' });
183
+ expect(vars['--primary']).toBeTruthy();
184
+ });
185
+
186
+ it('should map error → --destructive', () => {
187
+ const vars = generateColorVars({ primary: '#000', error: '#EF4444' });
188
+ expect(vars['--destructive']).toBeTruthy();
189
+ });
190
+
191
+ it('should map surface → --card', () => {
192
+ const vars = generateColorVars({ primary: '#000', surface: '#F8FAFC' });
193
+ expect(vars['--card']).toBeTruthy();
194
+ });
195
+
196
+ it('should map text → --foreground', () => {
197
+ const vars = generateColorVars({ primary: '#000', text: '#0F172A' });
198
+ expect(vars['--foreground']).toBeTruthy();
199
+ });
200
+
201
+ it('should map textSecondary → --muted-foreground', () => {
202
+ const vars = generateColorVars({ primary: '#000', textSecondary: '#64748B' });
203
+ expect(vars['--muted-foreground']).toBeTruthy();
204
+ });
205
+
206
+ it('should skip undefined color fields', () => {
207
+ const vars = generateColorVars({ primary: '#000' });
208
+ expect(Object.keys(vars)).toHaveLength(1);
209
+ });
210
+
211
+ it('should generate all vars for a complete palette', () => {
212
+ const vars = generateColorVars(baseTheme.colors);
213
+ expect(Object.keys(vars).length).toBeGreaterThanOrEqual(10);
214
+ });
215
+ });
216
+
217
+ // ============================================================================
218
+ // generateTypographyVars
219
+ // ============================================================================
220
+
221
+ describe('generateTypographyVars', () => {
222
+ it('should generate --font-sans from fontFamily.base', () => {
223
+ const vars = generateTypographyVars({ fontFamily: { base: 'Inter' } });
224
+ expect(vars['--font-sans']).toBe('Inter');
225
+ });
226
+
227
+ it('should generate --font-heading', () => {
228
+ const vars = generateTypographyVars({ fontFamily: { heading: 'Georgia' } });
229
+ expect(vars['--font-heading']).toBe('Georgia');
230
+ });
231
+
232
+ it('should generate --font-mono', () => {
233
+ const vars = generateTypographyVars({ fontFamily: { mono: 'Fira Code' } });
234
+ expect(vars['--font-mono']).toBe('Fira Code');
235
+ });
236
+
237
+ it('should generate font size vars', () => {
238
+ const vars = generateTypographyVars({
239
+ fontSize: { xs: '0.75rem', base: '1rem', '2xl': '1.5rem' },
240
+ });
241
+ expect(vars['--font-size-xs']).toBe('0.75rem');
242
+ expect(vars['--font-size-base']).toBe('1rem');
243
+ expect(vars['--font-size-2xl']).toBe('1.5rem');
244
+ });
245
+
246
+ it('should generate font weight vars as strings', () => {
247
+ const vars = generateTypographyVars({
248
+ fontWeight: { bold: 700, normal: 400 },
249
+ });
250
+ expect(vars['--font-weight-bold']).toBe('700');
251
+ expect(vars['--font-weight-normal']).toBe('400');
252
+ });
253
+
254
+ it('should generate line height vars', () => {
255
+ const vars = generateTypographyVars({
256
+ lineHeight: { tight: '1.25', normal: '1.5' },
257
+ });
258
+ expect(vars['--line-height-tight']).toBe('1.25');
259
+ expect(vars['--line-height-normal']).toBe('1.5');
260
+ });
261
+
262
+ it('should generate letter spacing vars', () => {
263
+ const vars = generateTypographyVars({
264
+ letterSpacing: { tight: '-0.025em', wide: '0.025em' },
265
+ });
266
+ expect(vars['--letter-spacing-tight']).toBe('-0.025em');
267
+ expect(vars['--letter-spacing-wide']).toBe('0.025em');
268
+ });
269
+
270
+ it('should handle complete typography config', () => {
271
+ const vars = generateTypographyVars(baseTheme.typography!);
272
+ expect(Object.keys(vars).length).toBeGreaterThanOrEqual(10);
273
+ });
274
+ });
275
+
276
+ // ============================================================================
277
+ // generateBorderRadiusVars
278
+ // ============================================================================
279
+
280
+ describe('generateBorderRadiusVars', () => {
281
+ it('should map base → --radius', () => {
282
+ const vars = generateBorderRadiusVars({ base: '0.25rem' });
283
+ expect(vars['--radius']).toBe('0.25rem');
284
+ });
285
+
286
+ it('should map all radius keys', () => {
287
+ const vars = generateBorderRadiusVars(baseTheme.borderRadius!);
288
+ expect(vars['--radius-none']).toBe('0');
289
+ expect(vars['--radius-sm']).toBe('0.125rem');
290
+ expect(vars['--radius']).toBe('0.25rem');
291
+ expect(vars['--radius-lg']).toBe('0.5rem');
292
+ expect(vars['--radius-full']).toBe('9999px');
293
+ });
294
+
295
+ it('should skip undefined radius values', () => {
296
+ const vars = generateBorderRadiusVars({ lg: '0.5rem' });
297
+ expect(Object.keys(vars)).toHaveLength(1);
298
+ });
299
+ });
300
+
301
+ // ============================================================================
302
+ // generateShadowVars
303
+ // ============================================================================
304
+
305
+ describe('generateShadowVars', () => {
306
+ it('should map base → --shadow', () => {
307
+ const vars = generateShadowVars({ base: '0 1px 3px 0 rgb(0 0 0 / 0.1)' });
308
+ expect(vars['--shadow']).toBe('0 1px 3px 0 rgb(0 0 0 / 0.1)');
309
+ });
310
+
311
+ it('should map all shadow keys', () => {
312
+ const vars = generateShadowVars(baseTheme.shadows!);
313
+ expect(vars['--shadow-none']).toBe('none');
314
+ expect(vars['--shadow-inner']).toBeTruthy();
315
+ expect(vars['--shadow-lg']).toBeTruthy();
316
+ });
317
+ });
318
+
319
+ // ============================================================================
320
+ // generateAnimationVars
321
+ // ============================================================================
322
+
323
+ describe('generateAnimationVars', () => {
324
+ it('should generate duration vars', () => {
325
+ const vars = generateAnimationVars({
326
+ duration: { fast: '150ms', base: '300ms', slow: '500ms' },
327
+ });
328
+ expect(vars['--duration-fast']).toBe('150ms');
329
+ expect(vars['--duration-base']).toBe('300ms');
330
+ expect(vars['--duration-slow']).toBe('500ms');
331
+ });
332
+
333
+ it('should generate timing vars', () => {
334
+ const vars = generateAnimationVars({
335
+ timing: { ease: 'cubic-bezier(0.4, 0, 0.2, 1)', linear: 'linear' },
336
+ });
337
+ expect(vars['--timing-ease']).toBe('cubic-bezier(0.4, 0, 0.2, 1)');
338
+ expect(vars['--timing-linear']).toBe('linear');
339
+ });
340
+ });
341
+
342
+ // ============================================================================
343
+ // generateZIndexVars
344
+ // ============================================================================
345
+
346
+ describe('generateZIndexVars', () => {
347
+ it('should generate z-index vars as strings', () => {
348
+ const vars = generateZIndexVars({
349
+ base: 0,
350
+ modal: 1400,
351
+ tooltip: 1600,
352
+ });
353
+ expect(vars['--z-base']).toBe('0');
354
+ expect(vars['--z-modal']).toBe('1400');
355
+ expect(vars['--z-tooltip']).toBe('1600');
356
+ });
357
+
358
+ it('should handle all z-index keys', () => {
359
+ const vars = generateZIndexVars(baseTheme.zIndex!);
360
+ expect(Object.keys(vars)).toHaveLength(8);
361
+ });
362
+ });
363
+
364
+ // ============================================================================
365
+ // generateThemeVars (integration)
366
+ // ============================================================================
367
+
368
+ describe('generateThemeVars', () => {
369
+ it('should generate vars from a minimal theme', () => {
370
+ const minimal: Theme = {
371
+ name: 'minimal',
372
+ label: 'Minimal',
373
+ colors: { primary: '#3B82F6' },
374
+ };
375
+ const vars = generateThemeVars(minimal);
376
+ expect(vars['--primary']).toBeTruthy();
377
+ expect(Object.keys(vars).length).toBe(1);
378
+ });
379
+
380
+ it('should generate vars from a complete theme', () => {
381
+ const vars = generateThemeVars(baseTheme);
382
+ // Colors + Typography + BorderRadius + Shadows + Animation + ZIndex
383
+ expect(Object.keys(vars).length).toBeGreaterThan(40);
384
+ });
385
+
386
+ it('should include customVars with -- prefix', () => {
387
+ const theme: Theme = {
388
+ name: 'custom',
389
+ label: 'Custom',
390
+ colors: { primary: '#000' },
391
+ customVars: {
392
+ '--sidebar-width': '280px',
393
+ 'header-height': '64px',
394
+ },
395
+ };
396
+ const vars = generateThemeVars(theme);
397
+ expect(vars['--sidebar-width']).toBe('280px');
398
+ expect(vars['--header-height']).toBe('64px');
399
+ });
400
+ });
401
+
402
+ // ============================================================================
403
+ // mergeThemes
404
+ // ============================================================================
405
+
406
+ describe('mergeThemes', () => {
407
+ const parent: Theme = {
408
+ name: 'parent',
409
+ label: 'Parent',
410
+ colors: { primary: '#000', secondary: '#111' },
411
+ typography: {
412
+ fontFamily: { base: 'Arial' },
413
+ fontSize: { base: '1rem', lg: '1.125rem' },
414
+ },
415
+ borderRadius: { sm: '2px', lg: '8px' },
416
+ zIndex: { base: 0, modal: 1400 },
417
+ };
418
+
419
+ it('should override top-level scalar fields', () => {
420
+ const merged = mergeThemes(parent, { label: 'Child', description: 'A child theme' } as Partial<Theme>);
421
+ expect(merged.label).toBe('Child');
422
+ expect(merged.description).toBe('A child theme');
423
+ expect(merged.name).toBe('parent');
424
+ });
425
+
426
+ it('should deep-merge colors', () => {
427
+ const merged = mergeThemes(parent, {
428
+ colors: { primary: '#FFF', accent: '#F00' },
429
+ } as Partial<Theme>);
430
+ expect(merged.colors.primary).toBe('#FFF');
431
+ expect(merged.colors.secondary).toBe('#111'); // from parent
432
+ expect(merged.colors.accent).toBe('#F00'); // from child
433
+ });
434
+
435
+ it('should deep-merge typography.fontFamily', () => {
436
+ const merged = mergeThemes(parent, {
437
+ typography: {
438
+ fontFamily: { heading: 'Georgia' },
439
+ },
440
+ } as Partial<Theme>);
441
+ expect(merged.typography?.fontFamily?.base).toBe('Arial'); // from parent
442
+ expect(merged.typography?.fontFamily?.heading).toBe('Georgia'); // from child
443
+ });
444
+
445
+ it('should deep-merge typography.fontSize', () => {
446
+ const merged = mergeThemes(parent, {
447
+ typography: {
448
+ fontSize: { base: '1.125rem', xl: '1.5rem' },
449
+ },
450
+ } as Partial<Theme>);
451
+ expect(merged.typography?.fontSize?.base).toBe('1.125rem'); // overridden
452
+ expect(merged.typography?.fontSize?.lg).toBe('1.125rem'); // from parent
453
+ expect(merged.typography?.fontSize?.xl).toBe('1.5rem'); // from child
454
+ });
455
+
456
+ it('should deep-merge borderRadius', () => {
457
+ const merged = mergeThemes(parent, {
458
+ borderRadius: { sm: '4px', xl: '16px' },
459
+ } as Partial<Theme>);
460
+ expect(merged.borderRadius?.sm).toBe('4px'); // overridden
461
+ expect(merged.borderRadius?.lg).toBe('8px'); // from parent
462
+ expect(merged.borderRadius?.xl).toBe('16px'); // new
463
+ });
464
+
465
+ it('should deep-merge zIndex', () => {
466
+ const merged = mergeThemes(parent, {
467
+ zIndex: { tooltip: 9999 },
468
+ } as Partial<Theme>);
469
+ expect(merged.zIndex?.base).toBe(0);
470
+ expect(merged.zIndex?.modal).toBe(1400);
471
+ expect(merged.zIndex?.tooltip).toBe(9999);
472
+ });
473
+
474
+ it('should preserve parent when child has no field', () => {
475
+ const merged = mergeThemes(parent, { colors: { primary: '#FFF' } } as Partial<Theme>);
476
+ expect(merged.borderRadius).toEqual(parent.borderRadius);
477
+ expect(merged.typography).toBeDefined();
478
+ });
479
+ });
480
+
481
+ // ============================================================================
482
+ // resolveThemeInheritance
483
+ // ============================================================================
484
+
485
+ describe('resolveThemeInheritance', () => {
486
+ it('should return the theme as-is when no extends', () => {
487
+ const theme: Theme = { name: 'solo', label: 'Solo', colors: { primary: '#000' } };
488
+ const registry = new Map([['solo', theme]]);
489
+ expect(resolveThemeInheritance(theme, registry)).toBe(theme);
490
+ });
491
+
492
+ it('should merge parent when extends is set', () => {
493
+ const parent: Theme = {
494
+ name: 'parent',
495
+ label: 'Parent',
496
+ colors: { primary: '#000', secondary: '#111' },
497
+ borderRadius: { lg: '8px' },
498
+ };
499
+ const child: Theme = {
500
+ name: 'child',
501
+ label: 'Child',
502
+ colors: { primary: '#FFF' },
503
+ extends: 'parent',
504
+ };
505
+ const registry = new Map([['parent', parent], ['child', child]]);
506
+ const resolved = resolveThemeInheritance(child, registry);
507
+ expect(resolved.colors.primary).toBe('#FFF');
508
+ expect(resolved.colors.secondary).toBe('#111');
509
+ expect(resolved.borderRadius?.lg).toBe('8px');
510
+ });
511
+
512
+ it('should resolve multi-level inheritance', () => {
513
+ const grandparent: Theme = {
514
+ name: 'gp',
515
+ label: 'Grandparent',
516
+ colors: { primary: '#000', accent: '#AAA' },
517
+ };
518
+ const parent: Theme = {
519
+ name: 'parent',
520
+ label: 'Parent',
521
+ colors: { primary: '#111' },
522
+ extends: 'gp',
523
+ };
524
+ const child: Theme = {
525
+ name: 'child',
526
+ label: 'Child',
527
+ colors: { primary: '#FFF' },
528
+ extends: 'parent',
529
+ };
530
+ const registry = new Map([
531
+ ['gp', grandparent],
532
+ ['parent', parent],
533
+ ['child', child],
534
+ ]);
535
+ const resolved = resolveThemeInheritance(child, registry);
536
+ expect(resolved.colors.primary).toBe('#FFF'); // child
537
+ expect(resolved.colors.accent).toBe('#AAA'); // grandparent
538
+ });
539
+
540
+ it('should handle missing parent gracefully', () => {
541
+ const child: Theme = {
542
+ name: 'orphan',
543
+ label: 'Orphan',
544
+ colors: { primary: '#000' },
545
+ extends: 'nonexistent',
546
+ };
547
+ const registry = new Map([['orphan', child]]);
548
+ const resolved = resolveThemeInheritance(child, registry);
549
+ expect(resolved.name).toBe('orphan');
550
+ expect(resolved.colors.primary).toBe('#000');
551
+ });
552
+
553
+ it('should detect and break circular inheritance', () => {
554
+ const a: Theme = { name: 'a', label: 'A', colors: { primary: '#000' }, extends: 'b' };
555
+ const b: Theme = { name: 'b', label: 'B', colors: { primary: '#111' }, extends: 'a' };
556
+ const registry = new Map([['a', a], ['b', b]]);
557
+ // Should not infinite loop
558
+ const resolved = resolveThemeInheritance(a, registry);
559
+ expect(resolved.name).toBeDefined();
560
+ });
561
+ });
562
+
563
+ // ============================================================================
564
+ // resolveMode
565
+ // ============================================================================
566
+
567
+ describe('resolveMode', () => {
568
+ it('should return "light" for mode="light"', () => {
569
+ expect(resolveMode('light')).toBe('light');
570
+ });
571
+
572
+ it('should return "dark" for mode="dark"', () => {
573
+ expect(resolveMode('dark')).toBe('dark');
574
+ });
575
+
576
+ it('should resolve "auto" using systemDark=true', () => {
577
+ expect(resolveMode('auto', true)).toBe('dark');
578
+ });
579
+
580
+ it('should resolve "auto" using systemDark=false', () => {
581
+ expect(resolveMode('auto', false)).toBe('light');
582
+ });
583
+
584
+ it('should default to "light" when mode is undefined', () => {
585
+ expect(resolveMode(undefined, false)).toBe('light');
586
+ });
587
+
588
+ it('should use matchMedia when systemDark is not provided', () => {
589
+ // Mock matchMedia
590
+ const original = window.matchMedia;
591
+ window.matchMedia = vi.fn().mockImplementation((query: string) => ({
592
+ matches: query === '(prefers-color-scheme: dark)',
593
+ media: query,
594
+ }));
595
+ expect(resolveMode('auto')).toBe('dark');
596
+ window.matchMedia = original;
597
+ });
598
+
599
+ it('should fallback to "light" when no matchMedia', () => {
600
+ const original = window.matchMedia;
601
+ // @ts-expect-error testing fallback
602
+ window.matchMedia = undefined;
603
+ expect(resolveMode('auto')).toBe('light');
604
+ window.matchMedia = original;
605
+ });
606
+ });
@@ -0,0 +1,22 @@
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
+ export {
10
+ hexToHSL,
11
+ toCSSColor,
12
+ generateColorVars,
13
+ generateTypographyVars,
14
+ generateBorderRadiusVars,
15
+ generateShadowVars,
16
+ generateAnimationVars,
17
+ generateZIndexVars,
18
+ generateThemeVars,
19
+ mergeThemes,
20
+ resolveThemeInheritance,
21
+ resolveMode,
22
+ } from './ThemeEngine';