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

vi.mock('@main/core/application', () => ({
  application: { get: () => ({ search: () => [] }) }
}))

import { READ_FILE_TOOL_NAME } from '@shared/ai/builtinTools'

import { ToolRegistry } from '../../registry'
import { registerBuiltinTools } from '../index'
import { KB_LIST_TOOL_NAME } from '../KnowledgeListTool'
import { KB_SEARCH_TOOL_NAME } from '../KnowledgeSearchTool'
import { WEB_FETCH_TOOL_NAME, WEB_SEARCH_TOOL_NAME } from '../WebSearchTool'

describe('registerBuiltinTools', () => {
  it('populates the given registry with every builtin entry', () => {
    const reg = new ToolRegistry()
    registerBuiltinTools(reg)
    expect(reg.has(KB_LIST_TOOL_NAME)).toBe(true)
    expect(reg.has(KB_SEARCH_TOOL_NAME)).toBe(true)
    expect(reg.has(READ_FILE_TOOL_NAME)).toBe(true)
    expect(reg.has(WEB_FETCH_TOOL_NAME)).toBe(true)
    expect(reg.has(WEB_SEARCH_TOOL_NAME)).toBe(true)
  })

  it('gates read_file on file attachments', () => {
    const reg = new ToolRegistry()
    registerBuiltinTools(reg)
    const readFile = reg.getByName(READ_FILE_TOOL_NAME)
    expect(readFile?.applies?.({ mcpToolIds: new Set(), hasFileAttachments: false })).toBe(false)
    expect(readFile?.applies?.({ mcpToolIds: new Set(), hasFileAttachments: true })).toBe(true)
  })
})
