@torrent-tv/proxy 2.80.18 → 2.81.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.
Files changed (67) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/docs/encode-architecture.md +51 -2
  3. package/package.json +1 -1
  4. package/research/double-spawn-2026-09-10.md +171 -0
  5. package/services/disk/DiskSpace.js +146 -0
  6. package/services/disk/wire.js +60 -0
  7. package/services/encode/EncodeRun.js +37 -9
  8. package/services/encode/SegmentStore.js +284 -232
  9. package/services/encode/run-command.js +16 -1
  10. package/services/hls-session-manager.js +31 -128
  11. package/services/orchestrators/EncodeOrchestrator.js +52 -38
  12. package/services/piece-store/allowance.js +107 -0
  13. package/services/piece-store/piece-disk-store.js +365 -0
  14. package/services/piece-store/shared-piece-store.js +1549 -1535
  15. package/services/segment-formats/fmp4.js +54 -0
  16. package/services/segment-formats/mpegts.js +54 -0
  17. package/services/torrent-worker/client.js +32 -0
  18. package/services/torrent-worker/pool-adapter.js +15 -0
  19. package/services/torrent-worker/protocol.js +9 -0
  20. package/services/torrent-worker/worker.js +8 -1
  21. package/services/viewer/positions.js +48 -0
  22. package/test/audio-inventory.test.js +176 -176
  23. package/test/auto-quality-step.test.js +514 -514
  24. package/test/concurrent-cost.test.js +138 -138
  25. package/test/coverage-follows-the-disk.test.js +191 -187
  26. package/test/coverage-map.test.js +195 -195
  27. package/test/declared-tracks.test.js +35 -35
  28. package/test/disk-space.test.js +138 -0
  29. package/test/encode-orchestrator.test.js +0 -3
  30. package/test/encode-run.test.js +5 -12
  31. package/test/held-request-width.test.js +155 -155
  32. package/test/helpers/encode-run.js +2 -2
  33. package/test/matroska-blocks.test.js +0 -0
  34. package/test/matroska-cues-track.test.js +192 -192
  35. package/test/mp4-composition-times.test.js +0 -0
  36. package/test/mp4-subtitles.test.js +173 -173
  37. package/test/one-authority.test.js +281 -220
  38. package/test/orchestrator-wired.test.js +199 -195
  39. package/test/packet-witness-ring.test.js +236 -236
  40. package/test/packet-witness.test.js +148 -148
  41. package/test/piece-disk-store.test.js +267 -0
  42. package/test/piece-reader.test.js +4 -4
  43. package/test/piece-store-eviction.test.js +17 -17
  44. package/test/piece-store-reservations.test.js +20 -1
  45. package/test/piece-store-slow-disk.test.js +16 -1
  46. package/test/produced-copy-choice.test.js +258 -358
  47. package/test/read-window.test.js +6 -6
  48. package/test/run-intervals.test.js +100 -100
  49. package/test/seek-landing.test.js +109 -109
  50. package/test/segment-serve-wiring.test.js +8 -9
  51. package/test/segment-store-eviction.test.js +232 -0
  52. package/test/segment-store.test.js +238 -216
  53. package/test/segments-are-shared.test.js +1 -1
  54. package/test/shared-piece-store.test.js +12 -12
  55. package/test/sidecar-naming.test.js +142 -142
  56. package/test/subtitle-cue-framing.test.js +200 -200
  57. package/test/subtitle-cue-walk.test.js +369 -369
  58. package/test/subtitle-defaults.test.js +97 -97
  59. package/test/subtitle-track-numbering.test.js +370 -370
  60. package/test/tail-duplication.test.js +167 -167
  61. package/test/tracks-begin-together.test.js +195 -195
  62. package/test/two-viewers-one-picture.test.js +374 -374
  63. package/test/video-facts.test.js +102 -102
  64. package/test/wedge-certainty.test.js +131 -131
  65. package/services/encode/open-piece.js +0 -135
  66. package/services/piece-store/disk-tier.js +0 -151
  67. package/test/open-piece.test.js +0 -152
