11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// From: https://github.com/w3c/web-platform-tests/blob/d74324b53c/encoding/textdecoder-fatal-streaming.html
41cb0ef41Sopenharmony_ci// With the twist that we specifically test for Node.js error codes
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst common = require('../common');
71cb0ef41Sopenharmony_ciconst assert = require('assert');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciif (!common.hasIntl)
101cb0ef41Sopenharmony_ci  common.skip('missing Intl');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci{
131cb0ef41Sopenharmony_ci  [
141cb0ef41Sopenharmony_ci    { encoding: 'utf-8', sequence: [0xC0] },
151cb0ef41Sopenharmony_ci    { encoding: 'utf-16le', sequence: [0x00] },
161cb0ef41Sopenharmony_ci    { encoding: 'utf-16be', sequence: [0x00] },
171cb0ef41Sopenharmony_ci  ].forEach((testCase) => {
181cb0ef41Sopenharmony_ci    const data = new Uint8Array([testCase.sequence]);
191cb0ef41Sopenharmony_ci    assert.throws(
201cb0ef41Sopenharmony_ci      () => {
211cb0ef41Sopenharmony_ci        const decoder = new TextDecoder(testCase.encoding, { fatal: true });
221cb0ef41Sopenharmony_ci        decoder.decode(data);
231cb0ef41Sopenharmony_ci      }, {
241cb0ef41Sopenharmony_ci        code: 'ERR_ENCODING_INVALID_ENCODED_DATA',
251cb0ef41Sopenharmony_ci        name: 'TypeError',
261cb0ef41Sopenharmony_ci        message:
271cb0ef41Sopenharmony_ci          `The encoded data was not valid for encoding ${testCase.encoding}`
281cb0ef41Sopenharmony_ci      }
291cb0ef41Sopenharmony_ci    );
301cb0ef41Sopenharmony_ci  });
311cb0ef41Sopenharmony_ci}
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci{
341cb0ef41Sopenharmony_ci  const decoder = new TextDecoder('utf-16le', { fatal: true });
351cb0ef41Sopenharmony_ci  const odd = new Uint8Array([0x00]);
361cb0ef41Sopenharmony_ci  const even = new Uint8Array([0x00, 0x00]);
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  assert.throws(
391cb0ef41Sopenharmony_ci    () => {
401cb0ef41Sopenharmony_ci      decoder.decode(even, { stream: true });
411cb0ef41Sopenharmony_ci      decoder.decode(odd);
421cb0ef41Sopenharmony_ci    }, {
431cb0ef41Sopenharmony_ci      code: 'ERR_ENCODING_INVALID_ENCODED_DATA',
441cb0ef41Sopenharmony_ci      name: 'TypeError',
451cb0ef41Sopenharmony_ci      message:
461cb0ef41Sopenharmony_ci        'The encoded data was not valid for encoding utf-16le'
471cb0ef41Sopenharmony_ci    }
481cb0ef41Sopenharmony_ci  );
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  assert.throws(
511cb0ef41Sopenharmony_ci    () => {
521cb0ef41Sopenharmony_ci      decoder.decode(odd, { stream: true });
531cb0ef41Sopenharmony_ci      decoder.decode(even);
541cb0ef41Sopenharmony_ci    }, {
551cb0ef41Sopenharmony_ci      code: 'ERR_ENCODING_INVALID_ENCODED_DATA',
561cb0ef41Sopenharmony_ci      name: 'TypeError',
571cb0ef41Sopenharmony_ci      message:
581cb0ef41Sopenharmony_ci        'The encoded data was not valid for encoding utf-16le'
591cb0ef41Sopenharmony_ci    }
601cb0ef41Sopenharmony_ci  );
611cb0ef41Sopenharmony_ci}
62