diff --git a/apps/web/src/api/schema.d.ts b/apps/web/src/api/schema.d.ts index 9976eab3..7bddd1e1 100644 --- a/apps/web/src/api/schema.d.ts +++ b/apps/web/src/api/schema.d.ts @@ -3858,6 +3858,26 @@ export interface paths { patch?: never; trace?: never; }; + "/api/acquisition/recovery/blocklist/{block_id}/resume": { + parameters: { + query?: never; + header?: never; + path?: never; + cookie?: never; + }; + get?: never; + put?: never; + /** + * Resume Block + * @description Remove the block and continue any held transfer it stopped (never re-adds a torrent). + */ + post: operations["resume_block_api_acquisition_recovery_blocklist__block_id__resume_post"]; + delete?: never; + options?: never; + head?: never; + patch?: never; + trace?: never; + }; "/api/acquisition/recovery/reports": { parameters: { query?: never; @@ -12153,6 +12173,7 @@ export interface components { /** Query Keys */ query_keys?: string[]; download?: components["schemas"]["ReleaseDownloadStatus"] | null; + block?: components["schemas"]["ReleaseBlockInfo"] | null; }; /** ReadIssueView */ ReadIssueView: { @@ -12615,6 +12636,31 @@ export interface components { /** Source Origin */ source_origin: string; }; + /** + * ReleaseBlockInfo + * @description Why a release cannot be downloaded again until an administrator unblocks it. + */ + ReleaseBlockInfo: { + /** + * Id + * Format: uuid + */ + id: string; + /** Reason */ + reason: string; + /** Automatic */ + automatic: boolean; + /** + * Blocked At + * Format: date-time + */ + blocked_at: string; + /** + * Can Resume + * @default false + */ + can_resume: boolean; + }; /** ReleaseDownloadStatus */ ReleaseDownloadStatus: { /** @@ -13107,6 +13153,18 @@ export interface components { description: string; release_policy?: components["schemas"]["ProfileSnapshot"] | null; }; + /** ResumeAttemptView */ + ResumeAttemptView: { + /** + * Attempt Id + * Format: uuid + */ + attempt_id: string; + /** Resumed */ + resumed: boolean; + /** Message */ + message: string; + }; /** ReviewInput */ ReviewInput: { /** Expected Revision */ @@ -14671,6 +14729,20 @@ export interface components { /** Size Bytes */ size_bytes: number; }; + /** UnblockView */ + UnblockView: { + /** + * Block Id + * Format: uuid + */ + block_id: string; + /** Unblocked */ + unblocked: boolean; + /** Resumed */ + resumed: number; + /** Attempts */ + attempts: components["schemas"]["ResumeAttemptView"][]; + }; /** Usage */ Usage: { /** User Id */ @@ -23237,6 +23309,37 @@ export interface operations { }; }; }; + resume_block_api_acquisition_recovery_blocklist__block_id__resume_post: { + parameters: { + query?: never; + header?: never; + path: { + block_id: string; + }; + cookie?: never; + }; + requestBody?: never; + responses: { + /** @description Successful Response */ + 200: { + headers: { + [name: string]: unknown; + }; + content: { + "application/json": components["schemas"]["UnblockView"]; + }; + }; + /** @description Validation Error */ + 422: { + headers: { + [name: string]: unknown; + }; + content: { + "application/json": components["schemas"]["HTTPValidationError"]; + }; + }; + }; + }; report_api_acquisition_recovery_reports_post: { parameters: { query?: never; diff --git a/apps/web/src/components/ReleaseBlockNotice.tsx b/apps/web/src/components/ReleaseBlockNotice.tsx new file mode 100644 index 00000000..6c595389 --- /dev/null +++ b/apps/web/src/components/ReleaseBlockNotice.tsx @@ -0,0 +1,105 @@ +import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; +import { CircleAlert, LockOpen } from "lucide-react"; +import { api, result, type Auth } from "../api/client"; +import type { components } from "../api/schema"; + +type Block = components["schemas"]["ReleaseBlockInfo"]; + +export function blockedSummary(block: Block) { + const when = new Date(block.blocked_at).toLocaleDateString(); + return `${block.reason} (${block.automatic ? "found automatically" : "reported"} ${when})`; +} + +// Why this release cannot be downloaded again, and the way out for administrators. +export default function ReleaseBlockNotice({ block }: { block: Block }) { + const cache = useQueryClient(); + const { data: session } = useQuery({ + queryKey: ["session"], + enabled: false, + }); + const admin = session?.user.role === "admin"; + const refresh = () => { + for (const name of [ + "book-sources", + "requests", + "downloads", + "activity", + "release-blocklist", + ]) + void cache.invalidateQueries({ queryKey: [name] }); + }; + const unblock = useMutation({ + mutationFn: async () => + result( + await api.DELETE("/api/acquisition/recovery/blocklist/{block_id}", { + params: { path: { block_id: block.id } }, + }), + ), + onSuccess: refresh, + }); + const resume = useMutation({ + mutationFn: async () => + result( + await api.POST( + "/api/acquisition/recovery/blocklist/{block_id}/resume", + { + params: { path: { block_id: block.id } }, + }, + ), + ), + onSuccess: refresh, + }); + const busy = unblock.isPending || resume.isPending; + const outcome = resume.data; + const refused = outcome?.attempts.filter((attempt) => !attempt.resumed); + const error = unblock.error || resume.error; + return ( +
+

+ Blocked: {blockedSummary(block)} +

+ {admin && ( +
+ {block.can_resume && ( + + )} + +
+ )} + {outcome && outcome.resumed > 0 && ( +

+ Resumed. Dewarr is checking the existing download; no new torrent was + added. +

+ )} + {refused?.map((attempt) => ( +

+ Unblocked, but the existing download was not resumed:{" "} + {attempt.message}. +

+ ))} + {error && ( +

+ {error.message} +

+ )} +
+ ); +} diff --git a/apps/web/src/components/SourceReleaseDetails.tsx b/apps/web/src/components/SourceReleaseDetails.tsx index 74f16d08..258c9ad8 100644 --- a/apps/web/src/components/SourceReleaseDetails.tsx +++ b/apps/web/src/components/SourceReleaseDetails.tsx @@ -4,6 +4,7 @@ import { useQuery } from "@tanstack/react-query"; import { ExternalLink } from "lucide-react"; import { api, result } from "../api/client"; import type { components } from "../api/schema"; +import ReleaseBlockNotice from "./ReleaseBlockNotice"; import SourceReleaseDownload from "./SourceReleaseDownload"; import { transferSize } from "../pages/DownloadConstraints"; import BookDialog from "./BookDialog"; @@ -104,6 +105,7 @@ export default function SourceReleaseDetails({ {!!item.assessment.blocked.length && (

{item.assessment.blocked.join(" · ")}

)} + {item.block && }
{canAcquire && ( + {item.block && } {item.release.authors?.join(", ") || "—"} @@ -742,12 +744,15 @@ function Results({ disabled={ data.status !== "completed" || !!unavailableReason(item) || + !!item.block || item.assessment.blocked.length > 0 } disabledReason={ unavailableReason(item) ? `${unavailableReason(item)}. Refresh results to download.` - : undefined + : item.block + ? "This release is blocked. Unblock it first." + : undefined } /> )} @@ -769,6 +774,7 @@ function Results({ canAcquire={canAcquire && canDownload(detailItem.release.medium)} disabled={ !!unavailableReason(detailItem) || + !!detailItem.block || detailItem.assessment.blocked.length > 0 } /> diff --git a/apps/web/src/sources.css b/apps/web/src/sources.css index b5a3c1cf..8f01549a 100644 --- a/apps/web/src/sources.css +++ b/apps/web/src/sources.css @@ -1017,3 +1017,13 @@ .collection-cover .cover-formats { display: none; } + +.release-block-notice { + display: grid; + gap: 0.35rem; + margin-top: 0.35rem; +} + +.release-block-notice p { + margin: 0; +} diff --git a/apps/web/tests/ui/release-blocklist.spec.ts b/apps/web/tests/ui/release-blocklist.spec.ts new file mode 100644 index 00000000..846982b3 --- /dev/null +++ b/apps/web/tests/ui/release-blocklist.spec.ts @@ -0,0 +1,209 @@ +import { expect, test } from "../fixtures"; + +const work = { + id: "work-1", + title: "The Midnight Train", + authors: ["Matt Haig"], + versions: [], + availability: { owned: false, ebook: false, audio: false, stale: false }, +}; +const profile = { id: null, generation: 0, preferences: {} }; + +function release(id: string) { + return { + id: `result-${id}`, + release: { + source: "mam", + source_id: id, + title: `The Midnight Train ${id}`, + medium: "audio", + protocol: "torrent", + authors: ["Matt Haig"], + narrators: [], + seeders: 12, + size_bytes: 492070294, + format: "m4b", + details: {}, + }, + assessment: { + identity: "corroborated", + blocked: [], + review: [], + explanation: [], + formats: ["m4b"], + source_origin: "mam", + }, + expires_at: new Date(Date.now() + 600_000).toISOString(), + current_connection: true, + possible_collection: false, + query_keys: [], + download: null, + }; +} + +async function open( + page: import("@playwright/test").Page, + role: "admin" | "member", + block: Record | null, + calls: string[], + responses: Record = {}, +) { + const errors: string[] = []; + page.on("pageerror", (error) => errors.push(error.message)); + await page.route("**/api/**", async (route) => { + const request = route.request(); + const path = new URL(request.url()).pathname; + let data: unknown = {}; + let status = 200; + if (path === "/api/auth/me") + data = { + user: { id: "u", role, username: "u", display_name: "U" }, + csrf_token: "test", + }; + else if (path === "/api/setup/onboarding") data = { status: "completed" }; + else if (path === "/api/catalog/works/work-1") data = work; + else if (path === "/api/acquisition/profiles") data = [profile]; + else if (path === "/api/acquisition/preferences/personal") + data = { effective: {} }; + else if ( + path.endsWith("/source-searches/latest") || + path.endsWith("/source-searches") + ) + data = { + id: "search-1", + work_id: work.id, + request_id: null, + query: "The Midnight Train", + medium: "audio", + offset: 0, + status: "completed", + stale_identity: false, + profile, + sources: [], + items: [ + { ...release("1"), block }, + { ...release("2"), block: null }, + ], + expires_at: new Date(Date.now() + 60_000).toISOString(), + }; + else if (path.includes("/api/acquisition/recovery/blocklist/")) { + calls.push(`${request.method()} ${path}`); + status = request.method() === "DELETE" ? 204 : 200; + data = request.method() === "DELETE" ? undefined : responses.resume; + } else if (path === "/api/metadata/works/work-1") + data = { + sources: [], + conflicts: [], + fields: {}, + versions: [], + versions_total: 0, + }; + else if (path.endsWith("/reader-match")) data = { book: null }; + else if (path.endsWith("/reader-details")) + data = { authors: [], reviews: [] }; + else if (path.endsWith("/versions")) data = []; + else if (path.includes("/group")) + data = { items: [], groups: [], members: [] }; + else if (path === "/api/requests" || path === "/api/catalog/works") + data = { items: [], total: 0 }; + await route.fulfill(status === 204 ? { status } : { status, json: data }); + }); + await page.goto("/books/work-1?tab=sources"); + return errors; +} + +const block = (canResume: boolean) => ({ + id: "block-1", + reason: "Completed files differ from the inspected torrent", + automatic: true, + blocked_at: "2026-11-18T17:00:00Z", + can_resume: canResume, +}); + +test("a blocked release says why, cannot be downloaded, and can be unblocked", async ({ + page, +}) => { + const calls: string[] = []; + const errors = await open(page, "admin", block(false), calls); + const row = page.getByRole("row", { name: "The Midnight Train 1" }); + await expect(row).toContainText("Blocked: Completed files differ"); + await expect(row).toContainText("found automatically"); + await expect( + row.getByRole("button", { name: "Download The Midnight Train 1" }), + ).toBeDisabled(); + // The other release in the list is unaffected. + const other = page.getByRole("row", { name: "The Midnight Train 2" }); + await expect(other).not.toContainText("Blocked"); + await expect( + other.getByRole("button", { name: "Download The Midnight Train 2" }), + ).toBeEnabled(); + // Nothing to resume: only the plain action is offered. + await expect( + row.getByRole("button", { name: "Unblock and resume" }), + ).toHaveCount(0); + await row.getByRole("button", { name: "Unblock", exact: true }).click(); + await expect + .poll(() => calls) + .toEqual(["DELETE /api/acquisition/recovery/blocklist/block-1"]); + expect(errors).toEqual([]); +}); + +test("an interrupted transfer offers Unblock and resume and reports the outcome", async ({ + page, +}) => { + const calls: string[] = []; + const errors = await open(page, "admin", block(true), calls, { + resume: { + block_id: "block-1", + unblocked: true, + resumed: 1, + attempts: [ + { attempt_id: "attempt-1", resumed: true, message: "Resumed" }, + ], + }, + }); + const row = page.getByRole("row", { name: "The Midnight Train 1" }); + await row.getByRole("button", { name: "Unblock and resume" }).click(); + await expect(row).toContainText("no new torrent was added"); + expect(calls).toEqual([ + "POST /api/acquisition/recovery/blocklist/block-1/resume", + ]); + expect(errors).toEqual([]); +}); + +test("a refused resume still unblocks and explains why", async ({ page }) => { + const calls: string[] = []; + await open(page, "admin", block(true), calls, { + resume: { + block_id: "block-1", + unblocked: true, + resumed: 0, + attempts: [ + { + attempt_id: "attempt-1", + resumed: false, + message: + "A replacement search or download already followed this failure", + }, + ], + }, + }); + const row = page.getByRole("row", { name: "The Midnight Train 1" }); + await row.getByRole("button", { name: "Unblock and resume" }).click(); + await expect(row).toContainText( + "Unblocked, but the existing download was not resumed", + ); + await expect(row).toContainText("already followed this failure"); + await expect(row).not.toContainText("Resumed. Dewarr"); +}); + +test("members see why a release is blocked but cannot unblock it", async ({ + page, +}) => { + const calls: string[] = []; + await open(page, "member", block(true), calls); + const row = page.getByRole("row", { name: "The Midnight Train 1" }); + await expect(row).toContainText("Blocked: Completed files differ"); + await expect(row.getByRole("button", { name: /Unblock/ })).toHaveCount(0); + expect(calls).toEqual([]); +}); diff --git a/docs/openapi.json b/docs/openapi.json index fca1ef4c..bd0fe8a5 100644 --- a/docs/openapi.json +++ b/docs/openapi.json @@ -12945,6 +12945,50 @@ } } }, + "/api/acquisition/recovery/blocklist/{block_id}/resume": { + "post": { + "tags": [ + "download-recovery" + ], + "summary": "Resume Block", + "description": "Remove the block and continue any held transfer it stopped (never re-adds a torrent).", + "operationId": "resume_block_api_acquisition_recovery_blocklist__block_id__resume_post", + "parameters": [ + { + "name": "block_id", + "in": "path", + "required": true, + "schema": { + "type": "string", + "format": "uuid", + "title": "Block Id" + } + } + ], + "responses": { + "200": { + "description": "Successful Response", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/UnblockView" + } + } + } + }, + "422": { + "description": "Validation Error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/HTTPValidationError" + } + } + } + } + } + } + }, "/api/acquisition/recovery/reports": { "post": { "tags": [ @@ -35548,6 +35592,16 @@ "type": "null" } ] + }, + "block": { + "anyOf": [ + { + "$ref": "#/components/schemas/ReleaseBlockInfo" + }, + { + "type": "null" + } + ] } }, "type": "object", @@ -36773,6 +36827,42 @@ ], "title": "ReleaseAssessment" }, + "ReleaseBlockInfo": { + "properties": { + "id": { + "type": "string", + "format": "uuid", + "title": "Id" + }, + "reason": { + "type": "string", + "title": "Reason" + }, + "automatic": { + "type": "boolean", + "title": "Automatic" + }, + "blocked_at": { + "type": "string", + "format": "date-time", + "title": "Blocked At" + }, + "can_resume": { + "type": "boolean", + "title": "Can Resume", + "default": false + } + }, + "type": "object", + "required": [ + "id", + "reason", + "automatic", + "blocked_at" + ], + "title": "ReleaseBlockInfo", + "description": "Why a release cannot be downloaded again until an administrator unblocks it." + }, "ReleaseDownloadStatus": { "properties": { "state": { @@ -38077,6 +38167,30 @@ ], "title": "RequestView" }, + "ResumeAttemptView": { + "properties": { + "attempt_id": { + "type": "string", + "format": "uuid", + "title": "Attempt Id" + }, + "resumed": { + "type": "boolean", + "title": "Resumed" + }, + "message": { + "type": "string", + "title": "Message" + } + }, + "type": "object", + "required": [ + "attempt_id", + "resumed", + "message" + ], + "title": "ResumeAttemptView" + }, "ReviewInput": { "properties": { "expected_revision": { @@ -42192,6 +42306,38 @@ ], "title": "TorrentFile" }, + "UnblockView": { + "properties": { + "block_id": { + "type": "string", + "format": "uuid", + "title": "Block Id" + }, + "unblocked": { + "type": "boolean", + "title": "Unblocked" + }, + "resumed": { + "type": "integer", + "title": "Resumed" + }, + "attempts": { + "items": { + "$ref": "#/components/schemas/ResumeAttemptView" + }, + "type": "array", + "title": "Attempts" + } + }, + "type": "object", + "required": [ + "block_id", + "unblocked", + "resumed", + "attempts" + ], + "title": "UnblockView" + }, "Usage": { "properties": { "user_id": { diff --git a/services/app/api/book_sources.py b/services/app/api/book_sources.py index 49821f3a..a779be1b 100644 --- a/services/app/api/book_sources.py +++ b/services/app/api/book_sources.py @@ -16,7 +16,13 @@ from app.api.prowlarr import resolve as resolve_prowlarr from app.api.source_artifacts import SourceArtifactView, artifact_view from app.db.models import AcquisitionIntent, Operation, SourceConnection, SourceResult -from app.domain import collection_review, release_download_status, series_preparation +from app.domain import ( + collection_review, + download_resume, + release_blocklist, + release_download_status, + series_preparation, +) from app.domain.automatic_eligibility import collection_candidate from app.domain.book_sources import SearchInput, accessible_work, checked, refresh_search, start from app.domain.release_profiles import ( @@ -66,6 +72,17 @@ class SearchQueryPlan(BaseModel): warnings: list[str] +class ReleaseBlockInfo(BaseModel): + """Why a release cannot be downloaded again until an administrator unblocks it.""" + + id: UUID + reason: str + automatic: bool + blocked_at: datetime + # An interrupted transfer for this release is still on the downloader and can continue. + can_resume: bool = False + + class RankedReleaseView(BaseModel): id: UUID release: SourceRelease @@ -75,6 +92,7 @@ class RankedReleaseView(BaseModel): possible_collection: bool = False query_keys: list[str] = Field(default_factory=list) download: release_download_status.ReleaseDownloadStatus | None = None + block: ReleaseBlockInfo | None = None class CatalogPreparationItem(BaseModel): @@ -134,8 +152,14 @@ async def view(db, user, operation_id): ) ranked = [] work = {**payload["work"], "identifiers": payload.get("identifiers", [])} - for row in rows: - release = parse_release(row.source_key, row.release_snapshot) + parsed = [parse_release(row.source_key, row.release_snapshot) for row in rows] + blocks = await release_blocklist.blocks_for( + db, UUID(payload["work"]["id"]), [(item, item.medium) for item in parsed] + ) + resumable = {} + for block in {b.id: b for b in blocks if b}.values(): + resumable[block.id] = bool(await download_resume.resumable_attempt_ids(db, block)) + for row, block, release in zip(rows, blocks, parsed, strict=True): release = catalog_language_release(release, work) assessment = assess_release(release, work, preferences, payload["medium"]) if not changed and (authors := indexer_title_authors(release, work)): @@ -154,6 +178,15 @@ async def view(db, user, operation_id): release=release, possible_collection=collection_candidate(release, work), download=downloads.get(release_download_status.identity(release)), + block=ReleaseBlockInfo( + id=block.id, + reason=block.reason, + automatic=block.automatic, + blocked_at=block.created_at, + can_resume=resumable[block.id], + ) + if block + else None, assessment=assessment, expires_at=row.expires_at, query_keys=row.query_keys if not changed else [], diff --git a/services/app/api/download_recovery.py b/services/app/api/download_recovery.py index dfc80bd8..2a46fec8 100644 --- a/services/app/api/download_recovery.py +++ b/services/app/api/download_recovery.py @@ -22,6 +22,7 @@ Work, ) from app.domain import download_recovery as recovery +from app.domain import download_resume from app.domain.download_attempts import locked from app.domain.operations import transaction_lock from app.domain.work_graph import family_ids @@ -139,19 +140,45 @@ async def blocklist( ] +class ResumeAttemptView(BaseModel): + attempt_id: UUID + resumed: bool + message: str + + +class UnblockView(BaseModel): + block_id: UUID + unblocked: bool + resumed: int + attempts: list[ResumeAttemptView] + + @router.delete("/blocklist/{block_id}", status_code=204) async def remove_block(block_id: UUID, user: Admin, db: Database): - block = await db.get(ReleaseBlock, block_id) - if not block: - raise HTTPException(404, "Blocklist entry not found") - from app.domain.work_graph import acquisition_lock - - await acquisition_lock(db, block.work_id) - block.active = False - db.add(AuditEvent(actor_id=user.id, action="acquisition.blocklist.removed", entity_id=block.id)) + await download_resume.unblock(db, user, block_id, resume=False) await db.commit() +@router.post("/blocklist/{block_id}/resume", response_model=UnblockView) +async def resume_block(block_id: UUID, user: Admin, db: Database): + """Remove the block and continue any held transfer it stopped (never re-adds a torrent).""" + block, results = await download_resume.unblock(db, user, block_id, resume=True) + await db.commit() + return UnblockView( + block_id=block.id, + unblocked=not block.active, + resumed=sum(1 for _, reason in results if reason is None), + attempts=[ + ResumeAttemptView( + attempt_id=attempt_id, + resumed=reason is None, + message=reason or "Resumed; checking the existing transfer", + ) + for attempt_id, reason in results + ], + ) + + @router.post("/reports", response_model=list[DownloadRecoveryView], status_code=202) async def report(body: ReportInput, user: Member, db: Database): from app.domain.download_memberships import attempt_for diff --git a/services/app/domain/download_attempts.py b/services/app/domain/download_attempts.py index 7c94c3ed..5105d61a 100644 --- a/services/app/domain/download_attempts.py +++ b/services/app/domain/download_attempts.py @@ -79,7 +79,38 @@ def attempt_tag(attempt): return "book-search:" + str(attempt.id) -def inspection_path(selection): +def layout_wrapper(expected, actual): + """Folder a client added around the torrent's own paths, or None. + + qBittorrent's "Subfolder" content layout wraps a torrent that has no root folder + (every single-file torrent) in a new folder named after it. The files and sizes + are the ones inspected, only one extra leading folder was added, so the transfer + is the expected one. Anything else (renames, other roots, different sizes, mixed + prefixes) is still a mismatch. + """ + if not expected or not actual or expected == actual or len(expected) != len(actual): + return None + wrappers = {path.partition("/")[0] for path in actual if "/" in path} + if len(wrappers) != 1 or not all("/" in path for path in actual): + return None + (wrapper,) = wrappers + if wrapper in {"", ".", ".."} or "\\" in wrapper: + return None + stripped = {path.partition("/")[2]: size for path, size in actual.items()} + return wrapper if stripped == expected else None + + +def observed_wrapper(selection, observation): + """The wrapper recorded in the last client observation, for a completed transfer.""" + files = (observation or {}).get("files") or [] + expected = { + item["path"]: item["size_bytes"] for item in selection.frozen["descriptor"]["files"] + } + actual = {item["relative_path"]: item["size_bytes"] for item in files} + return layout_wrapper(expected, actual) + + +def inspection_path(selection, wrapper=None): descriptor = selection.frozen["descriptor"] paths = [item["path"] for item in descriptor["files"]] if len(paths) == 1 and "/" not in paths[0]: @@ -90,8 +121,8 @@ def inspection_path(selection): raise HTTPException(409, "Torrent files do not have one supported inspection root") relative = "/".join( part - for part in (selection.frozen["mapping"]["relative_path"], name) - if part not in {"", "."} + for part in (selection.frozen["mapping"]["relative_path"], wrapper, name) + if part and part not in {"", "."} ) if len(relative) > 1024: raise HTTPException(409, "Completed download path exceeds the inspection limit") @@ -528,7 +559,8 @@ def transfer_stage(selection, state): item["path"]: item["size_bytes"] for item in selection.frozen["descriptor"]["files"] } actual = {item.relative_path: item.size_bytes for item in state.files} - if expected != actual or state.total_bytes != selection.frozen["descriptor"]["torrent_bytes"]: + matches = expected == actual or layout_wrapper(expected, actual) is not None + if not matches or state.total_bytes != selection.frozen["descriptor"]["torrent_bytes"]: inspected = ( "Soulseek file list" if selection.frozen["descriptor"].get("parser") == "slskd-file-list" @@ -665,7 +697,7 @@ async def create_inspection(db, attempt, selection, user, *, key=None): relative = ( completed_nzb_path(selection, (attempt.observation or {})["save_path"]) if nzb_release(selection) - else inspection_path(selection) + else inspection_path(selection, observed_wrapper(selection, attempt.observation)) ) operation = Operation( owner_id=user.id, diff --git a/services/app/domain/download_resume.py b/services/app/domain/download_resume.py new file mode 100644 index 00000000..98652d17 --- /dev/null +++ b/services/app/domain/download_resume.py @@ -0,0 +1,188 @@ +"""Continue an interrupted transfer once the release that stopped it is unblocked. + +A release is blocklisted when a transfer is judged failed. The torrent itself, its +hash claims and its files stay on the downloader, so a mistaken or outdated block +should not force a new download. Resuming reverses only the recovery bookkeeping +(cancelled selection, released reservation, recovery row) and lets the existing +attempt observe the existing transfer again. It never adds a torrent. +""" + +from datetime import UTC, datetime, timedelta +from uuid import UUID + +from fastapi import HTTPException +from sqlalchemy import select, text + +from app.config import get_settings +from app.db.models import ( + AcquisitionIntent, + AcquisitionReservation, + AcquisitionSelection, + AcquisitionTarget, + AuditEvent, + DownloadAttempt, + DownloadRecovery, + Operation, + ReportedDownloadAsset, + User, +) +from app.domain import download_attempts, release_blocklist +from app.domain.acquisition import evaluate +from app.domain.work_graph import canonical_work +from app.jobs.queue import enqueue + + +async def resumable_attempt_ids(db, block): + """Held attempts whose only obstacle is this block; the cheap pre-lock screen.""" + rows = await db.execute( + select(DownloadRecovery, AcquisitionSelection) + .join(AcquisitionSelection, AcquisitionSelection.id == DownloadRecovery.selection_id) + .join(DownloadAttempt, DownloadAttempt.id == DownloadRecovery.attempt_id) + .where( + DownloadRecovery.state == "held", + DownloadAttempt.state == "held", + DownloadAttempt.external_may_exist.is_(True), + AcquisitionSelection.state == "cancelled", + ) + .order_by(DownloadRecovery.created_at, DownloadRecovery.id) + ) + found = [] + for recovery, selection in rows: + frozen = selection.frozen + if frozen["requirements"]["medium"] != block.medium: + continue + keys = set(release_blocklist.release_keys(frozen["release"], frozen["descriptor"])[1]) + if not keys.intersection(block.identities): + continue + work = await canonical_work(db, UUID(frozen["origin_work_id"])) + if work.id == block.work_id and recovery.attempt_id not in found: + found.append(recovery.attempt_id) + return found + + +async def _refusal(db, attempt, recoveries): + """Why this attempt cannot be resumed, or None. Caller holds the attempt locks.""" + if get_settings().recovery_mode: + return "Downloads are paused for recovery" + if attempt.state != "held" or not attempt.external_may_exist: + return "The download is no longer held" + if not recoveries: + return "The download has no saved recovery to undo" + if any(row.state != "held" for row in recoveries): + return "A replacement search or download already followed this failure" + if any((row.evidence or {}).get("reported") for row in recoveries): + return "This release was reported as a problem by a person; it cannot be resumed" + if await db.scalar( + select(ReportedDownloadAsset.id) + .where(ReportedDownloadAsset.recovery_id.in_([row.id for row in recoveries])) + .limit(1) + ): + return "This release was reported as a problem by a person; it cannot be resumed" + return None + + +async def _queue_check(db, attempt): + """Same hand-off as an explicit recheck: one job, never two.""" + operation = await db.get(Operation, attempt.operation_id) + status = await db.scalar( + text("SELECT status::text FROM book_queue.procrastinate_jobs WHERE id=:id"), + {"id": operation.job_id}, + ) + if status not in {"todo", "doing"}: + operation.job_id = await enqueue(db, "acquisition.download", attempt_id=str(attempt.id)) + attempt.next_check_at = datetime.now(UTC) + timedelta(seconds=60) + + +async def _resume_attempt(db, user, attempt_id, block): + attempt, _ = await download_attempts.locked(db, attempt_id) + if not attempt: + return "The download no longer exists" + recoveries = list( + await db.scalars( + select(DownloadRecovery) + .where(DownloadRecovery.attempt_id == attempt.id) + .execution_options(populate_existing=True) + ) + ) + if reason := await _refusal(db, attempt, recoveries): + return reason + selections = [ + await db.get(AcquisitionSelection, row.selection_id, populate_existing=True) + for row in recoveries + ] + if any(item.state != "cancelled" for item in selections): + return "The request moved on after this failure" + block.active = False + await db.flush() + for item in selections: + frozen = item.frozen + if await release_blocklist.blocked( + db, + (await canonical_work(db, UUID(frozen["origin_work_id"]))).id, + frozen["requirements"]["medium"], + frozen["release"], + frozen["descriptor"], + ): + return "Another blocklist entry still covers this release" + try: + async with db.begin_nested(): + for row, item in zip(recoveries, selections, strict=True): + item.state, item.message = "committed", "Resumed after the release was unblocked" + (await db.get(AcquisitionReservation, item.reservation_id)).state = "committed" + await db.delete(row) + await db.flush() + for item in selections: + owner = await db.get(User, item.owner_id) + await evaluate(db, owner, await db.get(AcquisitionIntent, item.intent_id)) + await db.flush() + target = await db.get(AcquisitionTarget, item.target_id, populate_existing=True) + if target.state != "wanted" or target.reservation_id != item.reservation_id: + raise HTTPException( + 409, "The request is paused, withdrawn or already satisfied" + ) + except HTTPException as error: + return str(error.detail) + attempt.recovery_observation = None + attempt.message = "Resumed after unblocking; checking the existing transfer" + (await db.get(Operation, attempt.operation_id)).message = attempt.message + await _queue_check(db, attempt) + db.add( + AuditEvent( + actor_id=user.id, + action="acquisition.download.resumed", + entity_id=attempt.id, + detail={ + "block_id": str(block.id), + "selection_ids": [str(item.id) for item in selections], + "reasons": sorted({row.reason for row in recoveries}), + }, + ) + ) + return None + + +async def unblock(db, user, block_id, *, resume=True): + """Deactivate a block and, when possible, continue the transfer it stopped. + + Returns (block, results) with one (attempt_id, message-or-None) per candidate; + None means resumed. The block is removed even when nothing can be resumed. + """ + from app.db.models import ReleaseBlock + from app.domain.work_graph import acquisition_lock + + block = await db.get(ReleaseBlock, block_id) + if not block: + raise HTTPException(404, "Blocklist entry not found") + results = [] + if resume and block.active: + for attempt_id in await resumable_attempt_ids(db, block): + results.append((attempt_id, await _resume_attempt(db, user, attempt_id, block))) + if block.active: + # Plain removal, or no candidate: same lock the plain endpoint always took. + await acquisition_lock(db, block.work_id) + block.active = False + db.add(AuditEvent(actor_id=user.id, action="acquisition.blocklist.removed", entity_id=block.id)) + return block, results + + +__all__ = ["resumable_attempt_ids", "unblock"] diff --git a/services/app/domain/release_blocklist.py b/services/app/domain/release_blocklist.py index d3f13772..523b568d 100644 --- a/services/app/domain/release_blocklist.py +++ b/services/app/domain/release_blocklist.py @@ -69,3 +69,30 @@ async def add(db, selection, reason, actor_id, *, automatic=True): db.add(row) row.reason, row.actor_id, row.automatic, row.active = reason[:300], actor_id, automatic, True return row + + +async def blocks_for(db, work_id, releases): + """Active block (or None) for each (release, medium) pair, in one query. + + Used to show blocked releases on a result page. A result carries no torrent + descriptor yet, so only the source/id key can match; blocks always record it. + """ + rows = list( + await db.scalars( + select(ReleaseBlock).where( + ReleaseBlock.work_id.in_(family_ids(work_id)), ReleaseBlock.active.is_(True) + ) + ) + ) + if not rows: + return [None] * len(releases) + result = [] + for release, medium in releases: + keys = set(release_keys(release)[1]) + result.append( + next( + (row for row in rows if row.medium == medium and keys.intersection(row.identities)), + None, + ) + ) + return result diff --git a/tests/integration/test_download_resume.py b/tests/integration/test_download_resume.py new file mode 100644 index 00000000..b3f9cbd4 --- /dev/null +++ b/tests/integration/test_download_resume.py @@ -0,0 +1,365 @@ +# ruff: noqa: F811 +"""Unblocking a release continues the interrupted transfer instead of starting over.""" + +from uuid import UUID + +import httpx +import pytest +from sqlalchemy import func, select, update + +from app.db.models import ( + AcquisitionReason, + AcquisitionReservation, + AcquisitionSelection, + AuditEvent, + DownloadAttempt, + DownloadIdentityClaim, + DownloadInspection, + DownloadRecovery, + ReleaseBlock, + User, +) +from app.domain import book_sources, download_attempts, download_recovery, release_blocklist +from app.main import create_app +from app.security import hash_password +from tests.integration.test_acquisition import catalog # noqa: F401 +from tests.integration.test_acquisition_selections import selection_route # noqa: F401 +from tests.integration.test_automatic_selection import ( # noqa: F401 + additional_candidate, + source, + start, +) +from tests.integration.test_book_sources import begin, read +from tests.integration.test_failed_download_recovery import failed_source # noqa: F401 +from tests.integration.test_mam_sources import configure as configure_mam # noqa: F401 +from tests.integration.test_mam_sources import source_http # noqa: F401 +from tests.mam_fixture import release_row, search_response + +pytestmark = pytest.mark.integration + +BLOCKLIST = "/api/acquisition/recovery/blocklist" + + +async def given_up(database, recovery_id, *, evidence=None): + """The state a recovery reaches when no replacement qualifies.""" + async with database() as db, db.begin(): + row = await db.get(DownloadRecovery, recovery_id) + if evidence: + row.evidence = {**row.evidence, **evidence} + await download_recovery.hold(db, row, "No release qualified for automatic download") + + +async def only_block(client): + blocks = (await client.get(BLOCKLIST)).json() + assert len(blocks) == 1 + return blocks[0]["id"] + + +async def counts(database): + async with database() as db: + return { + "recoveries": await db.scalar(select(func.count()).select_from(DownloadRecovery)), + "claims": await db.scalar( + select(func.count()) + .select_from(DownloadIdentityClaim) + .where(DownloadIdentityClaim.active.is_(True)) + ), + "attempts": await db.scalar(select(func.count()).select_from(DownloadAttempt)), + } + + +async def unchanged(database, failed_source): + async with database() as db: + attempt = await db.get(DownloadAttempt, failed_source["attempt_id"]) + selection = await db.get(AcquisitionSelection, failed_source["selection_id"]) + reservation = await db.get(AcquisitionReservation, selection.reservation_id) + return attempt.state, selection.state, reservation.state + + +async def test_resume_continues_the_same_transfer_through_a_wrapped_layout( + client, database, failed_source +): + await given_up(database, failed_source["recovery_id"]) + block_id = await only_block(client) + response = await client.post(f"{BLOCKLIST}/{block_id}/resume") + assert response.status_code == 200, response.text + body = response.json() + assert body["unblocked"] and body["resumed"] == 1 + assert body["attempts"][0]["attempt_id"] == str(failed_source["attempt_id"]) + async with database() as db: + assert not (await db.get(ReleaseBlock, UUID(block_id))).active + assert await db.get(DownloadRecovery, failed_source["recovery_id"]) is None + selection = await db.get(AcquisitionSelection, failed_source["selection_id"]) + assert selection.state == "committed" + assert (await db.get(AcquisitionReservation, selection.reservation_id)).state == "committed" + attempt = await db.get(DownloadAttempt, failed_source["attempt_id"]) + assert attempt.recovery_observation is None and attempt.external_may_exist + assert "Resumed" in attempt.message + audit = await db.scalar( + select(AuditEvent).where(AuditEvent.action == "acquisition.download.resumed") + ) + assert audit and audit.detail["block_id"] == block_id + # The downloader now shows a finished transfer inside a client-added wrapper folder. + mock = failed_source["client"] + mock.states = [ + state.model_copy( + update={ + "state": "uploading", + "completed": True, + "progress": 1, + "seeders": 9, + "files": [ + file.model_copy( + update={"relative_path": "Harbor/" + file.relative_path, "complete": True} + ) + for file in state.files + ], + } + ) + for state in mock.states + ] + await download_attempts.run(failed_source["attempt_id"]) + async with database() as db: + attempt = await db.get(DownloadAttempt, failed_source["attempt_id"]) + assert attempt.state == "complete", attempt.message + inspection = await db.get(DownloadInspection, attempt.inspection_id) + # The torrent has its own root folder "Harbor"; the client put one more around it. + assert inspection.relative_path == "Harbor/Harbor" + # Nothing was added to the downloader and the original claim still owns the hash. + assert mock.calls.count("submit") == 1 + assert (await counts(database)) == {"recoveries": 0, "claims": 1, "attempts": 1} + + +async def test_second_resume_is_a_harmless_repeat(client, database, failed_source): + await given_up(database, failed_source["recovery_id"]) + block_id = await only_block(client) + assert (await client.post(f"{BLOCKLIST}/{block_id}/resume")).json()["resumed"] == 1 + again = await client.post(f"{BLOCKLIST}/{block_id}/resume") + assert again.status_code == 200 + assert again.json() == { + "block_id": block_id, + "unblocked": True, + "resumed": 0, + "attempts": [], + } + assert (await unchanged(database, failed_source))[1:] == ("committed", "committed") + + +async def test_plain_removal_leaves_the_failed_transfer_alone(client, database, failed_source): + await given_up(database, failed_source["recovery_id"]) + block_id = await only_block(client) + assert (await client.delete(f"{BLOCKLIST}/{block_id}")).status_code == 204 + async with database() as db: + assert not (await db.get(ReleaseBlock, UUID(block_id))).active + assert await db.get(DownloadRecovery, failed_source["recovery_id"]) + assert await unchanged(database, failed_source) == ("held", "cancelled", "released") + + +@pytest.mark.parametrize("state", ["queued", "searching", "selecting", "retried", "complete"]) +async def test_a_failure_that_already_triggered_a_replacement_is_not_resumed( + client, database, failed_source, state +): + async with database() as db, db.begin(): + (await db.get(DownloadRecovery, failed_source["recovery_id"])).state = state + block_id = await only_block(client) + response = await client.post(f"{BLOCKLIST}/{block_id}/resume") + assert response.status_code == 200 + # Unblocking still happens; a recovery that is not "given up" is not even a candidate, + # so the transfer and its recovery are left exactly as they were. + assert response.json() == { + "block_id": block_id, + "unblocked": True, + "resumed": 0, + "attempts": [], + } + async with database() as db: + assert not (await db.get(ReleaseBlock, UUID(block_id))).active + assert (await db.get(DownloadRecovery, failed_source["recovery_id"])).state == state + assert await unchanged(database, failed_source) == ("held", "cancelled", "released") + + +async def test_a_problem_reported_by_a_person_is_never_resumed(client, database, failed_source): + await given_up(database, failed_source["recovery_id"], evidence={"reported": True}) + block_id = await only_block(client) + body = (await client.post(f"{BLOCKLIST}/{block_id}/resume")).json() + assert body["resumed"] == 0 and "reported" in body["attempts"][0]["message"] + assert await unchanged(database, failed_source) == ("held", "cancelled", "released") + + +async def test_another_block_for_the_same_release_keeps_it_blocked(client, database, failed_source): + await given_up(database, failed_source["recovery_id"]) + block_id = await only_block(client) + async with database() as db, db.begin(): + original = await db.get(ReleaseBlock, UUID(block_id)) + db.add( + ReleaseBlock( + work_id=original.work_id, + medium=original.medium, + release_key="second-entry", + source=original.source, + title=original.title, + identities=original.identities, + reason="Reported as wrong", + actor_id=original.actor_id, + automatic=False, + ) + ) + body = (await client.post(f"{BLOCKLIST}/{block_id}/resume")).json() + assert body["resumed"] == 0 and "Another blocklist entry" in body["attempts"][0]["message"] + async with database() as db: + assert not (await db.get(ReleaseBlock, UUID(block_id))).active + assert await unchanged(database, failed_source) == ("held", "cancelled", "released") + + +async def test_a_withdrawn_request_rolls_the_whole_resume_back(client, database, failed_source): + await given_up(database, failed_source["recovery_id"]) + block_id = await only_block(client) + async with database() as db, db.begin(): + await db.execute(update(AcquisitionReason).values(active=False)) + body = (await client.post(f"{BLOCKLIST}/{block_id}/resume")).json() + assert body["resumed"] == 0 + assert "paused, withdrawn or already satisfied" in body["attempts"][0]["message"] + async with database() as db: + assert await db.get(DownloadRecovery, failed_source["recovery_id"]) + assert not (await db.get(ReleaseBlock, UUID(block_id))).active + assert await unchanged(database, failed_source) == ("held", "cancelled", "released") + + +async def test_a_recovered_attempt_that_is_no_longer_held_is_not_touched( + client, database, failed_source +): + await given_up(database, failed_source["recovery_id"]) + block_id = await only_block(client) + async with database() as db, db.begin(): + (await db.get(DownloadAttempt, failed_source["attempt_id"])).state = "downloading" + body = (await client.post(f"{BLOCKLIST}/{block_id}/resume")).json() + # Not even a candidate: the screen only considers held attempts. + assert body["unblocked"] and body["resumed"] == 0 and body["attempts"] == [] + async with database() as db: + assert await db.get(DownloadRecovery, failed_source["recovery_id"]) + + +async def test_unknown_block_is_a_404(client, admin): + assert (await client.post(f"{BLOCKLIST}/{UUID(int=7)}/resume")).status_code == 404 + assert (await client.delete(f"{BLOCKLIST}/{UUID(int=7)}")).status_code == 404 + + +async def test_only_administrators_can_unblock_or_resume(client, database, failed_source): + await given_up(database, failed_source["recovery_id"]) + block_id = await only_block(client) + async with database() as db, db.begin(): + db.add( + User( + username="reader", + display_name="Reader", + role="member", + password_hash=hash_password("private member password"), + ) + ) + async with httpx.AsyncClient( + transport=httpx.ASGITransport(app=create_app()), + base_url="http://testserver", + headers={"Origin": "http://testserver"}, + ) as member: + login = await member.post( + "/api/auth/login", json={"username": "reader", "password": "private member password"} + ) + member.headers["X-CSRF-Token"] = login.json()["csrf_token"] + assert (await member.post(f"{BLOCKLIST}/{block_id}/resume")).status_code == 403 + assert (await member.delete(f"{BLOCKLIST}/{block_id}")).status_code == 403 + async with database() as db: + assert (await db.get(ReleaseBlock, UUID(block_id))).active + assert await unchanged(database, failed_source) == ("held", "cancelled", "released") + + +async def test_resumable_screen_matches_only_the_blocked_release(database, client, failed_source): + await given_up(database, failed_source["recovery_id"]) + async with database() as db: + block = (await db.scalars(select(ReleaseBlock))).one() + assert await release_blocklist.blocked( + db, + block.work_id, + block.medium, + {"source": "mam", "source_id": "501"}, + ) + from app.domain import download_resume + + assert await download_resume.resumable_attempt_ids(db, block) == [ + failed_source["attempt_id"] + ] + other = ReleaseBlock( + work_id=block.work_id, + medium=block.medium, + release_key="unrelated", + source="mam", + title="Other", + identities=["release:unrelated"], + reason="Reported", + actor_id=block.actor_id, + automatic=False, + ) + assert await download_resume.resumable_attempt_ids(db, other) == [] + block.medium = "ebook" + assert await download_resume.resumable_attempt_ids(db, block) == [] + + +async def test_sources_page_lists_the_block_on_exactly_the_blocked_release( + client, admin, database, catalog, source_http +): + await configure_mam(client) + source_http["body"] = search_response( + data=[ + release_row(id=1, title="Harbor", author_info='{"1":"Writer"}'), + release_row(id=2, title="Harbor", author_info='{"1":"Writer"}'), + ], + found=2, + total=2, + ) + saved = await begin(client, catalog) + await book_sources.run(UUID(saved["id"]), "mam") + items = { + row["release"]["source_id"]: row + for row in (await read(client, saved["id"])).json()["items"] + } + assert all(row["block"] is None for row in items.values()) + release = items["1"]["release"] + _, keys = release_blocklist.release_keys(release) + async with database() as db, db.begin(): + db.add( + ReleaseBlock( + work_id=UUID(str(catalog["work"])), + medium=release["medium"], + release_key="one", + source="mam", + title=release["title"], + identities=keys, + reason="Completed files differ from the inspected torrent", + actor_id=UUID(admin["id"]), + automatic=True, + ) + ) + items = { + row["release"]["source_id"]: row + for row in (await read(client, saved["id"])).json()["items"] + } + block = items["1"]["block"] + assert block["reason"] == "Completed files differ from the inspected torrent" + assert block["automatic"] is True and block["can_resume"] is False and block["id"] + assert items["2"]["block"] is None + # A removed block disappears from the page; a block for another format never shows. + async with database() as db, db.begin(): + row = await db.scalar(select(ReleaseBlock)) + row.medium = "ebook" if release["medium"] == "audio" else "audio" + items = { + row["release"]["source_id"]: row + for row in (await read(client, saved["id"])).json()["items"] + } + assert items["1"]["block"] is None + async with database() as db, db.begin(): + row = await db.scalar(select(ReleaseBlock)) + row.medium, row.active = release["medium"], False + items = { + row["release"]["source_id"]: row + for row in (await read(client, saved["id"])).json()["items"] + } + assert items["1"]["block"] is None diff --git a/tests/unit/test_download_layout.py b/tests/unit/test_download_layout.py new file mode 100644 index 00000000..9fe32102 --- /dev/null +++ b/tests/unit/test_download_layout.py @@ -0,0 +1,133 @@ +"""A client-added wrapper folder (qBittorrent "Subfolder" layout) is not a different transfer.""" + +from types import SimpleNamespace + +import pytest + +from app.domain.download_attempts import ( + inspection_path, + layout_wrapper, + observed_wrapper, + transfer_stage, +) + +NAME = "Book - Author.m4b" +SIZE = 492_070_294 + + +def selection(files=None, *, relative="", name=None): + files = files or {NAME: SIZE} + return SimpleNamespace( + frozen={ + "descriptor": { + "name": name or NAME, + "torrent_bytes": sum(files.values()), + "files": [{"path": p, "size_bytes": s} for p, s in files.items()], + }, + "mapping": {"relative_path": relative}, + } + ) + + +def client_state(files, *, total=None, completed=True): + return SimpleNamespace( + state="stalledUP", + completed=completed, + reported_complete=completed, + total_bytes=sum(files.values()) if total is None else total, + files=[ + SimpleNamespace(relative_path=p, size_bytes=s, priority=1, complete=completed) + for p, s in files.items() + ], + ) + + +@pytest.mark.parametrize( + "expected,actual,wrapper", + [ + ({NAME: 5}, {f"Book - Author/{NAME}": 5}, "Book - Author"), + ({"a.mp3": 1, "b.mp3": 2}, {"W/a.mp3": 1, "W/b.mp3": 2}, "W"), + ({"sub/x.mp3": 1}, {"W/sub/x.mp3": 1}, "W"), + ({"Book/a.mp3": 1}, {"Book/Book/a.mp3": 1}, "Book"), + # identical or empty: nothing was wrapped + ({NAME: 5}, {NAME: 5}, None), + ({}, {}, None), + ({NAME: 5}, {}, None), + ({}, {"W/x": 1}, None), + # not the same files + ({NAME: 5}, {f"W/{NAME}": 6}, None), + ({NAME: 5}, {"W/Other.m4b": 5}, None), + ({NAME: 5}, {f"W/{NAME}": 5, "W/extra.nfo": 1}, None), + ({"a": 1, "b": 2}, {"W/a": 1}, None), + # more than one new leading folder, or a file left outside the wrapper + ({"a": 1, "b": 2}, {"W/a": 1, "V/b": 2}, None), + ({"a": 1, "b": 2}, {"W/a": 1, "b": 2}, None), + ({NAME: 5}, {f"W/V/{NAME}": 5}, None), + # wrapper names that could point elsewhere are never trusted + ({"a": 1}, {"../a": 1}, None), + ({"a": 1}, {"./a": 1}, None), + ({"a": 1}, {"/a": 1}, None), + ({"a": 1}, {"W\\X/a": 1}, None), + ], +) +def test_layout_wrapper_requires_exactly_one_added_leading_folder(expected, actual, wrapper): + assert layout_wrapper(expected, actual) == wrapper + + +def test_wrapped_single_file_download_is_the_inspected_torrent(): + chosen = selection() + wrapped = {f"Book - Author/{NAME}": SIZE} + stage, message = transfer_stage(chosen, client_state(wrapped)) + assert stage == "complete", message + assert transfer_stage(chosen, client_state({NAME: SIZE}))[0] == "complete" + + +@pytest.mark.parametrize( + "files,total", + [ + ({f"Book - Author/{NAME}": SIZE - 1}, None), # truncated or different file + ({"Book - Author/Other.m4b": SIZE}, None), # renamed file + ({f"Book - Author/{NAME}": SIZE, "Book - Author/x.nfo": 3}, None), # extra file + ({f"Book - Author/{NAME}": SIZE}, SIZE + 1), # client reports another total + ], +) +def test_wrapper_does_not_hide_real_differences(files, total): + stage, message = transfer_stage(selection(), client_state(files, total=total)) + assert stage == "held" and "differ" in message + + +def test_wrapped_transfer_still_waits_for_completion(): + wrapped = client_state({f"Book - Author/{NAME}": SIZE}, completed=False) + assert transfer_stage(selection(), wrapped)[0] == "downloading" + + +@pytest.mark.parametrize( + "relative,wrapper,expected", + [ + ("", None, NAME), + ("", "Book - Author", f"Book - Author/{NAME}"), + ("dl/books", "Book - Author", f"dl/books/Book - Author/{NAME}"), + (".", None, NAME), + ("dl", None, f"dl/{NAME}"), + ], +) +def test_inspection_path_includes_the_observed_wrapper(relative, wrapper, expected): + assert inspection_path(selection(relative=relative), wrapper) == expected + + +def test_inspection_path_for_a_rooted_torrent_keeps_its_root_inside_a_wrapper(): + chosen = selection({"Pack/a.mp3": 1, "Pack/b.mp3": 2}, name="Pack") + assert inspection_path(chosen) == "Pack" + assert inspection_path(chosen, "W") == "W/Pack" + + +def test_observed_wrapper_reads_the_last_client_observation(): + chosen = selection() + wrapped = {"files": [{"relative_path": f"Book - Author/{NAME}", "size_bytes": SIZE}]} + assert observed_wrapper(chosen, wrapped) == "Book - Author" + assert ( + observed_wrapper(chosen, {"files": [{"relative_path": NAME, "size_bytes": SIZE}]}) is None + ) + assert observed_wrapper(chosen, {"files": []}) is None + assert observed_wrapper(chosen, {}) is None + assert observed_wrapper(chosen, None) is None