1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
import { describe, it, expect } from 'vitest';
import {
computeRoutineState,
computeHealthScore,
computeHealthSeries,
computeProjectHealthSeries,
parseIntervalToDays,
} from './routine-metrics';
const DAY_MS = 24 * 60 * 60 * 1000;
const CREATED = '2026-09-01T00:00:00.000Z';
const routine = {
id: 1,
version: 1,
name: 'clean kitchen',
frequencyDays: 7,
createdAt: CREATED,
};
describe('computeRoutineState', () => {
it('is due when never done, even before the due date', () => {
const now = Date.parse(CREATED) + 3 * DAY_MS;
const state = computeRoutineState(routine, [], now);
expect(state.status).toBe('due');
expect(state.overdueDays).toBe(0);
expect(state.health).toBe(100);
expect(state.dueAt).toBe(
new Date(Date.parse(CREATED) + 7 * DAY_MS).toISOString(),
);
});
it('uses the last completion as reference once done', () => {
const doneAt = new Date(Date.parse(CREATED) + 5 * DAY_MS).toISOString();
const now = Date.parse(doneAt) + 2 * DAY_MS;
const state = computeRoutineState(routine, [{ routineId: 1, doneAt }], now);
expect(state.lastDoneAt).toBe(doneAt);
expect(state.status).toBe('ok');
});
it('is due at exactly the frequency', () => {
const now = Date.parse(CREATED) + 7 * DAY_MS;
const state = computeRoutineState(routine, [], now);
expect(state.status).toBe('due');
expect(state.health).toBe(100);
});
it('is very urgent (veryLate) when two full periods overdue', () => {
const now = Date.parse(CREATED) + 22 * DAY_MS;
const state = computeRoutineState(routine, [], now);
expect(state.status).toBe('veryLate');
expect(state.overdueDays).toBe(15);
expect(state.health).toBe(0);
});
it('is due (not late) when overdue by half a frequency', () => {
const now = Date.parse(CREATED) + 10.5 * DAY_MS;
const state = computeRoutineState(routine, [], now);
expect(state.status).toBe('due');
});
it('is late when a full period overdue', () => {
const now = Date.parse(CREATED) + 14 * DAY_MS;
const state = computeRoutineState(routine, [], now);
expect(state.status).toBe('late');
expect(state.health).toBe(50);
});
it('health of 50 means a full period overdue', () => {
const now = Date.parse(CREATED) + 7 * DAY_MS + 7 * DAY_MS;
const state = computeRoutineState(routine, [], now);
expect(state.health).toBe(50);
});
it('is due the calendar day after completion for a daily routine', () => {
const daily = { ...routine, frequencyDays: 1 };
const doneAt = '2026-09-07T20:00:00.000Z'; // yesterday evening
const now = Date.parse('2026-09-08T08:00:00.000Z'); // this morning, <24h later
const state = computeRoutineState(daily, [{ routineId: 1, doneAt }], now);
expect(state.status).toBe('due');
});
it('a daily done yesterday is late tomorrow, not today', () => {
const daily = { ...routine, frequencyDays: 1 };
const doneAt = '2026-09-07T20:00:00.000Z'; // yesterday evening
const completions = [{ routineId: 1, doneAt }];
const today = computeRoutineState(
daily,
completions,
Date.parse('2026-09-08T20:00:00.000Z'),
);
expect(today.status).toBe('due');
const tomorrow = computeRoutineState(
daily,
completions,
Date.parse('2026-09-09T20:00:00.000Z'),
);
expect(tomorrow.status).toBe('late');
});
it('is ok later the same day it was done for a daily routine', () => {
const daily = { ...routine, frequencyDays: 1 };
const doneAt = '2026-09-08T08:00:00.000Z';
const now = Date.parse('2026-09-08T20:00:00.000Z');
const state = computeRoutineState(daily, [{ routineId: 1, doneAt }], now);
expect(state.status).toBe('ok');
});
});
describe('computeHealthSeries', () => {
it('decays from 100 at creation to 0 two periods overdue when never done', () => {
const now = Date.parse(CREATED) + 22 * DAY_MS;
const series = computeHealthSeries(routine, [], now);
expect(series[0].health).toBe(100);
const at8d = series.find((p) => p.at === Date.parse(CREATED) + 8 * DAY_MS);
expect(at8d?.health).toBe(93); // 1/7 of a period overdue
const last = series[series.length - 1];
expect(last.at).toBe(now);
expect(last.health).toBe(0); // more than two full periods overdue
});
it('resets to 100 right after a completion, then decays again (sawtooth)', () => {
const doneAt = Date.parse(CREATED) + 5 * DAY_MS;
const now = doneAt + 13 * DAY_MS; // 13 days after the completion
const series = computeHealthSeries(
routine,
[{ routineId: 1, doneAt: new Date(doneAt).toISOString() }],
now,
);
const rightAfter = series.find((p) => p.at >= doneAt);
expect(rightAfter?.health).toBe(100);
// 8 days after the completion: 1 day into the 7-day period overdue.
const at8d = series.find((p) => p.at === doneAt + 8 * DAY_MS);
expect(at8d?.health).toBe(93);
});
it('a second completion resets the decay again', () => {
const first = Date.parse(CREATED) + 3 * DAY_MS;
const second = Date.parse(CREATED) + 10 * DAY_MS;
const now = Date.parse(CREATED) + 11 * DAY_MS;
const series = computeHealthSeries(
routine,
[
{ routineId: 1, doneAt: new Date(first).toISOString() },
{ routineId: 1, doneAt: new Date(second).toISOString() },
],
now,
);
// Just after the second completion the routine is back at full health.
const nearEnd = series[series.length - 1];
expect(nearEnd.health).toBe(100);
});
});
describe('computeProjectHealthSeries', () => {
it('is empty with no routines', () => {
expect(computeProjectHealthSeries([], [], Date.parse(CREATED))).toEqual([]);
});
it('averages per-routine health and ends at the current score', () => {
const second = { ...routine, id: 2, name: 'read' };
const doneAt = Date.parse(CREATED) + 5 * DAY_MS;
const now = Date.parse(CREATED) + 8 * DAY_MS;
const series = computeProjectHealthSeries(
[routine, second],
[{ routineId: 2, doneAt: new Date(doneAt).toISOString() }],
now,
);
const states = [
computeRoutineState(routine, [], now),
computeRoutineState(
second,
[{ routineId: 2, doneAt: new Date(doneAt).toISOString() }],
now,
),
];
const last = series[series.length - 1];
expect(last.at).toBe(now);
expect(last.health).toBe(computeHealthScore(states));
});
it('only counts routines alive that day', () => {
const late = {
...routine,
id: 2,
name: 'latecomer',
createdAt: new Date(Date.parse(CREATED) + 10 * DAY_MS).toISOString(),
};
const now = Date.parse(CREATED) + 11 * DAY_MS;
const series = computeProjectHealthSeries([routine, late], [], now);
const beforeLate = series.find(
(p) => p.at === Date.parse(CREATED) + 5 * DAY_MS,
);
const single = computeHealthSeries(
routine,
[],
Date.parse(CREATED) + 5 * DAY_MS,
);
expect(beforeLate?.health).toBe(single[single.length - 1].health);
});
});
describe('parseIntervalToDays', () => {
it('converts units to days', () => {
expect(parseIntervalToDays('1d')).toBe(1);
expect(parseIntervalToDays('2w')).toBe(14);
expect(parseIntervalToDays('3m')).toBe(90);
expect(parseIntervalToDays('1y')).toBe(365);
});
it('accepts whitespace and uppercase', () => {
expect(parseIntervalToDays(' 2W ')).toBe(14);
});
it('rejects invalid input', () => {
expect(parseIntervalToDays('2')).toBeNull();
expect(parseIntervalToDays('w')).toBeNull();
expect(parseIntervalToDays('2x')).toBeNull();
expect(parseIntervalToDays('-1d')).toBeNull();
expect(parseIntervalToDays('')).toBeNull();
});
});
describe('computeHealthScore', () => {
it('is 100 with no routines', () => {
expect(computeHealthScore([])).toBe(100);
});
it('averages routine healths', () => {
const now = Date.parse(CREATED) + 8 * DAY_MS;
const a = computeRoutineState(routine, [], now); // overdue 1 day
const b = computeRoutineState(
routine,
[],
Date.parse(CREATED) + 3 * DAY_MS,
);
expect(computeHealthScore([a, b])).toBe(
Math.round((a.health + b.health) / 2),
);
});
});
|