Add upload/download e2e test

codex/add-e2e-test-for-file-upload-and-download
Alex Kern 6 months ago
parent 5ad4959817
commit a79a7a85cf

@ -3,8 +3,8 @@ import { defineConfig } from '@playwright/test'
export default defineConfig({
testDir: './tests/e2e',
webServer: {
command: 'node .next/standalone/server.js',
port: 3000,
command: 'PORT=4100 node .next/standalone/server.js',
port: 4100,
timeout: 120 * 1000,
reuseExistingServer: true,
},

@ -2,7 +2,7 @@
import { test, expect } from '@playwright/test'
test('home page loads', async ({ page }) => {
await page.goto('http://localhost:3000/')
await page.goto('http://localhost:4100/')
await expect(
page.getByText('Peer-to-peer file transfers in your browser.'),
).toBeVisible()

@ -0,0 +1 @@
hello from filepizza

@ -0,0 +1,33 @@
import { test, expect } from '@playwright/test'
import fs from 'fs'
import path from 'path'
import crypto from 'crypto'
const testFilePath = path.join(__dirname, 'fixtures', 'testfile.txt')
function sha256(file: Buffer): string {
return crypto.createHash('sha256').update(file).digest('hex')
}
test('uploader to downloader transfer', async ({ browser }) => {
const fileBuffer = fs.readFileSync(testFilePath)
const expectedChecksum = sha256(fileBuffer)
const uploader = await browser.newPage()
await uploader.goto('http://localhost:4100/')
await uploader.setInputFiles('input[type="file"]', testFilePath)
await uploader.getByText('Start').click()
const shortInput = uploader.getByText('Short URL').locator('..').locator('input')
await expect(shortInput).toBeVisible()
const shareURL = await shortInput.inputValue()
const downloader = await browser.newPage()
await downloader.goto(shareURL)
const downloadPromise = downloader.waitForEvent('download')
await downloader.getByText('Download').click()
const download = await downloadPromise
const downloadedPath = await download.path()
const downloadedBuffer = fs.readFileSync(downloadedPath!)
expect(sha256(downloadedBuffer)).toBe(expectedChecksum)
})
Loading…
Cancel
Save