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