@torrent-tv/proxy 2.83.3 → 2.83.5

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 (39) hide show
  1. package/CHANGELOG.md +32 -0
  2. package/package.json +1 -1
  3. package/research/handover-reader-claims-removal-2026-09-12.md +166 -0
  4. package/research/piece-withdrawn-but-still-claimed-2026-09-12.md +175 -0
  5. package/research/priority-map-is-the-truth-2026-09-12.md +225 -0
  6. package/routes/api/sources/files/get.js +23 -9
  7. package/routes/api/sources/warm/post.js +26 -22
  8. package/routes/api/transcode-sessions/post.js +1 -49
  9. package/routes/stream/get.js +2 -36
  10. package/services/controllers/SubtitleController.js +0 -3
  11. package/services/download/withdraw-claim.js +80 -0
  12. package/services/hls-session-manager.js +17 -110
  13. package/services/orchestrators/EncodeOrchestrator.js +133 -0
  14. package/services/piece-store/piece-disk-store.js +24 -1
  15. package/services/piece-store/shared-piece-store.js +71 -1
  16. package/services/playback-planner.js +12 -13
  17. package/services/priority/PriorityOrchestrator.js +40 -6
  18. package/services/torrent/Contents.js +324 -0
  19. package/services/torrent/files.js +8 -1
  20. package/services/torrent-pool.js +378 -102
  21. package/services/torrent-worker/client.js +0 -24
  22. package/services/torrent-worker/piece-reader.js +30 -1
  23. package/services/torrent-worker/pool-adapter.js +3 -33
  24. package/services/torrent-worker/protocol.js +0 -4
  25. package/services/torrent-worker/worker.js +60 -60
  26. package/test/file-edges.test.js +191 -0
  27. package/test/input-lost-quiets-the-plan.test.js +261 -0
  28. package/test/logger-repeats.test.js +120 -0
  29. package/test/priority-map-emptied.test.js +207 -0
  30. package/test/read-survives-withdrawal.test.js +164 -0
  31. package/test/source-files-route.test.js +103 -0
  32. package/test/stream-route.test.js +4 -8
  33. package/test/swarm-follows-readers.test.js +96 -14
  34. package/test/torrent-contents.test.js +235 -0
  35. package/test/upload-hurry.test.js +66 -28
  36. package/test/withdraw-piece-claim.test.js +212 -0
  37. package/utils/logger.js +105 -7
  38. package/services/torrent-worker/file-claims.js +0 -91
  39. package/test/file-claims.test.js +0 -64
