// altinset.test.mjs — the ALT badge's names and what the description // field keeps (build note I22). The field itself is driven in a real // browser (internal/mapatest/images_alt*_browser_test.go). import { test } from 'node:test' import assert from 'node:assert/strict' import { badgeName, firstWords, fieldAlt, fitBytes } from './altinset.mjs' import { MaxAlt } from './markup.mjs' test('a badge is named for what it does, starting with its visible word', () => { assert.equal(badgeName(''), 'Alt text: describe this image') assert.equal(badgeName('A blue door'), 'Alt text: A blue door, edit') assert.equal(badgeName('A red barn in the snow at dusk, with crows'), 'Alt text: A red barn in the…, edit') }) test('first words stop at five words or forty characters', () => { assert.equal(firstWords(' one two '), 'one two') assert.equal(firstWords('one two three four five'), 'one two three four five') assert.equal(firstWords('one two three four five six'), 'one two three four five…') assert.equal(firstWords('x'.repeat(60)), `${'x'.repeat(40)}…`) }) test('the field keeps one trimmed line, without brackets', () => { assert.equal(fieldAlt(' A [red] barn \n in snow '), 'A red barn in snow') assert.equal(fieldAlt(' \n '), '') }) test('the field holds at most MaxAlt bytes, cut at a character', () => { const euros = '€'.repeat(400) // 3 bytes each: 1,200 bytes in 400 units assert.equal(fitBytes(euros), '€'.repeat(Math.floor(MaxAlt / 3))) assert.equal(fitBytes('a'.repeat(MaxAlt)), 'a'.repeat(MaxAlt)) assert.equal(fitBytes('a'.repeat(MaxAlt - 1) + '😀'), 'a'.repeat(MaxAlt - 1)) })