dfklfg/packages/system/tests/RequestBuilder.test.ts

203 lines
5.6 KiB
TypeScript
Raw Normal View History

2023-06-08 10:45:23 +00:00
import { RelayCache } from "../src/GossipModel";
import { RequestBuilder, RequestStrategy } from "../src/RequestBuilder";
2023-05-19 14:15:13 +00:00
import { describe, expect } from "@jest/globals";
2023-06-12 13:15:45 +00:00
import { expandFilter } from "../src/RequestExpander";
2023-06-13 14:56:06 +00:00
import { bytesToHex } from "@noble/curves/abstract/utils";
2023-06-19 08:55:40 +00:00
import { unixNow, unixNowMs } from "@snort/shared";
2023-03-28 14:34:01 +00:00
2023-05-29 21:25:40 +00:00
const DummyCache = {
2023-06-19 08:55:40 +00:00
getFromCache: (pk?: string) => {
2023-05-29 21:25:40 +00:00
if (!pk) return undefined;
2023-06-19 08:55:40 +00:00
return {
pubkey: pk,
created_at: unixNow(),
relays: [
{
url: `wss://${pk}.com/`,
settings: {
read: true,
write: true,
},
2023-05-29 21:25:40 +00:00
},
2023-06-19 08:55:40 +00:00
]
};
2023-05-29 21:25:40 +00:00
},
} as RelayCache;
2023-05-29 21:25:40 +00:00
describe("RequestBuilder", () => {
2023-03-28 14:34:01 +00:00
describe("basic", () => {
test("empty filter", () => {
const b = new RequestBuilder("test");
b.withFilter();
2023-05-29 21:25:40 +00:00
expect(b.buildRaw()).toEqual([{}]);
2023-03-28 14:34:01 +00:00
});
test("only kind", () => {
const b = new RequestBuilder("test");
b.withFilter().kinds([0]);
2023-05-29 21:25:40 +00:00
expect(b.buildRaw()).toMatchObject([{ kinds: [0] }]);
2023-03-28 14:34:01 +00:00
});
test("empty authors", () => {
const b = new RequestBuilder("test");
b.withFilter().authors([]);
2023-05-29 21:25:40 +00:00
expect(b.buildRaw()).toMatchObject([{ authors: [] }]);
2023-03-28 14:34:01 +00:00
});
test("authors/kinds/ids", () => {
const authors = ["a1", "a2"];
const kinds = [0, 1, 2, 3];
const ids = ["id1", "id2", "id3"];
const b = new RequestBuilder("test");
b.withFilter().authors(authors).kinds(kinds).ids(ids);
2023-05-29 21:25:40 +00:00
expect(b.buildRaw()).toMatchObject([{ ids, authors, kinds }]);
2023-03-28 14:34:01 +00:00
});
test("authors and kinds, duplicates removed", () => {
const authors = ["a1", "a2"];
const kinds = [0, 1, 2, 3];
const ids = ["id1", "id2", "id3"];
const b = new RequestBuilder("test");
b.withFilter().ids(ids).authors(authors).kinds(kinds).ids(ids).authors(authors).kinds(kinds);
2023-05-29 21:25:40 +00:00
expect(b.buildRaw()).toMatchObject([{ ids, authors, kinds }]);
2023-03-28 14:34:01 +00:00
});
test("search", () => {
const b = new RequestBuilder("test");
b.withFilter().kinds([1]).search("test-search");
2023-05-29 21:25:40 +00:00
expect(b.buildRaw()).toMatchObject([{ kinds: [1], search: "test-search" }]);
2023-03-28 14:34:01 +00:00
});
test("timeline", () => {
const authors = ["a1", "a2"];
const kinds = [0, 1, 2, 3];
const until = 10;
const since = 5;
const b = new RequestBuilder("test");
b.withFilter().kinds(kinds).authors(authors).since(since).until(until);
2023-05-29 21:25:40 +00:00
expect(b.buildRaw()).toMatchObject([{ kinds, authors, until, since }]);
2023-03-28 14:34:01 +00:00
});
test("multi-filter timeline", () => {
const authors = ["a1", "a2"];
const kinds = [0, 1, 2, 3];
const until = 10;
const since = 5;
const b = new RequestBuilder("test");
b.withFilter().kinds(kinds).authors(authors).since(since).until(until);
b.withFilter().kinds(kinds).authors(authors).since(since).until(until);
2023-05-29 21:25:40 +00:00
expect(b.buildRaw()).toMatchObject([
2023-03-28 14:34:01 +00:00
{ kinds, authors, until, since },
{ kinds, authors, until, since },
]);
});
});
2023-05-29 21:25:40 +00:00
describe("diff basic", () => {
const rb = new RequestBuilder("test");
const f0 = rb.withFilter();
const a = rb.buildRaw();
f0.authors(["a"]);
expect(a).toEqual([{}]);
2023-06-12 13:15:45 +00:00
const b = rb.buildDiff(DummyCache, a.flatMap(expandFilter));
2023-05-29 21:25:40 +00:00
expect(b).toMatchObject([
{
filters: [{ authors: ["a"] }],
},
]);
});
describe("build gossip simply", () => {
const rb = new RequestBuilder("test");
rb.withFilter().authors(["a", "b"]).kinds([0]);
const a = rb.build(DummyCache);
expect(a).toEqual([
{
strategy: RequestStrategy.AuthorsRelays,
relay: "wss://a.com/",
filters: [
{
kinds: [0],
authors: ["a"],
},
],
},
{
strategy: RequestStrategy.AuthorsRelays,
relay: "wss://b.com/",
filters: [
{
kinds: [0],
authors: ["b"],
},
],
},
]);
});
describe("build gossip merged similar filters", () => {
const rb = new RequestBuilder("test");
rb.withFilter().authors(["a", "b"]).kinds([0]);
rb.withFilter().authors(["a", "b"]).kinds([10002]);
rb.withFilter().authors(["a"]).limit(10).kinds([4]);
const a = rb.build(DummyCache);
expect(a).toEqual([
{
strategy: RequestStrategy.AuthorsRelays,
relay: "wss://a.com/",
filters: [
{
kinds: [0, 10002],
authors: ["a"],
},
{
kinds: [4],
authors: ["a"],
limit: 10,
},
],
},
{
strategy: RequestStrategy.AuthorsRelays,
relay: "wss://b.com/",
filters: [
{
kinds: [0, 10002],
authors: ["b"],
},
],
},
]);
});
2023-03-28 14:34:01 +00:00
});
2023-06-13 14:56:06 +00:00
describe("build diff, large follow list", () => {
const f = [];
2023-06-13 16:06:47 +00:00
for (let x = 0; x < 2500; x++) {
2023-06-13 14:56:06 +00:00
const bytes = crypto.getRandomValues(new Uint8Array(32));
f.push(bytesToHex(bytes));
}
const rb = new RequestBuilder("test");
rb.withFilter().authors(f).kinds([1, 6, 10002, 3, 6969]);
const start = unixNowMs();
const a = rb.build(DummyCache);
expect(a).toEqual(f.map(a => {
return {
strategy: RequestStrategy.AuthorsRelays,
relay: `wss://${a}.com/`,
filters: [
{
kinds: [1, 6, 10002, 3, 6969],
authors: [a],
}
],
}
}));
expect(unixNowMs() - start).toBeLessThan(500);
const start2 = unixNowMs();
const b = rb.buildDiff(DummyCache, rb.buildRaw().flatMap(expandFilter));
expect(b).toEqual([]);
2023-06-13 16:06:47 +00:00
expect(unixNowMs() - start2).toBeLessThan(100);
2023-06-13 14:56:06 +00:00
})