feat: tools pages
Various other fixes: - Better handeling of limit/since/before merging - Expose timeout through request builder - Expose PickN through request builder - Fix tests
This commit is contained in:
@ -1,17 +1,27 @@
|
||||
import { splitAllByWriteRelays } from "../src/gossip-model";
|
||||
import { splitAllByWriteRelays } from "../src/outbox-model";
|
||||
|
||||
describe("GossipModel", () => {
|
||||
describe("OutboxModel", () => {
|
||||
it("should not output empty", () => {
|
||||
const Relays = {
|
||||
getFromCache: (pk?: string) => {
|
||||
if (pk) {
|
||||
return {
|
||||
pubkey: pk,
|
||||
created_at: 0,
|
||||
created: 0,
|
||||
loaded: 0,
|
||||
relays: [],
|
||||
};
|
||||
}
|
||||
},
|
||||
update: () => {
|
||||
return Promise.resolve<"new" | "updated" | "refresh" | "no_change">("new");
|
||||
},
|
||||
buffer: () => {
|
||||
return Promise.resolve<Array<string>>([]);
|
||||
},
|
||||
bulkSet: () => {
|
||||
return Promise.resolve();
|
||||
},
|
||||
};
|
||||
const a = [
|
||||
{
|
@ -1,4 +1,4 @@
|
||||
import { RelayCache } from "../src/gossip-model";
|
||||
import { RelayCache } from "../src/outbox-model";
|
||||
import { RequestBuilder, RequestStrategy } from "../src/request-builder";
|
||||
import { describe, expect } from "@jest/globals";
|
||||
import { bytesToHex } from "@noble/curves/abstract/utils";
|
||||
@ -23,7 +23,16 @@ const DummyCache = {
|
||||
],
|
||||
};
|
||||
},
|
||||
} as FeedCache<UsersRelays>;
|
||||
update: () => {
|
||||
return Promise.resolve<"new" | "updated" | "refresh" | "no_change">("new");
|
||||
},
|
||||
buffer: () => {
|
||||
return Promise.resolve<Array<string>>([]);
|
||||
},
|
||||
bulkSet: () => {
|
||||
return Promise.resolve();
|
||||
},
|
||||
} as unknown as FeedCache<UsersRelays>;
|
||||
|
||||
const System = new NostrSystem({
|
||||
relayCache: DummyCache,
|
||||
@ -112,7 +121,7 @@ describe("RequestBuilder", () => {
|
||||
rb.withFilter().authors(["a", "b"]).kinds([0]);
|
||||
|
||||
const a = rb.build(System);
|
||||
expect(a).toEqual([
|
||||
expect(a).toMatchObject([
|
||||
{
|
||||
strategy: RequestStrategy.AuthorsRelays,
|
||||
relay: "wss://a.com/",
|
||||
@ -143,7 +152,7 @@ describe("RequestBuilder", () => {
|
||||
rb.withFilter().authors(["a"]).limit(10).kinds([4]);
|
||||
|
||||
const a = rb.build(System);
|
||||
expect(a).toEqual([
|
||||
expect(a).toMatchObject([
|
||||
{
|
||||
strategy: RequestStrategy.AuthorsRelays,
|
||||
relay: "wss://a.com/",
|
||||
|
@ -10,25 +10,25 @@ describe("RequestExpander", () => {
|
||||
since: 99,
|
||||
limit: 10,
|
||||
};
|
||||
expect(expandFilter(a)).toEqual([
|
||||
{ authors: "a", kinds: 1, ids: "x", "#p": "a", since: 99, limit: 10, keys: 4 },
|
||||
{ authors: "a", kinds: 1, ids: "y", "#p": "a", since: 99, limit: 10, keys: 4 },
|
||||
{ authors: "a", kinds: 2, ids: "x", "#p": "a", since: 99, limit: 10, keys: 4 },
|
||||
{ authors: "a", kinds: 2, ids: "y", "#p": "a", since: 99, limit: 10, keys: 4 },
|
||||
{ authors: "a", kinds: 3, ids: "x", "#p": "a", since: 99, limit: 10, keys: 4 },
|
||||
{ authors: "a", kinds: 3, ids: "y", "#p": "a", since: 99, limit: 10, keys: 4 },
|
||||
{ authors: "b", kinds: 1, ids: "x", "#p": "a", since: 99, limit: 10, keys: 4 },
|
||||
{ authors: "b", kinds: 1, ids: "y", "#p": "a", since: 99, limit: 10, keys: 4 },
|
||||
{ authors: "b", kinds: 2, ids: "x", "#p": "a", since: 99, limit: 10, keys: 4 },
|
||||
{ authors: "b", kinds: 2, ids: "y", "#p": "a", since: 99, limit: 10, keys: 4 },
|
||||
{ authors: "b", kinds: 3, ids: "x", "#p": "a", since: 99, limit: 10, keys: 4 },
|
||||
{ authors: "b", kinds: 3, ids: "y", "#p": "a", since: 99, limit: 10, keys: 4 },
|
||||
{ authors: "c", kinds: 1, ids: "x", "#p": "a", since: 99, limit: 10, keys: 4 },
|
||||
{ authors: "c", kinds: 1, ids: "y", "#p": "a", since: 99, limit: 10, keys: 4 },
|
||||
{ authors: "c", kinds: 2, ids: "x", "#p": "a", since: 99, limit: 10, keys: 4 },
|
||||
{ authors: "c", kinds: 2, ids: "y", "#p": "a", since: 99, limit: 10, keys: 4 },
|
||||
{ authors: "c", kinds: 3, ids: "x", "#p": "a", since: 99, limit: 10, keys: 4 },
|
||||
{ authors: "c", kinds: 3, ids: "y", "#p": "a", since: 99, limit: 10, keys: 4 },
|
||||
expect(expandFilter(a)).toMatchObject([
|
||||
{ authors: "a", kinds: 1, ids: "x", "#p": "a", since: 99, limit: 10 },
|
||||
{ authors: "a", kinds: 1, ids: "y", "#p": "a", since: 99, limit: 10 },
|
||||
{ authors: "a", kinds: 2, ids: "x", "#p": "a", since: 99, limit: 10 },
|
||||
{ authors: "a", kinds: 2, ids: "y", "#p": "a", since: 99, limit: 10 },
|
||||
{ authors: "a", kinds: 3, ids: "x", "#p": "a", since: 99, limit: 10 },
|
||||
{ authors: "a", kinds: 3, ids: "y", "#p": "a", since: 99, limit: 10 },
|
||||
{ authors: "b", kinds: 1, ids: "x", "#p": "a", since: 99, limit: 10 },
|
||||
{ authors: "b", kinds: 1, ids: "y", "#p": "a", since: 99, limit: 10 },
|
||||
{ authors: "b", kinds: 2, ids: "x", "#p": "a", since: 99, limit: 10 },
|
||||
{ authors: "b", kinds: 2, ids: "y", "#p": "a", since: 99, limit: 10 },
|
||||
{ authors: "b", kinds: 3, ids: "x", "#p": "a", since: 99, limit: 10 },
|
||||
{ authors: "b", kinds: 3, ids: "y", "#p": "a", since: 99, limit: 10 },
|
||||
{ authors: "c", kinds: 1, ids: "x", "#p": "a", since: 99, limit: 10 },
|
||||
{ authors: "c", kinds: 1, ids: "y", "#p": "a", since: 99, limit: 10 },
|
||||
{ authors: "c", kinds: 2, ids: "x", "#p": "a", since: 99, limit: 10 },
|
||||
{ authors: "c", kinds: 2, ids: "y", "#p": "a", since: 99, limit: 10 },
|
||||
{ authors: "c", kinds: 3, ids: "x", "#p": "a", since: 99, limit: 10 },
|
||||
{ authors: "c", kinds: 3, ids: "y", "#p": "a", since: 99, limit: 10 },
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
@ -81,25 +81,27 @@ describe("RequestMerger", () => {
|
||||
describe("flatMerge", () => {
|
||||
it("should flat merge simple", () => {
|
||||
const input = [
|
||||
{ ids: 0, authors: "a" },
|
||||
{ ids: 0, authors: "b" },
|
||||
{ kinds: 1 },
|
||||
{ kinds: 2 },
|
||||
{ kinds: 2 },
|
||||
{ ids: 0, authors: "c" },
|
||||
{ authors: "c", kinds: 1 },
|
||||
{ authors: "c", limit: 100 },
|
||||
{ ids: 1, authors: "c" },
|
||||
{ ids: "0", authors: "a", resultSetId: "" },
|
||||
{ ids: "0", authors: "b", resultSetId: "" },
|
||||
{ kinds: 1, resultSetId: "" },
|
||||
{ kinds: 2, resultSetId: "" },
|
||||
{ kinds: 2, resultSetId: "" },
|
||||
{ ids: "0", authors: "c", resultSetId: "" },
|
||||
{ authors: "c", kinds: 1, resultSetId: "" },
|
||||
{ authors: "c", limit: 100, resultSetId: "limit-c-100" },
|
||||
{ authors: "b", limit: 100, resultSetId: "limit-b-100" },
|
||||
{ ids: "1", authors: "c", resultSetId: "" },
|
||||
] as Array<FlatReqFilter>;
|
||||
const output = [
|
||||
{ ids: [0], authors: ["a", "b", "c"] },
|
||||
{ ids: ["0"], authors: ["a", "b", "c"] },
|
||||
{ kinds: [1, 2] },
|
||||
{ authors: ["c"], kinds: [1] },
|
||||
{ authors: ["c"], limit: 100 },
|
||||
{ ids: [1], authors: ["c"] },
|
||||
{ authors: ["b"], limit: 100 },
|
||||
{ ids: ["1"], authors: ["c"] },
|
||||
] as Array<ReqFilter>;
|
||||
|
||||
expect(flatMerge(input)).toEqual(output);
|
||||
expect(flatMerge(input)).toMatchObject(output);
|
||||
});
|
||||
|
||||
it("should expand and flat merge complex same", () => {
|
||||
@ -119,47 +121,47 @@ describe("canMerge", () => {
|
||||
it("should have 0 distance", () => {
|
||||
const a = {
|
||||
ids: "a",
|
||||
keys: 1,
|
||||
};
|
||||
resultSetId: "",
|
||||
} as FlatReqFilter;
|
||||
const b = {
|
||||
ids: "a",
|
||||
keys: 1,
|
||||
};
|
||||
resultSetId: "",
|
||||
} as FlatReqFilter;
|
||||
expect(canMergeFilters(a, b)).toEqual(true);
|
||||
});
|
||||
it("should have 1 distance", () => {
|
||||
const a = {
|
||||
ids: "a",
|
||||
keys: 1,
|
||||
};
|
||||
resultSetId: "",
|
||||
} as FlatReqFilter;
|
||||
const b = {
|
||||
ids: "b",
|
||||
keys: 1,
|
||||
};
|
||||
resultSetId: "",
|
||||
} as FlatReqFilter;
|
||||
expect(canMergeFilters(a, b)).toEqual(true);
|
||||
});
|
||||
it("should have 10 distance", () => {
|
||||
const a = {
|
||||
ids: "a",
|
||||
keys: 1,
|
||||
};
|
||||
resultSetId: "",
|
||||
} as FlatReqFilter;
|
||||
const b = {
|
||||
ids: "a",
|
||||
kinds: 1,
|
||||
keys: 2,
|
||||
};
|
||||
resultSetId: "",
|
||||
} as FlatReqFilter;
|
||||
expect(canMergeFilters(a, b)).toEqual(false);
|
||||
});
|
||||
it("should have 11 distance", () => {
|
||||
const a = {
|
||||
ids: "a",
|
||||
keys: 1,
|
||||
};
|
||||
resultSetId: "",
|
||||
} as FlatReqFilter;
|
||||
const b = {
|
||||
ids: "b",
|
||||
kinds: 1,
|
||||
keys: 2,
|
||||
};
|
||||
resultSetId: "",
|
||||
} as FlatReqFilter;
|
||||
expect(canMergeFilters(a, b)).toEqual(false);
|
||||
});
|
||||
it("should have 1 distance, arrays", () => {
|
||||
|
Reference in New Issue
Block a user