/**
 * v1.15.0-mcp-multi: unit tests for matchToolGlob.
 */
import { describe, it, expect } from 'vitest';
import { matchToolGlob } from '../agents.js';

describe('matchToolGlob', () => {
  it('exact match: "grep" matches "grep"', () => {
    expect(matchToolGlob('grep', ['grep'])).toBe(true);
  });

  it('exact match: "grep" does not match "grep2"', () => {
    expect(matchToolGlob('grep2', ['grep'])).toBe(false);
  });

  it('exact match: multiple tools', () => {
    expect(matchToolGlob('grep', ['grep', 'view_file'])).toBe(true);
    expect(matchToolGlob('view_file', ['grep', 'view_file'])).toBe(true);
    expect(matchToolGlob('find_files', ['grep', 'view_file'])).toBe(false);
  });

  it('wildcard: "context7_*" matches "context7_query-docs"', () => {
    expect(matchToolGlob('context7_query-docs', ['context7_*'])).toBe(true);
  });

  it('wildcard: "context7_*" matches "context7_resolve-library-id"', () => {
    expect(matchToolGlob('context7_resolve-library-id', ['context7_*'])).toBe(true);
  });

  it('wildcard: "context7_*" does not match "codecontext_overview"', () => {
    expect(matchToolGlob('codecontext_overview', ['context7_*'])).toBe(false);
  });

  it('wildcard: "view_*" matches "view_file" and "view_truncated_output"', () => {
    expect(matchToolGlob('view_file', ['view_*'])).toBe(true);
    expect(matchToolGlob('view_truncated_output', ['view_*'])).toBe(true);
  });

  it('wildcard: "*" matches everything', () => {
    expect(matchToolGlob('anything', ['*'])).toBe(true);
    expect(matchToolGlob('context7_query-docs', ['*'])).toBe(true);
  });

  it('deny: "!web_*" excludes "web_search"', () => {
    // With only a deny rule and no prior match, the tool is not matched
    expect(matchToolGlob('web_search', ['!web_*'])).toBe(false);
  });

  it('last-match-wins: ["*", "!web_*"] excludes web tools, includes others', () => {
    expect(matchToolGlob('web_search', ['*', '!web_*'])).toBe(false);
    expect(matchToolGlob('web_fetch', ['*', '!web_*'])).toBe(false);
    expect(matchToolGlob('grep', ['*', '!web_*'])).toBe(true);
    expect(matchToolGlob('context7_query-docs', ['*', '!web_*'])).toBe(true);
  });

  it('last-match-wins: deny then re-allow', () => {
    // ["!web_*", "web_search"] — deny all web, then re-allow web_search
    expect(matchToolGlob('web_search', ['!web_*', 'web_search'])).toBe(true);
    expect(matchToolGlob('web_fetch', ['!web_*', 'web_fetch'])).toBe(true);
  });

  it('empty patterns: nothing matches', () => {
    expect(matchToolGlob('grep', [])).toBe(false);
    expect(matchToolGlob('anything', [])).toBe(false);
  });

  it('no-glob fallback: exact-match only, same as pre-v1.15', () => {
    const patterns = ['grep', 'view_file'];
    expect(matchToolGlob('grep', patterns)).toBe(true);
    expect(matchToolGlob('view_file', patterns)).toBe(true);
    expect(matchToolGlob('find_files', patterns)).toBe(false);
    expect(matchToolGlob('web_search', patterns)).toBe(false);
  });

  it('mixed glob and exact patterns', () => {
    const patterns = ['grep', 'context7_*', '!context7_dangerous'];
    expect(matchToolGlob('grep', patterns)).toBe(true);
    expect(matchToolGlob('context7_query-docs', patterns)).toBe(true);
    expect(matchToolGlob('context7_dangerous', patterns)).toBe(false);
    expect(matchToolGlob('view_file', patterns)).toBe(false);
  });
});
