Skip to content

Commit

Permalink
Fix some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
muodov committed Sep 4, 2024
1 parent 4bd1822 commit f4ae631
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 34 deletions.
2 changes: 1 addition & 1 deletion post-processing/clickhouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Examples:
.option('--delete', 'Delete data for the given crawlid')
.parse(process.argv);

const opts = program.opts()
const opts = program.opts();

const ch = new Clickhouse();
const crawlName = opts.crawlname;
Expand Down
2 changes: 1 addition & 1 deletion tests/collectors/APICallCollector.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('APICallCollector', () => {
});

// @ts-ignore not a real CDP client
await collector.addTarget({cdpClient: fakeCDPClient, type: 'page', url: 'https://example.com'});
await collector.addTarget({session: fakeCDPClient, type: 'page', url: 'https://example.com'});

const executionContextCreated = listeners.find(a => a.name === 'Runtime.executionContextCreated');
assert(executionContextCreated, 'executionContextCreated listener was set');
Expand Down
19 changes: 8 additions & 11 deletions tests/collectors/CMPCollector.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('CMPCollector', () => {
}
});
// @ts-ignore not a real CDP client
await collector.addTarget({cdpClient: fakeCDPClient, type: 'page', url: 'https://example.com'});
await collector.addTarget({session: fakeCDPClient, type: 'page', url: 'https://example.com'});
});

describe('handleMessage', () => {
Expand Down Expand Up @@ -284,18 +284,15 @@ describe('CMPCollector', () => {
const contentScriptEval = commands.find(cmd => cmd[0] === 'Runtime.evaluate')[1];
assert.strictEqual(contentScriptEval.contextId, 31337);

// @ts-ignore no need to provide all params
collector.context.pages = () => Promise.resolve([
{
frames: () => [
{
evaluate: () => Promise.resolve('This website is using cookies. We are using cookies! To reiterate, you consent to the use of cookies on this website. In fact, there is nothing you can possibly do.')
}
]
const origSessionSend = collector._cdpClient.send;
// @ts-expect-error monkey patching the method
collector._cdpClient.send = () => Promise.resolve({
result: {
value: 'This website is using cookies. We are using cookies! To reiterate, you consent to the use of cookies on this website. In fact, there is nothing you can possibly do.'
}
]);

});
await collector.postLoad();
collector._cdpClient.send = origSessionSend;
const results = await collector.getData();
assert.deepStrictEqual(results, [{
name: '',
Expand Down
2 changes: 1 addition & 1 deletion tests/collectors/CookieCollector.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const fakeCDPClient = {
collector.init({});

// @ts-ignore not a real CDP client
collector.addTarget({cdpClient: fakeCDPClient, type: 'page', url: 'http://example.com'});
collector.addTarget({session: fakeCDPClient, type: 'page', url: 'http://example.com'});

collector.getData()
.then(data => {
Expand Down
35 changes: 17 additions & 18 deletions tests/collectors/RequestCollector.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function createFakeCDP() {
*/
const listeners = [];

const cdpClient = {
const cdpSession = {
send: () => Promise.resolve(),
on: (/** @type {string} **/name, /** @type {function(object)} **/callback) => {
listeners.push({name, callback});
Expand All @@ -18,7 +18,7 @@ function createFakeCDP() {

return {
listeners,
cdpClient
cdpSession
};
}

Expand All @@ -29,15 +29,15 @@ async function testDefaultSettings() {
/**
* getData
*/
const {listeners, cdpClient: fakeCDPClient} = createFakeCDP();
const {listeners, cdpSession: fakeCDPClient} = createFakeCDP();

// @ts-ignore no need to provide all params
collector.init({
log: () => {}
});

// @ts-ignore not a real CDP client
await collector.addTarget({cdpClient: fakeCDPClient, type: 'page', url: 'http://example.com'});
await collector.addTarget({session: fakeCDPClient, type: 'page', url: 'http://example.com'});

/**
* Regular request - success
Expand Down Expand Up @@ -273,15 +273,24 @@ async function testResponseHashSetting() {
/**
* getData
*/
const {listeners, cdpClient: fakeCDPClient} = createFakeCDP();
const {listeners, cdpSession: fakeCDPClient} = createFakeCDP();
// @ts-expect-error monkeypatching a method
fakeCDPClient.send = command => {
if (command === 'Network.getResponseBody') {
// eslint-disable-next-line no-console
return Promise.resolve({body: 'dGVzdA==', base64Encoded: true});//btoa('test')
}

return Promise.resolve();
};

// @ts-ignore no need to provide all params
collector.init({
log: () => {}
});

// @ts-ignore not a real CDP client
await collector.addTarget({cdpClient: fakeCDPClient, type: 'page', url: 'http://example.com'});
await collector.addTarget({session: fakeCDPClient, type: 'page', url: 'http://example.com'});

const requestWillBeSent = listeners.find(a => a.name === 'Network.requestWillBeSent');
const responseReceived = listeners.find(a => a.name === 'Network.responseReceived');
Expand Down Expand Up @@ -311,16 +320,6 @@ async function testResponseHashSetting() {
}
});

//@ts-ignore
fakeCDPClient.send = command => {
if (command === 'Network.getResponseBody') {
// eslint-disable-next-line no-console
return Promise.resolve({body: 'dGVzdA==', base64Encoded: true});//btoa('test')
}

return Promise.resolve();
};

await loadingFinished.callback({requestId: 100, encodedDataLength: 666, timestamp: 223456});

const data1 = collector.getData({finalUrl: 'https://example.com/'});
Expand Down Expand Up @@ -350,15 +349,15 @@ async function testCustomHeadersSetting() {
/**
* getData
*/
const {listeners, cdpClient: fakeCDPClient} = createFakeCDP();
const {listeners, cdpSession: fakeCDPClient} = createFakeCDP();

// @ts-ignore no need to provide all params
collector.init({
log: () => {}
});

// @ts-ignore not a real CDP client
await collector.addTarget({cdpClient: fakeCDPClient, type: 'page', url: 'http://example.com'});
await collector.addTarget({session: fakeCDPClient, type: 'page', url: 'http://example.com'});

const requestWillBeSent = listeners.find(a => a.name === 'Network.requestWillBeSent');
const responseReceived = listeners.find(a => a.name === 'Network.responseReceived');
Expand Down
4 changes: 2 additions & 2 deletions tests/collectors/TargetCollector.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ const fakeCDPClient = {};
collector.init();

// @ts-ignore not a real CDP client
collector.addTarget({cdpClient: fakeCDPClient, type: 'page', url: 'http://example.com'});
collector.addTarget({session: fakeCDPClient, type: 'page', url: 'http://example.com'});
// @ts-ignore not a real CDP client
collector.addTarget({cdpClient: fakeCDPClient, type: 'service_worker', url: 'http://example.com/sw.js'});
collector.addTarget({session: fakeCDPClient, type: 'service_worker', url: 'http://example.com/sw.js'});

const targets = collector.getData();

Expand Down

0 comments on commit f4ae631

Please sign in to comment.