1
0
forked from Kieran/snort
snort/packages/system/tests/request-merger.test.ts

189 lines
4.3 KiB
TypeScript
Raw Normal View History

2023-06-08 10:45:23 +00:00
import { ReqFilter } from "../src";
2023-07-23 22:19:26 +00:00
import { canMergeFilters, filterIncludes, flatMerge, mergeSimilar, simpleMerge } from "../src/request-merger";
import { FlatReqFilter, expandFilter } from "../src/request-expander";
2023-05-29 21:25:40 +00:00
describe("RequestMerger", () => {
it("should simple merge authors", () => {
const a = {
authors: ["a"],
} as ReqFilter;
2023-05-29 21:25:40 +00:00
const b = {
authors: ["b"],
} as ReqFilter;
2023-05-29 21:25:40 +00:00
const merged = mergeSimilar([a, b]);
expect(merged).toEqual([
2023-05-29 21:25:40 +00:00
{
authors: ["a", "b"],
},
]);
});
it("should append non-mergable filters", () => {
const a = {
authors: ["a"],
} as ReqFilter;
2023-05-29 21:25:40 +00:00
const b = {
authors: ["b"],
} as ReqFilter;
2023-05-29 21:25:40 +00:00
const c = {
limit: 5,
authors: ["a"],
};
const merged = mergeSimilar([a, b, c]);
expect(merged).toEqual([
2023-05-29 21:25:40 +00:00
{
authors: ["a", "b"],
},
{
limit: 5,
authors: ["a"],
},
]);
});
2023-05-30 13:48:38 +00:00
it("filterIncludes", () => {
const bigger = {
authors: ["a", "b", "c"],
since: 99,
} as ReqFilter;
2023-05-30 13:48:38 +00:00
const smaller = {
authors: ["c"],
since: 100,
} as ReqFilter;
2023-05-30 13:48:38 +00:00
expect(filterIncludes(bigger, smaller)).toBe(true);
});
2023-06-01 08:54:25 +00:00
it("simpleMerge", () => {
const a = {
authors: ["a", "b", "c"],
since: 99,
} as ReqFilter;
2023-06-01 08:54:25 +00:00
const b = {
authors: ["c", "d", "e"],
since: 100,
} as ReqFilter;
2023-06-01 08:54:25 +00:00
expect(simpleMerge([a, b])).toEqual({
authors: ["a", "b", "c", "d", "e"],
since: 100,
});
});
2023-05-29 21:25:40 +00:00
});
describe("flatMerge", () => {
it("should flat merge simple", () => {
const input = [
{ ids: 0, authors: "a" },
{ ids: 0, authors: "b" },
{ kinds: 1 },
{ kinds: 2 },
2023-06-08 04:39:10 +00:00
{ kinds: 2 },
{ ids: 0, authors: "c" },
{ authors: "c", kinds: 1 },
{ authors: "c", limit: 100 },
{ ids: 1, authors: "c" },
] as Array<FlatReqFilter>;
const output = [
{ ids: [0], authors: ["a", "b", "c"] },
{ kinds: [1, 2] },
{ authors: ["c"], kinds: [1] },
{ authors: ["c"], limit: 100 },
{ ids: [1], authors: ["c"] },
] as Array<ReqFilter>;
expect(flatMerge(input)).toEqual(output);
});
it("should expand and flat merge complex same", () => {
const input = [
{ kinds: [1, 6969, 6], authors: ["kieran", "snort", "c", "d", "e"], since: 1, until: 100 },
{ kinds: [4], authors: ["kieran"] },
{ kinds: [4], "#p": ["kieran"] },
{ kinds: [1000], authors: ["snort"], "#p": ["kieran"] },
] as Array<ReqFilter>;
const dut = flatMerge(input.flatMap(expandFilter).sort(() => (Math.random() > 0.5 ? 1 : -1)));
2023-06-12 13:15:45 +00:00
expect(dut.every(a => input.some(b => canMergeFilters(b, a) === false))).toEqual(true);
});
});
2023-06-12 13:15:45 +00:00
2023-07-22 18:37:46 +00:00
describe("canMerge", () => {
2023-06-12 13:15:45 +00:00
it("should have 0 distance", () => {
const a = {
ids: "a",
2023-07-22 18:37:46 +00:00
keys: 1,
2023-06-12 13:15:45 +00:00
};
const b = {
ids: "a",
2023-07-22 18:37:46 +00:00
keys: 1,
2023-06-12 13:15:45 +00:00
};
expect(canMergeFilters(a, b)).toEqual(true);
});
it("should have 1 distance", () => {
const a = {
ids: "a",
2023-07-22 18:37:46 +00:00
keys: 1,
2023-06-12 13:15:45 +00:00
};
const b = {
ids: "b",
2023-07-22 18:37:46 +00:00
keys: 1,
2023-06-12 13:15:45 +00:00
};
expect(canMergeFilters(a, b)).toEqual(true);
});
it("should have 10 distance", () => {
const a = {
ids: "a",
2023-07-22 18:37:46 +00:00
keys: 1,
2023-06-12 13:15:45 +00:00
};
const b = {
ids: "a",
kinds: 1,
2023-07-22 18:37:46 +00:00
keys: 2,
2023-06-12 13:15:45 +00:00
};
expect(canMergeFilters(a, b)).toEqual(false);
});
it("should have 11 distance", () => {
const a = {
ids: "a",
2023-07-22 18:37:46 +00:00
keys: 1,
2023-06-12 13:15:45 +00:00
};
const b = {
ids: "b",
kinds: 1,
2023-07-22 18:37:46 +00:00
keys: 2,
2023-06-12 13:15:45 +00:00
};
expect(canMergeFilters(a, b)).toEqual(false);
});
it("should have 1 distance, arrays", () => {
const a = {
since: 1,
until: 100,
kinds: [1],
2023-07-22 18:37:46 +00:00
authors: ["kieran", "snort", "c", "d", "e"],
2023-06-12 13:15:45 +00:00
};
const b = {
since: 1,
until: 100,
kinds: [6969],
2023-07-22 18:37:46 +00:00
authors: ["kieran", "snort", "c", "d", "e"],
2023-06-12 13:15:45 +00:00
};
expect(canMergeFilters(a, b)).toEqual(true);
});
it("should have 1 distance, array change extra", () => {
const a = {
since: 1,
until: 100,
kinds: [1],
2023-07-22 18:37:46 +00:00
authors: ["f", "kieran", "snort", "c", "d"],
2023-06-12 13:15:45 +00:00
};
const b = {
since: 1,
until: 100,
kinds: [1],
2023-07-22 18:37:46 +00:00
authors: ["kieran", "snort", "c", "d", "e"],
2023-06-12 13:15:45 +00:00
};
expect(canMergeFilters(a, b)).toEqual(true);
});
2023-07-22 18:37:46 +00:00
});