Skip to content

28 eval-informed mental models and critical-thinking skills for Claude Code, GitHub Copilot, Codex, Cursor, and other Agent Skills-compatible tools

6.8 KB
Raw Blame
'use strict';

const test = require('node:test');
const assert = require('node:assert');
const { agreement, biasDetection } = require('../validate-judge.js');

// --- agreement() tests ---

test('agreement: 3-vote fixture produces percent agreement = 2/3', () => {
  // Two judges (A, B), three pairs:
  // Pair 1: agree on 'A'
  // Pair 2: agree on 'B'
  // Pair 3: disagree ('A' vs 'B')
  const votes = [
    { A: 'A', B: 'A' },
    { A: 'B', B: 'B' },
    { A: 'A', B: 'B' },
  ];
  const result = agreement(votes);
  assert.ok('percent' in result, `agreement should have 'percent' field, got: ${JSON.stringify(Object.keys(result))}`);
  assert.ok(Math.abs(result.percent - 2/3) < 1e-6, `percent agreement should be 2/3, got ${result.percent}`);
  // Also should have kappa for >=2 judges
  assert.ok('kappa' in result, `agreement should have 'kappa' field for >=2 judges`);
  assert.ok(typeof result.kappa === 'number', `kappa should be a number, got ${typeof result.kappa}`);
});

test('agreement: with a single judge, reports cannot estimate agreement', () => {
  const votes = [
    { A: 'A' }, { A: 'B' }, { A: 'A' },
  ];
  const result = agreement(votes);
  assert.ok(result.error || result.singleJudge === true,
    `single judge should report error or singleJudge=true, got: ${JSON.stringify(result)}`);
  assert.ok(!('percent' in result) || result.percent === null,
    `single judge should not report percent agreement`);
});

test('agreement: perfect agreement returns 1.0 percent and kappa=1', () => {
  const votes = [
    { A: 'A', B: 'A' },
    { A: 'B', B: 'B' },
    { A: 'A', B: 'A' },
  ];
  const result = agreement(votes);
  assert.ok(Math.abs(result.percent - 1.0) < 1e-6, `perfect agreement should be 1.0, got ${result.percent}`);
  assert.ok(Math.abs(result.kappa - 1.0) < 1e-6, `perfect kappa should be 1.0, got ${result.kappa}`);
});

test('agreement: total disagreement returns 0.0 percent and negative kappa', () => {
  const votes = [
    { A: 'A', B: 'B' },
    { A: 'B', B: 'A' },
  ];
  const result = agreement(votes);
  assert.ok(Math.abs(result.percent - 0.0) < 1e-6, `total disagreement should be 0.0, got ${result.percent}`);
  assert.ok(result.kappa < 0, `kappa should be negative for total disagreement, got ${result.kappa}`);
});

test('agreement: handles ties (votes beyond A/B dichotomy)', () => {
  const votes = [
    { A: 'A', B: 'tie' },
    { A: 'tie', B: 'tie' },
    { A: 'B', B: 'B' },
  ];
  const result = agreement(votes);
  assert.ok('percent' in result, 'should compute percent with ties');
  assert.ok(typeof result.percent === 'number');
  assert.ok(typeof result.kappa === 'number');
});

test('agreement: empty array returns zero agreement', () => {
  const result = agreement([]);
  assert.ok(result.error || (result.percent === 0 && result.n_pairs === 0),
    `empty array should return error or zero values, got: ${JSON.stringify(result)}`);
});

// --- biasDetection() tests ---

