oxedyne/fe2o3/fe2o3_net/lens/js/test/clip.test.mjs
4.7 KiB, 1 run
created by r1870400018:66846, 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 | // clip.test.mjs -- `clipChars` and `soundText` of `../redact.js`: a string is cut at a character, never inside one. |
| 2 | // |
| 3 | // JSON.stringify writes half of a surrogate pair as a `\udXXX` escape, which the peer's strict reader |
| 4 | // (`fe2o3_net::lens::chunk`) refuses and so covers the whole bundle. No row may hold half a pair, so a |
| 5 | // page clips with the one and mends what arrives broken with the other. |
| 6 | // |
| 7 | // node test/clip.test.mjs |
| 8 | import * as redact from '../redact.js'; |
| 9 | import * as writer from '../writer.js'; |
| 10 | import { check, finish, part, section } from './harness.mjs'; |
| 11 | import { makeHost, makeWriter } from './host.mjs'; |
| 12 | |
| 13 | const E = '\u{1F600}'; |
| 14 | // Half a pair, as the text holds it, and as JSON.stringify would write it. |
| 15 | const lone = (s) => /[\uD800-\uDBFF](?![\uDC00-\uDFFF])/.test(s) || /(?:^|[^\uD800-\uDBFF])[\uDC00-\uDFFF]/.test(s); |
| 16 | const escaped = (s) => /\\ud[89a-f][0-9a-f]{2}/i.test(JSON.stringify(s)); |
| 17 | |
| 18 | section('clip: a string is cut at a character'); |
| 19 | await part(async () => { |
| 20 | // An emoji (two units) at each offset round the cut at forty, which falls inside it at one of them. |
| 21 | let naive = false, bad = '', short = ''; |
| 22 | for (let pad = 36; pad <= 43; pad++) { |
| 23 | const s = 'x'.repeat(pad) + E.repeat(4) + ' the rest'; |
| 24 | const c = redact.clipChars(s, 40); |
| 25 | naive = naive || lone(s.slice(0, 40)); |
| 26 | if (lone(c) || escaped(c) || !s.startsWith(c)) bad += ' ' + pad; |
| 27 | if (c.length < 39 || c.length > 40) short += ' ' + pad + ':' + c.length; |
| 28 | } |
| 29 | check('the cut this test makes does split a pair when it is made by unit', naive); |
| 30 | check('no cut leaves half of a pair, and each keeps what it can', bad === '' && short === '', 'bad' + bad + ' short' + short); |
| 31 | check('a string within the limit is returned as it is', redact.clipChars('short', 40) === 'short' && redact.clipChars(E, 2) === E); |
| 32 | check('a pair that fits whole is kept', redact.clipChars('a' + E + 'b', 3) === 'a' + E); |
| 33 | check('a cut at nothing is nothing', redact.clipChars('abc', 0) === ''); |
| 34 | }); |
| 35 | |
| 36 | section('sound: a lone half becomes U+FFFD'); |
| 37 | await part(async () => { |
| 38 | const cases = [ [ 'a\uD83Db', 'a�b' ], [ '\uDE00 hi', '� hi' ], [ 'ends\uD83D', 'ends�' ], [ 'two\uD83D\uD83Dhalves', 'two��halves' ] ]; |
| 39 | check('each half that has no other half is replaced, and nothing else is', cases.every(([ s, want ]) => redact.soundText(s) === want), |
| 40 | cases.map(([ s ]) => escape(redact.soundText(s))).join(' ')); |
| 41 | check('and what it returns holds no half to be written as an escape', cases.every(([ s ]) => !lone(redact.soundText(s)) && !escaped(redact.soundText(s)))); |
| 42 | check('a whole pair, and plain text, are left as they are', redact.soundText(E + ' ok') === E + ' ok' && redact.soundText('plain, é') === 'plain, é'); |
| 43 | }); |
| 44 | |
| 45 | section('writer: the core hands both on, and its own clip cuts at a character'); |
| 46 | await part(async () => { |
| 47 | check('a page needs one import', typeof writer.clipChars === 'function' && writer.clipChars === redact.clipChars && writer.soundText === redact.soundText); |
| 48 | const h = makeHost({ fastTimers: true, respond: () => 500 }); |
| 49 | const w = makeWriter(h); |
| 50 | // 199 units, then a pair: the writer's clip at 200 falls between its halves. |
| 51 | w.noteError('word '.repeat(39) + 'abcd' + E + ' tail', ''); |
| 52 | const row = w.outbox()[0]; |
| 53 | check('an error message clipped at 200 holds no half of a pair', !lone(JSON.parse(row.data).msg) && !/\\ud[89a-f][0-9a-f]{2}/i.test(row.data), row.data.slice(-40)); |
| 54 | h.halt(); |
| 55 | }); |
| 56 | |
| 57 | section('writer: the egress trim cuts at a character, and a lone half is mended on the way in'); |
| 58 | await part(async () => { |
| 59 | // QA #4 of the Opus bundle: `fit` trimmed the largest string by unit, so a trim that fell inside an emoji |
| 60 | // left half of it, and the peer's strict reader covered the whole row. Eight lengths, as the report ran them. |
| 61 | const bad = []; |
| 62 | for (let pad = 0; pad < 8; pad++) { |
| 63 | const h = makeHost({ fastTimers: true, respond: () => 500 }); |
| 64 | const w = makeWriter(h); |
| 65 | w.noteError('x'.repeat(pad) + E.repeat(98), ''); |
| 66 | const row = w.outbox()[0]; |
| 67 | if (!row || /\\ud[89a-f][0-9a-f]{2}/i.test(row.data) || lone(JSON.parse(row.data).msg || '')) bad.push(pad); |
| 68 | h.halt(); |
| 69 | } |
| 70 | check('no row the trim cuts holds half of a pair, at any of eight lengths', bad.length === 0, 'rows with half a pair, by pad: ' + bad.join(',')); |
| 71 | |
| 72 | // A lone half in the message as it arrives is mended by the writer's own clip, to U+FFFD, and not passed on. |
| 73 | const h = makeHost({ fastTimers: true, respond: () => 500 }); |
| 74 | const w = makeWriter(h); |
| 75 | w.noteError('broke here \uD83D and \uDE00 there', ''); |
| 76 | const data = w.outbox()[0].data; |
| 77 | check('a lone half in a message reaches the row as U+FFFD, never as an escape', !/\\ud[89a-f][0-9a-f]{2}/i.test(data) && JSON.parse(data).msg === 'broke here \uFFFD and \uFFFD there', data.slice(-60)); |
| 78 | h.halt(); |
| 79 | }); |
| 80 | |
| 81 | finish('clip'); |