import test from "node:test";
import assert from "node:assert/strict";
import {
  buildZerodhaBackendCallbackUrl,
  parseZerodhaCallbackParams,
} from "./zerodhaCallbackUtils";

test("parseZerodhaCallbackParams reads state from callback query", () => {
  const parsed = parseZerodhaCallbackParams(
    "?request_token=req-123&state=state-456&status=success&flow=reconnect",
  );

  assert.equal(parsed.requestToken, "req-123");
  assert.equal(parsed.state, "state-456");
  assert.equal(parsed.status, "success");
  assert.equal(parsed.flow, "reconnect");
});

test("buildZerodhaBackendCallbackUrl forwards request token and state", () => {
  const url = buildZerodhaBackendCallbackUrl({
    flow: "connect",
    status: "success",
    requestToken: "req-123",
    state: "state-456",
    error: "",
    errorDescription: "",
  });

  assert.equal(url, "/broker/zerodha/callback?request_token=req-123&state=state-456");
});

test("buildZerodhaBackendCallbackUrl uses reconnect callback route", () => {
  const url = buildZerodhaBackendCallbackUrl({
    flow: "reconnect",
    status: "success",
    requestToken: "req-123",
    state: "state-456",
    error: "",
    errorDescription: "",
  });

  assert.equal(url, "/broker/callback?request_token=req-123&state=state-456");
});
