import assert from "node:assert/strict";
import { test } from "bun:test";

import type { WorkbenchKafkaDebugReplayResult } from "@/api/workbench-debug";
import { classifyWorkbenchKafkaDebugLocalResult } from "./workbench-kafka-debug-local-result";

test("local replay classification reports transport_failed below server delivered", () => {
  const result = classifyWorkbenchKafkaDebugLocalResult(terminalResult(3), { clientReceived: 2, decoded: 2, applied: 2 });
  assert.equal(result.ok, false);
  assert.equal(result.phase, "client");
  assert.equal(result.code, "transport_failed");
  assert.deepEqual(result.counts, { delivered: 3, clientReceived: 2, decoded: 2, applied: 2 });
});

test("local replay classification reports transport_failed above server delivered", () => {
  const result = classifyWorkbenchKafkaDebugLocalResult(terminalResult(3), { clientReceived: 4, decoded: 4, applied: 4 });
  assert.equal(result.ok, false);
  assert.equal(result.code, "transport_failed");
});

test("local replay classification reports contract_invalid when server delivered is missing", () => {
  const result = classifyWorkbenchKafkaDebugLocalResult(terminalResult(), { clientReceived: 0, decoded: 0, applied: 0 });
  assert.equal(result.ok, false);
  assert.equal(result.code, "contract_invalid");
});

test("local replay classification reports decoder_rejected below client received", () => {
  const result = classifyWorkbenchKafkaDebugLocalResult(terminalResult(3), { clientReceived: 3, decoded: 2, applied: 2 });
  assert.equal(result.ok, false);
  assert.equal(result.code, "decoder_rejected");
});

test("local replay classification reports decoder_rejected above client received", () => {
  const result = classifyWorkbenchKafkaDebugLocalResult(terminalResult(3), { clientReceived: 3, decoded: 4, applied: 4 });
  assert.equal(result.ok, false);
  assert.equal(result.code, "decoder_rejected");
});

test("local replay classification reports reducer_rejected below decoded", () => {
  const result = classifyWorkbenchKafkaDebugLocalResult(terminalResult(3), { clientReceived: 3, decoded: 3, applied: 2 });
  assert.equal(result.ok, false);
  assert.equal(result.code, "reducer_rejected");
});

test("local replay classification reports reducer_rejected above decoded", () => {
  const result = classifyWorkbenchKafkaDebugLocalResult(terminalResult(3), { clientReceived: 3, decoded: 3, applied: 4 });
  assert.equal(result.ok, false);
  assert.equal(result.code, "reducer_rejected");
});

test("local replay classification preserves terminal_complete when every client stage agrees", () => {
  const result = classifyWorkbenchKafkaDebugLocalResult(terminalResult(3), { clientReceived: 3, decoded: 3, applied: 3 });
  assert.equal(result.ok, true);
  assert.equal(result.phase, "terminal");
  assert.equal(result.code, "terminal_complete");
  assert.equal(result.localOverlay, true);
});

function terminalResult(delivered?: number): WorkbenchKafkaDebugReplayResult {
  return {
    contractVersion: "workbench-kafka-debug-replay-v2",
    ok: true,
    phase: "terminal",
    code: "terminal_complete",
    terminalObserved: true,
    counts: { ...(delivered === undefined ? {} : { delivered }), clientReceived: null, decoded: null, applied: null }
  };
}
