import { createVaultfireTools, createVaultfireReadTools, VaultfireTool } from '../src/tools';

describe('Tool Factory', () => {
  test('createVaultfireReadTools returns 7 read-only tools', () => {
    const tools = createVaultfireReadTools({ chain: 'base' });
    expect(tools).toHaveLength(7);

    const names = tools.map((t) => t.name);
    expect(names).toContain('vaultfire_verify_agent');
    expect(names).toContain('vaultfire_get_street_cred');
    expect(names).toContain('vaultfire_get_agent');
    expect(names).toContain('vaultfire_get_bonds');
    expect(names).toContain('vaultfire_get_reputation');
    expect(names).toContain('vaultfire_discover_agents');
    expect(names).toContain('vaultfire_protocol_stats');
  });

  test('createVaultfireTools without signer returns 7 tools (read-only)', () => {
    const tools = createVaultfireTools({ chain: 'base' });
    expect(tools).toHaveLength(7);
  });

  test('createVaultfireTools with privateKey returns 9 tools (includes write)', () => {
    // Use a throwaway key that won't be used for actual transactions
    const tools = createVaultfireTools({
      chain: 'base',
      privateKey: '0x' + '00'.repeat(31) + '01', // dummy key
    });
    expect(tools).toHaveLength(9);

    const names = tools.map((t) => t.name);
    expect(names).toContain('vaultfire_register_agent');
    expect(names).toContain('vaultfire_create_bond');
  });

  test('every tool has required properties', () => {
    const tools = createVaultfireTools({
      chain: 'base',
      privateKey: '0x' + '00'.repeat(31) + '01',
    });

    for (const tool of tools) {
      expect(tool.name).toBeTruthy();
      expect(tool.name).toMatch(/^vaultfire_/);
      expect(tool.description).toBeTruthy();
      expect(tool.description.length).toBeGreaterThan(20);
      expect(tool.schema).toBeDefined();
      expect(typeof tool.func).toBe('function');
    }
  });

  test('tools work across all chains', () => {
    const chains = ['base', 'avalanche', 'arbitrum', 'polygon'] as const;
    for (const chain of chains) {
      const tools = createVaultfireReadTools({ chain });
      expect(tools).toHaveLength(7);
      expect(tools[0].name).toBe('vaultfire_verify_agent');
    }
  });

  test('default config creates base tools', () => {
    const tools = createVaultfireReadTools();
    expect(tools).toHaveLength(7);
  });

  test('tool descriptions mention Vaultfire Protocol', () => {
    const tools = createVaultfireReadTools({ chain: 'base' });
    for (const tool of tools) {
      expect(tool.description.toLowerCase()).toContain('vaultfire');
    }
  });

  test('tool schemas are valid Zod objects', () => {
    const tools = createVaultfireReadTools({ chain: 'base' });
    for (const tool of tools) {
      // Zod schemas have a .parse method
      expect(typeof tool.schema.parse).toBe('function');
    }
  });
});
