Files
bitvid/tests/ui/torrentStatusController.test.mjs
google-labs-jules[bot] 45b20bb58e refactor(ai): extract updateTorrentStatus from js/app.js (agent)
Moved torrent status update logic to `js/ui/torrentStatusController.js` to improve separation of concerns as per `AGENTS.md`. The controller accepts a `getVideoModal` accessor and an `onRemovePoster` callback to decouple it from direct `videoModal` or `Application` dependencies. Added unit tests in `tests/ui/torrentStatusController.test.mjs`.
2026-01-18 03:11:25 +00:00

67 lines
2.1 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import TorrentStatusController from "../../js/ui/torrentStatusController.js";
test("TorrentStatusController throws if accessor is missing", (t) => {
assert.throws(() => new TorrentStatusController({}), /accessor/);
});
test("TorrentStatusController updates video modal and calls onRemovePoster", (t) => {
let modalState = {};
let posterRemoved = false;
const mockModal = {
updateStatus: (msg) => { modalState.status = msg; },
updateProgress: (msg) => { modalState.progress = msg; },
updatePeers: (msg) => { modalState.peers = msg; },
updateSpeed: (msg) => { modalState.speed = msg; },
updateDownloaded: (msg) => { modalState.downloaded = msg; },
};
const onRemovePoster = (reason) => {
posterRemoved = reason;
};
const controller = new TorrentStatusController({
getVideoModal: () => mockModal,
onRemovePoster
});
// Test basic update
controller.update({
ready: false,
progress: 0.5,
numPeers: 5,
downloadSpeed: 1024 * 100, // 100 KB/s
downloaded: 1024 * 1024 * 10, // 10 MB
length: 1024 * 1024 * 100 // 100 MB
});
assert.equal(modalState.status, "Downloading");
assert.equal(modalState.progress, "50.00%");
assert.equal(modalState.peers, "Peers: 5");
assert.equal(modalState.speed, "100.00 KB/s");
assert.equal(modalState.downloaded, "10.00 MB / 100.00 MB");
assert.equal(posterRemoved, "torrent-progress");
// Test complete
controller.update({
ready: true,
progress: 1,
numPeers: 0,
downloadSpeed: 0,
downloaded: 1024 * 1024 * 100,
length: 1024 * 1024 * 100
});
// It sets "Complete" then "Ready to play" if ready is true
assert.equal(modalState.status, "Ready to play");
assert.equal(modalState.progress, "100.00%");
assert.equal(posterRemoved, "torrent-ready-flag");
});
test("TorrentStatusController handles missing modal gracefully", (t) => {
const controller = new TorrentStatusController({ getVideoModal: () => null });
assert.doesNotThrow(() => controller.update({ progress: 0.5 }));
});