mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
The renderer contract is what the application asks of a 3D renderer. It is nine ports (frame, globe, draw, camera, query, input, effects, credits, quality) plus the renderer itself, one strict .d.ts per port in src/render/contract/. PORTS lists every member at run time, and a TypeScript-compiler test fails when the list and the types disagree on one member. An optional member exists only with its capability; a required one answers null or empty without it. Nothing in the application uses the contract yet. The stub renderer (src/render/stub/) implements every port in memory and draws nothing. Its camera is a port of the default camera's maths: views, lookAt, flights with their height arc and easing, rectangle framing on the geodesic midpoint, the view rectangle with its horizon and pole cases, changed thresholds, follows, and flights that finish inside the call. The conformance suite (src/render/conformance/) runs 59 checks against a renderer through the contract alone, in Node or in a browser page. The camera checks compare traces recorded from Cesium's own Camera, headless on a virtual clock (scripts/record-camera-goldens.mjs), to 1 mm, 1e-9 and 1e-3 px. The other checks cover listener rules on every subscription, the frame loop, draw sets, picks, motion clocks, heights, leases, imagery, styles, credits and quality knobs. The stub passes all of them but the model check, which needs a glTF asset. Reference scenes draw one scene per primitive family, a city and a calibration path from orbit to the street, from contract calls only and with no network. /dev/conformance/ draws them on the Cesium scene renderer (src/render/cesium/sceneRenderer.js), records references through a dev-server-only, loopback-only endpoint, and compares captures with pixelmatch's antialias detection plus a solid-pixel count. Measured on Cesium: every MSAA setting passes, and a dropped 4 px point, a dropped 2 px line and a 1 px shift of the view fail. visual.js states the limits. The package exports ./render/contract, ./render/stub and ./render/conformance. Documented in docs/RENDERER-CONTRACT.md, docs/CODE-BOUNDARIES.md and docs/CURRENT-STATE.md.
169 lines
5.6 KiB
JavaScript
169 lines
5.6 KiB
JavaScript
import { test } from 'node:test';
|
||
import assert from 'node:assert/strict';
|
||
import path from 'node:path';
|
||
import { fileURLToPath } from 'node:url';
|
||
import ts from 'typescript';
|
||
import {
|
||
CAPABILITIES,
|
||
CONTRACT_VERSION,
|
||
FRAME_STEPS,
|
||
PORTS,
|
||
QUALITY_KEYS,
|
||
checkPortShape,
|
||
checkRendererShape,
|
||
} from './index.js';
|
||
|
||
const DIR = path.dirname(fileURLToPath(import.meta.url));
|
||
const INDEX = path.join(DIR, 'index.d.ts');
|
||
|
||
const program = ts.createProgram([INDEX], {
|
||
strict: true,
|
||
noEmit: true,
|
||
target: ts.ScriptTarget.ES2022,
|
||
lib: ['lib.es2022.d.ts', 'lib.dom.d.ts'],
|
||
skipLibCheck: false,
|
||
types: [],
|
||
});
|
||
const checker = program.getTypeChecker();
|
||
const contractFiles = program
|
||
.getSourceFiles()
|
||
.filter((file) => path.dirname(file.fileName) === DIR);
|
||
|
||
/** The named interface or type alias declared in the contract files. */
|
||
function declared(name) {
|
||
for (const file of contractFiles)
|
||
for (const statement of file.statements)
|
||
if (
|
||
(ts.isInterfaceDeclaration(statement) ||
|
||
ts.isTypeAliasDeclaration(statement)) &&
|
||
statement.name.text === name
|
||
)
|
||
return statement;
|
||
throw new Error(`${name} is not declared in the contract`);
|
||
}
|
||
|
||
const PORT_INTERFACES = {
|
||
renderer: 'Renderer',
|
||
frame: 'FramePort',
|
||
globe: 'GlobePort',
|
||
draw: 'DrawPort',
|
||
camera: 'CameraPort',
|
||
query: 'QueryPort',
|
||
input: 'InputPort',
|
||
effects: 'EffectsPort',
|
||
credits: 'CreditsPort',
|
||
quality: 'QualityPort',
|
||
drawSet: 'DrawSet',
|
||
imageryLayer: 'ImageryLayer',
|
||
flight: 'Flight',
|
||
lease: 'Lease',
|
||
};
|
||
const PORT_NAMES = new Set(Object.values(PORT_INTERFACES));
|
||
|
||
/** Whether a property's type is one of the contract's own object shapes to list member by member. */
|
||
function nestedShape(type) {
|
||
if (type.isUnion() || type.getCallSignatures().length) return null;
|
||
const symbol = type.aliasSymbol ?? type.getSymbol();
|
||
const declaration = symbol?.declarations?.[0];
|
||
if (!declaration) return null;
|
||
const inContract = path.dirname(declaration.getSourceFile().fileName) === DIR;
|
||
if (!inContract) return null;
|
||
if (ts.isTypeLiteralNode(declaration)) return type;
|
||
if (ts.isInterfaceDeclaration(declaration))
|
||
return PORT_NAMES.has(declaration.name.text) ? null : type;
|
||
return null;
|
||
}
|
||
|
||
/** `[member, optional]` for every member, nested shapes dotted. */
|
||
function members(type, prefix = '') {
|
||
const out = [];
|
||
for (const property of checker.getPropertiesOfType(type)) {
|
||
const name = `${prefix}${property.name}`;
|
||
const optional = Boolean(property.flags & ts.SymbolFlags.Optional);
|
||
out.push([name, optional]);
|
||
const declaration = property.valueDeclaration ?? property.declarations?.[0];
|
||
const propertyType = checker.getTypeOfSymbolAtLocation(
|
||
property,
|
||
declaration,
|
||
);
|
||
const nested = nestedShape(propertyType);
|
||
if (nested) out.push(...members(nested, `${name}.`));
|
||
}
|
||
return out;
|
||
}
|
||
|
||
function unionLiterals(name) {
|
||
const type = checker.getTypeAtLocation(declared(name).name);
|
||
return (type.isUnion() ? type.types : [type]).map((t) => t.value).sort();
|
||
}
|
||
|
||
test('the contract type-checks under strict TypeScript', () => {
|
||
const diagnostics = ts.getPreEmitDiagnostics(program).map((d) => {
|
||
const where = d.file
|
||
? `${path.relative(DIR, d.file.fileName)}:${d.file.getLineAndCharacterOfPosition(d.start).line + 1}`
|
||
: '';
|
||
return `${where} ${ts.flattenDiagnosticMessageText(d.messageText, '\n')}`;
|
||
});
|
||
assert.deepEqual(diagnostics, []);
|
||
});
|
||
|
||
test('PORTS lists every member of every port, required exactly when the type requires it', () => {
|
||
for (const [port, interfaceName] of Object.entries(PORT_INTERFACES)) {
|
||
const type = checker.getTypeAtLocation(declared(interfaceName).name);
|
||
const typed = Object.fromEntries(members(type));
|
||
const described = PORTS[port];
|
||
assert.deepEqual(
|
||
Object.keys(described).sort(),
|
||
Object.keys(typed).sort(),
|
||
`${port} members`,
|
||
);
|
||
for (const [member, capability] of Object.entries(described))
|
||
assert.equal(
|
||
capability !== null,
|
||
typed[member],
|
||
`${port}.${member}: optional in the type exactly when a capability adds it`,
|
||
);
|
||
}
|
||
});
|
||
|
||
test('the capability and frame-step lists are the types’ own', () => {
|
||
assert.deepEqual([...CAPABILITIES].sort(), unionLiterals('Capability'));
|
||
assert.deepEqual([...FRAME_STEPS].sort(), unionLiterals('FrameStep'));
|
||
for (const port of Object.values(PORTS))
|
||
for (const capability of Object.values(port))
|
||
if (capability !== null) assert.ok(CAPABILITIES.includes(capability));
|
||
assert.equal(CONTRACT_VERSION, '0');
|
||
const settings = checker.getTypeAtLocation(declared('QualitySettings').name);
|
||
assert.deepEqual(
|
||
[...QUALITY_KEYS].sort(),
|
||
checker
|
||
.getPropertiesOfType(settings)
|
||
.map((p) => p.name)
|
||
.sort(),
|
||
);
|
||
});
|
||
|
||
test('the shape check names what is missing and what a capability does not allow', () => {
|
||
assert.deepEqual(checkRendererShape(null), ['renderer is not an object']);
|
||
assert.deepEqual(checkRendererShape({}), [
|
||
'renderer.capabilities is not a Set',
|
||
]);
|
||
const problems = checkRendererShape({
|
||
capabilities: new Set(['warp']),
|
||
contractVersion: '0',
|
||
});
|
||
assert.ok(problems.includes('unknown capability warp'));
|
||
assert.ok(problems.includes('renderer.frame is missing'));
|
||
const set = Object.fromEntries(
|
||
Object.keys(PORTS.drawSet).map((member) => [member, () => {}]),
|
||
);
|
||
assert.deepEqual(checkPortShape(set, 'drawSet', new Set()), [
|
||
'drawSet.setColumns is present without columns',
|
||
]);
|
||
delete set.setColumns;
|
||
assert.deepEqual(checkPortShape(set, 'drawSet', new Set(['columns'])), [
|
||
'drawSet.setColumns is missing with columns',
|
||
]);
|
||
assert.deepEqual(checkPortShape(set, 'drawSet', new Set()), []);
|
||
});
|