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

const setupHtmx = vi.fn();
const registerAcmsExtensions = vi.fn();

vi.mock('@ablogcms/htmx', () => ({ setupHtmx, registerAcmsExtensions }));
vi.mock('@ablogcms/lib/geolocation', () => ({ default: vi.fn() }));

describe('dispatchHtmx', () => {
  it('setupHtmx を呼ぶ（非推奨通知は setupHtmx 自身が担う）', async () => {
    const { default: dispatchHtmx } = await import('./dispatch-htmx');
    const element = document.createElement('div');

    dispatchHtmx(element, { historyCacheSize: -1 });

    expect(setupHtmx).toHaveBeenCalledWith(
      element,
      { historyCacheSize: -1 },
      expect.objectContaining({ onAfterSwap: expect.any(Function), registerExtensions: expect.any(Function) })
    );
  });

  it('setupHtmx の registerExtensions フックが registerAcmsExtensions に委譲する', async () => {
    const { default: dispatchHtmx } = await import('./dispatch-htmx');
    dispatchHtmx(document.createElement('div'), {});

    const { registerExtensions } = setupHtmx.mock.calls.at(-1)?.[2] ?? {};
    const fakeHtmx = {};
    registerExtensions(fakeHtmx);

    expect(registerAcmsExtensions).toHaveBeenCalledWith(
      fakeHtmx,
      expect.objectContaining({ getGeolocation: expect.any(Function) })
    );
  });
});
