diff options
Diffstat (limited to 'test/test_data.lua')
| -rw-r--r-- | test/test_data.lua | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/test/test_data.lua b/test/test_data.lua index 00616d6..65f92e1 100644 --- a/test/test_data.lua +++ b/test/test_data.lua @@ -383,4 +383,66 @@ assert(ndays == 30 and first == 2, 'september 2026, got ' .. ndays .. '/' .. fir assert((data.month_shape(2024, 2)) == 29, 'leap february has 29 days') assert((data.month_shape(2026, 12)) == 31, 'december rolls over correctly') +-- === breaktimer =========================================================== +-- The phase-to-word and phase-to-colour decisions live in breaktimer_parse so +-- they can be checked here; the card only draws the result. +local bt = data.breaktimer_parse(read('test/fixtures/breaktimer_cache')) +assert(bt, 'breaktimer cache parses') +assert(bt.countdown == '24:31', 'countdown, got ' .. tostring(bt.countdown)) +assert(bt.phase_label == 'working', 'phase label, got ' .. tostring(bt.phase_label)) +assert(bt.next == 'pausa', 'next during work is a break, got ' .. tostring(bt.next)) +assert(bt.role == 'ok', 'working is ok, got ' .. tostring(bt.role)) + +-- Every phase has its own word and colour role. Build caches rather than files: +-- the sampler's output is six plain lines and the parse is the seam that +-- matters. +local function bt_cache(fields) + local out = { 'generated 1', 'running ' .. (fields.running or 'yes'), + 'state ' .. (fields.state or 'running'), + 'phase ' .. (fields.phase or 'working'), + 'remain ' .. (fields.remain or 60), + 'frozen ' .. (fields.frozen or 'no') } + return table.concat(out, '\n') .. '\n' +end + +local b + +b = data.breaktimer_parse(bt_cache({ phase = 'breaking', remain = 125 })) +assert(b.phase_label == 'micro-pausa' and b.role == 'heading' and b.next == 'lavoro', + 'micro-pause: ' .. b.phase_label .. '/' .. b.role .. '/' .. b.next) +assert(b.countdown == '2:05', 'micro-pause countdown, got ' .. b.countdown) + +b = data.breaktimer_parse(bt_cache({ phase = 'longbreak', remain = 600 })) +assert(b.phase_label == 'pausa lunga' and b.role == 'highlight' and b.next == 'lavoro', + 'long pause: ' .. b.phase_label .. '/' .. b.role) + +-- Paused freezes the number, but the phase underneath still decides what the +-- countdown leads to. +b = data.breaktimer_parse(bt_cache({ state = 'paused', phase = 'working' })) +assert(b.phase_label == 'in pausa' and b.role == 'warning' and b.next == 'pausa', + 'paused: ' .. b.phase_label .. '/' .. b.role .. '/' .. b.next) + +-- Frozen: the clock stopped while the phase stayed 'working'. Outside working +-- hours, or a hung daemon; both read the same. +b = data.breaktimer_parse(bt_cache({ frozen = 'yes' })) +assert(b.phase_label == 'outside working hours' and b.role == 'label', + 'frozen: ' .. b.phase_label .. '/' .. b.role) +assert(b.next == 'pausa', 'frozen working still leads to a break') + +-- A dead daemon: the sampler's liveness check sets running no even when the +-- state file was left saying running. +b = data.breaktimer_parse(bt_cache({ running = 'no' })) +assert(b.countdown == '--' and b.phase_label == 'fermo' and b.next == '--' + and b.role == 'label', 'stopped reads as fermo, got ' .. b.phase_label) + +-- A negative remain must clamp, not print '-1:59'. +b = data.breaktimer_parse(bt_cache({ remain = -5 })) +assert(b.countdown == '0:00', 'negative remain clamps, got ' .. b.countdown) + +-- No 'generated' key means the sampler never ran, which is different from a +-- stopped daemon (which still writes a cache). +assert(data.breaktimer_parse('running no\n') == nil, 'no generated key: nil') +assert(data.breaktimer_parse('') == nil, 'empty cache: nil') +assert(data.breaktimer_parse(nil) == nil, 'nil cache: nil') + print('test_data: all assertions passed') |
