|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | + |
| 3 | +import { magnetizationCoherence } from "../../../src/render/particles/coherence"; |
| 4 | + |
| 5 | +const FAR = Number.POSITIVE_INFINITY; |
| 6 | + |
| 7 | +describe("magnetizationCoherence", () => { |
| 8 | + it("always stays within [0, 1]", () => { |
| 9 | + for (let t = 0; t < 60; t += 0.37) { |
| 10 | + for (const boostLevel of [0, 1, 2, 3]) { |
| 11 | + for (const obstacleDistance of [0, 5, 11, 22, 50, FAR]) { |
| 12 | + const value = magnetizationCoherence({ timeSeconds: t, boostLevel, obstacleDistance }); |
| 13 | + expect(value).toBeGreaterThanOrEqual(0); |
| 14 | + expect(value).toBeLessThanOrEqual(1); |
| 15 | + } |
| 16 | + } |
| 17 | + } |
| 18 | + }); |
| 19 | + |
| 20 | + it("breathes over time when nothing else is acting", () => { |
| 21 | + const samples = Array.from({ length: 48 }, (_, index) => |
| 22 | + magnetizationCoherence({ timeSeconds: index * 0.25, boostLevel: 0, obstacleDistance: FAR }), |
| 23 | + ); |
| 24 | + const min = Math.min(...samples); |
| 25 | + const max = Math.max(...samples); |
| 26 | + // A visible in-and-out swing, never a hard toggle. |
| 27 | + expect(max - min).toBeGreaterThan(0.3); |
| 28 | + expect(min).toBeGreaterThanOrEqual(0); |
| 29 | + }); |
| 30 | + |
| 31 | + it("rises with boost level", () => { |
| 32 | + const none = magnetizationCoherence({ timeSeconds: 0, boostLevel: 0, obstacleDistance: FAR }); |
| 33 | + const some = magnetizationCoherence({ timeSeconds: 0, boostLevel: 1, obstacleDistance: FAR }); |
| 34 | + const full = magnetizationCoherence({ timeSeconds: 0, boostLevel: 3, obstacleDistance: FAR }); |
| 35 | + |
| 36 | + expect(some).toBeGreaterThan(none); |
| 37 | + expect(full).toBeGreaterThan(some); |
| 38 | + }); |
| 39 | + |
| 40 | + it("is damped toward zero as danger closes in, so danger reads crisp", () => { |
| 41 | + const inputs = { timeSeconds: 0, boostLevel: 3 } as const; |
| 42 | + const far = magnetizationCoherence({ ...inputs, obstacleDistance: FAR }); |
| 43 | + const near = magnetizationCoherence({ ...inputs, obstacleDistance: 11 }); |
| 44 | + const onTop = magnetizationCoherence({ ...inputs, obstacleDistance: 0 }); |
| 45 | + |
| 46 | + expect(near).toBeLessThan(far); |
| 47 | + expect(onTop).toBe(0); |
| 48 | + }); |
| 49 | + |
| 50 | + it("ignores obstacles beyond the danger range", () => { |
| 51 | + const far = magnetizationCoherence({ timeSeconds: 3, boostLevel: 2, obstacleDistance: FAR }); |
| 52 | + const justOutside = magnetizationCoherence({ timeSeconds: 3, boostLevel: 2, obstacleDistance: 40 }); |
| 53 | + expect(justOutside).toBeCloseTo(far, 10); |
| 54 | + }); |
| 55 | +}); |
0 commit comments