Merge branch 'test-hydrate' into 'main'

Add tests for hydrate repost and hydrate author

See merge request soapbox-pub/ditto!157
This commit is contained in:
Alex Gleason 2024-04-15 13:36:05 +00:00
commit 220bbeaba8
6 changed files with 115 additions and 1 deletions

View File

@ -0,0 +1,9 @@
{
"id": "155ee53d9319d427ac0d5a8f0089654d8db66d0f1b31d8bd0d389b7a5417992f",
"pubkey": "04c915daefee38317fa734444acee390a8269fe5810b2241e5e6dd343dfbecc9",
"created_at": 1712507673,
"kind": 0,
"tags": [],
"content": "{\"displayName\":\"ODELL\",\"pubkey\":\"04c915daefee38317fa734444acee390a8269fe5810b2241e5e6dd343dfbecc9\",\"npub\":\"npub1qny3tkh0acurzla8x3zy4nhrjz5zd8l9sy9jys09umwng00manysew95gx\",\"created_at\":1712419265,\"display_name\":\"ODELL\",\"name\":\"ODELL\",\"website\":\"https://odell.xyz\",\"about\":\"FREEDOM TECH IS HOPE 🫡 | COFOUNDER - OPENSATS AND BITCOIN PARK | MANAGING PARTNER - TEN31 |\",\"lud16\":\"odell@vlt.ge\",\"nip05\":\"odell@werunbtc.com\",\"picture\":\"https://m.primal.net/Hrsv.webp\",\"banner\":\"https://m.primal.net/HqQz.jpg\"}",
"sig": "355663a38ebd1cb31b5d2864357c4fdbeecfaedd602133638cf255ea30e6e532e3c927495c5eb5dfc0e77046d2e1d4e8be271f279c2c7eb9c9273028bfc033f4"
}

View File

@ -0,0 +1,9 @@
{
"id": "343e756c454d1fe623e0ea5a7653e5d0cb643fee49acef4b4e8df7645d27c8e4",
"pubkey": "82341f882b6eabcd2ba7f1ef90aad961cf074af15b9ef44a09f9d2a8fbfbe6a2",
"created_at": 1712835974,
"kind": 0,
"tags": [],
"content": "{\"lud16\":\"jack@primal.net\",\"about\":\"bitcoin & chill\",\"picture\":\"https:\\/\\/nostr.build\\/i\\/p\\/nostr.build_6b9909bccf0f4fdaf7aacd9bc01e4ce70dab86f7d90395f2ce925e6ea06ed7cd.jpeg\",\"display_name\":\"\",\"lud06\":\"\",\"banner\":\"https:\\/\\/upload.wikimedia.org\\/wikipedia\\/commons\\/b\\/b4\\/The_Sun_by_the_Atmospheric_Imaging_Assembly_of_NASA%27s_Solar_Dynamics_Observatory_-_20100819.jpg\",\"website\":\"\",\"nip05\":\"\",\"name\":\"jack\"}",
"sig": "f3a896b67145eeca606c30375a146055c424ce1216f3e894d720733912aba3a90cf70e018d131246977c85b4ed9491fa43843e7728caab6b28da5d80decf9045"
}

View File

@ -0,0 +1,9 @@
{
"id": "b460c8eb16ce7b3ebe3079240c47bb7cace5ad2bd0246fab1ed36a12fb816b1e",
"pubkey": "04c915daefee38317fa734444acee390a8269fe5810b2241e5e6dd343dfbecc9",
"created_at": 1713045404,
"kind": 1,
"tags": [],
"content": "BITCOIN IS THE ONLY GLOBAL FREE MARKET. 24/7/365 LIQUIDITY. NO CIRCUIT BREAKERS. FEATURE NOT A BUG. STAY HUMBLE AND STACK SATS.",
"sig": "b56ddd466d00de591f371b1933e73deeeba2f56f4b0ee8179b3f6a6b4e45f6bb850a6802d6be17055d13c699b733f610b69fbfbbfbcd609c526b8c5097b28505"
}

View File

