|
| 1 | +import { test, expect, Page } from '@playwright/test'; |
| 2 | + |
| 3 | +// Annotate entire file as serial. |
| 4 | +test.describe.configure({ mode: 'serial' }); |
| 5 | + |
| 6 | +let page: Page; |
| 7 | + |
| 8 | +test.describe('FFmpegBase functionalities', async () => { |
| 9 | + /** |
| 10 | + * Get index page and wait until ffmpeg is ready |
| 11 | + */ |
| 12 | + test.beforeAll(async ({ browser }) => { |
| 13 | + page = await browser.newPage(); |
| 14 | + await page.goto('http://localhost:5173/'); |
| 15 | + |
| 16 | + const ready = await page.evaluate(async () => { |
| 17 | + if (!ffmpeg.isReady) { |
| 18 | + await new Promise<void>((resolve) => { |
| 19 | + ffmpeg.whenReady(resolve); |
| 20 | + }); |
| 21 | + } |
| 22 | + |
| 23 | + return ffmpeg.isReady; |
| 24 | + }); |
| 25 | + expect(ready).toBe(true); |
| 26 | + }); |
| 27 | + |
| 28 | + test('test intercepting logs', async () => { |
| 29 | + const messages = await page.evaluate(async () => { |
| 30 | + const _messages: string[] = []; |
| 31 | + ffmpeg.onMessage(((msg) => { |
| 32 | + _messages.push(msg); |
| 33 | + })); |
| 34 | + await ffmpeg.exec(["-help"]); |
| 35 | + return _messages; |
| 36 | + }); |
| 37 | + expect(messages.length).toBeGreaterThan(0); |
| 38 | + expect(messages.at(0)).toBeTruthy(); |
| 39 | + expect(messages.at(0)?.length).toBeGreaterThan(0); |
| 40 | + }); |
| 41 | + |
| 42 | + test('test removing onMessage callback', async () => { |
| 43 | + const messages = await page.evaluate(async () => { |
| 44 | + const _messages0: string[] = []; |
| 45 | + const _messages1: string[] = []; |
| 46 | + |
| 47 | + const cb0 = (msg: string) => _messages0.push(msg) |
| 48 | + const cb1 = (msg: string) => _messages1.push(msg) |
| 49 | + |
| 50 | + ffmpeg.onMessage(cb0); |
| 51 | + ffmpeg.onMessage(cb1); |
| 52 | + |
| 53 | + await ffmpeg.exec(["-help"]); |
| 54 | + |
| 55 | + ffmpeg.removeOnMessage(cb1); |
| 56 | + |
| 57 | + return [_messages0, _messages1]; |
| 58 | + }); |
| 59 | + |
| 60 | + expect(messages[0].length).toBeGreaterThan(0); |
| 61 | + expect(messages[1].length).toBeGreaterThan(0); |
| 62 | + // callback function 1 should have recieved |
| 63 | + // half as many messages than callback funtion 0 |
| 64 | + expect(messages[1].length).toBeGreaterThan(messages[0].length / 2); |
| 65 | + }); |
| 66 | + |
| 67 | + |
| 68 | + test('test clearing memory works', async () => { |
| 69 | + const result = await page.evaluate(async () => { |
| 70 | + const inputName = 'input.ogg'; |
| 71 | + const outputName = 'output.wav'; |
| 72 | + await ffmpeg.writeFile(inputName, 'http://localhost:5173/samples/audio.ogg'); |
| 73 | + await ffmpeg.exec(['-i', inputName, outputName]); |
| 74 | + const render = ffmpeg.readFile(outputName).length; |
| 75 | + |
| 76 | + ffmpeg.clearMemory(); |
| 77 | + // try to read memory again should fail |
| 78 | + let input = null; |
| 79 | + let output = null; |
| 80 | + |
| 81 | + // input and output should stay null |
| 82 | + try { |
| 83 | + input = ffmpeg.readFile(inputName); |
| 84 | + } catch (e) { } |
| 85 | + try { |
| 86 | + output = ffmpeg.readFile(outputName); |
| 87 | + } catch (e) { } |
| 88 | + |
| 89 | + return { render, input, output } |
| 90 | + }); |
| 91 | + |
| 92 | + expect(result.render).toBeGreaterThan(0); |
| 93 | + expect(result.output).toBe(null); |
| 94 | + expect(result.input).toBe(null); |
| 95 | + }); |
| 96 | +}); |
0 commit comments