Skip to content

Commit fb110db

Browse files
authored
Merge pull request #369 from nostr-dev-kit/feature/events-tests
Fix `src/events` tests
2 parents 9855d32 + 487a6ad commit fb110db

12 files changed

Lines changed: 181 additions & 85 deletions

core/src/events/content-tagger.test.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { describe, expect, it } from "vitest";
12
import { generateContentTags } from "./content-tagger";
23
import type { ContentTaggingOptions, NDKEvent, NDKTag } from "./index.js";
34

@@ -199,7 +200,7 @@ describe("await generateContentTags", () => {
199200
]);
200201
});
201202

202-
it("copies p-tags from target event when copyPTagsFromTarget is true", async () => {
203+
it("do not copy p-tags from target event when copyPTagsFromTarget is true but pubkeys are invalid", async () => {
203204
const content = "Reply to event";
204205
const opts: ContentTaggingOptions = { copyPTagsFromTarget: true };
205206
const mockEvent = {
@@ -216,9 +217,29 @@ describe("await generateContentTags", () => {
216217

217218
const { tags: processedTags } = await generateContentTags(content, [], opts, mockEvent);
218219

220+
expect(processedTags).toEqual([]);
221+
});
222+
223+
it("copies p-tags from target event when copyPTagsFromTarget is true and pubkeys are valid", async () => {
224+
const content = "Reply to event";
225+
const opts: ContentTaggingOptions = { copyPTagsFromTarget: true };
226+
const mockEvent = {
227+
getMatchingTags: (tag: string) => {
228+
if (tag === "p") {
229+
return [
230+
["p", "fa984bd7dbb282f07e16e7ae87b26a2a7b9b90b7246a44771f0cf5ae58018f52"],
231+
["p", "b190175cef407c593f17c155480eda1a2ee7f6c84378eef0e97353f87ee59300"],
232+
];
233+
}
234+
return [];
235+
},
236+
} as unknown as NDKEvent;
237+
238+
const { tags: processedTags } = await generateContentTags(content, [], opts, mockEvent);
239+
219240
expect(processedTags).toEqual([
220-
["p", "pubkey1"],
221-
["p", "pubkey2"],
241+
["p", "fa984bd7dbb282f07e16e7ae87b26a2a7b9b90b7246a44771f0cf5ae58018f52"],
242+
["p", "b190175cef407c593f17c155480eda1a2ee7f6c84378eef0e97353f87ee59300"],
222243
]);
223244
});
224245

core/src/events/encryption.test.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ class MockCacheAdapter implements NDKCacheAdapter {
2525
return Promise.resolve();
2626
}
2727

28-
getDecryptedEvent(eventId: string): NDKEvent | null {
29-
return this.decryptedEvents.get(eventId) || null;
28+
getDecryptedEvent(wrapperId: string): NDKEvent | null {
29+
return this.decryptedEvents.get(wrapperId) || null;
3030
}
3131

32-
addDecryptedEvent(event: NDKEvent): void {
33-
this.decryptedEvents.set(event.id, event);
32+
addDecryptedEvent(wrapperId: string, decryptedEvent: NDKEvent): void {
33+
this.decryptedEvents.set(wrapperId, decryptedEvent);
3434
}
3535
}
3636

@@ -369,8 +369,7 @@ describe("NDKEvent encryption (Nip44 & Nip59)", () => {
369369
vi.spyOn(giftWrappingModule, "giftWrap").mockImplementation(async (event, _recipient, _signer, params = {}) => {
370370
const method = params.scheme === "nip04" ? "nip04_encrypt" : "nip44_encrypt";
371371
mockSendRequest("", method, {}, 0, () => {});
372-
const wrapped = new NDKEvent(event.ndk);
373-
return wrapped;
372+
return new NDKEvent(event.ndk);
374373
});
375374

376375
await giftWrappingModule.giftWrap(message, receiveUser, send46Signer);
@@ -407,12 +406,11 @@ describe("NDKEvent encryption (Nip44 & Nip59)", () => {
407406

408407
// Set up mock cache adapter
409408
const mockCache = new MockCacheAdapter();
410-
mockCache.addDecryptedEvent(decryptedEvent);
409+
mockCache.addDecryptedEvent(encryptedEvent.id, decryptedEvent);
411410
fixture.ndk.cacheAdapter = mockCache;
412411

413412
// Spy on cache methods
414413
const getDecryptedEventSpy = vi.spyOn(mockCache, "getDecryptedEvent");
415-
const _addDecryptedEventSpy = vi.spyOn(mockCache, "addDecryptedEvent");
416414

417415
// Mock the decrypt function for signer to verify it's not called
418416
const decryptSpy = vi.spyOn(receiveSigner, "decrypt");
@@ -465,7 +463,7 @@ describe("NDKEvent encryption (Nip44 & Nip59)", () => {
465463
expect(getDecryptedEventSpy).toHaveBeenCalledWith(encryptedEvent.id);
466464

467465
// Verify the decrypted event was cached
468-
expect(addDecryptedEventSpy).toHaveBeenCalledWith(encryptedEvent);
466+
expect(addDecryptedEventSpy).toHaveBeenCalledWith(encryptedEvent.id, encryptedEvent);
469467

470468
// Verify content is correct
471469
expect(encryptedEvent.content).toBe(original);

core/src/events/gift-wrapping.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
import { beforeEach, describe, expect, it } from "vitest";
2-
import { NDK } from "../ndk/index.js";
3-
import { NDKPrivateKeySigner } from "../signers/private-key/index.js";
4-
import type { NDKCacheAdapter, NDKEventId } from "../cache/index.js";
2+
import type { NDKCacheAdapter } from "../cache";
3+
import { NDK } from "../ndk";
4+
import { NDKPrivateKeySigner } from "../signers/private-key";
5+
import { giftUnwrap, giftWrap } from "./gift-wrapping.js";
56
import { NDKEvent } from "./index.js";
6-
import { NDKKind } from "./kinds/index.js";
7-
import { giftWrap, giftUnwrap } from "./gift-wrapping.js";
7+
import { NDKKind } from "./kinds";
88

99
// Mock cache adapter to track cache operations
1010
class MockCacheAdapter implements Partial<NDKCacheAdapter> {
1111
public locking = false;
12-
private cache = new Map<NDKEventId, NDKEvent>();
12+
private cache = new Map<NDKEvent["id"], NDKEvent>();
1313
public getDecryptedEventCalls: string[] = [];
1414
public addDecryptedEventCalls: Array<{ wrapperId: string; rumorId: string }> = [];
1515

16-
async getDecryptedEvent(wrapperId: NDKEventId): Promise<NDKEvent | null> {
16+
async getDecryptedEvent(wrapperId: NDKEvent["id"]): Promise<NDKEvent | null> {
1717
this.getDecryptedEventCalls.push(wrapperId);
1818
return this.cache.get(wrapperId) ?? null;
1919
}
2020

21-
async addDecryptedEvent(wrapperId: NDKEventId, decryptedEvent: NDKEvent): Promise<void> {
21+
async addDecryptedEvent(wrapperId: NDKEvent["id"], decryptedEvent: NDKEvent): Promise<void> {
2222
this.addDecryptedEventCalls.push({ wrapperId, rumorId: decryptedEvent.id });
2323
this.cache.set(wrapperId, decryptedEvent);
2424
}

core/src/events/kinds/follow-pack.test.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,26 @@ describe("NDKFollowPack", () => {
9090
});
9191
});
9292

93-
it("should get/set pubkeys and manipulate p tags", () => {
93+
it("should get/set pubkeys and manipulate p tags if pubkeys are invalid", () => {
9494
const fp = new NDKFollowPack();
9595
expect(fp.pubkeys).toEqual([]);
9696

9797
fp.pubkeys = ["pk1", "pk2"];
98-
expect(fp.pubkeys).toEqual(["pk1", "pk2"]);
98+
expect(fp.pubkeys).toEqual([]);
99+
});
100+
101+
it("should get/set pubkeys and manipulate p tags are valid", () => {
102+
const fp = new NDKFollowPack();
103+
expect(fp.pubkeys).toEqual([]);
104+
105+
fp.pubkeys = [
106+
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
107+
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
108+
];
109+
expect(fp.pubkeys).toEqual([
110+
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
111+
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
112+
]);
99113
expect(fp.tags.filter((t) => t[0] === "p").length).toBe(2);
100114

101115
fp.pubkeys = [];

core/src/events/kinds/interest-list.test.ts

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { beforeEach, describe, expect, test } from "bun:test";
2-
import { NDKEvent } from "../../events/index.js";
3-
import { NDK } from "../../ndk/index.js";
1+
import { beforeEach, describe, expect, test } from "vitest";
2+
import { NDK } from "../../ndk";
3+
import { NDKEvent } from "../index";
44
import { NDKKind } from "./index.js";
55
import { NDKInterestList } from "./interest-list.js";
66

@@ -29,6 +29,18 @@ describe("NDKInterestList", () => {
2929
expect(interests).toEqual(["nostr", "bitcoin", "technology"]);
3030
});
3131

32+
test("filters out falsy interest tag values", () => {
33+
const list = new NDKInterestList(ndk);
34+
list.tags = [
35+
["t", "nostr"],
36+
["t", ""],
37+
["t", undefined as any],
38+
["t", "bitcoin"],
39+
];
40+
41+
expect(list.interests).toEqual(["nostr", "bitcoin"]);
42+
});
43+
3244
test("sets interests replacing existing ones", () => {
3345
const list = new NDKInterestList(ndk);
3446
list.tags = [
@@ -51,6 +63,35 @@ describe("NDKInterestList", () => {
5163
expect(list.interests).toEqual(["nostr", "bitcoin"]);
5264
});
5365

66+
test("removeInterest is a no-op when interest does not exist", () => {
67+
const list = new NDKInterestList(ndk);
68+
list.tags = [
69+
["t", "nostr"],
70+
["title", "My Interests"],
71+
["t", "bitcoin"],
72+
];
73+
74+
const beforeTags = list.tags.map((t) => [...t]);
75+
76+
list.removeInterest("technology");
77+
78+
expect(list.tags).toEqual(beforeTags);
79+
expect(list.interests).toEqual(["nostr", "bitcoin"]);
80+
});
81+
82+
test("removes only the first matching interest tag", () => {
83+
const list = new NDKInterestList(ndk);
84+
list.tags = [
85+
["t", "nostr"],
86+
["t", "nostr"],
87+
["t", "bitcoin"],
88+
];
89+
90+
list.removeInterest("nostr");
91+
92+
expect(list.interests).toEqual(["nostr", "bitcoin"]);
93+
});
94+
5495
test("removes interest", () => {
5596
const list = new NDKInterestList(ndk);
5697
list.interests = ["nostr", "bitcoin", "technology"];
@@ -99,22 +140,32 @@ describe("NDKInterestList", () => {
99140
expect(list.interestSetReferences).toEqual([]);
100141
});
101142

102-
test("updates created_at when adding interest", () => {
143+
test("does not set created_at when adding interest (local mutation only)", () => {
103144
const list = new NDKInterestList(ndk);
104-
const before = Math.floor(Date.now() / 1000);
145+
expect(list.created_at).toBeUndefined();
105146

106147
list.addInterest("nostr");
107148

108-
expect(list.created_at).toBeGreaterThanOrEqual(before);
149+
expect(list.created_at).toBeUndefined();
109150
});
110151

111-
test("updates created_at when removing interest", () => {
152+
test("does not set created_at when removing interest (local mutation only)", () => {
112153
const list = new NDKInterestList(ndk);
113154
list.interests = ["nostr", "bitcoin"];
114-
const before = Math.floor(Date.now() / 1000);
155+
expect(list.created_at).toBeUndefined();
115156

116157
list.removeInterest("nostr");
117158

118-
expect(list.created_at).toBeGreaterThanOrEqual(before);
159+
expect(list.created_at).toBeUndefined();
160+
});
161+
162+
test("does not set created_at when removing a missing interest (no-op)", () => {
163+
const list = new NDKInterestList(ndk);
164+
list.interests = ["nostr", "bitcoin"];
165+
expect(list.created_at).toBeUndefined();
166+
167+
list.removeInterest("technology");
168+
169+
expect(list.created_at).toBeUndefined();
119170
});
120171
});

0 commit comments

Comments
 (0)