import assert from "node:assert/strict";
import test from "node:test";

import {
  DEVICE_DISCOVERY_SIGNAL_SCHEMA,
  DEVICE_PLANE_BINDING_SCHEMA,
  assertIdentifierDigest,
  assertSafeProjection,
  hashRestrictedIdentifier,
  normalizeDevicePlaneBinding,
  normalizeDiscoverySignal,
  toSafeDiscoveryView,
} from "../src/index.mjs";

const fakeImei = "000000000000001";
const fakeSignal = {
  schemaVersion: DEVICE_DISCOVERY_SIGNAL_SCHEMA,
  sessionRef: "session:test-001",
  modelProfileRef: "arusnavi.b2.internal.v1",
  protocol: "INTERNAL",
  observedAt: "2026-07-25T00:00:00.000Z",
  identifier: {
    kind: "imei",
    value: fakeImei,
  },
  evidence: {
    transport: "tcp",
    bytesObserved: 128,
    framingStatus: "verified",
    specificationRef: "arusnavi.internal.framing.test-v1",
  },
};

test("normalizes a verified discovery into quarantine with commands disabled", () => {
  const signal = normalizeDiscoverySignal(fakeSignal);
  assert.equal(signal.lifecycleState, "quarantine");
  assert.equal(signal.commandTransport, "disabled");
  assert.equal(signal.identifier.value, fakeImei);
});

test("safe discovery projection masks the restricted identifier", () => {
  const view = toSafeDiscoveryView(fakeSignal, {
    discoveryRef: "discovery:test-001",
  });
  const serialized = JSON.stringify(view);
  assert.equal(view.identifier.masked, "***********0001");
  assert.equal(serialized.includes(fakeImei), false);
  assertSafeProjection(view);
});

test("identifier hashing requires a strong process-only pepper", () => {
  const identifier = { kind: "imei", value: fakeImei };
  assert.throws(
    () => hashRestrictedIdentifier(identifier, "short"),
    /identifier_pepper_invalid/,
  );
  const digest = hashRestrictedIdentifier(
    identifier,
    "test-only-pepper-with-at-least-32-bytes",
  );
  assertIdentifierDigest(digest);
  assert.equal(digest.includes(fakeImei), false);
  assert.equal(
    digest,
    hashRestrictedIdentifier(
      identifier,
      "test-only-pepper-with-at-least-32-bytes",
    ),
  );
});

test("rejects unverified framing and command-shaped discovery input", () => {
  assert.throws(
    () => normalizeDiscoverySignal({
      ...fakeSignal,
      evidence: { ...fakeSignal.evidence, framingStatus: "unverified" },
    }),
    /discovery_evidence_framing_unverified/,
  );
  assert.throws(
    () => normalizeDiscoverySignal({
      ...fakeSignal,
      command: { kind: "restart" },
    }),
    /forbidden_device_field/,
  );
});

test("rejects secret-like fields recursively", () => {
  assert.throws(
    () => normalizeDiscoverySignal({
      ...fakeSignal,
      metadata: { devicePassword: "not-a-real-password" },
    }),
    /forbidden_device_field/,
  );
});

test("normalizes an opaque Foundry control binding without device data", () => {
  const binding = normalizeDevicePlaneBinding({
    schemaVersion: DEVICE_PLANE_BINDING_SCHEMA,
    bindingRef: "binding:test-001",
    contourRef: "contour:robot2b-test",
    capabilities: ["inspect", "observe", "observe"],
  });
  assert.deepEqual(binding.capabilities, ["inspect", "observe"]);
  assertSafeProjection(binding);
});
