Skip to content

Commit

Permalink
fix(grouper): handle out-of-type diff calculations (#275)
Browse files Browse the repository at this point in the history
* add failed test case to deepDiff

* update deepDiff method

Resolves #312

* eslint issue fixed

* Update utils.test.js

* Update utils.test.js

---------

Co-authored-by: Peter Savchenko <[email protected]>
  • Loading branch information
nikmel2803 and neSpecc authored Dec 9, 2024
1 parent 98997a4 commit cdf5984
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 2 deletions.
28 changes: 26 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,33 @@ module.exports.deepMerge = deepMerge;
* @returns {object}
*/
function deepDiff(source, target) {
if (typeOf(target) === 'array') {
const sourceType = typeOf(source);
const targetType = typeOf(target);

/**
* If we'll compare NOTHING with SOMETHING, the diff will be SOMETHING
*/
if (source === undefined) {
return target;
}

/**
* If we'll compare SOMETHING with NOTHING, the diff will be NOTHING
*/
if (targetType === undefined) {
return undefined;
}

/**
* We CAN'T compare apples with dogs
*/
if (sourceType !== targetType) {
return undefined;
}

if (targetType === 'array') {
return arrayDiff(source, target);
} else if (typeOf(target) === 'object') {
} else if (targetType === 'object') {
return objectDiff(source, target);
} else if (source !== target) {
return target;
Expand Down
135 changes: 135 additions & 0 deletions lib/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,35 @@ describe('Utils', () => {
files: [ { line: 1 }, { line: 2 }, { line: 3 } ],
},
},

/**
* The first - an empty array
* The second - array with the children non-empty array
*/
{
sourceObject: {
prop: [],
},
targetObject: {
prop: [ [ 'i am not empty' ] ],
},
expectedDiff: {
prop: [ [ 'i am not empty' ] ],
},
expectedMerge: {
prop: [ [ 'i am not empty' ] ],
},
},

/**
* Trying to compare two things of different type
*/
{
sourceObject: [],
targetObject: {},
expectedDiff: undefined,
expectedMerge: {},
},
];

test('should return right object diff', () => {
Expand All @@ -135,4 +164,110 @@ describe('Utils', () => {
expect(merge).toEqual(testCase.expectedMerge);
});
});

/**
* This test is a temporary solution to handle case with invalid events format sent by the PHP catcher
*
* The problem: the PHP catcher sends some data via incompatible format under invalid names and types.
* Those fields leads to the issues with diff calculation since fields could have different structure and types.
*
* The solution: deepDiff will return undefined in case of comparison of things with different types
*
* Original issue:
* https://github.com/codex-team/hawk.workers/issues/312
*
* PHP Catcher issue:
* https://github.com/codex-team/hawk.php/issues/39
*/
test('should not throw error comparing events with incompatible format', () => {
const originalStackTrace = [
{
function: 'postAddComment',
class: 'Components\\Comments\\Comments',
object: {},
type: '->',
args: [
286131,
{},
],
},
];

const repetitionStackTrace = [
{
file: '/var/www/osnova/vendor/php-di/invoker/src/Invoker.php',
line: 74,
function: 'call_user_func_array',
args: [
[
{},
'sendWebhooksJob',
],
[
[
{
id: 6697,
token: '539506',
event: 'new_comment',
url: 'https://callback.angry.space/vc_callback?date=2020&code=lMzBjOWZh@DADAD@jFlZmUy',
filter: '[]',
data: '[]',
removed: false,
},
],
{
type: 'new_comment',
data: {
id: 3206086,
url: 'https://somesite.io/trade/286961',
text: 'Это только со стороны так вроде долго, а если смотреть изнутри, то пока там освободиться купьер, пока найдут товар пока разберуться куда везти может и 4 часа пройти.',
media: [],
date: '2021-08-27T18:08:30+03:00',
creator: {
id: 27823,
avatar: 'https://s3.somesite.io/8ddee2e8-28e4-7863-425e-dd9b06deae5d/',
name: 'John S.',
url: 'https://somesite.io/u/27823-john-s',
},
content: {
id: 286961,
title: 'Wildberries запустил доставку товаров за 2 часа в Петербурге',
url: 'https://somesite.io/trade/286961',
owner: {
id: 199122,
name: 'Торговля',
avatar: 'https://leonardo.osnova.io/d8fbb348-a8fd-641c-55dd-6a404055b457/',
url: 'https://somesite.io/trade',
},
},
replyTo: {
id: 3205883,
url: 'https://somesite.io/trade/286961',
text: 'Никто не пошутил, тогда это сделаю я!\n\n- 2.. часа!!1',
media: [],
creator: {
id: 877711,
avatar: 'https://leonardo.osnova.io/476a4e2c-8045-5b77-8a37-f6b1eb58bf93/',
name: 'Вадим Осадчий',
url: 'https://somesite.io/u/877711-john-doe',
},
},
},
},
],
],
},
];

const diff = utils.deepDiff(originalStackTrace, repetitionStackTrace);

expect(diff).toEqual([
{
file: '/var/www/osnova/vendor/php-di/invoker/src/Invoker.php',
line: 74,
function: 'call_user_func_array',
args: [undefined, undefined],
},
]);
});
});

0 comments on commit cdf5984

Please sign in to comment.