Files
CatoandClaude Opus 5.5 2d0eb1d6aa feat(entities): stub companies from the relation graph, and a lab entity kind (#235)
* feat(entities): stub companies from the relation graph, and a lab entity kind

Stub companies: every minted company:<slug> node a stated or corroborated
relation touches (1,188 today; NAC Kazatomprom JSC, JV Inkai LLP, Uranium One
Inc.) becomes an entity built from the graph at load, with evidence "stub", a
page on the existing /markets/[slug] route that lists only the relations
naming it (grade, quote, source), and the note that no profile is held.
Rendered on demand, so the static page count is unchanged. The graph's
producers resolve against the directory only (resolveInDirectory), so the
stubs never change the graph they come from. Search and retrieval rank stubs
at 0.6 and leave word rarity to full-weight documents, so profiled rankings
do not move. Events and relations may name a stub; ownership and facility
operator fields stay directory-only. Reviewed aliases redirect to the
canonical page.

Labs: a closed-set `lab` kind (allowEmpty until the pilot fills it), the
labs candidate facet with schema and add.ts gate, a /labs/[slug] profile
(what it works on, where and how to join, stated funders from finances
relations), the lab dossier on lab:<id> with research fronts kept as their
own science kind, and the country people_labs box counting lab rows. Seeds
no lab data. Person is not added: the ADR pins it for office and board
holders, not lab staff.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018WZnBnaLP6RhwFkLxRTKgE

* test(stubs): assert the summary's invariants, not its tail

Main now carries the pilot's Kazatomprom relations, so its stub summary
names a second source and the shipments. The test keeps what must hold:
the yearbook leads, the stakes and the shipments are named.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018WZnBnaLP6RhwFkLxRTKgE

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-24 10:51:06 +02:00

308 lines
13 KiB
TypeScript

/**
* The profile system's promise is that a new section is one file.
*
* That promise is only true if selection is data-driven, so these tests check
* the contract rather than any particular module: a module declares the kinds
* it serves and its importance, and every entity of those kinds then shows it
* with no page edit anywhere.
*/
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { readdirSync, readFileSync } from 'node:fs';
import { allEntities, entitiesOfKind } from '../lib/entities/registry';
import { ENTITY_KINDS, type EntityKind } from '../lib/entities/types';
import { PROFILE_MODULES, modulesFor } from '../lib/profile/modules';
import { defineModule } from '../lib/profile/define';
import { connectionsFor } from '../lib/profile/modules/shared';
import { AROUND_GROUPS, aroundFor } from '../lib/profile/modules/science-around';
import { neighbors } from '../lib/graph';
import { COMPANY_SECTION_IMPORTANCE, COMPANY_SECTION_ORDER } from '../lib/company-sections';
import { sampleOf } from './fixtures/sample-entity';
test('module ids are unique and ordering is deterministic', () => {
const ids = PROFILE_MODULES.map((m) => m.id);
assert.equal(new Set(ids).size, ids.length, 'two modules share an id');
const sample = allEntities()[0];
const order = modulesFor(sample).map((m) => m.importance);
assert.deepEqual(
[...order].sort((a, b) => a - b),
order,
'modules are not sorted by importance',
);
});
test('discussion reaches every kind, because a claim is tested by being argued with', () => {
for (const kind of ENTITY_KINDS) {
const entity = sampleOf(kind);
assert.ok(entity, `no ${kind} entity`);
const ids = modulesFor(entity).map((m) => m.id);
assert.ok(ids.includes('discussion'), `${kind} has no discussion module`);
}
});
test('a discussion thread is keyed by a stable path, not by a filtered URL', () => {
// A country's href carries a query string; if the thread were keyed on it,
// the same country would hold different discussions per filter state.
const country = entitiesOfKind('country')[0];
const discussion = PROFILE_MODULES.find((m) => m.id === 'discussion');
assert.ok(discussion && country);
const output = discussion.render(country);
assert.ok(output, 'country should have a discussion');
assert.ok(!country.href.split('?')[0].includes('?'));
});
test('a company profile reads chokepoints first and discussion last', () => {
// The 2026-09 redesign replaced "What it makes" (materials only), the topic
// chips and the boilerplate "questions not answered" block with sections
// read from the full join. Order is the reading order: what it holds, what
// happened, then connections and discussion.
const company = entitiesOfKind('company').find((e) => e.key === 'asml');
assert.ok(company, 'ASML should be in the corpus');
const ids = modulesFor(company).map((m) => m.id);
for (const expected of ['chokepoints', 'events', 'sources', 'discussion']) {
assert.ok(ids.includes(expected), `company profile lost "${expected}"`);
}
assert.ok(ids.indexOf('chokepoints') < ids.indexOf('events'), 'chokepoints precede events');
assert.ok(ids.indexOf('events') < ids.indexOf('discussion'), 'discussion goes last');
});
test('company modules do not leak onto kinds that have no such data', () => {
// `products` reads the market directory; asking it about a country must not
// throw or invent a row.
const country = entitiesOfKind('country')[0];
const ids = modulesFor(country).map((m) => m.id);
for (const companyOnly of ['products', 'topics', 'relief', 'gaps', 'timeline']) {
assert.ok(!ids.includes(companyOnly), `${companyOnly} should not apply to a country`);
}
});
test('a new module appears on every kind it declares, with no page edit', () => {
// The whole modularity claim, exercised: define a module, and selection alone
// decides where it shows up.
const leadership = defineModule<{ note: string }>({
id: 'test-leadership',
title: 'Leadership',
appliesTo: ['company', 'capital'],
importance: 50,
load: (entity) =>
entity.kind === 'company' || entity.kind === 'capital' ? { note: 'x' } : null,
Render: () => null,
});
for (const kind of ENTITY_KINDS) {
const entity = sampleOf(kind);
const declared = leadership.applies(entity.kind);
assert.equal(
declared,
kind === 'company' || kind === 'capital',
`${kind}: wrong applicability`,
);
// And it renders only where it has something to say.
assert.equal(leadership.render(entity) !== null, declared, `${kind}: wrong render decision`);
}
});
test('a module with nothing to say renders nothing at all', () => {
// No empty shells: a thin entity should look thin, not broken.
const empty = defineModule<string>({
id: 'test-empty',
title: 'Never shown',
appliesTo: '*',
importance: 10,
load: () => null,
Render: () => null,
});
for (const entity of allEntities().slice(0, 20)) {
assert.equal(empty.render(entity), null, `${entity.id} rendered an empty module`);
}
});
test('every module is styled from one vocabulary', () => {
// Profiles are read one after another, so a module carrying its own CSS family
// reads as a different product. `.research-card-grid` set headings at 1.5rem —
// LARGER than the section heading above them — and `.research-prose` used its
// own margins and line height. Modules use the shared utility classes only.
const dir = new URL('../lib/profile/modules/', import.meta.url);
const offenders: string[] = [];
for (const file of readdirSync(dir)) {
if (!file.endsWith('.tsx') && !file.endsWith('.ts')) continue;
const source = readFileSync(new URL(file, dir), 'utf8');
for (const banned of ['research-card-grid', 'research-prose', 'research-results']) {
if (source.includes(banned)) offenders.push(`${file} uses .${banned}`);
}
}
assert.deepEqual(
offenders,
[],
`Modules must share one styling vocabulary:\n ${offenders.join('\n ')}`,
);
});
test('each migrated page kept every section it had', () => {
// The sections each page hand-wrote before the migration. If a module stops
// applying, or an id is renamed, this is where it surfaces — not in a
// screenshot somebody happens to look at.
const expected: Record<string, string[]> = {
bottleneck: [
'why',
'severity',
'producers',
'rules',
'removes',
'calls',
'funding',
'timeline',
// "Which loops wait on it" was bottleneck-only; `role` supersedes it and
// answers the same question for every kind.
'role',
],
science: ['relieves', 'readiness', 'milestone'],
capital: ['mandate', 'can-move', 'source-sentence'],
company: ['chokepoints', 'events', 'relief', 'same-layer', 'sources'],
};
for (const [kind, ids] of Object.entries(expected)) {
// A kind's sections are conditional on data, so check against the union of
// what its entities can render rather than one sample that may be thin.
const available = new Set(
entitiesOfKind(kind as EntityKind).flatMap((entity) =>
modulesFor(entity).map((module) => module.id),
),
);
for (const id of ids) {
assert.ok(available.has(id), `${kind} lost its "${id}" section`);
}
}
});
test('every kind reads in the same order, so profiles are comparable', () => {
// One importance scale across kinds: identity, judgement, who is involved,
// what governs it, what has happened, connections, discussion.
for (const kind of ENTITY_KINDS) {
const entity = sampleOf(kind);
const ids = modulesFor(entity).map((m) => m.id);
if (ids.includes('timeline') && ids.includes('discussion'))
assert.ok(
ids.indexOf('timeline') < ids.indexOf('discussion'),
`${kind}: timeline after discussion`,
);
if (ids.includes('related') && ids.includes('discussion'))
assert.ok(
ids.indexOf('related') < ids.indexOf('discussion'),
`${kind}: related after discussion`,
);
assert.equal(ids.at(-1), 'discussion', `${kind}: discussion should be last`);
}
});
test('connections never repeat a section the profile already has', () => {
// A bottleneck lists its makers under "Who makes it" and its funders under
// "Who could fund relief". Repeating both as "produced by" and "fundable by"
// turned this module into a second copy of two sections directly above it.
const bottleneck = entitiesOfKind('bottleneck').find(
(e) => e.key === 'grain-oriented-electrical-steel-goes',
);
assert.ok(bottleneck, 'GOES should be in the corpus');
const rels = connectionsFor(bottleneck).map((edge) => edge.rel);
assert.ok(!rels.includes('produced by'), 'producers already have a section');
assert.ok(!rels.includes('fundable by'), 'funders already have a section');
const company = entitiesOfKind('company').find((e) => e.key === 'posco');
assert.ok(company, 'POSCO should be in the corpus');
const companyRels = connectionsFor(company).map((edge) => edge.rel);
assert.ok(!companyRels.includes('makes'), 'what it makes already has a section');
// And it still says the thing nothing else does.
assert.ok(companyRels.includes('located in'), 'jurisdiction is not shown anywhere else');
});
test('every science page says who and what its bottleneck touches', () => {
// Visitor feedback on a science page: which companies would be affected,
// where the research is, and how it joins policy, capital and markets — on
// every such page, not one. So the module is selected by kind, and the
// questions with a stated empty line are always asked, even when the answer
// is "nothing on record yet".
const asked = AROUND_GROUPS.filter((g) => g.empty).map((g) => g.id);
for (const ask of ['companies', 'markets', 'science', 'policy', 'capital']) {
assert.ok(asked.includes(ask), `"${ask}" must always be asked`);
}
for (const science of entitiesOfKind('science')) {
assert.ok(
modulesFor(science).some((m) => m.id === 'around'),
`${science.key}: no around`,
);
for (const { groups } of aroundFor(science)) {
const ids = groups.map((g) => g.id);
for (const ask of asked) assert.ok(ids.includes(ask), `${science.key}: lost "${ask}"`);
}
}
});
test('around reads the same joins as the bottleneck page, never the page itself', () => {
const science = entitiesOfKind('science').find((e) => e.key === 'synthetic-crucible-quartz');
assert.ok(science, 'the crucible entry should be in the corpus');
const [first] = aroundFor(science);
assert.ok(first, 'it relieves a bottleneck');
const makers = first.groups.find((g) => g.id === 'companies')?.rows.map((r) => r.entity.name);
const fromGraph = neighbors('bottleneck', first.bottleneck.key)
.filter((e) => e.rel === 'produced by')
.map((e) => e.to.label);
assert.deepEqual(makers, fromGraph, 'makers differ from the bottleneck graph');
const others = first.groups.find((g) => g.id === 'science')?.rows ?? [];
assert.ok(
!others.some((r) => r.entity.id === science.id),
'a page is not its own "other science"',
);
});
test('a company reads in the order the owner asked for, and every module sits where the list says', () => {
// 2026-10-23: header → at a glance → what it holds → ownership → numbers →
// what has happened → leads → what could loosen its grip → ways to take
// part → hiring → others at the same step → what it connects to → sources
// → discussion. The page's own sections (leads, take-part, hiring) take
// their place from the same list.
const gev = entitiesOfKind('company').find((e) => e.key === 'ge-vernova');
assert.ok(gev, 'GE Vernova should be in the corpus');
const modules = modulesFor(gev);
for (const m of modules) {
const pinned = (COMPANY_SECTION_IMPORTANCE as Record<string, number>)[m.id];
if (pinned !== undefined) assert.equal(m.importance, pinned, `${m.id} drifted from the list`);
}
const rendered = modules.filter((m) => m.render(gev) !== null).map((m) => m.id);
for (const id of [
'chokepoints',
'ownership',
'numbers',
'events',
'relief',
'same-layer',
'sources',
'discussion',
])
assert.ok(rendered.includes(id), `GE Vernova renders no "${id}"`);
const inOrder = COMPANY_SECTION_ORDER.filter((id) => rendered.includes(id));
assert.deepEqual(
rendered.filter((id) => (COMPANY_SECTION_ORDER as readonly string[]).includes(id)),
inOrder,
'company sections are out of the reading order',
);
assert.ok(
rendered.indexOf('related') < rendered.indexOf('sources'),
'sources come after connections',
);
});
test('ownership says so for a private firm and a subsidiary, and stays silent for a listed firm with no rows', () => {
const ids = (key: string) => {
const e = entitiesOfKind('company').find((x) => x.key === key);
assert.ok(e, key);
return modulesFor(e)
.filter((m) => m.render(e) !== null)
.map((m) => m.id);
};
assert.ok(ids('sibelco').includes('ownership'), 'a private firm says who owns it');
assert.ok(ids('kuka').includes('ownership'), 'a subsidiary says whose it is');
assert.ok(!ids('nikon').includes('ownership'), 'no rows, no section');
});