import { NetaClawToolRegistryService } from '../src/modules/netaclaw/service/tool_registry.js';

describe('NetaClawToolRegistryService', () => {
  let service: NetaClawToolRegistryService;

  beforeEach(() => {
    service = new NetaClawToolRegistryService();
  });

  it('keeps governance fields from db during catalog sync', async () => {
    const save = jest.fn().mockResolvedValue(undefined);
    service.toolRepo = {
      find: jest.fn().mockResolvedValue([
        {
          id: 1,
          name: 'clarify',
          label: '自定义澄清',
          description: '自定义描述',
          toolset: 'interaction',
          visibility: 'tool',
          capability: 'text',
          status: 0,
          isCore: 0,
          canDisable: 1,
          supportsPromptHint: 1,
          promptHint: 'custom',
          sort: 30,
          extra: { source: 'db' },
        },
      ]),
      create: jest.fn((data: any) => data),
      save,
    } as any;

    await service.syncCatalogToDb();

    expect(save).toHaveBeenCalledWith(
      expect.arrayContaining([
        expect.objectContaining({
          name: 'clarify',
          label: '自定义澄清',
          description: '自定义描述',
          status: 0,
          isCore: 0,
          canDisable: 1,
          promptHint: 'custom',
          sort: 30,
          extra: { source: 'db' },
        }),
      ])
    );
  });

  it('allows updating isCore and canDisable from tool management', async () => {
    const current = {
      id: 1,
      name: 'clarify',
      label: 'clarify',
      description: 'old',
      toolset: 'interaction',
      visibility: 'tool',
      capability: 'text',
      status: 1,
      isCore: 1,
      canDisable: 0,
      supportsPromptHint: 1,
      promptHint: null,
      sort: 0,
      extra: null,
    };
    const save = jest.fn().mockResolvedValue(undefined);
    service.toolRepo = {
      findOneBy: jest.fn().mockResolvedValue(current),
      save,
    } as any;

    await service.update({
      id: 1,
      isCore: 0,
      canDisable: 1,
      status: 0,
    });

    expect(save).toHaveBeenCalledWith(
      expect.objectContaining({
        id: 1,
        isCore: 0,
        canDisable: 1,
        status: 0,
      })
    );
  });
});
