113 lines
6.3 KiB
TypeScript
113 lines
6.3 KiB
TypeScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { mkdtempSync, rmSync } from "node:fs";
|
|
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 { 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";
|
|
|
|
test("research revisions share the authentication database and preserve replay limits", () => {
|
|
const dir = mkdtempSync(join(tmpdir(), "site-research-"));
|
|
const previous = process.env.AUTH_CONFIG_FILE;
|
|
process.env.AUTH_CONFIG_FILE = join(dir, "config.json");
|
|
try {
|
|
initializeStore();
|
|
const config = newAuthConfig();
|
|
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.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 }[];
|
|
assert.ok(tables.some((r) => r.name === "login_state"));
|
|
assert.ok(tables.some((r) => r.name === "research_companies"));
|
|
assert.equal(db.prepare("PRAGMA foreign_keys").get()?.foreign_keys, 1);
|
|
db.close();
|
|
const next = structuredClone(seed) as ResearchSnapshot;
|
|
next.version += "-revision-test";
|
|
next.companies[0].name = "Revised company";
|
|
assert.equal(readResearchSnapshot(next).companies[0].name, "Revised company");
|
|
assert.deepEqual(readResearchSnapshot(), initial, "old release still reads its original revision");
|
|
assert.equal(consumeCode(config, "invalid"), "limited", "research import must not reset auth limits");
|
|
const collision = structuredClone(seed) as ResearchSnapshot;
|
|
collision.companies[0].name = "Changed without revision";
|
|
assert.throws(() => readResearchSnapshot(collision), /new revision/);
|
|
assert.deepEqual(readResearchSnapshot(), initial, "failed import leaves valid revision intact");
|
|
} finally {
|
|
if (previous === undefined) delete process.env.AUTH_CONFIG_FILE;
|
|
else process.env.AUTH_CONFIG_FILE = previous;
|
|
rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test("research validation rejects broken evidence and unsafe source links", () => {
|
|
validateResearch(seed as ResearchSnapshot);
|
|
const invalid = structuredClone(seed) as ResearchSnapshot;
|
|
invalid.observations[0].evidence = "missing";
|
|
assert.throws(() => validateResearch(invalid), /observation/);
|
|
assert.equal(evidenceUrl("javascript:alert(1)"), undefined);
|
|
assert.equal(evidenceUrl("/local-link"), undefined);
|
|
assert.equal(evidenceUrl("https://example.com/evidence"), "https://example.com/evidence");
|
|
});
|
|
|
|
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.self, 0), 8);
|
|
assert.equal(summary.reduce((n, c) => n + c.estimate, 0), 1);
|
|
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");
|
|
assert.ok(snapshot.observations.filter((o) => o.selection === "signal").every((o) => o.scope === "公司整体"));
|
|
});
|
|
|
|
// Retractions and forecasts must stay visible for review without becoming revenue signals.
|
|
test("retracted ARR and projected margins remain excluded", () => {
|
|
const snapshot = seed as ResearchSnapshot;
|
|
const cluely = snapshot.companies.find((c) => c.name === "Cluely")!;
|
|
assert.equal(cluely.revenueStatus, "unknown");
|
|
assert.ok(snapshot.reviews.some((r) => r.company === "Cluely" && r.evidenceIds.includes("E100")));
|
|
const retracted = snapshot.observations.filter((o) => o.company === cluely.id);
|
|
assert.ok(retracted.length > 0);
|
|
assert.ok(retracted.every((o) => o.selection === "excluded"));
|
|
const forecast = snapshot.observations.find((o) => o.kind === "非GAAP毛利率预期")!;
|
|
assert.equal(forecast.selection, "excluded");
|
|
const peec = snapshot.companies.find((c) => c.name === "Peec AI")!;
|
|
assert.equal(peec.teamBound, ">");
|
|
assert.equal(snapshot.observations.filter((o) => o.company === peec.id && o.selection === "signal").length, 1);
|
|
assert.ok(snapshot.observations.some((o) => o.company === peec.id && o.selection === "historical-estimate"));
|
|
});
|
|
|
|
test("directory coverage reconciles every candidate and keeps profile-only income unknown", () => {
|
|
const snapshot = seed as ResearchSnapshot;
|
|
const coverage = snapshot.coverage!;
|
|
assert.equal(coverageAudit.records.length, coverage.candidateRecords);
|
|
assert.equal(new Set(coverageAudit.records.map((r) => r.ycId)).size, coverage.candidateRecords);
|
|
const count = (status: string) => coverageAudit.records.filter((r) => r.status === status).length;
|
|
assert.equal(count("imported"), coverage.newProfiles);
|
|
assert.equal(count("existing"), coverage.existingProfiles);
|
|
assert.equal(count("date-conflict"), coverage.dateConflicts);
|
|
assert.equal(count("year-outside-or-missing"), coverage.otherOrMissingYear);
|
|
assert.equal(count("ai-unclear"), coverage.unclearAI);
|
|
for (const row of coverageAudit.records.filter((r) => r.status === "imported")) {
|
|
const company = snapshot.companies.find((c) => c.id === `YC${row.ycId}`)!;
|
|
assert.ok(company.profileOnly);
|
|
assert.equal(company.url, row.source);
|
|
assert.equal(company.revenueStatus, "unknown");
|
|
assert.equal(company.year, row.foundedYear);
|
|
assert.ok([2025, 2026].includes(company.year!));
|
|
}
|
|
const invalid = structuredClone(snapshot);
|
|
invalid.coverage!.newProfiles += 1;
|
|
assert.throws(() => validateResearch(invalid), /coverage accounting/);
|
|
});
|