@@ -0,0 +1,261 @@
1
+ /**
2
+ * @file An output whose input is away gets no encoders until it may.
3
+ *
4
+ * A run whose input has gone is not alive, so the plan reads the stretch it held
5
+ * as free and places another run there at once — which dies the same way,
6
+ * because nothing about the state has changed. A delay existed for exactly this,
7
+ * doubling from 2 s to 15 s, and it was timed against the DEAD RUN, which the
8
+ * plan never consults. Field 2026-09-12: 2432 ffmpeg starts in 23 minutes, one
9
+ * every 0.57 s, for 61 minutes, against a delay that had reached its ceiling
10
+ * long before; the flood also turned the log over twice and destroyed the
11
+ * record of how the failure began.
12
+ *
13
+ * The delay lives beside the decision it governs now, and these check that it
14
+ * binds.
15
+ */
16
+
17
+ import test from "node:test";
18
+ import assert from "node:assert/strict";
19
+ import { EventEmitter } from "node:events";
20
+ import { EncodeRun } from "../services/encode/EncodeRun.js";
21
+ import { ENCODE_EXIT } from "../services/encode/encode-exit.js";
22
+ import { SoftwareEncoder } from "../services/encode/SoftwareEncoder.js";
23
+ import { EncodeOrchestrator } from "../services/orchestrators/EncodeOrchestrator.js";
24
+
25
+ const PICTURE = "torrent:abc:fmt=fmp4:grid=kf@0:video-only:v=0/copy";
26
+
27
+ class FakeProcess extends EventEmitter {
28
+ constructor() {
29
+ super();
30
+ this.pid = 1;
31
+ }
32
+
33
+ kill(signal) {
34
+ this.emit("exit", null, signal);
35
+ }
36
+ }
37
+
38
+ /** An orchestrator whose clock the test moves, and a count of what it started. */
39
+ function orchestrator() {
40
+ const lines = [];
41
+ let clock = 1_000;
42
+ let started = 0;
43
+ let asked = 0;
44
+ /** @type {EncodeOrchestrator} */
45
+ let made;
46
+ /** @type {{ run: EncodeRun, process: FakeProcess }[]} */
47
+ const runs = [];
48
+ made = new EncodeOrchestrator({
49
+ maxRunsFor: () => 1,
50
+ segmentSeconds: 4,
51
+ startingSpeedFor: () => 2,
52
+ refetchSecPerFilmSecond: () => 0.25,
53
+ now: () => clock,
54
+ planSoon: () => { asked += 1; },
55
+ logger: { info: (line) => lines.push(line), warn: (line) => lines.push(line) },
56
+ makeRun: ({ address, from, to }) => {
57
+ started += 1;
58
+ const process_ = new FakeProcess();
59
+ const run = new EncodeRun({
60
+ address,
61
+ encoder: new SoftwareEncoder(),
62
+ from,
63
+ to,
64
+ buildArgs: () => ["-i", "in", "out"],
65
+ spawn: () => process_,
66
+ logger: { info: (line) => lines.push(line), warn: (line) => lines.push(line) },
67
+ now: () => clock,
68
+ onEnded: (ended) => made.noteEnded(ended)
69
+ });
70
+ runs.push({ run, process: process_ });
71
+ return run;
72
+ }
73
+ });
74
+ made.setSegmentCount(PICTURE, 1000);
75
+ made.noteStartupCosts({ killCostSec: 0, firstByteWaitSec: 0.12 });
76
+
77
+ const wants = () => made.notePriorityMap(PICTURE, [
78
+ { from: 0, to: 20, priority: 1, withinSeconds: 0 }
79
+ ]);
80
+
81
+ return {
82
+ made,
83
+ lines,
84
+ wants,
85
+ runs,
86
+ startedCount: () => started,
87
+ askedToPlanAgain: () => asked,
88
+ advance: (ms) => { clock += ms; },
89
+ /** End the newest run the way a torrent going away ends one. */
90
+ loseTheInput: () => {
91
+ const newest = runs[runs.length - 1];
92
+ made.noteEnded({
93
+ address: PICTURE,
94
+ run: newest.run,
95
+ from: newest.run.from,
96
+ to: newest.run.to,
97
+ ending: ENCODE_EXIT.INPUT_LOST,
98
+ because: "the torrent went away"
99
+ });
100
+ }
101
+ };
102
+ }
103
+
104
+ test("nothing is placed on an output whose input has just gone", () => {
105
+ const stand = orchestrator();
106
+ stand.wants();
107
+ stand.made.reconcile();
108
+ assert.equal(stand.startedCount(), 1, "one encoder for the one thing wanted");
109
+
110
+ stand.loseTheInput();
111
+ // The plan is asked again by every event there is — a request, a report, a
112
+ // piece — and in the field that was about twice a second.
113
+ for (let attempt = 0; attempt < 20; attempt += 1) {
114
+ stand.made.reconcile();
115
+ }
116
+ assert.equal(
117
+ stand.startedCount(),
118
+ 1,
119
+ "twenty decisions while the input is away must not be twenty processes"
120
+ );
121
+ });
122
+
123
+ test("once the wait is over the plan places again", () => {
124
+ const stand = orchestrator();
125
+ stand.wants();
126
+ stand.made.reconcile();
127
+ stand.loseTheInput();
128
+ stand.made.reconcile();
129
+ assert.equal(stand.startedCount(), 1);
130
+
131
+ // The first wait is the base delay; anything past it lets the plan act.
132
+ stand.advance(2_001);
133
+ stand.made.reconcile();
134
+ assert.equal(stand.startedCount(), 2, "the data may be back, and that is worth one attempt");
135
+ });
136
+
137
+ test("the wait doubles while the input stays away, and is capped", () => {
138
+ const stand = orchestrator();
139
+ stand.wants();
140
+ stand.made.reconcile();
141
+
142
+ const waits = [];
143
+ for (let attempt = 0; attempt < 8; attempt += 1) {
144
+ stand.loseTheInput();
145
+ // Find the shortest advance that lets the plan act again, by stepping to
146
+ // just before and just after the boundary rather than reading a private.
147
+ let waited = 0;
148
+ const step = 250;
149
+ for (;;) {
150
+ const before = stand.startedCount();
151
+ stand.made.reconcile();
152
+ if (stand.startedCount() > before) {
153
+ break;
154
+ }
155
+ stand.advance(step);
156
+ waited += step;
157
+ assert.ok(waited < 60_000, "a wait must not be unbounded");
158
+ }
159
+ waits.push(waited);
160
+ }
161
+
162
+ for (let at = 1; at < waits.length; at += 1) {
163
+ assert.ok(
164
+ waits[at] >= waits[at - 1],
165
+ `the wait must not shrink while the input stays away: ${JSON.stringify(waits)}`
166
+ );
167
+ }
168
+ assert.ok(
169
+ waits[waits.length - 1] <= 15_000,
170
+ `and it must stop growing: ${JSON.stringify(waits)}`
171
+ );
172
+ assert.ok(
173
+ waits[waits.length - 1] > waits[0],
174
+ `and it must actually have grown: ${JSON.stringify(waits)}`
175
+ );
176
+ });
177
+
178
+ test("a wake-up is asked for, because no event arrives while the data is away", async () => {
179
+ const stand = orchestrator();
180
+ stand.wants();
181
+ stand.made.reconcile();
182
+ stand.loseTheInput();
183
+ // The wake-up runs on the real clock — the orchestrator's injected `now` is
184
+ // what the DECISION reads, and a timer is not a decision. So this waits for
185
+ // the condition, with a deadline only as a backstop; a fixed pause here would
186
+ // measure the machine.
187
+ const deadline = Date.now() + 10_000;
188
+ while (stand.askedToPlanAgain() === 0) {
189
+ if (Date.now() > deadline) {
190
+ assert.fail("nothing about the state changes while the input is missing, so the plan must be recalled");
191
+ }
192
+ await new Promise((resolve) => setTimeout(resolve, 25));
193
+ }
194
+ assert.ok(stand.askedToPlanAgain() >= 1);
195
+ });
196
+
197
+ test("an ending that is not about the input clears the wait", () => {
198
+ const stand = orchestrator();
199
+ stand.wants();
200
+ stand.made.reconcile();
201
+ stand.loseTheInput();
202
+ stand.made.reconcile();
203
+ assert.equal(stand.startedCount(), 1, "held back, as it should be");
204
+
205
+ stand.advance(2_001);
206
+ stand.made.reconcile();
207
+ assert.equal(stand.startedCount(), 2);
208
+
209
+ // This one PRODUCED and was then stopped: a segment came out of the input, so
210
+ // the input was plainly there and the next attempt starts from no delay.
211
+ const newest = stand.runs[stand.runs.length - 1];
212
+ stand.made.noteEnded({
213
+ address: PICTURE,
214
+ run: newest.run,
215
+ from: newest.run.from,
216
+ to: newest.run.to,
217
+ reached: newest.run.from,
218
+ firstOutputMs: 120,
219
+ ending: ENCODE_EXIT.STOPPED,
220
+ because: "we asked it to"
221
+ });
222
+ stand.made.reconcile();
223
+ assert.equal(stand.startedCount(), 3, "a file that has just produced is not suspect");
224
+ });
225
+
226
+ test("an ending that produced NOTHING does not lift the wait", () => {
227
+ const stand = orchestrator();
228
+ stand.wants();
229
+ stand.made.reconcile();
230
+ stand.loseTheInput();
231
+
232
+ // At the moment of failure several runs end at once. One of them ending
233
+ // without having made anything proves nothing about the input, and lifting
234
+ // the wait on it is the storm again with an extra step.
235
+ const newest = stand.runs[stand.runs.length - 1];
236
+ stand.made.noteEnded({
237
+ address: PICTURE,
238
+ run: newest.run,
239
+ from: newest.run.from,
240
+ to: newest.run.to,
241
+ reached: newest.run.from - 1,
242
+ firstOutputMs: null,
243
+ ending: ENCODE_EXIT.GONE,
244
+ because: "it is no longer running, and it did not say so"
245
+ });
246
+ for (let attempt = 0; attempt < 10; attempt += 1) {
247
+ stand.made.reconcile();
248
+ }
249
+ assert.equal(stand.startedCount(), 1, "an ending with nothing produced is no evidence");
250
+ });
251
+
252
+ test("the wait is said out loud, with the attempt and how long", () => {
253
+ const stand = orchestrator();
254
+ stand.wants();
255
+ stand.made.reconcile();
256
+ stand.loseTheInput();
257
+ assert.ok(
258
+ stand.lines.some((line) => /its input was not there \(attempt 1\)/.test(line)),
259
+ `the reason nothing is being placed must be readable: ${JSON.stringify(stand.lines.slice(-4))}`
260
+ );
261
+ });
@@ -0,0 +1,120 @@
1
+ /**
2
+ * @file An established fact is said once, then with decreasing frequency.
3
+ *
4
+ * Field 2026-09-12: one absent piece produced 235 000 lines in 92 minutes —
5
+ * about 55 a second, 68.8 % of them byte-identical repeats — and it turned the
6
+ * log file over twice, so the beginning of the failure was gone before anybody
7
+ * read it. A log is not spoilt by its size; it is spoilt by uniformity.
8
+ *
9
+ * Matched verbatim, the whole line: that catches nearly all of the flood and
10
+ * cannot merge two different statements. Normalising numbers would catch a
11
+ * little more and would also merge the memory series, which exists precisely to
12
+ * catch a runaway.
13
+ */
14
+
15
+ import test from "node:test";
16
+ import assert from "node:assert/strict";
17
+ import { logger } from "../utils/logger.js";
18
+
19
+ /** What reached the console while `body` ran. */
20
+ function captured(body) {
21
+ const lines = [];
22
+ const real = { log: console.log, warn: console.warn, error: console.error };
23
+ console.log = (line) => lines.push(String(line));
24
+ console.warn = (line) => lines.push(String(line));
25
+ console.error = (line) => lines.push(String(line));
26
+ try {
27
+ body();
28
+ } finally {
29
+ console.log = real.log;
30
+ console.warn = real.warn;
31
+ console.error = real.error;
32
+ }
33
+ return lines;
34
+ }
35
+
36
+ /** A line no other test in this process will have said. */
37
+ function unique(what) {
38
+ return `logger-repeats ${what} ${Math.random().toString(36).slice(2)}`;
39
+ }
40
+
41
+ test("the first time is said, and the repeats right behind it are not", () => {
42
+ const message = unique("flood");
43
+ const lines = captured(() => {
44
+ for (let at = 0; at < 500; at += 1) {
45
+ logger.info(message);
46
+ }
47
+ });
48
+ assert.equal(lines.length, 1, "five hundred identical lines are one fact");
49
+ assert.ok(lines[0].includes(message));
50
+ });
51
+
52
+ test("two different lines are both said — repeats are matched whole", () => {
53
+ const one = unique("one");
54
+ const other = unique("other");
55
+ const lines = captured(() => {
56
+ logger.info(one);
57
+ logger.info(other);
58
+ logger.info(one);
59
+ logger.info(other);
60
+ });
61
+ assert.equal(lines.length, 2, "different statements never merge");
62
+ assert.ok(lines[0].includes(one));
63
+ assert.ok(lines[1].includes(other));
64
+ });
65
+
66
+ test("a line whose numbers differ is a different line", () => {
67
+ const stem = unique("rss");
68
+ const lines = captured(() => {
69
+ logger.info(`${stem} rss=327MB`);
70
+ logger.info(`${stem} rss=726MB`);
71
+ logger.info(`${stem} rss=4616MB`);
72
+ });
73
+ assert.equal(
74
+ lines.length,
75
+ 3,
76
+ "the memory series exists to catch a runaway; suppressing it would be worse than the flood"
77
+ );
78
+ });
79
+
80
+ test("when it is said again, it says how many were held back", async () => {
81
+ const message = unique("counted");
82
+ const first = captured(() => {
83
+ logger.info(message);
84
+ for (let at = 0; at < 9; at += 1) {
85
+ logger.info(message);
86
+ }
87
+ });
88
+ assert.equal(first.length, 1);
89
+
90
+ // The first interval is a second. Waited for rather than assumed: this asks
91
+ // the logger itself when it is ready to speak again, with a deadline as a
92
+ // backstop.
93
+ const deadline = Date.now() + 10_000;
94
+ let later = [];
95
+ while (later.length === 0) {
96
+ if (Date.now() > deadline) {
97
+ assert.fail("the line should be said again once its interval has passed");
98
+ }
99
+ await new Promise((resolve) => setTimeout(resolve, 100));
100
+ later = captured(() => logger.info(message));
101
+ }
102
+ const said = later[0].match(/\[said (\d+) more time\(s\) in the last ([0-9.]+)s\]/);
103
+ assert.ok(said, `the rate is the fact here, and must be stated: ${later[0]}`);
104
+ // At least the nine held back above. Each poll that found nothing said the
105
+ // line again and was itself held back, so the exact figure belongs to how
106
+ // often this test asked — the property being pinned is that the held-back
107
+ // count is reported at all, and that it counts every one of them.
108
+ assert.ok(Number(said[1]) >= 9, `held-back count too low: ${later[0]}`);
109
+ assert.ok(Number(said[2]) > 0, `the span it covers must be stated: ${later[0]}`);
110
+ });
111
+
112
+ test("every level goes through the same rule", () => {
113
+ const message = unique("levels");
114
+ const lines = captured(() => {
115
+ logger.warn(message);
116
+ logger.warn(message);
117
+ logger.error(message);
118
+ });
119
+ assert.equal(lines.length, 1, "a flood of errors is still a flood");
120
+ });
@@ -0,0 +1,207 @@
1
+ /**
2
+ * @file WHEN NOBODY WANTS A FILE ANY MORE, THE MAP SAYS SO.
3
+ *
4
+ * The map is the only statement of what is wanted, and until 2.83.4 it could
5
+ * make only one of the two statements it exists for. A file whose viewers had
6
+ * gone was deleted from the orchestrator's own memory and published nowhere, so
7
+ * the bands it had stated on the swarm's behalf stood until the torrent itself
8
+ * was removed — read from the demand register, a film nobody had watched for an
9
+ * hour was indistinguishable from one being watched now.
10
+ *
11
+ * Two halves, and both are here: the side that builds the map has to say it,
12
+ * and the side that receives it has to act on it without needing the file's
13
+ * length, the torrent's list, or the torrent to exist at all.
14
+ */
15
+
16
+ import test from "node:test";
17
+ import assert from "node:assert/strict";
18
+ import { PriorityOrchestrator } from "../services/priority/PriorityOrchestrator.js";
19
+ import { Viewers } from "../services/viewer/Viewers.js";
20
+ import { viewersOf } from "../services/viewer/Viewer.js";
21
+ import { TorrentPool } from "../services/torrent-pool.js";
22
+ import { demandFor, forgetTorrent } from "../services/download/registry.js";
23
+ import { Urgency } from "../services/demand/index.js";
24
+
25
+ const STALE_AFTER_MS = 60_000;
26
+
27
+ /**
28
+ * A session, as much of one as the orchestrator reads.
29
+ *
30
+ * @param {{ id: string, fileIndex?: number }} params
31
+ * @returns {object}
32
+ */
33
+ function outputOf({ id, fileIndex = 0 }) {
34
+ return {
35
+ id,
36
+ outputKey: `out:${id}`,
37
+ sourceKey: "source-1",
38
+ fileIndex,
39
+ file: { key: `film-${fileIndex}`, durationSeconds: 600 }
40
+ };
41
+ }
42
+
43
+ /**
44
+ * The real orchestrator, with everything it publishes kept.
45
+ *
46
+ * @param {object[]} sessions
47
+ * @returns {{ priority: PriorityOrchestrator, viewers: Viewers, published: object[], publish: (sessions?: object[]) => void }}
48
+ */
49
+ function over(sessions) {
50
+ const published = [];
51
+ const viewers = new Viewers();
52
+ const priority = new PriorityOrchestrator({
53
+ publish: (one) => published.push(one),
54
+ viewersOf: (session) => viewersOf(session),
55
+ allowanceFor: () => 10
56
+ });
57
+ return {
58
+ priority,
59
+ viewers,
60
+ published,
61
+ publish: (live = sessions) =>
62
+ priority.publishFor({ sessionGroups: [live], staleAfterMs: STALE_AFTER_MS })
63
+ };
64
+ }
65
+
66
+ test("a file whose viewers have all gone is published as wanting nothing", () => {
67
+ const picture = outputOf({ id: "pic" });
68
+ const { viewers, published, publish } = over([picture]);
69
+ const person = viewers.of(picture, "p");
70
+ person.moveTo(300);
71
+ publish();
72
+ assert.ok(published.at(-1).zones.length > 0, "somebody is watching, so something is wanted");
73
+
74
+ // They stop answering. The session is still there — it outlives them by half
75
+ // an hour — so this is the case that used to say nothing at all.
76
+ person.seen(Date.now() - STALE_AFTER_MS * 2);
77
+ publish();
78
+
79
+ const last = published.at(-1);
80
+ assert.deepEqual(last.zones, []);
81
+ assert.equal(last.sourceKey, "source-1");
82
+ assert.equal(last.fileIndex, 0);
83
+ });
84
+
85
+ test("a file whose session has gone is published as wanting nothing, once", () => {
86
+ const picture = outputOf({ id: "pic" });
87
+ const { viewers, published, publish } = over([picture]);
88
+ viewers.of(picture, "p").moveTo(300);
89
+ publish();
90
+ const said = published.length;
91
+
92
+ // The session is disposed: it is not among the live ones any more.
93
+ publish([]);
94
+ assert.deepEqual(published.at(-1).zones, []);
95
+ assert.equal(published.length, said + 1);
96
+
97
+ // And it is not repeated on every pass afterwards — the file is gone from
98
+ // this class's memory, and a departure is said once.
99
+ publish([]);
100
+ publish([]);
101
+ assert.equal(published.length, said + 1);
102
+ });
103
+
104
+ test("an unchanged map is still not republished", () => {
105
+ const picture = outputOf({ id: "pic" });
106
+ const { viewers, published, publish } = over([picture]);
107
+ viewers.of(picture, "p").moveTo(300);
108
+ publish();
109
+ const said = published.length;
110
+ publish();
111
+ publish();
112
+ assert.equal(published.length, said, "the downloading rebuilds its requests on every one");
113
+ });
114
+
115
+ /** A torrent that is nothing but one file of a known length. */
116
+ function torrentOf({ length = 1_000_000 } = {}) {
117
+ return {
118
+ infoHash: `hash-${Math.random().toString(36).slice(2)}`,
119
+ pieceLength: 1024,
120
+ files: [{ offset: 0, length, name: "film.mkv" }],
121
+ _selections: { _items: [] },
122
+ _select() {},
123
+ _deselect() {},
124
+ critical() {}
125
+ };
126
+ }
127
+
128
+ const applyPriorityMap = TorrentPool.prototype.applyPriorityMap;
129
+
130
+ /**
131
+ * @param {object} torrent
132
+ * @returns {object[]}
133
+ */
134
+ function stated(torrent) {
135
+ return demandFor(torrent)
136
+ .register.windows()
137
+ .filter((one) => String(one.claimant).startsWith("priority-map:"));
138
+ }
139
+
140
+ test("a map with nothing in it withdraws everything that file had stated", () => {
141
+ const torrent = torrentOf();
142
+ try {
143
+ applyPriorityMap.call(null, torrent, 0, [
144
+ { from: 0, to: 100, priority: 100 },
145
+ { from: 100, to: 600, priority: 50 }
146
+ ], 600);
147
+ assert.equal(stated(torrent).length, 2);
148
+
149
+ applyPriorityMap.call(null, torrent, 0, [], 600);
150
+ assert.equal(stated(torrent).length, 0);
151
+ } finally {
152
+ forgetTorrent(torrent);
153
+ }
154
+ });
155
+
156
+ test("it withdraws that file's bands and nobody else's", () => {
157
+ const torrent = torrentOf();
158
+ try {
159
+ const { register } = demandFor(torrent);
160
+ applyPriorityMap.call(null, torrent, 0, [{ from: 0, to: 600, priority: 100 }], 600);
161
+ // A read stopped on a piece right now, which only a read can say, and the
162
+ // background fill, which the pool states for itself. Neither is the map's
163
+ // to withdraw.
164
+ register.state({
165
+ claimant: "read-7:blocked",
166
+ fileIndex: 0,
167
+ byteStart: 0,
168
+ byteEnd: 999,
169
+ urgency: Urgency.BLOCKED
170
+ });
171
+ register.state({
172
+ claimant: "background-fill:0",
173
+ fileIndex: 0,
174
+ byteStart: 1000,
175
+ byteEnd: 9999,
176
+ urgency: Urgency.TAIL
177
+ });
178
+
179
+ applyPriorityMap.call(null, torrent, 0, [], 600);
180
+
181
+ assert.equal(stated(torrent).length, 0);
182
+ assert.deepEqual(
183
+ register.windows().map((one) => one.claimant).sort(),
184
+ ["background-fill:0", "read-7:blocked"]
185
+ );
186
+ } finally {
187
+ forgetTorrent(torrent);
188
+ }
189
+ });
190
+
191
+ test("a departure is answered even when nothing else about the file is known", () => {
192
+ // The length, the duration and the torrent's own list are what the ordinary
193
+ // path needs to turn seconds into bytes. A departure needs none of them, and
194
+ // it arrives exactly when they are going away: the map that says it is
195
+ // published as the last viewer leaves.
196
+ const torrent = torrentOf();
197
+ try {
198
+ applyPriorityMap.call(null, torrent, 0, [{ from: 0, to: 600, priority: 100 }], 600);
199
+ assert.equal(stated(torrent).length, 1);
200
+
201
+ torrent.files = [];
202
+ applyPriorityMap.call(null, torrent, 0, [], 0);
203
+ assert.equal(stated(torrent).length, 0);
204
+ } finally {
205
+ forgetTorrent(torrent);
206
+ }
207
+ });