Files
wekan/tests/pomodoroTimer.test.cjs
T
Lauri Ojansivu 570cc34552 Stopping a timer adds its time to the total, never replaces it.
Flowtime's Stop and Pomodoro's completed or stopped work interval read the
total on the client (getSpentTime()) and $set the sum. For a card that
links to a board or a card, that total is the linked document's, which can
still be on its way to the client: getSpentTime() then read null, and the
Stop replaced a linked board's 4 logged hours with the 1 hour of the
session (linked-timer-sessions.e2e.js failed 4 of 6 repeats). The session
length is now added on the server with addSpentTime(), an $inc on the same
spentTime field of the linked board, the linked card or the card. The
Flowtime and Pomodoro guards check addSpentTime and no longer pin
setSpentTime, and tests/linkedTimerSessions.test.cjs credits correctly
with no total on the client, pins the $inc targets, refuses a zero,
negative or non-number amount, and fails if a timer helper computes a total
from getSpentTime() again.

Thanks to xet7 !
2026-11-06 21:35:36 +02:00

201 lines
9.5 KiB
JavaScript

'use strict';
// Pomodoro timer (#4862): the classic FIXED-interval technique - a 25-minute
// work interval, then a break (5 minutes, or a longer break every 4th
// completed work interval), unlike the separate open-ended Flowtime feature
// (#3919, cardFlowtime.js/.jade/.css). This is additive: it sits alongside
// Flowtime in cardDetails.jade with its own fields, its own methods and its
// own template, and reuses the SAME setSpentTime()-based helper the manual
// time-entry popup (cardTime.js) and Flowtime both already use, rather than
// keeping a separate total.
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const root = path.join(__dirname, '..');
const read = file => fs.readFileSync(path.join(root, file), 'utf8');
const cardsSrc = read('models/cards.js');
// --- schema fields exist, mirroring the Flowtime fields' shape ---
assert.match(cardsSrc, /pomodoroStartAt: \{[\s\S]{0,220}?type: Date,\s*optional: true,\s*\},/);
assert.match(cardsSrc, /pomodoroPhase: \{[\s\S]{0,220}?type: String,\s*optional: true,\s*allowedValues: \['work', 'break'\],\s*\},/);
assert.match(cardsSrc, /pomodoroCount: \{[\s\S]{0,220}?type: Number,\s*optional: true,\s*defaultValue: 0,\s*\},/);
assert.match(cardsSrc, /pomodoroUserId: \{[\s\S]{0,220}?type: String,\s*optional: true,\s*\},/);
assert.match(cardsSrc, /pomodoroWorkMinutes: \{[\s\S]{0,220}?type: Number,\s*optional: true,\s*defaultValue: 25,\s*\},/);
// --- fields are additive next to Flowtime's, not a rename/repurpose of them ---
assert.match(cardsSrc, /flowStartAt: \{/, 'Flowtime fields must still be present');
assert.match(cardsSrc, /pomodoroStartAt: \{/, 'Pomodoro fields must be their own, separate fields');
assert.notEqual(
cardsSrc.indexOf('flowStartAt'),
cardsSrc.indexOf('pomodoroStartAt'),
'Pomodoro must not reuse/rename a Flowtime field',
);
// --- methods exist and touch the right fields ---
assert.match(cardsSrc, /startPomodoro\(userId, workMinutes\) \{/);
assert.match(cardsSrc, /completePomodoroWorkInterval\(\) \{/);
assert.match(cardsSrc, /completePomodoroBreakInterval\(\) \{/);
assert.match(cardsSrc, /stopPomodoro\(\) \{/);
assert.match(cardsSrc, /isPomodoroActive\(\) \{/);
assert.match(cardsSrc, /getPomodoroBreakMinutes\(\) \{/);
// A completed work interval adds to the SAME spentTime field the manual
// time-entry popup edits, with addSpentTime() - the helper Flowtime uses too -
// rather than a duplicate accumulator. Changed on purpose from read-and-$set
// through setSpentTime(): an $inc on the server does not replace the total
// when the client's copy of a linked board or card has not arrived yet
// (tests/linkedTimerSessions.test.cjs).
const workIntervalBody = cardsSrc.match(
/completePomodoroWorkInterval\(\) \{([\s\S]*?)\n \},/,
)[1];
assert.match(workIntervalBody, /this\.addSpentTime\(workHours\)/,
'a completed work interval must add its duration via addSpentTime()');
assert.match(workIntervalBody, /pomodoroPhase: 'break'/,
'a completed work interval must switch the card to the break phase');
assert.match(workIntervalBody, /pomodoroCount: newCount/,
'a completed work interval must increment the completed-interval count');
// A completed BREAK interval must NOT touch spentTime at all - only the
// work interval adds time.
const breakIntervalBody = cardsSrc.match(
/completePomodoroBreakInterval\(\) \{([\s\S]*?)\n \},/,
)[1];
assert.doesNotMatch(breakIntervalBody, /(set|add)SpentTime/,
'completing a break interval must add no time');
assert.match(breakIntervalBody, /pomodoroPhase: null/,
'completing a break interval must go back to ready-to-start, not auto-start the next work interval');
assert.doesNotMatch(breakIntervalBody, /pomodoroPhase: 'work'/,
'completing a break interval must not itself start the next work interval');
// stopPomodoro(): credits partial WORK time, adds nothing for a BREAK stop,
// and clears every Pomodoro field back to its empty/null default.
const stopBody = cardsSrc.match(/stopPomodoro\(\) \{([\s\S]*?)\n \},/)[1];
assert.match(stopBody, /if \(this\.pomodoroPhase === 'work'\) \{/);
assert.match(stopBody, /this\.addSpentTime\(elapsedHours\)/);
assert.match(stopBody, /pomodoroStartAt: null,/);
assert.match(stopBody, /pomodoroPhase: null,/);
assert.match(stopBody, /pomodoroCount: 0,/);
assert.match(stopBody, /pomodoroUserId: null,/);
// --- the "every 4th completed work interval gets a long break" rule ---
function getPomodoroBreakMinutes(count, BREAK_MINUTES, LONG_BREAK_MINUTES) {
return count > 0 && count % 4 === 0 ? LONG_BREAK_MINUTES : BREAK_MINUTES;
}
const BREAK = 5;
const LONG_BREAK = 15;
assert.equal(getPomodoroBreakMinutes(0, BREAK, LONG_BREAK), BREAK);
assert.equal(getPomodoroBreakMinutes(1, BREAK, LONG_BREAK), BREAK);
assert.equal(getPomodoroBreakMinutes(2, BREAK, LONG_BREAK), BREAK);
assert.equal(getPomodoroBreakMinutes(3, BREAK, LONG_BREAK), BREAK);
assert.equal(getPomodoroBreakMinutes(4, BREAK, LONG_BREAK), LONG_BREAK,
'the 4th completed work interval gets the long break');
assert.equal(getPomodoroBreakMinutes(5, BREAK, LONG_BREAK), BREAK);
assert.equal(getPomodoroBreakMinutes(8, BREAK, LONG_BREAK), LONG_BREAK,
'the 8th completed work interval also gets the long break');
// The same constants and modulo check are actually wired into cards.js.
assert.match(cardsSrc, /BREAK_MINUTES: 5,/);
assert.match(cardsSrc, /LONG_BREAK_MINUTES: 15,/);
assert.match(cardsSrc, /count > 0 && count % 4 === 0/);
// A completed work interval's duration credited to spentTime is computed in
// HOURS from pomodoroWorkMinutes, the same unit setSpentTime() already uses
// (mirrors stopFlowSession()'s own hours conversion).
function workHoursFor(workMinutes) {
return workMinutes / 60;
}
assert.equal(workHoursFor(25), 25 / 60);
assert.match(workIntervalBody, /this\.getPomodoroWorkMinutes\(\) \/ 60/);
// --- client template: additive, separate block from Flowtime's ---
const cardDetails = read('client/components/cards/cardDetails.jade');
assert.match(cardDetails, /\+cardFlowtime/, 'Flowtime\'s block must still be present');
assert.match(cardDetails, /\+cardPomodoro/, 'Pomodoro gets its own, separate template block');
assert.match(cardDetails, /card-details-item-pomodoro/);
const pomodoroJs = read('client/components/cards/cardPomodoro.js');
assert.match(pomodoroJs, /Template\.cardPomodoro\.onCreated/);
assert.match(pomodoroJs, /Template\.cardPomodoro\.events/);
assert.match(pomodoroJs, /card\.startPomodoro\(/);
assert.match(pomodoroJs, /card\.stopPomodoro\(\)/);
assert.match(pomodoroJs, /completePomodoroWorkInterval\(\)/);
assert.match(pomodoroJs, /completePomodoroBreakInterval\(\)/);
const pomodoroJade = read('client/components/cards/cardPomodoro.jade');
assert.match(pomodoroJade, /template\(name="cardPomodoro"\)/);
assert.match(pomodoroJade, /js-start-pomodoro/);
assert.match(pomodoroJade, /js-stop-pomodoro/);
// Flowtime's own files must be untouched/still present (additive-only scope).
assert.ok(fs.existsSync(path.join(root, 'client/components/cards/cardFlowtime.js')));
assert.ok(fs.existsSync(path.join(root, 'client/components/cards/cardFlowtime.jade')));
// --- i18n: the new keys exist, in the same position/order in every locale,
// and are really translated (never left equal to English, aside from
// English's own en-* regional variants) ---
const dataDir = path.join(root, 'imports/i18n/data');
const en = JSON.parse(read('imports/i18n/data/en.i18n.json'));
const enKeys = Object.keys(en);
const newKeys = [
'pomodoro',
'pomodoro-start',
'pomodoro-work',
'pomodoro-break',
'pomodoro-completed',
];
for (const k of newKeys) {
assert.ok(k in en, `en.i18n.json must have "${k}"`);
}
// They sit together, right after the existing "flow-interruptions" key.
const flowIdx = enKeys.indexOf('flow-interruptions');
assert.ok(flowIdx > 0);
for (let i = 0; i < newKeys.length; i += 1) {
assert.equal(enKeys[flowIdx + 1 + i], newKeys[i],
'the new Pomodoro keys must sit together, in order, right after flow-interruptions');
}
const tokensOf = value => [...String(value).matchAll(
/__[A-Za-z0-9_.]+__|%(?:\d+\$)?[A-Za-z%]/g,
)].map(match => match[0]).sort();
const locales = fs.readdirSync(dataDir)
.filter(f => f.endsWith('.i18n.json'))
.filter(f => f !== 'en.i18n.json');
let checked = 0;
for (const f of locales) {
const isEnglishVariant = /^en[-_.]/.test(f);
const locale = JSON.parse(read(path.join('imports/i18n/data', f)));
const keys = Object.keys(locale);
if (isEnglishVariant) {
// English regional variants are skipped by design (see
// releases/translations/fill-translations.mjs) - they may simply not
// carry the new keys at all.
continue;
}
for (const k of newKeys) {
assert.ok(k in locale, `${f}: missing new key "${k}"`);
assert.ok(locale[k].trim(), `${f}: empty translation for ${k}`);
// Pomodoro is the method's proper name in languages using Latin script.
if (k !== 'pomodoro') assert.notEqual(locale[k], en[k],
`${f}: "${k}" must be a real translation, not left equal to English`);
assert.deepEqual(tokensOf(locale[k]), tokensOf(en[k]),
`${f}: "${k}" placeholder inventory must match English exactly`);
}
// Same relative key order as English (matches
// tests/boardItemLinks.test.cjs's per-key position rule).
for (const k of newKeys) {
const enIndex = enKeys.indexOf(k);
const localeIndex = keys.indexOf(k);
assert.equal(localeIndex, enIndex,
`${f}: "${k}" is at a different position than in en.i18n.json`);
}
checked += 1;
}
assert.ok(checked > 200, `expected to check well over 200 locales, checked ${checked}`);
console.log(`pomodoroTimer: fields, methods, template, and ${checked} locales' i18n keys verified`);