Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/full-client/test/output.test.ts
1029 views
1
import { Helpers } from '@secret-agent/testing';
2
import { ITestKoaServer } from '@secret-agent/testing/helpers';
3
import Core from '@secret-agent/core';
4
import SessionDb from '@secret-agent/core/dbs/SessionDb';
5
import GlobalPool from '@secret-agent/core/lib/GlobalPool';
6
import { Handler, Observable } from '../index';
7
8
let koaServer: ITestKoaServer;
9
let handler: Handler;
10
beforeAll(async () => {
11
await Core.start();
12
handler = new Handler({ host: await Core.server.address });
13
Helpers.onClose(() => handler.close(), true);
14
koaServer = await Helpers.runKoaServer();
15
});
16
afterAll(Helpers.afterAll);
17
afterEach(Helpers.afterEach);
18
19
describe('Output tests', () => {
20
test('records object changes', async () => {
21
const agent = await openBrowser('/');
22
const output = agent.output;
23
output.started = new Date();
24
const url = await agent.url;
25
const title = await agent.document.title;
26
output.page = {
27
url,
28
title,
29
};
30
output.page.data = Buffer.from('I am buffer');
31
const sessionId = await agent.sessionId;
32
await agent.close();
33
34
const db = new SessionDb(GlobalPool.sessionsDir, sessionId, { readonly: true });
35
const outputs = db.output.all();
36
expect(outputs).toHaveLength(3);
37
expect(outputs[0]).toEqual({
38
type: 'insert',
39
value: expect.any(String),
40
timestamp: expect.any(Number),
41
lastCommandId: expect.any(Number),
42
path: '["started"]',
43
});
44
expect(outputs[1]).toEqual({
45
type: 'insert',
46
value: JSON.stringify({ url: `${koaServer.baseUrl}/`, title: 'Example Domain' }),
47
timestamp: expect.any(Number),
48
lastCommandId: 4,
49
path: '["page"]',
50
});
51
expect(outputs[2]).toEqual({
52
type: 'insert',
53
value: JSON.stringify(Buffer.from('I am buffer').toString('base64')),
54
timestamp: expect.any(Number),
55
lastCommandId: 4,
56
path: '["page","data"]',
57
});
58
expect(JSON.stringify(output)).toEqual(
59
JSON.stringify({
60
started: output.started,
61
page: {
62
url,
63
title,
64
data: Buffer.from('I am buffer').toString('base64'),
65
},
66
}),
67
);
68
});
69
70
test('can add array-ish items to the main object', async () => {
71
const agent = await openBrowser('/');
72
const output = agent.output;
73
const date = new Date();
74
output.push({
75
url: 'https://url.com',
76
title: 'Page',
77
date,
78
buffer: Buffer.from('whatever'),
79
});
80
const sessionId = await agent.sessionId;
81
await agent.close();
82
83
const db = new SessionDb(GlobalPool.sessionsDir, sessionId, { readonly: true });
84
const outputs = db.output.all();
85
expect(outputs).toHaveLength(1);
86
expect(outputs[0]).toEqual({
87
type: 'insert',
88
value: JSON.stringify({
89
url: 'https://url.com',
90
title: 'Page',
91
date,
92
buffer: Buffer.from('whatever').toString('base64'),
93
}),
94
timestamp: expect.any(Number),
95
lastCommandId: 2,
96
path: '[0]',
97
});
98
expect(JSON.stringify(output)).toEqual(
99
JSON.stringify([
100
{
101
url: 'https://url.com',
102
title: 'Page',
103
date,
104
buffer: Buffer.from('whatever').toString('base64'),
105
},
106
]),
107
);
108
});
109
110
test('can add observables directly', async () => {
111
const agent = await openBrowser('/');
112
const output = agent.output;
113
const record = Observable({} as any);
114
output.push(record);
115
record.test = 1;
116
record.watch = 2;
117
record.any = { more: true };
118
const sessionId = await agent.sessionId;
119
await agent.close();
120
121
const db = new SessionDb(GlobalPool.sessionsDir, sessionId, { readonly: true });
122
const outputs = db.output.all();
123
expect(outputs).toHaveLength(4);
124
expect(outputs[0]).toEqual({
125
type: 'insert',
126
value: JSON.stringify({}),
127
timestamp: expect.any(Number),
128
lastCommandId: 2,
129
path: '[0]',
130
});
131
expect(JSON.stringify(output)).toEqual(
132
JSON.stringify([
133
{
134
test: 1,
135
watch: 2,
136
any: { more: true },
137
},
138
]),
139
);
140
});
141
142
test('can replace the main object', async () => {
143
const agent = await openBrowser('/');
144
agent.output.test = 'true';
145
agent.output = {
146
try: true,
147
another: false,
148
};
149
const sessionId = await agent.sessionId;
150
await agent.close();
151
152
const db = new SessionDb(GlobalPool.sessionsDir, sessionId, { readonly: true });
153
const outputs = db.output.all();
154
expect(outputs).toHaveLength(4);
155
expect(outputs[0]).toEqual({
156
type: 'insert',
157
value: JSON.stringify('true'),
158
timestamp: expect.any(Number),
159
lastCommandId: 2,
160
path: '["test"]',
161
});
162
expect(outputs[1]).toEqual({
163
type: 'delete',
164
value: null,
165
timestamp: expect.any(Number),
166
lastCommandId: 2,
167
path: '["test"]',
168
});
169
expect(outputs[2]).toEqual({
170
type: 'insert',
171
value: JSON.stringify(true),
172
timestamp: expect.any(Number),
173
lastCommandId: 2,
174
path: '["try"]',
175
});
176
expect(JSON.stringify(agent.output)).toEqual(
177
JSON.stringify({
178
try: true,
179
another: false,
180
}),
181
);
182
});
183
});
184
185
async function openBrowser(path: string) {
186
const agent = await handler.createAgent();
187
Helpers.needsClosing.push(agent);
188
await agent.goto(`${koaServer.baseUrl}${path}`);
189
await agent.waitForPaintingStable();
190
return agent;
191
}
192
193