@torrent-tv/proxy 2.35.0 → 2.36.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,71 @@
1
+ /**
2
+ * @file What the torrent costs per megabyte, once the draw that would have
3
+ * happened anyway is taken off.
4
+ *
5
+ * The readings behind these cases are the addon host's own, 2026-08-18:
6
+ * 145.4 ms/MB over 8.7 MB and 23.1 ms/MB over 54 MB in the same session, which
7
+ * is what a fixed minimum-megabytes threshold failed to stop.
8
+ */
9
+ import assert from "node:assert/strict";
10
+ import { test } from "node:test";
11
+ import { baseDrawFrom, costPerMegabyteFrom } from "../services/torrent-cost.js";
12
+
13
+ test("the base draw is the share of a core spent with nothing to do", () => {
14
+ assert.equal(baseDrawFrom({ cpuSeconds: 0.5, elapsedSeconds: 5 }), 0.1);
15
+ assert.equal(baseDrawFrom({ cpuSeconds: 0, elapsedSeconds: 5 }), 0);
16
+ assert.equal(baseDrawFrom({ cpuSeconds: 1, elapsedSeconds: 0 }), null);
17
+ assert.equal(baseDrawFrom({ cpuSeconds: Number.NaN, elapsedSeconds: 5 }), null);
18
+ });
19
+
20
+ test("the draw comes off before the megabytes are divided by", () => {
21
+ // 1 s of CPU over 5 s, of which 0.1 of a core is spent regardless: 0.5 s left
22
+ // for 50 MB.
23
+ const cost = costPerMegabyteFrom({ cpuSeconds: 1, elapsedSeconds: 5, megabytes: 50, baseDraw: 0.1 });
24
+ assert.equal(cost, 0.01);
25
+ });
26
+
27
+ test("a small interval no longer reports a huge price", () => {
28
+ // The same host and the same draw, one interval moving 54 MB and one moving
29
+ // 8.7 MB. Without the subtraction the second reads six times the first.
30
+ const draw = 0.1;
31
+ const big = costPerMegabyteFrom({ cpuSeconds: 1.85, elapsedSeconds: 5, megabytes: 54, baseDraw: draw });
32
+ const small = costPerMegabyteFrom({ cpuSeconds: 0.72, elapsedSeconds: 5, megabytes: 8.7, baseDraw: draw });
33
+ assert.ok(Math.abs(big - small) / big < 0.05, `${big} vs ${small}`);
34
+ });
35
+
36
+ test("with no base draw measured the interval says nothing", () => {
37
+ assert.equal(costPerMegabyteFrom({ cpuSeconds: 1, elapsedSeconds: 5, megabytes: 50, baseDraw: null }), null);
38
+ });
39
+
40
+ test("an interval that spent no more than the draw says nothing either", () => {
41
+ assert.equal(costPerMegabyteFrom({ cpuSeconds: 0.5, elapsedSeconds: 5, megabytes: 50, baseDraw: 0.1 }), null);
42
+ });
43
+
44
+ test("nothing moved cannot be priced per megabyte", () => {
45
+ assert.equal(costPerMegabyteFrom({ cpuSeconds: 1, elapsedSeconds: 5, megabytes: 0, baseDraw: 0.1 }), null);
46
+ });
47
+
48
+ test("a remainder inside the draw's own disagreement is not a price", () => {
49
+ // The draw is 0.1 of a core and its readings disagree by 0.01 of one. Over a
50
+ // five-second interval that is 0.05 s of CPU nobody can attribute, so a
51
+ // remainder smaller than that says nothing however many megabytes moved.
52
+ const noisy = costPerMegabyteFrom({
53
+ cpuSeconds: 0.53,
54
+ elapsedSeconds: 5,
55
+ megabytes: 0.5,
56
+ baseDraw: 0.1,
57
+ drawScatter: 0.01
58
+ });
59
+ assert.equal(noisy, null);
60
+
61
+ // The same host and the same disagreement, an interval that moved enough for
62
+ // the remainder to exceed it.
63
+ const solid = costPerMegabyteFrom({
64
+ cpuSeconds: 1.74,
65
+ elapsedSeconds: 5,
66
+ megabytes: 54,
67
+ baseDraw: 0.1,
68
+ drawScatter: 0.01
69
+ });
70
+ assert.ok(Math.abs(solid - 0.023) < 0.001, `${solid}`);
71
+ });