|
| 1 | +import { afterEach, beforeEach, describe, expect, jest, test } from "bun:test" |
| 2 | +import { |
| 3 | + createRevealDriver, |
| 4 | + initialRevealState, |
| 5 | + REVEAL_TIMEOUT_MS, |
| 6 | + transitionReveal, |
| 7 | + type RevealCommand, |
| 8 | + type RevealEvent, |
| 9 | + type RevealReason, |
| 10 | + type RevealState, |
| 11 | +} from "./reveal-machine" |
| 12 | + |
| 13 | +// Folds events through the machine and keeps the commands of each step. |
| 14 | +function replay(events: readonly RevealEvent[], from = initialRevealState(false)) { |
| 15 | + return events.reduce<{ state: RevealState; commands: (readonly RevealCommand[])[] }>( |
| 16 | + (result, event) => { |
| 17 | + const next = transitionReveal(result.state, event) |
| 18 | + |
| 19 | + return { state: next.state, commands: [...result.commands, next.commands] } |
| 20 | + }, |
| 21 | + { state: from, commands: [] }, |
| 22 | + ) |
| 23 | +} |
| 24 | + |
| 25 | +const shown = (reason: RevealReason): RevealCommand[] => [{ type: "cancel-timeout" }, { type: "show", reason }] |
| 26 | + |
| 27 | +const hidden = { |
| 28 | + loading: [], |
| 29 | + "content-ready": [{ type: "content.ready" }], |
| 30 | + "theme-ready": [{ type: "theme.ready" }], |
| 31 | +} satisfies Record<string, RevealEvent[]> |
| 32 | + |
| 33 | +describe("window reveal", () => { |
| 34 | + test("shows a window whose theme never arrives once the fallback expires", () => { |
| 35 | + const result = replay([{ type: "content.ready" }, { type: "reveal.timed-out" }, { type: "theme.ready" }]) |
| 36 | + |
| 37 | + expect(result.state.status).toBe("shown") |
| 38 | + expect(result.commands).toEqual([[], shown("timeout"), []]) |
| 39 | + }) |
| 40 | + |
| 41 | + test.each(Object.entries(hidden))("shows a %s window before its recovery dialog", (_status, events) => { |
| 42 | + const result = replay([ |
| 43 | + ...events, |
| 44 | + { type: "recovery.requested" }, |
| 45 | + { type: "reveal.timed-out" }, |
| 46 | + { type: "theme.ready" }, |
| 47 | + ]) |
| 48 | + |
| 49 | + expect(result.state.status).toBe("shown") |
| 50 | + expect(result.commands.slice(events.length)).toEqual([shown("recovery"), [], []]) |
| 51 | + }) |
| 52 | + |
| 53 | + test.each([ |
| 54 | + ["content first", [{ type: "content.ready" }, { type: "theme.ready" }]], |
| 55 | + ["theme first", [{ type: "theme.ready" }, { type: "content.ready" }]], |
| 56 | + ] satisfies [string, RevealEvent[]][])("waits for content and theme, %s", (_order, events) => { |
| 57 | + expect(replay(events).commands).toEqual([[], shown("ready")]) |
| 58 | + }) |
| 59 | + |
| 60 | + test("ignores the second content readiness Linux reports", () => { |
| 61 | + const ready = transitionReveal(initialRevealState(false), { type: "content.ready" }).state |
| 62 | + const again = transitionReveal(ready, { type: "content.ready" }) |
| 63 | + |
| 64 | + expect(again.state).toBe(ready) |
| 65 | + expect(again.commands).toEqual([]) |
| 66 | + expect(transitionReveal(again.state, { type: "theme.ready" }).commands).toEqual(shown("ready")) |
| 67 | + }) |
| 68 | + |
| 69 | + test("never shows the early window again, including after a dev reload", () => { |
| 70 | + const early = initialRevealState(true) |
| 71 | + |
| 72 | + const result = replay( |
| 73 | + [ |
| 74 | + { type: "content.ready" }, |
| 75 | + { type: "theme.ready" }, |
| 76 | + { type: "reveal.timed-out" }, |
| 77 | + { type: "recovery.requested" }, |
| 78 | + { type: "theme.ready" }, |
| 79 | + ], |
| 80 | + early, |
| 81 | + ) |
| 82 | + |
| 83 | + expect(early.status).toBe("shown") |
| 84 | + expect(result.state).toBe(early) |
| 85 | + expect(result.commands.flat()).toEqual([]) |
| 86 | + }) |
| 87 | + |
| 88 | + test.each(Object.entries(hidden))("cancels the fallback when a %s window closes", (_status, events) => { |
| 89 | + const result = replay([...events, { type: "window.closed" }]) |
| 90 | + |
| 91 | + expect(result.state.status).toBe("closed") |
| 92 | + expect(result.commands.at(-1)).toEqual([{ type: "cancel-timeout" }]) |
| 93 | + }) |
| 94 | + |
| 95 | + test("ignores every event after the window closes", () => { |
| 96 | + const closed = replay([{ type: "content.ready" }, { type: "theme.ready" }, { type: "window.closed" }]) |
| 97 | + |
| 98 | + const after = replay( |
| 99 | + [ |
| 100 | + { type: "content.ready" }, |
| 101 | + { type: "theme.ready" }, |
| 102 | + { type: "reveal.timed-out" }, |
| 103 | + { type: "recovery.requested" }, |
| 104 | + { type: "window.closed" }, |
| 105 | + ], |
| 106 | + closed.state, |
| 107 | + ) |
| 108 | + |
| 109 | + expect(closed.commands.at(-1)).toEqual([]) |
| 110 | + expect(after.state).toBe(closed.state) |
| 111 | + expect(after.commands.flat()).toEqual([]) |
| 112 | + }) |
| 113 | +}) |
| 114 | + |
| 115 | +describe("window reveal driver", () => { |
| 116 | + beforeEach(() => { |
| 117 | + jest.useFakeTimers() |
| 118 | + }) |
| 119 | + |
| 120 | + afterEach(() => { |
| 121 | + jest.useRealTimers() |
| 122 | + }) |
| 123 | + |
| 124 | + test("shows a hidden window once when the fallback expires", () => { |
| 125 | + const shows: RevealReason[] = [] |
| 126 | + const reveal = createRevealDriver({ early: false, show: (reason) => shows.push(reason) }) |
| 127 | + |
| 128 | + reveal.dispatch({ type: "content.ready" }) |
| 129 | + jest.advanceTimersByTime(REVEAL_TIMEOUT_MS - 1) |
| 130 | + expect(shows).toEqual([]) |
| 131 | + |
| 132 | + jest.advanceTimersByTime(1) |
| 133 | + reveal.dispatch({ type: "theme.ready" }) |
| 134 | + expect(shows).toEqual(["timeout"]) |
| 135 | + }) |
| 136 | + |
| 137 | + test("does not fire the fallback after a ready reveal or a close", () => { |
| 138 | + const shows: RevealReason[] = [] |
| 139 | + const ready = createRevealDriver({ early: false, show: (reason) => shows.push(reason) }) |
| 140 | + const closed = createRevealDriver({ early: false, show: (reason) => shows.push(reason) }) |
| 141 | + |
| 142 | + ready.dispatch({ type: "theme.ready" }) |
| 143 | + ready.dispatch({ type: "content.ready" }) |
| 144 | + closed.dispatch({ type: "window.closed" }) |
| 145 | + jest.runOnlyPendingTimers() |
| 146 | + |
| 147 | + expect(shows).toEqual(["ready"]) |
| 148 | + expect(jest.getTimerCount()).toBe(0) |
| 149 | + }) |
| 150 | + |
| 151 | + test("arms no fallback for the early window", () => { |
| 152 | + const shows: RevealReason[] = [] |
| 153 | + createRevealDriver({ early: true, show: (reason) => shows.push(reason) }) |
| 154 | + |
| 155 | + expect(jest.getTimerCount()).toBe(0) |
| 156 | + expect(shows).toEqual([]) |
| 157 | + }) |
| 158 | +}) |
0 commit comments