// fixtures/indent.test.mjs — the indent vocabulary, under plain Node. // // align.test.mjs's counterpart, and the difference between them is the // point. Alignment's value is a string from a closed set, so its tests // are about a table being consulted and anything outside it dropped. // Indent's value is a number, so these are about a CLAMP — a different // mechanism, with its own ways to be wrong. // // The cases worth having are the ones no editor can produce. // POST /d/{sqid}/updates accepts raw Yjs bytes that never met the // schema, and core/src/fragment.rs writes its output into the page // unescaped, so "99", "0" and "3; drop" are all reachable and all have // to render as something safe. core/tests/fragment.rs holds the same // three in Rust against the same fixture; both sides check // independently because a document must not indent one distance on // screen and another on paper. import { test } from 'node:test' import assert from 'node:assert/strict' import { MAX_INDENT, INDENT_STEP_IN, indentClass, normaliseIndent, indentFromClasses } from './indent.mjs' test('a real level becomes its class', () => { assert.equal(indentClass(1), 'docs-indent-1') assert.equal(indentClass(MAX_INDENT), 'docs-indent-' + MAX_INDENT) }) test('zero, null and absent are all the same answer: no indent', () => { assert.equal(indentClass(0), null) assert.equal(indentClass(null), null) assert.equal(indentClass(undefined), null) assert.equal(indentClass(''), null) }) test('a run of digits too large for any integer type still clamps', () => { // The last corner where this and core/src/fragment.rs disagreed. // Rust parses as u32, which a twenty-digit run overflows; it now // treats an overflow as "too large" and clamps, matching what // Number() does here. Before that, the same document indented at 8 in // the editor and rendered flush left for a reader with no // JavaScript. assert.equal(normaliseIndent('99999999999999999999'), MAX_INDENT) assert.equal(indentClass('99999999999999999999'), 'docs-indent-' + MAX_INDENT) }) test('a level past the maximum clamps rather than being emitted', () => { assert.equal(normaliseIndent(99), MAX_INDENT) assert.equal(indentClass(99), 'docs-indent-' + MAX_INDENT) // The failure this guards against is docs-indent-99 reaching a class // attribute — the value would have come from the document. assert.equal(indentClass(99).includes('99'), false) }) test('anything that is not a whole number renders flush left', () => { for (const bad of ['3; drop', 'x', '1.5', -2, NaN, {}, [], '