-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.html
More file actions
619 lines (534 loc) · 22.4 KB
/
Copy pathtest.html
File metadata and controls
619 lines (534 loc) · 22.4 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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>WordTools tests</title>
<link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Crect width='32' height='32' rx='6' fill='%23538d4e'/%3E%3Ctext x='16' y='23' font-family='monospace' font-size='20' font-weight='700' fill='white' text-anchor='middle'%3ET%3C/text%3E%3C/svg%3E">
<style>
body { font: 14px/1.5 ui-monospace, SFMono-Regular, Consolas, monospace;
margin: 2rem auto; max-width: 60rem; padding: 0 1rem;
background: #14161a; color: #d7dbe0; }
h1 { font-size: 1.1rem; letter-spacing: .04em; text-transform: uppercase; color: #8b93a1; }
#summary { font-size: 1.3rem; margin: 1rem 0 1.5rem; }
.ok { color: #5ec27a; }
.bad { color: #f2777a; }
.row { padding: .15rem 0; border-bottom: 1px solid #22262d; display: flex; gap: .75rem; }
.row .mark { width: 3.2rem; flex: none; }
.row .name { flex: 1; }
.row .why { color: #f2777a; white-space: pre-wrap; }
.group { color: #8b93a1; font-weight: 700; letter-spacing: .05em; text-transform: uppercase;
font-size: .78rem; margin: 1.75rem 0 .35rem; }
.slow { color: #6f7784; }
</style>
</head>
<body>
<h1>WordTools tests</h1>
<div id="summary">running…</div>
<div id="out"></div>
<script type="module">
import {
GREEN, YELLOW, GRAY, MIN_LENGTH,
maskOf, emptyConstraints, tighten, constraintsFromRow, constraintsFromManual,
knownCounts, deriveConstraints, findConflicts, filter,
} from './js/engine.js';
import { buildIndex, load } from './js/dictionary.js';
import { encode, decode } from './js/state.js';
// ---------------------------------------------------------------- runner
const results = [];
let group = '';
const g = (name) => { group = name; };
function test(name, fn) {
try { fn(); results.push({ group, name, pass: true }); }
catch (e) { results.push({ group, name, pass: false, why: e.message }); }
}
function ok(cond, msg) { if (!cond) throw new Error(msg || 'expected truthy'); }
function eq(actual, expected, msg) {
if (actual !== expected) {
throw new Error(`${msg ? msg + ': ' : ''}expected ${JSON.stringify(expected)}, got ${JSON.stringify(actual)}`);
}
}
function deepEq(actual, expected, msg) {
const a = JSON.stringify(actual), b = JSON.stringify(expected);
if (a !== b) throw new Error(`${msg ? msg + ': ' : ''}expected ${b}, got ${a}`);
}
/** Compare a Set of chars against a plain string, order-independent. */
function setEq(set, chars, msg) {
deepEq([...set].sort(), chars.split('').sort(), msg);
}
// Build a tile row from parallel strings: letters + color codes (g/y/x/.).
// e.g. row('sract', 'xgygy') -> S gray, R green, A yellow, C green, T yellow
const COLOR = { g: GREEN, y: YELLOW, x: GRAY };
function row(letters, colors) {
return [...letters].map((ch, i) =>
ch === '.' ? null : { letter: ch, color: COLOR[colors[i]] });
}
const noManual = { include: [], noMore: [], notHere: [] };
// ---------------------------------------------------------------- masks
g('bitmask');
test('maskOf sets one bit per distinct letter', () => {
eq(maskOf('a'), 1);
eq(maskOf('b'), 2);
eq(maskOf('ab'), 3);
eq(maskOf('aab'), 3, 'duplicates do not add bits');
eq(maskOf('z'), 1 << 25);
});
// ------------------------------------------------- deriving from the row
g('row -> constraints');
test('green pins the letter to its position', () => {
const c = constraintsFromRow(row('.r.c.', '.g.g.'), 5);
deepEq(c.positions, ['', 'r', '', 'c', '']);
eq(c.min.r, 1);
eq(c.min.c, 1);
eq(c.max.r, undefined, 'green alone must not cap the count');
});
test('yellow requires the letter and bans it from that slot', () => {
const c = constraintsFromRow(row('l....', 'y....'), 5);
eq(c.min.l, 1);
setEq(c.posExclude[0], 'l');
eq(c.positions[0], '', 'yellow is not a position');
eq(c.max.l, undefined, 'yellow alone must not cap the count');
});
test('gray bans the letter outright', () => {
const c = constraintsFromRow(row('q....', 'x....'), 5);
eq(c.max.q, 0);
eq(c.min.q, undefined);
});
test('gray also excludes that position', () => {
// Matters when another tile proves the letter is elsewhere in the word.
const c = constraintsFromRow(row('e...e', 'x...y'), 5);
setEq(c.posExclude[0], 'e');
});
test('yellow + gray of one letter derives EXACTLY one', () => {
const c = constraintsFromRow(row('e...e', 'y...x'), 5);
eq(c.min.e, 1);
eq(c.max.e, 1);
});
test('two yellows + a gray derives exactly two', () => {
const c = constraintsFromRow(row('ee..e', 'yy..x'), 5);
eq(c.min.e, 2);
eq(c.max.e, 2);
});
test('green + yellow of one letter derives at least two', () => {
const c = constraintsFromRow(row('e...e', 'g...y'), 5);
eq(c.min.e, 2);
eq(c.max.e, undefined, 'no gray means no upper bound');
});
test('empty tiles are ignored', () => {
const c = constraintsFromRow(row('.....', '.....'), 5);
deepEq(c.min, {});
deepEq(c.max, {});
deepEq(c.positions, ['', '', '', '', '']);
});
test('positions still work when length is "any"', () => {
const c = constraintsFromRow(row('.r...', '.g...'), null);
eq(c.positions[1], 'r');
eq(c.length, null);
});
// ------------------------------------------------------- the letter panel
// The panel is ADDITIVE: its counts sit on top of what the tile row established.
g('panel -> constraints');
test('with an empty row it behaves like plain "must contain"', () => {
const c = constraintsFromManual({ include: [{ letter: 'e', count: 1 }] }, 5, null, {});
eq(c.min.e, 1);
eq(c.max.e, undefined);
});
test('with an empty row "no more of" is a plain ban', () => {
const c = constraintsFromManual({ noMore: ['q', 'z'] }, 5, null, {});
eq(c.max.q, 0);
eq(c.max.z, 0);
});
test('"also contains" counts on top of what the row knows', () => {
const c = constraintsFromManual({ include: [{ letter: 's', count: 1 }] }, 5, null, { s: 1 });
eq(c.min.s, 2, 'one green S plus one more');
});
test('"also contains x2" asks for two beyond the row', () => {
const c = constraintsFromManual({ include: [{ letter: 's', count: 2 }] }, 5, null, { s: 1 });
eq(c.min.s, 3);
});
test('"no more of" caps at what the row knows', () => {
const c = constraintsFromManual({ noMore: ['s'] }, 5, null, { s: 1 });
eq(c.max.s, 1, 'the known S is allowed, a second is not');
});
test('not-here strip excludes per position', () => {
const c = constraintsFromManual({ notHere: ['', '', 'lt', '', ''] }, 5);
setEq(c.posExclude[2], 'lt');
eq(c.posExclude[0].size, 0);
});
test('not-here ignores non-letters', () => {
const c = constraintsFromManual({ notHere: ['a B,3-c'] }, 5);
setEq(c.posExclude[0], 'ac');
});
test('knownCounts reads green + yellow from the row', () => {
const k = knownCounts(constraintsFromRow(row('sa..s', 'gy..y'), 5));
eq(k.s, 2);
eq(k.a, 1);
});
test('gray-only letters count as zero known', () => {
const k = knownCounts(constraintsFromRow(row('q....', 'x....'), 5));
eq(k.q, undefined, 'a gray tile establishes no copies');
});
// ---------------------------------------------------------------- merge
g('tighten');
test('min takes the larger, max takes the smaller', () => {
const a = emptyConstraints(5); a.min.e = 1; a.max.s = 3;
const b = emptyConstraints(5); b.min.e = 2; b.max.s = 1;
const m = tighten(a, b);
eq(m.min.e, 2);
eq(m.max.s, 1);
});
test('posExclude unions', () => {
const a = emptyConstraints(5); a.posExclude[0].add('x');
const b = emptyConstraints(5); b.posExclude[0].add('y');
setEq(tighten(a, b).posExclude[0], 'xy');
});
test('agreeing greens survive, disagreeing greens conflict', () => {
const a = emptyConstraints(5); a.positions[0] = 'r';
const b = emptyConstraints(5); b.positions[0] = 'r'; b.positions[2] = 'c';
const okConflicts = [];
const m = tighten(a, b, okConflicts);
eq(m.positions[0], 'r');
eq(m.positions[2], 'c');
eq(okConflicts.length, 0);
const d = emptyConstraints(5); d.positions[0] = 'z';
const bad = [];
tighten(a, d, bad);
eq(bad.length, 1);
ok(/position 1/i.test(bad[0]), `unhelpful message: ${bad[0]}`);
});
test('the panel can only tighten what the row derived', () => {
// Row: one yellow E. Panel: one more. Net: at least two.
const { constraints } = deriveConstraints(
row('e....', 'y....'),
{ include: [{ letter: 'e', count: 1 }], noMore: [], notHere: [] },
5
);
eq(constraints.min.e, 2);
});
// ------------------------------------------------- the additive panel, end to end
g('additive panel');
// Row: S green, A green, N gray, D gray, Y green -> SA__Y with no N and no D.
const saggy = () => row('sandy', 'ggxxg');
test('"no more of S" allows the green S but not a second', () => {
const { constraints, conflicts } = deriveConstraints(
saggy(), { include: [], noMore: ['s'], notHere: [] }, 5);
eq(conflicts.length, 0, 'banning a letter you already placed is not a contradiction');
eq(constraints.min.s, 1);
eq(constraints.max.s, 1);
});
test('"also contains S" demands a second S', () => {
const { constraints, conflicts } = deriveConstraints(
saggy(), { include: [{ letter: 's', count: 1 }], noMore: [], notHere: [] }, 5);
eq(conflicts.length, 0);
eq(constraints.min.s, 2);
eq(constraints.max.s, undefined);
});
test('asking for more of a letter the row ruled out IS a contradiction', () => {
const { conflicts } = deriveConstraints(
row('q....', 'x....'), { include: [{ letter: 'q', count: 1 }], noMore: [], notHere: [] }, 5);
ok(conflicts.length > 0, 'gray Q plus "also contains Q" cannot both hold');
});
// ------------------------------------------------------------ conflicts
g('conflicts');
const conflictsOf = (r, m, len) => deriveConstraints(r, m, len).conflicts;
test('a clean query reports nothing', () => {
eq(conflictsOf(row('.r.c.', '.g.g.'), noManual, 5).length, 0);
});
test('a yellow letter the row also rules out entirely', () => {
// Yellow E says "in the word"; a gray E in the same row says "that is all of them",
// so demanding another one on top is impossible.
const c = conflictsOf(row('e...e', 'y...x'),
{ ...noManual, include: [{ letter: 'e', count: 1 }] }, 5);
ok(c.length > 0, 'expected a conflict');
});
test('green letter the row rules out entirely', () => {
const c = conflictsOf(row('e...e', 'g...x'),
{ ...noManual, include: [{ letter: 'e', count: 1 }] }, 5);
ok(c.length > 0, JSON.stringify(c));
});
test('green letter excluded at its own position', () => {
const c = conflictsOf(row('r....', 'g....'), { ...noManual, notHere: ['r'] }, 5);
ok(c.some((m) => /position 1/.test(m)), JSON.stringify(c));
});
test('min above max', () => {
// Gray Q caps Q at zero; asking for two more is impossible.
const c = conflictsOf(row('q....', 'x....'),
{ ...noManual, include: [{ letter: 'q', count: 2 }] }, 5);
ok(c.length > 0, JSON.stringify(c));
});
test('more required letters than the word has slots', () => {
const c = conflictsOf(row('', ''), {
...noManual,
include: 'abcdef'.split('').map((letter) => ({ letter, count: 1 })),
}, 5);
ok(c.some((m) => /6 slots/.test(m)), JSON.stringify(c));
});
test('conflicts are de-duplicated', () => {
const c = conflictsOf(row('ee..e', 'ggx..'),
{ ...noManual, include: [{ letter: 'e', count: 1 }] }, 5);
eq(c.length, new Set(c).size);
});
// ------------------------------------------------------------- URL state
g('url state');
// A state object shaped the way ui.js keeps it.
const sample = {
length: 5,
row: row('srace', 'xgygy'),
notHere: ['', '', 'b', '', 'lt'],
include: [{ letter: 'e', count: 2 }, { letter: 'r', count: 1 }],
noMore: ['q', 'z'],
sort: 'alpha',
commonOnly: true,
};
test('a full state round-trips through the hash', () => {
const back = decode('#' + encode(sample));
eq(back.length, 5);
deepEq(back.row, sample.row);
deepEq(back.notHere, sample.notHere);
deepEq(back.include, sample.include);
deepEq(back.noMore, sample.noMore);
eq(back.sort, 'alpha');
eq(back.commonOnly, true);
});
test('re-encoding a decoded state is a fixed point', () => {
const once = encode(sample);
eq(encode(decode('#' + once)), once);
});
test('the hash format is what we documented', () => {
eq(encode(sample), 'l=5&r=srace&c=xgygy&n=..b..lt&i=e2,r1&x=qz&s=alpha&co=1');
});
test('a bare state produces just the length', () => {
eq(encode({ length: 5, row: [], notHere: [], include: [], noMore: [],
sort: 'common', commonOnly: false }), 'l=5');
});
test('a hand-edited URL cannot put a letter in both lists', () => {
const back = decode('#l=5&i=s1,e1&x=sq');
deepEq(back.include, [{ letter: 'e', count: 1 }], 'S dropped from "also contains"');
deepEq(back.noMore, ['s', 'q']);
});
test('empty tiles round-trip as gaps, not shifted letters', () => {
const s = { ...sample, row: row('.r.c.', '.g.g.'), notHere: ['', '', '', '', ''] };
const back = decode('#' + encode(s));
eq(back.row[0], null);
eq(back.row[1].letter, 'r');
eq(back.row[2], null);
eq(back.row[3].letter, 'c');
});
test('garbage in the hash is ignored, not fatal', () => {
const back = decode('#l=99&r=SR@CE&i=zzz,e&x=1234&s=bogus');
eq(back.length, undefined, 'out-of-range length dropped');
eq(decode('#l=2').length, undefined, 'two is below MIN_LENGTH now');
eq(decode('#l=3').length, 3, 'three is the shortest allowed');
eq(back.sort, undefined, 'unknown sort dropped');
deepEq(back.include, [{ letter: 'e', count: 1 }]);
deepEq(back.noMore, []);
});
test('decoded constraints match the originals through the engine', () => {
const back = decode('#' + encode(sample));
const a = deriveConstraints(sample.row,
{ include: sample.include, noMore: sample.noMore, notHere: sample.notHere }, sample.length);
const b = deriveConstraints(back.row,
{ include: back.include, noMore: back.noMore, notHere: back.notHere }, back.length);
deepEq(a.constraints.min, b.constraints.min);
deepEq(a.constraints.max, b.constraints.max);
deepEq(a.constraints.positions, b.constraints.positions);
deepEq(a.constraints.posExclude.map((s) => [...s].sort()),
b.constraints.posExclude.map((s) => [...s].sort()));
});
// -------------------------------------------- filtering, small fixture
g('filter (fixture)');
const FIX = buildIndex([
'ate', 'eat', 'tea', 'tee', 'see', 'sea', 'set',
'beet', 'been', 'bean', 'bane', 'tree', 'teem',
'crate', 'grace', 'trace', 'react', 'areca', 'erica',
].join('\n'));
function run(c) { return filter(FIX, c); }
test('length only', () => {
deepEq(run(emptyConstraints(3)), ['ate', 'eat', 'tea', 'tee', 'see', 'sea', 'set']);
});
test('length "any" spans every bucket', () => {
const c = emptyConstraints(null);
eq(run(c).length, 19);
});
test('fixed position', () => {
const c = emptyConstraints(4); c.positions[0] = 'b';
deepEq(run(c), ['beet', 'been', 'bean', 'bane']);
});
test('excluded position', () => {
const c = emptyConstraints(3); c.posExclude[0].add('t'); c.posExclude[0].add('s');
deepEq(run(c), ['ate', 'eat']);
});
test('banned letter', () => {
const c = emptyConstraints(3); c.max.s = 0;
deepEq(run(c), ['ate', 'eat', 'tea', 'tee']);
});
test('at least one of a letter', () => {
const c = emptyConstraints(4); c.min.n = 1;
deepEq(run(c), ['been', 'bean', 'bane']);
});
test('at least two of a letter', () => {
const c = emptyConstraints(4); c.min.e = 2;
deepEq(run(c), ['beet', 'been', 'tree', 'teem']);
});
test('exactly one of a letter', () => {
const c = emptyConstraints(4); c.min.e = 1; c.max.e = 1;
deepEq(run(c), ['bean', 'bane']);
});
test('required and banned together short-circuits to nothing', () => {
const c = emptyConstraints(3); c.min.s = 1; c.max.s = 0;
deepEq(run(c), []);
});
test('limit stops early', () => {
eq(filter(FIX, emptyConstraints(3), 2).length, 2);
});
test('a whole Waffle row, end to end', () => {
// S gray, R green, A yellow, C green, E yellow
const { constraints, conflicts } = deriveConstraints(row('srace', 'xgygy'), noManual, 5);
eq(conflicts.length, 0);
deepEq(run(constraints), ['areca', 'erica']);
});
// --------------------------------------- filtering, real dictionary
g('filter (real dictionary)');
const DICT = await load('data/words.txt');
test('dictionary loaded and indexed', () => {
eq(DICT.words.length, 196494);
eq(DICT.byLength[3].length, 1085);
eq(DICT.byLength[5].length, 9476);
eq(DICT.byLength[7].length, 25473);
eq(DICT.byLength[8].length, 31736);
eq(DICT.masks.length, 196494);
});
// Two-letter words are no use in the games this is for, so the list starts at 3.
test('the word list has nothing shorter than MIN_LENGTH', () => {
eq(MIN_LENGTH, 3);
eq(DICT.byLength[2], null, 'no two-letter bucket');
eq(DICT.words.filter((w) => w.length < MIN_LENGTH).length, 0);
});
const golden = (name, expected, build, sample) => test(name, () => {
const c = build();
const r = filter(DICT, c);
eq(r.length, expected, 'match count');
if (sample) for (const w of sample) ok(r.includes(w), `expected "${w}" among the matches`);
});
golden('green R at 2 and C at 4, five letters -> 36', 36,
() => constraintsFromRow(row('.r.c.', '.g.g.'), 5),
['areca', 'brace', 'bract']);
golden('at least two E, five letters -> 607', 607,
() => { const c = emptyConstraints(5); c.min.e = 2; return c; },
['abele', 'aerie']);
// Note the counts below are lower than a naive "contains A / exactly one E" query.
// That is the point: a yellow tile also excludes its own slot, and so does a gray one.
golden('yellow A at 1 + banned S, five letters -> 1836', 1836,
() => deriveConstraints(row('a....', 'y....'), { ...noManual, noMore: ['s'] }, 5).constraints,
['babel', 'baaed']);
golden('exactly one E via yellow@1 + gray@5, five letters -> 2703', 2703,
() => constraintsFromRow(row('e...e', 'y...x'), 5),
['aahed', 'abbey']);
// The same two queries without the positional half, to pin the count logic alone.
golden('contains A and no S, positions unconstrained -> 2206', 2206,
() => { const c = emptyConstraints(5); c.min.a = 1; c.max.s = 0; return c; },
['aargh', 'abaci']);
golden('exactly one E, positions unconstrained -> 3575', 3575,
() => { const c = emptyConstraints(5); c.min.e = 1; c.max.e = 1; return c; },
['abase', 'abbey']);
golden('yellow L, five letters -> 1892', 1892,
() => constraintsFromRow(row('l....', 'y....'), 5),
['abele', 'abled']);
golden('seven letters, S green, A+E required, no Q or Z -> 686', 686,
() => deriveConstraints(
row('s......', 'g......'),
{ include: [{ letter: 'a', count: 1 }, { letter: 'e', count: 1 }],
noMore: ['q', 'z'], notHere: [] },
7).constraints,
['sabered', 'saccade']);
golden('two T, T not first, five letters -> 99', 99,
() => { const c = emptyConstraints(5); c.min.t = 2; c.posExclude[0].add('t'); return c; },
['attic', 'battu']);
golden('full Waffle row S-R-A-C-E -> areca, erica', 2,
() => deriveConstraints(row('srace', 'xgygy'), noManual, 5).constraints,
['areca', 'erica']);
golden('length 8 unconstrained -> 31736', 31736, () => emptyConstraints(8));
// The scenario the additive panel was designed for: SA__Y green, N and D gray.
const SAY = () => row('sandy', 'ggxxg');
golden('SA__Y with no N or D -> 12', 12,
() => deriveConstraints(SAY(), noManual, 5).constraints,
['sassy', 'salty', 'savvy']);
golden('...plus "no more of S" -> 11, SASSY gone', 11,
() => deriveConstraints(SAY(), { ...noManual, noMore: ['s'] }, 5).constraints,
['salty', 'savvy']);
test('...plus "also contains S" -> SASSY alone', () => {
const { constraints } = deriveConstraints(
SAY(), { ...noManual, include: [{ letter: 's', count: 1 }] }, 5);
deepEq(filter(DICT, constraints), ['sassy']);
});
test('any-length query: R at index 2, exactly one R, contains X -> 159', () => {
const c = emptyConstraints(null, 2);
c.positions[1] = 'r';
c.min.r = 1; c.max.r = 1; c.min.x = 1;
const r = filter(DICT, c);
eq(r.length, 159);
ok(r.includes('aruspex'));
ok(r.includes('broadaxes'));
});
test('every result really satisfies the constraints', () => {
const { constraints } = deriveConstraints(
row('srace', 'xgygy'),
{ include: [], noMore: ['t'], notHere: ['', '', '', '', 'a'] },
5
);
for (const w of filter(DICT, constraints)) {
eq(w.length, 5, w);
eq(w[1], 'r', w);
eq(w[3], 'c', w);
ok(w.includes('a') && w.includes('e'), w);
ok(!w.includes('s') && !w.includes('t'), w);
ok(w[2] !== 'a' && w[4] !== 'e' && w[4] !== 'a', w);
}
});
// ------------------------------------------------------------ perf
g('performance');
// Only meaningful in a real browser: headless --virtual-time-budget freezes
// performance.now(), so this reads 0.0ms there and the assertion is vacuous.
test('worst realistic query stays under 50ms', () => {
const c = emptyConstraints(8);
c.min.e = 2; c.max.s = 1; c.posExclude[0].add('q');
const t0 = performance.now();
for (let i = 0; i < 5; i++) filter(DICT, c);
const per = (performance.now() - t0) / 5;
results.push({ group: 'performance', name: `(measured ${per.toFixed(1)}ms/query)`, note: true });
ok(per < 50, `took ${per.toFixed(1)}ms per query`);
});
// ---------------------------------------------------------------- render
const out = document.getElementById('out');
let lastGroup = null;
for (const r of results) {
if (r.note) {
out.insertAdjacentHTML('beforeend', `<div class="row"><span class="mark"></span><span class="name slow">${r.name}</span></div>`);
continue;
}
if (r.group !== lastGroup) {
lastGroup = r.group;
out.insertAdjacentHTML('beforeend', `<div class="group">${r.group}</div>`);
}
const mark = r.pass ? '<span class="mark ok">PASS</span>' : '<span class="mark bad">FAIL</span>';
const why = r.pass ? '' : `<span class="why">${r.why.replace(/</g, '<')}</span>`;
out.insertAdjacentHTML('beforeend',
`<div class="row">${mark}<span class="name">${r.name}</span>${why}</div>`);
}
const total = results.filter((r) => !r.note).length;
const failed = results.filter((r) => r.pass === false).length;
document.getElementById('summary').innerHTML = failed
? `<span class="bad">${failed} of ${total} failed</span>`
: `<span class="ok">all ${total} passed</span>`;
document.title = failed ? `${failed} FAILED — WordTools tests` : `all ${total} passed`;
// Dev convenience: echo the summary into the http.server access log so the suite
// can be run headlessly (`msedge --headless=new --screenshot`) and read from there.
// Harmless 404 against any static host; skipped when not served from localhost.
if (location.hostname === 'localhost' || location.hostname === '127.0.0.1') {
const detail = results.filter((r) => r.pass === false)
.map((r) => `${r.group}/${r.name}: ${r.why}`).join(' | ');
fetch(`/__tests?pass=${total - failed}&fail=${failed}&detail=${encodeURIComponent(detail)}`)
.catch(() => {});
}
</script>
</body>
</html>