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

import {
  buildAgentExecutionConfig,
  createDefaultBuiltinTools,
  normalizeBuiltinTools,
} from './builtin-tools';

describe('agent 内置工具配置', () => {
  it('新建 Agent 默认启用五项工具且仅 Shell 要求确认', () => {
    expect(createDefaultBuiltinTools()).toEqual({
      artifactPublish: { approvalRequired: false, enabled: true },
      patch: { approvalRequired: false, enabled: true },
      read: { approvalRequired: false, enabled: true },
      schemaVersion: 1,
      shell: { approvalRequired: true, enabled: true },
      write: { approvalRequired: false, enabled: true },
    });
  });

  it('旧草稿仅在构造保存载荷时补齐完整配置', () => {
    const source = { documentContextBudgetTokens: 32_000 };

    const normalized = buildAgentExecutionConfig(source, false);

    expect(source).not.toHaveProperty('builtinTools');
    expect(normalized.builtinTools).toEqual(createDefaultBuiltinTools());
    expect(normalized.documentContextBudgetTokens).toBe(32_000);
  });

  it('保留显式开关，并强制普通用户开启 Shell 调用确认', () => {
    const builtinTools = normalizeBuiltinTools({
      read: { enabled: false },
      shell: { approvalRequired: false, enabled: true },
      shellApprovalRiskConfirmed: true,
    });

    expect(
      buildAgentExecutionConfig({ builtinTools }, false).builtinTools,
    ).toMatchObject({
      read: { approvalRequired: false, enabled: false },
      shell: { approvalRequired: true, enabled: true },
    });
    expect(
      buildAgentExecutionConfig({ builtinTools }, false).builtinTools,
    ).not.toHaveProperty('shellApprovalRiskConfirmed');
    expect(
      buildAgentExecutionConfig({ builtinTools }, true).builtinTools?.shell
        .approvalRequired,
    ).toBe(false);
    expect(
      buildAgentExecutionConfig({ builtinTools }, true).builtinTools
        ?.shellApprovalRiskConfirmed,
    ).toBe(true);
  });
});