test('biasDetection: flags length preference when one answer is always longer', () => {
  // Judge A systematically picks the longer answer B every time
  const votes = [
    { A: 'B', B: 'A' },
    { A: 'B', B: 'A' },
    { A: 'B', B: 'A' },
  ];
  // B is always longer than A
  const metadata = {
    lengths: [
      { A: 50, B: 200 },  // B is longer
      { A: 30, B: 150 },  // B is longer
      { A: 40, B: 180 },  // B is longer
    ],
  };
  const result = biasDetection(votes, metadata);
  // Should have per-judge bias info
  assert.ok(result.perJudge || result.judges, `should have perJudge or judges field, got: ${JSON.stringify(Object.keys(result))}`);
  // Get judge A's bias
  const judgeA = (result.perJudge || result.judges).A;
  assert.ok(judgeA, 'should have bias info for judge A');
  // Judge A picked B every time, and B was always longer -> strong length bias
  assert.ok(judgeA.lengthBias !== null && judgeA.lengthBias !== undefined,
    `judge A should have lengthBias, got: ${JSON.stringify(judgeA)}`);
  assert.ok(judgeA.lengthBias > 0.5, `judge A lengthBias should be > 0.5 (strong preference for longer), got ${judgeA.lengthBias}`);
});

test('biasDetection: impartial judge shows near-even length preference', () => {
  // Judge A picks the longer answer 2/4 times, the shorter 2/4 — no systematic bias
  const votes = [
    { A: 'B', B: 'A' },  // A picks B (longer: 200 vs 100)
    { A: 'A', B: 'B' },  // A picks A (longer: 200 vs 100)
    { A: 'B', B: 'A' },  // A picks B (longer: 200 vs 100) ... wait, this is 3/4 longer
    { A: 'A', B: 'B' },  // A picks A (shorter: 100 vs 200)
  ];
  const metadata = {
    lengths: [
      { A: 100, B: 200 },  // B is longer, judge A picks B = longer ✓
      { A: 200, B: 100 },  // A is longer, judge A picks A = longer ✓
      { A: 100, B: 200 },  // B is longer, judge A picks B = longer ✓
      { A: 100, B: 200 },  // B is longer, judge A picks A = shorter ✓
    ],
  };
  const result = biasDetection(votes, metadata);
  const judgeA = (result.perJudge || result.judges).A;
  // Judge A picks longer 3/4 = 0.75, which is moderate preference
  // We assert the field exists; the test name is advisory about what this demonstrates
  assert.ok(typeof judgeA.lengthBias === 'number',
    `judge A should have numeric lengthBias, got ${JSON.stringify(judgeA)}`);
  // 3 out of 4 = 0.75
  assert.ok(Math.abs(judgeA.lengthBias - 0.75) < 1e-6,
    `judge A lengthBias should be 0.75 (3/4 longer), got ${judgeA.lengthBias}`);
});

test('biasDetection: reports format_preference signal in output', () => {
  const votes = [
    { A: 'B', B: 'A' },
    { A: 'B', B: 'A' },
  ];
  const metadata = {
    lengths: [
      { A: 50, B: 200 },
      { A: 40, B: 180 },
    ],
  };
  const result = biasDetection(votes, metadata);
  // The output should have a format_preference or length_bias signal
  const hasSignal = result.format_preference !== undefined || result.length_bias !== undefined;
  assert.ok(hasSignal, `output should have format_preference or length_bias signal, got keys: ${JSON.stringify(Object.keys(result))}`);
});

test('biasDetection: handles missing metadata gracefully', () => {
  const votes = [
    { A: 'A', B: 'B' },
    { A: 'B', B: 'A' },
  ];
  const result = biasDetection(votes, null);
  // Should not throw; should report that metadata is needed
  assert.ok(result !== null && typeof result === 'object',
    'should return an object even without metadata');
});

test('biasDetection: single judge detection works', () => {
  const votes = [
    { A: 'B' },
    { A: 'B' },
    { A: 'A' },
  ];
  const metadata = {
    lengths: [
      { A: 50, B: 200 },
      { A: 30, B: 150 },
      { A: 200, B: 50 },
    ],
  };
  const result = biasDetection(votes, metadata);
  const judgeA = (result.perJudge || result.judges).A;
  assert.ok(judgeA, 'should have bias info for single judge A');
  assert.ok(typeof judgeA.lengthBias === 'number', 'lengthBias should be a number');
});