|
1 | | -import { Buffer } from "node:buffer"; |
2 | 1 | import path from "node:path"; |
3 | 2 | import { chromium, expect, test } from "@playwright/test"; |
4 | 3 |
|
5 | | -function createSilentWav(durationSeconds: number) { |
6 | | - const sampleRate = 8_000; |
7 | | - const channelCount = 1; |
8 | | - const bytesPerSample = 2; |
9 | | - const dataSize = durationSeconds * sampleRate * bytesPerSample; |
10 | | - const buffer = Buffer.alloc(44 + dataSize); |
11 | | - |
12 | | - buffer.write("RIFF", 0); |
13 | | - buffer.writeUInt32LE(36 + dataSize, 4); |
14 | | - buffer.write("WAVEfmt ", 8); |
15 | | - buffer.writeUInt32LE(16, 16); |
16 | | - buffer.writeUInt16LE(1, 20); |
17 | | - buffer.writeUInt16LE(channelCount, 22); |
18 | | - buffer.writeUInt32LE(sampleRate, 24); |
19 | | - buffer.writeUInt32LE(sampleRate * channelCount * bytesPerSample, 28); |
20 | | - buffer.writeUInt16LE(channelCount * bytesPerSample, 32); |
21 | | - buffer.writeUInt16LE(bytesPerSample * 8, 34); |
22 | | - buffer.write("data", 36); |
23 | | - buffer.writeUInt32LE(dataSize, 40); |
24 | | - return buffer; |
25 | | -} |
26 | | - |
27 | 4 | test("loads the FAB, uploads audio, and survives YouTube navigation", async () => { |
28 | 5 | const extensionPath = path.resolve("dist/extension"); |
| 6 | + |
| 7 | + await using disposables = new AsyncDisposableStack(); |
29 | 8 | const context = await chromium.launchPersistentContext("", { |
30 | 9 | channel: "chromium", |
31 | 10 | args: [ |
32 | 11 | `--disable-extensions-except=${extensionPath}`, |
33 | 12 | `--load-extension=${extensionPath}`, |
34 | 13 | ], |
35 | 14 | }); |
| 15 | + disposables.defer(() => context.close()); |
36 | 16 |
|
37 | | - try { |
38 | | - const page = await context.newPage(); |
39 | | - await page.goto("https://www.youtube.com/watch?v=7GU_VQfgMT0", { |
40 | | - waitUntil: "domcontentloaded", |
41 | | - timeout: 30_000, |
42 | | - }); |
| 17 | + const page = await context.newPage(); |
| 18 | + await page.goto("https://www.youtube.com/watch?v=7GU_VQfgMT0", { |
| 19 | + waitUntil: "domcontentloaded", |
| 20 | + timeout: 30_000, |
| 21 | + }); |
43 | 22 |
|
44 | | - const host = page.locator("#youtube-external-audio-host"); |
45 | | - await expect(host).toBeAttached({ timeout: 15_000 }); |
46 | | - await host |
47 | | - .getByRole("button", { name: "Show external audio controls" }) |
48 | | - .click(); |
49 | | - await expect( |
50 | | - host.getByText("External audio", { exact: true }), |
51 | | - ).toBeVisible(); |
| 23 | + const host = page.locator("#youtube-external-audio-host"); |
| 24 | + await expect(host).toBeAttached({ timeout: 15_000 }); |
| 25 | + await host |
| 26 | + .getByRole("button", { name: "Show external audio controls" }) |
| 27 | + .click(); |
| 28 | + await expect(host.getByText("External audio", { exact: true })).toBeVisible(); |
52 | 29 |
|
53 | | - await host.locator('input[type="file"]').setInputFiles({ |
54 | | - name: "silent.wav", |
55 | | - mimeType: "audio/wav", |
56 | | - buffer: createSilentWav(240), |
57 | | - }); |
58 | | - await expect(host.getByText("silent.wav", { exact: true })).toBeVisible(); |
| 30 | + await host |
| 31 | + .locator('input[type="file"]') |
| 32 | + .setInputFiles(path.resolve("../../fixtures/sine-2s.wav")); |
| 33 | + await expect(host.getByText("sine-2s.wav", { exact: true })).toBeVisible(); |
59 | 34 |
|
60 | | - await page.evaluate(() => { |
61 | | - document.dispatchEvent(new Event("yt-navigate-start")); |
62 | | - history.pushState({}, "", "/watch?v=spa-navigation-test"); |
63 | | - document.dispatchEvent(new Event("yt-navigate-finish")); |
64 | | - }); |
65 | | - await expect(host).toBeAttached(); |
66 | | - await expect( |
67 | | - host.getByRole("button", { name: "Show external audio controls" }), |
68 | | - ).toBeVisible(); |
69 | | - } finally { |
70 | | - await context.close(); |
71 | | - } |
| 35 | + await page.evaluate(() => { |
| 36 | + document.dispatchEvent(new Event("yt-navigate-start")); |
| 37 | + history.pushState({}, "", "/watch?v=spa-navigation-test"); |
| 38 | + document.dispatchEvent(new Event("yt-navigate-finish")); |
| 39 | + }); |
| 40 | + await expect(host).toBeAttached(); |
| 41 | + await expect( |
| 42 | + host.getByRole("button", { name: "Show external audio controls" }), |
| 43 | + ).toBeVisible(); |
72 | 44 | }); |
0 commit comments