FeedType.test.ts raw
1 import { describe, it, expect } from 'vitest'
2 import { FeedType } from './FeedType'
3
4 describe('FeedType', () => {
5 describe('factory methods', () => {
6 it('creates a following feed type', () => {
7 const feedType = FeedType.following()
8
9 expect(feedType.value).toBe('following')
10 expect(feedType.relaySetId).toBeNull()
11 expect(feedType.relayUrl).toBeNull()
12 expect(feedType.isSocialFeed).toBe(true)
13 expect(feedType.isRelayFeed).toBe(false)
14 })
15
16 it('creates a pinned feed type', () => {
17 const feedType = FeedType.pinned()
18
19 expect(feedType.value).toBe('pinned')
20 expect(feedType.isSocialFeed).toBe(true)
21 expect(feedType.isRelayFeed).toBe(false)
22 })
23
24 it('creates a relay set feed type', () => {
25 const feedType = FeedType.relays('my-set-id')
26
27 expect(feedType.value).toBe('relays')
28 expect(feedType.relaySetId).toBe('my-set-id')
29 expect(feedType.relayUrl).toBeNull()
30 expect(feedType.isSocialFeed).toBe(false)
31 expect(feedType.isRelayFeed).toBe(true)
32 })
33
34 it('creates a single relay feed type', () => {
35 const feedType = FeedType.relay('wss://relay.example.com')
36
37 expect(feedType.value).toBe('relay')
38 expect(feedType.relaySetId).toBeNull()
39 expect(feedType.relayUrl).toBe('wss://relay.example.com')
40 expect(feedType.isSocialFeed).toBe(false)
41 expect(feedType.isRelayFeed).toBe(true)
42 })
43
44 it('throws for empty relay set ID', () => {
45 expect(() => FeedType.relays('')).toThrow('Relay set ID cannot be empty')
46 expect(() => FeedType.relays(' ')).toThrow('Relay set ID cannot be empty')
47 })
48
49 it('throws for empty relay URL', () => {
50 expect(() => FeedType.relay('')).toThrow('Relay URL cannot be empty')
51 expect(() => FeedType.relay(' ')).toThrow('Relay URL cannot be empty')
52 })
53 })
54
55 describe('tryFromString', () => {
56 it('parses following', () => {
57 const feedType = FeedType.tryFromString('following')
58
59 expect(feedType).not.toBeNull()
60 expect(feedType!.value).toBe('following')
61 })
62
63 it('parses pinned', () => {
64 const feedType = FeedType.tryFromString('pinned')
65
66 expect(feedType).not.toBeNull()
67 expect(feedType!.value).toBe('pinned')
68 })
69
70 it('parses relays with ID', () => {
71 const feedType = FeedType.tryFromString('relays', 'set-id')
72
73 expect(feedType).not.toBeNull()
74 expect(feedType!.value).toBe('relays')
75 expect(feedType!.relaySetId).toBe('set-id')
76 })
77
78 it('returns null for relays without ID', () => {
79 const feedType = FeedType.tryFromString('relays')
80
81 expect(feedType).toBeNull()
82 })
83
84 it('parses relay with URL', () => {
85 const feedType = FeedType.tryFromString('relay', 'wss://relay.example.com')
86
87 expect(feedType).not.toBeNull()
88 expect(feedType!.value).toBe('relay')
89 expect(feedType!.relayUrl).toBe('wss://relay.example.com')
90 })
91
92 it('returns null for relay without URL', () => {
93 const feedType = FeedType.tryFromString('relay')
94
95 expect(feedType).toBeNull()
96 })
97
98 it('returns null for unknown type', () => {
99 const feedType = FeedType.tryFromString('unknown')
100
101 expect(feedType).toBeNull()
102 })
103 })
104
105 describe('equals', () => {
106 it('returns true for identical following types', () => {
107 const type1 = FeedType.following()
108 const type2 = FeedType.following()
109
110 expect(type1.equals(type2)).toBe(true)
111 })
112
113 it('returns false for different types', () => {
114 const type1 = FeedType.following()
115 const type2 = FeedType.pinned()
116
117 expect(type1.equals(type2)).toBe(false)
118 })
119
120 it('returns true for same relay set ID', () => {
121 const type1 = FeedType.relays('same-id')
122 const type2 = FeedType.relays('same-id')
123
124 expect(type1.equals(type2)).toBe(true)
125 })
126
127 it('returns false for different relay set IDs', () => {
128 const type1 = FeedType.relays('id-1')
129 const type2 = FeedType.relays('id-2')
130
131 expect(type1.equals(type2)).toBe(false)
132 })
133
134 it('returns true for same relay URL', () => {
135 const type1 = FeedType.relay('wss://same.relay.com')
136 const type2 = FeedType.relay('wss://same.relay.com')
137
138 expect(type1.equals(type2)).toBe(true)
139 })
140
141 it('returns false for different relay URLs', () => {
142 const type1 = FeedType.relay('wss://relay1.com')
143 const type2 = FeedType.relay('wss://relay2.com')
144
145 expect(type1.equals(type2)).toBe(false)
146 })
147 })
148
149 describe('toString', () => {
150 it('returns simple string for following', () => {
151 expect(FeedType.following().toString()).toBe('following')
152 })
153
154 it('returns simple string for pinned', () => {
155 expect(FeedType.pinned().toString()).toBe('pinned')
156 })
157
158 it('returns relays:id format for relay sets', () => {
159 expect(FeedType.relays('my-set').toString()).toBe('relays:my-set')
160 })
161
162 it('returns relay:url format for single relay', () => {
163 expect(FeedType.relay('wss://relay.com').toString()).toBe('relay:wss://relay.com')
164 })
165 })
166 })
167