| 1 | // altinset.test.mjs — the ALT badge's names and what the description |
| 2 | // field keeps (build note I22). The field itself is driven in a real |
| 3 | // browser (internal/mapatest/images_alt*_browser_test.go). |
| 4 | |
| 5 | import { test } from 'node:test' |
| 6 | import assert from 'node:assert/strict' |
| 7 | import { badgeName, firstWords, fieldAlt, fitBytes } from './altinset.mjs' |
| 8 | import { MaxAlt } from './markup.mjs' |
| 9 | |
| 10 | test('a badge is named for what it does, starting with its visible word', () => { |
| 11 | assert.equal(badgeName(''), 'Alt text: describe this image') |
| 12 | assert.equal(badgeName('A blue door'), 'Alt text: A blue door, edit') |
| 13 | assert.equal(badgeName('A red barn in the snow at dusk, with crows'), |
| 14 | 'Alt text: A red barn in the…, edit') |
| 15 | }) |
| 16 | |
| 17 | test('first words stop at five words or forty characters', () => { |
| 18 | assert.equal(firstWords(' one two '), 'one two') |
| 19 | assert.equal(firstWords('one two three four five'), 'one two three four five') |
| 20 | assert.equal(firstWords('one two three four five six'), 'one two three four five…') |
| 21 | assert.equal(firstWords('x'.repeat(60)), `${'x'.repeat(40)}…`) |
| 22 | }) |
| 23 | |
| 24 | test('the field keeps one trimmed line, without brackets', () => { |
| 25 | assert.equal(fieldAlt(' A [red] barn \n in snow '), 'A red barn in snow') |
| 26 | assert.equal(fieldAlt(' \n '), '') |
| 27 | }) |
| 28 | |
| 29 | test('the field holds at most MaxAlt bytes, cut at a character', () => { |
| 30 | const euros = '€'.repeat(400) // 3 bytes each: 1,200 bytes in 400 units |
| 31 | assert.equal(fitBytes(euros), '€'.repeat(Math.floor(MaxAlt / 3))) |
| 32 | assert.equal(fitBytes('a'.repeat(MaxAlt)), 'a'.repeat(MaxAlt)) |
| 33 | assert.equal(fitBytes('a'.repeat(MaxAlt - 1) + '😀'), 'a'.repeat(MaxAlt - 1)) |
| 34 | }) |
| 35 | |