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