import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import test from "node:test";
import { fileURLToPath } from "node:url";

const repositoryRoot = fileURLToPath(new URL("../../..", import.meta.url));
const requiredProductionIdentifiers = [
  ["internal/cloud/kafka-event-bridge.ts", "startLiveHwlabKafkaEventBridge"],
  ["internal/cloud/kafka-event-bridge.ts", "queryHwlabEventRetention"],
  ["internal/cloud/server-workbench-realtime-http.ts", "handleLiveKafkaWorkbenchRealtimeHttp"],
  ["internal/cloud/server-workbench-realtime-http.ts", "handleKafkaRefreshReplayWorkbenchRealtimeHttp"],
  ["web/hwlab-cloud-web/src/stores/workbench.ts", "applyLiveKafkaBusinessEvent"],
  ["web/hwlab-cloud-web/src/stores/workbench-event-reducer.ts", "hwlab.event.v1"],
  ["web/hwlab-cloud-web/src/utils/workbench-stream-transport.ts", "workbenchRealtimeTraceIdForCapabilities"]
] as const;
const forbiddenRepairIdentifiers = [
  "handleWorkbenchSyncHttp",
  "/v1/workbench/sync",
  "scheduleSessionListRefresh",
  "invalidateSessionList",
  "fetchWorkbenchTurnStatus",
  "refreshTurnStatusByTraceId",
  "refreshMessageProjectionForTrace",
  "forceRead",
  "sealRestoredActiveTurnMessages",
  "messageNeedsRestoredTurnSeal",
  "refreshSessionStatusAuthority",
  "readTerminalTraceDetailGaps"
];
const productionFiles = [
  "internal/cloud/server-workbench-facts.ts",
  "internal/cloud/server-workbench-realtime-http.ts",
  "web/hwlab-cloud-web/src/stores/workbench.ts",
  "web/hwlab-cloud-web/src/stores/workbench-colada-queries.ts"
];

test("Workbench production source fixes Kafka as the realtime and replay authority", async () => {
  for (const [relativePath, identifier] of requiredProductionIdentifiers) {
    const source = await readFile(`${repositoryRoot}/${relativePath}`, "utf8");
    assert.ok(source.includes(identifier), `${relativePath} must contain ${identifier}`);
  }
});

test("Workbench production source keeps HTTP repair and sync authority deleted", async () => {
  const violations: string[] = [];
  for (const relativePath of productionFiles) {
    const source = await readFile(`${repositoryRoot}/${relativePath}`, "utf8");
    for (const identifier of forbiddenRepairIdentifiers) if (source.includes(identifier)) violations.push(`${relativePath}: ${identifier}`);
  }
  assert.deepEqual(violations, []);
});

test("production deploy selects direct publish, live SSE and retention replay without transactional startup authority", async () => {
  const deploySource = await readFile(`${repositoryRoot}/deploy/deploy.yaml`, "utf8");
  for (const expected of [
    "HWLAB_KAFKA_DIRECT_PUBLISH_ENABLED: \"true\"",
    "HWLAB_WORKBENCH_LIVE_KAFKA_SSE_ENABLED: \"true\"",
    "HWLAB_WORKBENCH_KAFKA_REFRESH_REPLAY_ENABLED: \"true\"",
    "HWLAB_KAFKA_TRANSACTIONAL_PROJECTOR_ENABLED: \"false\"",
    "HWLAB_KAFKA_PROJECTION_OUTBOX_RELAY_ENABLED: \"false\"",
    "HWLAB_WORKBENCH_PROJECTION_REALTIME_ENABLED: \"false\""
  ]) assert.ok(deploySource.includes(expected), `deploy must contain ${expected}`);
});

test("feature schema remains product-only and excludes architecture environment keys", async () => {
  const schemaSource = await readFile(`${repositoryRoot}/config/feature-config.schema.json`, "utf8");
  for (const name of [
    "HWLAB_KAFKA_DIRECT_PUBLISH_ENABLED",
    "HWLAB_WORKBENCH_LIVE_KAFKA_SSE_ENABLED",
    "HWLAB_WORKBENCH_KAFKA_REFRESH_REPLAY_ENABLED",
    "HWLAB_KAFKA_TRANSACTIONAL_PROJECTOR_ENABLED",
    "HWLAB_KAFKA_PROJECTION_OUTBOX_RELAY_ENABLED",
    "HWLAB_WORKBENCH_PROJECTION_REALTIME_ENABLED"
  ]) assert.equal(schemaSource.includes(name), false, `${name} must not enter feature schema`);
});
