|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { MascotAnimator } from "./mascot-animator.ts"; |
| 3 | +import { staticMascotPose, type MascotMood, type MascotPose } from "./mascot-pose.ts"; |
| 4 | + |
| 5 | +const MOODS: MascotMood[] = [ |
| 6 | + "idle", |
| 7 | + "curious", |
| 8 | + "thinking", |
| 9 | + "working", |
| 10 | + "happy", |
| 11 | + "celebrating", |
| 12 | + "sad", |
| 13 | + "sleepy", |
| 14 | + "attentive", |
| 15 | +]; |
| 16 | + |
| 17 | +function expectPoseInBounds(pose: MascotPose): void { |
| 18 | + expect(pose.floatOffset).toBeGreaterThanOrEqual(-12); |
| 19 | + expect(pose.floatOffset).toBeLessThanOrEqual(2); |
| 20 | + expect(pose.antennaDegrees).toBeGreaterThanOrEqual(-14); |
| 21 | + expect(pose.antennaDegrees).toBeLessThanOrEqual(14); |
| 22 | + expect(pose.antennaDroop).toBeGreaterThanOrEqual(0); |
| 23 | + expect(pose.antennaDroop).toBeLessThanOrEqual(1); |
| 24 | + expect(pose.leftClawDegrees).toBeGreaterThanOrEqual(-45); |
| 25 | + expect(pose.leftClawDegrees).toBeLessThanOrEqual(45); |
| 26 | + expect(pose.rightClawDegrees).toBeGreaterThanOrEqual(-45); |
| 27 | + expect(pose.rightClawDegrees).toBeLessThanOrEqual(45); |
| 28 | + expect(pose.eyeGlowOpacity).toBeGreaterThanOrEqual(0); |
| 29 | + expect(pose.eyeGlowOpacity).toBeLessThanOrEqual(1); |
| 30 | + expect(pose.glowScale).toBeGreaterThanOrEqual(0.5); |
| 31 | + expect(pose.glowScale).toBeLessThanOrEqual(1.6); |
| 32 | + expect(pose.leftEyeOpenness).toBeGreaterThanOrEqual(0); |
| 33 | + expect(pose.leftEyeOpenness).toBeLessThanOrEqual(1); |
| 34 | + expect(pose.rightEyeOpenness).toBeGreaterThanOrEqual(0); |
| 35 | + expect(pose.rightEyeOpenness).toBeLessThanOrEqual(1); |
| 36 | + expect(pose.happyEyes).toBeGreaterThanOrEqual(0); |
| 37 | + expect(pose.happyEyes).toBeLessThanOrEqual(1); |
| 38 | + expect(pose.gaze.x).toBeGreaterThanOrEqual(-1.2); |
| 39 | + expect(pose.gaze.x).toBeLessThanOrEqual(1.2); |
| 40 | + expect(pose.gaze.y).toBeGreaterThanOrEqual(-1.2); |
| 41 | + expect(pose.gaze.y).toBeLessThanOrEqual(1.2); |
| 42 | + expect(pose.mouthCurve).toBeGreaterThanOrEqual(-1); |
| 43 | + expect(pose.mouthCurve).toBeLessThanOrEqual(1); |
| 44 | + expect(pose.mouthOpen).toBeGreaterThanOrEqual(0); |
| 45 | + expect(pose.mouthOpen).toBeLessThanOrEqual(1); |
| 46 | + expect(pose.mouthRound).toBeGreaterThanOrEqual(0); |
| 47 | + expect(pose.mouthRound).toBeLessThanOrEqual(1); |
| 48 | + expect(pose.blush).toBeGreaterThanOrEqual(0); |
| 49 | + expect(pose.blush).toBeLessThanOrEqual(1); |
| 50 | + expect(pose.hardHat).toBeGreaterThanOrEqual(0); |
| 51 | + expect(pose.hardHat).toBeLessThanOrEqual(1); |
| 52 | + expect(pose.bodyTilt).toBeGreaterThanOrEqual(-8); |
| 53 | + expect(pose.bodyTilt).toBeLessThanOrEqual(8); |
| 54 | + expect(pose.bodyStretch).toBeGreaterThanOrEqual(0.86); |
| 55 | + expect(pose.bodyStretch).toBeLessThanOrEqual(1.05); |
| 56 | + expect(pose.dizzy).toBeGreaterThanOrEqual(0); |
| 57 | + expect(pose.dizzy).toBeLessThanOrEqual(1); |
| 58 | +} |
| 59 | + |
| 60 | +describe("MascotAnimator", () => { |
| 61 | + it("keeps every mood inside drawable channel bounds for 30 seconds", () => { |
| 62 | + for (const [index, mood] of MOODS.entries()) { |
| 63 | + const animator = new MascotAnimator(1_000 + index); |
| 64 | + animator.setMood(mood, 0); |
| 65 | + for (let frame = 0; frame <= 30 * 30; frame += 1) { |
| 66 | + expectPoseInBounds(animator.poseAt(frame / 30)); |
| 67 | + } |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + it("produces identical pose streams from the same seed", () => { |
| 72 | + const first = new MascotAnimator(0x5eed); |
| 73 | + const second = new MascotAnimator(0x5eed); |
| 74 | + first.setMood("thinking", 0); |
| 75 | + second.setMood("thinking", 0); |
| 76 | + |
| 77 | + for (let frame = 0; frame <= 20 * 30; frame += 1) { |
| 78 | + const time = frame / 30; |
| 79 | + expect(first.poseAt(time)).toEqual(second.poseAt(time)); |
| 80 | + } |
| 81 | + }); |
| 82 | + |
| 83 | + it("runs the working hard-hat, hammer, impact, and brow-wipe cycle", () => { |
| 84 | + const animator = new MascotAnimator(42); |
| 85 | + animator.setMood("working", 0); |
| 86 | + const rightClawAngles: number[] = []; |
| 87 | + const effects = new Set<string>(); |
| 88 | + let seatedHat = 0; |
| 89 | + |
| 90 | + for (let frame = 0; frame <= 30 * 30; frame += 1) { |
| 91 | + const pose = animator.poseAt(frame / 30); |
| 92 | + rightClawAngles.push(pose.rightClawDegrees); |
| 93 | + effects.add(pose.effect); |
| 94 | + if (frame >= 33) { |
| 95 | + seatedHat = Math.max(seatedHat, pose.hardHat); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + expect(seatedHat).toBe(1); |
| 100 | + expect(Math.max(...rightClawAngles) - Math.min(...rightClawAngles)).toBeGreaterThan(25); |
| 101 | + expect(effects).toContain("sparks"); |
| 102 | + expect(effects).toContain("sweat"); |
| 103 | + }); |
| 104 | + |
| 105 | + it("cancels stale gestures when the mood changes", () => { |
| 106 | + // Begin as idle (queues the hello wave at +0.9s), then switch mood before |
| 107 | + // it fires — mirrors the element lifecycle where firstUpdated precedes the |
| 108 | + // first `updated()` mood application. |
| 109 | + const animator = new MascotAnimator(9); |
| 110 | + animator.poseAt(0); |
| 111 | + animator.setMood("thinking", 0.1); |
| 112 | + |
| 113 | + for (let frame = 3; frame <= 4 * 30; frame += 1) { |
| 114 | + const pose = animator.poseAt(frame / 30); |
| 115 | + // The wave raises the right claw far past anything thinking's base loop |
| 116 | + // (claw ~0°) or a scheduled claw snap (-8°) can produce. |
| 117 | + expect(pose.rightClawDegrees).toBeGreaterThan(-15); |
| 118 | + } |
| 119 | + }); |
| 120 | + |
| 121 | + it("keeps sleepy z's visible through its yawn entrance", () => { |
| 122 | + const animator = new MascotAnimator(7); |
| 123 | + animator.setMood("sleepy", 0); |
| 124 | + const pose = animator.poseAt(0.8); |
| 125 | + |
| 126 | + expect(pose.effect).toBe("zzz"); |
| 127 | + expect(pose.mouthRound).toBeGreaterThan(0.8); |
| 128 | + expect(pose.leftEyeOpenness).toBeLessThan(0.1); |
| 129 | + }); |
| 130 | +}); |
| 131 | + |
| 132 | +describe("staticMascotPose", () => { |
| 133 | + it("preserves the identifying expression for each motionless mood", () => { |
| 134 | + expect(staticMascotPose("thinking").gaze.y).toBeLessThan(0); |
| 135 | + expect(staticMascotPose("working")).toMatchObject({ hardHat: 1, rightClawDegrees: -28 }); |
| 136 | + expect(staticMascotPose("happy").mouthCurve).toBeGreaterThan(0.5); |
| 137 | + expect(staticMascotPose("celebrating").leftClawDegrees).toBeGreaterThan(25); |
| 138 | + expect(staticMascotPose("celebrating").rightClawDegrees).toBeLessThan(-25); |
| 139 | + expect(staticMascotPose("sad")).toMatchObject({ antennaDroop: 0.75, mouthCurve: -0.55 }); |
| 140 | + expect(staticMascotPose("sleepy").leftEyeOpenness).toBe(0.25); |
| 141 | + expect(staticMascotPose("attentive")).toEqual(staticMascotPose("idle")); |
| 142 | + }); |
| 143 | +}); |
0 commit comments