import { describe, it, expect, beforeEach } from 'vitest';
import {
  createGatewayRegistry,
  createWorkerRegistry,
  getDefaultGatewayRegistry,
  getDefaultWorkerRegistry,
  resetAllRegistries,
} from './registry.js';

describe('metrics/registry', () => {
  beforeEach(() => {
    resetAllRegistries();
  });

  it('createGatewayRegistry returns a fresh Registry per call', () => {
    const a = createGatewayRegistry('aao_gateway_test_a');
    const b = createGatewayRegistry('aao_gateway_test_b');
    expect(a).not.toBe(b);
  });

  it('default singletons are stable until reset', () => {
    const first = getDefaultGatewayRegistry('aao_gateway_test_singleton');
    const second = getDefaultGatewayRegistry('aao_gateway_test_singleton');
    expect(first).toBe(second);
    resetAllRegistries();
    const third = getDefaultGatewayRegistry('aao_gateway_test_singleton');
    expect(third).not.toBe(first);
  });

  it('gateway and worker singletons are independent', () => {
    const g = getDefaultGatewayRegistry('aao_gateway_test_gw');
    const w = getDefaultWorkerRegistry('aao_worker_test_wk');
    expect(g).not.toBe(w);
  });

  it('default metrics are wired (process_cpu_user_seconds_total appears)', async () => {
    const reg = createWorkerRegistry('aao_worker_dm');
    // Default-metric collection registers some metrics lazily; call
    // metrics() to force gathering.
    const out = await reg.metrics();
    // Process CPU is always present on Node + Linux.
    expect(out).toContain('aao_worker_dm_process_cpu_user_seconds_total');
  });
});
