Path: blob/main/full-client/test/filechooser.test.ts
1028 views
import { Helpers } from '@secret-agent/testing';1import { createPromise } from '@secret-agent/commons/utils';2import Core from '@secret-agent/core';3import CoreServer from '@secret-agent/core/server';4import HumanEmulator from '@secret-agent/plugin-utils/lib/HumanEmulator';5import * as Fs from 'fs';6import { Handler } from '../index';78let koaServer;9let handler: Handler;10beforeAll(async () => {11const coreServer = new CoreServer();12await coreServer.listen({ port: 0 });13Core.use(14class BasicHumanEmulator extends HumanEmulator {15static id = 'basic';16},17);18handler = new Handler({ maxConcurrency: 1, host: await coreServer.address });19Helpers.onClose(() => {20handler.close();21coreServer.close();22}, true);23koaServer = await Helpers.runKoaServer();24});25afterAll(Helpers.afterAll);26afterEach(Helpers.afterEach);2728describe('Filechooser tests', () => {29it('can upload a file by path', async () => {30const didSubmit = createPromise<void>();31koaServer.post('/upload', koaServer.upload.single('file'), ctx => {32ctx.body = 'Ok';33expect(ctx.file).toBeTruthy();34expect(ctx.file.originalname).toBe('worker.js');35expect(ctx.file.mimetype).toBe('text/javascript');36didSubmit.resolve();37});38koaServer.get('/get-upload', ctx => {39ctx.body = `<html>40<body>41<h1>Upload form</h1>42<form name="upload" action="/upload" method="post" enctype="multipart/form-data">43<input type="file" name="file" id="files" />44<input type="submit" name="Go" id="submitter">45</body>46</html>`;47});4849const agent = await handler.createAgent({ humanEmulatorId: 'basic' });50Helpers.needsClosing.push(agent);5152await agent.goto(`${koaServer.baseUrl}/get-upload`);53await agent.waitForPaintingStable();54const input = await agent.document.querySelector('#files');55await agent.click(input);56const chooser = await agent.waitForFileChooser();57await chooser.chooseFiles(`${__dirname}/html/worker.js`);5859await expect(input.files.length).resolves.toBe(1);60const body = await input.files.item(0).text();61const actualText = await Fs.promises.readFile(`${__dirname}/html/worker.js`, 'utf8');6263expect(body).toBe(actualText);64await expect(input.files.item(0).type).resolves.toBe('text/javascript');6566await agent.click(agent.document.querySelector('#submitter'));67await didSubmit.promise;68});6970it('can trigger file upload when a user profile is set', async () => {71const didSubmit = createPromise<void>();7273koaServer.get('/get-upload-profile', ctx => {74ctx.body = `<html>75<body>76<h1>Upload form</h1>77<form name="upload" action="/upload" method="post" enctype="multipart/form-data">78<input type="file" name="file" id="files" />79<input type="submit" name="Go" id="submitter">80</body>81</html>`;82});8384const agent = await handler.createAgent({85humanEmulatorId: 'basic',86userProfile: {87cookies: [],88storage: {89'https://domain1.com': {90indexedDB: [],91localStorage: [],92sessionStorage: [['2', '1']],93},94'https://domain2.com': {95indexedDB: [],96localStorage: [],97sessionStorage: [['1', '2']],98},99},100},101});102Helpers.needsClosing.push(agent);103104await agent.goto(`${koaServer.baseUrl}/get-upload-profile`);105await agent.waitForPaintingStable();106const input = await agent.document.querySelector('#files');107await agent.click(input);108const chooser = await agent.waitForFileChooser();109expect(chooser).toBeTruthy();110});111112it('can upload multiple files', async () => {113const didSubmit = createPromise<void>();114koaServer.post('/upload-multi', koaServer.upload.array('sourcefiles'), ctx => {115ctx.body = 'Ok';116expect(ctx.files).toHaveLength(2);117expect(ctx.files[0].originalname).toBe('filechooser.test.js');118expect(ctx.files[0].mimetype).toBe('text/javascript');119didSubmit.resolve();120});121koaServer.get('/get-upload-multi', ctx => {122ctx.body = `<html>123<body>124<h1>Upload form</h1>125<form name="upload" action="/upload-multi" method="post" enctype="multipart/form-data">126<input type="file" name="sourcefiles" id="sourcefiles" multiple="multiple" />127<input type="submit" >128</body>129</html>`;130});131132const agent = await handler.createAgent({133humanEmulatorId: 'basic',134});135Helpers.needsClosing.push(agent);136137const file1 = await Fs.promises.readFile(`${__dirname}/filechooser.test.js`);138const file2 = await Fs.promises.readFile(`${__dirname}/filechooser.test.js.map`);139140await agent.goto(`${koaServer.baseUrl}/get-upload-multi`);141await agent.waitForPaintingStable();142const input = await agent.document.querySelector('#sourcefiles');143await agent.click(input);144const chooser = await agent.waitForFileChooser();145await chooser.chooseFiles(146{ data: file1, name: 'filechooser.test.js' },147{ data: file2, name: 'filechooser.test.js.map' },148);149150await expect(input.files.length).resolves.toBe(2);151const body = await input.files.item(0).arrayBuffer();152153expect(body).toStrictEqual(file1);154await expect(input.files.item(0).type).resolves.toBe('text/javascript');155156await agent.click(agent.document.querySelector('input[type=submit]'));157await didSubmit.promise;158});159});160161162