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"); });