Files
bitvid/tests/multi-webseed.test.mjs
T
thePR0M3TH3ANandClaude Opus 4.8 cf98d06cca feat(webseed): carry multiple web seeds through publish (slice 1/3)
Foundation for declaring more than one web seed per video so the WebTorrent
swarm can pull pieces from an independent backup origin when the primary CDN
is overloaded — multiple `ws=` params are the standard BEP19 carrier and the
playback path already attaches every webseed it finds in the magnet.

- magnetShared: add extractAllWebSeeds(), which returns every `ws=` URL
  (deduped, in order) instead of only the first like extractMagnetHints —
  so a magnet with several webseeds can be round-tripped without dropping the
  extras.
- videoNotePayload: normalizeWebSeedList() accepts the ws field as an array OR
  a newline/comma-delimited string and feeds the full, deduped set into the
  magnet (plus the hosted CDN url, as before). A single ws string still works
  unchanged; the content `ws` field still reflects the primary seed.

Upload/Edit UI to author and manage the list lands in slices 2 and 3.

Mutation-verified (array-drop, split-drop, first-only, dedupe-loss all fail
the new tests); build + full unit suite green.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-06-19 08:21:18 -04:00

98 lines
3.4 KiB
JavaScript

// A video may legitimately declare more than one web seed (e.g. the primary CDN
// plus an independent backup origin) so the WebTorrent swarm can pull pieces
// from either when one is overloaded. Multiple `ws=` params are the standard
// BEP19 carrier and playback already attaches every one of them, so the
// authoring/normalization layer must preserve the full set — never collapse it
// to a single seed.
import test from "node:test";
import assert from "node:assert/strict";
import { extractAllWebSeeds } from "../js/magnetShared.js";
import { normalizeVideoNotePayload } from "../js/services/videoNotePayload.js";
const BTIH = "a".repeat(40);
const MAGNET = `magnet:?xt=urn:btih:${BTIH}`;
const SEED_A = "https://cdn-a.example.com/video.mp4";
const SEED_B = "https://cdn-b.example.com/video.mp4";
test("extractAllWebSeeds returns every ws= in order, deduped", () => {
const magnet = `${MAGNET}&ws=${encodeURIComponent(SEED_A)}&ws=${encodeURIComponent(
SEED_B,
)}&ws=${encodeURIComponent(SEED_A)}`;
assert.deepEqual(extractAllWebSeeds(magnet), [SEED_A, SEED_B]);
});
test("extractAllWebSeeds returns [] when there are no web seeds", () => {
assert.deepEqual(extractAllWebSeeds(MAGNET), []);
assert.deepEqual(extractAllWebSeeds(""), []);
assert.deepEqual(extractAllWebSeeds(null), []);
});
test("an array of web seeds all survive into the published magnet", () => {
const { payload, errors } = normalizeVideoNotePayload({
title: "Multi seed",
magnet: MAGNET,
ws: [SEED_A, SEED_B],
});
assert.deepEqual(errors, []);
const seeds = extractAllWebSeeds(payload.legacyFormData.magnet);
assert.ok(seeds.includes(SEED_A), "first webseed must be carried in the magnet");
assert.ok(seeds.includes(SEED_B), "second webseed must be carried in the magnet");
});
test("a newline/comma-delimited ws field is split into multiple seeds", () => {
const { payload } = normalizeVideoNotePayload({
title: "Multi seed text",
magnet: MAGNET,
ws: `${SEED_A}\n${SEED_B}`,
});
const seeds = extractAllWebSeeds(payload.legacyFormData.magnet);
assert.ok(seeds.includes(SEED_A));
assert.ok(seeds.includes(SEED_B));
});
test("the hosted CDN url is added as a webseed alongside the explicit ones", () => {
const cdn = "https://cdn.example.com/video.mp4";
const { payload } = normalizeVideoNotePayload({
title: "Cdn plus seed",
url: cdn,
magnet: MAGNET,
ws: SEED_A,
});
const seeds = extractAllWebSeeds(payload.legacyFormData.magnet);
assert.ok(seeds.includes(SEED_A), "explicit webseed preserved");
assert.ok(seeds.includes(cdn), "hosted url also attached as a webseed");
});
test("a single webseed string still works (back-compat)", () => {
const { payload } = normalizeVideoNotePayload({
title: "Single seed",
magnet: MAGNET,
ws: SEED_A,
});
const seeds = extractAllWebSeeds(payload.legacyFormData.magnet);
assert.deepEqual(seeds, [SEED_A]);
// The single content `ws` field still reflects the primary seed.
assert.equal(payload.legacyFormData.ws, SEED_A);
});
test("duplicate webseeds are not written twice", () => {
const { payload } = normalizeVideoNotePayload({
title: "Dupe seed",
magnet: MAGNET,
ws: [SEED_A, SEED_A, " " + SEED_A + " "],
});
const seeds = extractAllWebSeeds(payload.legacyFormData.magnet);
assert.equal(
seeds.filter((s) => s === SEED_A).length,
1,
"the same webseed must appear at most once",
);
});