Subscribe and post RSS feeds to Bluesky
rss
bluesky
1package storage
2
3import (
4 "os"
5 "path/filepath"
6 "testing"
7)
8
9func TestNew(t *testing.T) {
10 tempDir := t.TempDir()
11 storagePath := filepath.Join(tempDir, "test_storage.txt")
12
13 store, err := New(storagePath)
14 if err != nil {
15 t.Fatalf("Failed to create storage: %v", err)
16 }
17
18 if store == nil {
19 t.Fatal("Expected non-nil storage")
20 }
21
22 if store.Count() != 0 {
23 t.Errorf("Expected empty storage, got count %d", store.Count())
24 }
25}
26
27func TestMarkPosted(t *testing.T) {
28 tempDir := t.TempDir()
29 storagePath := filepath.Join(tempDir, "test_storage.txt")
30
31 store, err := New(storagePath)
32 if err != nil {
33 t.Fatalf("Failed to create storage: %v", err)
34 }
35
36 // Mark an item as posted
37 guid := "test-guid-123"
38 err = store.MarkPosted(guid)
39 if err != nil {
40 t.Fatalf("Failed to mark item as posted: %v", err)
41 }
42
43 // Verify it's marked as posted
44 if !store.IsPosted(guid) {
45 t.Error("Expected item to be marked as posted")
46 }
47
48 // Verify count
49 if store.Count() != 1 {
50 t.Errorf("Expected count 1, got %d", store.Count())
51 }
52}
53
54func TestIsPosted(t *testing.T) {
55 tempDir := t.TempDir()
56 storagePath := filepath.Join(tempDir, "test_storage.txt")
57
58 store, err := New(storagePath)
59 if err != nil {
60 t.Fatalf("Failed to create storage: %v", err)
61 }
62
63 guid := "test-guid-456"
64
65 // Should not be posted initially
66 if store.IsPosted(guid) {
67 t.Error("Expected item to not be posted initially")
68 }
69
70 // Mark as posted
71 store.MarkPosted(guid)
72
73 // Should be posted now
74 if !store.IsPosted(guid) {
75 t.Error("Expected item to be posted after marking")
76 }
77}
78
79func TestPersistence(t *testing.T) {
80 tempDir := t.TempDir()
81 storagePath := filepath.Join(tempDir, "test_storage.txt")
82
83 // Create first storage instance and add items
84 store1, err := New(storagePath)
85 if err != nil {
86 t.Fatalf("Failed to create first storage: %v", err)
87 }
88
89 guids := []string{"guid-1", "guid-2", "guid-3"}
90 for _, guid := range guids {
91 if err := store1.MarkPosted(guid); err != nil {
92 t.Fatalf("Failed to mark item as posted: %v", err)
93 }
94 }
95
96 // Create second storage instance (simulating restart)
97 store2, err := New(storagePath)
98 if err != nil {
99 t.Fatalf("Failed to create second storage: %v", err)
100 }
101
102 // Verify all items are still marked as posted
103 for _, guid := range guids {
104 if !store2.IsPosted(guid) {
105 t.Errorf("Expected guid '%s' to be persisted", guid)
106 }
107 }
108
109 // Verify count
110 if store2.Count() != len(guids) {
111 t.Errorf("Expected count %d, got %d", len(guids), store2.Count())
112 }
113}
114
115func TestMarkPosted_Duplicate(t *testing.T) {
116 tempDir := t.TempDir()
117 storagePath := filepath.Join(tempDir, "test_storage.txt")
118
119 store, err := New(storagePath)
120 if err != nil {
121 t.Fatalf("Failed to create storage: %v", err)
122 }
123
124 guid := "duplicate-guid"
125
126 // Mark twice
127 store.MarkPosted(guid)
128 store.MarkPosted(guid)
129
130 // Should still only count as one
131 if store.Count() != 1 {
132 t.Errorf("Expected count 1 after duplicate mark, got %d", store.Count())
133 }
134
135 // Should still be posted
136 if !store.IsPosted(guid) {
137 t.Error("Expected item to still be posted")
138 }
139}
140
141func TestLoad_EmptyFile(t *testing.T) {
142 tempDir := t.TempDir()
143 storagePath := filepath.Join(tempDir, "empty.txt")
144
145 // Create empty file
146 file, err := os.Create(storagePath)
147 if err != nil {
148 t.Fatalf("Failed to create empty file: %v", err)
149 }
150 file.Close()
151
152 // Load storage
153 store, err := New(storagePath)
154 if err != nil {
155 t.Fatalf("Failed to load storage from empty file: %v", err)
156 }
157
158 if store.Count() != 0 {
159 t.Errorf("Expected count 0 from empty file, got %d", store.Count())
160 }
161}
162
163func TestLoad_FileWithEmptyLines(t *testing.T) {
164 tempDir := t.TempDir()
165 storagePath := filepath.Join(tempDir, "test_with_empty_lines.txt")
166
167 // Create file with some empty lines
168 content := "guid-1\n\nguid-2\n\n\nguid-3\n"
169 if err := os.WriteFile(storagePath, []byte(content), 0644); err != nil {
170 t.Fatalf("Failed to write test file: %v", err)
171 }
172
173 // Load storage
174 store, err := New(storagePath)
175 if err != nil {
176 t.Fatalf("Failed to load storage: %v", err)
177 }
178
179 // Should only have 3 items (empty lines ignored)
180 if store.Count() != 3 {
181 t.Errorf("Expected count 3, got %d", store.Count())
182 }
183
184 // Verify specific GUIDs
185 expectedGuids := []string{"guid-1", "guid-2", "guid-3"}
186 for _, guid := range expectedGuids {
187 if !store.IsPosted(guid) {
188 t.Errorf("Expected guid '%s' to be loaded", guid)
189 }
190 }
191}
192
193func TestCount(t *testing.T) {
194 tempDir := t.TempDir()
195 storagePath := filepath.Join(tempDir, "test_storage.txt")
196
197 store, err := New(storagePath)
198 if err != nil {
199 t.Fatalf("Failed to create storage: %v", err)
200 }
201
202 // Initial count should be 0
203 if store.Count() != 0 {
204 t.Errorf("Expected initial count 0, got %d", store.Count())
205 }
206
207 // Add items and check count
208 for i := 0; i < 5; i++ {
209 store.MarkPosted(string(rune('a' + i)))
210 expectedCount := i + 1
211 if store.Count() != expectedCount {
212 t.Errorf("After adding %d items, expected count %d, got %d", expectedCount, expectedCount, store.Count())
213 }
214 }
215}
216
217func TestConcurrentAccess(t *testing.T) {
218 tempDir := t.TempDir()
219 storagePath := filepath.Join(tempDir, "concurrent_test.txt")
220
221 store, err := New(storagePath)
222 if err != nil {
223 t.Fatalf("Failed to create storage: %v", err)
224 }
225
226 // Simulate concurrent access
227 done := make(chan bool)
228 for i := 0; i < 10; i++ {
229 go func(id int) {
230 guid := string(rune('a' + id))
231 store.MarkPosted(guid)
232 _ = store.IsPosted(guid)
233 _ = store.Count()
234 done <- true
235 }(i)
236 }
237
238 // Wait for all goroutines
239 for i := 0; i < 10; i++ {
240 <-done
241 }
242
243 // All items should be present
244 if store.Count() != 10 {
245 t.Errorf("Expected count 10 after concurrent access, got %d", store.Count())
246 }
247}