diff --git a/crates/arr-api/src/subtitles.rs b/crates/arr-api/src/subtitles.rs index 66e04ce..5741b71 100644 --- a/crates/arr-api/src/subtitles.rs +++ b/crates/arr-api/src/subtitles.rs @@ -713,14 +713,13 @@ async fn missing_for( subtitles: &[Subtitle], wanted: &[String], ) -> Result, ApiError> { - let satisfied: BTreeSet<&str> = subtitles - .iter() - .filter(|subtitle| !subtitle.forced) - .map(|subtitle| subtitle.language.as_str()) - .collect(); let gaps: Vec<&String> = wanted .iter() - .filter(|language| !satisfied.contains(language.as_str())) + .filter(|language| { + !subtitles.iter().any(|subtitle| { + !subtitle.forced && arr_core::subs::satisfies(language, &subtitle.language) + }) + }) .collect(); if gaps.is_empty() { return Ok(Vec::new()); @@ -3183,6 +3182,24 @@ mod tests { assert_eq!(missing[0]["language"], "en"); } + /// §15: pt-BR satisfies a pt-PT want, so an embedded pt-BR track drops + /// pt-PT from `missing` — the same rule the daemon's gap loop applies. + #[tokio::test] + async fn status_counts_pt_br_as_satisfying_pt_pt() { + let fixture = stub_application().await; + arr_db::subtitles::record_file( + &fixture.pool, + &arr_db::NewSubtitleFile::embedded(1, "pt-BR"), + ) + .await + .expect("embedded"); + + let statuses = status(&format!("{}/api/movies/1/subtitles/status", fixture.base)).await; + let missing = statuses[0]["missing"].as_array().expect("missing"); + assert_eq!(missing.len(), 1, "{missing:?}"); + assert_eq!(missing[0]["language"], "en"); + } + /// #201: `no_candidates`, `capped` and `failed` carry the attempt row's /// own state, and `failed` also carries `last_failure` as `detail`. #[tokio::test] diff --git a/crates/arr-core/src/subs.rs b/crates/arr-core/src/subs.rs index 102fbf6..2d9cfaf 100644 --- a/crates/arr-core/src/subs.rs +++ b/crates/arr-core/src/subs.rs @@ -157,6 +157,16 @@ pub fn rank<'a>( ranked } +/// Whether a subtitle in `have` settles a want for `wanted` (§15: pt-PT +/// preferred, pt-BR always fine). An unverified Portuguese embedded track +/// counts too: it satisfies viewing even when no signal resolved the +/// flavour. The daemon's gap loop and the API's `missing` list both read +/// this, so they cannot disagree about what is satisfied. +#[must_use] +pub fn satisfies(wanted: &str, have: &str) -> bool { + have == wanted || (wanted == "pt-PT" && matches!(have, "pt-BR" | "por-unverified")) +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/arr-daemon/src/subtitles.rs b/crates/arr-daemon/src/subtitles.rs index 9ab753b..0c93c52 100644 --- a/crates/arr-daemon/src/subtitles.rs +++ b/crates/arr-daemon/src/subtitles.rs @@ -24,7 +24,7 @@ use std::collections::{BTreeMap, BTreeSet, HashSet}; use std::path::{Path, PathBuf}; use std::sync::Arc; -use arr_core::subs::{rank, SubtitleTarget, SubtitleVerdict}; +use arr_core::subs::{rank, satisfies, SubtitleTarget, SubtitleVerdict}; use arr_core::{layout, Language}; use arr_db::subtitle_budget::{self as budget, BudgetKind}; use arr_db::subtitles::{self as db, PendingSubtitle, SubtitleOrigin, SubtitleState}; @@ -112,14 +112,6 @@ pub(crate) async fn load_settings(database: &Db) -> Result bool { - have == wanted || (wanted == "pt-PT" && matches!(have, "pt-BR" | "por-unverified")) -} - /// The languages worth fetching when `wanted` is the gap, preferred first. fn acceptable(wanted: &Language) -> Vec { match wanted { diff --git a/web/src/subs-panel.tsx b/web/src/subs-panel.tsx index 7f019a0..14bf8c7 100644 --- a/web/src/subs-panel.tsx +++ b/web/src/subs-panel.tsx @@ -26,6 +26,7 @@ import { type SubtitleCandidate, type SubtitleSearchResults, type SubtitleStatus, + satisfiesWanted, searchSubtitles, subtitleChipLabel, subtitleRuleLabel, @@ -84,7 +85,8 @@ function SubtitleChip({ return ( satisfiesWanted(language, subtitle.language))) ? "eligible" : undefined } diff --git a/web/src/subtitles.ts b/web/src/subtitles.ts index ff3f117..65fbca7 100644 --- a/web/src/subtitles.ts +++ b/web/src/subtitles.ts @@ -538,6 +538,15 @@ const SUMMARY_SHOWN = 2; * every non-forced language shows then, which is a superset rather than a * wrong answer. */ +/** + * Whether a subtitle in `have` settles a want for `wanted` — §15's pt-PT + * preferred, pt-BR accepted. Mirrors `arr_core::subs::satisfies`, which the + * server's `missing` list is computed with. + */ +export function satisfiesWanted(wanted: string, have: string): boolean { + return have === wanted || (wanted === "pt-PT" && (have === "pt-BR" || have === "por-unverified")); +} + export function wantedSubtitles(subtitles: Subtitle[], wanted: string[] | null): Subtitle[] { const present = subtitles.filter((subtitle) => !subtitle.forced); if (wanted === null) { @@ -545,7 +554,10 @@ export function wantedSubtitles(subtitles: Subtitle[], wanted: string[] | null): } // the operator's order, not the file's: two files with the same languages // must read identically at a glance - return wanted.flatMap((language) => present.filter((subtitle) => subtitle.language === language)); + const ordered = wanted.flatMap((language) => + present.filter((subtitle) => satisfiesWanted(language, subtitle.language)), + ); + return [...new Set(ordered)]; } export function subtitleSummary(status: SubtitleStatus, wanted: string[] | null): SubtitleSummary {