| 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 | }); |
projects-timfalken-com allianties