Files
wekan/tests/spentTimeFormat.test.cjs
T
Lauri Ojansivu 3e8e8039ff Spent time is shown as a duration, not decimal hours.
A card's Spent time is stored in decimal hours, and a Pomodoro or Flowtime
session adds its duration to it, so a short session left a badge such as
0.034722222222222224. The badge shows days, hours, minutes and seconds now,
the leading zero units left out (12h34m56s, 2m5s), and the tooltip that
names hours rounds them to two decimals. The stored value and the edit
popup keep their hours. models/lib/spentTimeFormat.js, with a unit test
and a UI test.

Thanks to brlin-tw and xet7 !

Fixes #6890.
2026-11-11 20:20:55 +02:00

37 lines
1.7 KiB
JavaScript

'use strict';
// #6890: a card's Spent time is decimal hours, and the badge showed them raw
// (0.034722222222222224 after a short Pomodoro). It shows a duration now, the
// leading zero units left out, and the tooltip rounds hours to two decimals.
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const { test } = require('node:test');
const { formatSpentTime, roundedSpentHours } = require('../models/lib/spentTimeFormat');
test('a duration in days, hours, minutes and seconds', () => {
assert.equal(formatSpentTime(0.034722222222222224), '2m5s');
assert.equal(formatSpentTime(12 + 34 / 60 + 56 / 3600), '12h34m56s');
assert.equal(formatSpentTime(26), '1d2h');
assert.equal(formatSpentTime(0.5), '30m');
assert.equal(formatSpentTime(1 / 3600 / 3), '0s');
assert.equal(formatSpentTime(0), '0s');
});
test('negative: nothing to show for no value, a negative or a non-number', () => {
for (const v of [null, undefined, -1, NaN, Infinity, '3']) assert.equal(formatSpentTime(v), '', String(v));
});
test('the tooltip hours have at most two decimals', () => {
assert.equal(roundedSpentHours(0.034722222222222224), '0.03');
assert.equal(roundedSpentHours(4), '4');
assert.equal(roundedSpentHours(null), '');
});
test('the badge draws the duration, never the raw hours', () => {
const src = fs.readFileSync(path.join(__dirname, '../client/components/cards/cardTime.js'), 'utf8');
const show = src.slice(src.indexOf('showTime()'), src.indexOf('getIsOvertime()', src.indexOf('showTime()')));
assert.match(show, /formatSpentTime\(card\.getSpentTime\(\)\)/);
assert.doesNotMatch(src, /\$\{card\.getSpentTime\(\)\}/, 'the tooltip rounds too');
});