@@ -159,7 +159,8 @@ func TestGetInfoReturnsQuotaAndListeners(t *testing.T) {
159159 assert .Empty (t , info .ChatListeners )
160160 assert .Empty (t , info .ActiveBroadcasts )
161161
162- // Simulate some quota usage.
162+ // Simulate some quota usage via per-op tracking.
163+ yt .YouTubeClient .UsedPointsByOp .Store ("GetBroadcasts" , 42 )
163164 yt .YouTubeClient .UsedPoints .Store (42 )
164165 info = yt .GetInfo (ctx )
165166 assert .Equal (t , uint64 (42 ), info .QuotaUsage .UsedPoints .Load ())
@@ -215,18 +216,20 @@ func TestTemplateBroadcastIDSet(t *testing.T) {
215216
216217func TestQuotaPersistenceLoad (t * testing.T ) {
217218 cfg := newTestAccountConfig ()
218- cfg .QuotaUsedPoints = 500
219+ cfg .QuotaUsedByOp = map [string ]uint64 {
220+ "GetBroadcasts" : 200 ,
221+ "Search" : 300 ,
222+ }
219223 cfg .QuotaUsedDate = getQuotaCutoffDate (time .Now ())
220224
221225 yt := newTestYouTubeWithConfig (t , cfg , func (AccountConfig ) error { return nil })
222226
223- // 500 loaded + 1 from Ping during New()
227+ // 500 loaded from per-op map + 1 from Ping during New()
224228 assert .Equal (t , uint64 (501 ), yt .YouTubeClient .UsedPoints .Load ())
225229}
226230
227231func TestQuotaPersistenceLoadPerOp (t * testing.T ) {
228232 cfg := newTestAccountConfig ()
229- cfg .QuotaUsedPoints = 200
230233 cfg .QuotaUsedDate = getQuotaCutoffDate (time .Now ())
231234 cfg .QuotaUsedByOp = map [string ]uint64 {
232235 "GetBroadcasts" : 3 ,
@@ -248,11 +251,17 @@ func TestQuotaPersistenceLoadPerOp(t *testing.T) {
248251 updVid , ok := yt .YouTubeClient .UsedPointsByOp .Load ("UpdateVideo" )
249252 assert .True (t , ok )
250253 assert .Equal (t , uint64 (97 ), updVid )
254+
255+ // Total should be derived from per-op sum + 1 from Ping during New()
256+ assert .Equal (t , uint64 (201 ), yt .YouTubeClient .UsedPoints .Load ())
251257}
252258
253259func TestQuotaPersistenceLoadStaleDate (t * testing.T ) {
254260 cfg := newTestAccountConfig ()
255- cfg .QuotaUsedPoints = 500
261+ cfg .QuotaUsedByOp = map [string ]uint64 {
262+ "GetBroadcasts" : 200 ,
263+ "Search" : 300 ,
264+ }
256265 cfg .QuotaUsedDate = "2020-01-01"
257266
258267 yt := newTestYouTubeWithConfig (t , cfg , func (AccountConfig ) error { return nil })
@@ -276,32 +285,22 @@ func TestPersistQuotaSavesOnChange(t *testing.T) {
276285 yt := newTestYouTubeWithConfig (t , cfg , saveFn )
277286 ctx := context .Background ()
278287
279- // Simulate some quota usage.
280- yt .YouTubeClient .UsedPoints .Store (42 )
281-
282- // First persist should save.
288+ // After New(), Ping added 1 point. First explicit persist should save.
283289 saveCount = 0
284290 yt .persistQuota (ctx )
285291 assert .Equal (t , 1 , saveCount )
286- assert .Equal (t , uint64 ( 42 ) , savedCfg .QuotaUsedPoints )
292+ assert .Equal (t , map [ string ] uint64 { "Ping" : 1 } , savedCfg .QuotaUsedByOp )
287293 assert .Equal (t , getQuotaCutoffDate (time .Now ()), savedCfg .QuotaUsedDate )
288294
289295 // Second persist with same value should not save again.
290296 yt .persistQuota (ctx )
291297 assert .Equal (t , 1 , saveCount , "persistQuota should skip save when values unchanged" )
292298
293- // Change the value; persist should save again.
294- yt .YouTubeClient .UsedPoints .Store (100 )
295- yt .persistQuota (ctx )
296- assert .Equal (t , 2 , saveCount )
297- assert .Equal (t , uint64 (100 ), savedCfg .QuotaUsedPoints )
298-
299- // Verify per-op breakdown is persisted.
299+ // Add more per-op usage; persist should save again.
300300 yt .YouTubeClient .UsedPointsByOp .Store ("GetBroadcasts" , 5 )
301301 yt .YouTubeClient .UsedPointsByOp .Store ("Search" , 95 )
302- yt .YouTubeClient .UsedPoints .Store (200 )
303302 yt .persistQuota (ctx )
304- assert .Equal (t , 3 , saveCount )
303+ assert .Equal (t , 2 , saveCount )
305304 assert .Equal (t , map [string ]uint64 {
306305 "GetBroadcasts" : 5 ,
307306 "Ping" : 1 ,
0 commit comments