forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoday.test.ts
More file actions
795 lines (697 loc) · 30.3 KB
/
Copy pathtoday.test.ts
File metadata and controls
795 lines (697 loc) · 30.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
// src/lib/__tests__/today.test.ts
//
// Tests for computeTodayAtAGlance() in src/lib/today.ts.
// The DB layer is mocked via vi.mock. Because vi.mock factories are hoisted to
// the top of the file before any variable declarations, the mock factories use
// inline string sentinels that match the schema mock values exactly.
import { beforeEach, describe, expect, it, vi } from "vitest"
// ---------------------------------------------------------------------------
// Per-test data store — populated by seedStore() in each test
// ---------------------------------------------------------------------------
// These string values must match the values returned by the schema mock below.
// "T_" prefix is just a local naming convention; the actual values are what matter.
const SENT_TRACKERS = "SENT_trackers"
const SENT_SNAPSHOTS = "SENT_snapshots"
const SENT_TRACKER_CP = "SENT_tracker_cp" // single merged query for both checkpoint dates
const SENT_TORRENT_CP = "SENT_torrent_cp"
const SENT_CLIENTS = "SENT_clients"
// The store is read by the mock's .from() handler
const store: Record<string, unknown[]> = {
[SENT_TRACKERS]: [],
[SENT_SNAPSHOTS]: [],
[SENT_TRACKER_CP]: [],
[SENT_TORRENT_CP]: [],
[SENT_CLIENTS]: [],
}
function seedStore(
trackers: unknown[] = [],
snapshots: unknown[] = [],
yesterdayCps: unknown[] = [],
dayBeforeCps: unknown[] = [],
torrentCps: unknown[] = [],
clients: unknown[] = []
) {
store[SENT_TRACKERS] = trackers
store[SENT_SNAPSHOTS] = snapshots
// Both date ranges are now fetched in one inArray query; combine them here
store[SENT_TRACKER_CP] = [...yesterdayCps, ...dayBeforeCps]
store[SENT_TORRENT_CP] = torrentCps
store[SENT_CLIENTS] = clients
}
// ---------------------------------------------------------------------------
// DB mock — vi.mock factory is hoisted; use ONLY inline literals here
// ---------------------------------------------------------------------------
vi.mock("@/lib/db", () => {
// NOTE: Cannot reference outer variables here — this factory is hoisted.
// The store/storeDayBefore variables are module-level so they ARE accessible
// after hoisting as long as we reference them by closure (not by value at
// declaration time). Vitest hoists vi.mock but the factory closure still
// has access to module-level mutable state.
return {
db: {
select: vi.fn(() => ({
from: vi.fn((table: string) => ({
where: vi.fn((_cond: unknown) => {
// todaySnapshots has .orderBy() after .where()
if (table === "SENT_snapshots") {
return {
orderBy: vi.fn(() => Promise.resolve(store.SENT_snapshots ?? [])),
}
}
// trackerDailyCheckpoints is fetched in a single inArray query
if (table === "SENT_tracker_cp") {
return Promise.resolve(store.SENT_tracker_cp ?? [])
}
if (table === "SENT_torrent_cp") {
return Promise.resolve(store.SENT_torrent_cp ?? [])
}
if (table === "SENT_clients") {
return Promise.resolve(store.SENT_clients ?? [])
}
if (table === "SENT_trackers") {
return Promise.resolve(store.SENT_trackers ?? [])
}
return Promise.resolve([])
}),
})),
})),
},
}
})
// Schema mock — each table is a string sentinel matching the from() dispatch above
vi.mock("@/lib/db/schema", () => ({
trackers: "SENT_trackers",
trackerSnapshots: "SENT_snapshots",
trackerDailyCheckpoints: "SENT_tracker_cp",
torrentDailyCheckpoints: "SENT_torrent_cp",
downloadClients: "SENT_clients",
}))
vi.mock("drizzle-orm", () => ({
eq: vi.fn((_col: unknown, _val: unknown) => ({ type: "eq" })),
gte: vi.fn((_col: unknown, _val: unknown) => ({ type: "gte" })),
inArray: vi.fn((_col: unknown, _vals: unknown) => ({ type: "inArray" })),
sql: vi.fn(() => ({ type: "sql" })),
}))
// ---------------------------------------------------------------------------
// Import under test (after mocks)
// ---------------------------------------------------------------------------
import { localDateStr } from "@/lib/formatters"
import { computeTodayAtAGlance } from "@/lib/today"
// ---------------------------------------------------------------------------
// Factories
// ---------------------------------------------------------------------------
function makeTracker(
id: number,
overrides: { qbtTag?: string | null; color?: string | null } = {}
) {
return {
id,
name: `Tracker ${id}`,
color: overrides.color ?? null,
qbtTag: overrides.qbtTag ?? null,
}
}
function makeSnapshot(
trackerId: number,
polledAt: Date,
uploadedBytes: string,
downloadedBytes: string,
bufferBytes: string | null = null,
ratio: number | null = null,
seedbonus: number | null = null
) {
return {
id: Math.random(),
trackerId,
polledAt,
uploadedBytes,
downloadedBytes,
bufferBytes,
ratio,
seedbonus,
seedingCount: null,
leechingCount: null,
hitAndRuns: null,
warned: null,
freeleechTokens: null,
shareScore: null,
username: null,
group: null,
}
}
function makeTrackerCheckpoint(
trackerId: number,
checkpointDate: string,
uploadedBytesEnd: string,
downloadedBytesEnd: string,
bufferBytesEnd: string | null = null
) {
return {
id: Math.random(),
trackerId,
checkpointDate,
uploadedBytesEnd,
downloadedBytesEnd,
bufferBytesEnd,
ratioEnd: null,
seedbonusEnd: null,
snapshotCount: 1,
}
}
function makeTorrent(
hash: string,
name: string,
tags: string,
uploaded: number,
downloaded: number,
addedAt = 0,
completedAt = -1
) {
return {
hash,
name,
state: "uploading",
tags,
category: "",
uploadSpeed: 0,
downloadSpeed: 0,
uploaded,
downloaded,
ratio: 1.0,
size: downloaded || 1,
seedCount: 5,
leechCount: 0,
swarmSeeders: 5,
swarmLeechers: 0,
addedAt,
completedAt,
lastActivityAt: 0,
seedingTime: 0,
activeTime: 0,
availability: 1,
remaining: 0,
progress: 1,
}
}
function makeTorrentCheckpoint(
clientId: number,
hash: string,
uploadedStart: string,
downloadedStart: string
) {
return {
id: Math.random(),
clientId,
hash,
checkpointDate: localDateStr(),
uploadedStart,
downloadedStart,
}
}
// ---------------------------------------------------------------------------
// beforeEach resets store to empty state
// ---------------------------------------------------------------------------
beforeEach(() => {
seedStore()
})
// ---------------------------------------------------------------------------
// Empty state
// ---------------------------------------------------------------------------
describe("computeTodayAtAGlance — empty state", () => {
it("returns zero fleet upload delta when there are no trackers", async () => {
const result = await computeTodayAtAGlance()
expect(result.fleet.uploadDelta).toBe("0")
})
it("returns zero fleet download delta when there are no trackers", async () => {
const result = await computeTodayAtAGlance()
expect(result.fleet.downloadDelta).toBe("0")
})
it("returns zero fleet buffer delta when there are no trackers", async () => {
const result = await computeTodayAtAGlance()
expect(result.fleet.bufferDelta).toBe("0")
})
it("returns null ratioChange when there is no upload activity", async () => {
const result = await computeTodayAtAGlance()
expect(result.fleet.ratioChange).toBeNull()
})
it("returns null yesterday delta values when no checkpoints exist", async () => {
const result = await computeTodayAtAGlance()
expect(result.fleet.uploadDeltaYesterday).toBeNull()
expect(result.fleet.downloadDeltaYesterday).toBeNull()
expect(result.fleet.bufferDeltaYesterday).toBeNull()
})
it("returns empty trackers array", async () => {
const result = await computeTodayAtAGlance()
expect(result.trackers).toEqual([])
})
it("returns zero activity counts", async () => {
const result = await computeTodayAtAGlance()
expect(result.activity.addedToday).toBe(0)
expect(result.activity.completedToday).toBe(0)
})
it("returns empty movers arrays", async () => {
const result = await computeTodayAtAGlance()
expect(result.movers.topUploaders).toHaveLength(0)
expect(result.movers.topDownloaders).toHaveLength(0)
})
})
// ---------------------------------------------------------------------------
// Single tracker, two snapshots today
// ---------------------------------------------------------------------------
describe("computeTodayAtAGlance — single tracker two snapshots today", () => {
const GiB = 1024 * 1024 * 1024
beforeEach(() => {
const now = new Date()
const tracker = makeTracker(1)
const earliest = makeSnapshot(
1,
new Date(now.getTime() - 3600000),
String(10n * BigInt(GiB)),
String(5n * BigInt(GiB))
)
const latest = makeSnapshot(
1,
now,
String(11n * BigInt(GiB)),
String(5n * BigInt(GiB) + 512n * 1024n * 1024n)
)
seedStore([tracker], [earliest, latest])
})
it("computes upload delta as last minus first snapshot value", async () => {
const result = await computeTodayAtAGlance()
expect(BigInt(result.fleet.uploadDelta)).toBe(BigInt(GiB))
})
it("computes download delta correctly", async () => {
const result = await computeTodayAtAGlance()
expect(BigInt(result.fleet.downloadDelta)).toBe(512n * 1024n * 1024n)
})
it("exposes the tracker entry with the correct upload delta", async () => {
const result = await computeTodayAtAGlance()
expect(result.trackers).toHaveLength(1)
expect(result.trackers[0].id).toBe(1)
expect(BigInt(result.trackers[0].uploadDelta)).toBe(BigInt(GiB))
})
})
// ---------------------------------------------------------------------------
// Fewer than 2 snapshots
// ---------------------------------------------------------------------------
describe("computeTodayAtAGlance — tracker with only one snapshot today", () => {
beforeEach(() => {
const tracker = makeTracker(1)
const snap = makeSnapshot(1, new Date(), "1000000000", "500000000")
seedStore([tracker], [snap])
})
it("returns zero deltas when tracker has fewer than 2 snapshots today", async () => {
const result = await computeTodayAtAGlance()
expect(result.trackers[0].uploadDelta).toBe("0")
expect(result.trackers[0].downloadDelta).toBe("0")
})
})
// ---------------------------------------------------------------------------
// Null bufferBytes handling
// ---------------------------------------------------------------------------
describe("computeTodayAtAGlance — null bufferBytes on both snapshots", () => {
beforeEach(() => {
const tracker = makeTracker(1)
const earliest = makeSnapshot(
1,
new Date(Date.now() - 3600000),
"1000000000",
"500000000",
null
)
const latest = makeSnapshot(1, new Date(), "2000000000", "600000000", null)
seedStore([tracker], [earliest, latest])
})
it("does not crash when bufferBytes is null and returns 0 buffer delta", async () => {
const result = await computeTodayAtAGlance()
expect(result.fleet.bufferDelta).toBe("0")
expect(result.trackers[0].bufferDelta).toBe("0")
})
})
describe("computeTodayAtAGlance — null bufferBytes on earliest, value on latest", () => {
beforeEach(() => {
const tracker = makeTracker(1)
const earliest = makeSnapshot(
1,
new Date(Date.now() - 3600000),
"1000000000",
"500000000",
null
)
const latest = makeSnapshot(1, new Date(), "2000000000", "600000000", "1073741824")
seedStore([tracker], [earliest, latest])
})
it("treats null bufferBytes as 0n so delta equals the latest buffer value", async () => {
const result = await computeTodayAtAGlance()
expect(BigInt(result.trackers[0].bufferDelta)).toBe(1073741824n)
})
})
// ---------------------------------------------------------------------------
// Yesterday comparison via checkpoint pairs
// ---------------------------------------------------------------------------
describe("computeTodayAtAGlance — yesterday comparison", () => {
const yesterdayStr = localDateStr(Date.now() - 86400000)
const dayBeforeStr = localDateStr(Date.now() - 172800000)
it("computes fleet upload and download deltas for yesterday", async () => {
const tracker = makeTracker(1)
const snap1 = makeSnapshot(1, new Date(Date.now() - 3600000), "2000000000", "1000000000")
const snap2 = makeSnapshot(1, new Date(), "3000000000", "1500000000")
// Yesterday ended at 2G upload, day-before ended at 1G → delta = 1G
const yestCp = makeTrackerCheckpoint(1, yesterdayStr, "2000000000", "1000000000")
const dayBeforeCp = makeTrackerCheckpoint(1, dayBeforeStr, "1000000000", "500000000")
seedStore([tracker], [snap1, snap2], [yestCp], [dayBeforeCp])
const result = await computeTodayAtAGlance()
expect(result.fleet.uploadDeltaYesterday).toBe("1000000000")
expect(result.fleet.downloadDeltaYesterday).toBe("500000000")
})
it("returns null when yesterday checkpoint exists but no day-before checkpoint", async () => {
const tracker = makeTracker(1)
const snap = makeSnapshot(1, new Date(), "2000000000", "1000000000")
const yestCp = makeTrackerCheckpoint(1, yesterdayStr, "2000000000", "1000000000")
seedStore([tracker], [snap], [yestCp], [])
const result = await computeTodayAtAGlance()
expect(result.fleet.uploadDeltaYesterday).toBeNull()
expect(result.fleet.downloadDeltaYesterday).toBeNull()
})
it("returns null when checkpoints exist for a different tracker id", async () => {
const tracker = makeTracker(1)
const snap = makeSnapshot(1, new Date(), "2000000000", "1000000000")
// trackerId 99 does not match tracker 1
const yestCp = makeTrackerCheckpoint(99, yesterdayStr, "2000000000", "1000000000")
const dayBeforeCp = makeTrackerCheckpoint(99, dayBeforeStr, "1000000000", "500000000")
seedStore([tracker], [snap], [yestCp], [dayBeforeCp])
const result = await computeTodayAtAGlance()
expect(result.fleet.uploadDeltaYesterday).toBeNull()
})
it("treats null bufferBytesEnd as 0n in yesterday buffer delta", async () => {
const tracker = makeTracker(1)
const snap1 = makeSnapshot(1, new Date(Date.now() - 3600000), "1000000000", "500000000")
const snap2 = makeSnapshot(1, new Date(), "2000000000", "1000000000")
const yestCp = makeTrackerCheckpoint(1, yesterdayStr, "2000000000", "1000000000", null)
const dayBeforeCp = makeTrackerCheckpoint(1, dayBeforeStr, "1000000000", "500000000", null)
seedStore([tracker], [snap1, snap2], [yestCp], [dayBeforeCp])
const result = await computeTodayAtAGlance()
expect(result.fleet.bufferDeltaYesterday).toBe("0")
})
})
// ---------------------------------------------------------------------------
// Torrent movers tag filtering
// ---------------------------------------------------------------------------
describe("computeTodayAtAGlance — torrent movers tag filtering", () => {
it("excludes torrents whose tags do not match any tracker qbtTag", async () => {
const tracker = makeTracker(1, { qbtTag: "aither" })
const snap1 = makeSnapshot(1, new Date(Date.now() - 3600000), "1000000000", "500000000")
const snap2 = makeSnapshot(1, new Date(), "2000000000", "1000000000")
const torrent = makeTorrent("hash1", "Some Movie", "other-tracker", 5000000000, 3000000000)
const cp = makeTorrentCheckpoint(1, "hash1", "0", "0")
const client = { id: 1, name: "qBit", cachedTorrents: JSON.stringify([torrent]) }
seedStore([tracker], [snap1, snap2], [], [], [cp], [client])
const result = await computeTodayAtAGlance()
expect(result.movers.topUploaders).toHaveLength(0)
expect(result.movers.topDownloaders).toHaveLength(0)
})
it("includes torrents whose tags match a tracker's qbtTag", async () => {
const tracker = makeTracker(1, { qbtTag: "aither" })
const snap1 = makeSnapshot(1, new Date(Date.now() - 3600000), "1000000000", "500000000")
const snap2 = makeSnapshot(1, new Date(), "2000000000", "1000000000")
const torrent = makeTorrent("hash1", "Some Movie", "aither", 5000000000, 3000000000)
const cp = makeTorrentCheckpoint(1, "hash1", "0", "0")
const client = { id: 1, name: "qBit", cachedTorrents: JSON.stringify([torrent]) }
seedStore([tracker], [snap1, snap2], [], [], [cp], [client])
const result = await computeTodayAtAGlance()
expect(result.movers.topUploaders).toHaveLength(1)
expect(result.movers.topUploaders[0].hash).toBe("hash1")
expect(result.movers.topUploaders[0].qbtTag).toBe("aither")
})
it("excludes torrents with zero upload delta from topUploaders", async () => {
const tracker = makeTracker(1, { qbtTag: "aither" })
const snap1 = makeSnapshot(1, new Date(Date.now() - 3600000), "1000000000", "500000000")
const snap2 = makeSnapshot(1, new Date(), "2000000000", "1000000000")
// uploaded matches checkpoint start → upload delta = 0
const torrent = makeTorrent("hash1", "Stalled Movie", "aither", 1000000000, 3000000000)
const cp = makeTorrentCheckpoint(1, "hash1", "1000000000", "0")
const client = { id: 1, name: "qBit", cachedTorrents: JSON.stringify([torrent]) }
seedStore([tracker], [snap1, snap2], [], [], [cp], [client])
const result = await computeTodayAtAGlance()
expect(result.movers.topUploaders).toHaveLength(0)
})
it("excludes torrents with zero download delta from topDownloaders", async () => {
const tracker = makeTracker(1, { qbtTag: "aither" })
const snap1 = makeSnapshot(1, new Date(Date.now() - 3600000), "1000000000", "500000000")
const snap2 = makeSnapshot(1, new Date(), "2000000000", "1000000000")
// downloaded matches checkpoint start → download delta = 0
const torrent = makeTorrent("hash1", "Completed Movie", "aither", 5000000000, 3000000000)
const cp = makeTorrentCheckpoint(1, "hash1", "0", "3000000000")
const client = { id: 1, name: "qBit", cachedTorrents: JSON.stringify([torrent]) }
seedStore([tracker], [snap1, snap2], [], [], [cp], [client])
const result = await computeTodayAtAGlance()
expect(result.movers.topDownloaders).toHaveLength(0)
})
it("caps topUploaders and topDownloaders at 5 entries each", async () => {
const tracker = makeTracker(1, { qbtTag: "aither" })
const snap1 = makeSnapshot(1, new Date(Date.now() - 3600000), "1000000000", "500000000")
const snap2 = makeSnapshot(1, new Date(), "2000000000", "1000000000")
const torrents = Array.from({ length: 8 }, (_, i) =>
makeTorrent(`hash${i}`, `Movie ${i}`, "aither", (i + 1) * 1000000000, (i + 1) * 500000000)
)
const cps = torrents.map((t) => makeTorrentCheckpoint(1, t.hash, "0", "0"))
const client = { id: 1, name: "qBit", cachedTorrents: JSON.stringify(torrents) }
seedStore([tracker], [snap1, snap2], [], [], cps, [client])
const result = await computeTodayAtAGlance()
expect(result.movers.topUploaders.length).toBeLessThanOrEqual(5)
expect(result.movers.topDownloaders.length).toBeLessThanOrEqual(5)
})
it("excludes torrents that have no matching torrent checkpoint entry", async () => {
const tracker = makeTracker(1, { qbtTag: "aither" })
const snap1 = makeSnapshot(1, new Date(Date.now() - 3600000), "1000000000", "500000000")
const snap2 = makeSnapshot(1, new Date(), "2000000000", "1000000000")
const torrent = makeTorrent(
"hash-no-cp",
"No Checkpoint Movie",
"aither",
5000000000,
3000000000
)
const client = { id: 1, name: "qBit", cachedTorrents: JSON.stringify([torrent]) }
// No checkpoint in torrent CP list
seedStore([tracker], [snap1, snap2], [], [], [], [client])
const result = await computeTodayAtAGlance()
expect(result.movers.topUploaders).toHaveLength(0)
})
it("attaches tracker color to matched mover entries", async () => {
const tracker = makeTracker(1, { qbtTag: "aither", color: "#00d4ff" })
const snap1 = makeSnapshot(1, new Date(Date.now() - 3600000), "1000000000", "500000000")
const snap2 = makeSnapshot(1, new Date(), "2000000000", "1000000000")
const torrent = makeTorrent("hash1", "Colored Movie", "aither", 5000000000, 3000000000)
const cp = makeTorrentCheckpoint(1, "hash1", "0", "0")
const client = { id: 1, name: "qBit", cachedTorrents: JSON.stringify([torrent]) }
seedStore([tracker], [snap1, snap2], [], [], [cp], [client])
const result = await computeTodayAtAGlance()
expect(result.movers.topUploaders[0].trackerColor).toBe("#00d4ff")
})
})
// ---------------------------------------------------------------------------
// Fleet aggregation — multiple trackers
// ---------------------------------------------------------------------------
describe("computeTodayAtAGlance — fleet aggregation with multiple trackers", () => {
it("sums upload deltas across all trackers", async () => {
const tracker1 = makeTracker(1)
const tracker2 = makeTracker(2)
const now = Date.now()
// tracker1: 3G - 1G = 2G; tracker2: 4G - 2G = 2G; total = 4G
const snaps = [
makeSnapshot(1, new Date(now - 3600000), "1000000000", "500000000"),
makeSnapshot(2, new Date(now - 3600000), "2000000000", "1000000000"),
makeSnapshot(1, new Date(now), "3000000000", "600000000"),
makeSnapshot(2, new Date(now), "4000000000", "1200000000"),
]
seedStore([tracker1, tracker2], snaps)
const result = await computeTodayAtAGlance()
expect(BigInt(result.fleet.uploadDelta)).toBe(4000000000n)
})
it("sums download deltas across all trackers", async () => {
const tracker1 = makeTracker(1)
const tracker2 = makeTracker(2)
const now = Date.now()
// tracker1 dl: 700M - 500M = 200M; tracker2 dl: 500M - 200M = 300M; total = 500M
const snaps = [
makeSnapshot(1, new Date(now - 3600000), "1000000000", "500000000"),
makeSnapshot(2, new Date(now - 3600000), "1000000000", "200000000"),
makeSnapshot(1, new Date(now), "2000000000", "700000000"),
makeSnapshot(2, new Date(now), "2000000000", "500000000"),
]
seedStore([tracker1, tracker2], snaps)
const result = await computeTodayAtAGlance()
expect(BigInt(result.fleet.downloadDelta)).toBe(500000000n)
})
it("tracker with no snapshots contributes zero and still appears in trackers array", async () => {
const tracker1 = makeTracker(1)
const tracker2 = makeTracker(2) // no snapshots
const now = Date.now()
const snaps = [
makeSnapshot(1, new Date(now - 3600000), "1000000000", "500000000"),
makeSnapshot(1, new Date(now), "2000000000", "600000000"),
]
seedStore([tracker1, tracker2], snaps)
const result = await computeTodayAtAGlance()
expect(BigInt(result.fleet.uploadDelta)).toBe(1000000000n)
expect(result.trackers).toHaveLength(2)
expect(result.trackers.find((t) => t.id === 2)?.uploadDelta).toBe("0")
})
})
// ---------------------------------------------------------------------------
// Ratio weighted average
// ---------------------------------------------------------------------------
describe("computeTodayAtAGlance — ratio weighted average", () => {
it("weights ratio change by upload volume — more upload means more weight", async () => {
const tracker1 = makeTracker(1)
const tracker2 = makeTracker(2)
const now = Date.now()
// tracker1: +2G upload, ratio +0.1; tracker2: +0.5G upload, ratio +1.0
// weighted avg = (0.1 * 2G + 1.0 * 0.5G) / (2G + 0.5G)
// = (0.2G + 0.5G) / 2.5G = 0.7 / 2.5 = 0.28
const snaps = [
makeSnapshot(1, new Date(now - 3600000), "2000000000", "2000000000", null, 1.0),
makeSnapshot(2, new Date(now - 3600000), "500000000", "500000000", null, 1.0),
makeSnapshot(1, new Date(now), "4000000000", "2000000000", null, 1.1),
makeSnapshot(2, new Date(now), "1000000000", "500000000", null, 2.0),
]
seedStore([tracker1, tracker2], snaps)
const result = await computeTodayAtAGlance()
expect(result.fleet.ratioChange).not.toBeNull()
expect(result.fleet.ratioChange as number).toBeCloseTo(0.28, 5)
})
it("returns null ratioChange when all upload deltas are zero (no progress this session)", async () => {
const tracker = makeTracker(1)
// Both snapshots have same uploaded bytes — delta = 0, weight = 0
const snap1 = makeSnapshot(
1,
new Date(Date.now() - 3600000),
"1000000000",
"500000000",
null,
1.0
)
const snap2 = makeSnapshot(1, new Date(), "1000000000", "500000000", null, 1.5)
seedStore([tracker], [snap1, snap2])
const result = await computeTodayAtAGlance()
expect(result.fleet.ratioChange).toBeNull()
})
})
// ---------------------------------------------------------------------------
// Activity counts
// ---------------------------------------------------------------------------
describe("computeTodayAtAGlance — activity counts", () => {
it("counts torrents whose addedAt unix timestamp falls on today", async () => {
const tracker = makeTracker(1, { qbtTag: "aither" })
const snap1 = makeSnapshot(1, new Date(Date.now() - 3600000), "1000000000", "500000000")
const snap2 = makeSnapshot(1, new Date(), "2000000000", "1000000000")
const todayNoon = new Date()
todayNoon.setHours(12, 0, 0, 0)
const torrent = makeTorrent(
"h1",
"New Movie",
"aither",
0,
0,
Math.floor(todayNoon.getTime() / 1000),
-1
)
const client = { id: 1, name: "qBit", cachedTorrents: JSON.stringify([torrent]) }
seedStore([tracker], [snap1, snap2], [], [], [], [client])
const result = await computeTodayAtAGlance()
expect(result.activity.addedToday).toBe(1)
})
it("does not count torrents with addedAt of 0 (unix epoch, not today)", async () => {
const tracker = makeTracker(1, { qbtTag: "aither" })
const snap1 = makeSnapshot(1, new Date(Date.now() - 3600000), "1000000000", "500000000")
const snap2 = makeSnapshot(1, new Date(), "2000000000", "1000000000")
const torrent = makeTorrent("h1", "Old Movie", "aither", 0, 0, 0, -1)
const client = { id: 1, name: "qBit", cachedTorrents: JSON.stringify([torrent]) }
seedStore([tracker], [snap1, snap2], [], [], [], [client])
const result = await computeTodayAtAGlance()
expect(result.activity.addedToday).toBe(0)
})
it("counts torrents whose completedAt unix timestamp falls on today", async () => {
const tracker = makeTracker(1, { qbtTag: "aither" })
const snap1 = makeSnapshot(1, new Date(Date.now() - 3600000), "1000000000", "500000000")
const snap2 = makeSnapshot(1, new Date(), "2000000000", "1000000000")
const todayNoon = new Date()
todayNoon.setHours(12, 0, 0, 0)
const torrent = makeTorrent(
"h1",
"Done Movie",
"aither",
0,
5000000000,
0,
Math.floor(todayNoon.getTime() / 1000)
)
const client = { id: 1, name: "qBit", cachedTorrents: JSON.stringify([torrent]) }
seedStore([tracker], [snap1, snap2], [], [], [], [client])
const result = await computeTodayAtAGlance()
expect(result.activity.completedToday).toBe(1)
})
it("ignores completedAt of -1 (torrent is still downloading)", async () => {
const tracker = makeTracker(1, { qbtTag: "aither" })
const snap1 = makeSnapshot(1, new Date(Date.now() - 3600000), "1000000000", "500000000")
const snap2 = makeSnapshot(1, new Date(), "2000000000", "1000000000")
const torrent = makeTorrent("h1", "Seeding Movie", "aither", 5000000000, 5000000000, 0, -1)
const client = { id: 1, name: "qBit", cachedTorrents: JSON.stringify([torrent]) }
seedStore([tracker], [snap1, snap2], [], [], [], [client])
const result = await computeTodayAtAGlance()
expect(result.activity.completedToday).toBe(0)
})
it("ignores completedAt of 0 (qBittorrent sentinel for 'not completed')", async () => {
const tracker = makeTracker(1, { qbtTag: "aither" })
const snap1 = makeSnapshot(1, new Date(Date.now() - 3600000), "1000000000", "500000000")
const snap2 = makeSnapshot(1, new Date(), "2000000000", "1000000000")
const torrent = makeTorrent("h1", "Seeding Movie", "aither", 5000000000, 5000000000, 0, 0)
const client = { id: 1, name: "qBit", cachedTorrents: JSON.stringify([torrent]) }
seedStore([tracker], [snap1, snap2], [], [], [], [client])
const result = await computeTodayAtAGlance()
expect(result.activity.completedToday).toBe(0)
})
})
// ---------------------------------------------------------------------------
// Output shape invariants
// ---------------------------------------------------------------------------
describe("computeTodayAtAGlance — output shape", () => {
it("fleet object has all required keys", async () => {
const result = await computeTodayAtAGlance()
expect(result.fleet).toHaveProperty("uploadDelta")
expect(result.fleet).toHaveProperty("downloadDelta")
expect(result.fleet).toHaveProperty("bufferDelta")
expect(result.fleet).toHaveProperty("ratioChange")
expect(result.fleet).toHaveProperty("seedbonusChange")
expect(result.fleet).toHaveProperty("uploadDeltaYesterday")
expect(result.fleet).toHaveProperty("downloadDeltaYesterday")
expect(result.fleet).toHaveProperty("bufferDeltaYesterday")
})
it("fleet delta values are strings (bigints serialized for JSON safety)", async () => {
const result = await computeTodayAtAGlance()
expect(typeof result.fleet.uploadDelta).toBe("string")
expect(typeof result.fleet.downloadDelta).toBe("string")
expect(typeof result.fleet.bufferDelta).toBe("string")
})
it("movers has topUploaders and topDownloaders as arrays", async () => {
const result = await computeTodayAtAGlance()
expect(Array.isArray(result.movers.topUploaders)).toBe(true)
expect(Array.isArray(result.movers.topDownloaders)).toBe(true)
})
it("each tracker entry has id, name, color, uploadDelta, downloadDelta, bufferDelta", async () => {
const tracker = makeTracker(1)
const snap1 = makeSnapshot(1, new Date(Date.now() - 3600000), "1000000000", "500000000")
const snap2 = makeSnapshot(1, new Date(), "2000000000", "600000000")
seedStore([tracker], [snap1, snap2])
const result = await computeTodayAtAGlance()
const t = result.trackers[0]
expect(t).toHaveProperty("id")
expect(t).toHaveProperty("name")
expect(t).toHaveProperty("color")
expect(t).toHaveProperty("uploadDelta")
expect(t).toHaveProperty("downloadDelta")
expect(t).toHaveProperty("bufferDelta")
})
})