linkedin-studio/scripts/analytics/tests/stats.test.ts
Kjell Tore Guttormsen 4dc868ea62 feat(ultraplan-local): v1.6.0 — /ultraresearch-local deep research command
Add /ultraresearch-local for structured research combining local codebase
analysis with external knowledge via parallel agent swarms. Produces research
briefs with triangulation, confidence ratings, and source quality assessment.

New command: /ultraresearch-local with modes --quick, --local, --external, --fg.
New agents: research-orchestrator (opus), docs-researcher, community-researcher,
security-researcher, contrarian-researcher, gemini-bridge (all sonnet).
New template: research-brief-template.md.

Integration: --research flag in /ultraplan-local accepts pre-built research
briefs (up to 3), enriches the interview and exploration phases. Planning
orchestrator cross-references brief findings during synthesis.

Design principle: Context Engineering — right information to right agent at
right time. Research briefs are structured artifacts in the pipeline:
ultraresearch → brief → ultraplan --research → plan → ultraexecute.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-08 08:58:35 +02:00

139 lines
4 KiB
TypeScript

import { describe, test } from "node:test";
import assert from "node:assert/strict";
import {
mean,
standardDeviation,
trendDirection,
percentChange,
deviationsFromMean,
} from "../src/utils/stats.js";
describe("stats", () => {
describe("mean", () => {
test("should return mean of values", () => {
const result = mean([10, 20, 30]);
assert.equal(result, 20);
});
test("should return 0 for empty array", () => {
const result = mean([]);
assert.equal(result, 0);
});
test("should handle single value", () => {
const result = mean([42]);
assert.equal(result, 42);
});
});
describe("standardDeviation", () => {
test("should calculate correctly for known values", () => {
// For [2, 4, 4, 4, 5, 5, 7, 9]:
// Mean = 5
// Variance = ((2-5)^2 + (4-5)^2 + (4-5)^2 + (4-5)^2 + (5-5)^2 + (5-5)^2 + (7-5)^2 + (9-5)^2) / 8
// Variance = (9 + 1 + 1 + 1 + 0 + 0 + 4 + 16) / 8 = 32 / 8 = 4
// StdDev = 2
const result = standardDeviation([2, 4, 4, 4, 5, 5, 7, 9]);
assert.equal(result, 2);
});
test("should return 0 for single value", () => {
const result = standardDeviation([5]);
assert.equal(result, 0);
});
test("should return 0 for empty array", () => {
const result = standardDeviation([]);
assert.equal(result, 0);
});
test("should handle uniform values", () => {
const result = standardDeviation([5, 5, 5, 5]);
assert.equal(result, 0);
});
});
describe("trendDirection", () => {
test("should detect up trend", () => {
const result = trendDirection(110, 100);
assert.equal(result, "up");
});
test("should detect down trend", () => {
const result = trendDirection(90, 100);
assert.equal(result, "down");
});
test("should detect stable trend", () => {
const result = trendDirection(103, 100);
assert.equal(result, "stable");
});
test("should use custom threshold", () => {
const result = trendDirection(103, 100, 10);
assert.equal(result, "stable");
});
test("should detect up with custom threshold", () => {
const result = trendDirection(112, 100, 10);
assert.equal(result, "up");
});
});
describe("percentChange", () => {
test("should calculate positive change correctly", () => {
const result = percentChange(110, 100);
assert.equal(result, 10);
});
test("should calculate negative change correctly", () => {
const result = percentChange(90, 100);
assert.equal(result, -10);
});
test("should handle zero previous value", () => {
const result = percentChange(100, 0);
assert.equal(result, 0);
});
test("should handle zero current value", () => {
const result = percentChange(0, 100);
assert.equal(result, -100);
});
test("should handle no change", () => {
const result = percentChange(100, 100);
assert.equal(result, 0);
});
});
describe("deviationsFromMean", () => {
test("should calculate correctly for value above mean", () => {
// Mean of [10, 20, 30] = 20
// StdDev = sqrt(((10-20)^2 + (20-20)^2 + (30-20)^2) / 3) = sqrt((100 + 0 + 100) / 3) = sqrt(66.67) ≈ 8.165
// Deviations for 30 = (30 - 20) / 8.165 ≈ 1.225
const result = deviationsFromMean(30, [10, 20, 30]);
assert.ok(Math.abs(result - 1.225) < 0.01);
});
test("should calculate correctly for value below mean", () => {
const result = deviationsFromMean(10, [10, 20, 30]);
assert.ok(Math.abs(result + 1.225) < 0.01); // Negative deviation
});
test("should return 0 for uniform data", () => {
const result = deviationsFromMean(5, [5, 5, 5]);
assert.equal(result, 0);
});
test("should return 0 for single value", () => {
const result = deviationsFromMean(5, [5]);
assert.equal(result, 0);
});
test("should calculate for value at mean", () => {
const result = deviationsFromMean(20, [10, 20, 30]);
assert.ok(Math.abs(result) < 0.01);
});
});
});