Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
release: v0.0.0-dev.42 to MASTER (#184)
Browse files Browse the repository at this point in the history
* fix: malformed definition breaking flow, add test (#181)

* release: v0.0.0-dev.42

Co-authored-by: Dennis <dennis@airgrid.io>
  • Loading branch information
naripok and ydennisy authored Jun 22, 2022
1 parent 723b8db commit 2585c9f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 40 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@airgrid/edgekit",
"version": "0.0.0-dev.41",
"version": "0.0.0-dev.42",
"homepage": "https://edgekit.org/",
"author": "AirGrid <https://airgrid.io>",
"license": "MIT",
Expand Down
78 changes: 41 additions & 37 deletions src/engine/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,49 @@ export const evaluateCondition = (
condition: Readonly<EngineCondition<AudienceDefinitionFilter>>,
pageViews: Readonly<PageView[]>
): boolean => {
const { filter, rules } = condition;
try {
const { filter, rules } = condition;

// if no queries, do not match at all
if (pageViews.length === 0 || filter.queries.length === 0) {
return false;
}
// if no queries, do not match at all
if (pageViews.length === 0 || filter.queries.length === 0) {
return false;
}

/* Checks each pageView once for any matching queries
* and returns the filtered array containing the matched
* pageViews
*
* TODO Here, also, we have an opportunity to implement the
* internal AND logic, swapping some for every
* according to the value of filter.any
*
* NOTE: there is actually a semantic problem here.
* The filter definition specifies an 'any' switch,
* which defaults to false. However, the current
* filtering uses `some` to check the query conditions.
* The complete implementation here would be:
* ```
* filter.queries[filter.any ? 'some' : 'every']((query) => ...
* ```
* Yet, there is too much (testing) code depending on
* this defaulting to `some` and the refactor would take
* some time.
*/
const filteredPageViews = pageViews.filter((pageView) =>
filter.queries.some((query) => {
const pageFeatures = pageView.features[query.queryProperty];
return queryMatches(query, pageFeatures);
})
);
/* Checks each pageView once for any matching queries
* and returns the filtered array containing the matched
* pageViews
*
* TODO Here, also, we have an opportunity to implement the
* internal AND logic, swapping some for every
* according to the value of filter.any
*
* NOTE: there is actually a semantic problem here.
* The filter definition specifies an 'any' switch,
* which defaults to false. However, the current
* filtering uses `some` to check the query conditions.
* The complete implementation here would be:
* ```
* filter.queries[filter.any ? 'some' : 'every']((query) => ...
* ```
* Yet, there is too much (testing) code depending on
* this defaulting to `some` and the refactor would take
* some time.
*/
const filteredPageViews = pageViews.filter((pageView) =>
filter.queries.some((query) => {
const pageFeatures = pageView.features[query.queryProperty];
return queryMatches(query, pageFeatures);
})
);

return rules.every((rule) => {
const reducer = reducers[rule.reducer.name]();
const value = reducer(filteredPageViews);
const matches = matchers[rule.matcher.name](rule.matcher.args);
return rules.every((rule) => {
const reducer = reducers[rule.reducer.name]();
const value = reducer(filteredPageViews);
const matches = matchers[rule.matcher.name](rule.matcher.args);

return matches(value);
});
return matches(value);
});
} catch (error) {
return false;
}
};
25 changes: 25 additions & 0 deletions test/unit/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,31 @@ describe('edkt run method', () => {

expect(edkt.getMatchedAudiences()).toHaveLength(0);
});

it('does not break execution if there is a malformed audience definition', async () => {
setUpLocalStorage(TWO_PAGE_VIEW);

const malformedAudience = {
id: 'test_id',
version: 7,
ttl: 1209600,
lookBack: 0,
occurrences: 0,
};

await edkt.run({
pageFeatures,
audienceDefinitions: [
audienceDefinition,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
malformedAudience,
],
omitGdprConsent: true,
});

expect(edkt.getMatchedAudiences()).toHaveLength(1);
});
});

describe('look back edkt run behaviour', () => {
Expand Down

0 comments on commit 2585c9f

Please sign in to comment.