import { describe, it, expect } from 'vitest';
import { canonicalizeToolName } from '../src/repositories/audit-event.repository.js';

describe('canonicalizeToolName — normalize presented tool names for usage ranking', () => {
  it('strips all/ back to canonical server/tool and derives the server', () => {
    expect(canonicalizeToolName('all/k8s/get_pods', null)).toEqual({ key: 'k8s/get_pods', server: 'k8s' });
  });

  it('keeps favourite/ distinct (already a pin), using the trace server if any', () => {
    expect(canonicalizeToolName('favourite/get_pods', null)).toEqual({ key: 'favourite/get_pods', server: null });
  });

  it('passes a canonical server/tool through, deriving the server from the name', () => {
    expect(canonicalizeToolName('vault/read_secret', 'vault')).toEqual({ key: 'vault/read_secret', server: 'vault' });
  });

  it('handles a bare (non-namespaced) tool name', () => {
    expect(canonicalizeToolName('read_prompts', null)).toEqual({ key: 'read_prompts', server: null });
  });

  it('handles all/ where the tool name itself contains slashes', () => {
    expect(canonicalizeToolName('all/srv/a/b', null)).toEqual({ key: 'srv/a/b', server: 'srv' });
  });

  it('all/<server>/<tool> and <server>/<tool> collapse to the same key', () => {
    const a = canonicalizeToolName('all/gitea/create_pull_request', null);
    const b = canonicalizeToolName('gitea/create_pull_request', 'gitea');
    expect(a.key).toBe(b.key);
  });
});
