oxedyne/fe2o3/fe2o3_net/lens/js/test/phrase.test.mjs
4.7 KiB, 1 run
created by r1870400018:65624, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | // phrase.test.mjs -- the word-run test of `../redact.js`, `createRunGuard`. |
| 2 | // |
| 3 | // A passphrase drawn from a list of words is `n` of them in a row, wherever it turned up. This holds |
| 4 | // the JavaScript to `fe2o3_net/tests/data/lens_phrase.tsv`, the table the Rust `lens::Phrase` is held |
| 5 | // to, so the page and the peer cover the same strings: the list in its first line, then each case as |
| 6 | // the input, `n`, whether it is a hit and the spans found (as the text of each). Offsets are the |
| 7 | // language's own (UTF-16 units here, bytes in Rust), so the table compares text, not positions. |
| 8 | // |
| 9 | // node test/phrase.test.mjs |
| 10 | import { readFileSync } from 'node:fs'; |
| 11 | import { dirname, join } from 'node:path'; |
| 12 | import { fileURLToPath } from 'node:url'; |
| 13 | import { createRunGuard } from '../redact.js'; |
| 14 | import { createRunGuard as viaWriter } from '../writer.js'; |
| 15 | import { check, finish, section } from './harness.mjs'; |
| 16 | |
| 17 | const DATA = join(dirname(fileURLToPath(import.meta.url)), '..', '..', '..', 'tests', 'data'); |
| 18 | const table = readFileSync(join(DATA, 'lens_phrase.tsv'), 'utf8').split('\n').filter((l) => l.length); |
| 19 | const list = table[0].split('\t')[1].split(' '); |
| 20 | const cases = table.slice(1).map((l) => l.split('\t')).map(([t, n, hit, spans]) => ({ text: JSON.parse(t), n: Number(n), hit: hit === '1', spans: JSON.parse(spans) })); |
| 21 | |
| 22 | section('phrase: the shared case file, ' + cases.length + ' cases'); |
| 23 | { |
| 24 | check('the table has the 40 cases Rust is tested on, and a list with hyphenated entries', cases.length === 40 && list.some((w) => w.includes('-')), cases.length + ' cases'); |
| 25 | let bad = 0, first = '', hits = 0, misses = 0; |
| 26 | for (const c of cases) { |
| 27 | const g = createRunGuard(list, c.n); |
| 28 | const got = g.spans(c.text).map(([a, b]) => c.text.slice(a, b)); |
| 29 | const ok = JSON.stringify(got) === JSON.stringify(c.spans) && g.has(c.text) === c.hit; |
| 30 | if (!ok) { bad += 1; first = first || JSON.stringify([c.text, got, c.spans]); } |
| 31 | if (c.hit) hits += 1; else misses += 1; |
| 32 | } |
| 33 | check('every case finds the spans the table says, and a hit only where it says', bad === 0, bad + ' differ, first ' + first); |
| 34 | check('the table is not one answer', hits >= 20 && misses >= 10, hits + ' hits, ' + misses + ' misses'); |
| 35 | const hy = cases.filter((c) => /drop-down|t-shirt|yo-yo|felt-tip/i.test(c.text)); |
| 36 | check('it holds the hyphenated entries a split on every non-letter cannot see', hy.length >= 5 && hy.filter((c) => c.hit).length >= 4, hy.length + ' cases'); |
| 37 | } |
| 38 | |
| 39 | section('phrase: the guard'); |
| 40 | { |
| 41 | const W = 'abacus abdomen abdominal ability able abstract absurd acid'; |
| 42 | const g = createRunGuard(list); |
| 43 | check('the writer core hands on the same guard, so a page needs one import', viaWriter === createRunGuard); |
| 44 | check('eight is the default run', g.has(W) && !g.has('abacus abdomen abdominal ability able abstract absurd')); |
| 45 | check('a run of nine is one span', g.spans(W + ' down').length === 1); |
| 46 | check('a word list may be a Set', createRunGuard(new Set(list)).has(W)); |
| 47 | check('a word list may be given in capitals', createRunGuard(list.map((w) => w.toUpperCase())).has(W)); |
| 48 | check('n may be 1', createRunGuard(list, 1).has('say acid now') && !createRunGuard(list, 1).has('say nothing')); |
| 49 | // The list is the app's, and may not be there yet. |
| 50 | let loaded = null; |
| 51 | const lazy = createRunGuard(() => loaded); |
| 52 | check('a list that is not there yet finds nothing', !lazy.has(W) && lazy.spans(W).length === 0 && lazy.mask(W, () => 'X') === W); |
| 53 | loaded = list; |
| 54 | check('and finds it once the list arrives', lazy.has(W)); |
| 55 | check('a list that throws finds nothing and does not throw', !createRunGuard(() => { throw new Error('no list'); }).has(W)); |
| 56 | check('an empty list finds nothing', !createRunGuard([]).has(W)); |
| 57 | check('a non-string finds nothing', !g.has(null) && !g.has(7) && g.spans(undefined).length === 0); |
| 58 | |
| 59 | const mark = (run) => '<' + run.length + '>'; |
| 60 | check('mask replaces each span and leaves the rest', g.mask('say ' + W + ' now, then ' + W + '.', mark) === 'say <' + W.length + '> now, then <' + W.length + '>.'); |
| 61 | check('mask leaves a string with no run as it is', g.mask('nothing to see here', mark) === 'nothing to see here'); |
| 62 | const obj = { a: W, [W]: 'k', list: ['x', 'y ' + W], n: 1, deep: { deeper: { text: W } } }; |
| 63 | const copy = JSON.stringify(obj); |
| 64 | const out = g.deep(obj, mark); |
| 65 | check('deep masks every string and every key', JSON.stringify(out) === JSON.stringify({ a: '<' + W.length + '>', ['<' + W.length + '>']: 'k', list: ['x', 'y <' + W.length + '>'], n: 1, deep: { deeper: { text: '<' + W.length + '>' } } })); |
| 66 | check('and never changes what it was given', JSON.stringify(obj) === copy); |
| 67 | let nest = { v: W }; for (let i = 0; i < 60; i++) nest = { n: nest }; |
| 68 | check('deep stops at forty levels without throwing', (() => { try { g.deep(nest, mark); return true; } catch (e) { return false; } })()); |
| 69 | } |
| 70 | |
| 71 | finish('phrase'); |