videoReplayer.spec.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. import {createEvent, fireEvent} from 'sentry-test/reactTestingLibrary';
  2. import {VideoReplayer} from './videoReplayer';
  3. // XXX: Not quite sure the best way to mock RAF - here we use fake timers
  4. // VideoReplayer uses `app/util/replays/timer` which uses RAF to count up. This
  5. // is used to render the progress of the seeker bar and sync with video
  6. // replays.
  7. //
  8. // advancing by 2000ms ~== 20000s in Timer, but this may depend on hardware, TBD
  9. jest.useFakeTimers();
  10. jest.spyOn(window.HTMLMediaElement.prototype, 'pause').mockImplementation(() => {});
  11. describe('VideoReplayer - no starting gap', () => {
  12. beforeEach(() => {
  13. jest.clearAllTimers();
  14. });
  15. const attachments = [
  16. {
  17. id: 0,
  18. timestamp: 0,
  19. duration: 5000,
  20. },
  21. // no gap
  22. {
  23. id: 1,
  24. timestamp: 5000,
  25. duration: 5000,
  26. },
  27. {
  28. id: 2,
  29. timestamp: 10_001,
  30. duration: 5000,
  31. },
  32. // 5 second gap
  33. {
  34. id: 3,
  35. timestamp: 20_000,
  36. duration: 5000,
  37. },
  38. // 5 second gap
  39. {
  40. id: 4,
  41. timestamp: 30_000,
  42. duration: 5000,
  43. },
  44. {
  45. id: 5,
  46. timestamp: 35_002,
  47. duration: 5000,
  48. },
  49. ];
  50. const extra = [
  51. {
  52. id: 6,
  53. timestamp: 40_002,
  54. duration: 5000,
  55. },
  56. {
  57. id: 7,
  58. timestamp: 45_002,
  59. duration: 5000,
  60. },
  61. ];
  62. const skip = [
  63. {
  64. id: 7,
  65. timestamp: 45_002,
  66. duration: 5000,
  67. },
  68. {
  69. id: 8,
  70. timestamp: 50_002,
  71. duration: 5000,
  72. },
  73. ];
  74. it('plays and seeks inside of a segment', async () => {
  75. const root = document.createElement('div');
  76. const inst = new VideoReplayer(attachments, {
  77. videoApiPrefix: '/foo/',
  78. root,
  79. start: 0,
  80. onFinished: jest.fn(),
  81. onLoaded: jest.fn(),
  82. onBuffer: jest.fn(),
  83. durationMs: 40000,
  84. });
  85. // @ts-expect-error private
  86. expect(inst._currentIndex).toEqual(0);
  87. const playPromise = inst.play(6500);
  88. jest.advanceTimersByTime(10000);
  89. await playPromise;
  90. // @ts-expect-error private
  91. expect(inst._currentIndex).toEqual(1);
  92. // `currentTime` is in seconds
  93. // @ts-expect-error private
  94. expect(inst.getVideo(inst._currentIndex)?.currentTime).toEqual(1.5);
  95. });
  96. it('seeks to a gap in a video', async () => {
  97. const root = document.createElement('div');
  98. const inst = new VideoReplayer(attachments, {
  99. videoApiPrefix: '/foo/',
  100. root,
  101. start: 0,
  102. onFinished: jest.fn(),
  103. onLoaded: jest.fn(),
  104. onBuffer: jest.fn(),
  105. durationMs: 40000,
  106. });
  107. const playPromise = inst.play(18100);
  108. // @ts-expect-error private
  109. const video = inst.getVideo(2)!;
  110. // the replay is actually stopped right now to wait for loading
  111. fireEvent(video, createEvent.loadedData(video));
  112. // 15000 -> 20000 is a gap, so player should start playing @ index 3, from
  113. // the beginning.
  114. jest.advanceTimersByTime(2500);
  115. await playPromise;
  116. // @ts-expect-error private
  117. expect(inst._currentIndex).toEqual(3);
  118. // `currentTime` is in seconds
  119. // @ts-expect-error private
  120. expect(inst.getVideo(inst._currentIndex)?.currentTime).toEqual(0);
  121. });
  122. it('seeks past end of the replay', async () => {
  123. const root = document.createElement('div');
  124. const inst = new VideoReplayer(attachments, {
  125. videoApiPrefix: '/foo/',
  126. root,
  127. start: 0,
  128. // Unfortunately, `video.play()` is not implemented in jsdom, so no events,
  129. // so can't check that onFinished is called
  130. onFinished: jest.fn(),
  131. onLoaded: jest.fn(),
  132. onBuffer: jest.fn(),
  133. durationMs: 40000,
  134. });
  135. const playPromise = inst.play(50000);
  136. // 15000 -> 20000 is a gap, so player should start playing @ index 3, from
  137. // the beginning.
  138. jest.advanceTimersByTime(5000);
  139. await playPromise;
  140. // @ts-expect-error private
  141. expect(inst._currentIndex).toEqual(5);
  142. // `currentTime` is in seconds
  143. // @ts-expect-error private
  144. expect(inst.getVideo(inst._currentIndex)?.currentTime).toEqual(5);
  145. });
  146. it('initially only loads videos from 0 to BUFFER', async () => {
  147. const root = document.createElement('div');
  148. const inst = new VideoReplayer(attachments, {
  149. videoApiPrefix: '/foo/',
  150. root,
  151. start: 0,
  152. onFinished: jest.fn(),
  153. onLoaded: jest.fn(),
  154. onBuffer: jest.fn(),
  155. durationMs: 40000,
  156. });
  157. const playPromise = inst.play(0);
  158. jest.advanceTimersByTime(2500);
  159. await playPromise;
  160. // @ts-expect-error private
  161. expect(inst._currentIndex).toEqual(0);
  162. // @ts-expect-error private
  163. expect(inst._videos.size).toEqual(3);
  164. });
  165. it('should load the correct videos after playing at a timestamp', async () => {
  166. const root = document.createElement('div');
  167. const inst = new VideoReplayer(attachments.concat(extra), {
  168. videoApiPrefix: '/foo/',
  169. root,
  170. start: 0,
  171. onFinished: jest.fn(),
  172. onLoaded: jest.fn(),
  173. onBuffer: jest.fn(),
  174. durationMs: 50000,
  175. });
  176. // play at segment 7
  177. const playPromise = inst.play(45_003);
  178. jest.advanceTimersByTime(2500);
  179. await playPromise;
  180. // @ts-expect-error private
  181. expect(inst._currentIndex).toEqual(7);
  182. // videos loaded should be [0, 1, 2, 4, 5, 6, 7]
  183. // since we have [0, 1, 2] preloaded initially
  184. // and only [4, 5, 6, 7] loaded when segment 7 is requested
  185. // @ts-expect-error private
  186. const videos = inst._videos;
  187. // @ts-expect-error private
  188. const getVideo = index => inst.getVideo(index);
  189. expect(videos.size).toEqual(7);
  190. expect(videos.get(0)).toEqual(getVideo(0));
  191. expect(videos.get(2)).toEqual(getVideo(2));
  192. expect(videos.get(3)).toEqual(undefined);
  193. expect(videos.get(4)).toEqual(getVideo(4));
  194. expect(videos.get(7)).toEqual(getVideo(7));
  195. });
  196. it('should work correctly if we have missing segments', async () => {
  197. const root = document.createElement('div');
  198. const inst = new VideoReplayer(attachments.concat(skip), {
  199. videoApiPrefix: '/foo/',
  200. root,
  201. start: 0,
  202. onFinished: jest.fn(),
  203. onLoaded: jest.fn(),
  204. onBuffer: jest.fn(),
  205. durationMs: 55000,
  206. });
  207. // play at segment 7
  208. const playPromise = inst.play(45_003);
  209. jest.advanceTimersByTime(2500);
  210. await playPromise;
  211. // @ts-expect-error private
  212. expect(inst._currentIndex).toEqual(6);
  213. // @ts-expect-error private
  214. const videos = inst._videos;
  215. // @ts-expect-error private
  216. const getVideo = index => inst.getVideo(index);
  217. // videos loaded should be [0, 1, 2, 3, 4, 5, 7, 8]
  218. expect(videos.size).toEqual(8);
  219. expect(videos.get(0)).toEqual(getVideo(0));
  220. expect(videos.get(2)).toEqual(getVideo(2));
  221. expect(videos.get(5)).toEqual(getVideo(5));
  222. expect(videos.get(6)).toEqual(getVideo(6));
  223. expect(videos.get(7)).toEqual(getVideo(7));
  224. });
  225. });
  226. describe('VideoReplayer - with starting gap', () => {
  227. beforeEach(() => {
  228. jest.clearAllTimers();
  229. });
  230. const attachments = [
  231. {
  232. id: 0,
  233. timestamp: 2500,
  234. duration: 5000,
  235. },
  236. // no gap
  237. {
  238. id: 1,
  239. timestamp: 5000,
  240. duration: 5000,
  241. },
  242. {
  243. id: 2,
  244. timestamp: 10_001,
  245. duration: 5000,
  246. },
  247. // 5 second gap
  248. {
  249. id: 3,
  250. timestamp: 20_000,
  251. duration: 5000,
  252. },
  253. // 5 second gap
  254. {
  255. id: 4,
  256. timestamp: 30_000,
  257. duration: 5000,
  258. },
  259. {
  260. id: 5,
  261. timestamp: 35_002,
  262. duration: 5000,
  263. },
  264. ];
  265. it('plays and seeks before replay starts', async () => {
  266. const root = document.createElement('div');
  267. const inst = new VideoReplayer(attachments, {
  268. videoApiPrefix: '/foo/',
  269. root,
  270. start: 0,
  271. onFinished: jest.fn(),
  272. onLoaded: jest.fn(),
  273. onBuffer: jest.fn(),
  274. durationMs: 40000,
  275. });
  276. // @ts-expect-error private
  277. expect(inst._currentIndex).toEqual(0);
  278. const playPromise = inst.play(1500);
  279. jest.advanceTimersByTime(2000);
  280. await playPromise;
  281. // @ts-expect-error private
  282. expect(inst._currentIndex).toEqual(0);
  283. // `currentTime` is in seconds
  284. // @ts-expect-error private
  285. expect(inst.getVideo(inst._currentIndex)?.currentTime).toEqual(0);
  286. });
  287. it('seeks to a gap in a video', async () => {
  288. const root = document.createElement('div');
  289. const inst = new VideoReplayer(attachments, {
  290. videoApiPrefix: '/foo/',
  291. root,
  292. start: 0,
  293. onFinished: jest.fn(),
  294. onLoaded: jest.fn(),
  295. onBuffer: jest.fn(),
  296. durationMs: 40000,
  297. });
  298. const playPromise = inst.play(18100);
  299. // @ts-expect-error private
  300. const video = inst.getVideo(2)!;
  301. // the replay is actually stopped right now to wait for loading
  302. fireEvent(video, createEvent.loadedData(video));
  303. // 15000 -> 20000 is a gap, so player should start playing @ index 3, from
  304. // the beginning.
  305. jest.advanceTimersByTime(2500);
  306. await playPromise;
  307. // @ts-expect-error private
  308. expect(inst._currentIndex).toEqual(3);
  309. // `currentTime` is in seconds
  310. // @ts-expect-error private
  311. expect(inst.getVideo(inst._currentIndex)?.currentTime).toEqual(0);
  312. });
  313. it('seeks past end of the replay', async () => {
  314. const root = document.createElement('div');
  315. const inst = new VideoReplayer(attachments, {
  316. videoApiPrefix: '/foo/',
  317. root,
  318. start: 0,
  319. // Unfortunately, `video.play()` is not implemented in jsdom, so no events,
  320. // so can't check that onFinished is called
  321. onFinished: jest.fn(),
  322. onLoaded: jest.fn(),
  323. onBuffer: jest.fn(),
  324. durationMs: 40000,
  325. });
  326. const playPromise = inst.play(50000);
  327. // 15000 -> 20000 is a gap, so player should start playing @ index 3, from
  328. // the beginning.
  329. jest.advanceTimersByTime(5000);
  330. await playPromise;
  331. // @ts-expect-error private
  332. expect(inst._currentIndex).toEqual(5);
  333. // `currentTime` is in seconds
  334. // @ts-expect-error private
  335. expect(inst.getVideo(inst._currentIndex)?.currentTime).toEqual(5);
  336. });
  337. });
  338. describe('VideoReplayer - with ending gap', () => {
  339. beforeEach(() => {
  340. jest.clearAllTimers();
  341. });
  342. const attachments = [
  343. {
  344. id: 0,
  345. timestamp: 2500,
  346. duration: 5000,
  347. },
  348. // no gap
  349. {
  350. id: 1,
  351. timestamp: 5000,
  352. duration: 5000,
  353. },
  354. {
  355. id: 2,
  356. timestamp: 10_001,
  357. duration: 5000,
  358. },
  359. // 5 second gap
  360. {
  361. id: 3,
  362. timestamp: 20_000,
  363. duration: 5000,
  364. },
  365. // 5 second gap
  366. {
  367. id: 4,
  368. timestamp: 30_000,
  369. duration: 5000,
  370. },
  371. {
  372. id: 5,
  373. timestamp: 35_002,
  374. duration: 5000,
  375. },
  376. ];
  377. it('keeps playing until the end if there is an ending gap', async () => {
  378. const root = document.createElement('div');
  379. const inst = new VideoReplayer(attachments, {
  380. videoApiPrefix: '/foo/',
  381. root,
  382. start: 0,
  383. onFinished: jest.fn(),
  384. onLoaded: jest.fn(),
  385. onBuffer: jest.fn(),
  386. durationMs: 50000,
  387. });
  388. // actual length of the segments is 40s
  389. // 10s gap at the end
  390. // play at the last segment
  391. const playPromise = inst.play(36000);
  392. await playPromise;
  393. jest.advanceTimersByTime(4000);
  394. // we're still within the last segment (5)
  395. // @ts-expect-error private
  396. expect(inst._currentIndex).toEqual(5);
  397. expect(inst.getCurrentTime()).toEqual(40000);
  398. // now we are in the gap
  399. // timer should still be going since the duration is 50s
  400. jest.advanceTimersByTime(5000);
  401. // @ts-expect-error private
  402. expect(inst._isPlaying).toEqual(true);
  403. // a long time passes
  404. // ensure the timer stops at the end duration (50s)
  405. jest.advanceTimersByTime(60000);
  406. expect(inst.getCurrentTime()).toEqual(50000);
  407. // @ts-expect-error private
  408. expect(inst._isPlaying).toEqual(false);
  409. });
  410. it('ends at the proper time if seeking into a gap at the end', async () => {
  411. const root = document.createElement('div');
  412. const inst = new VideoReplayer(attachments, {
  413. videoApiPrefix: '/foo/',
  414. root,
  415. start: 0,
  416. onFinished: jest.fn(),
  417. onLoaded: jest.fn(),
  418. onBuffer: jest.fn(),
  419. durationMs: 50000,
  420. });
  421. // actual length of the segments is 40s
  422. // 10s gap at the end
  423. // play at the gap
  424. const playPromise = inst.play(40002);
  425. await playPromise;
  426. jest.advanceTimersByTime(4000);
  427. // we should be still playing in the gap
  428. expect(inst.getCurrentTime()).toEqual(44002);
  429. // @ts-expect-error private
  430. expect(inst._isPlaying).toEqual(true);
  431. // a long time passes
  432. // ensure the timer stops at the end duration (50s)
  433. jest.advanceTimersByTime(60000);
  434. expect(inst.getCurrentTime()).toBeLessThan(50100);
  435. // @ts-expect-error private
  436. expect(inst._isPlaying).toEqual(false);
  437. });
  438. });