import assert from "node:assert/strict"; import test from "node:test"; import { ABSORB_LINGER, 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 falls through the shadow to the centre", () => { 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); // It ends at the singularity itself, lingers a moment so its trail can finish there, then is removed. const sunk = s.field.rockets.get("a"); assert.equal(sunk.x, 0, `${label} did not end at the centre`); assert.equal(sunk.y, 0, `${label} did not end at the centre`); s.step(ABSORB_LINGER + 0.1); 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); });