refactor: use qBittorrent Sync API (/api/v2/sync/maindata) with fallback
All checks were successful
Docs Check / Markdown lint (push) Successful in 51s
Licence Check / Licence compatibility and copyright header verification (push) Successful in 1m16s
CI / Tests & coverage (push) Successful in 1m37s
CI / Security audit (push) Successful in 1m44s
Docs Check / Mermaid diagram parse check (push) Successful in 1m52s

- QBittorrentClient now uses the incremental Sync API instead of repeatedly
  fetching the full torrent list via /api/v2/torrents/info.
- Per-client state: lastRid, torrentMap, fallbackThisCycle.
- Handles full_update, delta updates, and torrents_removed.
- Falls back to legacy torrents/info at most once per poll cycle.
- getAllTorrents() resets fallback flags before each cycle.
- Added 9 new unit tests covering: first sync, delta merge, full_update,
  torrents_removed, fallback path, direct-legacy-after-fallback, 403 re-auth,
  completed-field computation, and fallback reset.
This commit is contained in:
2026-05-19 09:33:20 +01:00
parent 8c4cc20551
commit 0a54d0d302
3 changed files with 392 additions and 11 deletions

View File

@@ -7,7 +7,8 @@
* dashboard card rendering so correctness matters for UX.
*/
import { mapTorrentToDownload, formatBytes, formatSpeed, formatEta } from '../../server/utils/qbittorrent.js';
import { mapTorrentToDownload, formatBytes, formatSpeed, formatEta, QBittorrentClient } from '../../server/utils/qbittorrent.js';
import nock from 'nock';
// Minimal torrent fixture that satisfies mapTorrentToDownload's expectations
function makeTorrent(overrides = {}) {
@@ -32,6 +33,35 @@ function makeTorrent(overrides = {}) {
};
}
const QBT_URL = 'http://qbittorrent.test:8080';
function makeClient(overrides = {}) {
return new QBittorrentClient({
id: 'test-qbt',
name: 'TestQBT',
url: QBT_URL,
username: 'admin',
password: 'adminadmin',
...overrides
});
}
function mockLogin() {
return nock(QBT_URL)
.post('/api/v2/auth/login')
.reply(200, {}, { 'set-cookie': ['SID=abc123; path=/'] });
}
function mockSync(rid, response) {
return nock(QBT_URL)
.get(`/api/v2/sync/maindata?rid=${rid}`)
.reply(200, response);
}
afterEach(() => {
nock.cleanAll();
});
describe('formatBytes', () => {
it('formats 0 bytes', () => expect(formatBytes(0)).toBe('0 B'));
it('formats bytes', () => expect(formatBytes(512)).toBe('512 B'));
@@ -110,3 +140,230 @@ describe('mapTorrentToDownload', () => {
expect(result.savePath).toBe('/dl/');
});
});
describe('QBittorrentClient sync API', () => {
it('first call uses rid=0 and returns full torrent list', async () => {
mockLogin();
const client = makeClient();
await client.login();
mockSync(0, {
rid: 1,
full_update: true,
torrents: {
hash01: { name: 'Test1', state: 'downloading', size: 1000, progress: 0.5, dlspeed: 100, eta: 60, num_seeds: 5, num_leechs: 2, availability: 1.0 }
}
});
const torrents = await client.getTorrents();
expect(torrents).toHaveLength(1);
expect(torrents[0].name).toBe('Test1');
expect(torrents[0].instanceId).toBe('test-qbt');
expect(torrents[0].hash).toBe('hash01');
expect(client.lastRid).toBe(1);
});
it('subsequent call uses last rid and merges delta', async () => {
mockLogin();
const client = makeClient();
await client.login();
// First call
mockSync(0, {
rid: 1,
full_update: true,
torrents: {
hash01: { name: 'Test1', state: 'downloading', size: 1000, progress: 0.5, dlspeed: 100, eta: 60, num_seeds: 5, num_leechs: 2, availability: 1.0 }
}
});
await client.getTorrents();
// Second call — delta
mockSync(1, {
rid: 2,
full_update: false,
torrents: {
hash01: { dlspeed: 200 }
}
});
const torrents = await client.getTorrents();
expect(torrents).toHaveLength(1);
expect(torrents[0].dlspeed).toBe(200);
expect(torrents[0].name).toBe('Test1');
expect(client.lastRid).toBe(2);
});
it('handles full_update=true on subsequent call', async () => {
mockLogin();
const client = makeClient();
await client.login();
// First call
mockSync(0, {
rid: 1,
full_update: true,
torrents: {
hash01: { name: 'Test1', state: 'downloading', size: 1000, progress: 0.5, dlspeed: 100, eta: 60, num_seeds: 5, num_leechs: 2, availability: 1.0 }
}
});
await client.getTorrents();
// Server forces full refresh
mockSync(1, {
rid: 2,
full_update: true,
torrents: {
hash02: { name: 'Test2', state: 'uploading', size: 2000, progress: 1.0, dlspeed: 0, eta: 0, num_seeds: 10, num_leechs: 0, availability: 1.0 }
}
});
const torrents = await client.getTorrents();
expect(torrents).toHaveLength(1);
expect(torrents[0].name).toBe('Test2');
expect(torrents[0].hash).toBe('hash02');
expect(client.lastRid).toBe(2);
});
it('removes torrents when torrents_removed is present', async () => {
mockLogin();
const client = makeClient();
await client.login();
mockSync(0, {
rid: 1,
full_update: true,
torrents: {
hash01: { name: 'Test1', state: 'downloading', size: 1000, progress: 0.5, dlspeed: 100, eta: 60, num_seeds: 5, num_leechs: 2, availability: 1.0 }
}
});
await client.getTorrents();
mockSync(1, {
rid: 2,
full_update: false,
torrents_removed: ['hash01']
});
const torrents = await client.getTorrents();
expect(torrents).toHaveLength(0);
});
it('falls back to torrents/info when sync fails', async () => {
mockLogin();
const client = makeClient();
await client.login();
// Sync fails with 500
nock(QBT_URL)
.get('/api/v2/sync/maindata?rid=0')
.reply(500, { error: 'Internal Server Error' });
// Legacy succeeds
nock(QBT_URL)
.get('/api/v2/torrents/info')
.reply(200, [
{ name: 'Fallback', hash: 'fb01', state: 'downloading', size: 1073741824, progress: 0.5, dlspeed: 1048576, eta: 512, num_seeds: 10, num_leechs: 3, availability: 1.0 }
]);
const torrents = await client.getTorrents();
expect(torrents).toHaveLength(1);
expect(torrents[0].name).toBe('Fallback');
expect(client.fallbackThisCycle).toBe(true);
});
it('uses legacy immediately if already fell back this cycle', async () => {
mockLogin();
const client = makeClient();
await client.login();
client.fallbackThisCycle = true;
// Only legacy should be called
nock(QBT_URL)
.get('/api/v2/torrents/info')
.reply(200, [
{ name: 'DirectLegacy', hash: 'dl01', state: 'downloading', size: 1000, progress: 0.5, dlspeed: 100, eta: 60, num_seeds: 5, num_leechs: 2, availability: 1.0 }
]);
// Ensure sync is NOT called
const syncScope = nock(QBT_URL)
.get('/api/v2/sync/maindata?rid=0')
.reply(200, { rid: 1, full_update: true });
const torrents = await client.getTorrents();
expect(torrents).toHaveLength(1);
expect(torrents[0].name).toBe('DirectLegacy');
expect(syncScope.isDone()).toBe(false);
});
it('re-authenticates on 403 during sync and retries', async () => {
mockLogin();
const client = makeClient();
await client.login();
// First sync call returns 403
nock(QBT_URL)
.get('/api/v2/sync/maindata?rid=0')
.reply(403, {});
// Re-login
nock(QBT_URL)
.post('/api/v2/auth/login')
.reply(200, {}, { 'set-cookie': ['SID=newtoken; path=/'] });
// Retry succeeds
nock(QBT_URL)
.get('/api/v2/sync/maindata?rid=0')
.reply(200, {
rid: 1,
full_update: true,
torrents: {
hash01: { name: 'AfterReauth', state: 'downloading', size: 1000, progress: 0.5, dlspeed: 100, eta: 60, num_seeds: 5, num_leechs: 2, availability: 1.0 }
}
});
const torrents = await client.getTorrents();
expect(torrents).toHaveLength(1);
expect(torrents[0].name).toBe('AfterReauth');
});
it('computes completed from size and progress when missing', async () => {
mockLogin();
const client = makeClient();
await client.login();
mockSync(0, {
rid: 1,
full_update: true,
torrents: {
hash01: { name: 'NoCompleted', state: 'downloading', size: 1000, progress: 0.5, dlspeed: 100, eta: 60, num_seeds: 5, num_leechs: 2, availability: 1.0 }
}
});
const torrents = await client.getTorrents();
expect(torrents[0].completed).toBe(500);
});
it('resets fallback flag when getAllTorrents resets it', async () => {
mockLogin();
const client = makeClient();
await client.login();
client.fallbackThisCycle = true;
// After reset, sync should be attempted
mockSync(0, {
rid: 1,
full_update: true,
torrents: {
hash01: { name: 'ResetWorks', state: 'downloading', size: 1000, progress: 0.5, dlspeed: 100, eta: 60, num_seeds: 5, num_leechs: 2, availability: 1.0 }
}
});
// Simulate the reset that getAllTorrents performs
client.fallbackThisCycle = false;
const torrents = await client.getTorrents();
expect(torrents[0].name).toBe('ResetWorks');
expect(client.fallbackThisCycle).toBe(false);
});
});