mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 03:19:46 +00:00
Add DittoPool tests
This commit is contained in:
parent
920e558c06
commit
239c427c78
2 changed files with 68 additions and 2 deletions
66
packages/ditto/storages/DittoPool.test.ts
Normal file
66
packages/ditto/storages/DittoPool.test.ts
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
import { DittoConf } from '@ditto/conf';
|
||||||
|
import { genEvent, MockRelay } from '@nostrify/nostrify/test';
|
||||||
|
import { assertEquals } from '@std/assert';
|
||||||
|
import { generateSecretKey, getPublicKey, nip19 } from 'nostr-tools';
|
||||||
|
|
||||||
|
import { DittoPool } from './DittoPool.ts';
|
||||||
|
|
||||||
|
Deno.test('DittoPool.reqRouter', async (t) => {
|
||||||
|
const conf = new DittoConf(new Map([['DITTO_NSEC', nip19.nsecEncode(generateSecretKey())]]));
|
||||||
|
const relay = new MockRelay();
|
||||||
|
|
||||||
|
const pool = new DittoPool({ conf, relay });
|
||||||
|
|
||||||
|
const [alex, mk] = [
|
||||||
|
generateKeypair(),
|
||||||
|
generateKeypair(),
|
||||||
|
];
|
||||||
|
|
||||||
|
const [ditto, henhouse, gleasonator] = [
|
||||||
|
'wss://ditto.pub/relay',
|
||||||
|
'wss://henhouse.social/relay',
|
||||||
|
'wss://gleasonator.dev/relay',
|
||||||
|
];
|
||||||
|
|
||||||
|
const events = [
|
||||||
|
genEvent({ kind: 10002, tags: [['r', gleasonator], ['r', ditto]] }, alex.sk),
|
||||||
|
genEvent({ kind: 10002, tags: [['r', henhouse], ['r', ditto]] }, mk.sk),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (const event of events) {
|
||||||
|
await relay.event(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
await t.step('no authors', async () => {
|
||||||
|
const reqRoutes = await pool.reqRouter([{ kinds: [1] }]);
|
||||||
|
assertEquals(reqRoutes, new Map());
|
||||||
|
});
|
||||||
|
|
||||||
|
await t.step('single author', async () => {
|
||||||
|
const reqRoutes = await pool.reqRouter([{ kinds: [10002], authors: [alex.pk] }]);
|
||||||
|
|
||||||
|
const expected = new Map([
|
||||||
|
[ditto, [{ kinds: [10002], authors: [alex.pk] }]],
|
||||||
|
[gleasonator, [{ kinds: [10002], authors: [alex.pk] }]],
|
||||||
|
]);
|
||||||
|
|
||||||
|
assertEquals(reqRoutes, expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
await t.step('multiple authors', async () => {
|
||||||
|
const reqRoutes = await pool.reqRouter([{ kinds: [10002], authors: [alex.pk, mk.pk] }]);
|
||||||
|
|
||||||
|
const expected = new Map([
|
||||||
|
[ditto, [{ kinds: [10002], authors: [alex.pk, mk.pk] }]],
|
||||||
|
[henhouse, [{ kinds: [10002], authors: [mk.pk] }]],
|
||||||
|
[gleasonator, [{ kinds: [10002], authors: [alex.pk] }]],
|
||||||
|
]);
|
||||||
|
|
||||||
|
assertEquals(reqRoutes, expected);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
function generateKeypair(): { pk: string; sk: Uint8Array } {
|
||||||
|
const sk = generateSecretKey();
|
||||||
|
return { pk: getPublicKey(sk), sk };
|
||||||
|
}
|
||||||
|
|
@ -33,7 +33,7 @@ export class DittoPool extends NPool<NRelay1> {
|
||||||
this._opts = opts;
|
this._opts = opts;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async reqRouter(filters: NostrFilter[]): Promise<Map<string, NostrFilter[]>> {
|
async reqRouter(filters: NostrFilter[]): Promise<Map<string, NostrFilter[]>> {
|
||||||
const { conf, relay, maxReqRelays = 5 } = this._opts;
|
const { conf, relay, maxReqRelays = 5 } = this._opts;
|
||||||
|
|
||||||
const routes = new Map<string, NostrFilter[]>();
|
const routes = new Map<string, NostrFilter[]>();
|
||||||
|
|
@ -91,7 +91,7 @@ export class DittoPool extends NPool<NRelay1> {
|
||||||
return routes;
|
return routes;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async eventRouter(event: NostrEvent): Promise<string[]> {
|
async eventRouter(event: NostrEvent): Promise<string[]> {
|
||||||
const { conf, maxEventRelays = 10 } = this._opts;
|
const { conf, maxEventRelays = 10 } = this._opts;
|
||||||
const { pubkey } = event;
|
const { pubkey } = event;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue