@torrent-tv/proxy 2.84.0 → 2.85.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,172 @@
1
+ /**
2
+ * @file The three states a viewer can be in, and what each is worth.
3
+ *
4
+ * Written against the failure of 2026-09-14: a page whose tab was hidden for
5
+ * 145 seconds, which had measured nothing and therefore said nothing, was
6
+ * carried 146 seconds into a film it had not begun — and the soundtrack's
7
+ * encoder was placed there while the browser asked for segment #0 and was
8
+ * refused for sixty seconds.
9
+ */
10
+
11
+ import assert from "node:assert/strict";
12
+ import test from "node:test";
13
+
14
+ import { Viewer } from "../services/viewer/Viewer.js";
15
+
16
+ const AT = 1_000_000;
17
+
18
+ test("a viewer who has stated nothing does not move, however long they are silent", () => {
19
+ const viewer = new Viewer("one", AT);
20
+ viewer.moveTo(0, AT);
21
+
22
+ // 146 seconds of silence — the field figure exactly.
23
+ assert.equal(viewer.positionSeconds(AT + 146_000), 0);
24
+ });
25
+
26
+ test("a viewer who has stated nothing is waiting, and their work is due now", () => {
27
+ const viewer = new Viewer("one", AT);
28
+ viewer.moveTo(0, AT);
29
+
30
+ assert.equal(viewer.playing, false);
31
+ assert.equal(viewer.waiting, true);
32
+ assert.equal(viewer.wantsFilmNow(), true);
33
+ });
34
+
35
+ test("a playing viewer moves with the film, and no further than what they hold", () => {
36
+ const viewer = new Viewer("one", AT);
37
+ viewer.report({ bufferedAheadSec: 30, positionSeconds: 100, playing: true, waiting: false }, AT);
38
+
39
+ // Ten seconds of silence, ten seconds of film.
40
+ assert.equal(viewer.positionSeconds(AT + 10_000), 110);
41
+ // Two minutes of silence buys only the thirty seconds they said they held.
42
+ assert.equal(viewer.positionSeconds(AT + 120_000), 130);
43
+ });
44
+
45
+ test("a viewer holding nothing cannot move at all, even reported as playing", () => {
46
+ const viewer = new Viewer("one", AT);
47
+ viewer.report({ bufferedAheadSec: 0, positionSeconds: 0, playing: true, waiting: false }, AT);
48
+
49
+ assert.equal(viewer.positionSeconds(AT + 146_000), 0);
50
+ });
51
+
52
+ test("a viewer who stopped the picture is not waiting, and nothing of theirs falls due", () => {
53
+ const viewer = new Viewer("one", AT);
54
+ viewer.report({ bufferedAheadSec: 42, positionSeconds: 100, playing: false, waiting: false }, AT);
55
+
56
+ assert.equal(viewer.wantsFilmNow(), false);
57
+ assert.equal(viewer.positionSeconds(AT + 60_000), 100);
58
+ });
59
+
60
+ test("a viewer starved of material is waiting, and is as urgent as one playing", () => {
61
+ const playing = new Viewer("playing", AT);
62
+ playing.report({ bufferedAheadSec: 30, positionSeconds: 100, playing: true, waiting: false }, AT);
63
+ const starved = new Viewer("starved", AT);
64
+ starved.report({ bufferedAheadSec: 0, positionSeconds: 100, playing: false, waiting: true }, AT);
65
+
66
+ // Both want film now, and that is the whole point: the starved one used to
67
+ // count as somebody who had chosen to stop.
68
+ assert.equal(starved.wantsFilmNow(), true);
69
+ assert.equal(playing.wantsFilmNow(), true);
70
+ // And the starved one cannot be carried forward from where they stand,
71
+ // because they hold nothing to play.
72
+ assert.equal(starved.positionSeconds(AT + 60_000), 100);
73
+ });
74
+
75
+ test("a report without a link measurement still states everything about the viewer", () => {
76
+ const viewer = new Viewer("one", AT);
77
+ viewer.report({ bufferedAheadSec: 0, positionSeconds: 12, playing: false, waiting: true }, AT);
78
+
79
+ assert.equal(viewer.positionSeconds(AT), 12);
80
+ assert.equal(viewer.waiting, true);
81
+ // Nothing was measured, so nothing is claimed about the link.
82
+ assert.equal(viewer.netReport, null);
83
+ });
84
+
85
+ test("a page one release behind, which cannot say `waiting`, is read from what it does say", () => {
86
+ const starved = new Viewer("starved", AT);
87
+ starved.report({ linkMbps: 5, bufferedAheadSec: 0, positionSeconds: 0, playing: false }, AT);
88
+ assert.equal(starved.waiting, true, "stopped and holding nothing is starved");
89
+
90
+ const paused = new Viewer("paused", AT);
91
+ paused.report({ linkMbps: 5, bufferedAheadSec: 40, positionSeconds: 0, playing: false }, AT);
92
+ assert.equal(paused.waiting, false, "stopped while holding film is a viewer's own pause");
93
+ });
94
+
95
+ test("a seek does not carry the cushion of the place it left", () => {
96
+ const viewer = new Viewer("one", AT);
97
+ viewer.report({ bufferedAheadSec: 120, positionSeconds: 100, playing: true, waiting: false }, AT);
98
+
99
+ viewer.moveTo(2000, AT + 1000);
100
+ // Nothing is held at the new place until they say so, so the position stands.
101
+ assert.equal(viewer.positionSeconds(AT + 61_000), 2000);
102
+ });
103
+
104
+ test("a page that is not on screen wants nothing now, whatever else it says", () => {
105
+ const viewer = new Viewer("one", AT);
106
+ viewer.report(
107
+ { bufferedAheadSec: 0, positionSeconds: 100, playing: false, waiting: true, onScreen: false },
108
+ AT
109
+ );
110
+
111
+ assert.equal(viewer.wantsFilmNow(), false);
112
+ });
113
+
114
+ test("the intake hands the statement to the viewer on the output they are watching", async () => {
115
+ const { recordViewerReport } = await import("../services/viewer/report-intake.js");
116
+ const picture = { id: "picture", state: "ready", activeVariantId: "rung" };
117
+ const rung = { id: "rung", state: "ready" };
118
+ const sessions = new Map([
119
+ ["picture", picture],
120
+ ["rung", rung]
121
+ ]);
122
+ const viewers = {
123
+ of(target, id) {
124
+ target.viewers ??= new Map();
125
+ if (!target.viewers.has(id)) {
126
+ target.viewers.set(id, new Viewer(id, AT));
127
+ }
128
+ return target.viewers.get(id);
129
+ }
130
+ };
131
+
132
+ // The page always addresses the PICTURE — it is never told which rung it is
133
+ // on — and the statement belongs to the rung actually on screen.
134
+ const taken = recordViewerReport({
135
+ sessions,
136
+ viewers,
137
+ sessionId: "picture",
138
+ report: { linkMbps: 8, bufferedAheadSec: 10, positionSeconds: 5, playing: true, consumerId: "one" },
139
+ now: AT
140
+ });
141
+
142
+ assert.equal(taken, true);
143
+ assert.equal(rung.viewers.get("one").positionSeconds(AT), 5);
144
+ assert.equal(picture.viewers?.has("one") ?? false, false);
145
+ });
146
+
147
+ test("a reading decides nothing once it is older than the link it describes", () => {
148
+ const viewer = new Viewer("one", AT);
149
+ viewer.report({ linkMbps: 8, bufferedAheadSec: 10, positionSeconds: 5, playing: true }, AT);
150
+
151
+ assert.equal(viewer.linkReading(AT)?.linkMbps, 8);
152
+ assert.equal(viewer.linkReading(AT + 29_000)?.linkMbps, 8);
153
+ assert.equal(viewer.linkReading(AT + 31_000), null);
154
+ // Nothing was deleted: the viewer is still here and still where they said.
155
+ assert.equal(viewer.isPresent(), true);
156
+ assert.equal(viewer.positionSeconds(AT + 31_000), 15);
157
+ });
158
+
159
+ test("a report into a session that is gone is refused rather than invented", async () => {
160
+ const { recordViewerReport } = await import("../services/viewer/report-intake.js");
161
+ const sessions = new Map([["dead", { id: "dead", state: "disposed" }]]);
162
+ const viewers = { of() { throw new Error("must not be asked"); } };
163
+
164
+ assert.equal(
165
+ recordViewerReport({ sessions, viewers, sessionId: "dead", report: { bufferedAheadSec: 0 }, now: AT }),
166
+ false
167
+ );
168
+ assert.equal(
169
+ recordViewerReport({ sessions, viewers, sessionId: "never", report: { bufferedAheadSec: 0 }, now: AT }),
170
+ false
171
+ );
172
+ });
@@ -49,19 +49,40 @@ test("position is one fact with one writer, and a function of time", () => {
49
49
  const viewer = new Viewer("someone");
50
50
  assert.equal(viewer.positionSeconds(), null);
51
51
 
52
- viewer.moveTo(40, 1_000_000);
53
- viewer.playing = false;
52
+ viewer.report({ bufferedAheadSec: 60, positionSeconds: 40, playing: false }, 1_000_000);
54
53
  assert.equal(viewer.positionSeconds(1_000_000), 40);
55
54
  assert.equal(viewer.positionSeconds(1_060_000), 40, "a stopped picture covers no film");
56
55
 
57
- viewer.playing = true;
56
+ viewer.report({ bufferedAheadSec: 60, positionSeconds: 40, playing: true }, 1_000_000);
58
57
  assert.equal(viewer.positionSeconds(1_010_000), 50, "a playing one covers a second a second");
58
+ assert.equal(
59
+ viewer.positionSeconds(1_600_000),
60
+ 100,
61
+ "and no further than the film they said they held"
62
+ );
59
63
 
60
64
  // A seek is the same statement, and there is no second field for it to win
61
65
  // against: two writers taking turns on one reading is what made the priority
62
66
  // map jump backwards several times a second in the field on 2026-09-13.
63
67
  viewer.moveTo(900, 1_010_000);
64
68
  assert.equal(viewer.positionSeconds(1_010_000), 900);
69
+ assert.equal(
70
+ viewer.positionSeconds(1_070_000),
71
+ 900,
72
+ "and a seek carries no cushion from where it left, so nothing is extrapolated"
73
+ );
74
+ });
75
+
76
+ test("a viewer who has stated nothing has a position and does not drift from it", () => {
77
+ const viewer = new Viewer("someone");
78
+ viewer.moveTo(0, 1_000_000);
79
+
80
+ // The whole of the fault of 2026-09-14: `playing` used to start true, and a
81
+ // page that had measured nothing was never allowed to say otherwise, so a
82
+ // hidden tab's viewer was carried 146 seconds into a film they had not begun.
83
+ assert.equal(viewer.playing, false);
84
+ assert.equal(viewer.waiting, true, "a viewer who has just arrived is waiting on us");
85
+ assert.equal(viewer.positionSeconds(1_146_000), 0);
65
86
  });
66
87
 
67
88
  test("asking for a viewer twice is the same viewer", () => {