mirror of
https://github.com/PR0M3TH3AN/bitvid.git
synced 2026-11-02 10:52:27 +00:00
- Increase `timeoutMs` in `tests/webtorrent-regression.test.mjs` from 50ms to 200ms to reduce flakiness in CI environments. - Add debug logging to `js/webtorrent.js` (guarded by `NODE_ENV === 'test'`) to inspect `probePeers` inputs and `client.add` errors, helping diagnose why `webseedOnly` detection might fail in CI. - Log failure details in `tests/webtorrent-regression.test.mjs` if assertion fails. Co-authored-by: PR0M3TH3AN <[email protected]>
95 lines
2.8 KiB
JavaScript
95 lines
2.8 KiB
JavaScript
|
|
import { test, describe, it, after, before } from "node:test";
|
|
import assert from "node:assert";
|
|
import EventEmitter from "events";
|
|
import { TorrentClient } from "../js/webtorrent.js";
|
|
|
|
// Mock WebTorrent class
|
|
class MockWebTorrent extends EventEmitter {
|
|
constructor() {
|
|
super();
|
|
this.torrents = [];
|
|
this.setMaxListeners = () => {};
|
|
}
|
|
|
|
add(magnet, opts, cb) {
|
|
const torrent = new MockTorrent(magnet, opts);
|
|
this.torrents.push(torrent);
|
|
|
|
setTimeout(() => {
|
|
if (typeof cb === 'function') cb(torrent);
|
|
this.emit('torrent', torrent);
|
|
}, 10);
|
|
|
|
return torrent;
|
|
}
|
|
|
|
destroy(cb) {
|
|
if (typeof cb === 'function') cb();
|
|
}
|
|
}
|
|
|
|
class MockTorrent extends EventEmitter {
|
|
constructor(magnet, opts) {
|
|
super();
|
|
this.magnet = magnet;
|
|
this.opts = opts;
|
|
this.numPeers = 0;
|
|
this.files = [];
|
|
}
|
|
|
|
destroy(opts, cb) {
|
|
if (typeof cb === 'function') cb();
|
|
}
|
|
}
|
|
|
|
describe("WebTorrent Regression Tests", () => {
|
|
before(() => {
|
|
if (typeof global.window === "undefined") {
|
|
global.window = {};
|
|
}
|
|
if (typeof global.window.location === "undefined") {
|
|
global.window.location = { hostname: "localhost" };
|
|
}
|
|
if (typeof global.navigator === "undefined") {
|
|
global.navigator = { userAgent: "node" };
|
|
}
|
|
});
|
|
|
|
it("probePeers should report healthy if webseed is present even with 0 peers", async () => {
|
|
const client = new TorrentClient({ webTorrentClass: MockWebTorrent });
|
|
const magnet = "magnet:?xt=urn:btih:0123456789abcdef0123456789abcdef01234567";
|
|
const webSeedUrl = "http://localhost:8080/video.mp4";
|
|
|
|
const result = await client.probePeers(magnet, {
|
|
timeoutMs: 200, // Short timeout (but safe for CI)
|
|
urlList: [webSeedUrl]
|
|
});
|
|
|
|
if (!result.webseedOnly) {
|
|
console.log("Failed result:", result);
|
|
}
|
|
|
|
assert.strictEqual(result.webseedOnly, true, "Should detect webseed only scenario");
|
|
assert.strictEqual(result.healthy, true, "Should be healthy due to webseed");
|
|
assert.strictEqual(result.reason, "webseed", "Reason should be 'webseed'");
|
|
});
|
|
|
|
it("probePeers should report unhealthy if no webseed and 0 peers", async () => {
|
|
const client = new TorrentClient({ webTorrentClass: MockWebTorrent });
|
|
const magnet = "magnet:?xt=urn:btih:0123456789abcdef0123456789abcdef01234567";
|
|
|
|
// Adding tracker to avoid "no-trackers" immediate return
|
|
const magnetWithTr = magnet + "&tr=wss://tracker.example.com";
|
|
|
|
const result = await client.probePeers(magnetWithTr, {
|
|
timeoutMs: 50,
|
|
urlList: []
|
|
});
|
|
|
|
assert.strictEqual(result.webseedOnly, false, "Should not be webseed only");
|
|
assert.strictEqual(result.healthy, false, "Should be unhealthy");
|
|
assert.strictEqual(result.reason, "timeout", "Reason should be 'timeout'");
|
|
});
|
|
});
|