github.com/hernad/nomad@v1.6.112/ui/tests/unit/utils/stream-frames-test.js (about)

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