1
0
This repository has been archived on 2026-08-25. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
tera/src/test/unify.test.ts
T
karti 285b5b19e7 fix: the merged board's live feeds, and two instruments it could not be judged without
An ultracode investigation mapped the single-board work across five parallel
readers and three adversarial reviewers. It found things this session would have
walked into, and two of them are fixed here.

**THE MERGED BOARD WAS SHIPPING WITH THREE FEEDS SILENTLY OFF.** Live ADS-B and
live weather were gated on `id !== "california"` — "not the coarse statewide
board", correct the day it was written, since live aircraft over a board at
1,919 m to the unit are a glyph problem and one station cannot speak for a
thousand kilometres of coast. `cities/unify.ts` then built a board that is the
whole state AND metro-detailed, keeping the `california` id deliberately so the
fire gate, the ladder's region table and every `?city=` deep link keep working.
It inherited a gate meant for something else. Measured before the fix: the
`#source` badge read `""` on the merged board and `live weather · live traffic`
on the Bay Area's. A defect that reads as "it feels less alive" and never as an
error.

`carriesMetroDetail(city)` asks the pack instead: `focusRegions` is the honest
predicate and needs no new field, because the coarse state pack declares none
and every pack with ground worth drawing at metro resolution declares one.

**AND ASKING WAS NOT ENOUGH, BECAUSE THE FEEDS ARE PER-METRO.** With the gate
fixed the board asked — and was refused: `GET /flights?lat=37.30&lng=-119.25&
radiusNm=402 → 400 bad_request, "Nothing this deployment serves is near
37.3,-119.25"`. Correctly: `regionOf` derives its circle from the board's bounds,
which on a statewide board is 402 nautical miles centred on the middle of the
state, and what the deployment serves is San Francisco and the Southland, five
hundred and sixty kilometres apart.

`mergedTraffic` asks for both. In `adapters/http.ts` and not `engine/flights.ts`
for the reason `TrafficSource` itself lives there: the engine draws darts at
coordinates and has no business with provenance, and every interesting part of
this merge is provenance. De-duplicated by id, because overlapping circles both
see the aircraft between them and `flights.ts` measures a track's span from
repeated observations — a duplicate is not merely a double image. `live()` is
`some` and not `every`, so one dark metro does not make the other's observed
traffic claim to be simulated. Verified: `?lat=37.77` → 200 live, 24 aircraft;
`?lat=33.82` → 200 live, 15 aircraft; `#source` now reads `live traffic` on the
merged board and stays `""` on the coarse one.

**TWO INSTRUMENTS, BOTH BECAUSE THIS SESSION KEPT FAILING WITHOUT THEM.**

`scripts/performance-budget.mjs` gains a `california-one` cell. Until now the
only way to measure the merged board was to hand-edit the `california` cell's
query, run, and edit it back — done six times in one session, which is exactly
the procedure that gets half-done. Its `ready` asserts `#sea-section`, not just
the signature chapter: `california-overview` is on the coarse board too, so a
cell whose `?one=1` quietly stopped working would measure the coarse board and
pass. No ports, no section, no readiness. Caps are RECORDED from its first run
with headroom, in the same spirit as bay-area and socal — they were briefly
copied from `california` and that is wrong for the same reason that cell's
numbers are wrong for this board. **No existing cap was raised.**

`scripts/look.mjs` gains `--lat/--lng/--standoff/--height`. Every aim in this
harness goes through a control a reader also uses, which is right and stays the
default — but it means a board can only be photographed where a chapter already
points, and the merged board carries the state pack's six: the whole state, the
north, two corridors, two doors. None is near a city. The board exists to put
cities on the state and there was no way to photograph one; three attempts by
clicking the minimap and guessing wheel notches landed in open ocean twice and
on empty coast once. The seek moves the camera and nothing else.

**A regression the new cell caught within one run.** The first version of the
marker fix gated on bounds alone, like the office doors. The coarse state
board's rectangle contains San Francisco, so it picked up forty-four company
markers it has no business drawing at 1,919 m to the unit: 373 draw calls → 417.
Now gated on `carriesMetroDetail` as well, and `california` measures 372,415
triangles / 373 draws — identical to before this commit.

All twelve budget cells pass. 1,705 tests pass.

Recorded for the next round, from the review: **SoCal's two focus rectangles
overlap by 1.7 x 4.6 km** (verified: lat 34.075–34.090, lng −118.300–−118.250),
so any per-rectangle terrain tier must clip them to a disjoint cover first or it
draws that ground twice. Today's per-axis lattice is what hides it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-24 04:16:33 -07:00

244 lines
11 KiB
TypeScript

