Repository navigation
Expand file tree
/
Copy pathdeath-clock.test.js
More file actions
1688 lines (1424 loc) · 58.1 KB
/
Copy pathdeath-clock.test.js
File metadata and controls
1688 lines (1424 loc) · 58.1 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
'use strict';
/**
* Unit tests for death-clock-core.js
*
* Run with: npm test
*/
const core = require('../death-clock-core');
const milestonesData = require('../milestones-data');
const {
formatTokenCount,
formatTokenCountShort,
getTriggeredMilestones,
getNextMilestone,
predictMilestoneDate,
calculateEnvironmentalImpact,
generateProjectionData,
formatDate,
getTimeDelta,
milestoneProgress,
getRateAtDate,
calculateTipImpact,
TOKEN_TIPS,
generateEquivalences,
calculatePersonalFootprint,
sessionEquivalences,
getNextMilestoneForPlayer,
computeComboMultiplier,
getSessionChallenges,
formatDoomPoints,
MILESTONES,
HISTORICAL_DATA,
RATE_SCHEDULE,
SESSION_CHALLENGE_DEFS,
BASE_TOKENS,
TOKENS_PER_SECOND,
BASE_DATE_ISO,
getSimulatedViewerCount,
getDynamicRate,
RATE_GROWTH_PER_YEAR,
computeExtinctionSecsRemaining,
} = core;
// ============================================================
// formatTokenCount
// ============================================================
describe('formatTokenCount', () => {
test('formats quintillion values', () => {
expect(formatTokenCount(2e18)).toBe('2.00 Quintillion');
});
test('formats quadrillion values', () => {
expect(formatTokenCount(1.5e15)).toBe('1.50 Quadrillion');
});
test('formats trillion values', () => {
expect(formatTokenCount(3.75e12)).toBe('3.75 Trillion');
});
test('formats billion values', () => {
expect(formatTokenCount(2.5e9)).toBe('2.50 Billion');
});
test('formats million values', () => {
expect(formatTokenCount(1.23e6)).toBe('1.23 Million');
});
test('formats thousand values', () => {
expect(formatTokenCount(5000)).toContain('K');
});
test('formats small numbers', () => {
expect(formatTokenCount(42)).toBe('42');
});
test('returns 0 for NaN', () => {
expect(formatTokenCount(NaN)).toBe('0');
});
test('returns 0 for non-numeric input', () => {
expect(formatTokenCount('hello')).toBe('0');
});
test('handles negative numbers', () => {
expect(formatTokenCount(-1e12)).toContain('Trillion');
expect(formatTokenCount(-1e12)).toContain('-');
});
test('handles zero', () => {
expect(formatTokenCount(0)).toBe('0');
});
test('handles Infinity', () => {
expect(formatTokenCount(Infinity)).toBe('0');
});
});
// ============================================================
// formatTokenCountShort
// ============================================================
describe('formatTokenCountShort', () => {
test('quadrillion abbreviation', () => {
expect(formatTokenCountShort(2e15)).toContain('Q');
});
test('trillion abbreviation', () => {
expect(formatTokenCountShort(5e12)).toContain('T');
});
test('billion abbreviation', () => {
expect(formatTokenCountShort(3e9)).toContain('B');
});
test('million abbreviation', () => {
expect(formatTokenCountShort(7e6)).toContain('M');
});
test('handles NaN', () => {
expect(formatTokenCountShort(NaN)).toBe('0');
});
test('handles small numbers (below 1 million)', () => {
expect(formatTokenCountShort(999)).toBe('999');
expect(formatTokenCountShort(42)).toBe('42');
});
test('quintillion abbreviation', () => {
expect(formatTokenCountShort(3e18)).toContain("Q'l");
});
});
// ============================================================
// getTriggeredMilestones
// ============================================================
describe('getTriggeredMilestones', () => {
const simpleMilestones = [
{ id: 'a', tokens: 100 },
{ id: 'b', tokens: 1000 },
{ id: 'c', tokens: 10000 },
];
test('returns empty array when no milestones triggered', () => {
expect(getTriggeredMilestones(50, simpleMilestones)).toHaveLength(0);
});
test('returns one milestone when exactly at threshold', () => {
const result = getTriggeredMilestones(100, simpleMilestones);
expect(result).toHaveLength(1);
expect(result[0].id).toBe('a');
});
test('returns all triggered milestones', () => {
expect(getTriggeredMilestones(5000, simpleMilestones)).toHaveLength(2);
});
test('returns all milestones when above highest threshold', () => {
expect(getTriggeredMilestones(99999, simpleMilestones)).toHaveLength(3);
});
test('handles invalid tokens', () => {
expect(getTriggeredMilestones('bad', simpleMilestones)).toEqual([]);
});
test('handles non-array milestones', () => {
expect(getTriggeredMilestones(100, null)).toEqual([]);
});
test('works with real MILESTONES data and a high token count', () => {
const triggered = getTriggeredMilestones(BASE_TOKENS, MILESTONES);
expect(triggered.length).toBeGreaterThan(0);
});
});
// ============================================================
// getNextMilestone
// ============================================================
describe('getNextMilestone', () => {
const simpleMilestones = [
{ id: 'a', tokens: 100 },
{ id: 'b', tokens: 1000 },
{ id: 'c', tokens: 10000 },
];
test('returns first milestone when tokens is 0', () => {
expect(getNextMilestone(0, simpleMilestones).id).toBe('a');
});
test('returns correct next milestone mid-series', () => {
expect(getNextMilestone(500, simpleMilestones).id).toBe('b');
});
test('returns null when all milestones passed', () => {
expect(getNextMilestone(99999, simpleMilestones)).toBeNull();
});
test('handles invalid inputs', () => {
expect(getNextMilestone(null, null)).toBeNull();
});
});
// ============================================================
// predictMilestoneDate
// ============================================================
describe('predictMilestoneDate', () => {
const now = new Date('2026-01-01T00:00:00Z');
test('predicts future date when target not yet reached', () => {
const date = predictMilestoneDate(0, 1_000_000, 1_000_000_000, now);
expect(date).toBeInstanceOf(Date);
expect(date > now).toBe(true);
});
test('returns null when target already reached', () => {
expect(predictMilestoneDate(1000, 100, 500, now)).toBeNull();
});
test('returns null when rate is zero', () => {
expect(predictMilestoneDate(0, 0, 1000, now)).toBeNull();
});
test('returns null when rate is negative', () => {
expect(predictMilestoneDate(0, -100, 1000, now)).toBeNull();
});
test('calculates correct duration', () => {
// 1000 tokens needed, at 1 per second → 1000 seconds in future
const date = predictMilestoneDate(0, 1, 1000, now);
const expectedMs = now.getTime() + 1000 * 1000;
expect(Math.abs(date.getTime() - expectedMs)).toBeLessThan(100);
});
test('handles non-numeric inputs gracefully', () => {
expect(predictMilestoneDate('a', 'b', 'c', now)).toBeNull();
});
});
// ============================================================
// calculateEnvironmentalImpact
// ============================================================
describe('calculateEnvironmentalImpact', () => {
test('returns zeros for 0 tokens', () => {
const r = calculateEnvironmentalImpact(0);
expect(r.kWh).toBe(0);
expect(r.co2Kg).toBe(0);
expect(r.waterL).toBe(0);
expect(r.treesEquivalent).toBe(0);
});
test('returns zeros for negative tokens', () => {
const r = calculateEnvironmentalImpact(-100);
expect(r.kWh).toBe(0);
});
test('returns zeros for non-numeric input', () => {
const r = calculateEnvironmentalImpact('abc');
expect(r.kWh).toBe(0);
});
test('kWh scales linearly with tokens', () => {
const r1 = calculateEnvironmentalImpact(1000);
const r2 = calculateEnvironmentalImpact(2000);
expect(r2.kWh).toBeCloseTo(r1.kWh * 2, 10);
});
test('CO₂ is derived from kWh at 0.4 kg/kWh', () => {
const r = calculateEnvironmentalImpact(1_000_000);
expect(r.co2Kg).toBeCloseTo(r.kWh * 0.4, 8);
});
test('water is 0.5L per 1000 tokens', () => {
const r = calculateEnvironmentalImpact(1000);
expect(r.waterL).toBeCloseTo(0.5, 8);
});
test('trees equivalent uses 21 kg CO₂/tree/year', () => {
const r = calculateEnvironmentalImpact(1_000_000_000_000); // 1T tokens
expect(r.treesEquivalent).toBeCloseTo(r.co2Kg / 21, 5);
});
test('returns valid numbers for very large token counts', () => {
const r = calculateEnvironmentalImpact(1e17);
expect(isFinite(r.kWh)).toBe(true);
expect(isFinite(r.co2Kg)).toBe(true);
});
});
// ============================================================
// generateProjectionData
// ============================================================
describe('generateProjectionData', () => {
const now = new Date('2026-01-01T00:00:00Z');
test('returns correct number of data points', () => {
const data = generateProjectionData(0, 1_000_000, 6, now);
expect(data).toHaveLength(7); // 0..6 inclusive
});
test('first point corresponds to current tokens', () => {
const data = generateProjectionData(1e15, 1_000_000, 3, now);
expect(data[0].tokensT).toBeCloseTo(1e15 / 1e12, 2);
});
test('subsequent points are larger', () => {
const data = generateProjectionData(0, 1_000_000, 4, now);
for (let i = 1; i < data.length; i++) {
expect(data[i].tokensT).toBeGreaterThanOrEqual(data[i - 1].tokensT);
}
});
test('returns empty array for invalid inputs', () => {
expect(generateProjectionData('a', 'b', 'c', now)).toEqual([]);
});
test('returns empty array for negative months', () => {
expect(generateProjectionData(0, 1000, -1, now)).toEqual([]);
});
test('each point has date and tokensT properties', () => {
const data = generateProjectionData(0, 1_000_000, 2, now);
data.forEach((d) => {
expect(d).toHaveProperty('date');
expect(d).toHaveProperty('tokensT');
expect(typeof d.date).toBe('string');
expect(typeof d.tokensT).toBe('number');
});
});
});
// ============================================================
// formatDate
// ============================================================
describe('formatDate', () => {
test('formats a valid date', () => {
const d = new Date('2026-06-15T00:00:00Z');
const result = formatDate(d);
expect(result).toContain('2026');
expect(result).toContain('June');
});
test('returns Unknown for null', () => {
expect(formatDate(null)).toBe('Unknown');
});
test('returns Unknown for non-Date input', () => {
expect(formatDate('2026-01-01')).toBe('Unknown');
});
test('returns Unknown for invalid date', () => {
expect(formatDate(new Date('not-a-date'))).toBe('Unknown');
});
});
// ============================================================
// getTimeDelta
// ============================================================
describe('getTimeDelta', () => {
const now = new Date('2026-01-01T00:00:00Z');
test('returns "Already passed" for past date', () => {
const past = new Date('2025-06-01T00:00:00Z');
expect(getTimeDelta(past, now)).toBe('Already passed');
});
test('returns years for far future', () => {
const future = new Date('2030-01-01T00:00:00Z');
const result = getTimeDelta(future, now);
expect(result).toContain('year');
});
test('returns months for medium future', () => {
const future = new Date('2026-04-01T00:00:00Z');
const result = getTimeDelta(future, now);
expect(result).toMatch(/month|day/);
});
test('returns days for near future', () => {
const future = new Date(now.getTime() + 5 * 24 * 60 * 60 * 1000); // 5 days later
const result = getTimeDelta(future, now);
expect(result).toContain('day');
});
test('handles invalid date', () => {
expect(getTimeDelta(null, now)).toBe('');
expect(getTimeDelta('bad', now)).toBe('');
});
});
// ============================================================
// milestoneProgress
// ============================================================
describe('milestoneProgress', () => {
test('returns 0 when at start', () => {
expect(milestoneProgress(0, 0, 1000)).toBe(0);
});
test('returns 50 at midpoint', () => {
expect(milestoneProgress(500, 0, 1000)).toBeCloseTo(50);
});
test('returns 100 at or past target', () => {
expect(milestoneProgress(1000, 0, 1000)).toBe(100);
expect(milestoneProgress(2000, 0, 1000)).toBe(100);
});
test('returns 100 when next <= prev', () => {
expect(milestoneProgress(500, 1000, 500)).toBe(100);
});
test('does not go below 0', () => {
expect(milestoneProgress(-100, 0, 1000)).toBe(0);
});
});
// ============================================================
// Constants sanity checks
// ============================================================
describe('Constants', () => {
test('BASE_TOKENS is a positive number', () => {
expect(typeof BASE_TOKENS).toBe('number');
expect(BASE_TOKENS).toBeGreaterThan(0);
});
test('TOKENS_PER_SECOND is a positive number', () => {
expect(typeof TOKENS_PER_SECOND).toBe('number');
expect(TOKENS_PER_SECOND).toBeGreaterThan(0);
});
test('MILESTONES is a non-empty array', () => {
expect(Array.isArray(MILESTONES)).toBe(true);
expect(MILESTONES.length).toBeGreaterThan(0);
});
test('each milestone has required fields', () => {
MILESTONES.forEach((m) => {
expect(m).toHaveProperty('id');
expect(m).toHaveProperty('name');
expect(m).toHaveProperty('tokens');
expect(m).toHaveProperty('description');
expect(m).toHaveProperty('consequence');
expect(m).toHaveProperty('followingEvent');
expect(typeof m.tokens).toBe('number');
expect(m.tokens).toBeGreaterThan(0);
});
});
test('milestones are in ascending token order', () => {
for (let i = 1; i < MILESTONES.length; i++) {
expect(MILESTONES[i].tokens).toBeGreaterThan(MILESTONES[i - 1].tokens);
}
});
test('at most one milestone has extinctionMarker: true', () => {
const marked = MILESTONES.filter((m) => m.extinctionMarker === true);
expect(marked.length).toBeLessThanOrEqual(1);
});
test('HISTORICAL_DATA is a non-empty array', () => {
expect(Array.isArray(HISTORICAL_DATA)).toBe(true);
expect(HISTORICAL_DATA.length).toBeGreaterThan(0);
});
test('each HISTORICAL_DATA point has date and tokensT', () => {
HISTORICAL_DATA.forEach((d) => {
expect(d).toHaveProperty('date');
expect(d).toHaveProperty('tokensT');
expect(typeof d.date).toBe('string');
expect(typeof d.tokensT).toBe('number');
});
});
test('HISTORICAL_DATA dates are in ascending order', () => {
for (let i = 1; i < HISTORICAL_DATA.length; i++) {
expect(new Date(HISTORICAL_DATA[i].date).getTime()).toBeGreaterThan(
new Date(HISTORICAL_DATA[i - 1].date).getTime()
);
}
});
});
// ============================================================
// Browser window export (lines 443-444)
// ============================================================
describe('Browser window export', () => {
test('sets window.DeathClockCore when module is not available', () => {
const vm = require('vm');
const fs = require('fs');
const path = require('path');
const coreCode = fs.readFileSync(path.join(__dirname, '../death-clock-core.js'), 'utf8');
// Run in a sandbox where `module` is undefined and `window` is available,
// with MilestonesData pre-loaded so the MILESTONES branch is fully exercised.
const sandboxWindow = { MilestonesData: milestonesData };
vm.runInNewContext(coreCode, { window: sandboxWindow });
expect(typeof sandboxWindow.DeathClockCore).toBe('object');
expect(typeof sandboxWindow.DeathClockCore.formatTokenCount).toBe('function');
});
test('MILESTONES loads from window.MilestonesData in browser context', () => {
const vm = require('vm');
const fs = require('fs');
const path = require('path');
const coreCode = fs.readFileSync(path.join(__dirname, '../death-clock-core.js'), 'utf8');
const sandboxWindow = { MilestonesData: milestonesData };
vm.runInNewContext(coreCode, { window: sandboxWindow });
expect(Array.isArray(sandboxWindow.DeathClockCore.MILESTONES)).toBe(true);
expect(sandboxWindow.DeathClockCore.MILESTONES.length).toBeGreaterThan(0);
});
test('MILESTONES falls back to empty array when MilestonesData is absent', () => {
const vm = require('vm');
const fs = require('fs');
const path = require('path');
const coreCode = fs.readFileSync(path.join(__dirname, '../death-clock-core.js'), 'utf8');
const sandboxWindow = {};
vm.runInNewContext(coreCode, { window: sandboxWindow });
expect(Array.isArray(sandboxWindow.DeathClockCore.MILESTONES)).toBe(true);
expect(sandboxWindow.DeathClockCore.MILESTONES.length).toBe(0);
});
});
// ============================================================
// getRateAtDate
// ============================================================
describe('getRateAtDate', () => {
test('returns a positive number for any date', () => {
expect(getRateAtDate(new Date('2023-01-01'))).toBeGreaterThan(0);
});
test('returns a higher rate after ChatGPT launch than before', () => {
const before = getRateAtDate(new Date('2022-01-01'));
const after = getRateAtDate(new Date('2023-01-01'));
expect(after).toBeGreaterThan(before);
});
test('returns a higher rate for Claude Code era than GPT-3 era', () => {
const gpt3Era = getRateAtDate(new Date('2020-07-01'));
const claudeCodeEra = getRateAtDate(new Date('2025-06-01'));
expect(claudeCodeEra).toBeGreaterThan(gpt3Era);
});
test('matches TOKENS_PER_SECOND at BASE_DATE_ISO', () => {
const rate = getRateAtDate(new Date(core.BASE_DATE_ISO));
expect(rate).toBe(TOKENS_PER_SECOND);
});
test('returns the earliest rate for dates before the schedule starts', () => {
const veryEarly = getRateAtDate(new Date('2010-01-01'));
expect(veryEarly).toBe(RATE_SCHEDULE[0].ratePerSec);
});
test('falls back to current time when no date is provided', () => {
const rate = getRateAtDate();
expect(typeof rate).toBe('number');
expect(rate).toBeGreaterThan(0);
});
test('falls back gracefully for an invalid date', () => {
const rate = getRateAtDate(new Date('not-a-date'));
expect(typeof rate).toBe('number');
expect(rate).toBeGreaterThan(0);
});
test('falls back gracefully for a non-Date argument', () => {
const rate = getRateAtDate('2025-01-01');
expect(typeof rate).toBe('number');
expect(rate).toBeGreaterThan(0);
});
test('rate is monotonically non-decreasing across schedule dates', () => {
let prevRate = 0;
for (const entry of RATE_SCHEDULE) {
const rate = getRateAtDate(new Date(entry.date));
expect(rate).toBeGreaterThanOrEqual(prevRate);
prevRate = rate;
}
});
});
// ============================================================
// getDynamicRate
// ============================================================
describe('getDynamicRate', () => {
const BASE_DATE = new Date(core.BASE_DATE_ISO);
test('returns TOKENS_PER_SECOND exactly at BASE_DATE_ISO', () => {
expect(getDynamicRate(BASE_DATE)).toBe(TOKENS_PER_SECOND);
});
test('returns more than TOKENS_PER_SECOND for a date one year after BASE_DATE', () => {
const oneYearLater = new Date(BASE_DATE.getTime() + 365.25 * 24 * 3600 * 1000);
expect(getDynamicRate(oneYearLater)).toBeGreaterThan(TOKENS_PER_SECOND);
});
test('grows by roughly RATE_GROWTH_PER_YEAR after one year', () => {
const oneYearLater = new Date(BASE_DATE.getTime() + 365.25 * 24 * 3600 * 1000);
const rate = getDynamicRate(oneYearLater);
const expected = TOKENS_PER_SECOND * (1 + RATE_GROWTH_PER_YEAR);
// Allow ±1 % tolerance for rounding
expect(rate).toBeGreaterThanOrEqual(expected * 0.99);
expect(rate).toBeLessThanOrEqual(expected * 1.01);
});
test('matches getRateAtDate for a historical date well before BASE_DATE', () => {
const historicalDate = new Date('2024-01-01');
expect(getDynamicRate(historicalDate)).toBe(getRateAtDate(historicalDate));
});
test('returns a positive number for any date', () => {
expect(getDynamicRate(new Date('2020-01-01'))).toBeGreaterThan(0);
expect(getDynamicRate(new Date('2028-01-01'))).toBeGreaterThan(0);
});
test('falls back to current time when no date is provided', () => {
const rate = getDynamicRate();
expect(typeof rate).toBe('number');
expect(rate).toBeGreaterThan(0);
});
test('falls back gracefully for an invalid date', () => {
const rate = getDynamicRate(new Date('not-a-date'));
expect(typeof rate).toBe('number');
expect(rate).toBeGreaterThan(0);
});
test('rate at BASE_DATE equals rate two years later times inverse growth factor', () => {
const twoYearsLater = new Date(BASE_DATE.getTime() + 2 * 365.25 * 24 * 3600 * 1000);
const rateLater = getDynamicRate(twoYearsLater);
const expectedFactor = Math.pow(1 + RATE_GROWTH_PER_YEAR, 2);
expect(rateLater / TOKENS_PER_SECOND).toBeCloseTo(expectedFactor, 1);
});
test('RATE_GROWTH_PER_YEAR is a positive number less than 1', () => {
expect(typeof RATE_GROWTH_PER_YEAR).toBe('number');
expect(RATE_GROWTH_PER_YEAR).toBeGreaterThan(0);
expect(RATE_GROWTH_PER_YEAR).toBeLessThan(1);
});
});
// ============================================================
// RATE_SCHEDULE sanity checks
// ============================================================
describe('RATE_SCHEDULE', () => {
test('is a non-empty array', () => {
expect(Array.isArray(RATE_SCHEDULE)).toBe(true);
expect(RATE_SCHEDULE.length).toBeGreaterThan(0);
});
test('each entry has date, ratePerSec, and event fields', () => {
RATE_SCHEDULE.forEach((r) => {
expect(typeof r.date).toBe('string');
expect(typeof r.ratePerSec).toBe('number');
expect(typeof r.event).toBe('string');
expect(r.ratePerSec).toBeGreaterThan(0);
});
});
test('dates are in ascending order', () => {
for (let i = 1; i < RATE_SCHEDULE.length; i++) {
expect(new Date(RATE_SCHEDULE[i].date).getTime())
.toBeGreaterThanOrEqual(new Date(RATE_SCHEDULE[i - 1].date).getTime());
}
});
test('rates are non-decreasing (AI consumption only grows)', () => {
for (let i = 1; i < RATE_SCHEDULE.length; i++) {
expect(RATE_SCHEDULE[i].ratePerSec).toBeGreaterThanOrEqual(RATE_SCHEDULE[i - 1].ratePerSec);
}
});
test('contains the ChatGPT launch event', () => {
const chatGPT = RATE_SCHEDULE.find((r) => r.event.toLowerCase().includes('chatgpt'));
expect(chatGPT).toBeDefined();
});
test('contains the Claude Code event', () => {
const claudeCode = RATE_SCHEDULE.find((r) => r.event.toLowerCase().includes('claude code'));
expect(claudeCode).toBeDefined();
});
});
// ============================================================
// Extended milestone checks
// ============================================================
describe('Extended MILESTONES', () => {
test('has more than 15 milestones', () => {
expect(MILESTONES.length).toBeGreaterThan(15);
});
test('each new milestone has required fields including icon and color', () => {
MILESTONES.forEach((m) => {
expect(typeof m.icon).toBe('string');
expect(m.icon.length).toBeGreaterThan(0);
expect(typeof m.color).toBe('string');
expect(typeof m.darkColor).toBe('string');
});
});
test('milestones span at least 6 orders of magnitude', () => {
const min = Math.min(...MILESTONES.map((m) => m.tokens));
const max = Math.max(...MILESTONES.map((m) => m.tokens));
expect(max / min).toBeGreaterThan(1e6);
});
test('all milestone ids are unique', () => {
const ids = MILESTONES.map((m) => m.id);
const unique = new Set(ids);
expect(unique.size).toBe(ids.length);
});
});
// ============================================================
// getTimeDelta — hours/minutes/seconds granularity
// ============================================================
describe('getTimeDelta sub-day granularity', () => {
const now = new Date('2026-01-01T00:00:00Z');
test('returns hours for a 3-hour future date', () => {
const future = new Date(now.getTime() + 3 * 60 * 60 * 1000);
const result = getTimeDelta(future, now);
expect(result).toMatch(/hour/);
});
test('returns singular hour for exactly 1 hour', () => {
const future = new Date(now.getTime() + 1 * 60 * 60 * 1000);
expect(getTimeDelta(future, now)).toBe('in ~1 hour');
});
test('returns minutes for a 45-minute future', () => {
const future = new Date(now.getTime() + 45 * 60 * 1000);
expect(getTimeDelta(future, now)).toMatch(/minute/);
});
test('returns singular minute for exactly 1 minute', () => {
const future = new Date(now.getTime() + 60 * 1000);
expect(getTimeDelta(future, now)).toBe('in ~1 minute');
});
test('returns seconds for a 30-second future', () => {
const future = new Date(now.getTime() + 30 * 1000);
expect(getTimeDelta(future, now)).toMatch(/second/);
});
test('returns singular second for exactly 1 second', () => {
const future = new Date(now.getTime() + 1000);
expect(getTimeDelta(future, now)).toBe('in ~1 second');
});
});
// ============================================================
// generateProjectionData — exponential growth
// ============================================================
describe('generateProjectionData with annualGrowthRate', () => {
const now = new Date('2026-01-01T00:00:00Z');
test('linear (growth=0) produces same result as before', () => {
const linear = generateProjectionData(0, 1_000_000, 12, now, 0);
expect(linear.length).toBe(13);
// Last point is 12 months of constant rate
const lastLinear = linear[linear.length - 1].tokensT;
expect(lastLinear).toBeGreaterThan(0);
});
test('exponential produces larger values than linear over same period', () => {
const linear = generateProjectionData(0, 1_000_000, 12, now, 0);
const expo = generateProjectionData(0, 1_000_000, 12, now, 0.5);
const lastLinear = linear[linear.length - 1].tokensT;
const lastExpo = expo[linear.length - 1].tokensT;
expect(lastExpo).toBeGreaterThan(lastLinear);
});
test('first data point is the same regardless of growth rate', () => {
const linear = generateProjectionData(1e15, 1_000_000, 6, now, 0);
const expo = generateProjectionData(1e15, 1_000_000, 6, now, 1.0);
expect(linear[0].tokensT).toBeCloseTo(expo[0].tokensT, 2);
});
test('values are monotonically increasing with exponential growth', () => {
const data = generateProjectionData(0, 1_000_000, 6, now, 0.5);
for (let i = 1; i < data.length; i++) {
expect(data[i].tokensT).toBeGreaterThan(data[i - 1].tokensT);
}
});
test('negative annualGrowthRate is treated as linear (0)', () => {
const result = generateProjectionData(0, 1_000_000, 3, now, -0.5);
// Should still return data without errors
expect(result.length).toBe(4);
result.forEach((d) => expect(typeof d.tokensT).toBe('number'));
});
});
// ============================================================
// calculateTipImpact
// ============================================================
describe('calculateTipImpact', () => {
test('returns zeros for invalid savingPct', () => {
const r = calculateTipImpact('bad', 10);
expect(r.tokensPerDay).toBe(0);
expect(r.kWhPerDay).toBe(0);
});
test('returns zeros for invalid percentOfUsers', () => {
const r = calculateTipImpact(50, 'bad');
expect(r.tokensPerDay).toBe(0);
});
test('returns zeros for negative savingPct', () => {
const r = calculateTipImpact(-10, 5);
expect(r.tokensPerDay).toBe(0);
});
test('returns zeros for negative percentOfUsers', () => {
const r = calculateTipImpact(50, -1);
expect(r.tokensPerDay).toBe(0);
});
test('positive values for valid inputs', () => {
const r = calculateTipImpact(30, 1);
expect(r.tokensPerDay).toBeGreaterThan(0);
expect(r.kWhPerDay).toBeGreaterThan(0);
expect(r.co2KgPerDay).toBeGreaterThan(0);
expect(r.waterLPerDay).toBeGreaterThan(0);
});
test('scales linearly with percentOfUsers', () => {
const r1 = calculateTipImpact(30, 1);
const r2 = calculateTipImpact(30, 2);
expect(r2.tokensPerDay).toBeCloseTo(r1.tokensPerDay * 2, 0);
});
test('scales linearly with savingPct', () => {
const r1 = calculateTipImpact(10, 1);
const r2 = calculateTipImpact(20, 1);
expect(r2.tokensPerDay).toBeCloseTo(r1.tokensPerDay * 2, 0);
});
test('caps savingPct at 100', () => {
const r100 = calculateTipImpact(100, 1);
const r200 = calculateTipImpact(200, 1);
expect(r100.tokensPerDay).toBeCloseTo(r200.tokensPerDay, 0);
});
test('caps percentOfUsers at 100', () => {
const r100 = calculateTipImpact(30, 100);
const r200 = calculateTipImpact(30, 200);
expect(r100.tokensPerDay).toBeCloseTo(r200.tokensPerDay, 0);
});
test('uses custom ratePerSec when provided', () => {
const r = calculateTipImpact(100, 100, 1);
// 1 token/sec * 86400 sec * 100% saving * 100% users = 86400 tokens
expect(r.tokensPerDay).toBeCloseTo(86400, 0);
});
test('falls back to TOKENS_PER_SECOND when ratePerSec is invalid', () => {
const rDefault = calculateTipImpact(10, 1);
const rExplicit = calculateTipImpact(10, 1, TOKENS_PER_SECOND);
expect(rDefault.tokensPerDay).toBeCloseTo(rExplicit.tokensPerDay, 0);
});
});
// ============================================================
// computeExtinctionSecsRemaining — 1-second-per-tick invariant
// ============================================================
describe('computeExtinctionSecsRemaining', () => {
// A token target safely beyond BASE_TOKENS (10 quadrillion above baseline)
const TARGET = BASE_TOKENS + 10_000_000_000_000_000; // 10 quadrillion above base
// Anchor "now" to BASE_DATE_ISO so results are deterministic
const baseMs = new Date(BASE_DATE_ISO).getTime();
test('returns a positive number when target is in the future', () => {
const secs = computeExtinctionSecsRemaining(TARGET, baseMs);
expect(secs).toBeGreaterThan(0);
});
test('decreases by exactly 1 for every 1 second that elapses', () => {
// This is the core invariant — the old linear approximation fails this test
// because it ticked down by ~2 seconds per second when the rate was growing.
const t0 = baseMs;
const t1 = baseMs + 1000;
const secs0 = computeExtinctionSecsRemaining(TARGET, t0);
const secs1 = computeExtinctionSecsRemaining(TARGET, t1);
expect(secs0 - secs1).toBeCloseTo(1, 9);
});
test('decreases by exactly 60 over a 60-second window', () => {
const t0 = baseMs;
const t1 = baseMs + 60_000;
const secs0 = computeExtinctionSecsRemaining(TARGET, t0);
const secs1 = computeExtinctionSecsRemaining(TARGET, t1);
expect(secs0 - secs1).toBeCloseTo(60, 9);
});
test('returns 0 or negative when target equals or is below BASE_TOKENS', () => {
expect(computeExtinctionSecsRemaining(BASE_TOKENS, baseMs)).toBe(0);
expect(computeExtinctionSecsRemaining(BASE_TOKENS - 1, baseMs)).toBe(0);
});
test('returns a negative number when target was passed in the past', () => {
// Advance far into the future so that target has already been passed
const farFuture = baseMs + 1_000_000_000 * 1000; // ~31 years from base
const secs = computeExtinctionSecsRemaining(TARGET, farFuture);
expect(secs).toBeLessThan(0);
});
test('handles non-numeric nowMs by defaulting to Date.now()', () => {
// Should not throw; result will be a finite number
const secs = computeExtinctionSecsRemaining(TARGET, 'abc');
expect(typeof secs).toBe('number');
expect(isFinite(secs)).toBe(true);
});
});
// ============================================================
// TOKEN_TIPS
// ============================================================
describe('TOKEN_TIPS', () => {
test('is a non-empty array', () => {
expect(Array.isArray(TOKEN_TIPS)).toBe(true);
expect(TOKEN_TIPS.length).toBeGreaterThan(0);
});
test('each tip has required fields', () => {
TOKEN_TIPS.forEach((tip) => {
expect(typeof tip.id).toBe('string');
expect(typeof tip.icon).toBe('string');
expect(typeof tip.title).toBe('string');
expect(typeof tip.tip).toBe('string');
expect(typeof tip.detail).toBe('string');
expect(typeof tip.savingPct).toBe('number');
expect(tip.savingPct).toBeGreaterThan(0);
expect(tip.savingPct).toBeLessThanOrEqual(100);
});
});
test('all tip ids are unique', () => {
const ids = TOKEN_TIPS.map((t) => t.id);
expect(new Set(ids).size).toBe(ids.length);
});
test('optional reference field is a string URL when present', () => {
TOKEN_TIPS.forEach((tip) => {
if (tip.reference !== undefined) {
expect(typeof tip.reference).toBe('string');
expect(tip.reference).toMatch(/^https?:\/\//);
}
});
});
});
// ============================================================
// generateEquivalences
// ============================================================
describe('generateEquivalences', () => {
test('returns a non-empty array for a large token count', () => {
const result = generateEquivalences(1e15);
expect(Array.isArray(result)).toBe(true);
expect(result.length).toBeGreaterThan(0);
});
test('each entry has icon (string) and text (non-empty string)', () => {
generateEquivalences(1e15).forEach((e) => {
expect(typeof e.icon).toBe('string');
expect(typeof e.text).toBe('string');
expect(e.text.length).toBeGreaterThan(0);
});
});
test('hopeful mode differs from snarky mode', () => {
const hopeful = generateEquivalences(1e15, 'hopeful');
const snarky = generateEquivalences(1e15, 'snarky');
expect(hopeful[0].text).not.toBe(snarky[0].text);
});
test('defaults to hopeful mode when no mode argument is given', () => {
const def = generateEquivalences(1e15);
const hopeful = generateEquivalences(1e15, 'hopeful');
expect(def[0].text).toBe(hopeful[0].text);
});
test('returns empty array for negative tokens', () => {
expect(generateEquivalences(-1)).toEqual([]);
});
test('returns empty array for non-numeric input', () => {
expect(generateEquivalences('abc')).toEqual([]);
});
test('returns an array for 0 tokens', () => {
expect(Array.isArray(generateEquivalences(0))).toBe(true);
});
test('both modes return the same number of entries', () => {
expect(generateEquivalences(1e15, 'hopeful').length)
.toBe(generateEquivalences(1e15, 'snarky').length);
});
});