Skip to main content

radio_cli/speech/
models.rs

1use crate::{
2    config::Config,
3    error::{DomainError, ErrorCode},
4};
5use anyhow::{Context, Result, bail, ensure};
6use clap::Subcommand;
7use fs2::FileExt;
8use serde::{Deserialize, Serialize};
9use sha2::{Digest, Sha256};
10use std::{
11    collections::HashMap,
12    fs::{self, File, OpenOptions},
13    io::{Read, Write},
14    os::unix::fs::MetadataExt,
15    path::{Path, PathBuf},
16    process::Command,
17    sync::atomic::{AtomicBool, Ordering},
18    time::{Duration, Instant, SystemTime, UNIX_EPOCH},
19};
20
21#[derive(Subcommand)]
22pub enum Action {
23    /// Install an isolated Python 3.12 speech runtime using uv.
24    Setup,
25    /// Check the runtime and selected model without downloading.
26    Doctor,
27    Models {
28        #[command(subcommand)]
29        action: ModelAction,
30    },
31}
32
33#[derive(Subcommand)]
34pub enum ModelAction {
35    List,
36    Install { id: String },
37    Verify { id: String },
38    Remove { id: String },
39}
40
41#[derive(Debug, Deserialize, Serialize)]
42pub(super) struct Model {
43    pub id: String,
44    pub license: String,
45    pub license_url: String,
46    pub files: Vec<ModelFile>,
47}
48
49#[derive(Debug, Clone, Deserialize, Serialize)]
50pub(super) struct ModelFile {
51    pub path: String,
52    pub url: String,
53    pub size: u64,
54    pub sha256: String,
55}
56
57pub(super) fn manifest() -> Vec<Model> {
58    serde_json::from_str(include_str!("models.json")).expect("embedded speech manifest")
59}
60
61fn required_models(id: &str) -> Result<[Model; 2]> {
62    required_from(manifest(), id)
63}
64
65pub(crate) fn validate_model(id: &str) -> Result<()> {
66    required_models(id).map(|_| ())
67}
68
69fn required_from(models: Vec<Model>, id: &str) -> Result<[Model; 2]> {
70    let mut selected = None;
71    let mut vad = None;
72    for model in models {
73        if model.id == "silero-vad" {
74            vad = Some(model);
75        } else if model.id == id {
76            selected = Some(model);
77        }
78    }
79    Ok([
80        selected.with_context(|| DomainError::new(ErrorCode::SubtitleModel).with("id", id))?,
81        vad.context("embedded speech manifest has no silero-vad")?,
82    ])
83}
84
85pub(super) fn root(config: &Config) -> PathBuf {
86    config.data_dir.join("speech")
87}
88pub(super) fn runtime(config: &Config) -> PathBuf {
89    root(config).join("runtime-2.0.1-v1")
90}
91pub(super) fn python(config: &Config) -> PathBuf {
92    runtime(config).join("bin/python")
93}
94
95fn supports_runtime(os: &str, arch: &str, glibc: bool) -> bool {
96    matches!((os, arch), ("macos", "x86_64" | "aarch64"))
97        || (os == "linux" && arch == "x86_64" && glibc)
98}
99
100fn ensure_runtime_platform() -> Result<()> {
101    // A statically linked musl radio binary can still use glibc Python on a
102    // glibc host. Check the host loader rather than rejecting that binary.
103    let glibc = cfg!(target_env = "gnu")
104        || [
105            "/lib64/ld-linux-x86-64.so.2",
106            "/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2",
107        ]
108        .iter()
109        .any(|path| Path::new(path).exists());
110    ensure!(
111        supports_runtime(std::env::consts::OS, std::env::consts::ARCH, glibc),
112        DomainError::new(ErrorCode::SubtitlePlatform).with(
113            "platform",
114            format!("{}-{}", std::env::consts::OS, std::env::consts::ARCH)
115        )
116    );
117    Ok(())
118}
119pub(super) fn model_dir(config: &Config, id: &str) -> PathBuf {
120    root(config).join("models").join(id)
121}
122
123pub(super) struct ResourceLock(File);
124impl Drop for ResourceLock {
125    fn drop(&mut self) {
126        let _ = FileExt::unlock(&self.0);
127    }
128}
129
130pub(super) fn lock(config: &Config, exclusive: bool) -> Result<ResourceLock> {
131    let path = root(config).join("resources.lock");
132    let create = || -> Result<File> {
133        fs::create_dir_all(root(config))?;
134        Ok(OpenOptions::new()
135            .create(true)
136            .truncate(false)
137            .read(true)
138            .write(true)
139            .open(&path)?)
140    };
141    let file = if exclusive {
142        create()
143    } else {
144        match File::open(&path) {
145            Ok(file) => Ok(file),
146            Err(error) if error.kind() == std::io::ErrorKind::NotFound => create(),
147            Err(error) => Err(error.into()),
148        }
149    }
150    .with_context(|| format!("cannot open speech resource lock {}", path.display()))?;
151    if exclusive {
152        FileExt::try_lock_exclusive(&file)
153    } else {
154        FileExt::try_lock_shared(&file)
155    }
156    .context(DomainError::new(ErrorCode::SubtitleBusy))?;
157    Ok(ResourceLock(file))
158}
159
160fn cancelled(stop: &AtomicBool) -> Result<()> {
161    ensure!(
162        !stop.load(Ordering::Relaxed),
163        DomainError::new(ErrorCode::Cancelled)
164    );
165    Ok(())
166}
167
168fn digest(path: &Path, stop: &AtomicBool) -> Result<String> {
169    let mut file = File::open(path)?;
170    let mut hash = Sha256::new();
171    let mut buf = [0; 65536];
172    loop {
173        cancelled(stop)?;
174        let n = file.read(&mut buf)?;
175        if n == 0 {
176            break;
177        }
178        hash.update(&buf[..n]);
179    }
180    Ok(format!("{:x}", hash.finalize()))
181}
182
183fn present(path: &Path, file: &ModelFile) -> Result<bool> {
184    match fs::symlink_metadata(path) {
185        Ok(s) => Ok(s.is_file() && s.len() == file.size),
186        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(false),
187        Err(e) => Err(e).with_context(|| format!("cannot inspect {}", path.display())),
188    }
189}
190
191fn valid(path: &Path, file: &ModelFile, stop: &AtomicBool) -> Result<bool> {
192    cancelled(stop)?;
193    Ok(present(path, file)? && digest(path, stop)? == file.sha256)
194}
195
196#[derive(Debug, PartialEq, Eq)]
197struct FileStamp {
198    device: u64,
199    inode: u64,
200    size: u64,
201    mode: u32,
202    modified: (i64, i64),
203    changed: (i64, i64),
204}
205
206impl FileStamp {
207    fn read(path: &Path) -> Result<Option<Self>> {
208        match fs::symlink_metadata(path) {
209            Ok(meta) if meta.is_file() => Ok(Some(Self {
210                device: meta.dev(),
211                inode: meta.ino(),
212                size: meta.len(),
213                mode: meta.mode(),
214                modified: (meta.mtime(), meta.mtime_nsec()),
215                changed: (meta.ctime(), meta.ctime_nsec()),
216            })),
217            Ok(_) => Ok(None),
218            Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(None),
219            Err(error) => Err(error).with_context(|| format!("cannot inspect {}", path.display())),
220        }
221    }
222
223    fn cacheable_at(&self, started: SystemTime) -> bool {
224        // Filesystems can reuse a timestamp for successive writes. Do not
225        // cache a hash until ctime is beyond that potentially racy second.
226        started
227            .duration_since(UNIX_EPOCH)
228            .is_ok_and(|now| i128::from(self.changed.0) + 1 < i128::from(now.as_secs()))
229    }
230}
231
232/// Successful hashes are reusable only within this controller's lifetime.
233/// Never trust persisted cache data or populate entries from metadata alone.
234#[derive(Default)]
235pub(super) struct VerificationCache {
236    files: HashMap<PathBuf, (FileStamp, String)>,
237    #[cfg(test)]
238    hashes: usize,
239}
240
241impl VerificationCache {
242    fn valid(&mut self, path: &Path, file: &ModelFile, stop: &AtomicBool) -> Result<bool> {
243        self.valid_at(path, file, stop, SystemTime::now())
244    }
245
246    fn valid_at(
247        &mut self,
248        path: &Path,
249        file: &ModelFile,
250        stop: &AtomicBool,
251        started: SystemTime,
252    ) -> Result<bool> {
253        cancelled(stop)?;
254        let stamp = FileStamp::read(path)?;
255        if let Some(stamp) = &stamp
256            && stamp.size == file.size
257            && self
258                .files
259                .get(path)
260                .is_some_and(|(old, hash)| old == stamp && hash == &file.sha256)
261        {
262            return Ok(true);
263        }
264        self.files.remove(path);
265        let Some(stamp) = stamp.filter(|stamp| stamp.size == file.size) else {
266            return Ok(false);
267        };
268        #[cfg(test)]
269        {
270            self.hashes += 1;
271        }
272        if digest(path, stop)? != file.sha256 || FileStamp::read(path)?.as_ref() != Some(&stamp) {
273            return Ok(false);
274        }
275        if stamp.cacheable_at(started) {
276            self.files
277                .insert(path.to_owned(), (stamp, file.sha256.clone()));
278        }
279        Ok(true)
280    }
281
282    pub(super) fn verify(&mut self, config: &Config, id: &str, stop: &AtomicBool) -> Result<()> {
283        verify_with(config, id, |path, file| self.valid(path, file, stop))
284    }
285}
286
287pub(super) fn verify(config: &Config, id: &str, stop: &AtomicBool) -> Result<()> {
288    verify_with(config, id, |path, file| valid(path, file, stop))
289}
290
291fn verify_with(
292    config: &Config,
293    id: &str,
294    mut valid_file: impl FnMut(&Path, &ModelFile) -> Result<bool>,
295) -> Result<()> {
296    for m in required_models(id)? {
297        for file in &m.files {
298            let path = model_dir(config, &m.id).join(&file.path);
299            ensure!(
300                valid_file(&path, file)?,
301                DomainError::new(ErrorCode::SubtitleIntegrity)
302                    .with("id", id)
303                    .with("path", path.display())
304            );
305        }
306    }
307    Ok(())
308}
309
310/// A Range response is appended only when its complete range matches our file.
311fn download(
312    client: &reqwest::Client,
313    id: &str,
314    path: &Path,
315    file: &ModelFile,
316    stop: &AtomicBool,
317) -> Result<()> {
318    let runtime = tokio::runtime::Builder::new_current_thread()
319        .enable_all()
320        .build()?;
321    let _enter = runtime.enter();
322    if valid(path, file, stop)? {
323        return Ok(());
324    }
325    fs::create_dir_all(path.parent().unwrap())?;
326    let partial = partial_path(path);
327    let mut have = partial_len(&partial)?;
328    if have == file.size && valid(&partial, file, stop)? {
329        fs::rename(partial, path)?;
330        return Ok(());
331    }
332    if have >= file.size {
333        have = 0;
334    }
335    cancelled(stop)?;
336    let mut request = client
337        .get(&file.url)
338        .header(reqwest::header::ACCEPT_ENCODING, "identity");
339    if have > 0 {
340        request = request.header(reqwest::header::RANGE, format!("bytes={have}-"));
341    }
342    let mut response = runtime
343        .block_on(network(request.send(), stop))??
344        .error_for_status()?;
345    if response.status() == reqwest::StatusCode::PARTIAL_CONTENT {
346        let expected = format!("bytes {have}-{}/{}", file.size - 1, file.size);
347        ensure!(
348            response
349                .headers()
350                .get(reqwest::header::CONTENT_RANGE)
351                .and_then(|v| v.to_str().ok())
352                == Some(expected.as_str()),
353            "invalid Content-Range"
354        );
355    } else {
356        ensure!(
357            response.status() == reqwest::StatusCode::OK,
358            "unexpected model download status"
359        );
360        have = 0;
361    }
362    let mut output = OpenOptions::new()
363        .create(true)
364        .write(true)
365        .truncate(have == 0)
366        .append(have > 0)
367        .open(&partial)?;
368    // A server may ignore Range, so recheck after any truncation as well as
369    // checking the complete installation plan before the first request.
370    check_space(
371        file.size - have,
372        fs2::available_space(path.parent().unwrap())?,
373    )?;
374    let mut last = Instant::now() - Duration::from_secs(2);
375    loop {
376        cancelled(stop)?;
377        let Some(bytes) = runtime.block_on(network(response.chunk(), stop))?? else {
378            break;
379        };
380        let n = bytes.len();
381        ensure!(
382            have + n as u64 <= file.size,
383            "model download exceeds manifest size"
384        );
385        output.write_all(&bytes)?;
386        have += n as u64;
387        if last.elapsed() >= Duration::from_secs(1) {
388            eprintln!(
389                "{}",
390                crate::t!("subtitle_install_progress", "file" => &file.path, "done" => have, "total" => file.size)
391            );
392            last = Instant::now();
393        }
394    }
395    output.sync_all()?;
396    drop(output);
397    if have != file.size {
398        bail!("model download truncated: {have}/{}", file.size);
399    }
400    if !valid(&partial, file, stop)? {
401        return reject_corrupt_download(id, path);
402    }
403    fs::rename(partial, path)?;
404    Ok(())
405}
406
407fn reject_corrupt_download(id: &str, path: &Path) -> Result<()> {
408    let partial = partial_path(path);
409    let integrity = DomainError::new(ErrorCode::SubtitleIntegrity)
410        .with("id", id)
411        .with("path", path.display());
412    match fs::remove_file(&partial) {
413        Ok(()) => {}
414        Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
415        Err(error) => {
416            return Err(anyhow::Error::new(error)
417                .context(format!(
418                    "cannot remove corrupt download {}",
419                    partial.display()
420                ))
421                .context(integrity));
422        }
423    }
424    Err(integrity.into())
425}
426
427fn partial_path(path: &Path) -> PathBuf {
428    path.with_extension(format!(
429        "{}.part",
430        path.extension().unwrap_or_default().to_string_lossy()
431    ))
432}
433
434fn partial_len(path: &Path) -> Result<u64> {
435    match fs::metadata(path) {
436        Ok(meta) => Ok(meta.len()),
437        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(0),
438        Err(e) => Err(e.into()),
439    }
440}
441
442fn sources(config: &Config, id: &str, file: &ModelFile) -> Result<Vec<String>> {
443    let mut urls = Vec::new();
444    if let Some(base) = &config.settings.subtitles.model_mirror {
445        let mut url = reqwest::Url::parse(base)?;
446        url.path_segments_mut()
447            .map_err(|_| DomainError::new(ErrorCode::SubtitleSettings))?
448            .pop_if_empty()
449            .push(id)
450            .extend(file.path.split('/'));
451        urls.push(url.into());
452    }
453    urls.push(file.url.clone());
454    Ok(urls)
455}
456
457fn install_file(
458    config: &Config,
459    client: &reqwest::Client,
460    id: &str,
461    file: &ModelFile,
462    stop: &AtomicBool,
463) -> Result<()> {
464    let path = model_dir(config, id).join(&file.path);
465    let mut last_error = None;
466    let mut failures = Vec::new();
467    for url in sources(config, id, file)? {
468        let source = ModelFile {
469            url,
470            ..file.clone()
471        };
472        let resumed = partial_len(&partial_path(&path))? > 0;
473        let mut result = download(client, id, &path, &source, stop);
474        if resumed
475            && source.url == file.url
476            && result
477                .as_ref()
478                .is_err_and(|e| e.downcast_ref::<std::io::Error>().is_none())
479            && result.as_ref().is_err_and(|e| {
480                e.downcast_ref::<DomainError>()
481                    .is_some_and(|e| e.code == ErrorCode::SubtitleIntegrity)
482            })
483        {
484            // A failed mirror may have left a corrupt prefix. Only retry the
485            // canonical source from scratch after a resumed digest mismatch;
486            // a mirror mismatch should fall back to the canonical source.
487            result = download(client, id, &path, &source, stop);
488        }
489        match result {
490            Ok(()) => return Ok(()),
491            Err(error) => {
492                cancelled(stop)?;
493                failures.push(crate::redact::urls(&format!("{}: {error:#}", source.url)));
494                // A second server cannot fix a local filesystem failure.
495                if error.downcast_ref::<std::io::Error>().is_some()
496                    || error
497                        .downcast_ref::<DomainError>()
498                        .is_some_and(|e| e.code == ErrorCode::SubtitleSpace)
499                {
500                    return Err(error.context(failures.join("; ")));
501                }
502                last_error = Some(error);
503            }
504        }
505    }
506    Err(last_error
507        .expect("canonical source")
508        .context(failures.join("; ")))
509}
510
511fn pending_bytes(config: &Config, models: &[Model], stop: &AtomicBool) -> Result<u64> {
512    let mut pending = 0_u64;
513    for model in models {
514        for file in &model.files {
515            let path = model_dir(config, &model.id).join(&file.path);
516            if valid(&path, file, stop)? {
517                continue;
518            }
519            let partial = partial_path(&path);
520            let mut have = partial_len(&partial)?;
521            if have >= file.size {
522                if valid(&partial, file, stop)? {
523                    continue;
524                }
525                have = 0;
526            }
527            pending = pending
528                .checked_add(file.size - have)
529                .context("model installation size overflow")?;
530        }
531    }
532    Ok(pending)
533}
534
535fn check_space(pending: u64, available: u64) -> Result<()> {
536    if pending > 0 {
537        let required = pending.saturating_add(64 * 1024 * 1024);
538        ensure!(
539            available >= required,
540            DomainError::new(ErrorCode::SubtitleSpace)
541                .with("available", available)
542                .with("required", required)
543        );
544    }
545    Ok(())
546}
547
548fn model_list(config: &Config) -> Result<serde_json::Value> {
549    let _lock = lock(config, false)?;
550    let mut rows = Vec::new();
551    for model in manifest().into_iter().filter(|m| m.id != "silero-vad") {
552        let mut bytes = 0_u64;
553        let mut all_present = true;
554        for required in required_models(&model.id)? {
555            for file in &required.files {
556                bytes = bytes
557                    .checked_add(file.size)
558                    .context("model size overflow")?;
559                all_present &= present(&model_dir(config, &required.id).join(&file.path), file)?;
560            }
561        }
562        rows.push(serde_json::json!({"id":model.id,"bytes":bytes,"license":model.license,"license_url":model.license_url,"present":all_present}));
563    }
564    Ok(serde_json::json!(rows))
565}
566
567async fn network<T>(future: impl std::future::Future<Output = T>, stop: &AtomicBool) -> Result<T> {
568    let timeout = tokio::time::sleep(Duration::from_secs(20));
569    tokio::pin!(future, timeout);
570    loop {
571        cancelled(stop)?;
572        tokio::select! {
573            result = &mut future => return Ok(result),
574            _ = &mut timeout => bail!("model download stalled"),
575            _ = tokio::time::sleep(Duration::from_millis(100)) => {}
576        }
577    }
578}
579
580pub(super) fn runtime_check(config: &Config, stop: &std::sync::Arc<AtomicBool>) -> Result<()> {
581    cancelled(stop)?;
582    ensure_runtime_platform()?;
583    let result = crate::process::output(Command::new(python(config)).args(["-I", "-c", "import sherpa_onnx,numpy; assert sherpa_onnx.__version__ == '2.0.1'; assert numpy.__version__ == '2.2.6'"]), Duration::from_secs(15), stop);
584    cancelled(stop)?;
585    result.context(DomainError::new(ErrorCode::SubtitleRuntime))?;
586    Ok(())
587}
588
589pub fn manage(
590    config: &Config,
591    action: Action,
592    stop: &std::sync::Arc<AtomicBool>,
593) -> Result<serde_json::Value> {
594    use serde_json::json;
595    match action {
596        Action::Setup => {
597            cancelled(stop)?;
598            ensure_runtime_platform()?;
599            let _lock = lock(config, true)?;
600            if runtime_check(config, stop).is_err() {
601                cancelled(stop)?;
602                let requirements = root(config).join("requirements-2.0.1.lock");
603                fs::write(&requirements, include_str!("requirements.lock"))?;
604                let run = |command: &mut Command| -> Result<()> {
605                    command.env_remove("UV_EXCLUDE_NEWER");
606                    let bytes = crate::process::output(command, Duration::from_secs(600), stop)?;
607                    eprintln!("{}", String::from_utf8_lossy(&bytes));
608                    Ok(())
609                };
610                run(Command::new(&config.settings.subtitles.uv)
611                    .args([
612                        "--no-config",
613                        "venv",
614                        "--python",
615                        "3.12",
616                        "--managed-python",
617                        "--clear",
618                    ])
619                    .arg(runtime(config)))?;
620                run(Command::new(&config.settings.subtitles.uv)
621                    .args([
622                        "--no-config",
623                        "pip",
624                        "sync",
625                        "--require-hashes",
626                        "--only-binary",
627                        ":all:",
628                        "--default-index",
629                        "https://pypi.org/simple/",
630                        "--python",
631                    ])
632                    .arg(python(config))
633                    .arg(requirements))?;
634                runtime_check(config, stop)?;
635            }
636            Ok(json!({"runtime":runtime(config),"ready":true}))
637        }
638        Action::Doctor => {
639            let _lock = lock(config, false)?;
640            runtime_check(config, stop)?;
641            verify(config, &config.settings.subtitles.model, stop)?;
642            crate::process::output(
643                Command::new(&config.settings.ffmpeg).arg("-version"),
644                Duration::from_secs(5),
645                stop,
646            )?;
647            Ok(
648                json!({"ready":true,"runtime":runtime(config),"model":config.settings.subtitles.model}),
649            )
650        }
651        Action::Models {
652            action: ModelAction::List,
653        } => model_list(config),
654        Action::Models { action } => {
655            if matches!(action, ModelAction::Install { .. }) {
656                cancelled(stop)?;
657                ensure_runtime_platform()?;
658            }
659            let id = match &action {
660                ModelAction::Install { id }
661                | ModelAction::Verify { id }
662                | ModelAction::Remove { id } => id,
663                ModelAction::List => unreachable!(),
664            };
665            let required = required_models(id)?;
666            let _lock = lock(config, !matches!(action, ModelAction::Verify { .. }))?;
667            match &action {
668                ModelAction::Install { .. } => {
669                    let pending = pending_bytes(config, &required, stop)?;
670                    check_space(pending, fs2::available_space(root(config))?)?;
671                    eprintln!(
672                        "{}",
673                        crate::t!("subtitle_install_plan", "id" => id, "bytes" => pending)
674                    );
675                    let client = reqwest::Client::builder()
676                        .user_agent(crate::USER_AGENT)
677                        .no_gzip()
678                        .connect_timeout(Duration::from_secs(config.settings.connect_timeout_secs))
679                        // network() bounds each wait and polls cancellation;
680                        // a progressing large download has no total deadline.
681                        .build()?;
682                    for m in required {
683                        for f in &m.files {
684                            install_file(config, &client, &m.id, f, stop)?;
685                        }
686                    }
687                    verify(config, id, stop)?;
688                }
689                ModelAction::Verify { .. } => verify(config, id, stop)?,
690                ModelAction::Remove { .. } => {
691                    let dir = model_dir(config, id);
692                    if dir.exists() {
693                        fs::remove_dir_all(dir)?;
694                    }
695                }
696                ModelAction::List => unreachable!(),
697            }
698            Ok(json!({"id":id,"removed":matches!(action, ModelAction::Remove{..})}))
699        }
700    }
701}
702
703#[cfg(test)]
704mod tests {
705    use super::*;
706    use crate::test_support::{Server, response};
707
708    fn verification_fixture(directory: &Path) -> (PathBuf, ModelFile) {
709        let file = ModelFile {
710            path: "model.onnx".into(),
711            url: "http://127.0.0.1:1".into(),
712            size: 6,
713            sha256: format!("{:x}", Sha256::digest(b"abcdef")),
714        };
715        let path = directory.join(&file.path);
716        fs::write(&path, b"abcdef").unwrap();
717        let stamp = FileStamp::read(&path).unwrap().unwrap();
718        let deadline = Instant::now() + Duration::from_secs(5);
719        while !stamp.cacheable_at(SystemTime::now()) {
720            assert!(Instant::now() < deadline, "file timestamp did not age");
721            std::thread::sleep(Duration::from_millis(10));
722        }
723        (path, file)
724    }
725
726    #[test]
727    fn verification_cache_does_not_trust_recent_timestamps() {
728        let dir = tempfile::tempdir().unwrap();
729        let (path, file) = verification_fixture(dir.path());
730        // A fresh write must not be cached even when its digest is correct.
731        fs::write(&path, b"abcdef").unwrap();
732        let stamp = FileStamp::read(&path).unwrap().unwrap();
733        let second = u64::try_from(stamp.changed.0).unwrap();
734        for offset in [0, 1, 2] {
735            assert_eq!(
736                stamp.cacheable_at(UNIX_EPOCH + Duration::from_secs(second + offset)),
737                offset == 2
738            );
739        }
740        let mut cache = VerificationCache::default();
741        assert!(
742            cache
743                .valid_at(
744                    &path,
745                    &file,
746                    &AtomicBool::new(false),
747                    UNIX_EPOCH + Duration::from_secs(second)
748                )
749                .unwrap()
750        );
751        assert!(cache.files.is_empty());
752    }
753
754    #[test]
755    fn verification_cache_reuses_hashes_but_checks_manifest_and_cancellation() {
756        let dir = tempfile::tempdir().unwrap();
757        let (path, file) = verification_fixture(dir.path());
758        let stop = AtomicBool::new(false);
759        let mut cache = VerificationCache::default();
760        assert!(cache.valid(&path, &file, &stop).unwrap());
761        assert!(cache.valid(&path, &file, &stop).unwrap());
762        assert_eq!(cache.hashes, 1);
763
764        stop.store(true, Ordering::Relaxed);
765        let error = cache.valid(&path, &file, &stop).unwrap_err();
766        assert_eq!(
767            error.downcast_ref::<DomainError>().unwrap().code,
768            ErrorCode::Cancelled
769        );
770        stop.store(false, Ordering::Relaxed);
771        let other = ModelFile {
772            sha256: format!("{:x}", Sha256::digest(b"xxxxxx")),
773            ..file.clone()
774        };
775        assert!(!cache.valid(&path, &other, &stop).unwrap());
776        assert!(cache.files.is_empty());
777        assert_eq!(cache.hashes, 2);
778        assert!(cache.valid(&path, &file, &stop).unwrap());
779        assert_eq!(cache.hashes, 3);
780
781        let mut fresh = VerificationCache::default();
782        assert!(fresh.valid(&path, &file, &stop).unwrap());
783        assert_eq!(fresh.hashes, 1);
784    }
785
786    #[test]
787    fn verification_cache_rehashes_changed_files_even_with_restored_mtime() {
788        use std::fs::FileTimes;
789        for replacement in [false, true] {
790            let dir = tempfile::tempdir().unwrap();
791            let (path, file) = verification_fixture(dir.path());
792            let stop = AtomicBool::new(false);
793            let mut cache = VerificationCache::default();
794            assert!(cache.valid(&path, &file, &stop).unwrap());
795            let original = fs::metadata(&path).unwrap();
796            if replacement {
797                let other = dir.path().join("replacement");
798                fs::write(&other, b"xxxxxx").unwrap();
799                fs::rename(other, &path).unwrap();
800            } else {
801                fs::write(&path, b"xxxxxx").unwrap();
802            }
803            File::options()
804                .write(true)
805                .open(&path)
806                .unwrap()
807                .set_times(FileTimes::new().set_modified(original.modified().unwrap()))
808                .unwrap();
809            let changed = fs::metadata(&path).unwrap();
810            assert_eq!(changed.modified().unwrap(), original.modified().unwrap());
811            assert_eq!(changed.len(), original.len());
812            assert!(!cache.valid(&path, &file, &stop).unwrap());
813            assert_eq!(cache.hashes, 2);
814            assert!(cache.files.is_empty());
815            // A failed verification must not become a reusable result.
816            assert!(!cache.valid(&path, &file, &stop).unwrap());
817            assert_eq!(cache.hashes, 3);
818        }
819    }
820
821    #[test]
822    fn verification_cache_rejects_missing_resized_and_nonregular_files() {
823        use std::os::unix::fs::symlink;
824        for mode in 0..4 {
825            let dir = tempfile::tempdir().unwrap();
826            let (path, file) = verification_fixture(dir.path());
827            let stop = AtomicBool::new(false);
828            let mut cache = VerificationCache::default();
829            assert!(cache.valid(&path, &file, &stop).unwrap());
830            fs::remove_file(&path).unwrap();
831            match mode {
832                0 => {}
833                1 => fs::write(&path, b"short").unwrap(),
834                2 => fs::create_dir(&path).unwrap(),
835                _ => {
836                    let target = dir.path().join("target");
837                    fs::write(&target, b"abcdef").unwrap();
838                    symlink(target, &path).unwrap();
839                }
840            }
841            assert!(!cache.valid(&path, &file, &stop).unwrap());
842            assert!(cache.files.is_empty());
843            assert_eq!(cache.hashes, 1);
844        }
845    }
846
847    #[test]
848    fn verification_cache_preserves_permission_errors() {
849        use std::os::unix::fs::PermissionsExt;
850        if unsafe { libc::geteuid() } == 0 {
851            return;
852        }
853        let dir = tempfile::tempdir().unwrap();
854        let (path, file) = verification_fixture(dir.path());
855        let stop = AtomicBool::new(false);
856        let mut cache = VerificationCache::default();
857        assert!(cache.valid(&path, &file, &stop).unwrap());
858        let permissions = fs::metadata(&path).unwrap().permissions();
859        fs::set_permissions(&path, fs::Permissions::from_mode(0o000)).unwrap();
860        let cached = cache.valid(&path, &file, &stop);
861        let full = valid(&path, &file, &stop);
862        fs::set_permissions(&path, permissions).unwrap();
863        for result in [cached, full] {
864            assert_eq!(
865                result
866                    .unwrap_err()
867                    .downcast_ref::<std::io::Error>()
868                    .unwrap()
869                    .kind(),
870                std::io::ErrorKind::PermissionDenied
871            );
872        }
873        assert!(cache.files.is_empty());
874        assert!(cache.valid(&path, &file, &stop).unwrap());
875    }
876
877    #[test]
878    fn shared_locks_open_existing_resources_without_write_access() {
879        use std::os::{fd::AsRawFd, unix::fs::PermissionsExt};
880        let dir = tempfile::tempdir().unwrap();
881        let config = crate::test_support::config(dir.path());
882        drop(lock(&config, true).unwrap());
883        let reader = lock(&config, false).unwrap();
884        let flags = unsafe { libc::fcntl(reader.0.as_raw_fd(), libc::F_GETFL) };
885        assert_ne!(flags, -1);
886        assert_eq!(flags & libc::O_ACCMODE, libc::O_RDONLY);
887        assert!(lock(&config, true).is_err());
888        drop(reader);
889        assert!(lock(&config, true).is_ok());
890
891        let directory = root(&config);
892        let path = directory.join("resources.lock");
893        let dir_permissions = fs::metadata(&directory).unwrap().permissions();
894        let file_permissions = fs::metadata(&path).unwrap().permissions();
895        fs::set_permissions(&directory, fs::Permissions::from_mode(0o555)).unwrap();
896        fs::set_permissions(&path, fs::Permissions::from_mode(0o444)).unwrap();
897        let result = model_list(&config);
898        fs::set_permissions(&directory, dir_permissions).unwrap();
899        fs::set_permissions(&path, file_permissions).unwrap();
900        assert!(result.is_ok(), "{result:?}");
901    }
902
903    #[test]
904    fn read_only_directory_without_a_lock_reports_why_it_cannot_coordinate() {
905        use std::os::unix::fs::PermissionsExt;
906        if unsafe { libc::geteuid() } == 0 {
907            return; // Root bypasses the permission boundary this test exercises.
908        }
909        let dir = tempfile::tempdir().unwrap();
910        let config = crate::test_support::config(dir.path());
911        let directory = root(&config);
912        fs::create_dir_all(&directory).unwrap();
913        let permissions = fs::metadata(&directory).unwrap().permissions();
914        fs::set_permissions(&directory, fs::Permissions::from_mode(0o555)).unwrap();
915        let result = model_list(&config);
916        fs::set_permissions(&directory, permissions).unwrap();
917        let error = result.unwrap_err();
918        assert_eq!(
919            error.downcast_ref::<std::io::Error>().unwrap().kind(),
920            std::io::ErrorKind::PermissionDenied
921        );
922        assert!(error.to_string().contains("resources.lock"));
923        assert!(!directory.join("resources.lock").exists());
924    }
925
926    #[test]
927    fn corrupt_download_cleanup_keeps_integrity_context_and_tolerates_missing_files() {
928        use std::os::unix::fs::PermissionsExt;
929        let dir = tempfile::tempdir().unwrap();
930        let path = dir.path().join("model.onnx");
931        let error = reject_corrupt_download("test-model", &path).unwrap_err();
932        let integrity = error.downcast_ref::<DomainError>().unwrap();
933        assert_eq!(integrity.code, ErrorCode::SubtitleIntegrity);
934        assert_eq!(integrity.params["id"], "test-model");
935        assert_eq!(integrity.params["path"], path.to_string_lossy());
936        assert!(error.downcast_ref::<std::io::Error>().is_none());
937
938        if unsafe { libc::geteuid() } == 0 {
939            return;
940        }
941        let partial = partial_path(&path);
942        fs::write(&partial, b"corrupt").unwrap();
943        let permissions = fs::metadata(dir.path()).unwrap().permissions();
944        fs::set_permissions(dir.path(), fs::Permissions::from_mode(0o555)).unwrap();
945        let result = reject_corrupt_download("test-model", &path);
946        fs::set_permissions(dir.path(), permissions).unwrap();
947        let error = result.unwrap_err();
948        assert_eq!(
949            error.downcast_ref::<DomainError>().unwrap().code,
950            ErrorCode::SubtitleIntegrity
951        );
952        assert_eq!(
953            error.downcast_ref::<std::io::Error>().unwrap().kind(),
954            std::io::ErrorKind::PermissionDenied
955        );
956        assert!(format!("{error:#}").contains("cannot remove corrupt download"));
957        assert!(partial.exists());
958    }
959
960    #[test]
961    fn cleanup_permission_errors_keep_source_diagnostics_without_retrying() {
962        use std::{
963            os::unix::fs::PermissionsExt,
964            sync::{Arc, atomic::AtomicUsize},
965        };
966        if unsafe { libc::geteuid() } == 0 {
967            return;
968        }
969        let dir = tempfile::tempdir().unwrap();
970        let mut config = crate::test_support::config(dir.path());
971        let path = model_dir(&config, "test-model").join("model.onnx");
972        let directory = path.parent().unwrap().to_path_buf();
973        fs::create_dir_all(&directory).unwrap();
974        fs::write(partial_path(&path), b"xxx").unwrap();
975        let permissions = fs::metadata(&directory).unwrap().permissions();
976        let protected = directory.clone();
977        let calls = Arc::new(AtomicUsize::new(0));
978        let counter = calls.clone();
979        let origin = Server::new(move |_, stream| {
980            counter.fetch_add(1, Ordering::Relaxed);
981            fs::set_permissions(&protected, fs::Permissions::from_mode(0o555)).unwrap();
982            response(stream, 200, "application/octet-stream", b"xxxxxx");
983        });
984        let mirror = Server::new(|_, stream| response(stream, 404, "text/plain", b"missing"));
985        config.settings.subtitles.model_mirror = Some(mirror.url.clone());
986        let file = ModelFile {
987            path: "model.onnx".into(),
988            url: origin.url.clone(),
989            size: 6,
990            sha256: format!("{:x}", Sha256::digest(b"abcdef")),
991        };
992        let result = install_file(
993            &config,
994            &reqwest::Client::new(),
995            "test-model",
996            &file,
997            &AtomicBool::new(false),
998        );
999        fs::set_permissions(&directory, permissions).unwrap();
1000        let error = result.unwrap_err();
1001        assert_eq!(
1002            error.downcast_ref::<DomainError>().unwrap().params["id"],
1003            "test-model"
1004        );
1005        assert_eq!(
1006            error.downcast_ref::<std::io::Error>().unwrap().kind(),
1007            std::io::ErrorKind::PermissionDenied
1008        );
1009        let diagnostic = format!("{error:#}");
1010        assert!(diagnostic.contains(&mirror.url));
1011        assert!(diagnostic.contains(&origin.url));
1012        assert!(diagnostic.contains(&path.to_string_lossy().to_string()));
1013        assert!(diagnostic.contains("cannot remove corrupt download"));
1014        assert_eq!(calls.load(Ordering::Relaxed), 1);
1015    }
1016
1017    #[test]
1018    fn runtime_platform_support_checks_host_libc() {
1019        assert!(supports_runtime("linux", "x86_64", true));
1020        assert!(!supports_runtime("linux", "x86_64", false));
1021        assert!(!supports_runtime("linux", "aarch64", true));
1022        assert!(supports_runtime("macos", "x86_64", false));
1023        assert!(supports_runtime("macos", "aarch64", false));
1024        assert!(!supports_runtime("windows", "x86_64", false));
1025    }
1026
1027    #[test]
1028    fn cancelled_runtime_checks_do_not_start_installation_or_mask_the_error() {
1029        let dir = tempfile::tempdir().unwrap();
1030        let config = crate::test_support::config(dir.path());
1031        let stop = std::sync::Arc::new(AtomicBool::new(true));
1032        for error in [
1033            runtime_check(&config, &stop).unwrap_err(),
1034            manage(&config, Action::Setup, &stop).unwrap_err(),
1035            manage(
1036                &config,
1037                Action::Models {
1038                    action: ModelAction::Install {
1039                        id: "sensevoice-small-yue-int8".into(),
1040                    },
1041                },
1042                &stop,
1043            )
1044            .unwrap_err(),
1045        ] {
1046            assert_eq!(
1047                error.downcast_ref::<DomainError>().unwrap().code,
1048                ErrorCode::Cancelled
1049            );
1050        }
1051        assert!(!root(&config).exists());
1052    }
1053
1054    #[test]
1055    fn model_install_checks_platform_before_model_lookup_or_disk_changes() {
1056        let dir = tempfile::tempdir().unwrap();
1057        let config = crate::test_support::config(dir.path());
1058        let expected = if ensure_runtime_platform().is_ok() {
1059            ErrorCode::SubtitleModel
1060        } else {
1061            ErrorCode::SubtitlePlatform
1062        };
1063        let error = manage(
1064            &config,
1065            Action::Models {
1066                action: ModelAction::Install {
1067                    id: "unknown-model".into(),
1068                },
1069            },
1070            &std::sync::Arc::new(AtomicBool::new(false)),
1071        )
1072        .unwrap_err();
1073        assert_eq!(error.downcast_ref::<DomainError>().unwrap().code, expected);
1074        assert!(!root(&config).exists());
1075    }
1076
1077    #[test]
1078    fn network_waits_remain_cancellable_and_abort_when_stalled() {
1079        let runtime = tokio::runtime::Builder::new_current_thread()
1080            .enable_all()
1081            .build()
1082            .unwrap();
1083        runtime.block_on(async {
1084            let stop = AtomicBool::new(false);
1085            let cancel = async {
1086                tokio::time::sleep(Duration::from_millis(100)).await;
1087                stop.store(true, Ordering::Relaxed);
1088            };
1089            let wait = network(std::future::pending::<()>(), &stop);
1090            let (result, ()) =
1091                tokio::time::timeout(Duration::from_secs(5), async { tokio::join!(wait, cancel) })
1092                    .await
1093                    .expect("network cancellation must remain responsive");
1094            assert_eq!(
1095                result
1096                    .unwrap_err()
1097                    .downcast_ref::<DomainError>()
1098                    .unwrap()
1099                    .code,
1100                ErrorCode::Cancelled
1101            );
1102
1103            stop.store(false, Ordering::Relaxed);
1104            let result = tokio::time::timeout(
1105                Duration::from_secs(25),
1106                network(std::future::pending::<()>(), &stop),
1107            )
1108            .await
1109            .expect("stalled network waits must time out");
1110            assert_eq!(result.unwrap_err().to_string(), "model download stalled");
1111        });
1112    }
1113
1114    #[test]
1115    fn mirrors_preserve_base_paths_and_fall_back_without_changing_hashes() {
1116        use std::sync::{Arc, atomic::AtomicUsize};
1117        // Successful mirror, bad digest, missing file, corrupt prefix left by
1118        // a mirror, and resumed mirror mismatches with Range honored/ignored.
1119        for mode in 0..6 {
1120            let origin_calls = Arc::new(AtomicUsize::new(0));
1121            let calls = origin_calls.clone();
1122            let origin = Server::new(move |request, stream| {
1123                let attempt = calls.fetch_add(1, Ordering::Relaxed);
1124                if mode == 3 && attempt == 0 {
1125                    assert!(request.to_ascii_lowercase().contains("range: bytes=3-"));
1126                    stream.write_all(b"HTTP/1.1 206 OK\r\nContent-Range: bytes 3-5/6\r\nContent-Length: 3\r\nConnection: close\r\n\r\ndef").unwrap();
1127                } else {
1128                    assert!(!request.to_ascii_lowercase().contains("range:"));
1129                    response(stream, 200, "application/octet-stream", b"abcdef");
1130                }
1131            });
1132            let mirror_calls = Arc::new(AtomicUsize::new(0));
1133            let calls = mirror_calls.clone();
1134            let mirror = Server::new(move |request, stream| {
1135                calls.fetch_add(1, Ordering::Relaxed);
1136                assert!(request.starts_with("GET /base/test-model/tokenizer/vocab.json "));
1137                match mode {
1138                    0 => response(stream, 200, "application/octet-stream", b"abcdef"),
1139                    1 | 5 => response(stream, 200, "application/octet-stream", b"xxxxxx"),
1140                    2 => response(stream, 404, "text/plain", b"missing"),
1141                    3 => response(stream, 200, "application/octet-stream", b"xxx"),
1142                    _ => {
1143                        assert!(request.to_ascii_lowercase().contains("range: bytes=3-"));
1144                        stream.write_all(b"HTTP/1.1 206 OK\r\nContent-Range: bytes 3-5/6\r\nContent-Length: 3\r\nConnection: close\r\n\r\nxxx").unwrap();
1145                    }
1146                }
1147            });
1148            let dir = tempfile::tempdir().unwrap();
1149            let mut config = crate::test_support::config(dir.path());
1150            config.settings.subtitles.model_mirror = Some(format!("{}/base/", mirror.url));
1151            let file = ModelFile {
1152                path: "tokenizer/vocab.json".into(),
1153                url: origin.url.clone(),
1154                size: 6,
1155                sha256: format!("{:x}", Sha256::digest(b"abcdef")),
1156            };
1157            if mode >= 4 {
1158                let path = model_dir(&config, "test-model").join(&file.path);
1159                fs::create_dir_all(path.parent().unwrap()).unwrap();
1160                fs::write(partial_path(&path), b"abc").unwrap();
1161            }
1162            install_file(
1163                &config,
1164                &reqwest::Client::new(),
1165                "test-model",
1166                &file,
1167                &AtomicBool::new(false),
1168            )
1169            .unwrap();
1170            assert_eq!(
1171                fs::read(model_dir(&config, "test-model").join(&file.path)).unwrap(),
1172                b"abcdef"
1173            );
1174            assert_eq!(
1175                origin_calls.load(Ordering::Relaxed),
1176                match mode {
1177                    0 => 0,
1178                    3 => 2,
1179                    _ => 1,
1180                }
1181            );
1182            assert_eq!(mirror_calls.load(Ordering::Relaxed), 1);
1183        }
1184    }
1185
1186    #[test]
1187    fn installation_space_accounts_for_verified_files_and_partial_downloads() {
1188        let dir = tempfile::tempdir().unwrap();
1189        let config = crate::test_support::config(dir.path());
1190        let model = Model {
1191            id: "test-model".into(),
1192            license: "MIT".into(),
1193            license_url: String::new(),
1194            files: vec![ModelFile {
1195                path: "model.onnx".into(),
1196                url: "http://127.0.0.1:1".into(),
1197                size: 6,
1198                sha256: format!("{:x}", Sha256::digest(b"abcdef")),
1199            }],
1200        };
1201        let models = [model];
1202        let path = model_dir(&config, "test-model").join("model.onnx");
1203        let stop = AtomicBool::new(false);
1204        assert_eq!(pending_bytes(&config, &models, &stop).unwrap(), 6);
1205        fs::create_dir_all(path.parent().unwrap()).unwrap();
1206        fs::write(partial_path(&path), b"abc").unwrap();
1207        assert_eq!(pending_bytes(&config, &models, &stop).unwrap(), 3);
1208        fs::write(&path, b"xxxxxx").unwrap();
1209        assert_eq!(pending_bytes(&config, &models, &stop).unwrap(), 3);
1210        fs::write(partial_path(&path), b"xxxxxx").unwrap();
1211        assert_eq!(pending_bytes(&config, &models, &stop).unwrap(), 6);
1212        fs::write(partial_path(&path), b"abcdef").unwrap();
1213        assert_eq!(pending_bytes(&config, &models, &stop).unwrap(), 0);
1214        fs::write(&path, b"abcdef").unwrap();
1215        assert_eq!(pending_bytes(&config, &models, &stop).unwrap(), 0);
1216        assert!(check_space(0, 0).is_ok());
1217        assert!(check_space(6, 6 + 64 * 1024 * 1024).is_ok());
1218        let error = check_space(6, 6 + 64 * 1024 * 1024 - 1).unwrap_err();
1219        assert_eq!(
1220            error.downcast_ref::<DomainError>().unwrap().code,
1221            ErrorCode::SubtitleSpace
1222        );
1223    }
1224
1225    #[test]
1226    fn vad_dependencies_are_independent_of_manifest_order() {
1227        let mut models = manifest();
1228        models.reverse();
1229        models.push(Model {
1230            id: "future-model".into(),
1231            license: "MIT".into(),
1232            license_url: String::new(),
1233            files: vec![],
1234        });
1235        let required = required_from(models, "qwen3-asr-0.6b-int8").unwrap();
1236        assert_eq!(required[0].id, "qwen3-asr-0.6b-int8");
1237        assert_eq!(required[1].id, "silero-vad");
1238    }
1239
1240    #[test]
1241    fn list_requires_vad_and_a_shared_resource_lock() {
1242        let dir = tempfile::tempdir().unwrap();
1243        let config = crate::test_support::config(dir.path());
1244        let required = required_models("sensevoice-small-yue-int8").unwrap();
1245        let create = |model: &Model| {
1246            for file in &model.files {
1247                let path = model_dir(&config, &model.id).join(&file.path);
1248                fs::create_dir_all(path.parent().unwrap()).unwrap();
1249                File::create(path).unwrap().set_len(file.size).unwrap();
1250            }
1251        };
1252        create(&required[0]);
1253        let listed = model_list(&config).unwrap();
1254        assert_eq!(listed[0]["present"], false);
1255        assert_eq!(
1256            listed[0]["bytes"],
1257            required
1258                .iter()
1259                .flat_map(|m| &m.files)
1260                .map(|f| f.size)
1261                .sum::<u64>()
1262        );
1263        create(&required[1]);
1264        assert_eq!(model_list(&config).unwrap()[0]["present"], true);
1265        let writer = lock(&config, true).unwrap();
1266        assert_eq!(
1267            model_list(&config)
1268                .unwrap_err()
1269                .downcast_ref::<DomainError>()
1270                .unwrap()
1271                .code,
1272            ErrorCode::SubtitleBusy
1273        );
1274        drop(writer);
1275    }
1276
1277    #[test]
1278    fn validation_preserves_cancellation_and_io_errors() {
1279        let dir = tempfile::tempdir().unwrap();
1280        let path = dir.path().join("model.onnx");
1281        fs::write(&path, b"abcdef").unwrap();
1282        let file = ModelFile {
1283            path: "model.onnx".into(),
1284            url: "http://127.0.0.1:1".into(),
1285            size: 6,
1286            sha256: format!("{:x}", Sha256::digest(b"abcdef")),
1287        };
1288        let stop = AtomicBool::new(true);
1289        assert_eq!(
1290            valid(&path, &file, &stop)
1291                .unwrap_err()
1292                .downcast_ref::<DomainError>()
1293                .unwrap()
1294                .code,
1295            ErrorCode::Cancelled
1296        );
1297        assert_eq!(
1298            download(&reqwest::Client::new(), "test-model", &path, &file, &stop)
1299                .unwrap_err()
1300                .downcast_ref::<DomainError>()
1301                .unwrap()
1302                .code,
1303            ErrorCode::Cancelled
1304        );
1305        assert!(valid(&path.join("child"), &file, &AtomicBool::new(false)).is_err());
1306        assert!(valid(&path, &file, &AtomicBool::new(false)).unwrap());
1307    }
1308
1309    #[test]
1310    fn downloads_resume_only_a_matching_range_and_restart_on_full_response() {
1311        for ranged in [true, false] {
1312            let server = Server::new(move |request, stream| {
1313                assert!(request.to_ascii_lowercase().contains("range: bytes=3-"));
1314                if ranged {
1315                    stream.write_all(b"HTTP/1.1 206 OK\r\nContent-Range: bytes 3-5/6\r\nContent-Length: 3\r\nConnection: close\r\n\r\ndef").unwrap();
1316                } else {
1317                    response(stream, 200, "application/octet-stream", b"abcdef");
1318                }
1319            });
1320            let dir = tempfile::tempdir().unwrap();
1321            let path = dir.path().join("model.onnx");
1322            fs::write(dir.path().join("model.onnx.part"), b"abc").unwrap();
1323            let file = ModelFile {
1324                path: "model.onnx".into(),
1325                url: server.url.clone(),
1326                size: 6,
1327                sha256: format!("{:x}", Sha256::digest(b"abcdef")),
1328            };
1329            download(
1330                &reqwest::Client::builder()
1331                    .timeout(Duration::from_secs(5))
1332                    .build()
1333                    .unwrap(),
1334                "test-model",
1335                &path,
1336                &file,
1337                &AtomicBool::new(false),
1338            )
1339            .unwrap();
1340            assert_eq!(fs::read(path).unwrap(), b"abcdef");
1341        }
1342    }
1343
1344    #[test]
1345    fn bad_range_truncation_digest_and_cancellation_never_publish() {
1346        for mode in 0..4 {
1347            let server = Server::new(move |_, stream| {
1348                match mode {
1349                0 => stream.write_all(b"HTTP/1.1 206 OK\r\nContent-Range: bytes 0-2/6\r\nContent-Length: 3\r\nConnection: close\r\n\r\ndef").unwrap(),
1350                1 => response(stream, 200, "application/octet-stream", b"abc"),
1351                _ => response(stream, 200, "application/octet-stream", b"xxxxxx"),
1352            }
1353            });
1354            let dir = tempfile::tempdir().unwrap();
1355            let path = dir.path().join("model.onnx");
1356            let partial = dir.path().join("model.onnx.part");
1357            fs::write(&partial, b"abc").unwrap();
1358            let file = ModelFile {
1359                path: "model.onnx".into(),
1360                url: server.url.clone(),
1361                size: 6,
1362                sha256: format!("{:x}", Sha256::digest(b"abcdef")),
1363            };
1364            assert!(
1365                download(
1366                    &reqwest::Client::builder()
1367                        .timeout(Duration::from_secs(5))
1368                        .build()
1369                        .unwrap(),
1370                    "test-model",
1371                    &path,
1372                    &file,
1373                    &AtomicBool::new(mode == 3)
1374                )
1375                .is_err()
1376            );
1377            assert!(!path.exists());
1378            if mode == 2 {
1379                assert!(!partial.exists());
1380            }
1381        }
1382    }
1383
1384    #[test]
1385    fn resources_cannot_be_modified_during_recognition() {
1386        let dir = tempfile::tempdir().unwrap();
1387        let config = crate::test_support::config(dir.path());
1388        let reader = lock(&config, false).unwrap();
1389        assert!(lock(&config, true).is_err());
1390        assert!(lock(&config, false).is_ok());
1391        drop(reader);
1392        assert!(lock(&config, true).is_ok());
1393        assert!(required_models("../../outside").is_err());
1394    }
1395}