#!/usr/bin/env node
// verify-is-93-percent-of-communication-nonverbal.mjs
//
// Offline gate for /strata/is-93-percent-of-communication-nonverbal/.
// Recomputes EVERY number shown on the page from Mehrabian's actual weighted
// equation (Silent Messages, 1971), two independent ways where possible:
//   Inferred liking = 0.07 * verbal + 0.38 * vocal + 0.55 * facial
// The two 1967 source studies (word-vs-tone; tone-vs-face, no verbal channel)
// are documented in the README; the coefficients below are the digitized values.
//
// Exits 0 iff all checks pass, non-zero otherwise.

const W_VERBAL = 0.07; // verbal / word
const W_VOCAL  = 0.38; // vocal / tone
const W_FACIAL = 0.55; // facial

let pass = 0, fail = 0;
const EPS = 1e-9;

function check(name, cond, detail = '') {
  const ok = !!cond;
  if (ok) pass++; else fail++;
  console.log(`${ok ? 'PASS' : 'FAIL'}  ${name}${detail ? '  ' + detail : ''}`);
}
function approx(a, b, eps = EPS) { return Math.abs(a - b) <= eps; }

// ── the equation, method A (direct weighted sum) ─────────────────────────
function inferredA(word, tone, face) {
  return W_VERBAL * word + W_VOCAL * tone + W_FACIAL * face;
}
// ── the equation, method B (dot product, independent implementation) ─────
function inferredB(word, tone, face) {
  const w = [W_VERBAL, W_VOCAL, W_FACIAL];
  const x = [word, tone, face];
  let s = 0;
  for (let i = 0; i < 3; i++) s += w[i] * x[i];
  return s;
}

console.log('# Mehrabian 7/38/55 verification\n');

// 1) coefficients sum to exactly 1.00
const sum = W_VERBAL + W_VOCAL + W_FACIAL;
check('coefficients sum to 1.00', approx(sum, 1.0, 1e-12), `(= ${sum.toFixed(4)})`);

// 2) nonverbal (vocal + facial) = 0.93  -> the "93% nonverbal" figure
const nonverbal = W_VOCAL + W_FACIAL;
check('nonverbal (vocal + facial) = 0.93', approx(nonverbal, 0.93, 1e-12), `(= ${nonverbal.toFixed(4)})`);

// 3) the split is not swapped: tone 0.38, face 0.55  (hazard #7)
check('tone weight is 0.38 (vocal), not 0.55', approx(W_VOCAL, 0.38, 1e-12));
check('face weight is 0.55 (facial), not 0.38', approx(W_FACIAL, 0.55, 1e-12));
check('verbal weight is 0.07', approx(W_VERBAL, 0.07, 1e-12));

// 4) methods A and B agree on a sweep of all 27 channel combinations
let abAgree = true;
for (const word of [-1, 0, 1])
  for (const tone of [-1, 0, 1])
    for (const face of [-1, 0, 1])
      if (!approx(inferredA(word, tone, face), inferredB(word, tone, face))) abAgree = false;
check('direct-sum and dot-product agree on all 27 combinations', abAgree);

// 5) WEIGHTS ARE INERT WHEN CUES AGREE: all three at valence x -> inferred = x
//    (true for any weights summing to 1; here we prove it for the digitized set)
let inert = true;
for (const x of [-1, 0, 1]) if (!approx(inferredA(x, x, x), x)) inert = false;
check('all-agree at x -> inferred = x exactly (split changes nothing)', inert);

// 6) the equal-weight (1/3 each) average lands the same SIGN as the weighted
//    result whenever the cues do not conflict (no opposing signs)
function conflicts(word, tone, face) {
  const pos = word > 0 || tone > 0 || face > 0;
  const neg = word < 0 || tone < 0 || face < 0;
  return pos && neg;
}
function avg3(word, tone, face) { return (word + tone + face) / 3; }
let sameSignWhenAgree = true;
for (const word of [-1, 0, 1])
  for (const tone of [-1, 0, 1])
    for (const face of [-1, 0, 1]) {
      if (conflicts(word, tone, face)) continue;
      const w = inferredA(word, tone, face), a = avg3(word, tone, face);
      if (Math.sign(w) !== Math.sign(a)) sameSignWhenAgree = false;
    }
check('no conflict -> weighted and equal-average share sign (weighting inert)', sameSignWhenAgree);

// 7) THE CANONICAL CONFLICT reproduced: positive word, cold tone, scowl
//    "thanks"(+1) + cold(-1) + scowl(-1) = 0.07 - 0.38 - 0.55 = -0.86
const conflictVal = inferredA(1, -1, -1);
check('conflict (+1 word, -1 tone, -1 face) = -0.86', approx(conflictVal, -0.86),
      `(= ${conflictVal.toFixed(2)})`);
check('  ...and the verdict is DISLIKE (nonverbal overrides the positive word)', conflictVal < 0);

// 8) the verbal channel can move the result by at most its whole weight, 0.07:
//    span from word=-1 to word=+1 (others fixed) = 0.14
const vspan = inferredA(1, 0, 0) - inferredA(-1, 0, 0);
check('verbal channel total span = 0.14 (max +/-0.07 pull)', approx(vspan, 0.14), `(= ${vspan.toFixed(4)})`);

// 9) nonverbal pull vs verbal pull in the canonical conflict
const nvPull = W_VOCAL * (-1) + W_FACIAL * (-1);  // -0.93
const vbPull = W_VERBAL * (1);                    // +0.07
check('conflict: nonverbal pull = -0.93 vs verbal pull = +0.07', approx(nvPull, -0.93) && approx(vbPull, 0.07),
      `(nv ${nvPull.toFixed(2)}, vb ${vbPull.toFixed(2)})`);

// 10) an all-warm agreement reads +1.00 (the "all agree (warm)" preset)
const warm = inferredA(1, 1, 1);
check('all-warm preset -> inferred = +1.00', approx(warm, 1.0), `(= ${warm.toFixed(2)})`);

// 11) informational-message invariance (the mode toggle): the equation models
//     an ATTITUDE inference, not information. We assert the design fact that no
//     rival single "true percentage" is computed for information -- there is
//     none to compute. (Documentary check: the model exposes no such constant.)
check('no rival "true percentage" constant is defined (question is ill-posed)',
      typeof globalThis.TRUE_NONVERBAL_PERCENT === 'undefined');

// ── source-fact ledger (cited, not computed; printed for the record) ─────
console.log('\n# cited source facts (see README):');
console.log('  Study 1  Mehrabian & Wiener 1967  J Pers Soc Psychol 6(1):109-114  (word vs tone)');
console.log('  Study 2  Mehrabian & Ferris 1967  J Consult Psychol 31(3):248-252  (tone vs face, NO verbal channel)');
console.log('  Stitch   Mehrabian 1971  Silent Messages  ->  0.07 verbal + 0.38 vocal + 0.55 facial');
console.log('  n total  ~137 undergraduates; single isolated words; liking/attitude inference only');

console.log(`\n${fail === 0 ? 'ALL PASS' : 'FAILED'}: ${pass} passed, ${fail} failed.`);
process.exit(fail === 0 ? 0 : 1);