@ -0,0 +1,18 @@
{
"id": "863185c0b3a18316f438b7920a1f5217ac5a0f2a078dc003f9969512e8c8a5de",
"pubkey": "82341f882b6eabcd2ba7f1ef90aad961cf074af15b9ef44a09f9d2a8fbfbe6a2",
"created_at": 1713045438,
"kind": 6,
"tags": [
[
"e",
"b460c8eb16ce7b3ebe3079240c47bb7cace5ad2bd0246fab1ed36a12fb816b1e"
],
[
"p",
"04c915daefee38317fa734444acee390a8269fe5810b2241e5e6dd343dfbecc9"
]
],
"content": "{\"content\":\"BITCOIN IS THE ONLY GLOBAL FREE MARKET. 24\\/7\\/365 LIQUIDITY. NO CIRCUIT BREAKERS. FEATURE NOT A BUG. STAY HUMBLE AND STACK SATS.\",\"id\":\"b460c8eb16ce7b3ebe3079240c47bb7cace5ad2bd0246fab1ed36a12fb816b1e\",\"tags\":[],\"sig\":\"b56ddd466d00de591f371b1933e73deeeba2f56f4b0ee8179b3f6a6b4e45f6bb850a6802d6be17055d13c699b733f610b69fbfbbfbcd609c526b8c5097b28505\",\"created_at\":1713045404,\"kind\":1,\"pubkey\":\"04c915daefee38317fa734444acee390a8269fe5810b2241e5e6dd343dfbecc9\"}",
"sig": "3bf75ad219c87194679ac08c2d90c1845cb0352633d73e1cf1b7e83e7be61a18a47dcb4746b4b9dfc1d640b7578a4a602d2f68eec605494a335c0732b3b61f37"
}

View File

@ -4,7 +4,7 @@ import { assertEquals, assertRejects } from '@/deps-test.ts';
import event0 from '~/fixtures/events/event-0.json' with { type: 'json' };
import event1 from '~/fixtures/events/event-1.json' with { type: 'json' };
import { EventsDB } from './events-db.ts';
import { EventsDB } from '@/storages/events-db.ts';
const eventsDB = new EventsDB(db);

View File

@ -0,0 +1,69 @@
import { assertEquals } from '@/deps-test.ts';
import { EventsDB } from '@/storages/events-db.ts';
import { db } from '@/db.ts';
import { hydrateEvents } from '@/storages/hydrate.ts';
import event0 from '~/fixtures/events/event-0.json' with { type: 'json' };
import event0madePost from '~/fixtures/events/event-0-the-one-who-post-and-users-repost.json' with { type: 'json' };
import event0madeRepost from '~/fixtures/events/event-0-the-one-who-repost.json' with { type: 'json' };
import event1 from '~/fixtures/events/event-1.json' with { type: 'json' };
import event1reposted from '~/fixtures/events/event-1-reposted.json' with { type: 'json' };
import event6 from '~/fixtures/events/event-6.json' with { type: 'json' };
import { DittoEvent } from '@/interfaces/DittoEvent.ts';
const eventsDB = new EventsDB(db);
Deno.test('hydrate author', async () => {
// Save events to database
await eventsDB.event(event0);
await eventsDB.event(event1);
assertEquals((event1 as DittoEvent).author, undefined, "Event hasn't been hydrated yet");
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 1000);
await hydrateEvents({
events: [event1],
relations: ['author'],
storage: eventsDB,
signal: controller.signal,
});
const expectedEvent = { ...event1, author: event0 };
assertEquals(event1, expectedEvent);
await eventsDB.remove([{ kinds: [0, 1] }]);
assertEquals(await eventsDB.query([{ kinds: [0, 1] }]), []);
clearTimeout(timeoutId);
});
Deno.test('hydrate repost', async () => {
// Save events to database
await eventsDB.event(event0madePost);
await eventsDB.event(event0madeRepost);
await eventsDB.event(event1reposted);
await eventsDB.event(event6);
assertEquals((event6 as DittoEvent).author, undefined, "Event hasn't been hydrated author yet");
assertEquals((event6 as DittoEvent).repost, undefined, "Event hasn't been hydrated repost yet");
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 1000);
await hydrateEvents({
events: [event6],
relations: ['repost', 'author'],
storage: eventsDB,
signal: controller.signal,
});
const expectedEvent6 = { ...event6, author: event0madeRepost, repost: { ...event1reposted, author: event0madePost } };
assertEquals(event6, expectedEvent6);
await eventsDB.remove([{ kinds: [0, 1, 6] }]);
assertEquals(await eventsDB.query([{ kinds: [0, 1, 6] }]), []);
clearTimeout(timeoutId);
});