1
0
forked from Kieran/snort
snort/packages/app/src/System/RequestMerger.test.ts

110 lines
2.7 KiB
TypeScript
Raw Normal View History

import { ReqFilter } from "System";
import { filterIncludes, flatMerge, mergeSimilar, simpleMerge } from "./RequestMerger";
import { FlatReqFilter, expandFilter } from "./RequestExpander";
import { distance } from "./Util";
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 },
{ 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)));
expect(dut.every(a => input.some(b => distance(b, a) === 0))).toEqual(true);
});
});