// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only

import assert from 'node:assert/strict';
import { unicodeSlice } from '../../util/unicodeSlice';
import { byteLength } from '../../Bytes';

describe('unicodeSlice()', () => {
  function test(
    title: string,
    input: string,
    begin: number,
    end: number,
    expected: string,
    expectedSize: number
  ): void {
    it(title, () => {
      const result = unicodeSlice(input, begin, end);
      assert.strictEqual(result, expected);
      assert.strictEqual(byteLength(result), expectedSize);
    });
  }

  test('one-byte chars', '123456', 2, 4, '34', 2);
  test('past max length', '123456', 0, 100, '123456', 6);
  test('end before start', '123456', 5, 1, '', 0);
  test('negative start', '123456', -5, 4, '1234', 4);
  test('negative end', '123456', 0, -5, '', 0);
  test('end at start', '123456', 3, 3, '', 0);

  test('multi-byte char', 'xโ‚ฌx', 1, 4, 'โ‚ฌ', 3);
  test('multi-byte char slice before end', 'โ‚ฌ', 1, 3, '', 0);
  test('multi-byte char slice after start', 'โ‚ฌ', 2, 4, '', 0);

  test('emoji', 'x๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆx', 1, 26, '๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ', 25);
  test('emoji slice before end', 'x๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆx', 1, 25, '', 0);
  test('emoji slice after start', 'x๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆx', 2, 26, '', 0);
  test('emoji slice capture around', 'x๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆx', 0, 27, 'x๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆx', 27);
});