From 33eead2148605a16c87482242ad04bce7d953dea Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 28 Dec 2023 13:26:41 -0600 Subject: [PATCH] Add filter.test.ts --- fixtures/events/event-0.json | 15 +++++++++++++++ fixtures/events/event-1.json | 15 +++++++++++++++ src/filter.test.ts | 37 ++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 fixtures/events/event-0.json create mode 100644 fixtures/events/event-1.json create mode 100644 src/filter.test.ts diff --git a/fixtures/events/event-0.json b/fixtures/events/event-0.json new file mode 100644 index 0000000..907e1a1 --- /dev/null +++ b/fixtures/events/event-0.json @@ -0,0 +1,15 @@ +{ + "id": "63d38c9b483d2d98a46382eadefd272e0e4bdb106a5b6eddb400c4e76f693d35", + "pubkey": "79c2cae114ea28a981e7559b4fe7854a473521a8d22a66bbab9fa248eb820ff6", + "created_at": 1699398376, + "kind": 0, + "tags": [ + [ + "proxy", + "https://gleasonator.com/users/alex", + "activitypub" + ] + ], + "content": "{\"name\":\"Alex Gleason\",\"about\":\"I create Fediverse software that empowers people online.\\n\\nI'm vegan btw.\\n\\nNote: If you have a question for me, please tag me publicly. This gives the opportunity for others to chime in, and bystanders to learn.\",\"picture\":\"https://media.gleasonator.com/aae0071188681629f200ab41502e03b9861d2754a44c008d3869c8a08b08d1f1.png\",\"banner\":\"https://media.gleasonator.com/e5f6e0e380536780efa774e8d3c8a5a040e3f9f99dbb48910b261c32872ee3a3.gif\",\"nip05\":\"alex_at_gleasonator.com@mostr.pub\",\"lud16\":\"alex@alexgleason.me\"}", + "sig": "9d48bbb600aab44abaeee11c97f1753f1d7de08378e9b33d84f9be893a09270aeceecfde3cfb698c555ae1bde3e4e54b3463a61bb99bdf673d64c2202f98b0e9" +} \ No newline at end of file diff --git a/fixtures/events/event-1.json b/fixtures/events/event-1.json new file mode 100644 index 0000000..f902786 --- /dev/null +++ b/fixtures/events/event-1.json @@ -0,0 +1,15 @@ +{ + "kind": 1, + "content": "I'm vegan btw", + "tags": [ + [ + "proxy", + "https://gleasonator.com/objects/8f6fac53-4f66-4c6e-ac7d-92e5e78c3e79", + "activitypub" + ] + ], + "pubkey": "79c2cae114ea28a981e7559b4fe7854a473521a8d22a66bbab9fa248eb820ff6", + "created_at": 1691091365, + "id": "55920b758b9c7b17854b6e3d44e6a02a83d1cb49e1227e75a30426dea94d4cb2", + "sig": "a72f12c08f18e85d98fb92ae89e2fe63e48b8864c5e10fbdd5335f3c9f936397a6b0a7350efe251f8168b1601d7012d4a6d0ee6eec958067cf22a14f5a5ea579" +} \ No newline at end of file diff --git a/src/filter.test.ts b/src/filter.test.ts new file mode 100644 index 0000000..efc00d7 --- /dev/null +++ b/src/filter.test.ts @@ -0,0 +1,37 @@ +import { type Event } from '@/deps.ts'; +import { assertEquals } from '@/deps-test.ts'; + +import event0 from '~/fixtures/events/event-0.json' assert { type: 'json' }; +import event1 from '~/fixtures/events/event-1.json' assert { type: 'json' }; + +import { eventToMicroFilter, getFilterId, getMicroFilters, isMicrofilter } from './filter.ts'; + +Deno.test('getMicroFilters', () => { + const event = event0 as Event<0>; + const microfilters = getMicroFilters(event); + assertEquals(microfilters.length, 2); + assertEquals(microfilters[0], { authors: [event.pubkey], kinds: [0] }); + assertEquals(microfilters[1], { ids: [event.id] }); +}); + +Deno.test('eventToMicroFilter', () => { + assertEquals(eventToMicroFilter(event0), { authors: [event0.pubkey], kinds: [0] }); + assertEquals(eventToMicroFilter(event1), { ids: [event1.id] }); +}); + +Deno.test('isMicrofilter', () => { + assertEquals(isMicrofilter({ ids: [event0.id] }), true); + assertEquals(isMicrofilter({ authors: [event0.pubkey], kinds: [0] }), true); + assertEquals(isMicrofilter({ ids: [event0.id], authors: [event0.pubkey], kinds: [0] }), false); +}); + +Deno.test('getFilterId', () => { + assertEquals( + getFilterId({ ids: [event0.id] }), + '{"ids":["63d38c9b483d2d98a46382eadefd272e0e4bdb106a5b6eddb400c4e76f693d35"]}', + ); + assertEquals( + getFilterId({ authors: [event0.pubkey], kinds: [0] }), + '{"authors":["79c2cae114ea28a981e7559b4fe7854a473521a8d22a66bbab9fa248eb820ff6"],"kinds":[0]}', + ); +});