snort/packages/app/src/System/Query.test.ts

114 lines
2.6 KiB
TypeScript
Raw Normal View History

2023-05-30 13:48:38 +00:00
import { Connection } from "System";
2023-05-19 14:15:13 +00:00
import { describe, expect } from "@jest/globals";
2023-05-24 16:17:17 +00:00
import { Query, QueryBase } from "./Query";
2023-04-08 19:14:08 +00:00
import { getRandomValues } from "crypto";
2023-04-25 17:01:29 +00:00
import { FlatNoteStore } from "./NoteCollection";
2023-06-01 08:54:25 +00:00
import { RequestStrategy } from "./RequestBuilder";
2023-04-08 19:14:08 +00:00
window.crypto = {} as any;
window.crypto.getRandomValues = getRandomValues as any;
2023-03-29 10:40:05 +00:00
describe("query", () => {
test("progress", () => {
2023-05-29 21:25:40 +00:00
const q = new Query("test", new FlatNoteStore());
2023-03-29 10:40:05 +00:00
const opt = {
read: true,
write: true,
2023-04-08 19:14:08 +00:00
};
2023-03-29 10:40:05 +00:00
const c1 = new Connection("wss://one.com", opt);
c1.Down = false;
const c2 = new Connection("wss://two.com", opt);
c2.Down = false;
const c3 = new Connection("wss://three.com", opt);
c3.Down = false;
2023-06-01 08:54:25 +00:00
const f = {
relay: "",
strategy: RequestStrategy.DefaultRelays,
2023-05-29 21:25:40 +00:00
filters: [
{
kinds: [1],
authors: ["test"],
},
],
2023-06-01 08:54:25 +00:00
};
const qt1 = q.sendToRelay(c1, f);
const qt2 = q.sendToRelay(c2, f);
const qt3 = q.sendToRelay(c3, f);
2023-03-29 10:40:05 +00:00
expect(q.progress).toBe(0);
2023-06-01 08:54:25 +00:00
q.eose(qt1!.id, c1);
2023-03-29 10:40:05 +00:00
expect(q.progress).toBe(1 / 3);
2023-06-01 08:54:25 +00:00
q.eose(qt1!.id, c1);
2023-03-29 10:40:05 +00:00
expect(q.progress).toBe(1 / 3);
2023-06-01 08:54:25 +00:00
q.eose(qt2!.id, c2);
2023-03-29 10:40:05 +00:00
expect(q.progress).toBe(2 / 3);
2023-06-01 08:54:25 +00:00
q.eose(qt3!.id, c3);
2023-03-29 10:40:05 +00:00
expect(q.progress).toBe(1);
2023-05-24 16:17:17 +00:00
const qs = {
2023-06-01 08:54:25 +00:00
relay: "",
strategy: RequestStrategy.DefaultRelays,
2023-05-24 16:17:17 +00:00
filters: [
2023-04-25 17:01:29 +00:00
{
kinds: [1],
authors: ["test-sub"],
},
],
2023-06-01 08:54:25 +00:00
};
const qt = q.sendToRelay(c1, qs);
2023-03-29 10:40:05 +00:00
2023-05-24 16:17:17 +00:00
expect(q.progress).toBe(3 / 4);
2023-06-01 08:54:25 +00:00
q.eose(qt!.id, c1);
2023-03-29 10:40:05 +00:00
expect(q.progress).toBe(1);
2023-05-29 21:25:40 +00:00
q.sendToRelay(c2, qs);
2023-05-24 16:17:17 +00:00
expect(q.progress).toBe(4 / 5);
2023-03-29 10:40:05 +00:00
});
2023-06-01 08:54:25 +00:00
it("should merge all sub-query filters", () => {
const q = new Query("test", new FlatNoteStore());
const c0 = new Connection("wss://test.com", { read: true, write: true });
q.sendToRelay(c0, {
filters: [
{
authors: ["a"],
kinds: [1],
},
],
relay: "",
strategy: RequestStrategy.DefaultRelays,
});
q.sendToRelay(c0, {
filters: [
{
authors: ["b"],
kinds: [1, 2],
},
],
relay: "",
strategy: RequestStrategy.DefaultRelays,
});
q.sendToRelay(c0, {
filters: [
{
authors: ["c"],
kinds: [2],
},
],
relay: "",
strategy: RequestStrategy.DefaultRelays,
});
expect(q.filters).toEqual([
{
2023-06-08 04:39:10 +00:00
authors: ["a", "b"],
kinds: [1],
},
{
authors: ["b", "c"],
kinds: [2],
2023-06-01 08:54:25 +00:00
},
]);
});
2023-03-29 10:40:05 +00:00
});