const assert = require('node:assert/strict');
const test = require('node:test');

const values = new Map();
global.window = global;
global.localStorage = {
  getItem: (key) => values.get(key) || null,
  setItem: (key, value) => values.set(key, value)
};
require('./preferences.js');

test('restores a matching track for the next file in the same folder', () => {
  const firstEpisode = 'Anime/Show/episode-01.mkv';
  const nextEpisode = 'Anime/Show/episode-02.mkv';
  const tracks = [{ lang: 'en' }, { lang: 'ja' }];
  LanPlayPreferences.remember(firstEpisode, 'audio', tracks[1], 1);
  assert.equal(LanPlayPreferences.find(nextEpisode, 'audio', tracks), tracks[1]);
});

test('does not apply a profile from another folder', () => {
  const tracks = [{ lang: 'ja' }];
  assert.equal(LanPlayPreferences.find('Anime/Other/episode-01.mkv', 'audio', tracks), undefined);
});

test('falls back to the player default when the saved track is unavailable', () => {
  const tracks = [{ lang: 'en' }];
  assert.equal(LanPlayPreferences.find('Anime/Show/episode-03.mkv', 'audio', tracks), undefined);
});

test('remembers disabled subtitles for the release folder', () => {
  LanPlayPreferences.remember('Series/Season/episode-01.mkv', 'subtitle', null, -1);
  assert.equal(
    LanPlayPreferences.find('Series/Season/episode-02.mkv', 'subtitle', [{ language: 'en' }]),
    null
  );
});
