Files
minstrel/web/src/lib/components/wanted/DownloadList.test.ts
T
bvandeusenandClaude Opus 5.5 8245f4406e
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
feat: download lifecycle — stalled and failed downloads are given up and searched again; imported ones seed, then go (M502 #5523)
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>
2026-10-10 15:27:22 -04:00

161 lines
6.5 KiB
TypeScript

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 { 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),
abandonDownload: vi.fn().mockResolvedValue(undefined)
}));
vi.mock('#lib/stores/toast.svelte.js', () => ({ pushToast: vi.fn() }));
import DownloadList from './DownloadList.svelte';
import { abandonDownload, createDownloadsQuery, importAnyway, refuseDownload, retryImport } from '#lib/api/wanted.js';
const ago = (ms: number) => new Date(Date.now() - ms).toISOString();
function download(id: string, extra: Partial<Download> = {}): Download {
return {
id,
want_id: 'w1',
want_title: 'Random Access Memories',
want_artist: 'Daft Punk',
name: 'Daft Punk - Random Access Memories (2013) [FLAC]',
indexer: 'Prowlarr',
client: 'qbittorrent',
quality: 'lossless',
origin: 'schedule',
grabbed_at: ago(3_600_000),
ended_at: null,
end_reason: '',
progress: 0,
import_state: 'downloading',
import_reason: '',
imported_at: null,
imported_files: 0,
destination: '',
files: [],
skipped: 0,
end_detail: '',
seeding: false,
ratio: 0,
seed_released_at: null,
condition: null,
...extra
};
}
afterEach(() => vi.clearAllMocks());
function setup(items: Download[], clientsDown: ClientDown[] = []) {
vi.mocked(createDownloadsQuery).mockReturnValue(
mockInfiniteQuery({
pages: [{ items, total: items.length, limit: 50, offset: 0, clients_down: clientsDown }]
}) as unknown as ReturnType<
typeof createDownloadsQuery
>
);
return render(DownloadList);
}
describe('DownloadList', () => {
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', {
import_state: 'review',
import_reason: 'Only the lengths agree. A person decides.',
evidence: { files: 13, tracks: 13, identified: 0, recordings: 0, lengths: 13, titles: 0, fingerprinted: false }
})
]);
expect(screen.getByTestId('download-state').textContent).toMatch(/needs you/i);
expect(screen.getByTestId('download-reason').textContent).toMatch(/a person decides/i);
expect(screen.getByText(/13 files against 13 tracks · no AcoustID · 13 lengths agree/)).toBeTruthy();
await fireEvent.click(screen.getByRole('button', { name: /import anyway/i }));
await waitFor(() => expect(importAnyway).toHaveBeenCalledWith('d1'));
await fireEvent.click(screen.getByRole('button', { name: /refuse/i }));
await waitFor(() => expect(refuseDownload).toHaveBeenCalledWith('d1'));
});
test('a failed import can be tried again', async () => {
setup([download('d1', { import_state: 'failed', import_reason: 'The library folder is not writable.' })]);
expect(screen.queryByRole('button', { name: /import anyway/i })).toBeNull();
await fireEvent.click(screen.getByRole('button', { name: /try again/i }));
await waitFor(() => expect(retryImport).toHaveBeenCalledWith('d1'));
});
test('an imported download says what was filed where', () => {
setup([
download('d1', {
ended_at: ago(60_000),
end_reason: 'imported',
import_state: 'imported',
imported_at: ago(60_000),
imported_files: 12,
skipped: 1,
destination: '/music/Daft Punk/Random Access Memories (2013)'
})
]);
expect(screen.getByTestId('download-state').textContent).toMatch(/imported/i);
expect(screen.getByText(/12 tracks filed, 1 already held into \/music\/Daft Punk/)).toBeTruthy();
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();
});
});