github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/tests/unit/utils/stream-frames-test.js (about)

     1  import { module, test } from 'qunit';
     2  import { decode } from 'nomad-ui/utils/stream-frames';
     3  import { TextEncoderLite } from 'text-encoder-lite';
     4  import base64js from 'base64-js';
     5  
     6  const Encoder = new TextEncoderLite('utf-8');
     7  const encode = str => base64js.fromByteArray(Encoder.encode(str));
     8  
     9  module('Unit | Util | stream-frames', function() {
    10    const { btoa } = window;
    11    const decodeTestCases = [
    12      {
    13        name: 'Single frame',
    14        in: `{"Offset":100,"Data":"${btoa('Hello World')}"}`,
    15        out: {
    16          offset: 100,
    17          message: 'Hello World',
    18        },
    19      },
    20      {
    21        name: 'Multiple frames',
    22        // prettier-ignore
    23        in: `{"Offset":1,"Data":"${btoa('One fish,')}"}{"Offset":2,"Data":"${btoa( ' Two fish.')}"}{"Offset":3,"Data":"${btoa(' Red fish, ')}"}{"Offset":4,"Data":"${btoa('Blue fish.')}"}`,
    24        out: {
    25          offset: 4,
    26          message: 'One fish, Two fish. Red fish, Blue fish.',
    27        },
    28      },
    29      {
    30        name: 'Empty frames',
    31        in: '{}{}{}',
    32        out: {},
    33      },
    34      {
    35        name: 'Empty string',
    36        in: '',
    37        out: {},
    38      },
    39      {
    40        name: 'Multi-byte unicode',
    41        in: `{"Offset":1,"Data":"${encode('ワンワン 🐶')}"}`,
    42        out: {
    43          offset: 1,
    44          message: 'ワンワン 🐶',
    45        },
    46      },
    47    ];
    48  
    49    decodeTestCases.forEach(testCase => {
    50      test(`decode: ${testCase.name}`, function(assert) {
    51        assert.deepEqual(decode(testCase.in), testCase.out);
    52      });
    53    });
    54  });