test-file-read-encoding.mjs 841 B

123456789101112131415161718192021
  1. import assert from 'node:assert/strict';
  2. import { readFileSync } from 'node:fs';
  3. import vm from 'node:vm';
  4. import { test } from 'node:test';
  5. const scope = vm.createContext({ TextDecoder, Uint8Array });
  6. vm.runInContext(readFileSync(new URL('../cloud/functions/fmode-file-read.js', import.meta.url), 'utf8'), scope);
  7. test('keeps UTF-8 text readable', () => {
  8. const bytes = new TextEncoder().encode('转录日志');
  9. assert.equal(scope._decodeText(bytes.buffer), '转录日志');
  10. });
  11. test('falls back to GB18030 for legacy Chinese text', () => {
  12. const bytes = Uint8Array.from([0xcc, 0xec, 0xc6, 0xf8]); // 天气 in GB18030
  13. assert.equal(scope._decodeText(bytes.buffer), '天气');
  14. });
  15. test('rejects bytes that cannot be decoded as supported text', () => {
  16. assert.equal(scope._decodeText(Uint8Array.from([0x81]).buffer), null);
  17. });