|
1 | 1 | // fallow-ignore-file code-duplication |
2 | | -import { describe, it, expect, vi, afterEach } from "vitest"; |
| 2 | +import { describe, it, expect, vi, afterEach, onTestFinished } from "vitest"; |
3 | 3 | import { mediaElementTime } from "./mediaClock"; |
4 | 4 | import { |
5 | 5 | evictMediaSyncState, |
6 | 6 | hasMediaSyncStateForTest, |
| 7 | + isUnplayable, |
7 | 8 | primeMediaForLaterStarts, |
8 | 9 | readElementPlaybackRate, |
9 | 10 | readElementPlaybackStart, |
@@ -1052,6 +1053,48 @@ describe("syncRuntimeMedia", () => { |
1052 | 1053 | expect(clip.el.play).not.toHaveBeenCalled(); |
1053 | 1054 | }); |
1054 | 1055 |
|
| 1056 | + function askedToPlayAtHeader(networkState: number, play?: () => Promise<void>) { |
| 1057 | + vi.useFakeTimers({ toFake: ["performance"] }); |
| 1058 | + onTestFinished(() => vi.useRealTimers()); |
| 1059 | + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 1060 | + onTestFinished(() => warn.mockRestore()); |
| 1061 | + const clip = createMockClip({ start: 0, end: 10 }); |
| 1062 | + clip.el.src = "/alpha.webm"; |
| 1063 | + if (play) clip.el.play = vi.fn(play); |
| 1064 | + Object.defineProperty(clip.el, "readyState", { value: 1, configurable: true }); |
| 1065 | + Object.defineProperty(clip.el, "networkState", { value: networkState, configurable: true }); |
| 1066 | + syncRuntimeMedia({ clips: [clip], timeSeconds: 1, playing: true, playbackRate: 1 }); |
| 1067 | + expect(clip.el.play).toHaveBeenCalledTimes(1); |
| 1068 | + return { el: clip.el, warn }; |
| 1069 | + } |
| 1070 | + |
| 1071 | + it("skips a clip whose fetch stopped at its header 5 s after it was asked to play", () => { |
| 1072 | + const { el, warn } = askedToPlayAtHeader(HTMLMediaElement.NETWORK_IDLE); |
| 1073 | + vi.advanceTimersByTime(4999); |
| 1074 | + expect(isUnplayable(el)).toBe(false); |
| 1075 | + vi.advanceTimersByTime(1); |
| 1076 | + expect(isUnplayable(el)).toBe(true); |
| 1077 | + expect(isUnplayable(el)).toBe(true); |
| 1078 | + expect(warn).toHaveBeenCalledTimes(1); |
| 1079 | + expect(String(warn.mock.calls[0]?.[0])).toContain("/alpha.webm"); |
| 1080 | + }); |
| 1081 | + |
| 1082 | + it("keeps waiting on a clip still loading at its header 5 s after it was asked to play", () => { |
| 1083 | + const { el, warn } = askedToPlayAtHeader(HTMLMediaElement.NETWORK_LOADING); |
| 1084 | + vi.advanceTimersByTime(60_000); |
| 1085 | + expect(isUnplayable(el)).toBe(false); |
| 1086 | + expect(warn).not.toHaveBeenCalled(); |
| 1087 | + }); |
| 1088 | + |
| 1089 | + it("keeps waiting on a clip at its header whose play() the browser refused", async () => { |
| 1090 | + const refused = () => Promise.reject(new DOMException("needs a tap", "NotAllowedError")); |
| 1091 | + const { el, warn } = askedToPlayAtHeader(HTMLMediaElement.NETWORK_IDLE, refused); |
| 1092 | + await Promise.resolve(); |
| 1093 | + vi.advanceTimersByTime(5000); |
| 1094 | + expect(isUnplayable(el)).toBe(false); |
| 1095 | + expect(warn).not.toHaveBeenCalled(); |
| 1096 | + }); |
| 1097 | + |
1055 | 1098 | it("plays again once the element recovers (error clears)", () => { |
1056 | 1099 | const clip = createMockClip({ start: 0, end: 10 }); |
1057 | 1100 | Object.defineProperty(clip.el, "error", { value: { code: 4 }, configurable: true }); |
|
0 commit comments