import { test } from 'node:test'; import assert from 'node:assert/strict'; import { checks, run } from './main.js'; const byLabel = () => new Map(checks().map((r) => [r.label, r.value])); test('length 数的是 UTF-16 码元:emoji 是 2,码点迭代是 1', () => { const m = byLabel(); assert.equal(m.get("'abc'.length"), 3); assert.equal(m.get("'😀'.length"), 2); assert.equal(m.get("[...'😀'].length"), 1); assert.equal(m.get("'😀'[0].length"), 1, '下标取到的是半个代理对'); }); test('codePointAt 与 charCodeAt 在增补平面字符上不同', () => { const m = byLabel(); assert.equal(m.get("'😀'.codePointAt(0).toString(16)"), '1f600'); assert.equal(m.get("'😀'.charCodeAt(0).toString(16)"), 'd83d'); }); test('按码元反转会破坏代理对,按码点反转才正确', () => { const m = byLabel(); assert.equal(m.get("[...'😀a'].reverse().join('')"), 'a😀'); assert.equal(m.get("'😀a'.split('').reverse().join('') === 'a😀'"), false); }); test('组合字符与预组合字符肉眼相同但 === 为 false,NFC 归一化后相等', () => { const m = byLabel(); assert.equal(m.get("'é' === 'e\\u0301'"), false); assert.equal(m.get("'é' === 'e\\u0301'.normalize('NFC')"), true); assert.equal(m.get("'é'.length / 'e\\u0301'.length"), '1 / 2'); }); test('UTF-8 字节长度与 UTF-16 长度不同:一个 emoji 是 4 字节', () => { assert.equal(byLabel().get("TextEncoder().encode('😀').length"), 4); }); test('run 返回 13 行并打印两条要点', () => { const out = []; const rows = run((s) => out.push(s)); assert.equal(rows.length, 13); assert.equal(out.length, 16); assert.match(out.at(-1), /TextEncoder/); });