-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpostprocess_test.go
More file actions
894 lines (773 loc) · 24.5 KB
/
postprocess_test.go
File metadata and controls
894 lines (773 loc) · 24.5 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
package glyph
import (
"testing"
"time"
)
func TestEachCell(t *testing.T) {
buf := NewBuffer(4, 3)
buf.Set(0, 0, Cell{Rune: 'A', Style: Style{FG: RGB(255, 0, 0)}})
buf.Set(1, 0, Cell{Rune: 'B', Style: Style{FG: RGB(0, 255, 0)}})
pass := EachCell(func(x, y int, c Cell, _ PostContext) Cell {
c.Style.FG = RGB(0, 0, 255)
return c
})
ctx := PostContext{Width: 4, Height: 3}
pass.Apply(buf, ctx)
got := buf.Get(0, 0)
if got.Style.FG.R != 0 || got.Style.FG.B != 255 {
t.Errorf("EachCell: expected blue FG, got R=%d G=%d B=%d", got.Style.FG.R, got.Style.FG.G, got.Style.FG.B)
}
if got.Rune != 'A' {
t.Errorf("EachCell: expected rune 'A', got %c", got.Rune)
}
}
func TestPipelineOrder(t *testing.T) {
buf := NewBuffer(2, 1)
buf.Set(0, 0, Cell{Rune: 'X', Style: Style{FG: RGB(100, 100, 100)}})
ctx := PostContext{Width: 2, Height: 1}
pass1 := EachCell(func(_, _ int, c Cell, _ PostContext) Cell {
c.Style.FG = RGB(255, 0, 0)
return c
})
pass2 := EachCell(func(_, _ int, c Cell, _ PostContext) Cell {
if c.Style.FG.Mode == ColorRGB {
c.Style.FG.R /= 2
}
return c
})
pass1.Apply(buf, ctx)
pass2.Apply(buf, ctx)
got := buf.Get(0, 0)
if got.Style.FG.R != 127 {
t.Errorf("pipeline order: expected R=127, got R=%d", got.Style.FG.R)
}
}
func TestSEDimAll(t *testing.T) {
buf := NewBuffer(3, 2)
buf.Set(1, 0, Cell{Rune: 'A', Style: Style{FG: RGB(255, 255, 255)}})
pass := SEDimAll()
pass.Apply(buf, PostContext{Width: 3, Height: 2})
got := buf.Get(1, 0)
if !got.Style.Attr.Has(AttrDim) {
t.Error("SEDimAll: expected AttrDim to be set")
}
if got.Rune != 'A' {
t.Errorf("SEDimAll: expected rune 'A', got %c", got.Rune)
}
}
func TestSETint(t *testing.T) {
buf := NewBuffer(1, 1)
buf.Set(0, 0, Cell{Rune: 'X', Style: Style{FG: RGB(200, 200, 200)}})
pass := SETint(RGB(255, 0, 0)).Strength(1.0)
pass.Apply(buf, PostContext{Width: 1, Height: 1})
got := buf.Get(0, 0)
if got.Style.FG.R != 255 || got.Style.FG.G != 0 || got.Style.FG.B != 0 {
t.Errorf("SETint: expected (255,0,0), got (%d,%d,%d)", got.Style.FG.R, got.Style.FG.G, got.Style.FG.B)
}
}
func TestSETintPartial(t *testing.T) {
buf := NewBuffer(1, 1)
buf.Set(0, 0, Cell{Rune: 'X', Style: Style{FG: RGB(100, 100, 100)}})
pass := SETint(RGB(200, 200, 200)).Strength(0.5)
pass.Apply(buf, PostContext{Width: 1, Height: 1})
got := buf.Get(0, 0)
if got.Style.FG.R != 150 {
t.Errorf("SETint partial: expected R=150, got R=%d", got.Style.FG.R)
}
}
func TestSETintProcessesAllModes(t *testing.T) {
// BasicColor(2) = green (R:0, G:170, B:0), tint fully toward blue
buf := NewBuffer(1, 1)
buf.Set(0, 0, Cell{Rune: 'X', Style: Style{FG: BasicColor(2)}})
pass := SETint(RGB(0, 0, 255)).Strength(1.0)
pass.Apply(buf, PostContext{Width: 1, Height: 1})
got := buf.Get(0, 0)
if got.Style.FG.B != 255 {
t.Errorf("SETint: Color16 should be tinted, got (%d,%d,%d)",
got.Style.FG.R, got.Style.FG.G, got.Style.FG.B)
}
// ColorDefault should still be skipped
buf2 := NewBuffer(1, 1)
buf2.Set(0, 0, Cell{Rune: 'X'})
pass.Apply(buf2, PostContext{Width: 1, Height: 1})
got2 := buf2.Get(0, 0)
if got2.Style.FG.Mode != ColorDefault {
t.Error("SETint: should skip ColorDefault")
}
}
func TestSEDesaturate(t *testing.T) {
buf := NewBuffer(1, 1)
buf.Set(0, 0, Cell{Rune: 'X', Style: Style{FG: RGB(255, 0, 0)}})
pass := SEDesaturate().Strength(1.0)
pass.Apply(buf, PostContext{Width: 1, Height: 1})
got := buf.Get(0, 0)
if got.Style.FG.R != got.Style.FG.G || got.Style.FG.G != got.Style.FG.B {
t.Errorf("SEDesaturate: expected equal RGB (gray), got (%d,%d,%d)",
got.Style.FG.R, got.Style.FG.G, got.Style.FG.B)
}
}
func TestSEFocusDim(t *testing.T) {
buf := NewBuffer(10, 5)
for y := range 5 {
for x := range 10 {
buf.Set(x, y, Cell{Rune: 'X', Style: DefaultStyle()})
}
}
ref := NodeRef{X: 2, Y: 1, W: 3, H: 2}
pass := SEFocusDim(&ref)
pass.Apply(buf, PostContext{Width: 10, Height: 5})
if buf.Get(2, 1).Style.Attr.Has(AttrDim) {
t.Error("SEFocusDim: cell inside focus should not be dimmed")
}
if buf.Get(4, 2).Style.Attr.Has(AttrDim) {
t.Error("SEFocusDim: cell inside focus should not be dimmed")
}
if !buf.Get(0, 0).Style.Attr.Has(AttrDim) {
t.Error("SEFocusDim: cell outside focus should be dimmed")
}
if !buf.Get(5, 3).Style.Attr.Has(AttrDim) {
t.Error("SEFocusDim: cell outside focus should be dimmed")
}
}
func TestSEDissolve(t *testing.T) {
buf := NewBuffer(20, 10)
for y := range 10 {
for x := range 20 {
buf.Set(x, y, Cell{Rune: 'X', Style: Style{FG: RGB(255, 255, 255)}})
}
}
progress := 0.5
pass := SEDissolve(&progress)
pass.Apply(buf, PostContext{Width: 20, Height: 10})
dissolved := 0
total := 20 * 10
for y := range 10 {
for x := range 20 {
if buf.Get(x, y).Rune == ' ' {
dissolved++
}
}
}
ratio := float64(dissolved) / float64(total)
if ratio < 0.3 || ratio > 0.7 {
t.Errorf("SEDissolve: expected ~50%% dissolved, got %.1f%% (%d/%d)", ratio*100, dissolved, total)
}
}
func TestSEVignette(t *testing.T) {
buf := NewBuffer(20, 10)
center := RGB(200, 200, 200)
for y := range 10 {
for x := range 20 {
buf.Set(x, y, Cell{Rune: 'X', Style: Style{FG: center}})
}
}
pass := SEVignette().Strength(1.0)
pass.Apply(buf, PostContext{Width: 20, Height: 10})
centerCell := buf.Get(10, 5)
edgeCell := buf.Get(0, 0)
centerLum := int(centerCell.Style.FG.R) + int(centerCell.Style.FG.G) + int(centerCell.Style.FG.B)
edgeLum := int(edgeCell.Style.FG.R) + int(edgeCell.Style.FG.G) + int(edgeCell.Style.FG.B)
if centerLum <= edgeLum {
t.Errorf("SEVignette: center (%d) should be brighter than edge (%d)", centerLum, edgeLum)
}
}
func TestSEVignetteFocus(t *testing.T) {
buf := NewBuffer(40, 20)
grey := RGB(200, 200, 200)
for y := range 20 {
for x := range 40 {
buf.Set(x, y, Cell{Rune: 'X', Style: Style{FG: grey}})
}
}
// ref centred at (30,15) — far from screen centre (20,10)
ref := NodeRef{X: 25, Y: 12, W: 10, H: 6}
SEVignette().Strength(1.0).Focus(&ref).Apply(buf, PostContext{Width: 40, Height: 20})
// cell near ref centre should be brighter than top-left corner
nearRef := buf.Get(30, 15)
corner := buf.Get(0, 0)
nearLum := int(nearRef.Style.FG.R) + int(nearRef.Style.FG.G) + int(nearRef.Style.FG.B)
cornerLum := int(corner.Style.FG.R) + int(corner.Style.FG.G) + int(corner.Style.FG.B)
if nearLum <= cornerLum {
t.Errorf("SEVignette Focus: cell near ref (%d) should be brighter than corner (%d)", nearLum, cornerLum)
}
}
func TestSEVignetteDodge(t *testing.T) {
buf := NewBuffer(40, 20)
grey := RGB(200, 200, 200)
for y := range 20 {
for x := range 40 {
buf.Set(x, y, Cell{Rune: 'X', Style: Style{FG: grey}})
}
}
ref := NodeRef{X: 25, Y: 12, W: 10, H: 6}
SEVignette().Strength(1.0).Dodge(&ref).Apply(buf, PostContext{Width: 40, Height: 20})
// cells inside the dodge ref must be completely unaffected
expected := int(grey.R) + int(grey.G) + int(grey.B)
for y := ref.Y; y < ref.Y+ref.H; y++ {
for x := ref.X; x < ref.X+ref.W; x++ {
c := buf.Get(x, y)
lum := int(c.Style.FG.R) + int(c.Style.FG.G) + int(c.Style.FG.B)
if lum != expected {
t.Errorf("SEVignette Dodge: cell inside ref (%d,%d) should be unaffected, got lum %d want %d", x, y, lum, expected)
}
}
}
}
func TestSEVignetteFocusCorner(t *testing.T) {
// focus node near a corner — previously produced a tiny maxDist and near-black screen
buf := NewBuffer(80, 24)
grey := RGB(200, 200, 200)
for y := range 24 {
for x := range 80 {
buf.Set(x, y, Cell{Rune: 'X', Style: Style{FG: grey}})
}
}
ref := NodeRef{X: 2, Y: 2, W: 10, H: 5}
SEVignette().Strength(0.9).Focus(&ref).Apply(buf, PostContext{Width: 80, Height: 24})
// the screen center should still have meaningful brightness, not be black
center := buf.Get(40, 12)
centerLum := int(center.Style.FG.R) + int(center.Style.FG.G) + int(center.Style.FG.B)
if centerLum < 300 {
t.Errorf("SEVignette corner Focus: screen center should not be black, got lum %d", centerLum)
}
}
func TestQuantizeUint8Overflow(t *testing.T) {
// step=32: 255 rounds up to 256, which previously wrapped to 0
if got := quantizeUint8(255, 32); got != 255 {
t.Errorf("quantizeUint8(255, 32) = %d, want 255", got)
}
if got := quantizeUint8(241, 32); got != 255 {
t.Errorf("quantizeUint8(241, 32) = %d, want 255", got)
}
// step=16: 255 rounds to 256 → should clamp
if got := quantizeUint8(255, 16); got != 255 {
t.Errorf("quantizeUint8(255, 16) = %d, want 255", got)
}
// normal case: 128 with step 32 → 128
if got := quantizeUint8(128, 32); got != 128 {
t.Errorf("quantizeUint8(128, 32) = %d, want 128", got)
}
}
func TestSEDropShadow(t *testing.T) {
buf := NewBuffer(40, 20)
grey := RGB(200, 200, 200)
for y := range 20 {
for x := range 40 {
buf.Set(x, y, Cell{Rune: 'X', Style: Style{FG: grey}})
}
}
ref := NodeRef{X: 10, Y: 5, W: 10, H: 5}
SEDropShadow().Strength(1.0).Radius(6).Focus(&ref).Apply(buf, PostContext{Width: 40, Height: 20})
// cells inside the focus ref must be completely unaffected
expected := int(grey.R) + int(grey.G) + int(grey.B)
for y := ref.Y; y < ref.Y+ref.H; y++ {
for x := ref.X; x < ref.X+ref.W; x++ {
c := buf.Get(x, y)
lum := int(c.Style.FG.R) + int(c.Style.FG.G) + int(c.Style.FG.B)
if lum != expected {
t.Errorf("SEDropShadow: cell inside ref (%d,%d) should be unaffected, got lum %d want %d", x, y, lum, expected)
}
}
}
// cell just outside the ref should be darker than one far away
near := buf.Get(ref.X+ref.W, ref.Y+ref.H/2) // immediately right of ref
far := buf.Get(39, 10)
nearLum := int(near.Style.FG.R) + int(near.Style.FG.G) + int(near.Style.FG.B)
farLum := int(far.Style.FG.R) + int(far.Style.FG.G) + int(far.Style.FG.B)
if nearLum >= farLum {
t.Errorf("SEDropShadow: cell near ref (%d) should be darker than far cell (%d)", nearLum, farLum)
}
}
func TestSEPulse(t *testing.T) {
buf := NewBuffer(2, 1)
buf.Set(0, 0, Cell{Rune: 'X', Style: Style{FG: RGB(200, 200, 200)}})
// at time=0, sin(0)=0, t=0.5, dim=0.5*0.5=0.25
pass := SEPulse().Speed(1.0).Strength(0.5)
pass.Apply(buf, PostContext{Width: 2, Height: 1, Time: 0})
got := buf.Get(0, 0)
// should be dimmed somewhat from 200
if got.Style.FG.R >= 200 {
t.Errorf("SEPulse: expected dimming at t=0, got R=%d", got.Style.FG.R)
}
if got.Style.FG.R == 0 {
t.Error("SEPulse: should not be fully black")
}
}
func TestSEScreenShake(t *testing.T) {
buf := NewBuffer(10, 1)
for x := range 10 {
buf.Set(x, 0, Cell{Rune: rune('A' + x), Style: DefaultStyle()})
}
// frame chosen so sin(frame*1.5) gives a non-zero offset
pass := SEScreenShake(3.0)
pass.Apply(buf, PostContext{Width: 10, Height: 1, Frame: 1})
// at least some cells should have shifted
unchanged := 0
for x := range 10 {
if buf.Get(x, 0).Rune == rune('A'+x) {
unchanged++
}
}
if unchanged == 10 {
t.Error("SEScreenShake: expected some cells to shift")
}
}
func TestSEGradientMap(t *testing.T) {
buf := NewBuffer(3, 1)
buf.Set(0, 0, Cell{Rune: 'D', Style: Style{FG: RGB(30, 30, 30)}}) // dark
buf.Set(1, 0, Cell{Rune: 'M', Style: Style{FG: RGB(128, 128, 128)}}) // mid
buf.Set(2, 0, Cell{Rune: 'B', Style: Style{FG: RGB(230, 230, 230)}}) // bright
pass := SEGradientMap(RGB(0, 0, 50), RGB(0, 128, 128), RGB(200, 255, 200))
pass.Apply(buf, PostContext{Width: 3, Height: 1})
dark := buf.Get(0, 0)
mid := buf.Get(1, 0)
bright := buf.Get(2, 0)
// dark cell should be near the dark stop (blue-ish)
if dark.Style.FG.B < dark.Style.FG.R {
t.Errorf("SEGradientMap: dark cell should lean blue, got (%d,%d,%d)",
dark.Style.FG.R, dark.Style.FG.G, dark.Style.FG.B)
}
// mid cell should be near teal
if mid.Style.FG.G < 100 {
t.Errorf("SEGradientMap: mid cell should have green, got G=%d", mid.Style.FG.G)
}
// bright cell should be near the bright stop (green-white)
if bright.Style.FG.G < 200 {
t.Errorf("SEGradientMap: bright cell should lean green-white, got G=%d", bright.Style.FG.G)
}
}
func TestSEBloom(t *testing.T) {
buf := NewBuffer(5, 1)
// dark cells with one bright cell in the middle
for x := range 5 {
buf.Set(x, 0, Cell{Rune: 'X', Style: Style{FG: RGB(20, 20, 20)}})
}
buf.Set(2, 0, Cell{Rune: 'X', Style: Style{FG: RGB(255, 255, 255)}})
pass := SEBloom().Threshold(0.5).Strength(0.8)
pass.Apply(buf, PostContext{Width: 5, Height: 1})
// neighbours of the bright cell should be brighter than they started
left := buf.Get(1, 0)
right := buf.Get(3, 0)
if left.Style.FG.R <= 20 {
t.Errorf("SEBloom: left neighbour should be brighter, got R=%d", left.Style.FG.R)
}
if right.Style.FG.R <= 20 {
t.Errorf("SEBloom: right neighbour should be brighter, got R=%d", right.Style.FG.R)
}
}
func TestSEBloomSkipsDark(t *testing.T) {
buf := NewBuffer(3, 1)
for x := range 3 {
buf.Set(x, 0, Cell{Rune: 'X', Style: Style{FG: RGB(30, 30, 30)}})
}
pass := SEBloom().Threshold(0.5).Strength(1.0)
pass.Apply(buf, PostContext{Width: 3, Height: 1})
// all cells dark, nothing should bloom
got := buf.Get(1, 0)
if got.Style.FG.R != 30 {
t.Errorf("SEBloom: all-dark buffer should be unchanged, got R=%d", got.Style.FG.R)
}
}
func TestSEMonochrome(t *testing.T) {
buf := NewBuffer(1, 1)
buf.Set(0, 0, Cell{Rune: 'X', Style: Style{FG: RGB(255, 0, 0)}})
// green phosphor monochrome
pass := SEMonochrome(RGB(0, 255, 0))
pass.Apply(buf, PostContext{Width: 1, Height: 1})
got := buf.Get(0, 0)
// red input → luminance ~76 (0.299*255) → green output
if got.Style.FG.R != 0 {
t.Errorf("SEMonochrome: R should be 0 with green tint, got %d", got.Style.FG.R)
}
if got.Style.FG.G == 0 {
t.Error("SEMonochrome: G should be non-zero with green tint")
}
if got.Style.FG.B != 0 {
t.Errorf("SEMonochrome: B should be 0 with green tint, got %d", got.Style.FG.B)
}
}
func TestSEMonochromeAmber(t *testing.T) {
buf := NewBuffer(1, 1)
buf.Set(0, 0, Cell{Rune: 'X', Style: Style{FG: RGB(200, 200, 200)}})
pass := SEMonochrome(RGB(255, 180, 0))
pass.Apply(buf, PostContext{Width: 1, Height: 1})
got := buf.Get(0, 0)
// should have R > G > B=0
if got.Style.FG.R <= got.Style.FG.G {
t.Errorf("SEMonochrome amber: expected R > G, got R=%d G=%d", got.Style.FG.R, got.Style.FG.G)
}
if got.Style.FG.B != 0 {
t.Errorf("SEMonochrome amber: B should be 0, got %d", got.Style.FG.B)
}
}
func TestBlendMultiply(t *testing.T) {
a := RGB(200, 100, 50)
b := RGB(128, 128, 128)
result := BlendColor(a, b, BlendMultiply)
// multiply darkens: 200*128/255 ≈ 100
if result.R >= 200 {
t.Errorf("BlendMultiply: expected darkened R, got %d", result.R)
}
}
func TestBlendScreen(t *testing.T) {
a := RGB(100, 100, 100)
b := RGB(100, 100, 100)
result := BlendColor(a, b, BlendScreen)
// screen lightens
if result.R <= 100 {
t.Errorf("BlendScreen: expected brighter R, got %d", result.R)
}
}
func TestBlendOverlay(t *testing.T) {
// overlay with a bright top pushes dark bases darker, light bases lighter
dark := BlendColor(RGB(50, 50, 50), RGB(200, 200, 200), BlendOverlay)
light := BlendColor(RGB(200, 200, 200), RGB(200, 200, 200), BlendOverlay)
if dark.R >= 100 {
t.Errorf("BlendOverlay: dark base with bright top should stay low, got R=%d", dark.R)
}
if light.R <= 200 {
t.Errorf("BlendOverlay: light base with bright top should get brighter, got R=%d", light.R)
}
}
func TestBlendProcessesAllModes(t *testing.T) {
// BasicColor(1) = red (170,0,0), top = (128,128,128)
// multiply: 170*128/255 ≈ 85
base := BasicColor(1)
top := RGB(128, 128, 128)
result := BlendColor(base, top, BlendMultiply)
if result.R >= 128 {
t.Errorf("BlendColor: Color16 should be blended, got R=%d", result.R)
}
// ColorDefault should still be skipped
result2 := BlendColor(Color{}, RGB(255, 0, 0), BlendMultiply)
if result2.R != 255 {
t.Errorf("BlendColor: should return top for ColorDefault base, got R=%d", result2.R)
}
}
func TestWithBlend(t *testing.T) {
buf := NewBuffer(2, 1)
buf.Set(0, 0, Cell{Rune: 'A', Style: Style{FG: RGB(200, 200, 200)}})
// effect that sets FG to mid-grey
effect := EachCell(func(_, _ int, c Cell, _ PostContext) Cell {
c.Style.FG = RGB(128, 128, 128)
return c
})
pass := WithBlend(BlendMultiply, effect)
pass.Apply(buf, PostContext{Width: 2, Height: 1})
got := buf.Get(0, 0)
// multiply: 200*128/255 ≈ 100, should be darker than both inputs
if got.Style.FG.R >= 128 {
t.Errorf("WithBlend(Multiply): expected R < 128, got %d", got.Style.FG.R)
}
}
func TestWithBlendTint(t *testing.T) {
buf := NewBuffer(5, 3)
for y := range 3 {
for x := range 5 {
buf.Set(x, y, Cell{Rune: 'X', Style: Style{FG: RGB(200, 200, 200)}})
}
}
pass := WithBlend(BlendMultiply, SETint(RGB(128, 128, 128)).Strength(1.0))
pass.Apply(buf, PostContext{Width: 5, Height: 3})
// tint through multiply should be darker than original
got := buf.Get(2, 1)
lum := int(got.Style.FG.R) + int(got.Style.FG.G) + int(got.Style.FG.B)
if lum >= 600 {
t.Errorf("WithBlend(Multiply, Tint): expected darkened output, got lum=%d", lum)
}
}
func TestScreenEffectInTree(t *testing.T) {
called := false
effect := funcEffect(func(buf *Buffer, ctx PostContext) { called = true })
tmpl := Build(VBox(
Text("hello"),
ScreenEffect(effect),
))
buf := NewBuffer(20, 5)
tmpl.Execute(buf, 20, 5)
effects := tmpl.ScreenEffects()
if len(effects) != 1 {
t.Fatalf("expected 1 screen effect, got %d", len(effects))
}
// run the collected effect
effects[0].Apply(buf, PostContext{Width: 20, Height: 5})
if !called {
t.Error("screen effect was not called")
}
}
func TestScreenEffectWithIf(t *testing.T) {
active := false
effect := funcEffect(func(buf *Buffer, ctx PostContext) {})
tmpl := Build(VBox(
Text("hello"),
If(&active).Then(ScreenEffect(effect)),
))
buf := NewBuffer(20, 5)
// inactive, no effects collected
tmpl.Execute(buf, 20, 5)
if len(tmpl.ScreenEffects()) != 0 {
t.Errorf("expected 0 effects when inactive, got %d", len(tmpl.ScreenEffects()))
}
// activate, effect collected
active = true
tmpl.Execute(buf, 20, 5)
if len(tmpl.ScreenEffects()) != 1 {
t.Errorf("expected 1 effect when active, got %d", len(tmpl.ScreenEffects()))
}
// deactivate, back to 0
active = false
tmpl.Execute(buf, 20, 5)
if len(tmpl.ScreenEffects()) != 0 {
t.Errorf("expected 0 effects when deactivated, got %d", len(tmpl.ScreenEffects()))
}
}
func TestScreenEffectMultiple(t *testing.T) {
order := make([]int, 0, 3)
e1 := funcEffect(func(buf *Buffer, ctx PostContext) { order = append(order, 1) })
e2 := funcEffect(func(buf *Buffer, ctx PostContext) { order = append(order, 2) })
e3 := funcEffect(func(buf *Buffer, ctx PostContext) { order = append(order, 3) })
tmpl := Build(VBox(
ScreenEffect(e1),
Text("content"),
ScreenEffect(e2),
ScreenEffect(e3),
))
buf := NewBuffer(20, 5)
tmpl.Execute(buf, 20, 5)
effects := tmpl.ScreenEffects()
if len(effects) != 3 {
t.Fatalf("expected 3 effects, got %d", len(effects))
}
for _, e := range effects {
e.Apply(buf, PostContext{})
}
if len(order) != 3 || order[0] != 1 || order[1] != 2 || order[2] != 3 {
t.Errorf("effects should run in tree order, got %v", order)
}
}
func TestScreenEffectZeroLayoutSpace(t *testing.T) {
tmpl := Build(VBox(
Text("line1"),
ScreenEffect(funcEffect(func(*Buffer, PostContext) {})),
Text("line2"),
))
buf := NewBuffer(20, 5)
tmpl.Execute(buf, 20, 5)
// line2 should be on row 1, not row 2. ScreenEffect takes no space
c := buf.Get(0, 1)
if c.Rune != 'l' {
t.Errorf("ScreenEffect should take zero layout space, got rune %c at (0,1)", c.Rune)
}
}
func BenchmarkScreenEffectCollection(b *testing.B) {
effect := funcEffect(func(buf *Buffer, ctx PostContext) {})
active := true
tmpl := Build(VBox(
Text("content"),
If(&active).Then(ScreenEffect(effect)),
))
buf := NewBuffer(80, 24)
b.ReportAllocs()
b.ResetTimer()
for range b.N {
tmpl.Execute(buf, 80, 24)
}
}
func TestPostContextFields(t *testing.T) {
ctx := PostContext{
Width: 80,
Height: 24,
Frame: 100,
Delta: 16 * time.Millisecond,
Time: 5 * time.Second,
}
if ctx.Width != 80 || ctx.Height != 24 {
t.Error("PostContext dimensions wrong")
}
if ctx.Frame != 100 {
t.Error("PostContext frame wrong")
}
if ctx.Delta != 16*time.Millisecond {
t.Error("PostContext delta wrong")
}
}
func TestParseOSCColor(t *testing.T) {
tests := []struct {
name string
data string
digit byte
wantR uint8
wantG uint8
wantB uint8
wantMode ColorMode
}{
{
name: "4-digit hex (xterm style)",
data: "\x1b]10;rgb:ffff/aaaa/0000\x1b\\",
digit: '0',
wantR: 255, wantG: 170, wantB: 0,
wantMode: ColorRGB,
},
{
name: "2-digit hex",
data: "\x1b]10;rgb:ff/80/40\x1b\\",
digit: '0',
wantR: 255, wantG: 128, wantB: 64,
wantMode: ColorRGB,
},
{
name: "OSC 11 (BG)",
data: "\x1b]11;rgb:1a1a/1a1a/2e2e\x1b\\",
digit: '1',
wantR: 26, wantG: 26, wantB: 46,
wantMode: ColorRGB,
},
{
name: "both FG and BG in one response",
data: "\x1b]10;rgb:cccc/bbbb/aaaa\x1b\\\x1b]11;rgb:1111/2222/3333\x1b\\",
digit: '1',
wantR: 17, wantG: 34, wantB: 51,
wantMode: ColorRGB,
},
{
name: "BEL terminator",
data: "\x1b]10;rgb:8080/4040/c0c0\x07",
digit: '0',
wantR: 128, wantG: 64, wantB: 192,
wantMode: ColorRGB,
},
{
name: "no match returns default",
data: "garbage",
digit: '0',
wantMode: ColorDefault,
},
{
name: "empty data",
data: "",
digit: '0',
wantMode: ColorDefault,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := parseOSCColor([]byte(tt.data), tt.digit)
if got.Mode != tt.wantMode {
t.Errorf("Mode: got %d, want %d", got.Mode, tt.wantMode)
}
if tt.wantMode == ColorRGB {
if got.R != tt.wantR || got.G != tt.wantG || got.B != tt.wantB {
t.Errorf("RGB: got (%d,%d,%d), want (%d,%d,%d)",
got.R, got.G, got.B, tt.wantR, tt.wantG, tt.wantB)
}
}
})
}
}
func TestParseOSC4Color(t *testing.T) {
tests := []struct {
name string
data string
index int
wantR uint8
wantG uint8
wantB uint8
wantMode ColorMode
}{
{
name: "index 2 (green) 4-digit hex",
data: "\x1b]4;2;rgb:4e4e/d2d2/8e8e\x07",
index: 2,
wantR: 78, wantG: 210, wantB: 142,
wantMode: ColorRGB,
},
{
name: "index 3 (yellow) 2-digit hex",
data: "\x1b]4;3;rgb:cc/88/00\x07",
index: 3,
wantR: 204, wantG: 136, wantB: 0,
wantMode: ColorRGB,
},
{
name: "index 11 (bright yellow) double-digit index",
data: "\x1b]4;11;rgb:ffff/ffff/0000\x1b\\",
index: 11,
wantR: 255, wantG: 255, wantB: 0,
wantMode: ColorRGB,
},
{
name: "multiple palette entries in one response",
data: "\x1b]4;0;rgb:0000/0000/0000\x07\x1b]4;1;rgb:cccc/0000/0000\x07\x1b]4;2;rgb:0000/cccc/0000\x07",
index: 1,
wantR: 204, wantG: 0, wantB: 0,
wantMode: ColorRGB,
},
{
name: "no match for requested index",
data: "\x1b]4;5;rgb:aaaa/bbbb/cccc\x07",
index: 3,
wantMode: ColorDefault,
},
{
name: "empty data",
data: "",
index: 0,
wantMode: ColorDefault,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := parseOSC4Color([]byte(tt.data), tt.index)
if got.Mode != tt.wantMode {
t.Errorf("Mode: got %d, want %d", got.Mode, tt.wantMode)
}
if tt.wantMode == ColorRGB {
if got.R != tt.wantR || got.G != tt.wantG || got.B != tt.wantB {
t.Errorf("RGB: got (%d,%d,%d), want (%d,%d,%d)",
got.R, got.G, got.B, tt.wantR, tt.wantG, tt.wantB)
}
}
})
}
}
func TestRefreshBasic16Vars(t *testing.T) {
// save original
origGreen := basic16RGB[2]
defer func() {
basic16RGB[2] = origGreen
refreshBasic16Vars()
}()
// simulate OSC 4 detection changing green to a themed value
basic16RGB[2] = [3]uint8{78, 210, 142}
refreshBasic16Vars()
if Green.R != 78 || Green.G != 210 || Green.B != 142 {
t.Errorf("Green after refresh: got (%d,%d,%d), want (78,210,142)",
Green.R, Green.G, Green.B)
}
if Green.Mode != Color16 || Green.Index != 2 {
t.Errorf("Green should stay Color16 index 2, got mode=%d index=%d",
Green.Mode, Green.Index)
}
}
func TestResolveFG(t *testing.T) {
detected := PostContext{
DefaultFG: RGB(200, 150, 100),
DefaultBG: RGB(10, 10, 30),
}
// ColorDefault with detected → uses detected
c := resolveFG(Color{}, detected)
if c.R != 200 || c.G != 150 || c.B != 100 {
t.Errorf("resolveFG(default, detected): got (%d,%d,%d)", c.R, c.G, c.B)
}
// ColorDefault without detection → returns as-is
c = resolveFG(Color{}, PostContext{})
if c.Mode != ColorDefault {
t.Error("resolveFG(default, undetected): should return ColorDefault")
}
// non-default → unchanged
orig := RGB(255, 0, 0)
c = resolveFG(orig, detected)
if c.R != 255 || c.G != 0 || c.B != 0 {
t.Errorf("resolveFG(explicit): should pass through, got (%d,%d,%d)", c.R, c.G, c.B)
}
}