@torrent-tv/proxy 2.31.0 → 2.32.0

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,145 @@
1
+ /**
2
+ * @file The decode-cost fit, and the case it exists to prevent.
3
+ *
4
+ * The failure being pinned is real and dated: on 2026-08-17 three clips for
5
+ * three unknowns returned `0.007542 × Mpx/s + 0.000000 × Mbit/s + 0.0000 s/s`,
6
+ * with the bitrate term and the constant exactly zero, and the prediction built
7
+ * on it was 1.8-2.2x optimistic. An exact system cannot notice that two of its
8
+ * points said the same thing; these tests hold the replacement to noticing.
9
+ */
10
+
11
+ import assert from "node:assert/strict";
12
+ import test from "node:test";
13
+
14
+ import { decodeCostOf, fitDecodeCost } from "../services/decode-cost-fit.js";
15
+
16
+ const FPS = 24;
17
+
18
+ /**
19
+ * A clip's measurement, priced by a known truth so a fit can be checked against
20
+ * the answer it should recover.
21
+ *
22
+ * @param {number} width
23
+ * @param {number} height
24
+ * @param {number} megabitsPerSecond
25
+ * @param {{ pixel: number, bitrate: number, constant: number, noise?: number }} truth
26
+ * @returns {{ megapixelsPerSecond: number, megabitsPerSecond: number, costSecondsPerSecond: number }}
27
+ */
28
+ function clip(width, height, megabitsPerSecond, truth) {
29
+ const megapixelsPerSecond = (width * height * FPS) / 1e6;
30
+ const cost =
31
+ truth.pixel * megapixelsPerSecond + truth.bitrate * megabitsPerSecond + truth.constant + (truth.noise ?? 0);
32
+ return { megapixelsPerSecond, megabitsPerSecond, costSecondsPerSecond: cost };
33
+ }
34
+
35
+ /** The set the clips were cut to: three sizes, two bitrates, varied independently. */
36
+ const SIZES = [
37
+ [1920, 1080],
38
+ [1280, 720],
39
+ [854, 480]
40
+ ];
41
+ const BITRATES = [9.5, 1.1];
42
+
43
+ /**
44
+ * @param {{ pixel: number, bitrate: number, constant: number }} truth
45
+ * @param {number[]} [noise] - Per-clip disturbance, in order.
46
+ * @returns {ReturnType<typeof clip>[]}
47
+ */
48
+ function wellConditionedSet(truth, noise = []) {
49
+ const samples = [];
50
+ let index = 0;
51
+ for (const [width, height] of SIZES) {
52
+ for (const bitrate of BITRATES) {
53
+ samples.push(clip(width, height, bitrate, { ...truth, noise: noise[index] ?? 0 }));
54
+ index += 1;
55
+ }
56
+ }
57
+ return samples;
58
+ }
59
+
60
+ test("a well-conditioned set recovers every term", () => {
61
+ const truth = { pixel: 0.0055, bitrate: 0.0099, constant: 0.057 };
62
+ const model = fitDecodeCost(wellConditionedSet(truth));
63
+
64
+ assert.ok(model, "six clips over three unknowns must produce a model");
65
+ assert.equal(model.shape, "pixels+bitrate+constant");
66
+ assert.deepEqual(model.dropped, []);
67
+ assert.ok(Math.abs(model.pixelTerm - truth.pixel) < 1e-6, `pixel term ${model.pixelTerm}`);
68
+ assert.ok(Math.abs(model.bitrateTerm - truth.bitrate) < 1e-6, `bitrate term ${model.bitrateTerm}`);
69
+ assert.ok(Math.abs(model.constantTerm - truth.constant) < 1e-6, `constant ${model.constantTerm}`);
70
+ });
71
+
72
+ test("three clips are refused outright — an exact system cannot see its own degeneracy", () => {
73
+ // Exactly the shape that shipped: two clips at the same pixel rate differing
74
+ // only in bitrate, and a third at another size.
75
+ const truth = { pixel: 0.0055, bitrate: 0.0099, constant: 0.057 };
76
+ const three = [clip(1920, 1080, 11.4, truth), clip(1920, 1080, 0.97, truth), clip(1280, 720, 2.25, truth)];
77
+
78
+ assert.equal(
79
+ fitDecodeCost(three),
80
+ null,
81
+ "with no residual there is nothing to notice a degeneracy with, so no model may be published"
82
+ );
83
+ });
84
+
85
+ test("a term the measurements do not determine is dropped, and said so", () => {
86
+ // A host whose decoding does not depend on bitrate at all: the term is not
87
+ // small, it is absent. What must NOT happen is publishing a zero as though it
88
+ // had been measured.
89
+ const truth = { pixel: 0.0055, bitrate: 0, constant: 0.057 };
90
+ // Deliberately NOT alternating with the bitrate column: an earlier version
91
+ // of this test put positive noise on every high-bitrate clip and negative on
92
+ // every low one, which IS a bitrate signal — the fit found it, correctly, and
93
+ // the test was wrong.
94
+ const noise = [0.004, 0.003, -0.004, 0.002, -0.003, -0.002];
95
+ const model = fitDecodeCost(wellConditionedSet(truth, noise));
96
+
97
+ assert.ok(model);
98
+ assert.ok(model.dropped.includes("bitrate"), `dropped: ${model.dropped.join(",") || "nothing"}`);
99
+ assert.equal(model.bitrateTerm, 0);
100
+ assert.ok(model.shape.includes("pixels"));
101
+ });
102
+
103
+ test("noise in the readings does not become a term", () => {
104
+ // Pure noise around a pixels-only truth, larger than any bitrate effect.
105
+ const truth = { pixel: 0.0055, bitrate: 0, constant: 0 };
106
+ const noise = [0.01, 0.009, -0.012, 0.011, -0.008, -0.01];
107
+ const model = fitDecodeCost(wellConditionedSet(truth, noise));
108
+
109
+ assert.ok(model);
110
+ assert.ok(model.pixelTerm > 0, "the one relationship every reading agrees on survives");
111
+ assert.ok(
112
+ model.dropped.length > 0,
113
+ "and the terms the noise could have supplied are reported as undetermined"
114
+ );
115
+ });
116
+
117
+ test("without a measurable dependence on the source there is no model", () => {
118
+ // Every clip costs the same regardless of size or bitrate: nothing here says
119
+ // what a bigger picture costs, and inventing it is worse than having none.
120
+ const flat = wellConditionedSet({ pixel: 0, bitrate: 0, constant: 0.3 });
121
+ assert.equal(fitDecodeCost(flat), null);
122
+ assert.equal(fitDecodeCost([]), null);
123
+ assert.equal(fitDecodeCost(null), null);
124
+ });
125
+
126
+ test("readings that are not measurements are left out", () => {
127
+ const truth = { pixel: 0.0055, bitrate: 0.0099, constant: 0.057 };
128
+ const samples = [
129
+ ...wellConditionedSet(truth),
130
+ { megapixelsPerSecond: Number.NaN, megabitsPerSecond: 5, costSecondsPerSecond: 1 },
131
+ { megapixelsPerSecond: 20, megabitsPerSecond: 5, costSecondsPerSecond: 0 }
132
+ ];
133
+ const model = fitDecodeCost(samples);
134
+ assert.ok(model);
135
+ assert.equal(model.samples, 6);
136
+ });
137
+
138
+ test("what a model prices a film at", () => {
139
+ const model = fitDecodeCost(wellConditionedSet({ pixel: 0.0055, bitrate: 0.0099, constant: 0.057 }));
140
+ // The field film: 1080p24 at about 8 Mbit/s.
141
+ const cost = decodeCostOf(model, { megapixelsPerSecond: 49.77, megabitsPerSecond: 8 });
142
+ assert.ok(cost > 0);
143
+ // Which is a decode speed of 1/cost — the figure the quality offer rests on.
144
+ assert.ok(1 / cost > 1 && 1 / cost < 10, `decodes at ${(1 / cost).toFixed(2)}x`);
145
+ });