Repository navigation
Expand file tree
/
Copy pathconstants.test.js
More file actions
355 lines (309 loc) · 17.9 KB
/
Copy pathconstants.test.js
File metadata and controls
355 lines (309 loc) · 17.9 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
// Node invariant tests for constants.js.
// Run with: npm run test:unit (uses the built-in node:test runner; no deps)
const test = require('node:test');
const assert = require('node:assert');
const c = Object.assign({},
require('./constants.js'),
// Split out 2026-07-30 (behavior/build-input, not literals) — the reflective
// sweeps below still cover the whole family:
require('./zoom-ladder.js'),
require('./hotkeys.js'),
require('./recent-colors.js'),
require('./recent-drops.js'));
const isStrictlyIncreasing = (arr) => arr.every((v, i) => i === 0 || v > arr[i - 1]);
test('backoff arrays are strictly increasing and positive', () => {
for (const name of ['AUTOSAVE_BACKOFF_LEVELS_MS', 'PROJECTS_CHECKOUT_RECONNECT_BACKOFF_MS']) {
const arr = c[name];
assert.ok(Array.isArray(arr) && arr.length > 0, `${name} is a non-empty array`);
assert.ok(arr.every((v) => typeof v === 'number' && v > 0), `${name} all > 0`);
assert.ok(isStrictlyIncreasing(arr), `${name} strictly increasing`);
}
});
test('all *_MS timing/threshold consts are positive numbers', () => {
for (const [k, v] of Object.entries(c)) {
if (/_MS$/.test(k) && !Array.isArray(v)) {
assert.strictEqual(typeof v, 'number', `${k} is a number`);
assert.ok(v > 0, `${k} > 0 (got ${v})`);
}
}
});
test('all *_MAX_* caps and threshold counts are positive numbers', () => {
const counts = [
'PDF_CACHE_MAX_ENTRIES', 'PDF_CACHE_MAX_BYTES', 'TAKEOFF_BACKUP_MAX_ENTRIES',
'TAKEOFF_BACKUP_MAX_BYTES', 'SAVE_LOGS_SNAPSHOT_MAX_ENTRIES',
'AUTOSAVE_BANNER_THRESHOLD', 'AUTOSAVE_RECOVERY_THRESHOLD', 'AUTOSAVE_SLOW_WINDOW',
'AUTOSAVE_SLOW_MIN_SAMPLES', 'AUTO_RECHECKOUT_MAX_PER_PROJECT', 'UNDO_STACK_SIZE',
];
for (const k of counts) {
assert.strictEqual(typeof c[k], 'number', `${k} is a number`);
assert.ok(c[k] > 0, `${k} > 0 (got ${c[k]})`);
}
});
test('TOOL ids are unique', () => {
const ids = Object.values(c.TOOL);
assert.strictEqual(new Set(ids).size, ids.length);
});
test('SCALE_MODES ids are unique', () => {
const ids = Object.values(c.SCALE_MODES);
assert.strictEqual(new Set(ids).size, ids.length);
});
test('COLORS are all valid 6-digit hex', () => {
assert.ok(c.COLORS.length > 0);
for (const col of c.COLORS) {
assert.ok(/^#[0-9a-f]{6}$/i.test(col), `invalid color: ${col}`);
}
});
test('SCALE_PRESETS each have positive pixelsPerUnit, a label, and a unit', () => {
assert.ok(c.SCALE_PRESETS.length > 0);
for (const p of c.SCALE_PRESETS) {
assert.strictEqual(typeof p.pixelsPerUnit, 'number');
assert.ok(p.pixelsPerUnit > 0, `pixelsPerUnit > 0 for ${p.label}`);
assert.ok(typeof p.label === 'string' && p.label.length > 0);
assert.ok(typeof p.unit === 'string' && p.unit.length > 0);
}
});
test('domain default arrays are non-empty', () => {
for (const arr of [c.PLUMBING_DEFAULTS.sizes, c.PLUMBING_DEFAULTS.types, c.PLUMBING_DEFAULTS.materials,
c.LINE_DEFAULTS.sizes, c.LINE_DEFAULTS.materials]) {
assert.ok(Array.isArray(arr) && arr.length > 0);
}
});
test('PDF upload timeout budget is sane (max >= base, positive rate/slack/attempts/threshold)', () => {
assert.ok(c.PDF_UPLOAD_TIMEOUT_MAX_MS >= c.PDF_UPLOAD_TIMEOUT_BASE_MS, 'max >= base');
assert.ok(c.PDF_UPLOAD_ASSUMED_BPS > 0, 'assumed bps > 0');
assert.ok(c.PDF_UPLOAD_TIMEOUT_SLACK_MS > 0, 'slack > 0');
assert.ok(Number.isInteger(c.PDF_UPLOAD_VERIFY_ATTEMPTS) && c.PDF_UPLOAD_VERIFY_ATTEMPTS > 0, 'verify attempts > 0');
assert.ok(c.PDF_RESUMABLE_THRESHOLD_BYTES > 0 && c.PDF_RESUMABLE_THRESHOLD_BYTES <= c.PDF_MAX_SIZE_BYTES, 'resumable threshold within cap');
assert.ok(c.PDF_ONESHOT_LARGE_BACKOFF_MS > c.PDF_ONESHOT_BACKOFF_MS, 'large-PDF backoff is longer than the default');
});
test('near-expiry and soft-grace stay within the inactivity window', () => {
// The keep-alive math assumes these are smaller than the full inactivity timeout.
assert.ok(c.CHECKOUT_NEAR_EXPIRY_MS < c.CHECKOUT_INACTIVITY_MS);
assert.ok(c.CHECKOUT_SOFT_GRACE_MS < c.CHECKOUT_INACTIVITY_MS);
assert.ok(c.CHECKOUT_KEEPALIVE_MS < c.CHECKOUT_INACTIVITY_MS);
});
const PRESETS = ['#e85447', '#4a9eff', '#e8c547'];
test('nextRecentColors: a preset color is never added to recents', () => {
assert.deepStrictEqual(c.nextRecentColors([], '#4a9eff', PRESETS), []);
assert.deepStrictEqual(c.nextRecentColors(['#123456'], '#e85447', PRESETS), ['#123456']);
});
test('nextRecentColors: an off-palette color is unshifted to the front', () => {
assert.deepStrictEqual(c.nextRecentColors([], '#123456', PRESETS), ['#123456']);
assert.deepStrictEqual(c.nextRecentColors(['#abcdef'], '#123456', PRESETS), ['#123456', '#abcdef']);
});
test('nextRecentColors: an existing color moves to the front without growing the list', () => {
assert.deepStrictEqual(
c.nextRecentColors(['#111111', '#222222', '#333333'], '#333333', PRESETS),
['#333333', '#111111', '#222222']
);
});
test('nextRecentColors: dedupe is case-insensitive and the stored value is lowercased', () => {
assert.deepStrictEqual(c.nextRecentColors(['#abcdef'], '#ABCDEF', PRESETS), ['#abcdef']);
assert.deepStrictEqual(c.nextRecentColors([], '#AB12CD', PRESETS), ['#ab12cd']);
});
test('nextRecentColors: caps the list at RECENT_COLORS_MAX, dropping the oldest', () => {
const max = c.RECENT_COLORS_MAX;
const full = Array.from({ length: max }, (_, i) => '#0000' + String(i).padStart(2, '0'));
const out = c.nextRecentColors(full, '#ffffff', PRESETS);
assert.strictEqual(out.length, max);
assert.strictEqual(out[0], '#ffffff');
assert.ok(!out.includes(full[full.length - 1]), 'oldest entry was dropped');
});
test('nextRecentColors: falsy/invalid color returns the list unchanged (capped)', () => {
assert.deepStrictEqual(c.nextRecentColors(['#123456'], '', PRESETS), ['#123456']);
assert.deepStrictEqual(c.nextRecentColors(['#123456'], null, PRESETS), ['#123456']);
assert.deepStrictEqual(c.nextRecentColors(['#123456'], 42, PRESETS), ['#123456']);
});
// --- nextUnusedCounterColor (recent-colors.js) ------------------------------
test('nextUnusedCounterColor: skips colors in use and returns the first free palette entry', () => {
const palette = ['#e85447', '#4a9eff', '#e8c547'];
assert.strictEqual(c.nextUnusedCounterColor([{ color: '#e8c547' }], palette, '#e8c547'), '#e85447');
assert.strictEqual(
c.nextUnusedCounterColor([{ color: '#e85447' }, { color: '#e8c547' }], palette, '#e8c547'),
'#4a9eff'
);
});
test('nextUnusedCounterColor: the in-use compare is case-insensitive', () => {
const palette = ['#e85447', '#4a9eff'];
assert.strictEqual(c.nextUnusedCounterColor([{ color: '#E85447' }], palette, '#e8c547'), '#4a9eff');
});
test('nextUnusedCounterColor: all palette entries used -> the entry after current, wrapping', () => {
const palette = ['#e85447', '#4a9eff', '#e8c547'];
const counters = palette.map(color => ({ color }));
assert.strictEqual(c.nextUnusedCounterColor(counters, palette, '#4a9eff'), '#e8c547');
assert.strictEqual(c.nextUnusedCounterColor(counters, palette, '#e8c547'), '#e85447', 'wraps past the end');
});
test('nextUnusedCounterColor: off-palette current with a fully used palette -> palette[0]', () => {
const palette = ['#e85447', '#4a9eff'];
const counters = palette.map(color => ({ color }));
assert.strictEqual(c.nextUnusedCounterColor(counters, palette, '#123456'), '#e85447');
});
test('nextUnusedCounterColor: empty counters -> palette[0]; empty palette -> current', () => {
const palette = ['#e85447', '#4a9eff'];
assert.strictEqual(c.nextUnusedCounterColor([], palette, '#e8c547'), '#e85447');
assert.strictEqual(c.nextUnusedCounterColor([{ color: '#e85447' }], [], '#e8c547'), '#e8c547');
});
// --- nextRecentDrops / formatDropLabel (recent-drops.js) --------------------
test('nextRecentDrops: a used size is unshifted; same value+unit dedupes to the front', () => {
assert.deepStrictEqual(c.nextRecentDrops([], 3, 'ft'), [{ value: 3, unit: 'ft' }]);
assert.deepStrictEqual(
c.nextRecentDrops([{ value: 10, unit: 'ft' }, { value: 3, unit: 'ft' }], 3, 'ft'),
[{ value: 3, unit: 'ft' }, { value: 10, unit: 'ft' }]
);
});
test('nextRecentDrops: same value in a different unit is a different drop', () => {
const out = c.nextRecentDrops([{ value: 3, unit: 'ft' }], 3, 'in');
assert.deepStrictEqual(out, [{ value: 3, unit: 'in' }, { value: 3, unit: 'ft' }]);
});
test('nextRecentDrops: caps at RECENT_DROPS_MAX, dropping the oldest', () => {
const max = c.RECENT_DROPS_MAX;
const full = Array.from({ length: max }, (_, i) => ({ value: i + 1, unit: 'ft' }));
const out = c.nextRecentDrops(full, 99, 'ft');
assert.strictEqual(out.length, max);
assert.deepStrictEqual(out[0], { value: 99, unit: 'ft' });
assert.ok(!out.some(d => d.value === max), 'oldest entry was dropped');
});
test('nextRecentDrops: non-positive/invalid values and junk entries are ignored', () => {
assert.deepStrictEqual(c.nextRecentDrops([{ value: 3, unit: 'ft' }], 0, 'ft'), [{ value: 3, unit: 'ft' }]);
assert.deepStrictEqual(c.nextRecentDrops([{ value: 3, unit: 'ft' }], NaN, 'ft'), [{ value: 3, unit: 'ft' }]);
assert.deepStrictEqual(c.nextRecentDrops([null, { value: -2, unit: 'ft' }, { value: 3, unit: 'ft' }], 'x', 'ft'), [{ value: 3, unit: 'ft' }]);
const input = [{ value: 3, unit: 'ft' }];
c.nextRecentDrops(input, 5, 'ft');
assert.deepStrictEqual(input, [{ value: 3, unit: 'ft' }], 'input list was not mutated');
});
test('formatDropLabel: whole numbers stay whole, fractions keep up to 2 decimals', () => {
assert.strictEqual(c.formatDropLabel(3, 'ft'), '3 ft');
assert.strictEqual(c.formatDropLabel(2.5, 'ft'), '2.5 ft');
assert.strictEqual(c.formatDropLabel(8.505, 'in'), '8.51 in');
assert.strictEqual(c.formatDropLabel(0, 'ft'), '');
assert.strictEqual(c.formatDropLabel(NaN, 'ft'), '');
});
test('nextRecentColors: tolerates a non-array list and never mutates the input', () => {
assert.deepStrictEqual(c.nextRecentColors(undefined, '#123456', PRESETS), ['#123456']);
const input = ['#abcdef'];
c.nextRecentColors(input, '#123456', PRESETS);
assert.deepStrictEqual(input, ['#abcdef'], 'input list was not mutated');
});
const { ZOOM_LADDER_STEP, snapZoomToRung, nextRungUp, nextRungDown } = c;
test('zoom ladder: snap is idempotent, monotone, clamped; up/down step exactly one rung', () => {
const lo = 0.2, hi = 4;
for (const z of [0.2, 0.25, 0.5, 0.83, 1, 1.07, 1.5, 2.2, 3.9, 4]) {
const r = snapZoomToRung(z, lo, hi);
assert.ok(r >= lo && r <= hi, 'clamped');
assert.ok(Math.abs(snapZoomToRung(r, lo, hi) - r) < 1e-12, 'idempotent');
// r is genuinely a rung: lo * step^n for integer n
const n = Math.log(r / lo) / Math.log(ZOOM_LADDER_STEP);
assert.ok(Math.abs(n - Math.round(n)) < 1e-9 || r === hi, 'on the ladder (or the clamped max)');
}
// Monotone: snapping preserves order
assert.ok(snapZoomToRung(1, lo, hi) <= snapZoomToRung(2, lo, hi));
// up/down from a rung step exactly one rung and invert each other
const rung = snapZoomToRung(1, lo, hi);
const up = nextRungUp(rung, lo, hi);
assert.ok(Math.abs(up / rung - ZOOM_LADDER_STEP) < 1e-9);
assert.ok(Math.abs(nextRungDown(up, lo, hi) - rung) < 1e-9);
// from between rungs: up -> the rung above, down -> the rung below
const mid = rung * 1.07;
assert.ok(Math.abs(nextRungUp(mid, lo, hi) - up) < 1e-9);
assert.ok(Math.abs(nextRungDown(mid, lo, hi) - rung) < 1e-9);
// clamps at the ends
assert.strictEqual(nextRungDown(lo, lo, hi), lo);
assert.strictEqual(nextRungUp(hi, lo, hi), hi);
// degenerate input
assert.strictEqual(snapZoomToRung(0, lo, hi), lo);
assert.strictEqual(snapZoomToRung(NaN, lo, hi), lo);
});
test('zoom ladder: the clamp ends behave as rungs (drag-to-max commits max)', () => {
const lo = 0.2, hi = 4;
assert.strictEqual(snapZoomToRung(hi, lo, hi), hi); // exactly max -> max
assert.strictEqual(snapZoomToRung(3.95, lo, hi), hi); // near max -> max
assert.strictEqual(snapZoomToRung(lo, lo, hi), lo); // exactly min -> min
const interior = snapZoomToRung(3.5, lo, hi);
assert.ok(interior < hi, 'well below max stays on an interior rung');
assert.ok(Math.abs(nextRungDown(hi, lo, hi) - 0.2 * Math.pow(ZOOM_LADDER_STEP, 21)) < 1e-9);
});
test('HOTKEYS: table shape — the handler/docs single source stays executable', () => {
assert.ok(Array.isArray(c.HOTKEYS) && c.HOTKEYS.length >= 20, 'HOTKEYS present');
const keys = [];
for (const h of c.HOTKEYS) {
assert.strictEqual(typeof h.action, 'string', 'action text');
assert.ok(h.section === 'Navigation' || h.section === 'Tools', 'known section');
if (h.bespoke) {
assert.ok(h.key === undefined, 'bespoke rows carry no runnable key');
continue;
}
assert.match(h.key, /^[a-z]$/, 'runnable key is a single lowercase char');
keys.push(h.key);
const ways = [h.btnId, h.runner].filter(Boolean).length;
assert.strictEqual(ways, 1, `${h.key}: exactly one of btnId/runner`);
}
assert.strictEqual(new Set(keys).size, keys.length, 'runnable keys unique');
});
test('takeoff-backup IDB record keys are pinned (restore prompt key-aside)', () => {
// The held key is a RECORD key in the existing takeoff_backup store (not a
// store name): the boot key-aside moves a promptable 'local' backup here so
// no later write can clobber it while the restore prompt is unresolved.
assert.strictEqual(c.TAKEOFF_BACKUP_HELD_ID, 'local-held');
// Must never collide with the live local record key.
assert.notStrictEqual(c.TAKEOFF_BACKUP_HELD_ID, 'local');
});
// --- nextLineTypeName (recent-colors.js) — X12, D19 fold-in -----------------
test('nextLineTypeName: an empty palette takes the bare stem', () => {
assert.strictEqual(c.nextLineTypeName([]), 'Line');
assert.strictEqual(c.nextLineTypeName(null), 'Line');
assert.strictEqual(c.nextLineTypeName(undefined), 'Line');
});
test('nextLineTypeName: a taken name numbers up rather than duplicating', () => {
assert.strictEqual(c.nextLineTypeName([{ name: 'Line' }]), 'Line 2');
assert.strictEqual(c.nextLineTypeName([{ name: 'Line' }, { name: 'Line 2' }]), 'Line 3');
// Gaps are filled, not skipped past.
assert.strictEqual(c.nextLineTypeName([{ name: 'Line' }, { name: 'Line 3' }]), 'Line 2');
});
test('nextLineTypeName: the taken compare ignores case and surrounding space', () => {
assert.strictEqual(c.nextLineTypeName([{ name: ' line ' }]), 'Line 2');
assert.strictEqual(c.nextLineTypeName([{ name: 'LINE' }, { name: 'line 2' }]), 'Line 3');
});
test('nextLineTypeName: a caller-supplied stem is honored', () => {
assert.strictEqual(c.nextLineTypeName([], '2in CPVC'), '2in CPVC');
assert.strictEqual(c.nextLineTypeName([{ name: '2in CPVC' }], '2in CPVC'), '2in CPVC 2');
// A blank or whitespace stem falls back to "Line".
assert.strictEqual(c.nextLineTypeName([], ' '), 'Line');
});
test('nextLineTypeName: malformed palette rows are ignored, not thrown on', () => {
assert.strictEqual(c.nextLineTypeName([null, {}, { name: null }, { name: 'Line' }]), 'Line 2');
});
test('normalizeProjectCodes keeps the editions, the jurisdiction and a public|private occupancy, drops the rest', () => {
assert.deepStrictEqual(c.normalizeProjectCodes({ plumbing: 'IPC 2018', jurisdiction: ' Ohio ', occupancy: 'private', electrical: 7 }), { plumbing: 'IPC 2018', jurisdiction: 'Ohio', occupancy: 'private' });
assert.deepStrictEqual(c.normalizeProjectCodes({ occupancy: 'mixed' }), null);
assert.deepStrictEqual(c.normalizeProjectCodes({ occupancy: 'public' }), { occupancy: 'public' });
assert.strictEqual(c.normalizeProjectCodes(null), null);
});
test('displaySettingsFields keeps only the defaults\' own keys, each of the default\'s type (MAP-SETTINGS)', () => {
const d = c.COUNTER_SETTINGS_DEFAULTS;
assert.deepStrictEqual(c.displaySettingsFields(d, { size: 40, opacity: 0.6, showRings: true, bogus: 1 }), { size: 40, opacity: 0.6, showRings: true });
// a wrong type or a non-finite number costs only that field
assert.deepStrictEqual(c.displaySettingsFields(d, { size: 'huge', numberSize: NaN, ringSolid: 'yes', outlineSize: 2 }), { outlineSize: 2 });
// strings stay strings (the drop icon)
assert.deepStrictEqual(c.displaySettingsFields(c.LINE_TYPE_SETTINGS_DEFAULTS, { dropIconStyle: 'plus', snapToHorizontalVertical: true }), { dropIconStyle: 'plus', snapToHorizontalVertical: true });
for (const raw of [null, undefined, 'x', 5, [1, 2]]) assert.deepStrictEqual(c.displaySettingsFields(d, raw), {});
});
test('displaySettingsFields leaves out the sidebar filter scope and its legacy booleans (their own device keys)', () => {
const out = c.displaySettingsFields(c.LINE_TYPE_SETTINGS_DEFAULTS, { sidebarFilterScope: 'page', showOnlyLineTypesOnCurrentPage: true, showOnlyLinesOnCurrentPage: true });
assert.deepStrictEqual(out, { showOnlyLinesOnCurrentPage: true });
assert.deepStrictEqual(c.displaySettingsFields(c.COUNTER_SETTINGS_DEFAULTS, { showOnlyCountersOnCurrentPage: true }), {});
// merged over a copy of the defaults, the stored blob of a full settings object round-trips
const full = Object.assign({}, c.COUNTER_SETTINGS_DEFAULTS, { size: 30 });
assert.deepStrictEqual(Object.assign({}, c.COUNTER_SETTINGS_DEFAULTS, c.displaySettingsFields(c.COUNTER_SETTINGS_DEFAULTS, JSON.parse(JSON.stringify(c.displaySettingsFields(c.COUNTER_SETTINGS_DEFAULTS, full))))), full);
});
test('the ring size default sits on its slider, and a stored size under the slider reads as the default (MAP-RING-DEFAULT)', () => {
// The Ring size slider runs 50 to 200 (app/index.html #counterRingSize); the default was 1.
assert.strictEqual(c.COUNTER_SETTINGS_DEFAULTS.ringSize, 100);
const d = c.COUNTER_SETTINGS_DEFAULTS;
const merged = (raw) => Object.assign({}, d, c.displaySettingsFields(d, raw));
assert.strictEqual(merged({ ringSize: 1, showRings: true }).ringSize, 100);
assert.strictEqual(merged({ ringSize: 1, showRings: true }).showRings, true);
assert.strictEqual(merged({ ringSize: 0 }).ringSize, 100);
assert.strictEqual(merged({ ringSize: 49 }).ringSize, 100);
assert.strictEqual(merged({ ringSize: 50 }).ringSize, 50);
assert.strictEqual(merged({ ringSize: 170 }).ringSize, 170);
});