Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Save Desktop Gallery batches with one folder choice (#16521)
Daniel Han3978a1b8600b
diff --git a/studio/frontend/src/features/library/actions.ts b/studio/frontend/src/features/library/actions.tsindex c6498b13b95..614fa3d8805 100644--- a/studio/frontend/src/features/library/actions.ts+++ b/studio/frontend/src/features/library/actions.ts@@ -22,7 +22,7 @@ import { useImageEditHandoffStore } from "@/stores/image-edit-handoff";import { MAX_AUDIO_SIZE } from "@/lib/audio-utils";import { isTauri } from "@/lib/api-base";import { MAX_UPLOAD_IMAGE_SIDE, blobToDataUrl, fitImageDataUrl } from "@/lib/image-fit";-import { downloadFile, downloadUrlStreaming, isDownloadCancelled } from "@/lib/native-files";+import { downloadFile, downloadUrlStreaming, downloadUrlInDirectory, pickNativeDownloadDirectory, releaseNativeDownloadDirectory, isDownloadCancelled } from "@/lib/native-files";import { toast } from "@/lib/toast";import { MAX_VIDEO_SIZE } from "@/lib/video-utils";import {@@ -59,23 +59,27 @@ function fitsInChat(item: LibraryItem): boolean {export async function downloadLibraryItem(item: LibraryItem,epoch = getAuthSessionEpoch(),-): Promise<void> {- if (getAuthSessionEpoch() !== epoch) return;+ directory?: { token: string; fileName: string },+): Promise<boolean> {+ if (getAuthSessionEpoch() !== epoch) return false;try {if (isTauri && !item.textOnly && hasOwnFile(item.id)) {const url = await libraryDownloadUrl(item);- if (getAuthSessionEpoch() !== epoch) return;- await downloadUrlStreaming(url, libraryFileName(item));- return;+ if (getAuthSessionEpoch() !== epoch) return false;+ if (directory) await downloadUrlInDirectory(directory.token, url, directory.fileName);+ else await downloadUrlStreaming(url, libraryFileName(item));+ return true;}const file = await libraryItemFile(item);- if (getAuthSessionEpoch() !== epoch) return;+ if (getAuthSessionEpoch() !== epoch) return false;await downloadFile(file, file.name, file.type);+ return true;} catch (error) {- if (isDownloadCancelled(error)) return;+ if (isDownloadCancelled(error)) return false;toast.error(translate("library.toast.downloadFailed", { name: item.name }), {description: errorMessage(error),});+ return true;}}@@ -83,8 +87,28 @@ const MAX_ZIP_BYTES = 256 * 1024 * 1024;export async function downloadLibraryItems(items: LibraryItem[]): Promise<void> {const epoch = getAuthSessionEpoch();+ if (isTauri && items.length > 1 && items.every((item) => !item.textOnly && hasOwnFile(item.id))) {+ try {+ const token = await pickNativeDownloadDirectory();+ try {+ const width = String(items.length).length;+ for (const [index, item] of items.entries()) {+ const fileName = `${String(index + 1).padStart(width, "0")}-${libraryFileName(item)}`;+ if (!(await downloadLibraryItem(item, epoch, { token, fileName }))) break;+ }+ } finally {+ await releaseNativeDownloadDirectory(token);+ }+ } catch (error) {+ if (isDownloadCancelled(error)) return;+ toast.error(translate("library.toast.downloadManyFailed"), { description: errorMessage(error) });+ }+ return;+ }if (items.length <= 1 || isTauri) {- for (const item of items) await downloadLibraryItem(item, epoch);+ for (const item of items) {+ if (!(await downloadLibraryItem(item, epoch))) break;+ }return;}// Unknown size counts as Infinity so it never goes into an in-memory zip.@@ -93,7 +117,9 @@ export async function downloadLibraryItems(items: LibraryItem[]): Promise<void>toast(translate("library.toast.downloadingMany", { count: items.length }), {description: translate("library.toast.downloadingManyDescription"),});- for (const item of items) await downloadLibraryItem(item, epoch);+ for (const item of items) {+ if (!(await downloadLibraryItem(item, epoch))) break;+ }return;}const progress = toast.loading(translate("library.toast.preparingMany", { count: items.length }));@@ -107,7 +133,9 @@ export async function downloadLibraryItems(items: LibraryItem[]): Promise<void>} catch (error) {if (!(error instanceof LibraryFileTooLarge)) throw error;toast.dismiss(progress);- for (const each of items) await downloadLibraryItem(each, epoch);+ for (const each of items) {+ if (!(await downloadLibraryItem(each, epoch))) break;+ }return;}budget -= file.size;diff --git a/studio/frontend/src/lib/native-files.ts b/studio/frontend/src/lib/native-files.tsindex 5b35f535b3d..d08d053b381 100644--- a/studio/frontend/src/lib/native-files.ts+++ b/studio/frontend/src/lib/native-files.ts@@ -158,6 +158,23 @@ export async function downloadBlobStreaming(}}+export async function pickNativeDownloadDirectory(): Promise<string> {+ const { invoke } = await import("@tauri-apps/api/core");+ const token = await invoke<string | null>("pick_native_download_directory");+ if (token === null) throw new DownloadCancelledError();+ return token;+}++export async function downloadUrlInDirectory(token: string, url: string, fileName: string): Promise<void> {+ const { invoke } = await import("@tauri-apps/api/core");+ await invoke<string>("save_native_file_in_directory", { token, url, fileName });+}++export async function releaseNativeDownloadDirectory(token: string): Promise<void> {+ const { invoke } = await import("@tauri-apps/api/core");+ await invoke<void>("release_native_download_directory", { token });+}+export async function urlToBlob(url: string): Promise<Blob> {if (isDataUri(url)) {const { bytes, mimeType } = decodeDataUri(url);diff --git a/studio/frontend/tests/library-state.test.ts b/studio/frontend/tests/library-state.test.tsindex 3950eadcb04..be514e3b189 100644--- a/studio/frontend/tests/library-state.test.ts+++ b/studio/frontend/tests/library-state.test.ts@@ -4,6 +4,7 @@import assert from "node:assert/strict";import test from "node:test";+import type * as NativeFilesModule from "../src/lib/native-files.ts";import * as fflate from "fflate";import * as zustand from "zustand";import * as zustandMiddleware from "zustand/middleware";@@ -422,16 +423,25 @@ test("a chat attachment is not deleted when an open chat could not drop it afterassert.deepEqual(emitted, [{ messageId: "m:1", attachmentId: "content-part-sha256-ab" }]);});+const cancelledDownload = new Error("Save cancelled");+function loadDownloads(fflate: object,saved: (File | Blob)[],fileNames: object,session: { epoch: number } = { epoch: 0 },fetched: (name: string) => void = () => {},+ options: {+ native?: boolean;+ save?: (file: File | Blob) => Promise<unknown>;+ nativeFiles?: Partial<typeof NativeFilesModule>;+ downloadUrl?: (item: { id: string }) => Promise<string>;+ errors?: unknown[];+ } = {},) {- const toast = Object.assign(() => {}, { loading: () => 0, dismiss: () => {}, error: () => {} });+ const toast = Object.assign(() => {}, { loading: () => 0, dismiss: () => {}, error: (...args: unknown[]) => options.errors?.push(args) });return loadWithStubs<{- downloadLibraryItems: (items: { name: string; sizeBytes: number | null }[]) => Promise<void>;+ downloadLibraryItems: (items: { id?: string; name: string; sizeBytes: number | null; textOnly?: boolean }[]) => Promise<void>;}>(new URL("../src/features/library/actions.ts", import.meta.url), {fflate,"@/config/env": { usePlatformStore: { getState: () => ({ deviceType: "linux" }) } },@@ -442,24 +452,30 @@ function loadDownloads("@/features/model-picker": {},"@/i18n": { translate: (key: string) => key },"@/lib/audio-utils": {},- "@/lib/api-base": { isTauri: false },+ "@/lib/api-base": { isTauri: options.native ?? false },"@/lib/image-fit": {},"@/stores/image-edit-handoff": {},"@/lib/native-files": {- downloadFile: async (f: File | Blob) => saved.push(f),- isDownloadCancelled: () => false,+ downloadFile: options.save ?? (async (f: File | Blob) => saved.push(f)),+ downloadUrlStreaming: async (_url: string, name: string) => {+ const save = options.save ?? (async (f: File | Blob) => saved.push(f));+ return save(file(name));+ },+ isDownloadCancelled: (error: unknown) => error === cancelledDownload,+ ...options.nativeFiles,},"@/lib/toast": { toast },"@/lib/video-utils": {},"./api": {errorMessage: String,+ libraryDownloadUrl: options.downloadUrl ?? (async (i: { id: string }) => `/download/${i.id}`),libraryItemFile: async (i: { name: string }) => {fetched(i.name);return file(i.name);},},"./file-kind": {},- "./file-name": { hasOwnFile: () => true, ...fileNames },+ "./file-name": { hasOwnFile: () => true, libraryFileName: (i: { name: string }) => i.name, ...fileNames },"./chat-handoff-store": {},"./start-chat": {},}).downloadLibraryItems;@@ -545,3 +561,197 @@ test("a file named __proto__ is kept in a zipped download", async () => {const archive = fflate.unzipSync(new Uint8Array(await saved[0]!.arrayBuffer()));assert.deepEqual(Object.keys(archive), ["__proto__ (2)", "a.txt"]);});+++function loadNativeDownloads(+ invoke: (command: string, args?: { token?: string; url?: string; fileName?: string }) => Promise<unknown>,+) {+ return loadWithStubs<typeof NativeFilesModule>(new URL("../src/lib/native-files.ts", import.meta.url), {+ "@/lib/api-base": { isTauri: true },+ "@/lib/data-uri": {},+ "@tauri-apps/api/core": { invoke },+ });+}++test("Desktop exports images with numbered names through one directory without fetching their bodies", async () => {+ const saved: { token?: string; url?: string; fileName?: string }[] = [];+ const fetched: string[] = [];+ let pickers = 0;+ let released = 0;+ const nativeFiles = loadNativeDownloads(async (command, args) => {+ if (command === "pick_native_download_directory") {+ pickers += 1;+ return "directory-token";+ }+ if (command === "release_native_download_directory") {+ assert.deepEqual(args, { token: "directory-token" });+ released += 1;+ return;+ }+ assert.equal(command, "save_native_file_in_directory");+ saved.push(args!);+ return "saved.png";+ });+ const download = loadDownloads({}, [], {}, undefined, (name) => fetched.push(name), {+ native: true,+ nativeFiles,+ });+ await download(Array.from({ length: 10 }, (_, index) => ({+ id: `image:${index}`,+ name: "same.png",+ sizeBytes: index % 2 ? null : 300 * 1024 * 1024,+ })));+ assert.deepEqual(saved, [+ "01-same.png", "02-same.png", "03-same.png", "04-same.png", "05-same.png",+ "06-same.png", "07-same.png", "08-same.png", "09-same.png", "10-same.png",+ ].map((fileName, index) => ({ token: "directory-token", url: `/download/image:${index}`, fileName })));+ assert.deepEqual(fetched, []);+ assert.equal(pickers, 1);+ assert.equal(released, 1);+});++test("cancelling the directory picker stops a batch without fetching or saving its files", async () => {+ const calls: string[] = [];+ const errors: unknown[] = [];+ const nativeFiles = loadNativeDownloads(async (command) => {+ calls.push(command);+ return null;+ });+ let lookups = 0;+ const download = loadDownloads({}, [], {}, undefined, undefined, {+ native: true,+ nativeFiles,+ errors,+ downloadUrl: async () => {+ lookups += 1;+ return "/download/image";+ },+ });+ await download([+ { id: "image:first", name: "first.png", sizeBytes: 1 },+ { id: "image:second", name: "second.png", sizeBytes: 1 },+ ]);+ assert.deepEqual(calls, ["pick_native_download_directory"]);+ assert.equal(lookups, 0);+ assert.deepEqual(errors, []);+});++test("a single Desktop image retains its individual save", async () => {+ const saved: File[] = [];+ let pickers = 0;+ const download = loadDownloads({}, saved, {}, undefined, undefined, {+ native: true,+ nativeFiles: {+ pickNativeDownloadDirectory: async () => {+ pickers += 1;+ return "directory-token";+ },+ },+ });+ await download([{ id: "image:first", name: "single.png", sizeBytes: 1 }]);+ assert.deepEqual(names(saved), ["single.png"]);+ assert.equal(pickers, 0);+});++test("a native batch stops after an account switch and a fresh attempt can save the full batch", async () => {+ const session = { epoch: 1 };+ const saved: string[] = [];+ let pickers = 0;+ let released = 0;+ let switchAccount = true;+ const nativeFiles = loadNativeDownloads(async (command, args) => {+ if (command === "pick_native_download_directory") return `directory-${++pickers}`;+ if (command === "release_native_download_directory") {+ released += 1;+ return;+ }+ assert.equal(command, "save_native_file_in_directory");+ saved.push(args!.url!);+ if (switchAccount) {+ session.epoch += 1;+ switchAccount = false;+ }+ return "saved.png";+ });+ const download = loadDownloads({}, [], {}, session, undefined, { native: true, nativeFiles });+ const images = [+ { id: "image:first", name: "first.png", sizeBytes: null },+ { id: "image:second", name: "second.png", sizeBytes: null },+ ];+ await download(images);+ assert.deepEqual(saved, ["/download/image:first"]);+ assert.equal(released, 1);+ await download(images);+ assert.deepEqual(saved, ["/download/image:first", "/download/image:first", "/download/image:second"]);+ assert.equal(pickers, 2);+ assert.equal(released, 2);+});++for (const failure of ["lookup", "save"]) {+ test(`a native batch reports a failed ${failure} but continues to the remaining images`, async () => {+ const attempted: string[] = [];+ const saved: string[] = [];+ const errors: unknown[] = [];+ let released = 0;+ const nativeFiles = loadNativeDownloads(async (command, args) => {+ if (command === "pick_native_download_directory") return "directory-token";+ if (command === "release_native_download_directory") {+ released += 1;+ return;+ }+ assert.equal(command, "save_native_file_in_directory");+ attempted.push(args!.url!);+ if (failure === "save" && args!.url === "/download/image:failed") throw new Error("Disk full");+ saved.push(args!.url!);+ return "saved.png";+ });+ const lookedUp: string[] = [];+ const download = loadDownloads({}, [], {}, undefined, undefined, {+ native: true,+ nativeFiles,+ errors,+ downloadUrl: async ({ id }) => {+ lookedUp.push(id);+ if (failure === "lookup" && id === "image:failed") throw new Error("Download failed with status 404");+ return `/download/${id}`;+ },+ });+ await download(["first", "failed", "remaining"].map((id) => ({+ id: `image:${id}`, name: `${id}.png`, sizeBytes: 1,+ })));+ assert.deepEqual(lookedUp, ["image:first", "image:failed", "image:remaining"]);+ assert.deepEqual(attempted, failure === "lookup"+ ? ["/download/image:first", "/download/image:remaining"]+ : ["/download/image:first", "/download/image:failed", "/download/image:remaining"]);+ assert.deepEqual(saved, ["/download/image:first", "/download/image:remaining"]);+ assert.equal(errors.length, 1);+ assert.equal(released, 1);+ });+}++test("cancelling a sequential export stops it and a later retry still saves every file", async () => {+ const saved: File[] = [];+ const attempts: string[] = [];+ const errors: unknown[] = [];+ let cancel = true;+ const download = loadDownloads({}, saved, {}, undefined, undefined, {+ native: true,+ errors,+ save: async (f) => {+ const image = f as File;+ attempts.push(image.name);+ if (cancel) throw cancelledDownload;+ saved.push(image);+ },+ });+ const files = [+ { name: "first.txt", sizeBytes: null, textOnly: true },+ { name: "second.txt", sizeBytes: null, textOnly: true },+ ];+ await download(files);+ assert.deepEqual(attempts, ["first.txt"]);+ assert.deepEqual(errors, []);+ cancel = false;+ await download(files);+ assert.deepEqual(names(saved), ["first.txt", "second.txt"]);+});diff --git a/studio/frontend/tests/native-file-save.test.ts b/studio/frontend/tests/native-file-save.test.tsindex d45bd0fd59f..f02eb014810 100644--- a/studio/frontend/tests/native-file-save.test.ts+++ b/studio/frontend/tests/native-file-save.test.ts@@ -192,3 +192,61 @@ test("a native URL save reports the basename the dialog wrote", async (t) => {);assert.equal(await files.downloadUrl("/clip.wav", "clip.wav"), "renamed-clip.wav");});+++test("a native directory picker returns its save lease", async () => {+ const calls: string[] = [];+ const files = loadNativeFiles(true, async (command, args) => {+ calls.push(command);+ assert.equal(args, undefined);+ return "directory-token";+ });+ assert.equal(await files.pickNativeDownloadDirectory(), "directory-token");+ assert.deepEqual(calls, ["pick_native_download_directory"]);+});++test("cancelling the native directory picker rejects as cancellation", async () => {+ const calls: string[] = [];+ const files = loadNativeFiles(true, async (command) => {+ calls.push(command);+ return null;+ });+ await assert.rejects(+ files.pickNativeDownloadDirectory(),+ (error: unknown) => files.isDownloadCancelled(error),+ );+ assert.deepEqual(calls, ["pick_native_download_directory"]);+});++test("a native directory save sends the file through its existing lease", async () => {+ let calls = 0;+ const files = loadNativeFiles(true, async (command, args) => {+ calls += 1;+ assert.equal(command, "save_native_file_in_directory");+ assert.deepEqual(args, {+ token: "directory-token", url: "http://127.0.0.1:8000/image.png", fileName: "01-image.png",+ });+ return "01-image.png";+ });+ await files.downloadUrlInDirectory("directory-token", "http://127.0.0.1:8000/image.png", "01-image.png");+ assert.equal(calls, 1);+});++test("a native directory save propagates a file failure", async () => {+ const failure = new Error("Disk full");+ const files = loadNativeFiles(true, async () => { throw failure; });+ await assert.rejects(+ files.downloadUrlInDirectory("directory-token", "http://127.0.0.1:8000/image.png", "image.png"),+ (error: unknown) => error === failure,+ );+});++test("a native directory release drops the named lease", async () => {+ const calls: string[] = [];+ const files = loadNativeFiles(true, async (command, args) => {+ calls.push(command);+ assert.deepEqual(args, { token: "directory-token" });+ });+ await files.releaseNativeDownloadDirectory("directory-token");+ assert.deepEqual(calls, ["release_native_download_directory"]);+});diff --git a/studio/src-tauri/src/main.rs b/studio/src-tauri/src/main.rsindex cd730b2d39d..f5981057bd7 100644--- a/studio/src-tauri/src/main.rs+++ b/studio/src-tauri/src/main.rs@@ -2580,6 +2580,9 @@ fn main() {native_file_dialogs::finish_native_file_save,native_file_dialogs::cancel_native_file_save,native_file_dialogs::save_native_file_from_url,+ native_file_dialogs::pick_native_download_directory,+ native_file_dialogs::save_native_file_in_directory,+ native_file_dialogs::release_native_download_directory,native_file_dialogs::download_logs_to_downloads,native_file_dialogs::pick_native_chat_import,native_file_dialogs::read_native_chat_import_chunk,diff --git a/studio/src-tauri/src/native_file_dialogs.rs b/studio/src-tauri/src/native_file_dialogs.rsindex e8cbb824e2e..4dca011dbce 100644--- a/studio/src-tauri/src/native_file_dialogs.rs+++ b/studio/src-tauri/src/native_file_dialogs.rs@@ -78,9 +78,42 @@ const NATIVE_SAVE_HANDLE_LIMIT: usize = 8;#[derive(Default)]pub struct NativeSaveRegistry {files: Mutex<Vec<(String, Arc<Mutex<NativeSave>>)>>,+ directories: Mutex<Vec<(String, PathBuf)>>,}impl NativeSaveRegistry {+ fn register_directory(&self, directory: PathBuf) -> String {+ let token: String = (0..4)+ .map(|_| format!("{:016x}", rand::random::<u64>()))+ .collect();+ let mut directories = self+ .directories+ .lock()+ .expect("native save registry poisoned");+ directories.push((token.clone(), directory));+ while directories.len() > NATIVE_SAVE_HANDLE_LIMIT {+ directories.remove(0);+ }+ token+ }++ fn resolve_directory(&self, token: &str) -> Result<PathBuf, String> {+ self.directories+ .lock()+ .expect("native save registry poisoned")+ .iter()+ .find(|(candidate, _)| candidate == token)+ .map(|(_, directory)| directory.clone())+ .ok_or_else(|| "That export folder is no longer available.".to_string())+ }++ fn release_directory(&self, token: &str) {+ self.directories+ .lock()+ .expect("native save registry poisoned")+ .retain(|(candidate, _)| candidate != token);+ }+fn register(&self, save: NativeSave) -> String {let token: String = (0..4).map(|_| format!("{:016x}", rand::random::<u64>()))@@ -711,6 +744,73 @@ pub async fn save_native_file_from_url(Ok(Some(saved_file_name(&path)))}+#[tauri::command]+pub async fn pick_native_download_directory(+ webview: tauri::Webview,+ app: AppHandle,+ registry: State<'_, NativeSaveRegistry>,+) -> Result<Option<String>, String> {+ crate::native_intents::ensure_main_window(&webview)?;+ let config_dir = app.path().app_config_dir().ok();+ let (tx, rx) = tokio::sync::oneshot::channel();+ let mut dialog = app.dialog().file().set_title("Save Unsloth exports");+ if let Some(dir) = config_dir.as_deref().and_then(last_save_dir) {+ dialog = dialog.set_directory(dir);+ }+ dialog.pick_folder(move |path| {+ let _ = tx.send(path);+ });+ let selected_path = rx+ .await+ .map_err(|_| "Save dialog closed unexpectedly.".to_string())?+ .map(local_dialog_path)+ .transpose()?;+ let Some(directory) = selected_path else {+ return Ok(None);+ };+ Ok(Some(registry.register_directory(directory)))+}++#[tauri::command]+pub async fn save_native_file_in_directory(+ webview: tauri::Webview,+ app: AppHandle,+ registry: State<'_, NativeSaveRegistry>,+ token: String,+ url: String,+ file_name: String,+) -> Result<String, String> {+ crate::native_intents::ensure_main_window(&webview)?;+ require_loopback_url(&url)?;+ let directory = registry.resolve_directory(&token)?;+ let name = stream_file_to_directory(&url, &file_name, &directory).await?;+ if let Ok(config_dir) = app.path().app_config_dir() {+ remember_save_dir(&config_dir, &directory.join(&name));+ }+ Ok(name)+}++#[tauri::command]+pub fn release_native_download_directory(+ webview: tauri::Webview,+ registry: State<'_, NativeSaveRegistry>,+ token: String,+) -> Result<(), String> {+ crate::native_intents::ensure_main_window(&webview)?;+ registry.release_directory(&token);+ Ok(())+}++async fn stream_file_to_directory(+ url: &str,+ file_name: &str,+ directory: &Path,+) -> Result<String, String> {+ let destination = unique_destination(directory, &safe_download_name(file_name))?;+ stream_url_to_path_inner(url, &destination, DOWNLOAD_READ_TIMEOUT, None, false).await?;+ Ok(saved_file_name(&destination))+}+/// Resets per chunk, so a large save is not cut short.const DOWNLOAD_READ_TIMEOUT: Duration = Duration::from_secs(30);@@ -721,6 +821,16 @@ async fn stream_url_to_path(path: &Path,read_timeout: Duration,bearer: Option<&str>,+) -> Result<(), String> {+ stream_url_to_path_inner(url, path, read_timeout, bearer, true).await+}++async fn stream_url_to_path_inner(+ url: &str,+ path: &Path,+ read_timeout: Duration,+ bearer: Option<&str>,+ replace: bool,) -> Result<(), String> {let request = crate::loopback_http::streaming_client(Duration::from_secs(10), read_timeout).map_err(|error| format!("Download failed: {error}"))?@@ -754,9 +864,12 @@ async fn stream_url_to_path(.as_file().sync_all().map_err(|error| format!("Failed to save {}: {error}", path.display()))?;- temporary- .persist(path)- .map_err(|error| format!("Failed to save {}: {}", path.display(), error.error))?;+ let saved = if replace {+ temporary.persist(path)+ } else {+ temporary.persist_noclobber(path)+ };+ saved.map_err(|error| format!("Failed to save {}: {}", path.display(), error.error))?;Ok(())}@@ -1057,6 +1170,126 @@ pub async fn pick_native_training_config(mod tests {use super::*;+ #[test]+ fn export_directory_grants_are_released_without_affecting_other_exports() {+ let registry = NativeSaveRegistry::default();+ let first = registry.register_directory(PathBuf::from("first"));+ let second = registry.register_directory(PathBuf::from("second"));+ assert_ne!(first, second);+ assert_eq!(+ registry.resolve_directory(&first).unwrap(),+ PathBuf::from("first")+ );+ registry.release_directory(&first);+ registry.release_directory(&first);+ assert!(registry.resolve_directory(&first).is_err());+ assert!(registry.resolve_directory("unissued").is_err());+ assert_eq!(+ registry.resolve_directory(&second).unwrap(),+ PathBuf::from("second")+ );+ }++ #[test]+ fn an_automatic_export_does_not_replace_a_file_created_during_download() {+ let directory = tempfile::tempdir().unwrap();+ let destination = directory.path().join("photo.png");+ fs::write(&destination, b"previous export").unwrap();+ let (url, server) = serve_once(b"new export".to_vec(), "200 OK");+ assert!(test_runtime()+ .block_on(stream_url_to_path_inner(+ &url,+ &destination,+ DOWNLOAD_READ_TIMEOUT,+ None,+ false+ ))+ .is_err());+ server.join().unwrap();+ assert_eq!(fs::read(&destination).unwrap(), b"previous export");+ assert_eq!(fs::read_dir(directory.path()).unwrap().count(), 1);+ }++ #[test]+ fn native_batch_downloads_keep_matching_names_and_previous_exports() {+ let directory = tempfile::tempdir().unwrap();+ for (run, expected) in [+ ("first", vec!["1-photo.png", "2-photo.png"]),+ ("second", vec!["1-photo (2).png", "2-photo (2).png"]),+ ] {+ let (url_a, server_a) = serve_once(format!("{run}-a").into_bytes(), "200 OK");+ let (url_b, server_b) = serve_once(format!("{run}-b").into_bytes(), "200 OK");+ let runtime = test_runtime();+ let names = vec![+ runtime+ .block_on(stream_file_to_directory(+ &url_a,+ "1-photo.png",+ directory.path(),+ ))+ .unwrap(),+ runtime+ .block_on(stream_file_to_directory(+ &url_b,+ "2-photo.png",+ directory.path(),+ ))+ .unwrap(),+ ];+ server_a.join().unwrap();+ server_b.join().unwrap();+ assert_eq!(names, expected);+ assert_eq!(+ fs::read(directory.path().join(&names[0])).unwrap(),+ format!("{run}-a").as_bytes()+ );+ assert_eq!(+ fs::read(directory.path().join(&names[1])).unwrap(),+ format!("{run}-b").as_bytes()+ );+ }+ assert_eq!(+ fs::read(directory.path().join("1-photo.png")).unwrap(),+ b"first-a"+ );+ assert_eq!(+ fs::read(directory.path().join("2-photo.png")).unwrap(),+ b"first-b"+ );+ assert_eq!(fs::read_dir(directory.path()).unwrap().count(), 4);+ }++ #[test]+ fn native_batch_failure_preserves_completed_files_without_partial_output() {+ let directory = tempfile::tempdir().unwrap();+ let (url_a, server_a) = serve_once(b"first".to_vec(), "200 OK");+ let (url_b, server_b) = serve_once(b"not found".to_vec(), "404 Not Found");+ let runtime = test_runtime();+ runtime+ .block_on(stream_file_to_directory(+ &url_a,+ "1-photo.png",+ directory.path(),+ ))+ .unwrap();+ let error = test_runtime()+ .block_on(stream_file_to_directory(+ &url_b,+ "2-photo.png",+ directory.path(),+ ))+ .unwrap_err();+ server_a.join().unwrap();+ server_b.join().unwrap();+ assert!(error.contains("404"), "{error}");+ assert_eq!(+ fs::read(directory.path().join("1-photo.png")).unwrap(),+ b"first"+ );+ assert!(!directory.path().join("2-photo.png").exists());+ assert_eq!(fs::read_dir(directory.path()).unwrap().count(), 1);+ }+#[test]fn download_names_are_safe_on_every_platform() {assert_eq!(safe_download_name("report:2026.pdf"), "report_2026.pdf");