@@ -169,6 +169,59 @@ var _ = Describe("PlayQueueRepository", func() {
169
169
Expect (actual .Position ).To (Equal (int64 (200 )))
170
170
Expect (actual .Items ).To (HaveLen (2 )) // Should remain unchanged
171
171
})
172
+
173
+ It ("ensures only one record per user by reusing existing record ID" , func () {
174
+ By ("Storing initial playqueue" )
175
+ initial := aPlayQueue ("userid" , 0 , 100 , songComeTogether )
176
+ Expect (repo .Store (initial )).To (Succeed ())
177
+ initialCount := countPlayQueues (repo , "userid" )
178
+ Expect (initialCount ).To (Equal (1 ))
179
+
180
+ By ("Storing another playqueue with different ID but same user" )
181
+ different := aPlayQueue ("userid" , 1 , 200 , songDayInALife )
182
+ different .ID = "different-id" // Force a different ID
183
+ Expect (repo .Store (different )).To (Succeed ())
184
+
185
+ By ("Verifying only one record exists for the user" )
186
+ finalCount := countPlayQueues (repo , "userid" )
187
+ Expect (finalCount ).To (Equal (1 ))
188
+
189
+ By ("Verifying the record was updated, not duplicated" )
190
+ actual , err := repo .Retrieve ("userid" )
191
+ Expect (err ).ToNot (HaveOccurred ())
192
+ Expect (actual .Current ).To (Equal (1 )) // Should be updated value
193
+ Expect (actual .Position ).To (Equal (int64 (200 ))) // Should be updated value
194
+ Expect (actual .Items ).To (HaveLen (1 )) // Should be new items
195
+ Expect (actual .Items [0 ].ID ).To (Equal (songDayInALife .ID ))
196
+ })
197
+
198
+ It ("ensures only one record per user even with partial updates" , func () {
199
+ By ("Storing initial playqueue" )
200
+ initial := aPlayQueue ("userid" , 0 , 100 , songComeTogether , songDayInALife )
201
+ Expect (repo .Store (initial )).To (Succeed ())
202
+ initialCount := countPlayQueues (repo , "userid" )
203
+ Expect (initialCount ).To (Equal (1 ))
204
+
205
+ By ("Storing partial update with different ID but same user" )
206
+ partialUpdate := & model.PlayQueue {
207
+ ID : "completely-different-id" , // Use a completely different ID
208
+ UserID : "userid" ,
209
+ Current : 1 ,
210
+ ChangedBy : "test-partial" ,
211
+ }
212
+ Expect (repo .Store (partialUpdate , "current" )).To (Succeed ())
213
+
214
+ By ("Verifying only one record still exists for the user" )
215
+ finalCount := countPlayQueues (repo , "userid" )
216
+ Expect (finalCount ).To (Equal (1 ))
217
+
218
+ By ("Verifying the existing record was updated with new current value" )
219
+ actual , err := repo .Retrieve ("userid" )
220
+ Expect (err ).ToNot (HaveOccurred ())
221
+ Expect (actual .Current ).To (Equal (1 )) // Should be updated value
222
+ Expect (actual .Position ).To (Equal (int64 (100 ))) // Should remain unchanged
223
+ Expect (actual .Items ).To (HaveLen (2 )) // Should remain unchanged
224
+ })
172
225
})
173
226
174
227
Describe ("Retrieve" , func () {
0 commit comments