import { describe, expect, it } from "vitest";
import { createEnvRuntimeSettings } from "../../src/settings/runtime.js";

describe("runtime settings", () => {
  it("reads the model registry from the current env at call time", async () => {
    const env: Record<string, string | undefined> = {
      ANTHROPIC_DEFAULT_SONNET_MODEL: "anthropic/claude-sonnet-5",
    };
    const settings = createEnvRuntimeSettings(env);

    expect((await settings.modelRegistry()).resolve(undefined, "draft")).toBe("anthropic/claude-sonnet-5");

    env["ANTHROPIC_DEFAULT_SONNET_MODEL"] = "z-ai/glm-4.7";

    expect((await settings.modelRegistry()).resolve(undefined, "draft")).toBe("z-ai/glm-4.7");
  });

  it("refuses process-global provider credentials even when they are present", async () => {
    const settings = createEnvRuntimeSettings({
      ANTHROPIC_BASE_URL: "https://example.test/api",
      ANTHROPIC_AUTH_TOKEN: "openrouter-token",
      ANTHROPIC_API_KEY: "explicit-api-key",
    });

    await expect(settings.provider("openrouter")).rejects.toThrow(
      "process-global provider credentials are disabled",
    );
  });

  it("honors HUB_AGENT_MAX_TURNS as a runtime run policy", async () => {
    const settings = createEnvRuntimeSettings({
      HUB_AGENT_MAX_TURNS: "9",
      HUB_AGENT_MAX_CONCURRENT_RUNS: "2",
      HUB_AGENT_MAX_RUN_SECONDS: "600",
    });

    await expect(settings.runPolicy({ projectId: "p", roleId: "draft" })).resolves.toEqual({
      maxTurns: 9,
      maxConcurrentRuns: 2,
      maxRunSeconds: 600,
    });
  });
});
