mirror of
https://github.com/PR0M3TH3AN/bitvid.git
synced 2026-11-07 13:22:54 +00:00
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`.
This commit is contained in:
1 parent
dedc2aefa5
commit
45b20bb58e
3 files changed
+147
-45
No files matched your search
@@ -0,0 +1,66 @@
|
||||
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 }));
|
||||
});
|
||||
Reference in new issue
Block a user