local createLibTest = require('/apis/libtest');
local createMusic = require('/apis/libmusic');

local testlib = createLibTest({ ... });

local function fakeFs(dir, entries)
  return {
    isDir = function(path) return path == dir; end,
    list = function(path)
      if path ~= dir then error('unexpected list: ' .. tostring(path)); end
      return entries;
    end,
  };
end

testlib.test('listTracks filters dfpwm, strips extension and sorts', function()
  local music = createMusic({
    dir = '/data/musics',
    fs = fakeFs('/data/musics', { 'Zebra.dfpwm', 'notes.txt', 'Apple.dfpwm' }),
  });
  local tracks = music.listTracks();
  testlib.assertEquals(#tracks, 2);
  testlib.assertEquals(tracks[1].name, 'Apple');
  testlib.assertEquals(tracks[1].path, '/data/musics/Apple.dfpwm');
  testlib.assertEquals(tracks[2].name, 'Zebra');
  testlib.assertEquals(tracks[2].path, '/data/musics/Zebra.dfpwm');
end);

testlib.test('listTracks returns empty when directory is missing', function()
  local music = createMusic({
    dir = '/data/musics',
    fs = { isDir = function() return false; end },
  });
  testlib.assertEquals(#music.listTracks(), 0);
end);

testlib.run();
