Skip to content

The swiss army knife of lossless video/audio editing

1.8 KB
Raw Blame
// eslint-disable-next-line import-js/no-extraneous-dependencies
import { test, expect, describe } from 'vitest';

import { pathToFileURL } from 'node:url';


// eslint-disable-next-line vitest/no-conditional-tests -- TODO fix (new rule in @mifi/lint-config)
if (process.platform === 'win32') {
  describe('file uri windows only', () => {
    test('converts path to file url', () => {
      expect(pathToFileURL(String.raw`C:\Users\sindresorhus\dev\te^st.jpg`).href).toBe('file:///C:/Users/sindresorhus/dev/te%5Est.jpg');
    });
  });
} else {
  describe('file uri non-windows', () => {
    // https://github.com/mifi/lossless-cut/issues/1941
    test('file with backslash', () => {
      expect(pathToFileURL(String.raw`/has/back\slash`).href).toBe('file:///has/back%5Cslash');
    });
  });
}

// taken from https://github.com/sindresorhus/file-url
describe('file uri both platforms', () => {
  test('converts path to file url', () => {
    expect(pathToFileURL('/test.jpg').href).toMatch(/^file:\/{3}.*test\.jpg$/);

    expect(pathToFileURL('/Users/sindresorhus/dev/te^st.jpg').href).toMatch(/^file:\/{2}.*\/Users\/sindresorhus\/dev\/te%5Est\.jpg$/);
  });

  test('escapes more special characters in path', () => {
    expect(pathToFileURL('/a^?!@#$%&\'";<>').href).toMatch(/^file:\/{3}.*a%5E%3F!@%23\$%25&'%22;%3C%3E$/);
  });

  test('escapes whitespace characters in path', () => {
    expect(pathToFileURL('/file with\r\nnewline').href).toMatch(/^file:\/{3}.*file%20with%0D%0Anewline$/);
  });

  test('relative path', () => {
    expect(pathToFileURL('relative/test.jpg').href).toMatch(/^file:\/{3}.*\/relative\/test\.jpg$/);
  });

  test('slash', () => {
    expect(pathToFileURL('/').href).toMatch(/^file:\/{2}.*\/$/);
  });

  test('empty', () => {
    expect(pathToFileURL('').href).toMatch(/^file:\/{3}.*$/);
  });
});