import assert from 'node:assert/strict'
import { describe, test } from 'vitest'
import { decodeWebText } from './web-text-decoder'
describe('decodeWebText', () => {
test('honours a quoted Big5 charset in the response header', () => {
const bytes = new Uint8Array([
...Buffer.from('
'),
0xb4,
0xa3,
0xa5,
0xdc,
0xab,
0x48,
0xae,
0xa7,
...Buffer.from('')
])
assert.equal(decodeWebText(bytes, 'text/html; charset="big5"'), '提示信息')
})
test('sniffs a Shift-JIS meta charset before decoding the document', () => {
const bytes = new Uint8Array([
...Buffer.from(''),
0x93,
0xfa,
0x96,
0x7b,
0x8c,
0xea,
...Buffer.from('')
])
assert.equal(decodeWebText(bytes), '日本語')
})
test('sniffs charset from an http-equiv content attribute', () => {
const bytes = new Uint8Array([
...Buffer.from(''),
0xd6,
0xd0,
0xce,
0xc4,
...Buffer.from('')
])
assert.ok(decodeWebText(bytes).includes('中文'))
})
test('falls back to UTF-8 when a server declares an unknown charset', () => {
const bytes = new TextEncoder().encode('Résumé')
assert.equal(decodeWebText(bytes, 'text/html; charset=not-a-real-encoding'), 'Résumé')
})
test('the HTTP charset takes precedence over a conflicting meta declaration', () => {
const bytes = new Uint8Array([...Buffer.from(''), 0xd6, 0xd0, 0xce, 0xc4])
assert.equal(decodeWebText(bytes, 'text/html; charset=gbk'), '中文')
})
})