71 lines
3.8 KiB
TypeScript
71 lines
3.8 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 { 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, 54);
|
|
assert.equal(initial.evidence.length, 87);
|
|
assert.equal(initial.observations.length, 63);
|
|
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), 54);
|
|
assert.equal(summary.reduce((n, c) => n + c.self, 0), 5);
|
|
assert.equal(summary.reduce((n, c) => n + c.estimate, 0), 1);
|
|
assert.equal(summary.reduce((n, c) => n + c.unknown, 0), 48);
|
|
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 === "公司整体"));
|
|
});
|