import { describe, it, expect, vi, beforeEach } from 'vitest';

vi.mock('electron', () => ({
  default: {
    app: {
      on: vi.fn(),
      getPath: vi.fn(),
      getVersion: vi.fn(() => '0.2.7'),
      isPackaged: false,
      getLoginItemSettings: vi.fn(() => ({ openAtLogin: false }))
    },
    Tray: vi.fn(function (this: unknown) {
      Object.assign(this as object, {
        setToolTip: vi.fn(),
        setContextMenu: vi.fn(),
        on: vi.fn()
      });
    }),
    Menu: { buildFromTemplate: vi.fn((items: unknown) => ({ items })) },
    nativeImage: { createEmpty: vi.fn() },
    dialog: {},
    shell: {},
    clipboard: {}
  }
}));

import { createTray, type TrayCallbacks } from '../../src/main/tray';

describe('tray menu — slim 4 items', () => {
  beforeEach(() => { vi.clearAllMocks(); });

  function makeCallbacks(): TrayCallbacks {
    return {
      showInbox: vi.fn(),
      showCapture: vi.fn(),
      showSettings: vi.fn()
    };
  }

  it('builds menu with 4 click items + 2 separators', async () => {
    createTray(makeCallbacks());
    const electron = (await import('electron')).default;
    const calls = (electron.Menu.buildFromTemplate as any).mock.calls;
    const items = calls[calls.length - 1][0];
    const labels = items.filter((i: any) => i.type !== 'separator').map((i: any) => i.label);
    expect(labels).toEqual(['한 줄 적기', '보관한 메모 보기', '설정...', '종료']);
  });

  it('does not include removed items', async () => {
    createTray(makeCallbacks());
    const electron = (await import('electron')).default;
    const calls = (electron.Menu.buildFromTemplate as any).mock.calls;
    const items = calls[calls.length - 1][0];
    const labels = items.filter((i: any) => i.type !== 'separator').map((i: any) => i.label);
    expect(labels).not.toContain('지금 백업');
    expect(labels).not.toContain('내보내기...');
    expect(labels).not.toContain('Ollama 재확인');
    expect(labels).not.toContain('Ollama 설정...');
  });

  it('"설정..." click invokes showSettings callback', async () => {
    const cb = makeCallbacks();
    createTray(cb);
    const electron = (await import('electron')).default;
    const calls = (electron.Menu.buildFromTemplate as any).mock.calls;
    const items = calls[calls.length - 1][0];
    const settingsItem = items.find((i: any) => i.label === '설정...');
    settingsItem.click();
    expect(cb.showSettings).toHaveBeenCalled();
  });
});
