ktg-plugin-marketplace/plugins/llm-security-copilot/tests/lib/distribution-stats.test.mjs
Kjell Tore Guttormsen f418a8fe08 feat(llm-security-copilot): port llm-security v5.1.0 to GitHub Copilot CLI
Full port of llm-security plugin for internal use on Windows with GitHub
Copilot CLI. Protocol translation layer (copilot-hook-runner.mjs)
normalizes Copilot camelCase I/O to Claude Code snake_case format — all
original hook scripts run unmodified.

- 8 hooks with protocol translation (stdin/stdout/exit code)
- 18 SKILL.md skills (Agent Skills Open Standard)
- 6 .agent.md agent definitions
- 20 scanners + 14 scanner lib modules (unchanged)
- 14 knowledge files (unchanged)
- 39 test files including copilot-port-verify.mjs (17 tests)
- Windows-ready: node:path, os.tmpdir(), process.execPath, no bash

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-09 21:56:10 +02:00

108 lines
4.2 KiB
JavaScript

// distribution-stats.test.mjs — Tests for scanners/lib/distribution-stats.mjs
// Zero external dependencies: node:test + node:assert only.
import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { jensenShannonDivergence, buildDistribution } from '../../scanners/lib/distribution-stats.mjs';
// ---------------------------------------------------------------------------
// buildDistribution
// ---------------------------------------------------------------------------
describe('distribution-stats — buildDistribution', () => {
it('empty array → empty map', () => {
const d = buildDistribution([]);
assert.equal(d.size, 0);
});
it('single category normalizes to 1.0', () => {
const d = buildDistribution(['Read', 'Read', 'Read']);
assert.equal(d.size, 1);
assert.equal(d.get('Read'), 1.0);
});
it('two equal categories normalize to 0.5 each', () => {
const d = buildDistribution(['Read', 'Bash', 'Read', 'Bash']);
assert.equal(d.size, 2);
assert.equal(d.get('Read'), 0.5);
assert.equal(d.get('Bash'), 0.5);
});
it('unequal distribution normalizes correctly', () => {
const d = buildDistribution(['Read', 'Read', 'Read', 'Bash']);
assert.equal(d.get('Read'), 0.75);
assert.equal(d.get('Bash'), 0.25);
});
it('sum of probabilities equals 1.0', () => {
const d = buildDistribution(['Read', 'Bash', 'Write', 'Grep', 'Bash']);
let sum = 0;
for (const v of d.values()) sum += v;
assert.ok(Math.abs(sum - 1.0) < 1e-10, `Sum ${sum} should be ~1.0`);
});
});
// ---------------------------------------------------------------------------
// jensenShannonDivergence
// ---------------------------------------------------------------------------
describe('distribution-stats — jensenShannonDivergence', () => {
it('identical distributions → JSD = 0', () => {
const P = buildDistribution(['Read', 'Bash', 'Read', 'Bash']);
const Q = buildDistribution(['Read', 'Bash', 'Read', 'Bash']);
const jsd = jensenShannonDivergence(P, Q);
assert.ok(Math.abs(jsd) < 1e-10, `JSD ${jsd} should be ~0`);
});
it('fully disjoint distributions → JSD = 1', () => {
const P = buildDistribution(['Read', 'Read', 'Read']);
const Q = buildDistribution(['Bash', 'Bash', 'Bash']);
const jsd = jensenShannonDivergence(P, Q);
assert.ok(Math.abs(jsd - 1.0) < 1e-10, `JSD ${jsd} should be ~1.0`);
});
it('partially overlapping distributions → 0 < JSD < 1', () => {
const P = buildDistribution(['Read', 'Read', 'Bash']);
const Q = buildDistribution(['Read', 'Bash', 'Bash']);
const jsd = jensenShannonDivergence(P, Q);
assert.ok(jsd > 0, `JSD ${jsd} should be > 0`);
assert.ok(jsd < 1, `JSD ${jsd} should be < 1`);
});
it('JSD is symmetric: JSD(P,Q) = JSD(Q,P)', () => {
const P = buildDistribution(['Read', 'Read', 'Read', 'Bash']);
const Q = buildDistribution(['Read', 'Bash', 'Bash', 'Bash']);
const jsd1 = jensenShannonDivergence(P, Q);
const jsd2 = jensenShannonDivergence(Q, P);
assert.ok(Math.abs(jsd1 - jsd2) < 1e-10, `JSD(P,Q)=${jsd1} should equal JSD(Q,P)=${jsd2}`);
});
it('two empty distributions → JSD = 0', () => {
const P = new Map();
const Q = new Map();
const jsd = jensenShannonDivergence(P, Q);
assert.equal(jsd, 0);
});
it('one empty + one non-empty → JSD = 0.5', () => {
const P = buildDistribution(['Read']);
const Q = new Map();
const jsd = jensenShannonDivergence(P, Q);
assert.ok(Math.abs(jsd - 0.5) < 1e-10, `JSD ${jsd} should be 0.5`);
});
it('three categories with different distributions', () => {
const P = buildDistribution(['Read', 'Read', 'Read', 'Write', 'Write', 'Bash']);
const Q = buildDistribution(['Read', 'Write', 'Write', 'Write', 'Bash', 'Bash']);
const jsd = jensenShannonDivergence(P, Q);
assert.ok(jsd > 0, `JSD ${jsd} should be > 0`);
assert.ok(jsd < 1, `JSD ${jsd} should be < 1`);
});
it('diverse vs concentrated → high JSD', () => {
const P = buildDistribution(['Read', 'Write', 'Bash', 'Grep', 'Glob']);
const Q = buildDistribution(['Read', 'Read', 'Read', 'Read', 'Read']);
const jsd = jensenShannonDivergence(P, Q);
assert.ok(jsd > 0.3, `JSD ${jsd} should be > 0.3 for diverse vs concentrated`);
});
});