/**
* One California, as one pack.
*
* These assert the three things about `unifiedCalifornia` that a picture cannot
* check and that a careless edit would silently undo: that the state stops
* drawing what a metro now draws, that every metro lot is marked as detail so
* the level of detail has something to act on, and that the lattice this all
* rides on is the size the file claims it is. The last one is the whole reason
* the merge is affordable, and it is one multiplication away from not being.
*/
import assert from "node:assert/strict";
import { describe, it } from "node:test";
import {
UNIFIED_COARSE_FACTOR,
UNIFIED_FINE_METRES,
unifiedCalifornia,
} from "../cities/unify.ts";
import CALIFORNIA from "../cities/california.ts";
import SAN_FRANCISCO from "../cities/sf.ts";
import SOCAL from "../cities/socal.ts";
const { city, report } = unifiedCalifornia();
/** `buildAxis`, restated — the lattice cost is the claim being checked. */
function axisLength(min: number, max: number, fine: number, coarse: number, bands: number[][]): number {
let n = 1;
let x = min;
const inFine = (v: number) => bands.some(([a, b]) => v >= (a as number) - coarse && v <= (b as number) + coarse);
while (x < max) {
x += inFine(x) ? fine : coarse;
n += 1;
}
return n;
}
describe("one California", () => {
it("is still California, by id, because everything downstream keys on that", () => {
// The fire gate, the ladder's region table, `?city=`, and the budget
// harness's `data-board` assertion all name "california" already. A fourth
// board id would mean teaching every one of them about a board that is the
// same place.
assert.equal(city.id, CALIFORNIA.id);
assert.equal(city.bounds.minLat, CALIFORNIA.bounds.minLat);
assert.equal(city.bounds.maxLat, CALIFORNIA.bounds.maxLat);
});
it("stops drawing the state's coarse stand-ins where a metro now stands", () => {
/*
* Hills *sum*: `World.elevationAt` blends every hill covering a point, so a
* coarse state hill left under a metro's fine ones lifts the whole Bay. And
* the state's districts include coarse stand-ins for both metros, which
* drawn together with the real ones is two cities in one place.
*/
assert.ok(report.hills.dropped > 0, "no state hill was cleared for a metro");
assert.ok(report.districts.dropped > 0, "no state district was cleared for a metro");
const metroBounds = [SAN_FRANCISCO.bounds, SOCAL.bounds];
const inside = (lat: number, lng: number) =>
metroBounds.some((b) => lat >= b.minLat && lat <= b.maxLat && lng >= b.minLng && lng <= b.maxLng);
/*
* By **identity**, not by name, and that is a finding rather than a style
* choice: six hills are named in both the state pack and a metro pack —
* Marin Headlands, San Bruno Mountain, Mount Diablo, Bolinas Ridge, Mount
* Gleason, Mount Wilson — because two packs independently named the same
* real hill. Keyed on the string, San Francisco's own Marin Headlands reads
* as a state hill that survived inside a metro and fails this test for a
* bug that is not there. `ladder.ts` hit the same thing with "Whole Board"
* on two boards; the rule both times is that a name is not an identity.
*
* The merge spreads the arrays without copying their elements, so the state
* pack's own hill objects are the ones to look for.
*/
const stateHills = new Set<unknown>(CALIFORNIA.hills);
let checked = 0;
for (const hill of city.hills) {
if (!stateHills.has(hill)) continue;
checked += 1;
assert.ok(!inside(hill.lat, hill.lng), `state hill "${hill.name}" is still inside a metro`);
}
assert.ok(checked > 0, "no state hill survived at all, so this asserted nothing");
});
it("marks every metro lot as detail, which is what the LOD acts on", () => {
const detail = city.districts.filter((d) => d.detail === true);
const base = city.districts.filter((d) => d.detail !== true);
assert.equal(detail.length, SAN_FRANCISCO.districts.length + SOCAL.districts.length);
assert.equal(base.length, report.districts.state - report.districts.dropped);
// And the state's own districts are never marked, or the board would hide
// California when you stood back from it.
assert.ok(base.length > 0, "the state kept no districts of its own");
});
it("keeps the lattice the size this whole merge depends on", () => {
const cf = city.coarseFactor ?? 1;
const lat = axisLength(
city.bounds.minLat, city.bounds.maxLat, city.cellLat, city.cellLat * cf,
(city.focusRegions ?? []).map((r) => [r.minLat, r.maxLat]),
);
const lng = axisLength(
city.bounds.minLng, city.bounds.maxLng, city.cellLng, city.cellLng * cf,
(city.focusRegions ?? []).map((r) => [r.minLng, r.maxLng]),
);
const points = lat * lng;
/*
* The ceiling, not a description. At 150 m / x8 this was 1.23M points and
* the board rendered 1,114,226 triangles against a 440,000 cap — terrain,
* not buildings. Anything that pushes this back over half a million points
* is re-opening that, and should be measured rather than assumed.
*/
assert.ok(points < 500_000, `${points} lattice points — the terrain budget was found at ~180k`);
assert.equal(UNIFIED_COARSE_FACTOR, cf);
assert.ok(Math.abs(city.cellLat * 111_320 - UNIFIED_FINE_METRES) < 1);
});
it("carries both metros' ports, which the state board never had", () => {
// The most visible single consequence of the merge: the state board now has
// a harbour on it, and `main.ts` attaches the vessel layer to any pack with
// one. Before this, `sf.ts` and `california.ts` had zero berths between them.
assert.ok((city.ports?.length ?? 0) >= (SOCAL.ports?.length ?? 0));
assert.ok((city.ports?.length ?? 0) > (CALIFORNIA.ports?.length ?? 0));
});
it("is memoised, because World builds one per mount", () => {
assert.equal(unifiedCalifornia().city, city);
});
});
describe("one California's chapters", () => {
it("is the state's list, until the corridor is drawn at true width", () => {
/*
* The metro rungs were merged, photographed and reverted; `unify.ts` records
* why in full. The short version is that the conversion was right and the
* frame was not — a two-unit stand-off on a 551-unit board lands inside the
* corridor's 4.7 km-wide atlas glyph. This test exists so that the day
* somebody merges them again, they do it deliberately.
*/
assert.deepEqual(
city.chapters.map((c) => c.id),
CALIFORNIA.chapters.map((c) => c.id),
);
});
});
describe("one California's cities are lotted like cities", () => {
it("measures a detail district's lot in metres, not in scene units", () => {
/*
* The defect this guards: `LOT` is fixed in scene units, which makes a lot
* 40 m on San Francisco's board and **806 m** on the statewide one. Put
* San Francisco's fifty-two districts on the statewide board unchanged and
* they are lotted at 806 m — and lots go as the inverse square of their
* size, so the Bay Area's ~84,000 buildings become about two hundred and a
* city renders as a handful of grey slabs.
*
* Measured in the browser after the fix: the merged board carries 59,166
* building instances, of which 2,839 are the state's own and 56,327 are the
* two metros'. Before it, the metros contributed roughly two hundred.
*
* This asserts the property that produces that, rather than the count,
* because the count moves with any pack edit and the property does not.
*/
const detail = city.districts.filter((d) => d.detail === true);
assert.ok(detail.length > 0);
// The state's own districts must NOT be marked, or they would be re-lotted
// at 160 m across the whole of California and the board would never build.
const base = city.districts.filter((d) => d.detail !== true);
assert.ok(base.length > 0);
for (const d of base) assert.equal(d.detail, undefined);
});
it("keeps the metros' own boards untouched, which is the point of the flag", () => {
// `unify` copies districts before marking them, so nothing it does can
// reach back into `sf.ts` or `socal.ts` and re-lot the dedicated boards.
for (const d of SAN_FRANCISCO.districts) assert.equal(d.detail, undefined);
for (const d of SOCAL.districts) assert.equal(d.detail, undefined);
});
});
describe("the detail repack", () => {
it("distinguishes 'nothing packed yet' from 'nothing visible'", async () => {
/*
* A regression test for a bug this file's own author wrote and the budget
* caught. `DetailStore.key` remembers which districts the instance buffers
* currently hold, and it used to start as `""` — the same key an *empty*
* visible set produces. So the first call, at the whole-board pose where
* nothing should draw, compared equal to the initial state, took the early
* return, and left `InstancedMesh.count` at the constructor's value, which
* is every instance. Measured: 911,541 triangles against a 440,000 cap,
* with the code meant to prevent it running and doing nothing.
*
* The distinction is the whole fix, so it is what is asserted — on the
* source, because building a real `InstancedMesh` here would need a GPU.
*/
const source = await import("node:fs/promises").then((fs) =>
fs.readFile("src/engine/blocks.ts", "utf8"),
);
assert.ok(
/key: string \| null;/.test(source),
"DetailStore.key must admit a value that no visible set can produce",
);
assert.ok(
/key: null,/.test(source),
"the store must start at that value, not at an empty key",
);
assert.ok(
!/key: "",/.test(source),
'an empty-string initial key is the bug: it equals the key of an empty visible set',
);
});
});
describe("one California is a detailed board, and the engine must agree", () => {
it("declares focus regions, which is what the live-feed gates read", async () => {
/*
* Live ADS-B and live weather were gated on `id !== "california"` — "not the
* coarse statewide board", correct on the day it was written. `unify` then
* built a board that is the whole state AND metro-detailed, keeping the
* `california` id on purpose so the fire gate, the ladder's region table and
* every `?city=` deep link keep working. It inherited a gate meant for
* something else and shipped with live traffic and live weather silently
* off — a defect that reads as "it feels less alive" and never as an error.
*
* The predicate is now `focusRegions`, so this asserts the property the
* gates depend on, and that the coarse pack still fails it.
*/
assert.ok((city.focusRegions?.length ?? 0) > 0, "the merged board must declare focus regions");
assert.equal(CALIFORNIA.focusRegions ?? undefined, undefined);
const main = await import("node:fs/promises").then((fs) =>
fs.readFile("src/main.ts", "utf8"),
);
assert.ok(
!/id !== "california" && access\.can\.liveEnvironment/.test(main),
"a live feed is still gated on the board id rather than on the pack",
);
assert.ok(
!/const initialMarkers = id === "sf"/.test(main),
"markers are still gated on the board id rather than on the board's bounds",
);
});
});