1
+ import { describe , it , expect } from "vitest" ;
2
+ import { NDKFollowPack } from "./follow-pack" ;
3
+ import { NDKKind } from "./index" ;
4
+ import type { NDKImetaTag } from "../../utils/imeta" ;
5
+ import { mapImetaTag } from "../../utils/imeta" ;
6
+
7
+ // Helper imeta tag object
8
+ const imetaObj : NDKImetaTag = {
9
+ url : "https://example.com/image.png" ,
10
+ alt : "Example image" ,
11
+ dim : "100x100"
12
+ } ;
13
+
14
+ describe ( "NDKFollowPack" , ( ) => {
15
+ describe ( "Kind Handling" , ( ) => {
16
+ it ( "should default to NDKKind.FollowPack (39089)" , ( ) => {
17
+ const fp = new NDKFollowPack ( ) ;
18
+ expect ( fp . kind ) . toBe ( NDKKind . FollowPack ) ;
19
+ } ) ;
20
+
21
+ it ( "should accept NDKKind.FollowPack and NDKKind.MediaFollowPack" , ( ) => {
22
+ const fp = new NDKFollowPack ( ) ;
23
+ fp . kind = NDKKind . FollowPack ;
24
+ expect ( NDKFollowPack . kinds ) . toContain ( fp . kind ) ;
25
+
26
+ fp . kind = NDKKind . MediaFollowPack ;
27
+ expect ( NDKFollowPack . kinds ) . toContain ( fp . kind ) ;
28
+ } ) ;
29
+ } ) ;
30
+
31
+ describe ( "Getters/Setters" , ( ) => {
32
+ it ( "should get/set title and manipulate the title tag" , ( ) => {
33
+ const fp = new NDKFollowPack ( ) ;
34
+ expect ( fp . title ) . toBeUndefined ( ) ;
35
+
36
+ fp . title = "Test Title" ;
37
+ expect ( fp . title ) . toBe ( "Test Title" ) ;
38
+ expect ( fp . tags . find ( t => t [ 0 ] === "title" ) ) . toEqual ( [ "title" , "Test Title" ] ) ;
39
+
40
+ fp . title = undefined ;
41
+ expect ( fp . title ) . toBeUndefined ( ) ;
42
+ expect ( fp . tags . find ( t => t [ 0 ] === "title" ) ) . toBeUndefined ( ) ;
43
+ } ) ;
44
+
45
+ it ( "should get/set identifier (d tag)" , ( ) => {
46
+ const fp = new NDKFollowPack ( ) ;
47
+ // No explicit getter/setter, use setTag/tagValue
48
+ expect ( fp . tagValue ( "d" ) ) . toBeUndefined ( ) ;
49
+
50
+ fp [ "setTag" ] ( "d" , "identifier-123" ) ;
51
+ expect ( fp . tagValue ( "d" ) ) . toBe ( "identifier-123" ) ;
52
+
53
+ fp [ "setTag" ] ( "d" , undefined ) ;
54
+ expect ( fp . tagValue ( "d" ) ) . toBeUndefined ( ) ;
55
+ } ) ;
56
+
57
+ describe ( "Image" , ( ) => {
58
+ it ( "should get/set image with string URL" , ( ) => {
59
+ const fp = new NDKFollowPack ( ) ;
60
+ fp . image = "https://example.com/image.png" ;
61
+ expect ( fp . image ) . toBe ( "https://example.com/image.png" ) ;
62
+ expect ( fp . tags . find ( t => t [ 0 ] === "image" ) ) . toEqual ( [ "image" , "https://example.com/image.png" ] ) ;
63
+ } ) ;
64
+
65
+ it ( "should get/set image with NDKImetaTag object" , ( ) => {
66
+ const fp = new NDKFollowPack ( ) ;
67
+ fp . image = imetaObj ;
68
+ // Should set both imeta and image tags
69
+ const imetaTag = fp . tags . find ( t => t [ 0 ] === "imeta" ) ;
70
+ expect ( imetaTag ) . toBeDefined ( ) ;
71
+ expect ( mapImetaTag ( imetaTag ! as any ) . url ) . toBe ( imetaObj . url ) ;
72
+ expect ( fp . tags . find ( t => t [ 0 ] === "image" ) ) . toEqual ( [ "image" , imetaObj . url ] ) ;
73
+ } ) ;
74
+
75
+ it ( "should prefer imeta over image when both are present" , ( ) => {
76
+ const fp = new NDKFollowPack ( ) ;
77
+ fp . image = "https://fallback.com/image.png" ;
78
+ fp . image = imetaObj ; // This sets both imeta and image tags
79
+ expect ( fp . image ) . toBe ( imetaObj . url ) ;
80
+ } ) ;
81
+
82
+ it ( "should remove both imeta and image tags when image is set to undefined" , ( ) => {
83
+ const fp = new NDKFollowPack ( ) ;
84
+ fp . image = imetaObj ;
85
+ expect ( fp . tags . find ( t => t [ 0 ] === "imeta" ) ) . toBeDefined ( ) ;
86
+ expect ( fp . tags . find ( t => t [ 0 ] === "image" ) ) . toBeDefined ( ) ;
87
+
88
+ fp . image = undefined ;
89
+ expect ( fp . tags . find ( t => t [ 0 ] === "imeta" ) ) . toBeUndefined ( ) ;
90
+ expect ( fp . tags . find ( t => t [ 0 ] === "image" ) ) . toBeUndefined ( ) ;
91
+ } ) ;
92
+ } ) ;
93
+
94
+ it ( "should get/set pubkeys and manipulate p tags" , ( ) => {
95
+ const fp = new NDKFollowPack ( ) ;
96
+ expect ( fp . pubkeys ) . toEqual ( [ ] ) ;
97
+
98
+ fp . pubkeys = [ "pk1" , "pk2" ] ;
99
+ expect ( fp . pubkeys ) . toEqual ( [ "pk1" , "pk2" ] ) ;
100
+ expect ( fp . tags . filter ( t => t [ 0 ] === "p" ) . length ) . toBe ( 2 ) ;
101
+
102
+ fp . pubkeys = [ ] ;
103
+ expect ( fp . pubkeys ) . toEqual ( [ ] ) ;
104
+ expect ( fp . tags . find ( t => t [ 0 ] === "p" ) ) . toBeUndefined ( ) ;
105
+ } ) ;
106
+
107
+ it ( "should get/set description" , ( ) => {
108
+ const fp = new NDKFollowPack ( ) ;
109
+ expect ( fp . description ) . toBeUndefined ( ) ;
110
+
111
+ fp . description = "A follow pack description" ;
112
+ expect ( fp . description ) . toBe ( "A follow pack description" ) ;
113
+ expect ( fp . tags . find ( t => t [ 0 ] === "description" ) ) . toEqual ( [ "description" , "A follow pack description" ] ) ;
114
+
115
+ fp . description = undefined ;
116
+ expect ( fp . description ) . toBeUndefined ( ) ;
117
+ expect ( fp . tags . find ( t => t [ 0 ] === "description" ) ) . toBeUndefined ( ) ;
118
+ } ) ;
119
+ } ) ;
120
+
121
+ describe ( "Edge Cases" , ( ) => {
122
+ it ( "should remove tags when setting undefined values" , ( ) => {
123
+ const fp = new NDKFollowPack ( ) ;
124
+ fp . title = "Test" ;
125
+ expect ( fp . tags . find ( t => t [ 0 ] === "title" ) ) . toBeDefined ( ) ;
126
+
127
+ fp . title = undefined ;
128
+ expect ( fp . tags . find ( t => t [ 0 ] === "title" ) ) . toBeUndefined ( ) ;
129
+ } ) ;
130
+
131
+ it ( "should remove all p tags when setting pubkeys to empty array" , ( ) => {
132
+ const fp = new NDKFollowPack ( ) ;
133
+ fp . pubkeys = [ "pk1" , "pk2" ] ;
134
+ expect ( fp . tags . filter ( t => t [ 0 ] === "p" ) . length ) . toBe ( 2 ) ;
135
+
136
+ fp . pubkeys = [ ] ;
137
+ expect ( fp . tags . find ( t => t [ 0 ] === "p" ) ) . toBeUndefined ( ) ;
138
+ } ) ;
139
+
140
+ it ( "should handle malformed or missing tags gracefully" , ( ) => {
141
+ const fp = new NDKFollowPack ( ) ;
142
+ // Manually add malformed tags
143
+ fp . tags . push ( [ "p" ] ) ;
144
+ fp . tags . push ( [ "image" ] ) ;
145
+ fp . tags . push ( [ "imeta" ] ) ;
146
+ fp . tags . push ( [ "title" ] ) ;
147
+ fp . tags . push ( [ "description" ] ) ;
148
+
149
+ // Getters should not throw and should return undefined or empty
150
+ expect ( fp . pubkeys ) . toEqual ( [ ] ) ;
151
+ expect ( fp . image ) . toBeUndefined ( ) ;
152
+ expect ( fp . title ) . toBeUndefined ( ) ;
153
+ expect ( fp . description ) . toBeUndefined ( ) ;
154
+ } ) ;
155
+ } ) ;
156
+ } ) ;
0 commit comments