feat: show what each backup is doing as a light that moves like a rocket
Replace the single-shot meteor with a per-orbit activity system. A backup's state is read from how a small glowing triangle moves, not from a drawing of a rocket. - Events come from status changes between polls, so scheduled backups animate too. A click shows its own verdict until a poll that began after it has landed, which keeps one backup from being animated twice or flipping through a stale status. Loading the page only sets a baseline and never replays history. - Queued waits on its star. Running circles it, climbing and spinning up. - Success waits for a launch window, where the light's own motion already carries it round the black hole, then departs with that velocity. It falls under a pseudo-Newtonian pull after a burn that trims angular momentum to just under the capture limit, so the path spirals with the orbits, always arrives, and never slows on the way in. Near the shadow it reddens, dims and stretches, and the disk spins up as it feeds. - Failure explodes in place or, when the hub says the transfer failed, launches, loses power and falls back. The star keeps its busy look until the outcome and then turns red. Rehearsal is a faint dashed light that lands on its star. - Position and velocity are integrated and a mode change only swaps forces, so every transition is continuous and interruptible. Parameters use the shared spring tokens. Trails follow the path actually flown. - At most ten lights are drawn; reduced motion draws none and shows outcomes at once. - Pure modules with tests for activity, physics and trails. The Playwright capture script now checks lights by data-orbit. The animation guideline in docs/UI.md describes the simulation approach in place of the old single progress value. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -132,7 +132,8 @@ try {
|
||||
|
||||
await page.getByRole("button", { name: "Capture Load test orbit 1", exact: true }).click();
|
||||
await page.getByText("Snapshot secured from Load test orbit 1.", { exact: true }).waitFor();
|
||||
await page.waitForFunction(() => Number(document.querySelector(".capture-arrival").getAttribute("opacity")) > 0.1);
|
||||
// The clicked orbit's rocket is on screen (rockets are marked with data-orbit; other orbits may be running too).
|
||||
await page.waitForFunction(() => Number(document.querySelector('.rocket[data-orbit="orbit-0"]')?.getAttribute("opacity")) > 0.1);
|
||||
await page.waitForFunction(() => Number(
|
||||
document.querySelector("[data-vault-core]").style.getPropertyValue("--capture-absorption"),
|
||||
) > 0.2);
|
||||
@@ -144,8 +145,10 @@ try {
|
||||
await page.locator("#register-orbit-1").click();
|
||||
await page.getByRole("button", { name: "Capture Load test orbit 2", exact: true }).click();
|
||||
await page.getByText("Capture failed for Load test orbit 2.", { exact: true }).waitFor();
|
||||
await page.waitForFunction(() => Number(document.querySelector(".capture-flare").getAttribute("opacity")) > 0.1);
|
||||
assert.equal(await page.locator(".capture-arrival").getAttribute("opacity"), "0");
|
||||
await page.waitForFunction(() => Number(document.querySelector(".rocket-flare").getAttribute("opacity")) > 0.1);
|
||||
// A failed backup explodes and its rocket is gone.
|
||||
assert.equal(await page.evaluate(() =>
|
||||
[...document.querySelectorAll('.rocket[data-orbit="orbit-1"]')].filter(node => Number(node.getAttribute("opacity")) > 0).length), 0);
|
||||
|
||||
const workspaceWidth = await frame.evaluate(element => element.getBoundingClientRect().width);
|
||||
await page.getByRole("button", { name: "Big screen", exact: true }).click();
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import { capturePhases, assignOrbitSlots, orbitPose, groupSnapshots, moonPoses, trailAngle } from "../src/lib/galaxyLayout.ts";
|
||||
import { assignOrbitSlots, orbitPose, groupSnapshots, moonPoses, trailAngle } from "../src/lib/galaxyLayout.ts";
|
||||
import { placeLabels } from "../src/lib/galaxyLabelLayout.ts";
|
||||
|
||||
test("slot identities survive reordering, insertion and removal", () => {
|
||||
@@ -63,21 +63,6 @@ test("labels respect screen bounds and preserve priority in a crowded scene", ()
|
||||
assert.ok(labels[0].x >= 0 && labels[0].x + 72 <= 140);
|
||||
});
|
||||
|
||||
test("capture flares before flight, then absorbs and highlights the snapshot", () => {
|
||||
const flare = capturePhases(0.06);
|
||||
assert.equal(flare.flare, 1);
|
||||
assert.equal(flare.flight, 0);
|
||||
assert.equal(flare.highlight, 0);
|
||||
const arrival = capturePhases(0.84);
|
||||
assert.equal(arrival.flight, 1);
|
||||
assert.equal(arrival.absorption, 1);
|
||||
assert.equal(arrival.highlight, 0);
|
||||
assert.equal(capturePhases(0.94).absorption, 0);
|
||||
assert.equal(capturePhases(0.94).highlight, 0);
|
||||
assert.equal(capturePhases(0.97).highlight, 1);
|
||||
assert.deepEqual(capturePhases(1), { flare: 0, flight: 1, absorption: 0, highlight: 0, failure: 0 });
|
||||
});
|
||||
|
||||
test("the selected label searches farther when nearby orbit markers crowd it out", () => {
|
||||
const selected = { id: "focused", x: 140, y: 140, width: 60 };
|
||||
const cluster = { x: 61, y: 61, width: 158, height: 158 };
|
||||
|
||||
79
web/scripts/orbitActivity.test.mjs
Normal file
79
web/scripts/orbitActivity.test.mjs
Normal file
@@ -0,0 +1,79 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import { createActivity, intentFor, observeStatuses } from "../src/lib/orbitActivity.ts";
|
||||
|
||||
const map = (entries) => new Map(Object.entries(entries));
|
||||
const run = (state, entries, causes) => observeStatuses(state, map(entries), causes ? map(causes) : undefined);
|
||||
|
||||
test("the first observation is a baseline: running orbits start circling, finished ones stay settled", () => {
|
||||
const state = createActivity();
|
||||
const events = run(state, { a: "running", b: "completed", c: "failed", d: "queued", e: "rehearsing", f: "idle" });
|
||||
assert.deepEqual(events, [
|
||||
{ kind: "begin", id: "a", intent: "circle" },
|
||||
{ kind: "begin", id: "d", intent: "pad" },
|
||||
{ kind: "begin", id: "e", intent: "ghost" },
|
||||
]);
|
||||
assert.equal(state.intents.get("b"), "none");
|
||||
});
|
||||
|
||||
test("a status that has not changed produces nothing", () => {
|
||||
const state = createActivity();
|
||||
run(state, { a: "running", b: "completed" });
|
||||
assert.deepEqual(run(state, { a: "running", b: "completed" }), []);
|
||||
});
|
||||
|
||||
test("a running backup that completes launches, and one that fails reports its cause", () => {
|
||||
const state = createActivity();
|
||||
run(state, { a: "running", b: "running", c: "running" });
|
||||
const events = run(state, { a: "completed", b: "failed", c: "failed" }, { c: "transfer" });
|
||||
assert.deepEqual(events, [
|
||||
{ kind: "launch", id: "a" },
|
||||
{ kind: "fail", id: "b", cause: "unknown" },
|
||||
{ kind: "fail", id: "c", cause: "transfer" },
|
||||
]);
|
||||
});
|
||||
|
||||
test("queued becoming running is the same rocket changing pace, then it launches", () => {
|
||||
const state = createActivity();
|
||||
run(state, { a: "queued" });
|
||||
assert.deepEqual(run(state, { a: "running" }), [{ kind: "begin", id: "a", intent: "circle" }]);
|
||||
assert.deepEqual(run(state, { a: "completed" }), [{ kind: "launch", id: "a" }]);
|
||||
});
|
||||
|
||||
test("a rehearsal lands instead of launching", () => {
|
||||
const state = createActivity();
|
||||
run(state, { a: "rehearsing" });
|
||||
assert.deepEqual(run(state, { a: "rehearsed" }), [{ kind: "land", id: "a" }]);
|
||||
});
|
||||
|
||||
test("a backup that finished between two polls still gets its outcome", () => {
|
||||
const state = createActivity();
|
||||
run(state, { a: "idle", b: "failed", c: "completed", d: "completed" });
|
||||
const events = run(state, { a: "completed", b: "completed", c: "failed", d: "completed" });
|
||||
assert.deepEqual(events, [
|
||||
{ kind: "launch", id: "a" },
|
||||
{ kind: "launch", id: "b" },
|
||||
{ kind: "fail", id: "c", cause: "unknown" },
|
||||
]);
|
||||
});
|
||||
|
||||
test("an orbit that appears later is treated like one present at load", () => {
|
||||
const state = createActivity();
|
||||
run(state, { a: "completed" });
|
||||
const events = run(state, { a: "completed", b: "running", c: "completed" });
|
||||
assert.deepEqual(events, [{ kind: "begin", id: "b", intent: "circle" }]);
|
||||
});
|
||||
|
||||
test("a backup that stops without a verdict, or whose orbit is deleted, lets the rocket go quietly", () => {
|
||||
const state = createActivity();
|
||||
run(state, { a: "running", b: "running" });
|
||||
assert.deepEqual(run(state, { a: "idle", b: "running" }), [{ kind: "dismiss", id: "a" }]);
|
||||
assert.deepEqual(run(state, { a: "idle" }), [{ kind: "dismiss", id: "b" }]);
|
||||
});
|
||||
|
||||
test("intents follow the status while a backup is going", () => {
|
||||
assert.equal(intentFor("queued"), "pad");
|
||||
assert.equal(intentFor("running"), "circle");
|
||||
assert.equal(intentFor("rehearsing"), "ghost");
|
||||
for (const status of ["completed", "failed", "idle", "rehearsed", "", "mystery"]) assert.equal(intentFor(status), "none");
|
||||
});
|
||||
344
web/scripts/rocket.test.mjs
Normal file
344
web/scripts/rocket.test.mjs
Normal file
@@ -0,0 +1,344 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import {
|
||||
MAX_ROCKETS, SHADOW_EDGE, adoptCircle, applyActivity, createField, fieldActive, holding, infallLook, screenRadius,
|
||||
starPose, stepField,
|
||||
} from "../src/lib/rocket.ts";
|
||||
|
||||
const SHELLS = [[128, 0.047], [190, 0.033], [260, 0.023]];
|
||||
const DT = 1 / 60;
|
||||
|
||||
function scene(radius, speed, angle, rate = 1) {
|
||||
const slot = { radius, angle, speed };
|
||||
const state = { clock: 0, field: createField(), rate, slot };
|
||||
state.star = () => starPose(slot, state.clock, rate);
|
||||
state.step = (seconds) => {
|
||||
const events = [];
|
||||
for (let i = 0; i < Math.round(seconds / DT); i++) {
|
||||
state.clock += DT;
|
||||
events.push(...stepField(state.field, DT, state.star));
|
||||
}
|
||||
return events;
|
||||
};
|
||||
state.apply = (event) => applyActivity(state.field, event, state.star(), 3);
|
||||
return state;
|
||||
}
|
||||
|
||||
function fly(s, limit = 12) {
|
||||
// Run until the rocket arrives. Returns how long it waited for its launch window, how long it flew, and how many
|
||||
// degrees its path swept around the black hole.
|
||||
let wait = 0;
|
||||
let flight = 0;
|
||||
let launched = false;
|
||||
let previous = 0;
|
||||
let swept = 0;
|
||||
let arrived = false;
|
||||
for (let i = 0; i < 60 * limit && !arrived; i++) {
|
||||
const events = s.step(DT);
|
||||
const r = s.field.rockets.get("a");
|
||||
if (r && r.mode === "launch") {
|
||||
const angle = Math.atan2(r.y, r.x);
|
||||
if (launched) swept += Math.atan2(Math.sin(angle - previous), Math.cos(angle - previous));
|
||||
launched = true;
|
||||
previous = angle;
|
||||
flight += DT;
|
||||
} else if (!launched) wait += DT;
|
||||
if (events.some((e) => e.kind === "arrive")) arrived = true;
|
||||
}
|
||||
return { arrived, wait, flight, sweep: (Math.abs(swept) * 180) / Math.PI };
|
||||
}
|
||||
|
||||
test("every launch, from every shell and phase of the circle, spirals in and reaches the shadow", () => {
|
||||
const sweeps = [];
|
||||
for (const [radius, speed] of SHELLS) {
|
||||
for (const rate of [0, 1]) {
|
||||
for (let k = 0; k < 12; k++) {
|
||||
const s = scene(radius, speed, (k / 12) * Math.PI * 2, rate);
|
||||
s.apply({ kind: "begin", id: "a", intent: "circle" });
|
||||
s.step(3);
|
||||
s.apply({ kind: "launch", id: "a" });
|
||||
const result = fly(s);
|
||||
const label = `shell ${radius} phase ${k} rate ${rate}`;
|
||||
assert.ok(result.arrived, `${label} never arrived`);
|
||||
assert.ok(result.wait < 3.05, `${label} waited ${result.wait}`);
|
||||
assert.ok(result.flight > 1 && result.flight < 4.5, `${label} flew for ${result.flight}`);
|
||||
// The path follows the momentum of the circle round the black hole: it curves, it does not run straight in.
|
||||
assert.ok(result.sweep > 50 && result.sweep < 520, `${label} swept ${result.sweep} degrees`);
|
||||
sweeps.push(result.sweep);
|
||||
assert.equal(s.field.rockets.size, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
sweeps.sort((x, y) => x - y);
|
||||
assert.ok(sweeps[Math.floor(sweeps.length / 2)] > 120, `median sweep ${sweeps[Math.floor(sweeps.length / 2)]}`);
|
||||
});
|
||||
|
||||
test("a launch spirals the way the orbits already go, never against them", () => {
|
||||
for (let k = 0; k < 12; k++) {
|
||||
const s = scene(190, 0.033, (k / 12) * Math.PI * 2);
|
||||
s.apply({ kind: "begin", id: "a", intent: "circle" });
|
||||
s.step(3);
|
||||
s.apply({ kind: "launch", id: "a" });
|
||||
let angular = 0;
|
||||
for (let i = 0; i < 60 * 8; i++) {
|
||||
s.step(DT);
|
||||
const r = s.field.rockets.get("a");
|
||||
if (r?.mode === "launch") angular += (r.x * r.vy - r.y * r.vx) / (r.x * r.x + r.y * r.y) * DT;
|
||||
}
|
||||
assert.ok(angular > 0, `phase ${k} went round the wrong way`);
|
||||
}
|
||||
});
|
||||
|
||||
test("no launch ever slows down on its way in, from any shell, phase or map speed", () => {
|
||||
for (const [radius, speed] of SHELLS) {
|
||||
for (const rate of [0, 1]) {
|
||||
for (let k = 0; k < 12; k++) {
|
||||
const s = scene(radius, speed, (k / 12) * Math.PI * 2, rate);
|
||||
s.apply({ kind: "begin", id: "a", intent: "circle" });
|
||||
s.step(3);
|
||||
s.apply({ kind: "launch", id: "a" });
|
||||
let launchedAt;
|
||||
let peak = 0;
|
||||
for (let i = 0; i < 60 * 10; i++) {
|
||||
const events = s.step(DT);
|
||||
const r = s.field.rockets.get("a");
|
||||
if (r?.mode === "launch") {
|
||||
launchedAt ??= i * DT;
|
||||
if (i * DT - launchedAt > 1) {
|
||||
const v = Math.hypot(r.vx, r.vy);
|
||||
assert.ok(v >= peak * 0.97, `shell ${radius} phase ${k} rate ${rate} slowed from ${peak} to ${v}`);
|
||||
peak = Math.max(peak, v);
|
||||
}
|
||||
}
|
||||
if (events.some((e) => e.kind === "arrive")) break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test("the rocket speeds up as it falls toward the black hole", () => {
|
||||
const s = scene(190, 0.033, 1.0);
|
||||
s.apply({ kind: "begin", id: "a", intent: "circle" });
|
||||
s.step(4);
|
||||
s.apply({ kind: "launch", id: "a" });
|
||||
const samples = [];
|
||||
let launchedAt;
|
||||
for (let i = 0; i < 60 * 8; i++) {
|
||||
const events = s.step(DT);
|
||||
const r = s.field.rockets.get("a");
|
||||
if (r?.mode === "launch") {
|
||||
launchedAt ??= i * DT;
|
||||
samples.push({ t: i * DT - launchedAt, radius: screenRadius(r.x, r.y), speed: Math.hypot(r.vx, r.vy) });
|
||||
}
|
||||
if (events.some((e) => e.kind === "arrive")) break;
|
||||
}
|
||||
// After the engine burn the speed rises monotonically all the way in, with only numerical slack.
|
||||
const coast = samples.filter((p) => p.t > 0.9);
|
||||
for (let i = 1; i < coast.length; i++) assert.ok(coast[i].speed >= coast[i - 1].speed * 0.99, `slowed at t=${coast[i].t}`);
|
||||
const far = samples.find((p) => p.radius < 130).speed;
|
||||
const near = samples.find((p) => p.radius < 50).speed;
|
||||
assert.ok(near > 1.6 * far, `speed ${far} far, ${near} near`);
|
||||
assert.ok(Math.max(...samples.map((p) => p.speed)) > 4 * samples[0].speed);
|
||||
});
|
||||
|
||||
test("light from a falling rocket is redshifted and dims to nothing by the shadow edge", () => {
|
||||
let previous = infallLook(200);
|
||||
assert.equal(previous.alpha, 1);
|
||||
assert.equal(previous.stretch, 1);
|
||||
for (let radius = 200; radius >= SHADOW_EDGE; radius -= 2) {
|
||||
const look = infallLook(radius);
|
||||
assert.ok(look.alpha <= previous.alpha + 1e-12 && look.stretch >= previous.stretch - 1e-12 && look.red >= previous.red - 1e-12);
|
||||
previous = look;
|
||||
}
|
||||
assert.ok(infallLook(SHADOW_EDGE + 2).alpha < 0.01);
|
||||
assert.ok(infallLook(SHADOW_EDGE + 2).stretch > 3);
|
||||
});
|
||||
|
||||
test("a rocket circling its star stays close to it and spins up", () => {
|
||||
const s = scene(190, 0.033, 0.4);
|
||||
s.apply({ kind: "begin", id: "a", intent: "circle" });
|
||||
let last = 0;
|
||||
for (let i = 0; i < 60 * 30; i++) {
|
||||
s.step(DT);
|
||||
const r = s.field.rockets.get("a");
|
||||
const star = s.star();
|
||||
assert.ok(Math.hypot(r.x - star.x, r.y - star.y) <= 20);
|
||||
if (i > 60) {
|
||||
assert.ok(r.omega.value >= last - 0.02, "angular speed should not fall while circling");
|
||||
last = r.omega.value;
|
||||
}
|
||||
}
|
||||
assert.ok(last > 4 && last < 5.5, `final spin ${last}`);
|
||||
});
|
||||
|
||||
test("a queued rocket waits on the pad, docked on its star", () => {
|
||||
const s = scene(190, 0.033, 0.4);
|
||||
s.apply({ kind: "begin", id: "a", intent: "pad" });
|
||||
s.step(2.5);
|
||||
const r = s.field.rockets.get("a");
|
||||
const star = s.star();
|
||||
assert.ok(Math.hypot(r.x - star.x, r.y - star.y) < 1);
|
||||
assert.ok(r.alpha.value > 0.95);
|
||||
});
|
||||
|
||||
test("leaving the circle keeps the velocity the rocket had, so the departure is seamless", () => {
|
||||
const s = scene(190, 0.033, 0.4);
|
||||
s.apply({ kind: "begin", id: "a", intent: "circle" });
|
||||
s.step(3.3);
|
||||
s.apply({ kind: "launch", id: "a" });
|
||||
const r = s.field.rockets.get("a");
|
||||
assert.equal(r.pendingLaunch, true, "it should wait for a launch window while circling");
|
||||
assert.equal(r.mode, "circle");
|
||||
let before;
|
||||
for (let i = 0; i < 60 * 4 && r.mode === "circle"; i++) {
|
||||
before = { vx: r.vx, vy: r.vy };
|
||||
s.step(DT);
|
||||
}
|
||||
assert.equal(r.mode, "launch");
|
||||
// The speed changes by what one frame of gravity and engine can give, with no jump at the switch.
|
||||
assert.ok(Math.hypot(r.vx - before.vx, r.vy - before.vy) < 15, "velocity jumped when the rocket left the circle");
|
||||
});
|
||||
|
||||
test("a launch is released in a window where its own motion already carries it round the hole and a little inward", () => {
|
||||
const s = scene(190, 0.033, 1.1);
|
||||
s.apply({ kind: "begin", id: "a", intent: "circle" });
|
||||
s.step(3);
|
||||
s.apply({ kind: "launch", id: "a" });
|
||||
const r = s.field.rockets.get("a");
|
||||
for (let i = 0; i < 60 * 4 && r.mode === "circle"; i++) s.step(DT);
|
||||
const rr = Math.hypot(r.x, r.y);
|
||||
const speed = Math.hypot(r.vx, r.vy);
|
||||
const inward = -(r.x * r.vx + r.y * r.vy) / (rr * speed);
|
||||
const prograde = (r.x * r.vy - r.y * r.vx) / (rr * speed);
|
||||
assert.ok(prograde > 0.8, `prograde ${prograde}`);
|
||||
assert.ok(inward > 0.05 && inward < 0.6, `inward ${inward}`);
|
||||
});
|
||||
|
||||
test("a new run interrupts a flight and takes over exactly where the rocket is", () => {
|
||||
const s = scene(190, 0.033, 0.4);
|
||||
s.apply({ kind: "begin", id: "a", intent: "circle" });
|
||||
s.step(3);
|
||||
s.apply({ kind: "launch", id: "a" });
|
||||
s.step(0.5);
|
||||
const r = s.field.rockets.get("a");
|
||||
const before = { x: r.x, y: r.y, vx: r.vx, vy: r.vy };
|
||||
s.apply({ kind: "begin", id: "a", intent: "circle" });
|
||||
assert.equal(r.mode, "circle");
|
||||
// The circling controller reproduces the position and velocity the rocket had.
|
||||
const star = s.star();
|
||||
const cos = Math.cos(r.phi);
|
||||
const sin = Math.sin(r.phi);
|
||||
const rho = r.radius.value;
|
||||
assert.ok(Math.abs(star.x + rho * cos - before.x) < 1e-9 && Math.abs(star.y + rho * sin - before.y) < 1e-9);
|
||||
assert.ok(Math.abs(star.vx + r.radius.velocity * cos - rho * r.omega.value * sin - before.vx) < 1e-6);
|
||||
assert.ok(Math.abs(star.vy + r.radius.velocity * sin + rho * r.omega.value * cos - before.vy) < 1e-6);
|
||||
});
|
||||
|
||||
test("adopting a circle from a stationary rocket on top of its star is safe", () => {
|
||||
const s = scene(190, 0.033, 0.4);
|
||||
s.apply({ kind: "begin", id: "a", intent: "pad" });
|
||||
const r = s.field.rockets.get("a");
|
||||
adoptCircle(r, s.star());
|
||||
assert.equal(r.radius.value, 0);
|
||||
assert.ok(Number.isFinite(r.phi));
|
||||
});
|
||||
|
||||
test("a failure before departure explodes in place and leaves debris that dies away", () => {
|
||||
const s = scene(190, 0.033, 0.4);
|
||||
s.apply({ kind: "begin", id: "a", intent: "circle" });
|
||||
s.step(3);
|
||||
const events = [];
|
||||
s.apply({ kind: "fail", id: "a", cause: "prepare" });
|
||||
assert.equal(s.field.rockets.has("a"), false);
|
||||
assert.equal(s.field.debris.length, 12);
|
||||
events.push(...s.step(DT));
|
||||
assert.ok(events.some((e) => e.kind === "boom" && e.id === "a"));
|
||||
assert.ok(s.field.flares.get("a").value > 0.5);
|
||||
s.step(2);
|
||||
assert.equal(s.field.debris.length, 0);
|
||||
assert.equal(s.field.flares.size, 0);
|
||||
assert.equal(fieldActive(s.field), false);
|
||||
});
|
||||
|
||||
test("a failure during transfer launches, loses power, and falls back to its star", () => {
|
||||
const s = scene(190, 0.033, 0.4);
|
||||
s.apply({ kind: "begin", id: "a", intent: "circle" });
|
||||
s.step(3);
|
||||
s.apply({ kind: "fail", id: "a", cause: "transfer" });
|
||||
const events = [];
|
||||
let farthest = 0;
|
||||
const start = screenRadius(s.field.rockets.get("a").x, s.field.rockets.get("a").y);
|
||||
for (let i = 0; i < 60 * 6; i++) {
|
||||
events.push(...s.step(DT));
|
||||
const r = s.field.rockets.get("a");
|
||||
if (r) farthest = Math.max(farthest, Math.abs(screenRadius(r.x, r.y) - start));
|
||||
}
|
||||
assert.ok(events.some((e) => e.kind === "impact"), "should come down on its star");
|
||||
assert.ok(events.some((e) => e.kind === "boom"), "the impact flares the star");
|
||||
assert.equal(events.some((e) => e.kind === "arrive"), false, "a failed backup never reaches the black hole");
|
||||
assert.ok(farthest > 8, `it should actually climb before failing, climbed ${farthest}`);
|
||||
assert.equal(s.field.rockets.size, 0);
|
||||
});
|
||||
|
||||
test("a rehearsal lands back on its star without reaching the black hole", () => {
|
||||
const s = scene(190, 0.033, 0.4);
|
||||
s.apply({ kind: "begin", id: "a", intent: "ghost" });
|
||||
s.step(3);
|
||||
assert.ok(s.field.rockets.get("a").alpha.value < 0.6, "a rehearsal rocket is a faint ghost");
|
||||
s.apply({ kind: "land", id: "a" });
|
||||
const events = s.step(4);
|
||||
assert.ok(events.some((e) => e.kind === "landed"));
|
||||
assert.equal(s.field.rockets.size, 0);
|
||||
});
|
||||
|
||||
test("the black hole feeds while a rocket falls in and settles afterwards", () => {
|
||||
const s = scene(190, 0.033, 1);
|
||||
s.apply({ kind: "begin", id: "a", intent: "circle" });
|
||||
s.step(3);
|
||||
s.apply({ kind: "launch", id: "a" });
|
||||
let peak = 0;
|
||||
for (let i = 0; i < 60 * 2; i++) {
|
||||
s.step(DT);
|
||||
peak = Math.max(peak, s.field.absorption.value);
|
||||
}
|
||||
assert.ok(peak > 0.4, `absorption peaked at ${peak}`);
|
||||
s.step(5);
|
||||
assert.ok(s.field.absorption.value < 0.03);
|
||||
assert.equal(fieldActive(s.field), false);
|
||||
});
|
||||
|
||||
test("a star keeps looking busy while its outcome is still playing out", () => {
|
||||
const s = scene(190, 0.033, 1);
|
||||
s.apply({ kind: "begin", id: "a", intent: "circle" });
|
||||
s.step(3);
|
||||
assert.equal(holding(s.field, "a"), false);
|
||||
s.apply({ kind: "launch", id: "a" });
|
||||
assert.equal(holding(s.field, "a"), true);
|
||||
s.step(3);
|
||||
assert.equal(holding(s.field, "a"), false);
|
||||
});
|
||||
|
||||
test("at most ten rockets fly at once and an eleventh request is declined", () => {
|
||||
const s = scene(190, 0.033, 0.4);
|
||||
for (let i = 0; i < MAX_ROCKETS; i++) assert.equal(applyActivity(s.field, { kind: "begin", id: `o${i}`, intent: "circle" }, s.star(), i + 1), true);
|
||||
assert.equal(applyActivity(s.field, { kind: "begin", id: "extra", intent: "circle" }, s.star(), 99), false);
|
||||
assert.equal(s.field.rockets.size, MAX_ROCKETS);
|
||||
});
|
||||
|
||||
test("an outcome for an orbit that was never seen working still plays from its star", () => {
|
||||
const s = scene(190, 0.033, 0.4);
|
||||
s.apply({ kind: "launch", id: "a" });
|
||||
const events = s.step(3);
|
||||
assert.ok(events.some((e) => e.kind === "arrive"));
|
||||
const t = scene(190, 0.033, 0.4);
|
||||
t.apply({ kind: "fail", id: "a", cause: "unknown" });
|
||||
assert.equal(t.field.debris.length, 12);
|
||||
});
|
||||
|
||||
test("a rocket whose orbit has disappeared is removed", () => {
|
||||
const s = scene(190, 0.033, 0.4);
|
||||
s.apply({ kind: "begin", id: "a", intent: "circle" });
|
||||
stepField(s.field, DT, () => undefined);
|
||||
assert.equal(s.field.rockets.size, 0);
|
||||
});
|
||||
72
web/scripts/trail.test.mjs
Normal file
72
web/scripts/trail.test.mjs
Normal file
@@ -0,0 +1,72 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import { TRAIL_JUMP, TRAIL_MAX_POINTS, TRAIL_SECONDS, advanceTrail, smoothPath, trailLength } from "../src/lib/trail.ts";
|
||||
|
||||
const DT = 1 / 60;
|
||||
|
||||
function fly(path, seconds, start) {
|
||||
let trail = start;
|
||||
for (let i = 0; i < Math.round(seconds / DT); i++) {
|
||||
const p = path(i * DT);
|
||||
trail = advanceTrail(trail, "a", p.x, p.y, DT);
|
||||
}
|
||||
return trail;
|
||||
}
|
||||
|
||||
test("the trail is the path that was flown: points on a circle stay on that circle", () => {
|
||||
const radius = 40;
|
||||
const trail = fly((t) => ({ x: radius * Math.cos(2 * t), y: radius * Math.sin(2 * t) }), 3);
|
||||
assert.ok(trail.points.length > 10);
|
||||
for (const p of trail.points) assert.ok(Math.abs(Math.hypot(p.x, p.y) - radius) < 1e-6);
|
||||
});
|
||||
|
||||
test("a curved path gives a curved trail, not a straight one", () => {
|
||||
const trail = fly((t) => ({ x: 60 * Math.cos(1.6 * t), y: 60 * Math.sin(1.6 * t) }), 3);
|
||||
const [first] = trail.points;
|
||||
const last = trail.points.at(-1);
|
||||
const chord = Math.hypot(last.x - first.x, last.y - first.y);
|
||||
assert.ok(trailLength(trail.points) > chord * 1.01, "the trail must bend away from its chord");
|
||||
// The middle of the trail lies off the straight line between its ends.
|
||||
const mid = trail.points[Math.floor(trail.points.length / 2)];
|
||||
const offLine = Math.abs((last.x - first.x) * (first.y - mid.y) - (first.x - mid.x) * (last.y - first.y)) / chord;
|
||||
assert.ok(offLine > 1, `middle only ${offLine} off the chord`);
|
||||
});
|
||||
|
||||
test("a faster rocket leaves a longer trail", () => {
|
||||
const slow = fly((t) => ({ x: 30 * t, y: 0 }), 2);
|
||||
const fast = fly((t) => ({ x: 150 * t, y: 0 }), 2);
|
||||
assert.ok(trailLength(fast.points) > 3 * trailLength(slow.points));
|
||||
});
|
||||
|
||||
test("the trail forgets what is older than its window and never grows without bound", () => {
|
||||
const trail = fly((t) => ({ x: 100 * t, y: 0 }), 5);
|
||||
assert.ok(trail.clock - trail.points[0].t <= TRAIL_SECONDS + DT);
|
||||
assert.ok(trail.points.length <= TRAIL_MAX_POINTS);
|
||||
});
|
||||
|
||||
test("a rocket that is not moving leaves no trail to draw", () => {
|
||||
const trail = fly(() => ({ x: 10, y: 10 }), 2);
|
||||
assert.equal(trail.points.length, 1);
|
||||
assert.equal(trailLength(trail.points), 0);
|
||||
});
|
||||
|
||||
test("a new rocket, or a jump too big to be motion, starts a new trail", () => {
|
||||
let trail = fly((t) => ({ x: 40 * t, y: 0 }), 1);
|
||||
const before = trail.points.length;
|
||||
assert.ok(before > 3);
|
||||
trail = advanceTrail(trail, "b", 5, 5, DT);
|
||||
assert.equal(trail.id, "b");
|
||||
assert.equal(trail.points.length, 1);
|
||||
trail = fly((t) => ({ x: 40 * t, y: 0 }), 0.5, trail);
|
||||
const jumped = advanceTrail(trail, "b", trail.points.at(-1).x + TRAIL_JUMP + 5, 0, DT);
|
||||
assert.equal(jumped.points.length, 1);
|
||||
});
|
||||
|
||||
test("the smooth path starts at the oldest point, ends at the newest, and rounds the corners between", () => {
|
||||
assert.equal(smoothPath([]), "");
|
||||
assert.equal(smoothPath([{ x: 1, y: 2 }]), "");
|
||||
assert.equal(smoothPath([{ x: 0, y: 0 }, { x: 10, y: 0 }]), "M0.00 0.00L10.00 0.00");
|
||||
const d = smoothPath([{ x: 0, y: 0 }, { x: 10, y: 0 }, { x: 10, y: 10 }, { x: 20, y: 10 }]);
|
||||
assert.ok(d.startsWith("M0.00 0.00") && d.endsWith("L20.00 10.00"));
|
||||
assert.equal((d.match(/Q/g) ?? []).length, 2, "one quadratic segment per interior point");
|
||||
});
|
||||
Reference in New Issue
Block a user