-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglaze.test.ts
More file actions
1040 lines (836 loc) · 32.3 KB
/
glaze.test.ts
File metadata and controls
1040 lines (836 loc) · 32.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { glaze } from './glaze';
describe('glaze', () => {
beforeEach(() => {
glaze.resetConfig();
});
describe('theme creation', () => {
it('creates a theme with hue and saturation', () => {
const theme = glaze(280, 80);
expect(theme.hue).toBe(280);
expect(theme.saturation).toBe(80);
});
it('creates a theme with options object', () => {
const theme = glaze({ hue: 280, saturation: 80 });
expect(theme.hue).toBe(280);
expect(theme.saturation).toBe(80);
});
it('defaults saturation to 100 when using shorthand', () => {
const theme = glaze(280);
expect(theme.saturation).toBe(100);
});
});
describe('color definitions', () => {
it('resolves root colors', () => {
const theme = glaze(280, 80);
theme.colors({
surface: { lightness: 97, saturation: 0.75 },
});
const resolved = theme.resolve();
const surface = resolved.get('surface')!;
expect(surface).toBeDefined();
expect(surface.light.h).toBe(280);
expect(surface.light.l).toBeCloseTo(0.97, 2);
expect(surface.light.s).toBeCloseTo(0.6, 2); // 0.75 * 80/100
});
it('resolves dependent colors with relative lightness', () => {
const theme = glaze(280, 80);
theme.colors({
surface: { lightness: 97, saturation: 0.75 },
text: { base: 'surface', lightness: '-52', contrast: 'AAA' },
});
const resolved = theme.resolve();
const text = resolved.get('text')!;
expect(text).toBeDefined();
// Text should be darker than surface in light mode
expect(text.light.l).toBeLessThan(0.97);
});
it('resolves dependent colors with absolute lightness', () => {
const theme = glaze(0, 0);
theme.colors({
surface: { lightness: 97 },
text: { base: 'surface', lightness: 45, contrast: 'AAA' },
});
const resolved = theme.resolve();
const text = resolved.get('text')!;
expect(text).toBeDefined();
// Absolute lightness 45 in light mode
expect(text.light.l).toBeLessThan(0.97);
});
it('resolves dependent colors without lightness (inherits base)', () => {
const theme = glaze(0, 0);
theme.colors({
surface: { lightness: 97 },
overlay: { base: 'surface' },
});
const resolved = theme.resolve();
const overlay = resolved.get('overlay')!;
// Should inherit base lightness
expect(overlay.light.l).toBeCloseTo(0.97, 2);
});
it('merges colors additively on second .colors() call', () => {
const theme = glaze(280, 80);
theme.colors({ surface: { lightness: 97 } });
theme.colors({ text: { lightness: 30 } });
const resolved = theme.resolve();
expect(resolved.has('surface')).toBe(true);
expect(resolved.has('text')).toBe(true);
});
it('overwrites existing color on .colors() with same key', () => {
const theme = glaze(280, 80);
theme.colors({ surface: { lightness: 97 } });
theme.colors({ surface: { lightness: 50 } });
const resolved = theme.resolve();
expect(resolved.get('surface')!.light.l).toBeCloseTo(0.5, 2);
});
});
describe('validation', () => {
it('throws on contrast without base', () => {
const theme = glaze(280, 80);
theme.colors({
text: { lightness: 50, contrast: 'AA' } as any,
});
expect(() => theme.resolve()).toThrow('contrast');
});
it('throws on relative lightness without base', () => {
const theme = glaze(280, 80);
theme.colors({
text: { lightness: '-52' } as any,
});
expect(() => theme.resolve()).toThrow('relative');
});
it('throws on non-existent base reference', () => {
const theme = glaze(280, 80);
theme.colors({
text: { base: 'nonexistent', lightness: '-52' },
});
expect(() => theme.resolve()).toThrow('non-existent');
});
it('throws on circular base references', () => {
const theme = glaze(280, 80);
theme.colors({
a: { base: 'b', lightness: '-10' },
b: { base: 'a', lightness: '-10' },
});
expect(() => theme.resolve()).toThrow('circular');
});
it('throws when color has neither absolute lightness nor base', () => {
const theme = glaze(280, 80);
theme.colors({
text: { saturation: 0.5 } as any,
});
expect(() => theme.resolve()).toThrow('must have either');
});
});
describe('relative lightness', () => {
it('negative relative lightness means darker than base', () => {
const theme = glaze(0, 0);
theme.colors({
surface: { lightness: 97 },
text: { base: 'surface', lightness: '-52' },
});
const resolved = theme.resolve();
const text = resolved.get('text')!;
// 97 - 52 = 45
expect(text.light.l).toBeCloseTo(0.45, 2);
});
it('positive relative lightness means lighter than base', () => {
const theme = glaze(0, 0);
theme.colors({
fill: { lightness: 52 },
text: { base: 'fill', lightness: '+48' },
});
const resolved = theme.resolve();
const text = resolved.get('text')!;
// 52 + 48 = 100
expect(text.light.l).toBeCloseTo(1.0, 2);
});
});
describe('per-color hue', () => {
it('absolute hue overrides theme seed', () => {
const theme = glaze(280, 80);
theme.colors({
surface: { lightness: 97, hue: 120 },
});
const resolved = theme.resolve();
const surface = resolved.get('surface')!;
expect(surface.light.h).toBe(120);
});
it('relative hue shifts from theme seed', () => {
const theme = glaze(280, 80);
theme.colors({
surface: { lightness: 97, hue: '+20' },
});
const resolved = theme.resolve();
const surface = resolved.get('surface')!;
expect(surface.light.h).toBeCloseTo(300, 2);
});
it('negative relative hue shifts backwards', () => {
const theme = glaze(30, 80);
theme.colors({
surface: { lightness: 97, hue: '-50' },
});
const resolved = theme.resolve();
const surface = resolved.get('surface')!;
// 30 - 50 = -20 → wraps to 340
expect(surface.light.h).toBeCloseTo(340, 2);
});
it('hue wraps around 360', () => {
const theme = glaze(350, 80);
theme.colors({
surface: { lightness: 97, hue: '+30' },
});
const resolved = theme.resolve();
const surface = resolved.get('surface')!;
// 350 + 30 = 380 → wraps to 20
expect(surface.light.h).toBeCloseTo(20, 2);
});
it('per-color hue is relative to theme seed, not base', () => {
const theme = glaze(280, 80);
theme.colors({
surface: { lightness: 97, hue: 120 },
text: { base: 'surface', lightness: '-30', hue: '+20' },
});
const resolved = theme.resolve();
const text = resolved.get('text')!;
// hue: '+20' is relative to theme seed 280, not surface's 120
expect(text.light.h).toBeCloseTo(300, 2);
});
});
describe('adaptation modes', () => {
it('auto mode inverts lightness in dark scheme', () => {
const theme = glaze(280, 80);
theme.colors({
surface: { lightness: 97, saturation: 0.75 },
});
const resolved = theme.resolve();
const surface = resolved.get('surface')!;
// Light: L=97
// Dark (auto, inverted): ((100-97) * (90-10)) / 100 + 10 = 3*0.8 + 10 = 12.4
expect(surface.dark.l).toBeCloseTo(0.124, 2);
});
it('fixed mode maps lightness without inversion', () => {
const theme = glaze(280, 80);
theme.colors({
fill: { lightness: 52, mode: 'fixed' },
});
const resolved = theme.resolve();
const fill = resolved.get('fill')!;
// Fixed: (52 * (90-10)) / 100 + 10 = 52*0.8 + 10 = 51.6
expect(fill.dark.l).toBeCloseTo(0.516, 2);
});
it('static mode preserves lightness across schemes', () => {
const theme = glaze(280, 80);
theme.colors({
brand: { lightness: 60, mode: 'static' },
});
const resolved = theme.resolve();
const brand = resolved.get('brand')!;
expect(brand.dark.l).toBeCloseTo(brand.light.l, 4);
expect(brand.dark.s).toBeCloseTo(brand.light.s, 4);
});
});
describe('dark scheme', () => {
it('applies desaturation in dark mode', () => {
const theme = glaze(280, 80);
theme.colors({
surface: { lightness: 97, saturation: 0.75 },
});
const resolved = theme.resolve();
const surface = resolved.get('surface')!;
// Dark saturation = light_sat * (1 - 0.1)
expect(surface.dark.s).toBeCloseTo(surface.light.s * 0.9, 2);
});
});
describe('high-contrast mode', () => {
it('uses HC pair value for lightness', () => {
const theme = glaze(0, 0);
theme.colors({
surface: { lightness: [97, 100] },
});
const resolved = theme.resolve();
const surface = resolved.get('surface')!;
expect(surface.light.l).toBeCloseTo(0.97, 2);
expect(surface.lightContrast.l).toBeCloseTo(1.0, 2);
});
it('uses HC pair value for relative lightness', () => {
const theme = glaze(0, 0);
theme.colors({
surface: { lightness: 97 },
border: { base: 'surface', lightness: ['-7', '-20'] },
});
const resolved = theme.resolve();
const border = resolved.get('border')!;
// Normal: 97 - 7 = 90
// HC: 97 - 20 = 77
expect(border.light.l).toBeCloseTo(0.9, 2);
expect(border.lightContrast.l).toBeCloseTo(0.77, 2);
});
});
describe('extend', () => {
it('inherits all color definitions', () => {
const primary = glaze(280, 80);
primary.colors({
surface: { lightness: 97, saturation: 0.75 },
text: { base: 'surface', lightness: '-52', contrast: 'AAA' },
});
const danger = primary.extend({ hue: 23 });
const resolved = danger.resolve();
expect(resolved.has('surface')).toBe(true);
expect(resolved.has('text')).toBe(true);
expect(resolved.get('surface')!.light.h).toBe(23);
});
it('can override saturation', () => {
const primary = glaze(280, 80);
primary.colors({ surface: { lightness: 97 } });
const muted = primary.extend({ saturation: 40 });
expect(muted.saturation).toBe(40);
});
it('can override individual colors (additive merge)', () => {
const primary = glaze(280, 80);
primary.colors({
surface: { lightness: 97 },
fill: { lightness: 52 },
});
const danger = primary.extend({
hue: 23,
colors: {
fill: { lightness: 48, mode: 'fixed' },
},
});
const resolved = danger.resolve();
expect(resolved.has('surface')).toBe(true);
expect(resolved.get('fill')!.mode).toBe('fixed');
});
});
describe('token export', () => {
it('exports tokens with # prefix', () => {
const theme = glaze(280, 80);
theme.colors({
surface: { lightness: 97 },
});
// Enable all modes for this test
const tokens = theme.tokens({
modes: { dark: true, highContrast: true },
});
expect(tokens['#surface']).toBeDefined();
expect(tokens['#surface']['']).toMatch(/^okhsl\(/);
expect(tokens['#surface']['@dark']).toMatch(/^okhsl\(/);
expect(tokens['#surface']['@high-contrast']).toMatch(/^okhsl\(/);
expect(tokens['#surface']['@dark & @high-contrast']).toMatch(/^okhsl\(/);
});
it('supports custom state aliases', () => {
const theme = glaze(280, 80);
theme.colors({ surface: { lightness: 97 } });
// Enable highContrast to test HC state aliases
const tokens = theme.tokens({
states: { dark: '@night', highContrast: '@hc' },
modes: { dark: true, highContrast: true },
});
expect(tokens['#surface']['@night']).toBeDefined();
expect(tokens['#surface']['@hc']).toBeDefined();
expect(tokens['#surface']['@night & @hc']).toBeDefined();
});
});
describe('JSON export', () => {
it('exports plain scheme variants', () => {
const theme = glaze(280, 80);
theme.colors({ surface: { lightness: 97 } });
// Enable all modes for this test
const json = theme.json({ modes: { dark: true, highContrast: true } });
expect(json.surface).toBeDefined();
expect(json.surface.light).toMatch(/^okhsl\(/);
expect(json.surface.dark).toMatch(/^okhsl\(/);
expect(json.surface.lightContrast).toMatch(/^okhsl\(/);
expect(json.surface.darkContrast).toMatch(/^okhsl\(/);
});
});
describe('palette', () => {
it('combines multiple themes with prefix', () => {
const primary = glaze(280, 80);
primary.colors({ surface: { lightness: 97 } });
const danger = primary.extend({ hue: 23 });
const palette = glaze.palette({ primary, danger });
const tokens = palette.tokens({ prefix: true });
expect(tokens['#primary-surface']).toBeDefined();
expect(tokens['#danger-surface']).toBeDefined();
});
it('supports custom prefix mapping', () => {
const primary = glaze(280, 80);
primary.colors({ surface: { lightness: 97 } });
const danger = primary.extend({ hue: 23 });
const palette = glaze.palette({ primary, danger });
const tokens = palette.tokens({
prefix: { primary: 'brand-', danger: 'error-' },
});
expect(tokens['#brand-surface']).toBeDefined();
expect(tokens['#error-surface']).toBeDefined();
});
it('exports JSON with theme grouping', () => {
const primary = glaze(280, 80);
primary.colors({ surface: { lightness: 97 } });
const danger = primary.extend({ hue: 23 });
const palette = glaze.palette({ primary, danger });
const json = palette.json();
expect(json.primary).toBeDefined();
expect(json.danger).toBeDefined();
expect(json.primary.surface.light).toMatch(/^okhsl\(/);
});
});
describe('configure', () => {
it('updates dark lightness window', () => {
glaze.configure({ darkLightness: [5, 95] });
const config = glaze.getConfig();
expect(config.darkLightness).toEqual([5, 95]);
});
it('updates dark desaturation', () => {
glaze.configure({ darkDesaturation: 0.2 });
const config = glaze.getConfig();
expect(config.darkDesaturation).toBe(0.2);
});
it('updates state aliases', () => {
glaze.configure({ states: { dark: '@night' } });
const config = glaze.getConfig();
expect(config.states.dark).toBe('@night');
// highContrast should keep default
expect(config.states.highContrast).toBe('@high-contrast');
});
it('updates modes', () => {
glaze.configure({ modes: { dark: false } });
const config = glaze.getConfig();
expect(config.modes.dark).toBe(false);
expect(config.modes.highContrast).toBe(false); // default is false
});
});
describe('output modes', () => {
it('defaults to light + dark (2 variants) in tokens', () => {
const theme = glaze(280, 80);
theme.colors({ surface: { lightness: 97 } });
const tokens = theme.tokens();
// Default: dark=true, highContrast=false → light + dark
expect(Object.keys(tokens['#surface'])).toHaveLength(2);
});
it('modes: { dark: false, highContrast: true } omits dark and darkContrast from tokens', () => {
const theme = glaze(280, 80);
theme.colors({ surface: { lightness: 97 } });
const tokens = theme.tokens({
modes: { dark: false, highContrast: true },
});
const keys = Object.keys(tokens['#surface']);
expect(keys).toContain('');
expect(keys).toContain('@high-contrast');
expect(keys).toHaveLength(2);
});
it('modes: { highContrast: false } omits HC and darkContrast from tokens', () => {
const theme = glaze(280, 80);
theme.colors({ surface: { lightness: 97 } });
const tokens = theme.tokens({ modes: { highContrast: false } });
const keys = Object.keys(tokens['#surface']);
expect(keys).toContain('');
expect(keys).toContain('@dark');
expect(keys).toHaveLength(2);
});
it('modes: { dark: false, highContrast: false } exports light only in tokens', () => {
const theme = glaze(280, 80);
theme.colors({ surface: { lightness: 97 } });
const tokens = theme.tokens({
modes: { dark: false, highContrast: false },
});
const keys = Object.keys(tokens['#surface']);
expect(keys).toEqual(['']);
});
it('modes work on json() export', () => {
const theme = glaze(280, 80);
theme.colors({ surface: { lightness: 97 } });
const json = theme.json({ modes: { highContrast: false } });
const keys = Object.keys(json.surface);
expect(keys).toContain('light');
expect(keys).toContain('dark');
expect(keys).not.toContain('lightContrast');
expect(keys).not.toContain('darkContrast');
});
it('global modes config is respected', () => {
glaze.configure({ modes: { highContrast: true } });
const theme = glaze(280, 80);
theme.colors({ surface: { lightness: 97 } });
const tokens = theme.tokens();
// highContrast enabled → all 4 variants
expect(Object.keys(tokens['#surface'])).toHaveLength(4);
});
it('per-call modes override global config', () => {
glaze.configure({ modes: { dark: false, highContrast: false } });
const theme = glaze(280, 80);
theme.colors({ surface: { lightness: 97 } });
// Override back to all modes
const tokens = theme.tokens({
modes: { dark: true, highContrast: true },
});
expect(Object.keys(tokens['#surface'])).toHaveLength(4);
});
it('modes work on palette tokens', () => {
const primary = glaze(280, 80);
primary.colors({ surface: { lightness: 97 } });
const palette = glaze.palette({ primary });
const tokens = palette.tokens({
prefix: true,
modes: { dark: false, highContrast: false },
});
expect(Object.keys(tokens['#primary-surface'])).toEqual(['']);
});
it('modes work on palette json', () => {
const primary = glaze(280, 80);
primary.colors({ surface: { lightness: 97 } });
const palette = glaze.palette({ primary });
// Enable highContrast to test filtering
const json = palette.json({
modes: { dark: false, highContrast: true },
});
const keys = Object.keys(json.primary.surface);
expect(keys).toContain('light');
expect(keys).toContain('lightContrast');
expect(keys).not.toContain('dark');
expect(keys).not.toContain('darkContrast');
});
});
describe('color getter/setter', () => {
it('sets a single color with .color(name, def)', () => {
const theme = glaze(280, 80);
theme.color('surface', { lightness: 97 });
const resolved = theme.resolve();
expect(resolved.has('surface')).toBe(true);
expect(resolved.get('surface')!.light.l).toBeCloseTo(0.97, 2);
});
it('gets a color definition with .color(name)', () => {
const theme = glaze(280, 80);
theme.color('surface', { lightness: 97, saturation: 0.75 });
const def = theme.color('surface');
expect(def).toEqual({ lightness: 97, saturation: 0.75 });
});
it('returns undefined for missing color', () => {
const theme = glaze(280, 80);
expect(theme.color('nonexistent')).toBeUndefined();
});
});
describe('remove', () => {
it('removes a single color', () => {
const theme = glaze(280, 80);
theme.colors({ surface: { lightness: 97 }, text: { lightness: 30 } });
theme.remove('surface');
expect(theme.has('surface')).toBe(false);
expect(theme.has('text')).toBe(true);
});
it('removes multiple colors', () => {
const theme = glaze(280, 80);
theme.colors({
surface: { lightness: 97 },
text: { lightness: 30 },
bg: { lightness: 100 },
});
theme.remove(['surface', 'text']);
expect(theme.has('surface')).toBe(false);
expect(theme.has('text')).toBe(false);
expect(theme.has('bg')).toBe(true);
});
it('is a no-op for missing names', () => {
const theme = glaze(280, 80);
theme.colors({ surface: { lightness: 97 } });
theme.remove('nonexistent');
expect(theme.list()).toEqual(['surface']);
});
});
describe('has / list', () => {
it('has() returns true for defined colors', () => {
const theme = glaze(280, 80);
theme.colors({ surface: { lightness: 97 } });
expect(theme.has('surface')).toBe(true);
expect(theme.has('text')).toBe(false);
});
it('list() returns all defined color names', () => {
const theme = glaze(280, 80);
theme.colors({ surface: { lightness: 97 }, text: { lightness: 30 } });
expect(theme.list()).toEqual(['surface', 'text']);
});
it('list() reflects removals', () => {
const theme = glaze(280, 80);
theme.colors({ surface: { lightness: 97 }, text: { lightness: 30 } });
theme.remove('surface');
expect(theme.list()).toEqual(['text']);
});
});
describe('reset', () => {
it('clears all color definitions', () => {
const theme = glaze(280, 80);
theme.colors({ surface: { lightness: 97 }, text: { lightness: 30 } });
theme.reset();
expect(theme.list()).toEqual([]);
});
});
describe('export / from', () => {
it('exports theme configuration', () => {
const theme = glaze(280, 80);
theme.colors({ surface: { lightness: 97, saturation: 0.75 } });
const exported = theme.export();
expect(exported.hue).toBe(280);
expect(exported.saturation).toBe(80);
expect(exported.colors.surface).toEqual({
lightness: 97,
saturation: 0.75,
});
});
it('round-trips through export/from', () => {
const theme = glaze(280, 80);
theme.colors({
surface: { lightness: 97, saturation: 0.75 },
text: { base: 'surface', lightness: '-52', contrast: 'AAA' },
});
const exported = theme.export();
const restored = glaze.from(exported);
expect(restored.hue).toBe(280);
expect(restored.saturation).toBe(80);
const origResolved = theme.resolve();
const restoredResolved = restored.resolve();
const origSurface = origResolved.get('surface')!;
const restoredSurface = restoredResolved.get('surface')!;
expect(restoredSurface.light.l).toBeCloseTo(origSurface.light.l, 6);
expect(restoredSurface.dark.l).toBeCloseTo(origSurface.dark.l, 6);
});
it('export creates a snapshot (mutations do not affect it)', () => {
const theme = glaze(280, 80);
theme.colors({ surface: { lightness: 97 } });
const exported = theme.export();
theme.colors({ newColor: { lightness: 50 } });
expect(exported.colors).not.toHaveProperty('newColor');
});
});
describe('glaze.color standalone', () => {
it('resolves a standalone color', () => {
const color = glaze.color({ hue: 280, saturation: 80, lightness: 52 });
const resolved = color.resolve();
expect(resolved.light.h).toBe(280);
expect(resolved.light.l).toBeCloseTo(0.52, 2);
});
it('exports token for a standalone color', () => {
const color = glaze.color({
hue: 280,
saturation: 80,
lightness: 52,
mode: 'fixed',
});
const token = color.token();
expect(token['']).toMatch(/^okhsl\(/);
expect(token['@dark']).toMatch(/^okhsl\(/);
});
it('exports json for a standalone color', () => {
const color = glaze.color({ hue: 280, saturation: 80, lightness: 52 });
const json = color.json();
expect(json.light).toMatch(/^okhsl\(/);
expect(json.dark).toMatch(/^okhsl\(/);
});
it('supports format option', () => {
const color = glaze.color({ hue: 280, saturation: 80, lightness: 52 });
const rgbToken = color.token({ format: 'rgb' });
expect(rgbToken['']).toMatch(/^rgb\(/);
const hslJson = color.json({ format: 'hsl' });
expect(hslJson.light).toMatch(/^hsl\(/);
});
});
describe('glaze.fromHex / fromRgb', () => {
it('creates a theme from hex', () => {
const theme = glaze.fromHex('#7a4dbf');
expect(theme.hue).toBeGreaterThan(0);
expect(theme.saturation).toBeGreaterThan(0);
});
it('creates a theme from shorthand hex', () => {
const theme = glaze.fromHex('#f00');
expect(theme.hue).toBeGreaterThan(0);
expect(theme.saturation).toBeGreaterThan(0);
});
it('throws on invalid hex', () => {
expect(() => glaze.fromHex('not-a-color')).toThrow('invalid hex');
});
it('creates a theme from RGB values', () => {
const theme = glaze.fromRgb(122, 77, 191);
expect(theme.hue).toBeGreaterThan(0);
expect(theme.saturation).toBeGreaterThan(0);
});
it('fromHex and fromRgb produce similar results for same color', () => {
const fromHex = glaze.fromHex('#7a4dbf');
const fromRgb = glaze.fromRgb(122, 77, 191);
expect(fromHex.hue).toBeCloseTo(fromRgb.hue, 1);
expect(fromHex.saturation).toBeCloseTo(fromRgb.saturation, 1);
});
});
describe('format option', () => {
it('outputs rgb format in tokens', () => {
const theme = glaze(280, 80);
theme.colors({ surface: { lightness: 97 } });
const tokens = theme.tokens({ format: 'rgb' });
expect(tokens['#surface']['']).toMatch(/^rgb\(/);
});
it('outputs hsl format in tokens', () => {
const theme = glaze(280, 80);
theme.colors({ surface: { lightness: 97 } });
const tokens = theme.tokens({ format: 'hsl' });
expect(tokens['#surface']['']).toMatch(/^hsl\(/);
});
it('outputs oklch format in tokens', () => {
const theme = glaze(280, 80);
theme.colors({ surface: { lightness: 97 } });
const tokens = theme.tokens({ format: 'oklch' });
expect(tokens['#surface']['']).toMatch(/^oklch\(/);
});
it('outputs rgb format in json', () => {
const theme = glaze(280, 80);
theme.colors({ surface: { lightness: 97 } });
const json = theme.json({ format: 'rgb' });
expect(json.surface.light).toMatch(/^rgb\(/);
});
it('outputs format in palette tokens', () => {
const primary = glaze(280, 80);
primary.colors({ surface: { lightness: 97 } });
const palette = glaze.palette({ primary });
const tokens = palette.tokens({ prefix: true, format: 'rgb' });
expect(tokens['#primary-surface']['']).toMatch(/^rgb\(/);
});
it('outputs format in palette json', () => {
const primary = glaze(280, 80);
primary.colors({ surface: { lightness: 97 } });
const palette = glaze.palette({ primary });
const json = palette.json({ format: 'hsl' });
expect(json.primary.surface.light).toMatch(/^hsl\(/);
});
it('rgb format uses fractional values', () => {
const theme = glaze(280, 80);
theme.colors({ surface: { lightness: 52 } });
const tokens = theme.tokens({ format: 'rgb' });
const value = tokens['#surface'][''];
// Should contain decimal points for fractional precision
expect(value).toMatch(/\d+\.\d+/);
});
});
describe('full example from spec', () => {
it('resolves the full example without errors', () => {
const primary = glaze(280, 80);
primary.colors({
surface: { lightness: 97, saturation: 0.75 },
text: { base: 'surface', lightness: '-52', contrast: 'AAA' },
border: {
base: 'surface',
lightness: ['-7', '-20'],
contrast: 'AA-large',
},
bg: { lightness: 97, saturation: 0.75 },
icon: { lightness: 60, saturation: 0.94 },
'accent-fill': { lightness: 52, mode: 'fixed' },
'accent-text': {
base: 'accent-fill',
lightness: '+48',
contrast: 'AA',
mode: 'fixed',
},
disabled: { lightness: 81, saturation: 0.4 },
});
const danger = primary.extend({ hue: 23 });
const success = primary.extend({ hue: 157 });
const warning = primary.extend({ hue: 84 });
const note = primary.extend({ hue: 302 });
const palette = glaze.palette({
primary,
danger,
success,
warning,
note,
});
const tokens = palette.tokens({ prefix: true });
// Verify all expected tokens exist
expect(tokens['#primary-surface']).toBeDefined();
expect(tokens['#primary-text']).toBeDefined();
expect(tokens['#primary-border']).toBeDefined();
expect(tokens['#primary-accent-fill']).toBeDefined();
expect(tokens['#primary-accent-text']).toBeDefined();
expect(tokens['#danger-surface']).toBeDefined();
expect(tokens['#success-surface']).toBeDefined();
expect(tokens['#warning-surface']).toBeDefined();
expect(tokens['#note-surface']).toBeDefined();
// Verify token structure (default: dark=true, highContrast=false → 2 variants)
const surfaceToken = tokens['#primary-surface'];
expect(Object.keys(surfaceToken)).toHaveLength(2);
expect(surfaceToken['']).toMatch(/^okhsl\(/);
});
});
describe('css export', () => {
it('outputs CSS custom properties with default options (rgb format, -color suffix)', () => {
const theme = glaze(280, 80);
theme.colors({
surface: { lightness: 97, saturation: 0.75 },
text: { base: 'surface', lightness: '-52', contrast: 'AAA' },
});
const css = theme.css();
// All four variants should be present
expect(css.light).toBeDefined();
expect(css.dark).toBeDefined();
expect(css.lightContrast).toBeDefined();
expect(css.darkContrast).toBeDefined();
// Should use rgb format by default
expect(css.light).toMatch(/^--surface-color: rgb\(/);
expect(css.light).toMatch(/--text-color: rgb\(/);
// Each variant should have two lines (one per color)
expect(css.light.split('\n')).toHaveLength(2);
expect(css.dark.split('\n')).toHaveLength(2);
// Lines should end with semicolons
for (const line of css.light.split('\n')) {
expect(line).toMatch(/;$/);
}
});
it('respects custom format option', () => {
const theme = glaze(280, 80);
theme.colors({ surface: { lightness: 97 } });
const css = theme.css({ format: 'okhsl' });
expect(css.light).toMatch(/--surface-color: okhsl\(/);
const cssHsl = theme.css({ format: 'hsl' });
expect(cssHsl.light).toMatch(/--surface-color: hsl\(/);
const cssOklch = theme.css({ format: 'oklch' });
expect(cssOklch.light).toMatch(/--surface-color: oklch\(/);
});
it('respects custom suffix option', () => {
const theme = glaze(280, 80);
theme.colors({ surface: { lightness: 97 } });
const css = theme.css({ suffix: '' });
expect(css.light).toMatch(/^--surface: rgb\(/);
const cssBg = theme.css({ suffix: '-bg' });
expect(cssBg.light).toMatch(/^--surface-bg: rgb\(/);
});
it('produces different values for light and dark variants', () => {
const theme = glaze(280, 80);
theme.colors({ surface: { lightness: 97 } });
const css = theme.css();
expect(css.light).not.toBe(css.dark);
});