Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/core/test/connection.test.ts
1029 views
1
import { Helpers } from '@secret-agent/testing';
2
import agent, { Agent, Handler } from '@secret-agent/client';
3
import * as http from 'http';
4
import { Log } from '@secret-agent/commons/Logger';
5
import BrowserEmulator from '@secret-agent/default-browser-emulator';
6
import { DependenciesMissingError } from '@secret-agent/chrome-app/lib/DependenciesMissingError';
7
import { DependencyInstaller } from '@secret-agent/chrome-app/lib/DependencyInstaller';
8
import ChromeApp from '@secret-agent/chrome-app/index';
9
import Core from '../index';
10
import CoreServer from '../server';
11
12
const validate = jest.spyOn(DependencyInstaller.prototype, 'validate');
13
const logError = jest.spyOn(Log.prototype, 'error');
14
15
let httpServer: Helpers.ITestHttpServer<http.Server>;
16
let coreServer: CoreServer;
17
18
beforeAll(async () => {
19
httpServer = await Helpers.runHttpServer({ onlyCloseOnFinal: true });
20
coreServer = new CoreServer();
21
Helpers.onClose(() => coreServer.close(), true);
22
await coreServer.listen({ port: 0 });
23
});
24
afterAll(Helpers.afterAll);
25
afterEach(Helpers.afterEach);
26
27
describe('basic connection tests', () => {
28
it('should goto and waitForLocation', async () => {
29
// bind a core server to core
30
31
const handler = new Handler({
32
host: await coreServer.address,
33
});
34
const handlerAgent = await handler.createAgent();
35
const sessionId = await handlerAgent.sessionId;
36
expect(sessionId).toBeTruthy();
37
38
const { url } = httpServer;
39
await handlerAgent.goto(url);
40
41
const html = await handlerAgent.document.documentElement.outerHTML;
42
expect(html).toBe('<html><head></head><body>Hello world</body></html>');
43
44
await handlerAgent.close();
45
await handler.close();
46
});
47
48
it('should be able to set a new connection on the default agent', async () => {
49
// bind a core server to core
50
await agent.configure({
51
connectionToCore: {
52
host: await coreServer.address,
53
},
54
});
55
const sessionId = await agent.sessionId;
56
expect(sessionId).toBeTruthy();
57
58
const { url } = httpServer;
59
await agent.goto(url);
60
61
const html = await agent.document.documentElement.outerHTML;
62
expect(html).toBe('<html><head></head><body>Hello world</body></html>');
63
64
await agent.close();
65
});
66
67
it('should be able to configure a new agent', async () => {
68
// bind a core server to core
69
const customAgent = new Agent({
70
connectionToCore: {
71
host: await coreServer.address,
72
},
73
});
74
const sessionId = await customAgent.sessionId;
75
expect(sessionId).toBeTruthy();
76
77
const { url } = httpServer;
78
await customAgent.goto(url);
79
80
const html = await customAgent.document.documentElement.outerHTML;
81
expect(html).toBe('<html><head></head><body>Hello world</body></html>');
82
83
await customAgent.close();
84
});
85
86
it('should throw an error informing how to install dependencies', async () => {
87
class CustomEmulator extends BrowserEmulator {
88
public static id = 'emulate-test';
89
public static selectBrowserMeta() {
90
return super.selectBrowserMeta();
91
}
92
93
public static onBrowserWillLaunch() {
94
// don't change launch args so it doesn't reuse a previous one
95
}
96
}
97
Core.use(CustomEmulator as any);
98
99
logError.mockClear();
100
validate.mockClear();
101
validate.mockImplementationOnce(() => {
102
throw new DependenciesMissingError(
103
`You can resolve this by running the apt dependency installer at:${ChromeApp.aptScriptPath}`,
104
'Chrome',
105
['libnacl'],
106
);
107
});
108
109
logError.mockImplementationOnce(() => null /* no op*/);
110
111
const agent1 = new Agent({
112
browserEmulatorId: 'emulate-test',
113
connectionToCore: {
114
host: await coreServer.address,
115
},
116
});
117
Helpers.needsClosing.push(agent1);
118
119
try {
120
await agent1;
121
} catch (err) {
122
// eslint-disable-next-line jest/no-try-expect
123
expect(String(err)).toMatch(
124
'CoreServer needs further setup to launch the browserEmulator. See server logs',
125
);
126
}
127
expect(logError).toHaveBeenCalledTimes(1);
128
const error = String((logError.mock.calls[0][1] as any).error);
129
expect(error).toMatch('PuppetLaunchError');
130
expect(error).toMatch('You can resolve this by running');
131
expect(validate).toHaveBeenCalledTimes(1);
132
});
133
});
134
135