Files
substrata/test/relations.test.ts
CatoandClaude Opus 5 375a2448f8 The web as a schema, and the seams for eight languages (#61)
* feat(relations): the web becomes a schema, and every join says how well it is evidenced

Substrata's claim is that these constraints are connected — a firm makes a
material, a rule governs it, a mandate could fund relief. The joins are the
product, so they now get the same discipline as the rows.

Relations were free strings built ad hoc per kind, and that cost three things.

`makes` and `produced by` were the same fact declared twice, in two directions,
with nothing making them agree. A relation is declared once now and read from
both ends, so they cannot drift.

A policy instrument was emitted with `kind: 'capital'`, so rules rendered as
capital on a country dossier. Instruments are policy entities, and a test holds
that.

And no edge carried evidence at all, so an unverified lead and a sourced finding
rendered as the same line — the one thing this site exists to keep apart. Every
relation now states how well THE JOIN is stood up, which is not how well either
end is: a sourced company and a sourced material can still be connected by
nothing but a directory guess, and it says so.

335 relations across seven kinds. `governed-by` is new: which rules govern a
bottleneck was not in the graph at all.

`lib/graph.ts` keeps its shape and becomes an adapter, so /api/graph, the
country dossier, chat retrieval and the profile module inherit the fixes. Labels
come from the entity registry, so a country is its name rather than its ISO
code, and the graph route stops carrying a hand-copied kind list that had
already fallen a kind behind.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Y6ecRd91ZsPPcjuNkKTFV2

* feat(i18n): open the seams for eight languages, without faking a single translation

The expensive part of shipping in eight languages is not translating. It is
discovering afterwards that the code assumed one — strings welded into
components, dates built by hand, a layout that only goes left to right. Those
assumptions are cheap to remove now and painful to remove at scale, so they are
removed now. No translation is invented in the process.

The content divides into three kinds that cannot share a mechanism.

**Interface strings** are keyed, in `lib/i18n/messages.ts`. English is the
source and defines the key set, so a missing key is a type error rather than a
blank on a page. Every other locale is deliberately partial: a missing string
falls back to English, and `gapsIn()` and `coverageOf()` report exactly what
fell back. A fallback nobody can measure is how a half-translated product ships
without anyone finding out. All twenty profile-module titles and the evidence
vocabulary are migrated, and a test fails the build if a module writes a title
into the component — a string welded into a component is one no translator can
reach. Proved by mutation.

**Research prose** — about 5,500 words — is Substrata's own writing, so it is
translatable, and a translation is a claim like any other: it carries who made
it and when. `readProse()` reports whether the text is really in the language
asked for. A machine translation may exist as a draft and may never count as
checked, because prose that reads fluently but was never verified is
indistinguishable, to a reader, from prose that was.

**Quotations** — about 1,000 words — are never translated. The entire
evidentiary value of a quote is that these are the words the source published;
a translated mandate sentence presented as the EIB's own would be invented
evidence. `Quotation` has no translations field, so the type enforces it rather
than a convention.

Also: `lang` and `dir` on the document, so Arabic flips the layout and not just
the text, and locale-aware date and number formatting, because the corpus stores
ISO dates for storage reasons and no reader wants to read one.

Not done, and deliberately not faked: no translations exist, every non-English
locale reports 0%, and there is no /de/ routing — that is cheap to add once
strings are external and pointless before translations exist, since it would
ship seven locales silently serving English. docs/LOCALIZATION.md records the
rules and what is left.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Y6ecRd91ZsPPcjuNkKTFV2

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-17 06:07:52 -07:00

108 lines
4.8 KiB
TypeScript

/**
* The joins are the product, so their invariants are build gates.
*
* Substrata's claim is that these constraints are connected — a firm makes a
* material, a rule slows the firm, a mandate could fund relief. A web is only
* worth reading if every strand resolves, says which direction it is read in,
* and admits how well it is evidenced.
*/
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { allEntities, resolveEntity } from '../lib/entities/registry';
import { allRelations, connectionsOf } from '../lib/relations/registry';
import { RELATION_KINDS, RELATION_LABEL } from '../lib/relations/types';
import { neighbors } from '../lib/graph';
test('every relation joins two entities that exist', () => {
// A dangling join is worse than a missing one: it renders as a fact and
// leads nowhere.
const broken: string[] = [];
for (const relation of allRelations()) {
if (!resolveEntity(relation.from))
broken.push(`${relation.kind}: no such entity ${relation.from}`);
if (!resolveEntity(relation.to)) broken.push(`${relation.kind}: no such entity ${relation.to}`);
}
assert.deepEqual(broken, [], `Dangling relations:\n ${broken.join('\n ')}`);
});
test('every relation says how well the join itself is evidenced', () => {
// Not how well either END is: a sourced company and a sourced material can
// still be joined by nothing but a directory guess.
for (const relation of allRelations()) {
assert.ok(
relation.evidence.trim().length > 0,
`${relation.kind} ${relation.from} → ${relation.to} claims nothing about its evidence`,
);
}
});
test('a sourced producer row makes a sourced join, and an unverified one does not', () => {
const produces = allRelations().filter((r) => r.kind === 'produces');
assert.ok(produces.length > 0, 'no producer joins');
const sourced = produces.filter((r) => r.evidence === 'sourced');
assert.ok(sourced.length > 0, 'no sourced producer joins');
// Every sourced join carries the source it is sourced to.
for (const relation of sourced) {
assert.ok(
relation.sources.length > 0,
`${relation.from} → ${relation.to} says sourced with no source`,
);
}
// And the unverified ones are still present, distinguishable rather than hidden.
assert.ok(
produces.some((r) => r.evidence !== 'sourced'),
'unverified producer rows should still appear, labelled',
);
});
test('a relation is declared once and reads correctly from both ends', () => {
// `makes` and `produced by` were separate declarations before, so they could
// disagree. They are one fact now.
const sample = allRelations().find((r) => r.kind === 'produces');
assert.ok(sample, 'no producer relation to check');
const forward = connectionsOf(sample.from).find((c) => c.other === sample.to);
const inverse = connectionsOf(sample.to).find((c) => c.other === sample.from);
assert.ok(forward && inverse, 'the relation is not visible from both ends');
assert.equal(forward.label, RELATION_LABEL.produces.forward);
assert.equal(inverse.label, RELATION_LABEL.produces.inverse);
assert.equal(forward.evidence, inverse.evidence, 'the same join told two evidence stories');
});
test('every relation kind has both labels, and each is used by nothing else', () => {
const labels = new Set<string>();
for (const kind of RELATION_KINDS) {
const { forward, inverse } = RELATION_LABEL[kind];
for (const label of [forward, inverse]) {
assert.ok(label.trim().length > 0, `${kind} is missing a direction label`);
assert.ok(!labels.has(label), `"${label}" is used by more than one relation kind`);
labels.add(label);
}
}
});
test('a policy instrument is a policy node, not a capital one', () => {
// The old graph emitted instruments with `kind: 'capital'`, so rules rendered
// as capital on a country dossier.
const policyJoins = allRelations().filter((r) => r.kind === 'in-force-in');
assert.ok(policyJoins.length > 0, 'no policy jurisdiction joins');
for (const relation of policyJoins) {
assert.ok(relation.from.startsWith('policy:'), `${relation.from} should be a policy entity`);
assert.ok(relation.to.startsWith('country:'), `${relation.to} should be a country entity`);
}
});
test('the graph adapter resolves labels from the registry', () => {
// A country used to render as its ISO code because the graph built its own
// labels instead of asking the registry.
const company = allEntities().find((e) => e.kind === 'company' && e.key === 'posco');
assert.ok(company, 'POSCO should be in the corpus');
const edges = neighbors('company', company.key);
assert.ok(edges.length > 0, 'POSCO should have edges');
for (const edge of edges) {
assert.notEqual(edge.to.label, edge.to.id.toUpperCase(), 'label looks like a raw code');
assert.ok(edge.evidence.length > 0, 'graph edge lost its evidence');
}
});