import { describe, expect, it } from 'vitest';
import { formatPrometheus, type MetricsSnapshot } from './metrics.js';

describe('confirmation custom_id matchers', () => {
  it('detects moderation confirm/cancel prefixes', () => {
    const isMod = (id: string) => id.startsWith('mod:confirm:') || id.startsWith('mod:cancel:');
    expect(isMod('mod:confirm:abc')).toBe(true);
    expect(isMod('mod:cancel:abc')).toBe(true);
    expect(isMod('other')).toBe(false);
  });

  it('detects guildbackup confirm/cancel prefixes', () => {
    const isBackup = (id: string) =>
      id.startsWith('gbackup:confirm:delete:') ||
      id.startsWith('gbackup:confirm:restore:') ||
      id.startsWith('gbackup:cancel:');
    expect(isBackup('gbackup:confirm:delete:abc')).toBe(true);
    expect(isBackup('gbackup:confirm:restore:abc')).toBe(true);
    expect(isBackup('gbackup:cancel:abc')).toBe(true);
    expect(isBackup('mod:confirm:abc')).toBe(false);
  });
});

describe('formatPrometheus', () => {
  it('emits core counters and shard gauges', () => {
    const snapshot: MetricsSnapshot = {
      up: 1,
      commandsTotal: 10,
      commandsErrors: 2,
      commandDurationSumMs: 500,
      commandDurationCount: 10,
      eventsTotal: 100,
      shards: [{ id: 0, ping: 42, guilds: 5, updatedAt: Date.now() }],
      queueWaiting: 3
    };
    const body = formatPrometheus(snapshot);
    expect(body).toContain('nexumi_up 1');
    expect(body).toContain('nexumi_commands_total 10');
    expect(body).toContain('nexumi_commands_errors_total 2');
    expect(body).toContain('nexumi_guilds{shard="0"} 5');
    expect(body).toContain('nexumi_shard_ping_ms{shard="0"} 42');
    expect(body).toContain('nexumi_queue_waiting 3');
  });
});
