import { describe, expect, test } from "bun:test";
import { buildAdapters } from "../src/adapters";
import { defaultConfig } from "../src/config";
import { CollectorFatalError } from "../src/errors";

const TOKEN = "test-collector-token";

describe("buildAdapters", () => {
  test("default config enables every wave-1 adapter", () => {
    const ids = buildAdapters(defaultConfig(), TOKEN).map((a) => a.id);
    expect(ids.sort()).toEqual([
      "agent-sessions",
      "botmaster",
      "cluster",
      "factory",
      "gates",
      "ghci",
      "golive",
      "harness",
      "kubernetes",
      "offload",
      "prometheus",
      "sandbox-toolgap",
      "sessions",
      "systray-ai",
    ]);
  });

  test("enabled = false omits an adapter, others stay", () => {
    const config = { ...defaultConfig(), adapters: { ghci: { enabled: false } } };
    const ids = buildAdapters(config, TOKEN).map((a) => a.id);
    expect(ids).not.toContain("ghci");
    expect(ids).toContain("harness");
  });

  test("enabled = false omits cluster", () => {
    const config = { ...defaultConfig(), adapters: { cluster: { enabled: false } } };
    const ids = buildAdapters(config, TOKEN).map((a) => a.id);
    expect(ids).not.toContain("cluster");
    expect(ids).toContain("gates");
  });

  test("enabled = false omits gates", () => {
    const config = { ...defaultConfig(), adapters: { gates: { enabled: false } } };
    const ids = buildAdapters(config, TOKEN).map((a) => a.id);
    expect(ids).not.toContain("gates");
    expect(ids).toContain("cluster");
  });

  test("enabled = false omits botmaster", () => {
    const config = { ...defaultConfig(), adapters: { botmaster: { enabled: false } } };
    const ids = buildAdapters(config, TOKEN).map((a) => a.id);
    expect(ids).not.toContain("botmaster");
    expect(ids).toContain("harness");
  });

  test("enabled = false omits offload", () => {
    const config = { ...defaultConfig(), adapters: { offload: { enabled: false } } };
    const ids = buildAdapters(config, TOKEN).map((a) => a.id);
    expect(ids).not.toContain("offload");
    expect(ids).toContain("harness");
  });

  test("intervalMs overrides the adapter's default interval", () => {
    const config = {
      ...defaultConfig(),
      adapters: { prometheus: { enabled: true, intervalMs: 12_345 } },
    };
    const prometheus = buildAdapters(config, TOKEN).find((a) => a.id === "prometheus");
    expect(prometheus?.interval).toBe(12_345);
  });

  test("every built adapter id is unique — registry registration cannot collide", () => {
    const ids = buildAdapters(defaultConfig(), TOKEN).map((a) => a.id);
    expect(new Set(ids).size).toBe(ids.length);
  });

  test("empty token throws TOKEN_EMPTY when offload is enabled", () => {
    expect(() => buildAdapters(defaultConfig(), "")).toThrow(CollectorFatalError);
    try {
      buildAdapters(defaultConfig(), "");
    } catch (err) {
      expect((err as CollectorFatalError).code).toBe("TOKEN_EMPTY");
    }
  });
});
