Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/client/test/document.test.ts
1028 views
1
// need to import this before the awaited stuff gets imported
2
import '../lib/SetupAwaitedHandler';
3
4
import { getState as getElementState } from 'awaited-dom/base/official-klasses/Element';
5
import IExecJsPathResult from '@secret-agent/interfaces/IExecJsPathResult';
6
import { getNodePointerFnName } from '@secret-agent/interfaces/jsPathFnNames';
7
import { Helpers } from '@secret-agent/testing';
8
import ICoreRequestPayload from '@secret-agent/interfaces/ICoreRequestPayload';
9
import ICoreResponsePayload from '@secret-agent/interfaces/ICoreResponsePayload';
10
import { Handler } from '../index';
11
import ConnectionToCore from '../connections/ConnectionToCore';
12
13
afterAll(Helpers.afterAll);
14
15
describe('document tests', () => {
16
it('runs querySelector', async () => {
17
const outgoing = jest.fn(
18
async (payload: ICoreRequestPayload): Promise<ICoreResponsePayload> => {
19
const { command, args } = payload;
20
await new Promise(resolve => setTimeout(resolve, 5));
21
if (command === 'Session.create') {
22
return {
23
data: { tabId: 'tab-id', sessionId: 'session-id', sessionsDataLocation: '' },
24
};
25
}
26
if (command === 'Session.addEventListener') {
27
return {
28
data: { listenerId: '1' },
29
};
30
}
31
if (command === 'FrameEnvironment.execJsPath') {
32
const [jsPath] = args;
33
const lastPath = jsPath[jsPath.length - 1];
34
if (lastPath && lastPath[0] === getNodePointerFnName) {
35
return {
36
data: {
37
value: null,
38
nodePointer: { id: 1 },
39
} as IExecJsPathResult,
40
};
41
}
42
}
43
},
44
);
45
46
class Piper extends ConnectionToCore {
47
async internalSendRequest(payload: ICoreRequestPayload): Promise<void> {
48
const data = await outgoing(payload);
49
50
this.onMessage({
51
responseId: payload.messageId,
52
data: data?.data,
53
...(data ?? {}),
54
});
55
}
56
57
protected createConnection = () => Promise.resolve(null);
58
protected destroyConnection = () => Promise.resolve(null);
59
}
60
61
const handler = new Handler(new Piper());
62
const agent = await handler.createAgent();
63
Helpers.needsClosing.push(agent);
64
65
const element = agent.document.querySelector('h1');
66
const jsPath = getElementState(element).awaitedPath.toJSON();
67
expect(jsPath[0]).toBe('document');
68
expect(jsPath[1]).toMatchObject(['querySelector', 'h1']);
69
70
const superElement = await element;
71
await superElement.tagName;
72
73
await agent.close();
74
await handler.close();
75
76
const outgoingCommands = outgoing.mock.calls;
77
expect(outgoingCommands.map(x => x[0].command)).toMatchObject([
78
'Core.connect',
79
'Session.create',
80
'Session.addEventListener',
81
'FrameEnvironment.execJsPath',
82
'FrameEnvironment.execJsPath',
83
'Session.close',
84
'Core.disconnect',
85
]);
86
expect(outgoingCommands[3][0].args).toMatchObject([[...jsPath, [getNodePointerFnName]]]);
87
expect(outgoingCommands[4][0].args).toMatchObject([[1, 'tagName']]);
88
});
89
});
90
91