From 3219b53682688519c03e323291e29a60503114e3 Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Tue, 16 Apr 2024 13:25:43 -0300 Subject: [PATCH] test: add test for hydrate quote repost without author & fix tests' parallelism problem --- src/storages/hydrate.test.ts | 99 +++++++++++++++++++++++++++--------- 1 file changed, 76 insertions(+), 23 deletions(-) diff --git a/src/storages/hydrate.test.ts b/src/storages/hydrate.test.ts index ab2b41d..1744543 100644 --- a/src/storages/hydrate.test.ts +++ b/src/storages/hydrate.test.ts @@ -1,7 +1,6 @@ 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 { NCache } from 'jsr:@nostrify/nostrify'; 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' }; @@ -16,12 +15,15 @@ import event1reposted from '~/fixtures/events/event-1-reposted.json' with { type 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 () => { + const db = new NCache({ max: 100 }); + + const event0copy = structuredClone(event0); + const event1copy = structuredClone(event1); + // Save events to database - await eventsDB.event(event0); - await eventsDB.event(event1); + await db.event(event0copy); + await db.event(event1copy); assertEquals((event1 as DittoEvent).author, undefined, "Event hasn't been hydrated yet"); @@ -31,25 +33,32 @@ Deno.test('hydrate author', async () => { await hydrateEvents({ events: [event1], relations: ['author'], - storage: eventsDB, + storage: db, signal: controller.signal, }); const expectedEvent = { ...event1, author: event0 }; assertEquals(event1, expectedEvent); - await eventsDB.remove([{ kinds: [0, 1] }]); - assertEquals(await eventsDB.query([{ kinds: [0, 1] }]), []); + await db.remove([{ kinds: [0, 1] }]); + assertEquals(await db.query([{ kinds: [0, 1] }]), []); clearTimeout(timeoutId); }); Deno.test('hydrate repost', async () => { + const db = new NCache({ max: 100 }); + + const event0madePostCopy = structuredClone(event0madePost); + const event0madeRepostCopy = structuredClone(event0madeRepost); + const event1repostedCopy = structuredClone(event1reposted); + const event6copy = structuredClone(event6); + // Save events to database - await eventsDB.event(event0madePost); - await eventsDB.event(event0madeRepost); - await eventsDB.event(event1reposted); - await eventsDB.event(event6); + await db.event(event0madePostCopy); + await db.event(event0madeRepostCopy); + await db.event(event1repostedCopy); + await db.event(event6copy); 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"); @@ -60,25 +69,32 @@ Deno.test('hydrate repost', async () => { await hydrateEvents({ events: [event6], relations: ['repost', 'author'], - storage: eventsDB, + storage: db, 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] }]), []); + await db.remove([{ kinds: [0, 1, 6] }]); + assertEquals(await db.query([{ kinds: [0, 1, 6] }]), []); clearTimeout(timeoutId); }); Deno.test('hydrate quote repost with hydrate author', async () => { + const db = new NCache({ max: 100 }); + + const event0madeQuoteRepostCopy = structuredClone(event0madeQuoteRepost); + const event0copy = structuredClone(event0); + const event1quoteRepostCopy = structuredClone(event1quoteRepost); + const event1willBeQuoteRepostedCopy = structuredClone(event1willBeQuoteReposted); + // Save events to database - await eventsDB.event(event0madeQuoteRepost); - await eventsDB.event(event0); - await eventsDB.event(event1quoteRepost); - await eventsDB.event(event1willBeQuoteReposted); + await db.event(event0madeQuoteRepostCopy); + await db.event(event0copy); + await db.event(event1quoteRepostCopy); + await db.event(event1willBeQuoteRepostedCopy); const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), 1000); @@ -86,7 +102,7 @@ Deno.test('hydrate quote repost with hydrate author', async () => { await hydrateEvents({ events: [event1quoteRepost], relations: ['author', 'quote_repost'], // if author is called first the performance will be better - storage: eventsDB, + storage: db, signal: controller.signal, }); @@ -98,8 +114,45 @@ Deno.test('hydrate quote repost with hydrate author', async () => { assertEquals(event1quoteRepost, expectedEvent1quoteRepost); - await eventsDB.remove([{ kinds: [0, 1] }]); - assertEquals(await eventsDB.query([{ kinds: [0, 1] }]), []); + await db.remove([{ kinds: [0, 1] }]); + assertEquals(await db.query([{ kinds: [0, 1] }]), []); + + clearTimeout(timeoutId); +}); + +Deno.test('hydrate quote repost WITHOUT hydrate author', async () => { + const db = new NCache({ max: 100 }); + + const event0madeQuoteRepostCopy = structuredClone(event0madeQuoteRepost); + const event0copy = structuredClone(event0); + const event1quoteRepostCopy = structuredClone(event1quoteRepost); + const event1willBeQuoteRepostedCopy = structuredClone(event1willBeQuoteReposted); + + // Save events to database + await db.event(event0madeQuoteRepostCopy); + await db.event(event0copy); + await db.event(event1quoteRepostCopy); + await db.event(event1willBeQuoteRepostedCopy); + + const controller = new AbortController(); + const timeoutId = setTimeout(() => controller.abort(), 1000); + + await hydrateEvents({ + events: [event1quoteRepost, event1willBeQuoteReposted], + relations: ['quote_repost'], + storage: db, + signal: controller.signal, + }); + + const expectedEvent1quoteRepost = { + ...event1quoteRepost, + quote_repost: { ...event1willBeQuoteReposted, author: event0 }, + }; + + assertEquals(event1quoteRepost, expectedEvent1quoteRepost); + + await db.remove([{ kinds: [0, 1] }]); + assertEquals(await db.query([{ kinds: [0, 1] }]), []); clearTimeout(timeoutId); });