import { renderHook } from '@testing-library/react';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { getDataviewActionRegistry } from '@ablogcms/plugins/core';
import useModuleAdminActions, { type GetActionsContext } from './use-module-admin-actions';

const createContext = (): GetActionsContext => ({
  invalidate: vi.fn(),
  searchParams: new URLSearchParams(),
});

describe('useModuleAdminActions', () => {
  it('ハンドラ0件でもコア定義のアクションがそのまま getActions に渡る（特徴付けテスト）', () => {
    let received: string[] | undefined;
    renderHook(() =>
      useModuleAdminActions({
        getActions: (actions) => {
          received = actions.map((a) => a.id);
          return actions;
        },
        context: createContext(),
      })
    );

    expect(received).toEqual(['edit']);
  });

  describe('ctx.dataview.registerAction("module", ...) レジストリとのマージ', () => {
    afterEach(() => {
      getDataviewActionRegistry().remove('module', 'plugin-action');
    });

    it('レジストリに登録したアクションがコア定義の後ろに追加され getActions に渡る', () => {
      getDataviewActionRegistry().add('module', 'plugin-action', {
        id: 'plugin-action',
        label: 'プラグインアクション',
      });

      const { result } = renderHook(() =>
        useModuleAdminActions({ getActions: (actions) => actions, context: createContext() })
      );

      expect(result.current.map((a) => a.id)).toEqual(['edit', 'plugin-action']);
    });

    it('entry 側（view="entry"）に登録したアクションは module 側には出ない（view 分離）', () => {
      getDataviewActionRegistry().add('entry', 'plugin-action', { id: 'plugin-action', label: 'entry 側専用' });

      const { result } = renderHook(() =>
        useModuleAdminActions({ getActions: (actions) => actions, context: createContext() })
      );

      expect(result.current.map((a) => a.id)).not.toContain('plugin-action');
      getDataviewActionRegistry().remove('entry', 'plugin-action');
    });
  });
});
