test query progress

This commit is contained in:
Kieran 2023-03-29 11:40:05 +01:00
parent e9fd08d808
commit 8c44d123bd
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
2 changed files with 66 additions and 1 deletions

View File

@ -0,0 +1,60 @@
import { Connection, RelaySettings } from "@snort/nostr";
import { unixNow } from "Util";
import { Query } from "./Query";
describe("query", () => {
test("progress", () => {
const q = new Query("test", {
filters: [
{
kinds: [1],
authors: ["test"],
},
],
started: unixNow(),
});
const opt = {
read: true,
write: true,
} as RelaySettings;
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;
q.sendToRelay(c1);
q.sendToRelay(c2);
q.sendToRelay(c3);
expect(q.progress).toBe(0);
q.eose(q.id, c1.Address);
expect(q.progress).toBe(1 / 3);
q.eose(q.id, c1.Address);
expect(q.progress).toBe(1 / 3);
q.eose(q.id, c2.Address);
expect(q.progress).toBe(2 / 3);
q.eose(q.id, c3.Address);
expect(q.progress).toBe(1);
const qs = new Query("test-1", {
filters: [
{
kinds: [1],
authors: ["test-sub"],
},
],
started: unixNow(),
});
q.subQueries.push(qs);
qs.sendToRelay(c1);
expect(q.progress).toBe(0.5);
q.eose(qs.id, c1.Address);
expect(q.progress).toBe(1);
qs.sendToRelay(c2);
// 1 + 0.5 (1/2 sent sub query)
expect(q.progress).toBe(1.5 / 2);
});
});

View File

@ -102,6 +102,8 @@ export class Query {
const subQ = this.subQueries.find(a => a.id === sub);
if (subQ) {
subQ.eose(sub, relay);
} else {
throw new Error("No query found");
}
}
}
@ -110,7 +112,10 @@ export class Query {
* Get the progress to EOSE, can be used to determine when we should load more content
*/
get progress() {
const thisProgress = this.#eoseRelays.size / this.#sentToRelays.reduce((acc, v) => (acc += v.Down ? 0 : 1), 0);
let thisProgress = this.#eoseRelays.size / this.#sentToRelays.reduce((acc, v) => (acc += v.Down ? 0 : 1), 0);
if (isNaN(thisProgress)) {
thisProgress = 0;
}
if (this.subQueries.length === 0) {
return thisProgress;
}