| 1 | 'use strict'; |
| 2 | const { describe, it } = require('node:test'); |
| 3 | const assert = require('node:assert/strict'); |
| 4 | const Rules = require('../src/rules.js'); |
| 5 | |
| 6 | function ams(iso) { |
| 7 | // iso like '2026-11-13 23:30:00' as Europe/Amsterdam wall clock → unix |
| 8 | const [d, t] = iso.split(' '); |
| 9 | const hm = t.slice(0, 5); |
| 10 | const sec = parseInt(t.slice(6), 10) || 0; |
| 11 | return Rules.amsterdamWallclock(d, hm) + sec; |
| 12 | } |
| 13 | |
| 14 | describe('Rules grid and Chebyshev', () => { |
| 15 | it('suggests max(6 + ceil(n/2), n + 1), capped at 24', () => { |
| 16 | assert.equal(Rules.suggestedGrid(2), 7); |
| 17 | assert.equal(Rules.suggestedGrid(3), 8); // ceil(1.5) = 2 |
| 18 | assert.equal(Rules.suggestedGrid(6), 9); |
| 19 | assert.equal(Rules.suggestedGrid(10), 11); |
| 20 | assert.equal(Rules.suggestedGrid(14), 15); |
| 21 | assert.equal(Rules.suggestedGrid(16), 17); |
| 22 | assert.equal(Rules.suggestedGrid(30), 24); |
| 23 | assert.equal(Rules.gridSize(2), 7); |
| 24 | assert.equal(Rules.clampGrid(3), 6); |
| 25 | assert.equal(Rules.clampGrid(100), 24); |
| 26 | assert.equal(Rules.maxInset(6), 1); |
| 27 | assert.equal(Rules.maxInset(7), 2); |
| 28 | }); |
| 29 | it('uses Chebyshev distance (diagonals included)', () => { |
| 30 | assert.equal(Rules.chebyshev(0, 0, 2, 2), 2); |
| 31 | assert.equal(Rules.chebyshev(0, 0, 2, 0), 2); |
| 32 | }); |
| 33 | it('passable accounts for arena inset', () => { |
| 34 | assert.equal(Rules.isPassable(6, 1, 0, 0), false); |
| 35 | assert.equal(Rules.isPassable(6, 1, 1, 1), true); |
| 36 | assert.equal(Rules.isPassable(6, 1, 5, 3), false); |
| 37 | }); |
| 38 | }); |
| 39 | |
| 40 | describe('Rules cease-fire, midnight wrap and DST', () => { |
| 41 | const rules = { |
| 42 | ceasefire: true, |
| 43 | ceasefire_ranges: [ |
| 44 | { start: '22:00', end: '06:00' }, |
| 45 | { start: '12:00', end: '13:00' } |
| 46 | ] |
| 47 | }; |
| 48 | |
| 49 | it('treats 23:30 as cease-fire wrapping to 06:00 next day', () => { |
| 50 | const info = Rules.ceasefireInfo(rules, ams('2026-11-13 23:30:00')); |
| 51 | assert.ok(info && info.active); |
| 52 | assert.equal(info.until, ams('2026-11-14 06:00:00')); |
| 53 | }); |
| 54 | |
| 55 | it('treats the end timestamp as exclusive', () => { |
| 56 | const info = Rules.ceasefireInfo(rules, ams('2026-11-14 06:00:00')); |
| 57 | assert.equal(info.active, false); |
| 58 | assert.equal(info.until, ams('2026-11-14 12:00:00')); |
| 59 | }); |
| 60 | |
| 61 | it('handles a second daytime range and countdown seconds', () => { |
| 62 | const info = Rules.ceasefireInfo(rules, ams('2026-11-14 12:30:00')); |
| 63 | assert.equal(info.active, true); |
| 64 | assert.equal(info.until, ams('2026-11-14 13:00:00')); |
| 65 | const before = Rules.ceasefireInfo(rules, ams('2026-11-14 21:59:00')); |
| 66 | assert.equal(before.active, false); |
| 67 | assert.equal(before.seconds, 60); |
| 68 | const late = Rules.ceasefireInfo(rules, ams('2026-11-15 05:59:00')); |
| 69 | assert.equal(late.active, true); |
| 70 | assert.equal(late.seconds, 60); |
| 71 | }); |
| 72 | |
| 73 | it('shortens 01:00–04:00 across the 2026 spring-forward to 2 real hours', () => { |
| 74 | const r = { ceasefire: true, ceasefire_ranges: [{ start: '01:00', end: '04:00' }] }; |
| 75 | const start = ams('2026-03-29 01:00:00'); |
| 76 | const info = Rules.ceasefireInfo(r, start); |
| 77 | assert.equal(info.active, true); |
| 78 | assert.equal(info.until - start, 2 * 3600); |
| 79 | const three = Rules.ceasefireInfo(r, ams('2026-03-29 03:00:00')); |
| 80 | assert.equal(three.active, true); |
| 81 | assert.equal(three.seconds, 3600); |
| 82 | const half = Rules.ceasefireInfo(r, ams('2026-03-29 00:30:00')); |
| 83 | assert.equal(half.active, false); |
| 84 | assert.equal(half.seconds, 1800); |
| 85 | }); |
| 86 | |
| 87 | it('lengthens 01:00–03:30 across the 2026 fall-back to 3.5 real hours', () => { |
| 88 | const r = { ceasefire: true, ceasefire_ranges: [{ start: '01:00', end: '03:30' }] }; |
| 89 | const start = ams('2026-10-25 01:00:00'); |
| 90 | const info = Rules.ceasefireInfo(r, start); |
| 91 | assert.equal(info.active, true); |
| 92 | assert.equal(info.until - start, 3.5 * 3600); |
| 93 | // Both occurrences of 02:15 are inside. |
| 94 | const first = Rules.ceasefireInfo(r, start + 75 * 60); |
| 95 | const second = Rules.ceasefireInfo(r, start + (75 + 60) * 60); |
| 96 | assert.equal(first.active, true); |
| 97 | assert.equal(second.active, true); |
| 98 | const after = Rules.ceasefireInfo(r, ams('2026-10-25 03:30:00')); |
| 99 | assert.equal(after.active, false); |
| 100 | }); |
| 101 | |
| 102 | it('is inactive when the rule is off or ranges are empty', () => { |
| 103 | assert.equal(Rules.ceasefireInfo({ ceasefire: false, ceasefire_ranges: [{ start: '00:00', end: '23:59' }] }, ams('2026-11-14 12:00:00')), null); |
| 104 | assert.equal(Rules.ceasefireInfo({ ceasefire: true, ceasefire_ranges: [] }, ams('2026-11-14 12:00:00')), null); |
| 105 | }); |
| 106 | |
| 107 | it('merges overlapping ranges for the countdown', () => { |
| 108 | const r = { ceasefire: true, ceasefire_ranges: [ |
| 109 | { start: '10:00', end: '12:00' }, |
| 110 | { start: '11:00', end: '13:00' } |
| 111 | ] }; |
| 112 | const info = Rules.ceasefireInfo(r, ams('2026-11-14 10:30:00')); |
| 113 | assert.equal(info.active, true); |
| 114 | assert.equal(info.until, ams('2026-11-14 13:00:00')); |
| 115 | }); |
| 116 | |
| 117 | it('formats HH:MM:SS countdowns', () => { |
| 118 | assert.equal(Rules.formatHms(0), '00:00:00'); |
| 119 | assert.equal(Rules.formatHms(5), '00:00:05'); |
| 120 | assert.equal(Rules.formatHms(3661), '01:01:01'); |
| 121 | assert.equal(Rules.formatHms(12 * 3600 + 34 * 60 + 56), '12:34:56'); |
| 122 | }); |
| 123 | }); |
| 124 | |
| 125 | describe('Rules timelineIndex (nearest event)', () => |
| 126 | { |
| 127 | it('returns 0 when there are no spends', () => |
| 128 | { |
| 129 | assert.equal(Rules.timelineIndex(0.9, 0), 0); |
| 130 | assert.equal(Rules.timelineIndex(0, 0), 0); |
| 131 | }); |
| 132 | it('maps 0 to the start and 1 to live (after the last spend)', () => |
| 133 | { |
| 134 | assert.equal(Rules.timelineIndex(0, 4), 0); |
| 135 | assert.equal(Rules.timelineIndex(1, 4), 4); |
| 136 | assert.equal(Rules.timelineIndex(0, 1), 0); |
| 137 | assert.equal(Rules.timelineIndex(1, 1), 1); |
| 138 | }); |
| 139 | it('jumps to the nearest event along the bar', () => |
| 140 | { |
| 141 | // 4 spends: slots at 0, 0.25, 0.5, 0.75, 1 |
| 142 | assert.equal(Rules.timelineIndex(0.12, 4), 0); |
| 143 | assert.equal(Rules.timelineIndex(0.13, 4), 1); |
| 144 | assert.equal(Rules.timelineIndex(0.26, 4), 1); |
| 145 | assert.equal(Rules.timelineIndex(0.37, 4), 1); |
| 146 | assert.equal(Rules.timelineIndex(0.38, 4), 2); |
| 147 | assert.equal(Rules.timelineIndex(0.88, 4), 4); |
| 148 | assert.equal(Rules.timelineIndex(1.5, 4), 4); |
| 149 | assert.equal(Rules.timelineIndex(-0.2, 4), 0); |
| 150 | }); |
| 151 | }); |
| 152 | |
| 153 | describe('Rules.angleToMinutes (cease-fire clock)', () => { |
| 154 | it('maps the dial with 0 at the top, clockwise, snapped to 15 min', () => { |
| 155 | const top = -Math.PI / 2, right = 0, bottom = Math.PI / 2, left = Math.PI; |
| 156 | assert.equal(Rules.angleToMinutes(top, 15), 0); |
| 157 | assert.equal(Rules.angleToMinutes(right, 15), 360); |
| 158 | assert.equal(Rules.angleToMinutes(bottom, 15), 720); |
| 159 | assert.equal(Rules.angleToMinutes(left, 15), 1080); |
| 160 | assert.equal(Rules.angleToMinutes(-Math.PI, 15), 1080); |
| 161 | // 7 minutes past 6:00 snaps to 6:00; 8 minutes snaps to 6:15 |
| 162 | const perMin = 2 * Math.PI / 1440; |
| 163 | assert.equal(Rules.angleToMinutes(right + 7 * perMin, 15), 360); |
| 164 | assert.equal(Rules.angleToMinutes(right + 8 * perMin, 15), 375); |
| 165 | // just before midnight wraps to 0 |
| 166 | assert.equal(Rules.angleToMinutes(top - 3 * perMin, 15), 0); |
| 167 | assert.equal(Rules.angleToMinutes(top - 3 * perMin, 1), 1437); |
| 168 | }); |
| 169 | }); |
| 170 | |
| 171 | describe('Rules time zones (host zone → viewer local)', () => { |
| 172 | const nov = Date.UTC(2026, 10, 14, 12) / 1000; |
| 173 | const jul = Date.UTC(2026, 6, 14, 12) / 1000; |
| 174 | it('converts wall-clock times between zones, DST-aware', () => { |
| 175 | assert.equal(Rules.convertHm('22:00', 'Europe/Amsterdam', 'America/New_York', nov), '16:00'); |
| 176 | assert.equal(Rules.convertHm('22:00', 'Europe/Amsterdam', 'America/New_York', jul), '16:00'); |
| 177 | assert.equal(Rules.convertHm('22:00', 'Europe/Amsterdam', 'Asia/Tokyo', nov), '06:00'); // CET+8 |
| 178 | assert.equal(Rules.convertHm('22:00', 'Europe/Amsterdam', 'Asia/Tokyo', jul), '05:00'); // CEST+7 |
| 179 | assert.equal(Rules.convertHm('09:30', 'Asia/Kolkata', 'UTC', nov), '04:00'); |
| 180 | assert.equal(Rules.convertHm('12:00', 'Europe/Amsterdam', 'Europe/Amsterdam', nov), '12:00'); |
| 181 | }); |
| 182 | it('uses the offset of the reference day around a DST switch', () => { |
| 183 | // 2026-03-08 US spring-forward; Europe still on CET until 03-29. |
| 184 | const before = Date.UTC(2026, 2, 7, 12) / 1000; |
| 185 | const after = Date.UTC(2026, 2, 9, 12) / 1000; |
| 186 | assert.equal(Rules.convertHm('09:00', 'America/New_York', 'Europe/Amsterdam', before), '15:00'); |
| 187 | assert.equal(Rules.convertHm('09:00', 'America/New_York', 'Europe/Amsterdam', after), '14:00'); |
| 188 | }); |
| 189 | it('converts rules for a viewer; ranges may wrap midnight after conversion', () => { |
| 190 | const r = Rules.rulesInTz({ tz: 'Europe/Amsterdam', ap_time: '00:00', |
| 191 | ceasefire_ranges: [{ start: '10:00', end: '14:00' }] }, 'Asia/Tokyo', nov); |
| 192 | assert.equal(r.tz, 'Asia/Tokyo'); |
| 193 | assert.equal(r.ap_time, '08:00'); |
| 194 | assert.deepEqual(r.ceasefire_ranges, [{ start: '18:00', end: '22:00' }]); |
| 195 | const w = Rules.rulesInTz({ tz: 'Europe/Amsterdam', ap_time: '12:00', |
| 196 | ceasefire_ranges: [{ start: '14:00', end: '20:00' }] }, 'Asia/Tokyo', nov); |
| 197 | assert.deepEqual(w.ceasefire_ranges, [{ start: '22:00', end: '04:00' }], 'wraps after conversion'); |
| 198 | }); |
| 199 | it('computes the cease-fire in the rules zone', () => { |
| 200 | const rules = { ceasefire: true, tz: 'Asia/Tokyo', ceasefire_ranges: [{ start: '22:00', end: '06:00' }] }; |
| 201 | const t2330tokyo = Date.UTC(2026, 10, 14, 14, 30) / 1000; // 23:30 JST |
| 202 | const info = Rules.ceasefireInfo(rules, t2330tokyo); |
| 203 | assert.equal(info.active, true); |
| 204 | assert.equal(info.until, Date.UTC(2026, 10, 14, 21, 0) / 1000); // 06:00 JST |
| 205 | assert.equal(Rules.validTz('Mars/Olympus'), 'Europe/Amsterdam'); |
| 206 | assert.equal(Rules.validTz('Asia/Tokyo'), 'Asia/Tokyo'); |
| 207 | }); |
| 208 | }); |
| 209 |
projects-timfalken-com allianties