-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathplayback-source.test.ts
More file actions
244 lines (222 loc) · 6.24 KB
/
playback-source.test.ts
File metadata and controls
244 lines (222 loc) · 6.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import { describe, expect, it, vi } from "vitest";
import {
canPlayRawContentType,
detectCrossOriginSupport,
resolvePlaybackSource,
shouldFallbackToRawPlaybackSource,
} from "@/app/s/[videoId]/_components/playback-source";
function createResponse(
url: string,
init: {
status: number;
headers?: Record<string, string>;
redirected?: boolean;
},
): Response {
const response = new Response(null, {
status: init.status,
headers: init.headers,
});
Object.defineProperty(response, "url", {
value: url,
configurable: true,
});
Object.defineProperty(response, "redirected", {
value: init.redirected ?? false,
configurable: true,
});
return response;
}
describe("detectCrossOriginSupport", () => {
it("disables cross-origin for S3 and R2 URLs", () => {
expect(
detectCrossOriginSupport(
"https://cap-assets.r2.cloudflarestorage.com/video.mp4",
),
).toBe(false);
expect(
detectCrossOriginSupport(
"https://bucket.s3.eu-west-2.amazonaws.com/video.mp4",
),
).toBe(false);
expect(detectCrossOriginSupport("/api/playlist?videoType=mp4")).toBe(true);
});
});
describe("canPlayRawContentType", () => {
it("treats mp4 raw uploads as playable without probing browser support", () => {
expect(
canPlayRawContentType("video/mp4", "https://cap.so/raw-upload.mp4"),
).toBe(true);
});
it("checks browser support for webm raw uploads", () => {
expect(
canPlayRawContentType(
"video/webm;codecs=vp9,opus",
"https://cap.so/raw-upload.webm",
() => ({
canPlayType: vi.fn().mockReturnValue("probably"),
}),
),
).toBe(true);
expect(
canPlayRawContentType(
"video/webm;codecs=vp9,opus",
"https://cap.so/raw-upload.webm",
() => ({
canPlayType: vi.fn().mockReturnValue(""),
}),
),
).toBe(false);
});
});
describe("resolvePlaybackSource", () => {
it("returns the MP4 source immediately when it is available", async () => {
const fetchImpl = vi.fn<typeof fetch>().mockResolvedValueOnce(
createResponse("https://bucket.s3.amazonaws.com/result.mp4", {
status: 206,
redirected: true,
}),
);
const result = await resolvePlaybackSource({
videoSrc: "/api/playlist?videoType=mp4",
rawFallbackSrc: "/api/playlist?videoType=raw-preview",
enableCrossOrigin: true,
fetchImpl,
now: () => 123,
});
expect(fetchImpl).toHaveBeenCalledTimes(1);
expect(fetchImpl).toHaveBeenCalledWith(
"/api/playlist?videoType=mp4&_t=123",
{
headers: { range: "bytes=0-0" },
},
);
expect(result).toEqual({
url: "https://bucket.s3.amazonaws.com/result.mp4",
type: "mp4",
supportsCrossOrigin: false,
});
});
it("falls back to the raw preview when the MP4 probe fails", async () => {
const fetchImpl = vi
.fn<typeof fetch>()
.mockResolvedValueOnce(
createResponse("/api/playlist?videoType=mp4&_t=200", { status: 404 }),
)
.mockResolvedValueOnce(
createResponse("https://cap.so/raw-upload.mp4", {
status: 206,
headers: { "content-type": "video/mp4" },
redirected: true,
}),
);
const result = await resolvePlaybackSource({
videoSrc: "/api/playlist?videoType=mp4",
rawFallbackSrc: "/api/playlist?videoType=raw-preview",
enableCrossOrigin: true,
fetchImpl,
now: () => 200,
});
expect(fetchImpl).toHaveBeenNthCalledWith(
1,
"/api/playlist?videoType=mp4&_t=200",
{
headers: { range: "bytes=0-0" },
},
);
expect(fetchImpl).toHaveBeenNthCalledWith(
2,
"/api/playlist?videoType=raw-preview&_t=200",
{
headers: { range: "bytes=0-0" },
},
);
expect(result).toEqual({
url: "https://cap.so/raw-upload.mp4",
type: "raw",
supportsCrossOrigin: true,
});
});
it("can prefer the raw preview after the MP4 source fails in the player", async () => {
const fetchImpl = vi.fn<typeof fetch>().mockResolvedValueOnce(
createResponse("https://cap.so/raw-upload.webm", {
status: 206,
headers: { "content-type": "video/webm;codecs=vp9,opus" },
redirected: true,
}),
);
const result = await resolvePlaybackSource({
videoSrc: "/api/playlist?videoType=mp4",
rawFallbackSrc: "/api/playlist?videoType=raw-preview",
preferredSource: "raw",
fetchImpl,
now: () => 250,
createVideoElement: () => ({
canPlayType: vi.fn().mockReturnValue("probably"),
}),
});
expect(fetchImpl).toHaveBeenCalledTimes(1);
expect(fetchImpl).toHaveBeenCalledWith(
"/api/playlist?videoType=raw-preview&_t=250",
{
headers: { range: "bytes=0-0" },
},
);
expect(result).toEqual({
url: "https://cap.so/raw-upload.webm",
type: "raw",
supportsCrossOrigin: false,
});
});
it("rejects raw webm previews when the browser cannot play them", async () => {
const fetchImpl = vi
.fn<typeof fetch>()
.mockResolvedValueOnce(
createResponse("/api/playlist?videoType=mp4&_t=300", { status: 404 }),
)
.mockResolvedValueOnce(
createResponse("https://cap.so/raw-upload.webm", {
status: 206,
headers: { "content-type": "video/webm;codecs=vp9,opus" },
redirected: true,
}),
);
const result = await resolvePlaybackSource({
videoSrc: "/api/playlist?videoType=mp4",
rawFallbackSrc: "/api/playlist?videoType=raw-preview",
fetchImpl,
now: () => 300,
createVideoElement: () => ({
canPlayType: vi.fn().mockReturnValue(""),
}),
});
expect(result).toBeNull();
});
it("falls back after MP4 network errors and returns null when no source works", async () => {
const fetchImpl = vi
.fn<typeof fetch>()
.mockRejectedValueOnce(new Error("network"))
.mockResolvedValueOnce(
createResponse("/api/playlist?videoType=raw-preview&_t=400", {
status: 404,
}),
);
const result = await resolvePlaybackSource({
videoSrc: "/api/playlist?videoType=mp4",
rawFallbackSrc: "/api/playlist?videoType=raw-preview",
fetchImpl,
now: () => 400,
});
expect(result).toBeNull();
});
});
describe("shouldFallbackToRawPlaybackSource", () => {
it("allows a single mp4-to-raw fallback", () => {
expect(shouldFallbackToRawPlaybackSource("mp4", "/raw", false)).toBe(true);
expect(shouldFallbackToRawPlaybackSource("mp4", "/raw", true)).toBe(false);
expect(shouldFallbackToRawPlaybackSource("raw", "/raw", true)).toBe(false);
expect(shouldFallbackToRawPlaybackSource("mp4", undefined, false)).toBe(
false,
);
});
});