|
1 | 1 | import { describe, expect, test } from "bun:test"; |
2 | 2 |
|
3 | 3 | import { PRODUCT_NAME } from "../branding.js"; |
| 4 | +import { MARK_PERIOD_SECONDS, markFrame, markText, renderMark } from "./mark-anim.js"; |
4 | 5 | import { MARK_LARGE, MARK_MID, MARK_SMALL } from "./mark-shape.js"; |
5 | 6 | import { createHarness } from "./harness.js"; |
6 | | -import { resolveWelcomeMarkGrid, runWelcome, WELCOME_LINE } from "./welcome.js"; |
| 7 | +import { stringWidth } from "./view/height.js"; |
| 8 | +import { |
| 9 | + resolveWelcomeLine, |
| 10 | + resolveWelcomeMarkGrid, |
| 11 | + runWelcome, |
| 12 | + WELCOME_AUTO_ADVANCE_MS, |
| 13 | + WELCOME_LINE, |
| 14 | + welcomeMarkStill, |
| 15 | +} from "./welcome.js"; |
7 | 16 |
|
8 | 17 | describe("WELCOME_LINE", () => { |
9 | 18 | test("names the product as the local software factory", () => { |
@@ -74,3 +83,114 @@ describe("runWelcome", () => { |
74 | 83 | } |
75 | 84 | }); |
76 | 85 | }); |
| 86 | + |
| 87 | +describe("welcomeMarkStill", () => { |
| 88 | + test("animates through fill, then freezes the full frame", () => { |
| 89 | + const fillMs = 0.76 * MARK_PERIOD_SECONDS * 1000; |
| 90 | + expect(welcomeMarkStill(fillMs - 1)).toBe(false); |
| 91 | + expect(welcomeMarkStill(fillMs)).toBe(true); |
| 92 | + expect(welcomeMarkStill(0.95 * MARK_PERIOD_SECONDS * 1000)).toBe(true); |
| 93 | + expect(welcomeMarkStill(MARK_PERIOD_SECONDS * 1000 + 900)).toBe(true); |
| 94 | + }); |
| 95 | +}); |
| 96 | + |
| 97 | +describe("WELCOME_AUTO_ADVANCE_MS", () => { |
| 98 | + test("lands on the held filled frame, not fade-out or a second draw-in", () => { |
| 99 | + const seconds = WELCOME_AUTO_ADVANCE_MS / 1000; |
| 100 | + expect(seconds).toBeGreaterThanOrEqual(0.76 * MARK_PERIOD_SECONDS); |
| 101 | + expect(seconds).toBeLessThan(MARK_PERIOD_SECONDS); |
| 102 | + |
| 103 | + const still = welcomeMarkStill(WELCOME_AUTO_ADVANCE_MS); |
| 104 | + const frame = markFrame(seconds, still); |
| 105 | + expect(still).toBe(true); |
| 106 | + expect(frame).toEqual({ drawProg: 1, fillProg: 1, alpha: 1 }); |
| 107 | + |
| 108 | + // Looping math at this delay must also still be the full hold — never the |
| 109 | + // fade (90–100%) or the wrapped second draw-in. |
| 110 | + const looping = markFrame(seconds, false); |
| 111 | + expect(looping.alpha).toBe(1); |
| 112 | + expect(looping.fillProg).toBe(1); |
| 113 | + expect(looping.drawProg).toBe(1); |
| 114 | + expect(seconds).toBeLessThan(0.9 * MARK_PERIOD_SECONDS + 1e-9); |
| 115 | + }); |
| 116 | +}); |
| 117 | + |
| 118 | +describe("resolveWelcomeLine", () => { |
| 119 | + test("keeps the full factory sentence or hides it, never a mid-word slice", () => { |
| 120 | + expect(resolveWelcomeLine(80)).toBe(WELCOME_LINE); |
| 121 | + expect(resolveWelcomeLine(stringWidth(WELCOME_LINE))).toBe(WELCOME_LINE); |
| 122 | + |
| 123 | + const truncated = WELCOME_LINE.slice(0, 39); |
| 124 | + expect(truncated).toContain("facto"); |
| 125 | + expect(truncated).not.toBe(WELCOME_LINE); |
| 126 | + |
| 127 | + const narrow = resolveWelcomeLine(40); |
| 128 | + expect(narrow === "" || narrow === WELCOME_LINE).toBe(true); |
| 129 | + expect(narrow).not.toBe(truncated); |
| 130 | + expect(narrow.includes("facto") && !narrow.includes("factory")).toBe(false); |
| 131 | + }); |
| 132 | +}); |
| 133 | + |
| 134 | +describe("runWelcome hold and cancel", () => { |
| 135 | + test("paints a still full mark after fill instead of fading", async () => { |
| 136 | + const fadeMs = 0.95 * MARK_PERIOD_SECONDS * 1000; |
| 137 | + // First `now()` is mount (`startedAt`); later samples are elapsed fadeMs. |
| 138 | + let samples = 0; |
| 139 | + const harness = await createHarness({ width: 80, height: 30 }); |
| 140 | + const done = runWelcome({ |
| 141 | + createRenderer: async () => harness.renderer, |
| 142 | + autoAdvanceMs: 60_000, |
| 143 | + now: () => (samples++ === 0 ? 0 : fadeMs), |
| 144 | + }); |
| 145 | + try { |
| 146 | + await harness.renderOnce(); |
| 147 | + await harness.renderOnce(); |
| 148 | + const frame = harness.captureCharFrame(); |
| 149 | + const held = markText(renderMark({ nowMs: fadeMs, still: true, grid: MARK_LARGE })); |
| 150 | + const fading = markText(renderMark({ nowMs: fadeMs, still: false, grid: MARK_LARGE })); |
| 151 | + const mountain = (text: string) => (text.match(/[▁▂▃▄▅▆▇█]/g) ?? []).length; |
| 152 | + expect(mountain(frame)).toBe(mountain(held)); |
| 153 | + expect(mountain(held)).toBeGreaterThan(mountain(fading)); |
| 154 | + } finally { |
| 155 | + harness.pressKey("Ctrl+C"); |
| 156 | + await Promise.race([done, new Promise((r) => setTimeout(r, 50))]); |
| 157 | + harness.destroy(); |
| 158 | + } |
| 159 | + }); |
| 160 | + |
| 161 | + test("cancels on Ctrl+D without continuing", async () => { |
| 162 | + const harness = await createHarness({ width: 80, height: 30 }); |
| 163 | + const done = runWelcome({ |
| 164 | + createRenderer: async () => harness.renderer, |
| 165 | + autoAdvanceMs: 60_000, |
| 166 | + now: () => 2_000, |
| 167 | + }); |
| 168 | + try { |
| 169 | + await harness.renderOnce(); |
| 170 | + harness.pressKey("d", { ctrl: true }); |
| 171 | + await expect(done).resolves.toBe(false); |
| 172 | + } finally { |
| 173 | + harness.destroy(); |
| 174 | + } |
| 175 | + }); |
| 176 | + |
| 177 | + test("narrow terminals do not paint a sliced factory sentence", async () => { |
| 178 | + const harness = await createHarness({ width: 42, height: 30 }); |
| 179 | + const done = runWelcome({ |
| 180 | + createRenderer: async () => harness.renderer, |
| 181 | + autoAdvanceMs: 60_000, |
| 182 | + now: () => 2_000, |
| 183 | + }); |
| 184 | + try { |
| 185 | + await harness.renderOnce(); |
| 186 | + await harness.renderOnce(); |
| 187 | + const frame = harness.captureCharFrame(); |
| 188 | + expect(frame).not.toContain("software facto"); |
| 189 | + expect(frame.includes("facto") && !frame.includes("factory")).toBe(false); |
| 190 | + } finally { |
| 191 | + harness.pressKey("Ctrl+C"); |
| 192 | + await Promise.race([done, new Promise((r) => setTimeout(r, 50))]); |
| 193 | + harness.destroy(); |
| 194 | + } |
| 195 | + }); |
| 196 | +}); |
0 commit comments