新增支付平台收入样本与分布分析

This commit is contained in:
2026-10-30 05:25:05 +08:00
parent 6d26a5b0c1
commit 69fa0b240f
10 changed files with 2172 additions and 29 deletions
+78 -4
View File
@@ -5,11 +5,13 @@ import { join } from "node:path";
import { tmpdir } from "node:os";
import seed from "../../../content/research/ai-companies.seed.json";
import coverageAudit from "../../../content/research/ai-companies.coverage.json";
import incomeReference from "../../../content/research/ai-income-analysis.json";
import { readResearchSnapshot } from "../src/lib/ai-research-store";
import { openSiteDatabase } from "../src/lib/site-database";
import { categorySummary, evidenceUrl, validateResearch, type ResearchSnapshot } from "../src/lib/ai-research";
import { consumeCode, initializeStore } from "../src/lib/auth-store";
import { newAuthConfig } from "../src/lib/auth";
import { describeIncome, groupIncome, platformIncomeSample } from "../src/lib/research-statistics";
test("research revisions share the authentication database and preserve replay limits", () => {
const dir = mkdtempSync(join(tmpdir(), "site-research-"));
@@ -21,9 +23,9 @@ test("research revisions share the authentication database and preserve replay l
for (let i = 0; i < 20; i++) consumeCode(config, "invalid");
assert.equal(consumeCode(config, "invalid"), "limited");
const initial = readResearchSnapshot();
assert.equal(initial.companies.length, 986);
assert.equal(initial.evidence.length, 1035);
assert.equal(initial.observations.length, 75);
assert.equal(initial.companies.length, 1002);
assert.equal(initial.evidence.length, 1055);
assert.equal(initial.observations.length, 126);
assert.deepEqual(readResearchSnapshot(), initial, "reopening does not duplicate records");
const db = openSiteDatabase();
const tables = db.prepare("SELECT name FROM sqlite_master WHERE type='table'").all() as { name: string }[];
@@ -61,9 +63,10 @@ test("research validation rejects broken evidence and unsafe source links", () =
test("disclosure counts exclude client income, biographies, targets and duplicate milestones", () => {
const snapshot = seed as ResearchSnapshot;
const summary = categorySummary(snapshot);
assert.equal(summary.reduce((n, c) => n + c.count, 0), 986);
assert.equal(summary.reduce((n, c) => n + c.count, 0), 1002);
assert.equal(summary.reduce((n, c) => n + c.self, 0), 8);
assert.equal(summary.reduce((n, c) => n + c.estimate, 0), 1);
assert.equal(summary.reduce((n, c) => n + c.platform, 0), 16);
assert.equal(summary.reduce((n, c) => n + c.unknown, 0), 977);
assert.equal(snapshot.companies.find((c) => c.name === "Rork")?.revenueStatus, "unknown");
assert.equal(snapshot.companies.find((c) => c.name === "Result")?.revenueStatus, "unknown");
@@ -110,3 +113,74 @@ test("directory coverage reconciles every candidate and keeps profile-only incom
invalid.coverage!.newProfiles += 1;
assert.throws(() => validateResearch(invalid), /coverage accounting/);
});
test("income descriptions report sample dispersion and maximum-value sensitivity", () => {
const stats = describeIncome([100, 200, 300])!;
assert.equal(stats.mean, 200);
assert.equal(stats.median, 200);
assert.equal(stats.sampleDeviation, 100);
assert.equal(stats.cv, .5);
assert.equal(stats.largestShare, .5);
assert.equal(stats.meanWithoutLargest, 150);
assert.equal(stats.largestRemovalDrop, .25);
assert.equal(describeIncome([5])!.cv, null);
assert.equal(describeIncome([0, 0])!.mean, 0, "observed zero is distinct from missing");
assert.equal(describeIncome([0, 0])!.cv, null);
assert.equal(describeIncome([]), null);
assert.equal(describeIncome([100, NaN]), null);
assert.equal(describeIncome([-1]), null);
assert.ok(describeIncome([1, 2, 100])!.largestRemovalDrop! > .95);
});
test("platform samples exclude stale data and keep ARR, estimates and collections separate", () => {
const snapshot = seed as ResearchSnapshot;
const recent = platformIncomeSample(snapshot, { windowDays: 7 });
assert.equal(recent.points.length, 14);
assert.equal(recent.excluded.length, 3);
assert.ok(recent.points.every((p) => p.observation.metric === "mrr" && p.observation.selection === "platform"));
assert.deepEqual(new Set(recent.excluded.map((p) => p.observation.company)), new Set(["AI016", "MR013", "MR014"]));
assert.ok(recent.points.some((p) => p.company.name === "TrackAI" && p.observation.connectionExpired), "dated recent value is not a claim of active connection");
const older = platformIncomeSample(snapshot, { windowDays: 90 });
assert.equal(older.points.length, 15);
assert.ok(older.points.some((p) => p.company.id === "AI016"));
const scaled = platformIncomeSample(snapshot, { windowDays: 7, annualize: true });
const monthlyGroups = groupIncome(recent.points);
const annualGroups = groupIncome(scaled.points);
for (const group of monthlyGroups) {
const annual = annualGroups.find((g) => g.key === group.key)!;
assert.ok(Math.abs(annual.stats.mean - group.stats.mean * 12) < 1e-6);
if (group.stats.cv !== null) assert.ok(Math.abs(annual.stats.cv! - group.stats.cv) < 1e-10);
}
const invalid = structuredClone(snapshot);
invalid.observations = invalid.observations.filter((o) => o.company === "MR001" && o.metric === "mrr");
const first = invalid.observations[0];
invalid.observations.push({ ...first, id: "future", observedAt: "2026-11-01", value: 999999 });
invalid.observations.push({ ...first, id: "bad-date", observedAt: "2026-02-30", value: 999999 });
invalid.observations.push({ ...first, id: "currency", unit: "EUR", value: 999999 });
invalid.observations.push({ ...first, id: "older", observedAt: "2026-10-28", value: 1 });
const selected = platformIncomeSample(invalid, { windowDays: 7 });
assert.equal(selected.points.length, 1);
assert.equal(selected.points[0].observation.id, first.id, "bad newer data and older duplicate must not replace latest valid point");
assert.equal(selected.excluded.length, 4);
assert.throws(() => platformIncomeSample(snapshot, { windowDays: -1 }), /window/);
});
test("distribution statistics match independently calculated snapshot references", () => {
const snapshot = seed as ResearchSnapshot;
assert.equal(incomeReference.sourceVersion, snapshot.version);
for (const reference of incomeReference.windows) {
const sample = platformIncomeSample(snapshot, { windowDays: reference.days });
assert.deepEqual(new Set(sample.points.map((p) => p.company.id)), new Set(reference.products));
const groups = groupIncome(sample.points);
assert.equal(groups.length, reference.groups.length);
for (const expected of reference.groups) {
const actual = groups.find((g) => g.category === expected.category && g.provider === expected.provider && g.model === expected.model)!;
assert.equal(actual.stats.n, expected.n);
for (const metric of ["mean", "median", "sampleDeviation", "cv", "meanWithoutLargest", "largestRemovalDrop"] as const) {
const value = expected[metric];
if (value === null) assert.equal(actual.stats[metric], null);
else assert.ok(Math.abs(actual.stats[metric]! - value) < 1e-6, metric);
}
}
}
});