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" ;
44import { NDKKind } from "./index.js" ;
55import { 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