@@ -1,195 +1,195 @@
1
- /**
2
- * @file The three states of a segment number, and the two questions that decide
3
- * where an encoder goes.
4
- */
5
-
6
- import test from "node:test";
7
- import assert from "node:assert/strict";
8
- import { CoverageMap } from "../services/encode/CoverageMap.js";
9
-
10
- /**
11
- * A stand-in for a run.
12
- *
13
- * The map files a claim under the run ITSELF: a run has no name, and needs
14
- * none — what identifies it is that it is itself, and what identifies it in a
15
- * line is the stretch it was given, which no other live run of one output can
16
- * hold.
17
- *
18
- * @returns {object}
19
- */
20
- function aRun() {
21
- return {};
22
- }
23
-
24
- test("a number nobody has made and nobody is making is free", () => {
25
- const map = new CoverageMap({ segmentCount: 10 });
26
- assert.equal(map.stateOf(3), "free");
27
- assert.equal(map.firstGapFrom(0), 0);
28
- });
29
-
30
- test("a closed file is ready whoever made it", () => {
31
- const map = new CoverageMap({ segmentCount: 10 });
32
- map.markReady(0);
33
- map.markReady(1);
34
- assert.equal(map.stateOf(1), "ready");
35
- assert.equal(map.firstGapFrom(0), 2);
36
- });
37
-
38
- test("a run claims a stretch, and the gap search skips it", () => {
39
- // The claim is an interval and not a head, which is the whole reason two runs
40
- // can work on one output: the second is sent past what the first will reach.
41
- const map = new CoverageMap({ segmentCount: 100 });
42
- const runA = aRun();
43
- map.claim(runA, 0, 40);
44
- assert.equal(map.stateOf(20), "making");
45
- assert.equal(map.makerOf(20), runA);
46
- assert.equal(map.firstGapFrom(0), 41);
47
- });
48
-
49
- test("a run that ends gives back what it did not finish, and keeps what it did", () => {
50
- const map = new CoverageMap({ segmentCount: 100 });
51
- const runA = aRun();
52
- map.claim(runA, 0, 40);
53
- map.markReady(0);
54
- map.markReady(1);
55
- map.release(runA);
56
- assert.equal(map.stateOf(0), "ready", "a closed file survives its maker");
57
- assert.equal(map.stateOf(2), "free", "the rest of the stretch is free again");
58
- assert.equal(map.firstGapFrom(0), 2);
59
- });
60
-
61
- test("a run with no end claims everything ahead of it", () => {
62
- // Which is what every run was before ends existed, and is why a second run
63
- // could never be placed.
64
- const map = new CoverageMap({ segmentCount: 100 });
65
- const runA = aRun();
66
- map.claim(runA, 10, Number.POSITIVE_INFINITY);
67
- assert.equal(map.firstGapFrom(10), null);
68
- assert.equal(map.firstGapFrom(0), 0, "behind it is still free");
69
- });
70
-
71
- test("claiming again replaces the earlier claim rather than adding to it", () => {
72
- // A run moved forward past ready material states its new stretch; if the two
73
- // accumulated, the ground it has left would stay unavailable for ever.
74
- const map = new CoverageMap({ segmentCount: 100 });
75
- const runA = aRun();
76
- map.claim(runA, 0, 20);
77
- map.claim(runA, 50, 70);
78
- assert.equal(map.stateOf(10), "free");
79
- assert.equal(map.stateOf(60), "making");
80
- });
81
-
82
- test("the covered stretch ahead is measured, which is what prices a move", () => {
83
- const map = new CoverageMap({ segmentCount: 100 });
84
- const runA = aRun();
85
- const runB = aRun();
86
- for (let index = 10; index <= 24; index += 1) {
87
- map.markReady(index);
88
- }
89
- map.claim(runB, 25, 30);
90
- // Ready 10..24 then another run's 25..30, so fifteen plus six.
91
- assert.equal(map.coveredRunFrom(10, runA), 21);
92
- assert.equal(map.firstGapFrom(10), 31);
93
- });
94
-
95
- test("a run's own claim is not counted as somebody else's coverage", () => {
96
- // Otherwise a run would read its own stretch as a reason to move off it.
97
- const map = new CoverageMap({ segmentCount: 100 });
98
- const runA = aRun();
99
- map.claim(runA, 10, 30);
100
- assert.equal(map.coveredRunFrom(10, runA), 0);
101
- });
102
-
103
- test("nothing free ahead answers null rather than a number past the end", () => {
104
- const map = new CoverageMap({ segmentCount: 5 });
105
- map.setReady([0, 1, 2, 3, 4]);
106
- assert.equal(map.firstGapFrom(0), null);
107
- });
108
-
109
- test("a file that has gone stops being ready", () => {
110
- // THE CHECK THE FIELD FAILURE OF 2026-09-07 NEEDED. Readiness used to
111
- // accumulate: a number told once stayed ready for the life of the process
112
- // however its file ended, so the map claimed a whole film that was not there,
113
- // the plan scored every arrangement as perfect and took the only encoder away.
114
- const map = new CoverageMap({ segmentCount: 10 });
115
- map.setReady([3, 4]);
116
- assert.equal(map.stateOf(4), "ready");
117
- map.setReady([3]);
118
- assert.equal(map.stateOf(4), "free", "the picture is replaced, not added to");
119
- assert.equal(map.stateOf(3), "ready", "and what is still there stays");
120
- });
121
-
122
- test("the whole picture is replaced, so nothing survives that the disk has lost", () => {
123
- const map = new CoverageMap({ segmentCount: 500 });
124
- for (let index = 0; index < 500; index += 1) {
125
- map.markReady(index);
126
- }
127
- assert.equal(map.stats().ready, 500);
128
- map.setReady([]);
129
- assert.equal(map.stats().ready, 0, "an empty directory is an empty map");
130
- assert.equal(map.firstGapFrom(0), 0, "and the first gap is the beginning again");
131
- });
132
-
133
- test("a run's own claim does not hide the gap it is trying to move into", () => {
134
- // Found by the plan's own checks: a run that had claimed the rest of the film
135
- // — which is what every run did before ends existed — could find no gap
136
- // anywhere, so it was stopped instead of moved.
137
- const map = new CoverageMap({ segmentCount: 100 });
138
- const runA = aRun();
139
- map.claim(runA, 0, 99);
140
- map.setReady([10, 11, 12]);
141
- assert.equal(map.firstGapFrom(10, 90), null, "to anybody else its ground is taken");
142
- assert.equal(map.firstGapFrom(10, 90, runA), 13, "to itself the ground beyond is a gap");
143
- });
144
-
145
- test("the free stretch ahead is measured, which is what gives a run its end", () => {
146
- // A run handed the whole rest of the film would drive through the stretch
147
- // another run is making. Handed the free stretch, it stops where the covered
148
- // material begins.
149
- const map = new CoverageMap({ segmentCount: 100 });
150
- const runB = aRun();
151
- map.claim(runB, 30, 60);
152
- assert.equal(map.freeRunFrom(10), 20, "free from 10 up to 29");
153
- map.markReady(15);
154
- assert.equal(map.freeRunFrom(10), 5, "a ready segment ends the free stretch too");
155
- assert.equal(map.freeRunFrom(15), 0, "a number that is not free has no free stretch");
156
- });
157
-
158
- test("a run's own claim does not end its own free stretch", () => {
159
- const map = new CoverageMap({ segmentCount: 100 });
160
- const runA = aRun();
161
- map.claim(runA, 10, 40);
162
- assert.equal(map.freeRunFrom(10, runA), 90, "its own ground is still its to fill");
163
- assert.equal(map.freeRunFrom(10), 0, "to anybody else it is taken");
164
- });
165
-
166
- test("with the length unknown, a gap search needs its own bound", () => {
167
- const map = new CoverageMap();
168
- assert.equal(map.firstGapFrom(0), null, "no length and no bound answers nothing");
169
- assert.equal(map.firstGapFrom(0, 3), 0);
170
- });
171
-
172
- test("with the length unknown, the free stretch is answered without walking to it", () => {
173
- // What this pins: on the addon host, 2026-09-05, a map with no length walked
174
- // one number at a time to MAX_SAFE_INTEGER, scanning every claim at each step.
175
- // The main thread spun at 100% from the look-ahead timer and the proxy stopped
176
- // answering anything, its own log included. The length was missing because the
177
- // field naming it had moved to the timeline and three readers were left on the
178
- // old name — but this walk must not be able to do that even when it is.
179
- //
180
- // This check cannot FAIL against the old code — a synchronous walk cannot be
181
- // interrupted by the runner, so it would hang the whole run instead, which is
182
- // worse than no check. What it pins is the contract that replaced the walk:
183
- // the answer comes from what the map holds.
184
- const map = new CoverageMap();
185
-
186
- assert.equal(map.freeRunFrom(0), Number.POSITIVE_INFINITY, "nothing is covered, so nothing bounds it");
187
-
188
- const run = aRun();
189
- map.claim(run, 500, 900);
190
- assert.equal(map.freeRunFrom(0), 500, "and a claim ahead is where the free stretch ends");
191
- assert.equal(map.freeRunFrom(0, run), Number.POSITIVE_INFINITY, "its own claim does not bound it");
192
-
193
- map.markReady(200);
194
- assert.equal(map.freeRunFrom(0), 200, "so is a segment somebody has already made");
195
- });
1
+ /**
2
+ * @file The three states of a segment number, and the two questions that decide
3
+ * where an encoder goes.
4
+ */
5
+
6
+ import test from "node:test";
7
+ import assert from "node:assert/strict";
8
+ import { CoverageMap } from "../services/encode/CoverageMap.js";
9
+
10
+ /**
11
+ * A stand-in for a run.
12
+ *
13
+ * The map files a claim under the run ITSELF: a run has no name, and needs
14
+ * none — what identifies it is that it is itself, and what identifies it in a
15
+ * line is the stretch it was given, which no other live run of one output can
16
+ * hold.
17
+ *
18
+ * @returns {object}
19
+ */
20
+ function aRun() {
21
+ return {};
22
+ }
23
+
24
+ test("a number nobody has made and nobody is making is free", () => {
25
+ const map = new CoverageMap({ segmentCount: 10 });
26
+ assert.equal(map.stateOf(3), "free");
27
+ assert.equal(map.firstGapFrom(0), 0);
28
+ });
29
+
30
+ test("a closed file is ready whoever made it", () => {
31
+ const map = new CoverageMap({ segmentCount: 10 });
32
+ map.markReady(0);
33
+ map.markReady(1);
34
+ assert.equal(map.stateOf(1), "ready");
35
+ assert.equal(map.firstGapFrom(0), 2);
36
+ });
37
+
38
+ test("a run claims a stretch, and the gap search skips it", () => {
39
+ // The claim is an interval and not a head, which is the whole reason two runs
40
+ // can work on one output: the second is sent past what the first will reach.
41
+ const map = new CoverageMap({ segmentCount: 100 });
42
+ const runA = aRun();
43
+ map.claim(runA, 0, 40);
44
+ assert.equal(map.stateOf(20), "making");
45
+ assert.equal(map.makerOf(20), runA);
46
+ assert.equal(map.firstGapFrom(0), 41);
47
+ });
48
+
49
+ test("a run that ends gives back what it did not finish, and keeps what it did", () => {
50
+ const map = new CoverageMap({ segmentCount: 100 });
51
+ const runA = aRun();
52
+ map.claim(runA, 0, 40);
53
+ map.markReady(0);
54
+ map.markReady(1);
55
+ map.release(runA);
56
+ assert.equal(map.stateOf(0), "ready", "a closed file survives its maker");
57
+ assert.equal(map.stateOf(2), "free", "the rest of the stretch is free again");
58
+ assert.equal(map.firstGapFrom(0), 2);
59
+ });
60
+
61
+ test("a run with no end claims everything ahead of it", () => {
62
+ // Which is what every run was before ends existed, and is why a second run
63
+ // could never be placed.
64
+ const map = new CoverageMap({ segmentCount: 100 });
65
+ const runA = aRun();
66
+ map.claim(runA, 10, Number.POSITIVE_INFINITY);
67
+ assert.equal(map.firstGapFrom(10), null);
68
+ assert.equal(map.firstGapFrom(0), 0, "behind it is still free");
69
+ });
70
+
71
+ test("claiming again replaces the earlier claim rather than adding to it", () => {
72
+ // A run moved forward past ready material states its new stretch; if the two
73
+ // accumulated, the ground it has left would stay unavailable for ever.
74
+ const map = new CoverageMap({ segmentCount: 100 });
75
+ const runA = aRun();
76
+ map.claim(runA, 0, 20);
77
+ map.claim(runA, 50, 70);
78
+ assert.equal(map.stateOf(10), "free");
79
+ assert.equal(map.stateOf(60), "making");
80
+ });
81
+
82
+ test("the covered stretch ahead is measured, which is what prices a move", () => {
83
+ const map = new CoverageMap({ segmentCount: 100 });
84
+ const runA = aRun();
85
+ const runB = aRun();
86
+ for (let index = 10; index <= 24; index += 1) {
87
+ map.markReady(index);
88
+ }
89
+ map.claim(runB, 25, 30);
90
+ // Ready 10..24 then another run's 25..30, so fifteen plus six.
91
+ assert.equal(map.coveredRunFrom(10, runA), 21);
92
+ assert.equal(map.firstGapFrom(10), 31);
93
+ });
94
+
95
+ test("a run's own claim is not counted as somebody else's coverage", () => {
96
+ // Otherwise a run would read its own stretch as a reason to move off it.
97
+ const map = new CoverageMap({ segmentCount: 100 });
98
+ const runA = aRun();
99
+ map.claim(runA, 10, 30);
100
+ assert.equal(map.coveredRunFrom(10, runA), 0);
101
+ });
102
+
103
+ test("nothing free ahead answers null rather than a number past the end", () => {
104
+ const map = new CoverageMap({ segmentCount: 5 });
105
+ map.setReady([0, 1, 2, 3, 4]);
106
+ assert.equal(map.firstGapFrom(0), null);
107
+ });
108
+
109
+ test("a file that has gone stops being ready", () => {
110
+ // THE CHECK THE FIELD FAILURE OF 2026-09-07 NEEDED. Readiness used to
111
+ // accumulate: a number told once stayed ready for the life of the process
112
+ // however its file ended, so the map claimed a whole film that was not there,
113
+ // the plan scored every arrangement as perfect and took the only encoder away.
114
+ const map = new CoverageMap({ segmentCount: 10 });
115
+ map.setReady([3, 4]);
116
+ assert.equal(map.stateOf(4), "ready");
117
+ map.setReady([3]);
118
+ assert.equal(map.stateOf(4), "free", "the picture is replaced, not added to");
119
+ assert.equal(map.stateOf(3), "ready", "and what is still there stays");
120
+ });
121
+
122
+ test("the whole picture is replaced, so nothing survives that the disk has lost", () => {
123
+ const map = new CoverageMap({ segmentCount: 500 });
124
+ for (let index = 0; index < 500; index += 1) {
125
+ map.markReady(index);
126
+ }
127
+ assert.equal(map.stats().ready, 500);
128
+ map.setReady([]);
129
+ assert.equal(map.stats().ready, 0, "an empty directory is an empty map");
130
+ assert.equal(map.firstGapFrom(0), 0, "and the first gap is the beginning again");
131
+ });
132
+
133
+ test("a run's own claim does not hide the gap it is trying to move into", () => {
134
+ // Found by the plan's own checks: a run that had claimed the rest of the film
135
+ // — which is what every run did before ends existed — could find no gap
136
+ // anywhere, so it was stopped instead of moved.
137
+ const map = new CoverageMap({ segmentCount: 100 });
138
+ const runA = aRun();
139
+ map.claim(runA, 0, 99);
140
+ map.setReady([10, 11, 12]);
141
+ assert.equal(map.firstGapFrom(10, 90), null, "to anybody else its ground is taken");
142
+ assert.equal(map.firstGapFrom(10, 90, runA), 13, "to itself the ground beyond is a gap");
143
+ });
144
+
145
+ test("the free stretch ahead is measured, which is what gives a run its end", () => {
146
+ // A run handed the whole rest of the film would drive through the stretch
147
+ // another run is making. Handed the free stretch, it stops where the covered
148
+ // material begins.
149
+ const map = new CoverageMap({ segmentCount: 100 });
150
+ const runB = aRun();
151
+ map.claim(runB, 30, 60);
152
+ assert.equal(map.freeRunFrom(10), 20, "free from 10 up to 29");
153
+ map.markReady(15);
154
+ assert.equal(map.freeRunFrom(10), 5, "a ready segment ends the free stretch too");
155
+ assert.equal(map.freeRunFrom(15), 0, "a number that is not free has no free stretch");
156
+ });
157
+
158
+ test("a run's own claim does not end its own free stretch", () => {
159
+ const map = new CoverageMap({ segmentCount: 100 });
160
+ const runA = aRun();
161
+ map.claim(runA, 10, 40);
162
+ assert.equal(map.freeRunFrom(10, runA), 90, "its own ground is still its to fill");
163
+ assert.equal(map.freeRunFrom(10), 0, "to anybody else it is taken");
164
+ });
165
+
166
+ test("with the length unknown, a gap search needs its own bound", () => {
167
+ const map = new CoverageMap();
168
+ assert.equal(map.firstGapFrom(0), null, "no length and no bound answers nothing");
169
+ assert.equal(map.firstGapFrom(0, 3), 0);
170
+ });
171
+
172
+ test("with the length unknown, the free stretch is answered without walking to it", () => {
173
+ // What this pins: on the addon host, 2026-09-05, a map with no length walked
174
+ // one number at a time to MAX_SAFE_INTEGER, scanning every claim at each step.
175
+ // The main thread spun at 100% from the look-ahead timer and the proxy stopped
176
+ // answering anything, its own log included. The length was missing because the
177
+ // field naming it had moved to the timeline and three readers were left on the
178
+ // old name — but this walk must not be able to do that even when it is.
179
+ //
180
+ // This check cannot FAIL against the old code — a synchronous walk cannot be
181
+ // interrupted by the runner, so it would hang the whole run instead, which is
182
+ // worse than no check. What it pins is the contract that replaced the walk:
183
+ // the answer comes from what the map holds.
184
+ const map = new CoverageMap();
185
+
186
+ assert.equal(map.freeRunFrom(0), Number.POSITIVE_INFINITY, "nothing is covered, so nothing bounds it");
187
+
188
+ const run = aRun();
189
+ map.claim(run, 500, 900);
190
+ assert.equal(map.freeRunFrom(0), 500, "and a claim ahead is where the free stretch ends");
191
+ assert.equal(map.freeRunFrom(0, run), Number.POSITIVE_INFINITY, "its own claim does not bound it");
192
+
193
+ map.markReady(200);
194
+ assert.equal(map.freeRunFrom(0), 200, "so is a segment somebody has already made");
195
+ });
@@ -1,35 +1,35 @@
1
- /**
2
- * @file What the proxy promises the browser must be answerable.
3
- *
4
- * The session manager asks the planner's media-info cache which tracks the
5
- * output will carry. That cache never held the codecs — it holds dimensions,
6
- * duration, fps, start time and an HDR flag — so the read produced `undefined`
7
- * for both and the declaration came out `{video: false, audio: false}` for
8
- * EVERY session since the check existed. Measured 2026-08-11 in the field.
9
- *
10
- * Nothing caught it: the writer and the reader each looked correct alone, and
11
- * no test ever compared the shape of what is stored with the shape of what is
12
- * read. That comparison is this file's whole job.
13
- */
14
-
15
- import test from "node:test";
16
- import assert from "node:assert/strict";
17
- import { readFileSync } from "node:fs";
18
-
19
- const planner = readFileSync(new URL("../services/playback-planner.js", import.meta.url), "utf8");
20
- const sessions = readFileSync(new URL("../services/hls-session-manager.js", import.meta.url), "utf8");
21
-
22
- test("every field the declaration reads is a field the cache stores", () => {
23
- // What `declaredTracks` reads off the cached media info.
24
- const read = [...sessions.matchAll(/probed\?\.([A-Za-z]+)/g)].map((match) => match[1]);
25
- assert.ok(read.length > 0, "the declaration must read something, or it declares nothing");
26
-
27
- const stored = planner.slice(planner.indexOf("mediaInfoCache.set("));
28
- for (const field of new Set(read)) {
29
- assert.ok(
30
- stored.includes(`${field}:`),
31
- `the declaration reads "${field}" and the media-info cache never stores it — ` +
32
- "the read yields undefined and the promise silently becomes false"
33
- );
34
- }
35
- });
1
+ /**
2
+ * @file What the proxy promises the browser must be answerable.
3
+ *
4
+ * The session manager asks the planner's media-info cache which tracks the
5
+ * output will carry. That cache never held the codecs — it holds dimensions,
6
+ * duration, fps, start time and an HDR flag — so the read produced `undefined`
7
+ * for both and the declaration came out `{video: false, audio: false}` for
8
+ * EVERY session since the check existed. Measured 2026-08-11 in the field.
9
+ *
10
+ * Nothing caught it: the writer and the reader each looked correct alone, and
11
+ * no test ever compared the shape of what is stored with the shape of what is
12
+ * read. That comparison is this file's whole job.
13
+ */
14
+
15
+ import test from "node:test";
16
+ import assert from "node:assert/strict";
17
+ import { readFileSync } from "node:fs";
18
+
19
+ const planner = readFileSync(new URL("../services/playback-planner.js", import.meta.url), "utf8");
20
+ const sessions = readFileSync(new URL("../services/hls-session-manager.js", import.meta.url), "utf8");
21
+
22
+ test("every field the declaration reads is a field the cache stores", () => {
23
+ // What `declaredTracks` reads off the cached media info.
24
+ const read = [...sessions.matchAll(/probed\?\.([A-Za-z]+)/g)].map((match) => match[1]);
25
+ assert.ok(read.length > 0, "the declaration must read something, or it declares nothing");
26
+
27
+ const stored = planner.slice(planner.indexOf("mediaInfoCache.set("));
28
+ for (const field of new Set(read)) {
29
+ assert.ok(
30
+ stored.includes(`${field}:`),
31
+ `the declaration reads "${field}" and the media-info cache never stores it — ` +
32
+ "the read yields undefined and the promise silently becomes false"
33
+ );
34
+ }
35
+ });
@@ -0,0 +1,138 @@
1
+ /**
2
+ * @file One disk, one owner, three claimants.
3
+ *
4
+ * What these pin is the thing that was wrong: each consumer read the free space
5
+ * as though it were alone, so three ceilings each stood for the whole disk.
6
+ */
7
+
8
+ import test from "node:test";
9
+ import assert from "node:assert/strict";
10
+ import { DiskSpace } from "../services/disk/DiskSpace.js";
11
+
12
+ const MEGABYTE = 1024 * 1024;
13
+
14
+ /**
15
+ * @param {string} name
16
+ * @param {number} held
17
+ * @param {number} wanted
18
+ */
19
+ function consumerOf(name, held, wanted) {
20
+ const state = { allowed: -1 };
21
+ return {
22
+ state,
23
+ consumer: {
24
+ name,
25
+ held: () => held,
26
+ wanted: () => wanted,
27
+ allow: (bytes) => {
28
+ state.allowed = bytes;
29
+ }
30
+ }
31
+ };
32
+ }
33
+
34
+ test("what everyone may hold together never exceeds what the disk has", async () => {
35
+ const free = 100 * MEGABYTE;
36
+ const space = new DiskSpace({ readFree: async () => free });
37
+ const segments = consumerOf("segments", 10 * MEGABYTE, 1000 * MEGABYTE);
38
+ const pieces = consumerOf("pieces", 20 * MEGABYTE, 1000 * MEGABYTE);
39
+ const diagnostics = consumerOf("diagnostics", 5 * MEGABYTE, 1000 * MEGABYTE);
40
+ space.register(segments.consumer);
41
+ space.register(pieces.consumer);
42
+ space.register(diagnostics.consumer);
43
+
44
+ const { allowanceBytes } = await space.revise();
45
+
46
+ const together = segments.state.allowed + pieces.state.allowed + diagnostics.state.allowed;
47
+ assert.ok(together <= allowanceBytes, "the shares add up to more than the allowance");
48
+ // Free space plus what they already hold: that pair is the ceiling they could
49
+ // reach, which is the same statement the memory allowance makes.
50
+ assert.equal(allowanceBytes, free + 35 * MEGABYTE);
51
+ assert.ok(
52
+ together <= free + 35 * MEGABYTE,
53
+ "three consumers were between them allowed more than the disk has"
54
+ );
55
+ });
56
+
57
+ test("when everyone's ask fits, everyone gets it and the disk never binds", async () => {
58
+ const space = new DiskSpace({ readFree: async () => 1000 * MEGABYTE });
59
+ const small = consumerOf("diagnostics", 0, 5 * MEGABYTE);
60
+ const large = consumerOf("segments", 0, 500 * MEGABYTE);
61
+ space.register(small.consumer);
62
+ space.register(large.consumer);
63
+
64
+ await space.revise();
65
+
66
+ assert.equal(small.state.allowed, 5 * MEGABYTE);
67
+ assert.equal(large.state.allowed, 500 * MEGABYTE);
68
+ });
69
+
70
+ test("when they do not fit, each is cut in proportion to what it asked", async () => {
71
+ // The same rule memory divides by, and the same reason: a share taken from
72
+ // whoever asked most would hand the disk to whoever grew fastest.
73
+ const space = new DiskSpace({ readFree: async () => 200 * MEGABYTE });
74
+ const modest = consumerOf("diagnostics", 0, 100 * MEGABYTE);
75
+ const greedy = consumerOf("segments", 0, 300 * MEGABYTE);
76
+ space.register(modest.consumer);
77
+ space.register(greedy.consumer);
78
+
79
+ await space.revise();
80
+
81
+ assert.equal(modest.state.allowed, 50 * MEGABYTE);
82
+ assert.equal(greedy.state.allowed, 150 * MEGABYTE);
83
+ assert.equal(modest.state.allowed + greedy.state.allowed, 200 * MEGABYTE);
84
+ });
85
+
86
+ test("a disk that fills lowers every share, rather than keeping one taken when it was empty", async () => {
87
+ let free = 1000 * MEGABYTE;
88
+ const space = new DiskSpace({ readFree: async () => free });
89
+ const segments = consumerOf("segments", 0, 10_000 * MEGABYTE);
90
+ const pieces = consumerOf("pieces", 0, 10_000 * MEGABYTE);
91
+ space.register(segments.consumer);
92
+ space.register(pieces.consumer);
93
+
94
+ await space.revise();
95
+ const roomy = segments.state.allowed;
96
+
97
+ free = 40 * MEGABYTE;
98
+ await space.revise();
99
+
100
+ assert.ok(segments.state.allowed < roomy, "the share did not follow the disk down");
101
+ assert.ok(pieces.state.allowed < roomy, "the other share did not follow either");
102
+ });
103
+
104
+ test("what another process took is left for it, not spent", async () => {
105
+ // Between two readings the disk lost 400 MB that none of our consumers took.
106
+ // That is a measurement of somebody else's demand, and it is what the next
107
+ // allowance leaves alone.
108
+ let free = 1000 * MEGABYTE;
109
+ const space = new DiskSpace({ readFree: async () => free });
110
+ const only = consumerOf("segments", 0, 10_000 * MEGABYTE);
111
+ space.register(only.consumer);
112
+
113
+ await space.revise();
114
+ free = 600 * MEGABYTE;
115
+ const { allowanceBytes } = await space.revise();
116
+
117
+ assert.equal(allowanceBytes, 200 * MEGABYTE, "the fall we did not cause was spent anyway");
118
+ });
119
+
120
+ test("a disk that cannot be read allows nothing to grow", async () => {
121
+ const space = new DiskSpace({ readFree: async () => null });
122
+ const segments = consumerOf("segments", 0, 500 * MEGABYTE);
123
+ space.register(segments.consumer);
124
+
125
+ const { freeBytes, allowanceBytes } = await space.revise();
126
+
127
+ assert.equal(freeBytes, 0);
128
+ assert.equal(allowanceBytes, 0);
129
+ assert.equal(segments.state.allowed, 0, "an unreadable disk was treated as an empty one");
130
+ });
131
+
132
+ test("it says what the disk has and what each claimant may hold", async () => {
133
+ const space = new DiskSpace({ readFree: async () => 100 * MEGABYTE });
134
+ assert.match(space.describe(), /nothing has claimed/);
135
+ space.register(consumerOf("segments", 4 * MEGABYTE, 8 * MEGABYTE).consumer);
136
+ await space.revise();
137
+ assert.match(space.describe(), /disk: 100MB free; segments 4MB of 8MB/);
138
+ });
@@ -172,7 +172,6 @@ test("an encoder already working covers what it will reach in time", () => {
172
172
  // wanted at all.
173
173
  const { made, buildRun } = orchestrator();
174
174
  const first = buildRun({ from: 0, to: -1 });
175
- first.start("a viewer needs it");
176
175
  first.noteSpeed(8);
177
176
  made.adopt(PICTURE, first);
178
177
  made.noteProduced(PICTURE, 0);
@@ -203,7 +202,6 @@ test("somebody stopped where no encoder can arrive in time is served, and the sc
203
202
  // seconds anybody spends looking at a spinner — outranks it.
204
203
  const { made, buildRun } = orchestrator();
205
204
  const first = buildRun({ from: 0, to: -1 });
206
- first.start("a viewer needs it");
207
205
  first.noteSpeed(1);
208
206
  made.adopt(PICTURE, first);
209
207
  made.noteProduced(PICTURE, 0);
@@ -224,7 +222,6 @@ test("two encoders on one output never share a segment number", () => {
224
222
  // what that neighbour makes sooner.
225
223
  const { made, buildRun } = orchestrator();
226
224
  const first = buildRun({ from: 0, to: -1 });
227
- first.start("a viewer needs it");
228
225
  first.noteSpeed(1);
229
226
  made.adopt(PICTURE, first);
230
227
  made.noteProduced(PICTURE, 0);