/**
 * @license
 * Copyright 2022 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import {test} from 'node:test';
import * as assert from 'node:assert';
import {optimizeMkdirs} from '../util/optimize-mkdirs.js';
import {shuffle} from '../util/shuffle.js';
import {windowsifyPathIfOnWindows} from './util/windows.js';

const check = (input: string[], expected: string[]) =>
  assert.deepEqual(
    optimizeMkdirs(input.map(windowsifyPathIfOnWindows)).sort(),
    expected.map(windowsifyPathIfOnWindows).sort(),
  );

void test('empty', () => {
  check([], []);
});

void test('1 item', () => {
  check(['a'], ['a']);
});

void test('duplicates', () => {
  check(['a', 'a'], ['a']);
});

void test('parent and children', () => {
  check(['a', 'a/b', 'a/b/c'], ['a/b/c']);
});

void test('parent and child reversed', () => {
  check(['a/b/c', 'a/b', 'a'], ['a/b/c']);
});

void test('various shuffled cases', () => {
  const input = [
    '',
    'a/b/c',
    'd/e/f',
    'd/e/f',
    'd/e/f',
    'foo/bar/baz',
    'foo/bar',
    'foo',
    '1/2/3/4/5',
    '1/2/3/4/5',
    '1/2/3/4',
    '1/2/3',
    '1/2',
    '1',
  ];
  const expected = ['', 'a/b/c', 'd/e/f', 'foo/bar/baz', '1/2/3/4/5'];
  for (let i = 0; i < 1000; i++) {
    shuffle(input);
    check(input, expected);
  }
});
