lawsynth/apps/playground/tests/integrator.test.ts
caelum0x 8c4bee8e2f feat(product): export/templates/validate CLI, HTTP product endpoints, web onboarding
Wave 2 product depth:
CLI — export (world -> runnable Python/NumPy module + LaTeX + JSON; the exported
Python reproduces the engine to ~11 digits), new/templates (onboarding catalog:
lorenz, lotka-volterra, pendulum, van-der-pol, sir with synthetic-data
generation), validate (holdout forecast-skill diagnostic: RMSE/MAE/R2/skill vs
persistence, honest STRONG/FAIR verdicts).

Services — /v1/worlds explain, forecast, report (HTML), compare over HTTP,
backed by the real domain + native engine, honest 503/404/422 boundaries;
reuses the SDK HTML report generator. 98 -> 120 API tests.

Web — docs-site getting-started flow + 5-system examples gallery + concepts;
playground interactive pick-world -> tune-params -> simulate via a local
Euler integrator over world-schema. Both apps typecheck clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-08-17 21:19:13 +03:00

28 lines
1.4 KiB
TypeScript

import { forwardEuler } from "../src/integrator.js";
import { dampedOscillatorWorld, lotkaVolterraWorld, SEED_INITIAL_STATE } from "../src/seed_worlds.js";
import { deepEqual, equal, ok, test, throws } from "./testkit.js";
await test("forwardEuler decays exponentially for a first-order world", () => {
const trajectory = forwardEuler(dampedOscillatorWorld, {
start: 0, end: 4, step: 0.01, initial: SEED_INITIAL_STATE["damped-oscillator"]!,
});
deepEqual([...trajectory.variables], ["x", "v"]);
equal(trajectory.times[0], 0);
equal(trajectory.values[0]![0], 1);
// With ζ=0.15, ω=2 the amplitude must decay below its initial displacement.
const finalX = trajectory.values.at(-1)![0]!;
ok(Math.abs(finalX) < 1, "oscillator amplitude should decay");
});
await test("forwardEuler produces bounded predator-prey cycles", () => {
const trajectory = forwardEuler(lotkaVolterraWorld, {
start: 0, end: 10, step: 0.005, initial: SEED_INITIAL_STATE["lotka-volterra"]!,
});
ok(trajectory.values.every((row) => row.every((value) => Number.isFinite(value) && value > 0)), "populations stay finite and positive");
});
await test("forwardEuler rejects a world without continuous laws", () => {
const empty = { formatVersion: "0.1.0", id: "empty", time: { kind: "continuous" }, variables: [{ id: "x", role: "state" }], laws: [] } as never;
throws(() => forwardEuler(empty, { start: 0, end: 1, step: 0.1, initial: {} }), /no integrable continuous laws/);
});