Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/core/test/basic.test.ts
1029 views
1
import { Helpers } from '@secret-agent/testing/index';
2
import Core, { GlobalPool } from '../index';
3
import Session from '../lib/Session';
4
5
const shutdownSpy = jest.spyOn(Core, 'shutdown');
6
beforeEach(() => {
7
GlobalPool.maxConcurrentAgentsCount = 10;
8
});
9
afterEach(Helpers.afterEach);
10
afterAll(Helpers.afterAll);
11
12
describe('basic Core tests', () => {
13
it('starts, configures, and shuts down', async () => {
14
const connection = Core.addConnection();
15
Helpers.onClose(() => connection.disconnect());
16
await connection.connect({ maxConcurrentAgentsCount: 5 });
17
18
expect(GlobalPool.maxConcurrentAgentsCount).toBe(5);
19
expect(GlobalPool.activeSessionCount).toBe(0);
20
21
await Core.shutdown();
22
});
23
24
it('runs createTab', async () => {
25
const connection = Core.addConnection();
26
Helpers.onClose(() => connection.disconnect());
27
await connection.connect({ maxConcurrentAgentsCount: 2 });
28
await connection.createSession();
29
30
expect(GlobalPool.maxConcurrentAgentsCount).toBe(2);
31
expect(GlobalPool.activeSessionCount).toBe(1);
32
33
await Core.shutdown();
34
expect(GlobalPool.activeSessionCount).toBe(0);
35
});
36
37
it('shuts down if connect set to be not persistent and Core.start not called', async () => {
38
shutdownSpy.mockClear();
39
40
const connection = Core.addConnection();
41
await connection.connect({ isPersistent: false });
42
Helpers.onClose(() => connection.disconnect());
43
const connectionCloseSpy = jest.spyOn(connection, 'disconnect');
44
connection.autoShutdownMillis = 0;
45
46
const { sessionId } = await connection.createSession();
47
await Session.get(sessionId).close();
48
49
await new Promise(resolve => setTimeout(resolve, 100));
50
51
expect(shutdownSpy).toHaveBeenCalledTimes(1);
52
expect(connectionCloseSpy).toHaveBeenCalled();
53
await Core.shutdown();
54
expect(GlobalPool.activeSessionCount).toBe(0);
55
});
56
57
it('will not shutdown if start called and there are no open connections', async () => {
58
shutdownSpy.mockClear();
59
await Core.start();
60
61
const connection = Core.addConnection();
62
await connection.connect();
63
const connectionCloseSpy = jest.spyOn(connection, 'disconnect');
64
65
const { sessionId } = await connection.createSession();
66
await Session.get(sessionId).close();
67
68
await connection.disconnect();
69
70
expect(shutdownSpy).toHaveBeenCalledTimes(0);
71
expect(connectionCloseSpy).toHaveBeenCalledTimes(1);
72
await Core.shutdown();
73
expect(GlobalPool.activeSessionCount).toBe(0);
74
});
75
});
76
77