feat: download lifecycle — stalled and failed downloads are given up and searched again; imported ones seed, then go (M502 #5523)
release / govulncheck (push) Successful in 17s
release / web (push) Failing after 17s
release / go (push) Successful in 1m11s
release / integration (push) Successful in 4m50s
release / android (push) Successful in 4m54s
release / Build signed APK (releases and dev) (push) Successful in 5m9s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Skipped
release / Verify release artifacts (tag releases only) (push) Skipped

Ported from Fabled Librarian's System 104. A new lifecycle poller, every
ten minutes:
- a download the client fails is given up at once;
- one nobody shares, or still fetching its file list, past the stall
  setting (6 h) is asked for again three times, then given up;
- giving up removes the download with its files, holds the release back
  for a week (release_blocks.expires_at), ends the grab and searches the
  album again, with no limit per album;
- a running download the client stops listing for half an hour ends and is
  searched again, not held back; an empty list is not taken as gone;
- an imported download seeds to ratio 2.0 or 14 days, then is removed;
- a download the client would not remove is asked again every pass.

Every condition and act is one open row per (client, download, condition)
in download_interventions. Activity shows what is wrong with a download and
how often it was retried, why one ended, its seeding, and a banner for a
client that does not answer. A running download can be given up by hand
(set aside for good). The seed ratio, seed days and stall hours are on the
search settings card. The download client lookup moves to
acquire.DownloadClientFor, shared by the importer and the lifecycle.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-10 15:27:22 -04:00
co-authored by Claude Opus 5.5
parent d9e00d8bc2
commit 8245f4406e
34 changed files with 2362 additions and 106 deletions
@@ -1,18 +1,19 @@
import { afterEach, describe, expect, test, vi } from 'vitest';
import { fireEvent, render, screen, waitFor } from '@testing-library/svelte';
import { mockInfiniteQuery } from '../../../test-utils/query';
import type { Download } from '#lib/api/types.js';
import type { ClientDown, Download } from '#lib/api/types.js';
vi.mock('#lib/api/wanted.js', () => ({
createDownloadsQuery: vi.fn(),
importAnyway: vi.fn().mockResolvedValue(undefined),
retryImport: vi.fn().mockResolvedValue(undefined),
refuseDownload: vi.fn().mockResolvedValue(undefined)
refuseDownload: vi.fn().mockResolvedValue(undefined),
abandonDownload: vi.fn().mockResolvedValue(undefined)
}));
vi.mock('#lib/stores/toast.svelte.js', () => ({ pushToast: vi.fn() }));
import DownloadList from './DownloadList.svelte';
import { createDownloadsQuery, importAnyway, refuseDownload, retryImport } from '#lib/api/wanted.js';
import { abandonDownload, createDownloadsQuery, importAnyway, refuseDownload, retryImport } from '#lib/api/wanted.js';
const ago = (ms: number) => new Date(Date.now() - ms).toISOString();
@@ -38,15 +39,22 @@ function download(id: string, extra: Partial<Download> = {}): Download {
destination: '',
files: [],
skipped: 0,
end_detail: '',
seeding: false,
ratio: 0,
seed_released_at: null,
condition: null,
...extra
};
}
afterEach(() => vi.clearAllMocks());
function setup(items: Download[]) {
function setup(items: Download[], clientsDown: ClientDown[] = []) {
vi.mocked(createDownloadsQuery).mockReturnValue(
mockInfiniteQuery({ pages: [{ items, total: items.length, limit: 50, offset: 0 }] }) as unknown as ReturnType<
mockInfiniteQuery({
pages: [{ items, total: items.length, limit: 50, offset: 0, clients_down: clientsDown }]
}) as unknown as ReturnType<
typeof createDownloadsQuery
>
);
@@ -54,13 +62,45 @@ function setup(items: Download[]) {
}
describe('DownloadList', () => {
test('a running download shows how far it has got', () => {
test('a running download shows how far it has got, and can be given up', async () => {
setup([download('d1', { progress: 0.375 })]);
expect(screen.getByTestId('download-state').textContent).toMatch(/downloading/i);
expect(screen.getByRole('progressbar', { name: /downloaded/i }).getAttribute('aria-valuenow')).toBe('37');
expect(screen.queryByTestId('download-condition')).toBeNull();
await fireEvent.click(screen.getByRole('button', { name: /give up/i }));
await waitFor(() => expect(abandonDownload).toHaveBeenCalledWith('d1'));
});
test('a stalled download says what is wrong and how often it was asked for again', () => {
setup([
download('d1', {
progress: 0.4,
condition: { trigger: 'no_peers', detail: 'Nobody has shared it in 7 hours; 40% downloaded.', retries: 2, since: ago(1_200_000) }
})
]);
expect(screen.getByTestId('download-condition').textContent).toMatch(
/nobody has shared it in 7 hours; 40% downloaded\. asked for again 2 of 3 times/i
);
});
test('a download given up on says why it ended', () => {
setup([
download('d1', {
ended_at: ago(60_000),
end_reason: 'stalled',
end_detail: 'Taken 7 hours ago and never finished: nobody was sharing it. Held back until 17 Oct.'
})
]);
expect(screen.getByTestId('download-state').textContent).toMatch(/given up/i);
expect(screen.getByTestId('download-ending').textContent).toMatch(/nobody was sharing it/);
expect(screen.queryByRole('button')).toBeNull();
});
test('a download client that does not answer is named above the list', () => {
setup([download('d1')], [{ client: 'qbittorrent', detail: 'connection refused', since: ago(600_000) }]);
expect(screen.getByTestId('client-down').textContent).toMatch(/can't reach the download client qbittorrent .*connection refused/i);
});
test('a doubtful download says why and can be imported anyway or refused', async () => {
setup([
download('d1', {
@@ -102,6 +142,17 @@ describe('DownloadList', () => {
expect(screen.queryByRole('button')).toBeNull();
});
test('an imported download says how its seeding goes', () => {
const imported = { ended_at: ago(60_000), end_reason: 'imported', import_state: 'imported' as const };
setup([
download('d1', { ...imported, seeding: true, ratio: 0.8 }),
download('d2', { ...imported, seeding: true, ratio: 2.1, seed_released_at: ago(60_000) })
]);
const lines = screen.getAllByTestId('download-seeding').map((e) => e.textContent);
expect(lines[0]).toMatch(/seeding: share ratio 0\.80/i);
expect(lines[1]).toMatch(/seeded, then removed/i);
});
test('with nothing downloaded it says what lands here', () => {
setup([]);
expect(screen.getByText(/nothing has been downloaded yet/i)).toBeTruthy();