import assert from 'node:assert/strict';
import test from 'node:test';
import {
  createBuildAppendToMpvLogMainDepsHandler,
  createBuildShowMpvOsdMainDepsHandler,
} from './mpv-osd-log-main-deps';

test('append to mpv log main deps map filesystem functions and log path', async () => {
  const calls: string[] = [];
  const deps = createBuildAppendToMpvLogMainDepsHandler({
    getLogPath: () => '/tmp/mpv.log',
    dirname: (targetPath) => {
      calls.push(`dirname:${targetPath}`);
      return '/tmp';
    },
    mkdir: async (targetPath) => {
      calls.push(`mkdir:${targetPath}`);
    },
    appendFile: async (_targetPath, data) => {
      calls.push(`append:${data}`);
    },
    now: () => new Date('2026-02-20T00:00:00.000Z'),
  })();

  assert.equal(deps.getLogPath(), '/tmp/mpv.log');
  assert.equal(deps.dirname('/tmp/mpv.log'), '/tmp');
  await deps.mkdir('/tmp', { recursive: true });
  await deps.appendFile('/tmp/mpv.log', 'line', { encoding: 'utf8' });
  assert.equal(deps.now().toISOString(), '2026-02-20T00:00:00.000Z');
  assert.deepEqual(calls, ['dirname:/tmp/mpv.log', 'mkdir:/tmp', 'append:line']);
});

test('show mpv osd main deps map runtime delegates and logging callback', () => {
  const calls: string[] = [];
  const client = {
    connected: true,
    send: () => {},
  };
  const deps = createBuildShowMpvOsdMainDepsHandler({
    appendToMpvLog: (message) => calls.push(`append:${message}`),
    showMpvOsdRuntime: (_mpvClient, text, fallbackLog) => {
      calls.push(`show:${text}`);
      fallbackLog('fallback');
      return false;
    },
    getMpvClient: () => client,
    logInfo: (line) => calls.push(`info:${line}`),
  })();

  assert.deepEqual(deps.getMpvClient(), client);
  deps.appendToMpvLog('hello');
  const shown = deps.showMpvOsdRuntime(deps.getMpvClient(), 'subtitle', (line) =>
    deps.logInfo(line),
  );
  assert.equal(shown, false);
  assert.deepEqual(calls, ['append:hello', 'show:subtitle', 'info:fallback']);
});
