-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.html
More file actions
391 lines (352 loc) · 17 KB
/
Copy pathtests.html
File metadata and controls
391 lines (352 loc) · 17 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Conditioning Guide — tests</title>
<style>
body { font-family: ui-monospace, "SF Mono", Menlo, monospace; padding: 20px; background:#fafafa; color:#222; max-width: 900px; }
h1 { font-family: Georgia, serif; }
.summary { padding: 10px 14px; border-radius: 8px; margin-bottom: 14px; font-weight: 600; }
.summary.pass { background: #e7f3e3; color: #2f5e22; }
.summary.fail { background: #f7e0e0; color: #7a1f1f; }
.row { padding: 6px 10px; border-bottom: 1px solid #eee; }
.row.pass::before { content: "✓ "; color: #3e9e5e; font-weight: 600; }
.row.fail::before { content: "✗ "; color: #c64545; font-weight: 600; }
.row.fail { background: #fdf3f3; }
.why { color: #888; margin-left: 18px; font-size: 12px; }
iframe { position: absolute; left: -9999px; width: 1px; height: 1px; }
button { padding: 8px 14px; border-radius: 6px; border: 1px solid #ccc; background: white; cursor: pointer; }
</style>
</head>
<body>
<h1>Conditioning Guide — smoke tests</h1>
<p>Loads <code>index.html?test=1</code> in a hidden iframe and exercises the real helper functions against fixture data.</p>
<button onclick="location.reload()">Re-run tests</button>
<div id="summary" class="summary">Running…</div>
<div id="results"></div>
<iframe id="app"></iframe>
<script>
const results = [];
let app = null; // contentWindow of the iframe
/* Load index.html via fetch, strip the Chart.js CDN script (which would
* block iframe parsing offline / in CI), then inject the rest via srcdoc.
* Append ?test=1 by appending a tiny inline script that flips location
* — but srcdoc has its own location, so we instead just hard-code the
* test-mode signal by injecting a sentinel script before init runs. */
(async function bootIframe() {
let html;
try {
html = await (await fetch('index.html')).text();
} catch (e) {
document.getElementById('summary').textContent = 'Could not fetch index.html: ' + e.message;
document.getElementById('summary').classList.add('fail');
return;
}
// Strip the Chart.js CDN script tag — we don't draw any charts in tests
// and the network fetch can stall in headless runs.
const closeTag = '<' + '/script>';
html = html.replace(new RegExp('<script src="https://cdn\\.jsdelivr[^>]*>' + closeTag), '');
// Inject a sentinel that flips test mode without needing a query string.
const openTag = '<' + 'script>';
html = html.replace('<head>',
'<head>' + openTag + 'window.__forceTestMode = true;' + closeTag);
const iframe = document.getElementById('app');
iframe.addEventListener('load', () => {
app = iframe.contentWindow;
const start = Date.now();
(function waitReady() {
if (app.__cgTestReady) { run(); return; }
if (Date.now() - start > 5000) {
document.getElementById('summary').textContent = 'Iframe did not signal ready within 5s';
document.getElementById('summary').classList.add('fail');
return;
}
setTimeout(waitReady, 30);
})();
});
iframe.srcdoc = html;
})();
function assert(name, cond, why) {
results.push({ name, pass: !!cond, why: cond ? '' : (why || '') });
}
function eq(name, actual, expected) {
const pass = JSON.stringify(actual) === JSON.stringify(expected);
results.push({
name, pass,
why: pass ? '' : `expected ${JSON.stringify(expected)}, got ${JSON.stringify(actual)}`
});
}
/* Reset the iframe's `state` to a clean fixture between tests so one
* test doesn't pollute the next. The iframe never calls loadState(), so
* we own the world. */
function reset(over) {
app.state = {
vitals: [],
activity: [],
ekg: [],
bloodSugar: [],
stageDecisions: [],
doctorNotes: [],
config: { ...app.DEFAULT_CONFIG },
unsynced: [],
lastSyncAt: null,
...(over || {})
};
}
function run() {
/* ─── date helpers ─────────────────────────────────────────────── */
eq('addDays adds positive days',
app.addDays('2026-06-08', 5), '2026-06-13');
eq('addDays handles month rollover',
app.addDays('2026-06-30', 2), '2026-07-02');
eq('addDays handles negative',
app.addDays('2026-06-08', -8), '2026-05-31');
eq('mondayOf a Wednesday → that week\'s Monday',
app.mondayOf('2026-06-10'), '2026-06-08');
eq('mondayOf a Sunday → previous Monday',
app.mondayOf('2026-06-14'), '2026-06-08');
eq('mondayOf a Monday → itself',
app.mondayOf('2026-06-08'), '2026-06-08');
/* ─── dayState + streak ────────────────────────────────────────── */
reset();
eq('dayState with no entries → empty',
app.dayState('2026-06-01'), 'empty');
reset({
activity: [{ id: 'a1', date: '2026-06-01', isProgrammed: true, totalMinutes: 20 }]
});
eq('dayState with a programmed activity → programmed',
app.dayState('2026-06-01'), 'programmed');
reset({
activity: [{ id: 'a1', date: '2026-06-01', isProgrammed: false, totalMinutes: 10 }]
});
eq('dayState with non-programmed activity → light',
app.dayState('2026-06-01'), 'light');
reset({
config: { ...app.DEFAULT_CONFIG, restDays: ['2026-06-01'] }
});
eq('dayState with a planned rest day → rest',
app.dayState('2026-06-01'), 'rest');
// Streak — three consecutive programmed days ending today
const today = app.todayISO();
reset({
activity: [
{ id: '1', date: app.addDays(today, -2), isProgrammed: true, totalMinutes: 15 },
{ id: '2', date: app.addDays(today, -1), isProgrammed: true, totalMinutes: 15 },
{ id: '3', date: today, isProgrammed: true, totalMinutes: 15 }
]
});
eq('computeStreak across 3 consecutive programmed days', app.computeStreak(), 3);
// Streak with a gap → resets
reset({
activity: [
{ id: '1', date: app.addDays(today, -3), isProgrammed: true, totalMinutes: 15 },
// gap on -2
{ id: '3', date: app.addDays(today, -1), isProgrammed: true, totalMinutes: 15 },
{ id: '4', date: today, isProgrammed: true, totalMinutes: 15 }
]
});
eq('computeStreak stops at a gap', app.computeStreak(), 2);
/* ─── weekSnapshot ─────────────────────────────────────────────── */
reset({
activity: [
{ id: '1', date: '2026-06-08', isProgrammed: true, totalMinutes: 20, minutesPerRound: 10, borg: 11 },
{ id: '2', date: '2026-06-09', isProgrammed: true, totalMinutes: 15, minutesPerRound: 15, borg: 12 },
{ id: '3', date: '2026-06-11', isProgrammed: true, totalMinutes: 30, minutesPerRound: 12, borg: 11 }
],
vitals: [
{ id: 'v1', date: '2026-06-08', restingHR: 64 },
{ id: 'v2', date: '2026-06-10', restingHR: 70 }
]
});
const snap = app.weekSnapshot('2026-06-08');
eq('weekSnapshot.daysActive — 3 programmed days', snap.daysActive, 3);
eq('weekSnapshot.longestContinuous — max minutesPerRound', snap.longestContinuous, 15);
eq('weekSnapshot.totalProgrammed — sum of totalMinutes', snap.totalProgrammed, 65);
eq('weekSnapshot.typicalBorg — mean to 1dp', snap.typicalBorg, 11.3);
eq('weekSnapshot.hrRange — [min, max]', snap.hrRange, [64, 70]);
assert('weekSnapshot.anySymptoms — false when none flagged', snap.anySymptoms === false);
/* ─── No home-screen reminders — everything is inline now ──────── */
reset({
config: { ...app.DEFAULT_CONFIG, weightTrackingEnabled: true, weightBaselineLb: 150,
doctorRanges: { ...app.DEFAULT_CONFIG.doctorRanges, hrFloor: 55, hrCeiling: 90 } },
vitals: [
{ id: 'v1', date: app.addDays(today, -1), weight: 150, restingHR: 50 },
{ id: 'v2', date: today, weight: 155, restingHR: 95 }
]
});
assert('checkPlanReminders is always empty (inline markers replace banners)',
app.checkPlanReminders().length === 0,
'got: ' + JSON.stringify(app.checkPlanReminders()));
/* ─── Inline HR marker on vitals entry ─────────────────────────── */
reset({
config: { ...app.DEFAULT_CONFIG, doctorRanges: { ...app.DEFAULT_CONFIG.doctorRanges, hrFloor: 55, hrCeiling: 90 } }
});
let metaV = app.entryMeta('vitals', { date: today, restingHR: 50 });
assert('HR below floor → vitals subtitle marks "below range"',
/HR 50.*below range/.test(metaV.subtitle), 'got: ' + metaV.subtitle);
metaV = app.entryMeta('vitals', { date: today, restingHR: 95 });
assert('HR above ceiling → vitals subtitle marks "above range"',
/HR 95.*above range/.test(metaV.subtitle), 'got: ' + metaV.subtitle);
metaV = app.entryMeta('vitals', { date: today, restingHR: 70 });
assert('HR in range → vitals subtitle has no marker',
!/range/.test(metaV.subtitle), 'got: ' + metaV.subtitle);
reset(); // default config — HR range fields blank
metaV = app.entryMeta('vitals', { date: today, restingHR: 50 });
assert('HR with no range configured → no marker',
!/range/.test(metaV.subtitle), 'got: ' + metaV.subtitle);
reset({
config: { ...app.DEFAULT_CONFIG, doctorRanges: { ...app.DEFAULT_CONFIG.doctorRanges, hrFloor: 0, hrCeiling: 0 } }
});
metaV = app.entryMeta('vitals', { date: today, restingHR: 50 });
assert('HR range stored as 0 (from prior bug) → still no marker',
!/range/.test(metaV.subtitle), 'got: ' + metaV.subtitle);
/* ─── Inline weight gain marker on vitals entry ────────────────── */
const yesterday = app.addDays(today, -1);
reset({
config: { ...app.DEFAULT_CONFIG, weightTrackingEnabled: true, weightBaselineLb: 150 },
vitals: [
{ id: 'v1', date: yesterday, weight: 150 },
{ id: 'v2', date: today, weight: 153 } // +3 lb, > 2.5 threshold
]
});
// entryMeta reads state.vitals for the prior-weight lookup. Pass the
// record directly — state already holds the fixture set by reset().
metaV = app.entryMeta('vitals', { id: 'v2', date: today, weight: 153 });
assert('weight: +3 lb day-over-day → vitals subtitle marks the gain',
/day-over-day/.test(metaV.subtitle), 'got: ' + metaV.subtitle);
reset({
config: { ...app.DEFAULT_CONFIG, weightTrackingEnabled: true, weightBaselineLb: 150 },
vitals: [
{ id: 'v1', date: yesterday, weight: 150 },
{ id: 'v2', date: today, weight: 151 } // +1 lb, under threshold
]
});
metaV = app.entryMeta('vitals', { id: 'v2', date: today, weight: 151 });
assert('weight: +1 lb day-over-day → no marker',
!/day-over-day/.test(metaV.subtitle), 'got: ' + metaV.subtitle);
reset({
config: { ...app.DEFAULT_CONFIG, weightTrackingEnabled: true, weightBaselineLb: 150 },
vitals: [
{ id: 'v1', date: yesterday, weight: 155 },
{ id: 'v2', date: today, weight: 150 } // -5 lb. Gain-only logic — no flag.
]
});
metaV = app.entryMeta('vitals', { id: 'v2', date: today, weight: 150 });
assert('weight: 5 lb LOSS → no flag (gain-only logic)',
!/day-over-day/.test(metaV.subtitle), 'got: ' + metaV.subtitle);
reset({
config: { ...app.DEFAULT_CONFIG, weightTrackingEnabled: false },
vitals: [
{ id: 'v1', date: yesterday, weight: 150 },
{ id: 'v2', date: today, weight: 158 } // +8 lb, but tracking off
]
});
metaV = app.entryMeta('vitals', { id: 'v2', date: today, weight: 158 });
assert('weight tracking disabled → no marker even for big gain',
!/day-over-day/.test(metaV.subtitle), 'got: ' + metaV.subtitle);
/* ─── BS never fires home-screen reminders ─────────────────────── */
reset({
config: {
...app.DEFAULT_CONFIG,
doctorRanges: {
...app.DEFAULT_CONFIG.doctorRanges,
bsFasting: { low: 80, high: 120 }
}
},
bloodSugar: [
{ id: 'b1', dateTime: today + 'T07:00:00', value: 145, context: 'fasting' }
]
});
r = app.checkPlanReminders();
assert('BS out-of-range does NOT raise a home-screen reminder',
!r.some(x => x.msg.toLowerCase().includes('blood sugar')),
'BS should be inline-only now; got ' + JSON.stringify(r));
/* ─── BS inline marker via entryMeta — now on the title ────────── */
reset({
config: {
...app.DEFAULT_CONFIG,
doctorRanges: {
...app.DEFAULT_CONFIG.doctorRanges,
bsFasting: { low: 80, high: 120 }
}
}
});
let meta = app.entryMeta('bloodSugar', { value: 145, context: 'fasting', dateTime: today + 'T07:00:00' });
assert('BS entry above range → title marks "above range"',
/above range/.test(meta.title), 'got: ' + meta.title);
meta = app.entryMeta('bloodSugar', { value: 75, context: 'fasting', dateTime: today + 'T07:00:00' });
assert('BS entry below range → title marks "below range"',
/below range/.test(meta.title), 'got: ' + meta.title);
meta = app.entryMeta('bloodSugar', { value: 100, context: 'fasting', dateTime: today + 'T07:00:00' });
assert('BS entry in range → title has no marker',
!/range/.test(meta.title), 'got: ' + meta.title);
// Empty range — no marker should appear regardless of value.
reset(); // default config has all BS ranges as { low: null, high: null }
meta = app.entryMeta('bloodSugar', { value: 145, context: 'fasting', dateTime: today + 'T07:00:00' });
assert('BS entry with no range configured → no marker',
!/range/.test(meta.title), 'got: ' + meta.title);
// Defensive: 0s left over from the earlier blank-save bug should be
// treated as "unset", not as a real range of zero.
reset({
config: {
...app.DEFAULT_CONFIG,
doctorRanges: {
...app.DEFAULT_CONFIG.doctorRanges,
bsFasting: { low: 0, high: 0 }
}
}
});
meta = app.entryMeta('bloodSugar', { value: 145, context: 'fasting', dateTime: today + 'T07:00:00' });
assert('BS range stored as 0 (from prior bug) → still no marker',
!/range/.test(meta.title), 'got: ' + meta.title);
/* ─── doctor notes excluded from collectAllEntries ─────────────── */
reset({
vitals: [{ id: 'v1', date: today, restingHR: 65 }],
activity: [{ id: 'a1', date: today, isProgrammed: true, totalMinutes: 20 }],
doctorNotes: [
{ id: 'd1', date: today, body: 'Ask about beta blocker timing' },
{ id: 'd2', date: today, body: 'Any new exercises ok?' }
]
});
const allEntries = app.collectAllEntries();
assert('collectAllEntries excludes doctorNotes',
!allEntries.some(it => it.type === 'doctorNotes'),
'leaked: ' + JSON.stringify(allEntries.map(x => x.type)));
assert('collectAllEntries still includes vitals + activity',
allEntries.some(it => it.type === 'vitals') &&
allEntries.some(it => it.type === 'activity'));
/* ─── rollingMean ──────────────────────────────────────────────── */
eq('rollingMean of single value', app.rollingMean([10], 7), [10]);
eq('rollingMean of constant series', app.rollingMean([5, 5, 5, 5], 7), [5, 5, 5, 5]);
eq('rollingMean skips nulls', app.rollingMean([10, null, 20], 7), [10, 10, 15]);
eq('rollingMean window respected',
app.rollingMean([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3),
[1, 1.5, 2, 3, 4, 5, 6, 7, 8, 9]);
/* ─── Monday prompt scheduling ─────────────────────────────────── */
const thisMon = app.mondayOf(today);
const lastMon = app.addDays(thisMon, -7);
reset({ config: { ...app.DEFAULT_CONFIG, firstRunComplete: false } });
assert('mondayPromptDue is false before first run is complete',
app.mondayPromptDue() === false);
reset({ config: { ...app.DEFAULT_CONFIG, firstRunComplete: true, lastMondayPromptWeek: '' } });
assert('mondayPromptDue is true when never shown',
app.mondayPromptDue() === true);
reset({ config: { ...app.DEFAULT_CONFIG, firstRunComplete: true, lastMondayPromptWeek: thisMon } });
assert('mondayPromptDue is false after shown this week',
app.mondayPromptDue() === false);
reset({ config: { ...app.DEFAULT_CONFIG, firstRunComplete: true, lastMondayPromptWeek: lastMon } });
assert('mondayPromptDue is true once a new week begins',
app.mondayPromptDue() === true);
/* ─── render summary ───────────────────────────────────────────── */
const passed = results.filter(r => r.pass).length;
const failed = results.length - passed;
const summary = document.getElementById('summary');
summary.textContent = `${passed} passed, ${failed} failed (${results.length} total)`;
summary.classList.add(failed === 0 ? 'pass' : 'fail');
document.getElementById('results').innerHTML = results.map(r =>
`<div class="row ${r.pass ? 'pass' : 'fail'}">${r.name}${r.why ? `<div class="why">${r.why}</div>` : ''}</div>`
).join('');
}
</script>
</body>
</html>