Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/client/test/waitForResource.test.ts
1028 views
1
import IResourceMeta from '@secret-agent/interfaces/IResourceMeta';
2
import { Helpers } from '@secret-agent/testing/index';
3
import ICoreRequestPayload from '@secret-agent/interfaces/ICoreRequestPayload';
4
import ICoreResponsePayload from '@secret-agent/interfaces/ICoreResponsePayload';
5
import { Handler } from '../index';
6
import ConnectionToCore from '../connections/ConnectionToCore';
7
8
let payloadHandler: (payload: ICoreRequestPayload) => ICoreResponsePayload = () => null;
9
const outgoing = jest.fn(
10
async (payload: ICoreRequestPayload): Promise<ICoreResponsePayload> => {
11
const { command } = payload;
12
const response = payloadHandler(payload);
13
if (response) return response;
14
if (command === 'Session.create') {
15
return {
16
data: { tabId: 'tab-id', sessionId: 'session-id', sessionsDataLocation: '' },
17
};
18
}
19
if (command === 'Session.addEventListener') {
20
return {
21
data: { listenerId: 1 },
22
};
23
}
24
},
25
);
26
class Piper extends ConnectionToCore {
27
async internalSendRequest(payload: ICoreRequestPayload): Promise<void> {
28
const responsePayload = await outgoing(payload);
29
const response = <ICoreResponsePayload>{
30
responseId: payload.messageId,
31
...(responsePayload ?? {}),
32
};
33
this.onMessage(response);
34
}
35
36
protected createConnection = () => Promise.resolve(null);
37
protected destroyConnection = () => Promise.resolve(null);
38
}
39
40
beforeAll(() => {});
41
42
afterEach(Helpers.afterEach);
43
afterAll(Helpers.afterAll);
44
45
describe('waitForResource', () => {
46
it('should break after finding one resource', async () => {
47
payloadHandler = ({ command }: ICoreRequestPayload): ICoreResponsePayload => {
48
if (command === 'Tab.waitForResource') {
49
return { data: [{ id: 1, url: '/test.js' } as IResourceMeta] };
50
}
51
};
52
53
const handler = new Handler(new Piper());
54
Helpers.needsClosing.push(handler);
55
const agent = await handler.createAgent();
56
const resources = await agent.waitForResource({ url: '/test.js' });
57
expect(resources).toHaveLength(1);
58
await agent.close();
59
await handler.close();
60
});
61
62
it('should try more than once to get files', async () => {
63
let attempts = 0;
64
payloadHandler = ({ command }: ICoreRequestPayload): ICoreResponsePayload => {
65
if (command === 'Tab.waitForResource') {
66
attempts += 1;
67
if (attempts === 3) {
68
return { data: [{ id: 1, url: '/test2.js' } as IResourceMeta] };
69
}
70
return { data: [] };
71
}
72
};
73
74
const handler = new Handler(new Piper());
75
Helpers.needsClosing.push(handler);
76
const agent = await handler.createAgent();
77
const resources = await agent.waitForResource({ url: '/test2.js' });
78
expect(resources).toHaveLength(1);
79
expect(attempts).toBe(3);
80
81
await agent.close();
82
await handler.close();
83
});
84
85
it('should return multiple files if many match on one round trip', async () => {
86
payloadHandler = ({ command }: ICoreRequestPayload): ICoreResponsePayload => {
87
if (command === 'Tab.waitForResource') {
88
return {
89
data: [
90
{ id: 1, url: '/test3.js', type: 'Xhr' } as IResourceMeta,
91
{ id: 2, url: '/test4.js', type: 'Xhr' } as IResourceMeta,
92
],
93
};
94
}
95
};
96
97
const handler = new Handler(new Piper());
98
Helpers.needsClosing.push(handler);
99
const agent = await handler.createAgent();
100
const resources = await agent.waitForResource({ type: 'Xhr' });
101
expect(resources).toHaveLength(2);
102
103
await agent.close();
104
await handler.close();
105
});
106
107
it('should match multiple files by url', async () => {
108
payloadHandler = ({ command }: ICoreRequestPayload): ICoreResponsePayload => {
109
if (command === 'Tab.waitForResource') {
110
return {
111
data: [
112
{ id: 1, url: '/test3.js' } as IResourceMeta,
113
{ id: 2, url: '/test4.js' } as IResourceMeta,
114
],
115
};
116
}
117
};
118
119
const handler = new Handler(new Piper());
120
Helpers.needsClosing.push(handler);
121
const agent = await handler.createAgent();
122
const resources = await agent.waitForResource({ url: '/test3.js' });
123
expect(resources).toHaveLength(2);
124
125
await agent.close();
126
await handler.close();
127
});
128
129
it('should allow a user to specify a match function', async () => {
130
payloadHandler = ({ command }: ICoreRequestPayload): ICoreResponsePayload => {
131
if (command === 'Tab.waitForResource') {
132
return {
133
data: [
134
{ id: 1, url: '/test1.js' } as IResourceMeta,
135
{ id: 2, url: '/test2.js' } as IResourceMeta,
136
{ id: 3, url: '/test3.js' } as IResourceMeta,
137
{ id: 4, url: '/test4.js' } as IResourceMeta,
138
],
139
};
140
}
141
};
142
143
const handler = new Handler(new Piper());
144
Helpers.needsClosing.push(handler);
145
const agent = await handler.createAgent();
146
const resources = await agent.waitForResource({
147
filterFn(resource, done) {
148
if (resource.url === '/test1.js') {
149
done();
150
return true;
151
}
152
},
153
});
154
expect(resources).toHaveLength(1);
155
expect(resources[0].url).toBe('/test1.js');
156
157
await agent.close();
158
await handler.close();
159
});
160
161
it('should run multiple batches when a match function is provided', async () => {
162
let counter = 0;
163
payloadHandler = ({ command }: ICoreRequestPayload): ICoreResponsePayload => {
164
if (command === 'Tab.waitForResource') {
165
counter += 1;
166
if (counter === 1) {
167
return {
168
data: [
169
{ id: 1, url: '/test1.js' } as IResourceMeta,
170
{ id: 2, url: '/test2.js' } as IResourceMeta,
171
{ id: 3, url: '/test3.js' } as IResourceMeta,
172
{ id: 4, url: '/test4.js' } as IResourceMeta,
173
],
174
};
175
}
176
if (counter === 2 || counter === 3) {
177
return { data: [] };
178
}
179
if (counter === 4) {
180
return { data: [{ id: 5, url: '/test5.js' } as IResourceMeta] };
181
}
182
}
183
};
184
185
const handler = new Handler(new Piper());
186
Helpers.needsClosing.push(handler);
187
const agent = await handler.createAgent();
188
const resources = await agent.waitForResource({
189
filterFn(resource, done) {
190
if (resource.url === '/test5.js') {
191
done();
192
}
193
if (resource.url === '/test2.js') {
194
return false;
195
}
196
return true;
197
},
198
});
199
expect(resources).toHaveLength(4);
200
201
await agent.close();
202
await handler.close();
203
});
204
});
205
206