const test = require("node:test");
const assert = require("node:assert/strict");
const { consentChanges } = require("../src/security/consent");

test("consent absent blocks both processing paths", () => {
  const value = consentChanges({ aiGranted: false, recordingGranted: false, policyVersion: "v1", actorId: "client", now: new Date(0) });
  assert.equal(value.ai_processing_consent_at, null);
  assert.equal(value.recording_consent_at, null);
  assert.equal(value.consent_withdrawn_at.getTime(), 0);
});

test("consent granted records actor, time, and policy version", () => {
  const value = consentChanges({ aiGranted: true, recordingGranted: true, policyVersion: "v1", actorId: "coach", now: new Date(0) });
  assert.equal(value.ai_processing_consent_policy_version, "v1");
  assert.equal(value.recording_consent_policy_version, "v1");
  assert.equal(value.consent_updated_by_id, "coach");
  assert.equal(value.consent_withdrawn_at, null);
});

test("withdrawal clears prior processing authorization", () => {
  const value = consentChanges({ aiGranted: false, recordingGranted: false, policyVersion: "v1", actorId: "client" });
  assert.equal(value.ai_processing_consent_granted, false);
  assert.equal(value.recording_consent_granted, false);
  assert.ok(value.consent_withdrawn_at);
});

test("policy update records the newly accepted version", () => {
  const value = consentChanges({ aiGranted: true, recordingGranted: true, policyVersion: "v2", actorId: "client" });
  assert.equal(value.ai_processing_consent_policy_version, "v2");
  assert.equal(value.recording_consent_policy_version, "v2");
});
