import { afterEach, expect, test } from "bun:test";
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { runtimeGitopsScriptsConfigMap } from "./runtime-gitops-scripts-configmap.mjs";

const roots: string[] = [];

afterEach(() => {
  while (roots.length > 0) rmSync(roots.pop()!, { recursive: true, force: true });
});

test("preserves codeAgentRuntime in the owning runtime GitOps overlay", () => {
  const scriptsDir = mkdtempSync(join(tmpdir(), "runtime-gitops-configmap-"));
  roots.push(scriptsDir);
  for (const name of [
    "runtime-gitops-observability.mjs",
    "runtime-gitops-postprocess.mjs",
    "runtime-gitops-verify.mjs",
    "feature-config-schema-warning.mjs",
    "ajv2020.min.js",
  ]) {
    writeFileSync(join(scriptsDir, name), `${name}\n`);
  }
  const codeAgentRuntime = {
    enabled: true,
    kafkaEventBridge: {
      enabled: true,
      transactionalProjector: { heartbeatIntervalMs: 2000 },
      projectionOutboxRelay: { intervalMs: 250 },
      projectionRealtime: { sseHeartbeatMs: 1000 },
    },
  };

  const configMap = runtimeGitopsScriptsConfigMap({
    overlay: { runtimePath: "runtime", observability: { prometheusOperator: false }, codeAgentRuntime },
    scriptsDir,
    namespace: "hwlab",
    configMapName: "runtime-gitops-scripts",
  });

  expect(JSON.parse(configMap.data["runtime-gitops-overlay.json"])).toEqual({
    runtimePath: "runtime",
    observability: { prometheusOperator: false },
    codeAgentRuntime,
  });
  expect(configMap.data["feature-config-schema-warning.mjs"]).toBe("feature-config-schema-warning.mjs\n");
  expect(configMap.data["ajv2020.min.js"]).toBe("ajv2020.min.js\n");
});

test("publishes the complete Code Agent runtime patcher", () => {
  const configMap = runtimeGitopsScriptsConfigMap({
    overlay: { runtimePath: "runtime" },
    scriptsDir: import.meta.dir,
    namespace: "hwlab",
    configMapName: "runtime-gitops-scripts",
  });
  const postprocess = configMap.data["runtime-gitops-postprocess.mjs"];

  expect(postprocess).toContain('setEnvValue(container, "AGENTRUN_MGR_URL"');
  expect(postprocess).toContain('setEnvValue(container, "HWLAB_CODE_AGENT_AGENTRUN_RUNNER_NAMESPACE"');
  expect(postprocess).toContain('setEnvFromSecret(container, "AGENTRUN_API_KEY"');
});

test("missing optional validator artifacts do not block ConfigMap rendering", () => {
  const scriptsDir = mkdtempSync(join(tmpdir(), "runtime-gitops-configmap-"));
  roots.push(scriptsDir);
  for (const name of [
    "runtime-gitops-observability.mjs",
    "runtime-gitops-postprocess.mjs",
    "runtime-gitops-verify.mjs",
  ]) {
    writeFileSync(join(scriptsDir, name), `${name}\n`);
  }

  const configMap = runtimeGitopsScriptsConfigMap({
    overlay: { runtimePath: "runtime" },
    scriptsDir,
    namespace: "hwlab",
    configMapName: "runtime-gitops-scripts",
  });

  expect(configMap.data["runtime-gitops-verify.mjs"]).toBe("runtime-gitops-verify.mjs\n");
  expect(configMap.data["feature-config-schema-warning.mjs"]).toBeUndefined();
  expect(configMap.data["ajv2020.min.js"]).toBeUndefined();
});
