diff --git a/.gitignore b/.gitignore index ed7d4a3..3995e6c 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,5 @@ apps/desktop/dist/ apps/desktop/src-tauri/target/ apps/desktop/src-tauri/binaries/ apps/desktop/src-tauri/gen/ +apps/desktop/.screens/ .scratch/ diff --git a/CHANGELOG.md b/CHANGELOG.md index 73ddfd8..6d47dfb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,21 @@ Releases / Catalog / Listings / Health / Reviews / TestFlight, App folders, Accounts, Dependencies, Settings), store keys in the OS keychain, native confirm dialogs with the dry run before any write. See `docs/desktop.md`. - `--json` on `catalog diff` goes through the same machine-readable result line. +- **`storebound doctor`** (`--json`): ffmpeg, ffprobe, Chrome, playwright, Xcode tools, simctl, adb and git with + versions (the same detection as the desktop Dependencies screen) and the install commands for this OS. It runs none. +- `playwright` is an optional peer dependency (it was only documented before). The compiled desktop sidecar bundles + `playwright-core` and renders with the app's Chrome for Testing (`STOREBOUND_CHROME`), so desktop users need neither. +- `inspect --icon` / `inspect --listings` (listing completeness per language on both stores, rendered screenshots), + `reviews drafts [--json]` (local reply drafts with the send checks), `reviews approve [--undo] [--dry]`, + `init --dry`; templates are embedded so `init` works from a single-file binary. +- `apple status --json` includes the phased release of the newest live versions; release trains now count a staged + rollout or iOS phased release (also halted / paused) as the current train. +- **Desktop D2** (`apps/desktop`): redesigned shell (command palette, toasts with progress and cancel, skeletons, + illustrated empty states, first-run guide), release timeline per app, catalog parity matrix, listing completeness, + health sparklines against the Play thresholds, reviews inbox with star distribution and draft approval, TestFlight + groups and builds, store readiness with fix hints; offline cache of the last reads (SQLite, "last read" stamps); + one-click ffmpeg (pinned SHA-256) and Chrome for Testing (Google's code signature) downloads into app data; + English and Turkish with an i18n check, 7:1 text contrast checked in both themes. - `tools/build-sidecars.ts`: single-file executables for macOS (arm64, x64), Linux (x64, arm64) and Windows (x64), re-signed ad hoc on macOS and checked for build-machine paths. The MCP server now also runs from such a binary. diff --git a/README.md b/README.md index cf6c315..6b431da 100644 --- a/README.md +++ b/README.md @@ -182,6 +182,7 @@ run in the background with a log (`storebound shots render > .storebound/render. | `validate [--apple] [--play]` | API, read-only | readiness report with ordered blockers plus the local renders checks (`--no-local` skips); exit 1 while any blocker is left | | `whats-new [--since ]` | local | draft player-facing release notes from git log into both listing files | | `privacy-manifest --ios ` | local | PrivacyInfo.xcprivacy, required-reason APIs, SDKs without manifests | +| `doctor [--json]` | local | which local tools are here (ffmpeg, Chrome, playwright, Xcode, adb, git) and how to install the missing ones | | `icons` | local | App Store 1024 (no alpha), Play 512, web, Capacitor sources, Android legacy launcher icons | diff --git a/apps/desktop/index.html b/apps/desktop/index.html index 573ef21..3036910 100644 --- a/apps/desktop/index.html +++ b/apps/desktop/index.html @@ -4,6 +4,7 @@ + storebound diff --git a/apps/desktop/scripts/screens.ts b/apps/desktop/scripts/screens.ts new file mode 100644 index 0000000..157489b --- /dev/null +++ b/apps/desktop/scripts/screens.ts @@ -0,0 +1,62 @@ +#!/usr/bin/env bun +// Screenshots of every desktop screen in light and dark, from the Vite dev server in fixture mode (no Tauri, no store +// accounts), plus a few interaction checks (command palette, toast progress, keyboard focus). Dev tool only. +// cd apps/desktop && VITE_FIXTURES=1 bunx vite --port 1431 & +// bun scripts/screens.ts --base http://127.0.0.1:1431 --out [--only portfolio,app:health] [--themes light,dark] +// Uses playwright (root devDependency) with the installed Google Chrome (channel "chrome"). +import { mkdirSync } from 'node:fs'; +import { join } from 'node:path'; +import { chromium, type Page } from 'playwright'; + +const arg = (k: string, d: string) => { const i = process.argv.indexOf(`--${k}`); return i >= 0 ? process.argv[i + 1] ?? d : d; }; +const base = arg('base', 'http://127.0.0.1:1431'); +const out = arg('out', join(import.meta.dir, '..', '.screens')); +const only = arg('only', '').split(',').filter(Boolean); +const themes = arg('themes', 'light,dark').split(','); +const lang = arg('lang', 'en'); +const suffix = arg('suffix', ''); + +export const SCREENS: [string, string][] = [ + ['portfolio', 'portfolio'], ['releases', 'app:releases'], ['releases-2', 'app:releases:2'], ['catalog', 'app:catalog'], ['listings', 'app:listings'], + ['health', 'app:health'], ['reviews', 'app:reviews'], ['testflight', 'app:testflight'], ['checks', 'app:checks'], + ['apps', 'apps'], ['accounts', 'accounts'], ['dependencies', 'dependencies'], ['settings', 'settings'], + ['palette', 'palette'], ['welcome-1', 'welcome:1'], ['welcome-2', 'welcome:2'], ['welcome-3', 'welcome:3'], ['welcome-4', 'welcome:4'], ['welcome-5', 'welcome:5'], +]; + +async function settle(page: Page) { + await page.waitForLoadState('networkidle'); + await page.waitForTimeout(900); +} + +mkdirSync(out, { recursive: true }); +const browser = await chromium.launch({ channel: 'chrome', headless: true }); +const files: string[] = []; +for (const theme of themes) { + const ctx = await browser.newContext({ viewport: { width: 1440, height: 900 }, deviceScaleFactor: 1, colorScheme: theme === 'dark' ? 'dark' : 'light', reducedMotion: 'reduce' }); + const page = await ctx.newPage(); + const errors: string[] = []; + page.on('pageerror', e => errors.push(String(e))); + page.on('console', m => { if (m.type() === 'error') errors.push(m.text()); }); + for (const [name, start] of SCREENS) { + if (only.length && !only.includes(name)) continue; + await page.goto(`${base}/?start=${encodeURIComponent(start)}&theme=${theme}&lang=${lang}`); + await settle(page); + const f = join(out, `d2-${name}${suffix}-${theme}${lang === 'en' ? '' : `-${lang}`}.png`); + await page.screenshot({ path: f }); + files.push(f); + } + if (!only.length || only.includes('toast')) { + // a background job: Read the stores, caught mid-way with its progress toast + await page.goto(`${base}/?start=portfolio&theme=${theme}&lang=${lang}`); + await settle(page); + await page.getByRole('button', { name: /Read the stores|Mağazaları oku/ }).first().click(); + await page.waitForTimeout(900); + const f = join(out, `d2-toast${suffix}-${theme}${lang === 'en' ? '' : `-${lang}`}.png`); + await page.screenshot({ path: f }); + files.push(f); + } + if (errors.length) console.error(`${theme}: page errors:\n ${[...new Set(errors)].join('\n ')}`); + await ctx.close(); +} +await browser.close(); +console.log(files.join('\n')); diff --git a/apps/desktop/src-tauri/Cargo.lock b/apps/desktop/src-tauri/Cargo.lock index 262144c..d262f3f 100644 --- a/apps/desktop/src-tauri/Cargo.lock +++ b/apps/desktop/src-tauri/Cargo.lock @@ -1090,6 +1090,18 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "fallible-iterator" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649" + +[[package]] +name = "fallible-streaming-iterator" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a" + [[package]] name = "fastrand" version = "2.5.0" @@ -1577,12 +1589,30 @@ version = "0.14.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" +[[package]] +name = "hashbrown" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" +dependencies = [ + "foldhash", +] + [[package]] name = "hashbrown" version = "0.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" +[[package]] +name = "hashlink" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "824e001ac4f3012dd16a264bec811403a67ca9deb6c102fc5049b32c4574b35f" +dependencies = [ + "hashbrown 0.16.1", +] + [[package]] name = "heck" version = "0.4.1" @@ -2219,6 +2249,17 @@ dependencies = [ "libc", ] +[[package]] +name = "libsqlite3-sys" +version = "0.36.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95b4103cffefa72eb8428cb6b47d6627161e51c2739fc5e3b734584157bc642a" +dependencies = [ + "cc", + "pkg-config", + "vcpkg", +] + [[package]] name = "linux-raw-sys" version = "0.12.1" @@ -3337,6 +3378,31 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "rsqlite-vfs" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c51c9ae4df8a7fba42103df5c621fa3c37eccf3a3c650879e90fc48b11cc192c" +dependencies = [ + "hashbrown 0.16.1", + "thiserror 2.0.21", +] + +[[package]] +name = "rusqlite" +version = "0.38.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1c93dd1c9683b438c392c492109cb702b8090b2bfc8fed6f6e4eb4523f17af3" +dependencies = [ + "bitflags 2.13.2", + "fallible-iterator", + "fallible-streaming-iterator", + "hashlink", + "libsqlite3-sys", + "smallvec", + "sqlite-wasm-rs", +] + [[package]] name = "rust-ini" version = "0.21.3" @@ -3918,6 +3984,18 @@ dependencies = [ "system-deps", ] +[[package]] +name = "sqlite-wasm-rs" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc3efc0da82635d7e1ced0053bbbfa8c7ab9645d0bf36ceb4f7127bb85315d75" +dependencies = [ + "cc", + "js-sys", + "rsqlite-vfs", + "wasm-bindgen", +] + [[package]] name = "stable_deref_trait" version = "1.2.1" @@ -3938,6 +4016,9 @@ dependencies = [ "getrandom 0.3.4", "hmac", "keyring", + "reqwest", + "rusqlite", + "rustls", "serde", "serde_json", "sha2", @@ -3955,6 +4036,7 @@ dependencies = [ "tempfile", "tokio", "url", + "zip", ] [[package]] @@ -5040,6 +5122,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + [[package]] name = "version-compare" version = "0.2.1" @@ -6078,6 +6166,7 @@ checksum = "caa8cd6af31c3b31c6631b8f483848b91589021b28fffe50adada48d4f4d2ed1" dependencies = [ "arbitrary", "crc32fast", + "flate2", "indexmap 2.14.2", "memchr", ] diff --git a/apps/desktop/src-tauri/Cargo.toml b/apps/desktop/src-tauri/Cargo.toml index cad1df1..301fe2e 100644 --- a/apps/desktop/src-tauri/Cargo.toml +++ b/apps/desktop/src-tauri/Cargo.toml @@ -41,6 +41,12 @@ base64 = "0.22" getrandom = "0.3" url = "2" tokio = { version = "1", features = ["sync", "time"] } +# Offline cache of the last store reads (app data cache.sqlite); SQLite compiled in, nothing system-wide. +rusqlite = { version = "0.38", features = ["bundled"] } +# Dependencies screen downloads (ffmpeg, Chrome for Testing): the same HTTP stack the updater already brings. +reqwest = { version = "0.13", default-features = false, features = ["rustls-no-provider", "system-proxy"] } +rustls = { version = "0.23", default-features = false, features = ["ring", "std", "tls12"] } +zip = { version = "4", default-features = false, features = ["deflate-flate2-zlib-rs"] } [dev-dependencies] tempfile = "3" diff --git a/apps/desktop/src-tauri/capabilities/main.json b/apps/desktop/src-tauri/capabilities/main.json index f78cc80..2ef3bf4 100644 --- a/apps/desktop/src-tauri/capabilities/main.json +++ b/apps/desktop/src-tauri/capabilities/main.json @@ -1,8 +1,10 @@ { "$schema": "../gen/schemas/desktop-schema.json", "identifier": "main-window", - "description": "The main window with the app's own bundled pages only (no remote content). It may listen to app events (sidecar progress, deep links) and call the storebound app commands; every command re-checks the window label and page origin in Rust. No plugin API is granted to the webview: the sidecar is spawned from Rust with fixed arguments, dialogs, the opener, notifications, the updater and process control are driven from Rust only, and credentials never reach the page.", - "windows": ["main"], + "description": "The main window with the app's own bundled pages only (no remote content). It may listen to app events (sidecar progress, tool downloads, deep links) and call the storebound app commands; every command re-checks the window label and page origin in Rust. No plugin API is granted to the webview: the sidecar is spawned from Rust with fixed arguments, dialogs, the opener, notifications, the updater and process control are driven from Rust only, and credentials never reach the page.", + "windows": [ + "main" + ], "local": true, "permissions": [ "core:event:allow-listen", @@ -10,9 +12,13 @@ "allow-app-info", "allow-sidecar-status", "allow-sidecar-call", + "allow-cache-get", + "allow-cache-clear", + "allow-job-cancel", "allow-write-with-confirm", "allow-projects-list", "allow-project-add", + "allow-project-init", "allow-project-remove", "allow-project-set-accounts", "allow-accounts-list", @@ -20,6 +26,9 @@ "allow-account-add-apple", "allow-account-remove", "allow-portfolio-status", - "allow-open-external" + "allow-open-external", + "allow-tools-info", + "allow-tools-install", + "allow-tools-uninstall" ] } diff --git a/apps/desktop/src-tauri/permissions/autogenerated/cache_clear.toml b/apps/desktop/src-tauri/permissions/autogenerated/cache_clear.toml new file mode 100644 index 0000000..935428c --- /dev/null +++ b/apps/desktop/src-tauri/permissions/autogenerated/cache_clear.toml @@ -0,0 +1,11 @@ +# Automatically generated - DO NOT EDIT! + +[[permission]] +identifier = "allow-cache-clear" +description = "Enables the cache_clear command without any pre-configured scope." +commands.allow = ["cache_clear"] + +[[permission]] +identifier = "deny-cache-clear" +description = "Denies the cache_clear command without any pre-configured scope." +commands.deny = ["cache_clear"] diff --git a/apps/desktop/src-tauri/permissions/autogenerated/cache_get.toml b/apps/desktop/src-tauri/permissions/autogenerated/cache_get.toml new file mode 100644 index 0000000..c6b73dc --- /dev/null +++ b/apps/desktop/src-tauri/permissions/autogenerated/cache_get.toml @@ -0,0 +1,11 @@ +# Automatically generated - DO NOT EDIT! + +[[permission]] +identifier = "allow-cache-get" +description = "Enables the cache_get command without any pre-configured scope." +commands.allow = ["cache_get"] + +[[permission]] +identifier = "deny-cache-get" +description = "Denies the cache_get command without any pre-configured scope." +commands.deny = ["cache_get"] diff --git a/apps/desktop/src-tauri/permissions/autogenerated/job_cancel.toml b/apps/desktop/src-tauri/permissions/autogenerated/job_cancel.toml new file mode 100644 index 0000000..4ff0353 --- /dev/null +++ b/apps/desktop/src-tauri/permissions/autogenerated/job_cancel.toml @@ -0,0 +1,11 @@ +# Automatically generated - DO NOT EDIT! + +[[permission]] +identifier = "allow-job-cancel" +description = "Enables the job_cancel command without any pre-configured scope." +commands.allow = ["job_cancel"] + +[[permission]] +identifier = "deny-job-cancel" +description = "Denies the job_cancel command without any pre-configured scope." +commands.deny = ["job_cancel"] diff --git a/apps/desktop/src-tauri/permissions/autogenerated/project_init.toml b/apps/desktop/src-tauri/permissions/autogenerated/project_init.toml new file mode 100644 index 0000000..f042840 --- /dev/null +++ b/apps/desktop/src-tauri/permissions/autogenerated/project_init.toml @@ -0,0 +1,11 @@ +# Automatically generated - DO NOT EDIT! + +[[permission]] +identifier = "allow-project-init" +description = "Enables the project_init command without any pre-configured scope." +commands.allow = ["project_init"] + +[[permission]] +identifier = "deny-project-init" +description = "Denies the project_init command without any pre-configured scope." +commands.deny = ["project_init"] diff --git a/apps/desktop/src-tauri/permissions/autogenerated/tools_info.toml b/apps/desktop/src-tauri/permissions/autogenerated/tools_info.toml new file mode 100644 index 0000000..789f7f4 --- /dev/null +++ b/apps/desktop/src-tauri/permissions/autogenerated/tools_info.toml @@ -0,0 +1,11 @@ +# Automatically generated - DO NOT EDIT! + +[[permission]] +identifier = "allow-tools-info" +description = "Enables the tools_info command without any pre-configured scope." +commands.allow = ["tools_info"] + +[[permission]] +identifier = "deny-tools-info" +description = "Denies the tools_info command without any pre-configured scope." +commands.deny = ["tools_info"] diff --git a/apps/desktop/src-tauri/permissions/autogenerated/tools_install.toml b/apps/desktop/src-tauri/permissions/autogenerated/tools_install.toml new file mode 100644 index 0000000..39e7086 --- /dev/null +++ b/apps/desktop/src-tauri/permissions/autogenerated/tools_install.toml @@ -0,0 +1,11 @@ +# Automatically generated - DO NOT EDIT! + +[[permission]] +identifier = "allow-tools-install" +description = "Enables the tools_install command without any pre-configured scope." +commands.allow = ["tools_install"] + +[[permission]] +identifier = "deny-tools-install" +description = "Denies the tools_install command without any pre-configured scope." +commands.deny = ["tools_install"] diff --git a/apps/desktop/src-tauri/permissions/autogenerated/tools_uninstall.toml b/apps/desktop/src-tauri/permissions/autogenerated/tools_uninstall.toml new file mode 100644 index 0000000..3d8fe2a --- /dev/null +++ b/apps/desktop/src-tauri/permissions/autogenerated/tools_uninstall.toml @@ -0,0 +1,11 @@ +# Automatically generated - DO NOT EDIT! + +[[permission]] +identifier = "allow-tools-uninstall" +description = "Enables the tools_uninstall command without any pre-configured scope." +commands.allow = ["tools_uninstall"] + +[[permission]] +identifier = "deny-tools-uninstall" +description = "Denies the tools_uninstall command without any pre-configured scope." +commands.deny = ["tools_uninstall"] diff --git a/apps/desktop/src-tauri/src/cache.rs b/apps/desktop/src-tauri/src/cache.rs new file mode 100644 index 0000000..3bafb86 --- /dev/null +++ b/apps/desktop/src-tauri/src/cache.rs @@ -0,0 +1,102 @@ +//! Offline cache of the last successful reads (app data `cache.sqlite`): store status, health, reviews, TestFlight, +//! catalog parity, listings, icons. The window shows a cached value with its "last read" time while nothing newer is +//! there, and says "unknown" when there is none; a cached value is never shown as fresh. Secrets are never cached +//! (results only, never params), and a project's rows go when the project is removed. +use std::path::Path; +use std::sync::Mutex; + +use rusqlite::{params, Connection, OptionalExtension}; +use serde::Serialize; +use serde_json::Value; + +pub const FILE: &str = "cache.sqlite"; +/// Results above this size are not kept (logs of a long render, say); the window reads them live. +pub const MAX_VALUE_BYTES: usize = 4 * 1024 * 1024; + +pub struct Cache { + db: Mutex, +} + +#[derive(Debug, Clone, PartialEq, Serialize)] +pub struct Entry { + pub value: Value, + /// When it was read (RFC 3339 UTC). + pub at: String, +} + +/// Cache key: project id, method and the canonical arguments (the same canonical JSON as confirm tokens). +pub fn key(project: &str, method: &str, args: &Value) -> String { + let a = crate::confirm::canonical(args).unwrap_or_else(|_| args.to_string()); + format!("{project}\u{1f}{method}\u{1f}{a}") +} + +impl Cache { + pub fn open(dir: &Path) -> Result { + std::fs::create_dir_all(dir).map_err(|e| e.to_string())?; + Cache::with(Connection::open(dir.join(FILE)).map_err(|e| e.to_string())?) + } + + pub fn in_memory() -> Result { + Cache::with(Connection::open_in_memory().map_err(|e| e.to_string())?) + } + + fn with(db: Connection) -> Result { + db.execute_batch( + "PRAGMA journal_mode=WAL; + CREATE TABLE IF NOT EXISTS reads ( + key TEXT PRIMARY KEY, + project TEXT NOT NULL, + method TEXT NOT NULL, + value TEXT NOT NULL, + at TEXT NOT NULL + ); + CREATE INDEX IF NOT EXISTS reads_project ON reads(project);", + ) + .map_err(|e| e.to_string())?; + Ok(Cache { db: Mutex::new(db) }) + } + + pub fn put(&self, project: &str, method: &str, args: &Value, value: &Value, at: &str) -> Result<(), String> { + let text = value.to_string(); + if text.len() > MAX_VALUE_BYTES { + return Ok(()); + } + let db = self.db.lock().map_err(|_| "cache poisoned")?; + db.execute( + "INSERT INTO reads(key, project, method, value, at) VALUES (?1, ?2, ?3, ?4, ?5) + ON CONFLICT(key) DO UPDATE SET value = excluded.value, at = excluded.at", + params![key(project, method, args), project, method, text, at], + ) + .map_err(|e| e.to_string())?; + Ok(()) + } + + pub fn get(&self, project: &str, method: &str, args: &Value) -> Result, String> { + let db = self.db.lock().map_err(|_| "cache poisoned")?; + let row: Option<(String, String)> = db + .query_row("SELECT value, at FROM reads WHERE key = ?1", params![key(project, method, args)], |r| Ok((r.get(0)?, r.get(1)?))) + .optional() + .map_err(|e| e.to_string())?; + Ok(row.and_then(|(v, at)| serde_json::from_str(&v).ok().map(|value| Entry { value, at }))) + } + + /// Every cached method of a project with its time (for the "last read" stamps). + pub fn stamps(&self, project: &str) -> Result, String> { + let db = self.db.lock().map_err(|_| "cache poisoned")?; + let mut st = db.prepare("SELECT method, MAX(at) FROM reads WHERE project = ?1 GROUP BY method ORDER BY method").map_err(|e| e.to_string())?; + let rows = st.query_map(params![project], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?))).map_err(|e| e.to_string())?; + rows.collect::, _>>().map_err(|e| e.to_string()) + } + + pub fn forget_project(&self, project: &str) -> Result<(), String> { + let db = self.db.lock().map_err(|_| "cache poisoned")?; + db.execute("DELETE FROM reads WHERE project = ?1", params![project]).map_err(|e| e.to_string())?; + Ok(()) + } + + pub fn clear(&self) -> Result<(), String> { + let db = self.db.lock().map_err(|_| "cache poisoned")?; + db.execute("DELETE FROM reads", []).map_err(|e| e.to_string())?; + Ok(()) + } +} diff --git a/apps/desktop/src-tauri/src/commands.rs b/apps/desktop/src-tauri/src/commands.rs index 73d9fcf..aaca49b 100644 --- a/apps/desktop/src-tauri/src/commands.rs +++ b/apps/desktop/src-tauri/src/commands.rs @@ -2,17 +2,20 @@ //! the webview: account files are picked in native dialogs and read here, and credentials go from the keychain //! straight into the sidecar call. use std::path::{Path, PathBuf}; +use std::sync::Arc; use std::sync::Mutex; use serde::Serialize; use serde_json::{json, Map, Value}; -use tauri::{AppHandle, Manager, State, WebviewWindow}; +use tauri::{AppHandle, Emitter, Manager, State, WebviewWindow}; use tauri_plugin_dialog::{DialogExt, MessageDialogButtons, MessageDialogKind}; use tauri_plugin_opener::OpenerExt; use crate::accounts::{self, Account, AccountKind, Secret, SecretStore}; +use crate::cache::{Cache, Entry}; +use crate::downloads; use crate::policy::{self, Kind}; -use crate::sidecar::{Fail, Sidecar}; +use crate::sidecar::{valid_job, Fail, Progress, Sidecar}; use crate::store::{self, Project, State as Saved}; pub struct AppState { @@ -20,6 +23,8 @@ pub struct AppState { pub data_dir: PathBuf, pub saved: Mutex, pub secrets: Box, + pub cache: Cache, + pub downloads: downloads::Jobs, } type Res = Result; @@ -37,6 +42,13 @@ fn persist(s: &AppState, st: &Saved) -> Res<()> { store::save(&s.data_dir, st).map_err(Fail::host) } +fn job_of(job: Option) -> Res> { + match job { + Some(j) if !valid_job(&j) => Err(Fail::host("bad job id")), + j => Ok(j), + } +} + /// Project folder + credentials of its mapped accounts, as sidecar params. fn project_params(s: &AppState, project_id: &str) -> Res<(Project, Map)> { let p = saved(s)?.projects.iter().find(|p| p.id == project_id).cloned().ok_or_else(|| Fail::host("unknown project"))?; @@ -81,12 +93,16 @@ pub async fn sidecar_status(window: WebviewWindow, app: AppHandle, state: State< } /// A read (or dry-run write) through the sidecar. Writes from here are always dry runs (policy::sanitize_js_call). +/// Successful reads of a project are kept in the offline cache (results only, never the credentials). #[tauri::command] -pub async fn sidecar_call(window: WebviewWindow, app: AppHandle, state: State<'_, AppState>, method: String, project_id: Option, params: Option) -> Res { +pub async fn sidecar_call(window: WebviewWindow, app: AppHandle, state: State<'_, AppState>, method: String, project_id: Option, params: Option, job: Option) -> Res { guard(&window)?; + let job = job_of(job)?; let r = state.sidecar.get(&app).await?; let kind = r.kind(&method); let mut p = policy::sanitize_js_call(&method, kind, ¶ms.unwrap_or(Value::Null)).map_err(Fail::host)?; + let args = p.get("args").cloned().unwrap_or_else(|| json!({})); + let mut cache_project = None; if matches!(kind, Some(Kind::Read | Kind::Write)) { let id = project_id.ok_or_else(|| Fail::host(format!("{method} needs a project")))?; let (proj, creds) = project_params(&state, &id)?; @@ -95,8 +111,46 @@ pub async fn sidecar_call(window: WebviewWindow, app: AppHandle, state: State<'_ if !creds.is_empty() && method != "project.inspect" { o.insert("credentials".into(), Value::Object(creds)); } + if kind == Some(Kind::Read) { + cache_project = Some(id); + } } - r.call(&method, p).await + let res = r.call_job(&method, p, job.as_deref()).await?; + if let Some(id) = cache_project { + // only results that read something: a failed store read must not hide the last good one + if res["ok"] == Value::Bool(true) && !res["data"].is_null() { + let _ = state.cache.put(&id, &method, &args, &res, &store::now_iso()); + } + } + Ok(res) +} + +/// The last cached result of a read (`project_id` "*" for the portfolio), with its time; null when never read. +#[tauri::command] +pub fn cache_get(window: WebviewWindow, state: State<'_, AppState>, method: String, project_id: String, args: Option) -> Res> { + guard(&window)?; + state.cache.get(&project_id, &method, &args.unwrap_or_else(|| json!({}))).map_err(Fail::host) +} + +#[tauri::command] +pub fn cache_clear(window: WebviewWindow, state: State<'_, AppState>) -> Res<()> { + guard(&window)?; + state.cache.clear().map_err(Fail::host) +} + +/// Stops a running job: its sidecar requests (`$/cancelRequest`) and a running download. +#[tauri::command] +pub async fn job_cancel(window: WebviewWindow, state: State<'_, AppState>, job: String) -> Res { + guard(&window)?; + if !valid_job(&job) { + return Err(Fail::host("bad job id")); + } + let mut hit = state.downloads.cancel(&job); + if let Some(r) = state.sidecar.current().await { + hit |= r.cancel_job(&job) > 0; + hit |= r.cancel_job(&format!("{job}:p")) > 0; + } + Ok(hit) } const DIALOG_LINES: usize = 28; @@ -135,14 +189,32 @@ pub fn dialog_summary(title: &str, description: &str, project: &str, network: bo s } +/// Native Apply / Cancel dialog; true on Apply. +async fn confirm_dialog(window: &WebviewWindow, title: String, text: String) -> Res { + let w = window.clone(); + tauri::async_runtime::spawn_blocking(move || { + w.app_handle() + .dialog() + .message(text) + .title(title) + .kind(MessageDialogKind::Warning) + .buttons(MessageDialogButtons::OkCancelCustom("Apply".into(), "Cancel".into())) + .parent(&w) + .blocking_show() + }) + .await + .map_err(|e| Fail::host(e.to_string())) +} + /// Dry run, then a native dialog with the summary and the dry run, then (only on Apply) the real run with a confirm /// token minted here for exactly this method, project and arguments. #[tauri::command] -pub async fn write_with_confirm(window: WebviewWindow, app: AppHandle, state: State<'_, AppState>, method: String, project_id: String, args: Option) -> Res { +pub async fn write_with_confirm(window: WebviewWindow, app: AppHandle, state: State<'_, AppState>, method: String, project_id: String, args: Option, job: Option) -> Res { guard(&window)?; + let job = job_of(job)?; let r = state.sidecar.get(&app).await?; - if r.kind(&method) != Some(Kind::Write) { - return Err(Fail::host(format!("{method} is not a write method"))); + if r.kind(&method) != Some(Kind::Write) || method == "project.init" { + return Err(Fail::host(format!("{method} is not a write method of an app folder"))); } let d = r.descriptor(&method).cloned().ok_or_else(|| Fail::host("unknown method"))?; let args = match args.unwrap_or(json!({})) { @@ -154,7 +226,7 @@ pub async fn write_with_confirm(window: WebviewWindow, app: AppHandle, state: St if !creds.is_empty() { dry_params["credentials"] = Value::Object(creds.clone()); } - let dry = r.call(&method, dry_params).await?; + let dry = r.call_job(&method, dry_params, job.as_deref()).await?; if dry["ok"] != Value::Bool(true) { return Ok(json!({ "confirmed": false, "stage": "dry-run", "dryRun": dry })); } @@ -165,22 +237,7 @@ pub async fn write_with_confirm(window: WebviewWindow, app: AppHandle, state: St dialog_excerpt(dry["output"].as_str().unwrap_or_default()) ); let home = app.path().home_dir().ok().map(|h| h.to_string_lossy().to_string()); - let text = tilde(&text, home.as_deref()); - let w = window.clone(); - let title = format!("Apply: {}", d.title); - let yes = tauri::async_runtime::spawn_blocking(move || { - w.app_handle() - .dialog() - .message(text) - .title(title) - .kind(MessageDialogKind::Warning) - .buttons(MessageDialogButtons::OkCancelCustom("Apply".into(), "Cancel".into())) - .parent(&w) - .blocking_show() - }) - .await - .map_err(|e| Fail::host(e.to_string()))?; - if !yes { + if !confirm_dialog(&window, format!("Apply: {}", d.title), tilde(&text, home.as_deref())).await? { return Ok(json!({ "confirmed": false, "stage": "dialog", "dryRun": dry })); } let bound = json!({ "project": proj.path, "args": args }); @@ -189,7 +246,7 @@ pub async fn write_with_confirm(window: WebviewWindow, app: AppHandle, state: St if !creds.is_empty() { real["credentials"] = Value::Object(creds); } - let result = r.call(&method, real).await?; + let result = r.call_job(&method, real, job.as_deref()).await?; Ok(json!({ "confirmed": true, "stage": "done", "dryRun": dry, "result": result })) } @@ -205,25 +262,23 @@ pub fn has_config(dir: &Path) -> bool { CONFIG_NAMES.iter().any(|n| dir.join(n).is_file()) } -/// Native folder picker; the folder must contain storebound.config.ts and load (project.inspect). None = cancelled. -#[tauri::command] -pub async fn project_add(window: WebviewWindow, app: AppHandle, state: State<'_, AppState>) -> Res> { - guard(&window)?; +async fn pick_folder(window: &WebviewWindow, title: &'static str) -> Res> { let w = window.clone(); - let picked = tauri::async_runtime::spawn_blocking(move || w.app_handle().dialog().file().set_title("Add a game folder (with storebound.config.ts)").set_parent(&w).blocking_pick_folder()) + let picked = tauri::async_runtime::spawn_blocking(move || w.app_handle().dialog().file().set_title(title).set_parent(&w).blocking_pick_folder()) .await .map_err(|e| Fail::host(e.to_string()))?; let Some(picked) = picked else { return Ok(None) }; let dir = picked.into_path().map_err(|e| Fail::host(e.to_string()))?; - let dir = std::fs::canonicalize(&dir).unwrap_or(dir); - if !has_config(&dir) { - return Err(Fail::host(format!("no storebound.config.ts in {}", dir.display()))); - } + Ok(Some(std::fs::canonicalize(&dir).unwrap_or(dir))) +} + +/// Adds a folder that has a config (inspected by the sidecar) to the list. +async fn add_folder(app: &AppHandle, state: &AppState, dir: &Path) -> Res { let path = dir.to_string_lossy().to_string(); - if saved(&state)?.projects.iter().any(|p| p.path == path) { + if saved(state)?.projects.iter().any(|p| p.path == path) { return Err(Fail::host("this folder is already in the list")); } - let r = state.sidecar.get(&app).await?; + let r = state.sidecar.get(app).await?; let summary = r.call("project.inspect", json!({ "project": path })).await?; let project = Project { id: store::new_id()?, @@ -233,10 +288,58 @@ pub async fn project_add(window: WebviewWindow, app: AppHandle, state: State<'_, apple_account: None, added: store::now_iso(), }; - let mut st = saved(&state)?; + let mut st = saved(state)?; st.projects.push(project.clone()); - persist(&state, &st)?; - Ok(Some(project)) + persist(state, &st)?; + Ok(project) +} + +/// Native folder picker; the folder must contain storebound.config.ts and load (project.inspect). None = cancelled. +#[tauri::command] +pub async fn project_add(window: WebviewWindow, app: AppHandle, state: State<'_, AppState>) -> Res> { + guard(&window)?; + let Some(dir) = pick_folder(&window, "Add an app folder (with storebound.config.ts)").await? else { return Ok(None) }; + if !has_config(&dir) { + return Err(Fail::host(format!("no storebound.config.ts in {}", dir.display()))); + } + add_folder(&app, &state, &dir).await.map(Some) +} + +pub const PROFILES: [&str; 3] = ["app", "game", "subscription-app"]; + +/// `storebound init` for a new app folder: native folder picker, the dry run in a native dialog, then the templates +/// for `profile` are written (existing files kept) and the folder is added. None = cancelled at any step. +#[tauri::command] +pub async fn project_init(window: WebviewWindow, app: AppHandle, state: State<'_, AppState>, profile: String) -> Res> { + guard(&window)?; + if !PROFILES.contains(&profile.as_str()) { + return Err(Fail::host("profile: app, game or subscription-app")); + } + let Some(dir) = pick_folder(&window, "Choose the app's folder (storebound files go into it)").await? else { return Ok(None) }; + if has_config(&dir) { + return Err(Fail::host("this folder already has a storebound config: add it instead")); + } + let path = dir.to_string_lossy().to_string(); + let args = json!({ "profile": profile }); + let r = state.sidecar.get(&app).await?; + let dry = r.call("project.init", json!({ "project": path, "args": args, "dryRun": true })).await?; + if dry["ok"] != Value::Bool(true) { + return Err(Fail::host(format!("init dry run failed: {}", dialog_excerpt(dry["output"].as_str().unwrap_or_default())))); + } + let home = app.path().home_dir().ok().map(|h| h.to_string_lossy().to_string()); + let text = format!( + "Start an app folder · {profile} profile\n\n{path}\n\nCopies the storebound templates into this folder; existing files are kept. Every answer in them is a starting point to edit.\n\n{}", + dialog_excerpt(dry["output"].as_str().unwrap_or_default()) + ); + if !confirm_dialog(&window, "Apply: Start an app folder".into(), tilde(&text, home.as_deref())).await? { + return Ok(None); + } + let token = r.mint("project.init", &json!({ "project": path, "args": args })).map_err(Fail::host)?; + let res = r.call("project.init", json!({ "project": path, "args": args, "dryRun": false, "confirmToken": token })).await?; + if res["ok"] != Value::Bool(true) { + return Err(Fail::host(format!("init failed: {}", dialog_excerpt(res["output"].as_str().unwrap_or_default())))); + } + add_folder(&app, &state, &dir).await.map(Some) } #[tauri::command] @@ -244,6 +347,7 @@ pub fn project_remove(window: WebviewWindow, state: State<'_, AppState>, id: Str guard(&window)?; let mut st = saved(&state)?; st.projects.retain(|p| p.id != id); + let _ = state.cache.forget_project(&id); persist(&state, &st) } @@ -342,10 +446,16 @@ pub fn account_remove(window: WebviewWindow, state: State<'_, AppState>, id: Str persist(&state, &st) } -/// `portfolio.status` over every project. Offline unless `live`; live runs each project with its own accounts. +/// Store reads run at most this many apps at once (each is two status reads). +const LIVE_PARALLEL: usize = 4; + +/// `portfolio.status` over every project. Offline unless `live`; live reads every app with its own accounts, several +/// at once, and sends one progress line per finished app (job progress n / total). Each live app result is cached; +/// the whole live result too (project "*"), so the portfolio shows the last read when offline. #[tauri::command] -pub async fn portfolio_status(window: WebviewWindow, app: AppHandle, state: State<'_, AppState>, live: Option) -> Res { +pub async fn portfolio_status(window: WebviewWindow, app: AppHandle, state: State<'_, AppState>, live: Option, job: Option) -> Res { guard(&window)?; + let job = job_of(job)?; let projects = saved(&state)?.projects.clone(); if projects.is_empty() { return Ok(json!({ "apps": [], "at": store::now_iso() })); @@ -353,15 +463,37 @@ pub async fn portfolio_status(window: WebviewWindow, app: AppHandle, state: Stat let r = state.sidecar.get(&app).await?; if live != Some(true) { let paths: Vec<&str> = projects.iter().map(|p| p.path.as_str()).collect(); - return r.call("portfolio.status", json!({ "projects": paths })).await; + return r.call_job("portfolio.status", json!({ "projects": paths }), job.as_deref()).await; } - let mut all = Vec::new(); + let total = projects.len(); + let sem = Arc::new(tokio::sync::Semaphore::new(LIVE_PARALLEL)); + let done = Arc::new(std::sync::atomic::AtomicUsize::new(0)); + let mut handles = Vec::new(); for p in &projects { let (_, creds) = project_params(&state, &p.id)?; - let res = r.call("portfolio.status", json!({ "projects": [p.path], "live": true, "credentials": creds })).await?; - all.extend(res["apps"].as_array().cloned().unwrap_or_default()); + let (r, sem, done, app, job, path, id) = (r.clone(), sem.clone(), done.clone(), app.clone(), job.clone(), p.path.clone(), p.id.clone()); + handles.push(tauri::async_runtime::spawn(async move { + let _permit = sem.acquire_owned().await.ok(); + let sub = job.as_ref().map(|j| format!("{j}:p")); + let res = r.call_job("portfolio.status", json!({ "projects": [path], "live": true, "credentials": creds }), sub.as_deref()).await; + let n = done.fetch_add(1, std::sync::atomic::Ordering::SeqCst) + 1; + let _ = app.emit_to(policy::MAIN, "sidecar-progress", Progress { params: json!({ "requestId": 0, "n": n, "total": total, "project": path }), job: job.clone() }); + (id, res) + })); } - Ok(json!({ "apps": all, "at": store::now_iso() })) + let mut all = Vec::new(); + let at = store::now_iso(); + for h in handles { + let (id, res) = h.await.map_err(|e| Fail::host(e.to_string()))?; + let res = res?; + for a in res["apps"].as_array().cloned().unwrap_or_default() { + let _ = state.cache.put(&id, "portfolio.app", &json!({}), &a, &at); + all.push(a); + } + } + let out = json!({ "apps": all, "at": at }); + let _ = state.cache.put("*", "portfolio.status", &json!({ "live": true }), &out, &at); + Ok(out) } #[tauri::command] @@ -373,3 +505,39 @@ pub fn open_external(window: WebviewWindow, app: AppHandle, url: String) -> Res< } app.opener().open_url(u.as_str(), None::<&str>).map_err(|e| Fail::host(e.to_string())) } + +/// What the Dependencies screen can download on this platform, and what is installed. +#[tauri::command] +pub fn tools_info(window: WebviewWindow, state: State<'_, AppState>) -> Res> { + guard(&window)?; + downloads::info(&state.data_dir).map_err(Fail::host) +} + +/// Downloads, verifies and installs ffmpeg or Chrome for Testing into the app data folder (progress: `tool-download` +/// events with the job id; job_cancel stops it). The sidecar restarts afterwards so it sees the new tool. +#[tauri::command] +pub async fn tools_install(window: WebviewWindow, app: AppHandle, state: State<'_, AppState>, tool: String, job: String) -> Res { + guard(&window)?; + if !valid_job(&job) || !matches!(tool.as_str(), "ffmpeg" | "chrome") { + return Err(Fail::host("bad tool or job")); + } + let flag = state.downloads.start(&job).map_err(Fail::host)?; + let handle = app.clone(); + let res = downloads::install(&state.data_dir, &tool, &job, flag, move |e| { + let _ = handle.emit_to(policy::MAIN, "tool-download", e); + }) + .await; + state.downloads.finish(&job); + if res.is_ok() { + state.sidecar.shutdown().await; + } + res.map_err(Fail::host) +} + +#[tauri::command] +pub async fn tools_uninstall(window: WebviewWindow, state: State<'_, AppState>, tool: String) -> Res<()> { + guard(&window)?; + downloads::uninstall(&state.data_dir, &tool).map_err(Fail::host)?; + state.sidecar.shutdown().await; + Ok(()) +} diff --git a/apps/desktop/src-tauri/src/commands_list.rs b/apps/desktop/src-tauri/src/commands_list.rs index 2f21eac..ce2e1fc 100644 --- a/apps/desktop/src-tauri/src/commands_list.rs +++ b/apps/desktop/src-tauri/src/commands_list.rs @@ -4,9 +4,13 @@ pub const COMMANDS: &[&str] = &[ "app_info", "sidecar_status", "sidecar_call", + "cache_get", + "cache_clear", + "job_cancel", "write_with_confirm", "projects_list", "project_add", + "project_init", "project_remove", "project_set_accounts", "accounts_list", @@ -15,4 +19,7 @@ pub const COMMANDS: &[&str] = &[ "account_remove", "portfolio_status", "open_external", + "tools_info", + "tools_install", + "tools_uninstall", ]; diff --git a/apps/desktop/src-tauri/src/downloads.rs b/apps/desktop/src-tauri/src/downloads.rs new file mode 100644 index 0000000..c640af4 --- /dev/null +++ b/apps/desktop/src-tauri/src/downloads.rs @@ -0,0 +1,454 @@ +//! One-click downloads of the optional local tools (Dependencies screen) into the app data folder, never system-wide +//! and never bundled with the installer (docs/desktop.md): +//! +//! - **ffmpeg + ffprobe**: the exact archives of `tools-manifest.json` (URL, SHA-256, size; written by +//! tools/tools-manifest.ts per desktop release). The SHA-256 is checked while downloading; a mismatch deletes the file. +//! - **Chrome for Testing**: Google's official JSON endpoint (Stable channel), archive from storage.googleapis.com. No +//! hashes are published, so the unpacked browser must carry Google's code signature (macOS team EQHXZ8M8AV, +//! Windows Authenticode by Google LLC); on Linux the version banner is checked. +//! +//! Everything happens in a staging folder; only a verified result replaces the installed copy. Progress goes to the +//! window as `tool-download` events with the job id; cancel removes the staging folder. Redirects may only lead to +//! the hosts in DOWNLOAD_HOSTS, over HTTPS. +use std::collections::HashMap; +use std::fs; +use std::io::Write; +use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Arc, Mutex}; + +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; +use url::Url; + +pub const MANIFEST_JSON: &str = include_str!("../tools-manifest.json"); + +/// Hosts a download (or one of its redirects) may touch. +pub const DOWNLOAD_HOSTS: &[&str] = &[ + "ffmpeg.martin-riedl.de", + "github.com", + "objects.githubusercontent.com", + "release-assets.githubusercontent.com", + "googlechromelabs.github.io", + "storage.googleapis.com", +]; + +#[derive(Debug, Clone, Deserialize, Serialize)] +pub struct FileMap { + pub from: String, + pub to: String, +} + +#[derive(Debug, Clone, Deserialize, Serialize)] +pub struct Archive { + pub url: String, + pub sha256: String, + pub size: u64, + pub kind: String, + pub files: Vec, +} + +#[derive(Debug, Clone, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct Ffmpeg { + pub version: String, + pub license: String, + pub license_url: String, + pub sources: HashMap, + pub targets: HashMap>, +} + +#[derive(Debug, Clone, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct Chrome { + pub endpoint: String, + pub channel: String, + pub platforms: HashMap, + pub mac_team_id: String, + pub windows_signer: String, + pub license: String, + pub license_url: String, +} + +#[derive(Debug, Clone, Deserialize, Serialize)] +pub struct Manifest { + pub generated: String, + pub ffmpeg: Ffmpeg, + pub chrome: Chrome, +} + +pub fn manifest() -> Result { + serde_json::from_str(MANIFEST_JSON).map_err(|e| format!("tools manifest: {e}")) +} + +/// Rust target triple of this build (the manifest's keys). +pub fn target_triple() -> &'static str { + if cfg!(all(target_os = "macos", target_arch = "aarch64")) { + "aarch64-apple-darwin" + } else if cfg!(target_os = "macos") { + "x86_64-apple-darwin" + } else if cfg!(all(target_os = "linux", target_arch = "aarch64")) { + "aarch64-unknown-linux-gnu" + } else if cfg!(target_os = "linux") { + "x86_64-unknown-linux-gnu" + } else { + "x86_64-pc-windows-msvc" + } +} + +pub fn download_allowed(u: &Url) -> bool { + u.scheme() == "https" && u.username().is_empty() && u.password().is_none() && u.port().is_none() && u.host_str().is_some_and(|h| DOWNLOAD_HOSTS.contains(&h)) +} + +pub fn hex(b: &[u8]) -> String { + crate::confirm::hex(b) +} + +/// What the Dependencies screen shows next to the Download buttons. +#[derive(Debug, Clone, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct DownloadInfo { + pub tool: &'static str, + pub available: bool, + pub version: Option, + pub size: Option, + pub license: String, + pub license_url: String, + pub source: String, + pub installed: bool, + pub folder: String, +} + +pub fn info(data_dir: &Path) -> Result, String> { + let m = manifest()?; + let t = target_triple(); + let ff = m.ffmpeg.targets.get(t); + let os = if t.contains("apple") { "macos" } else if t.contains("windows") { "windows" } else { "linux" }; + let exe = if cfg!(windows) { ".exe" } else { "" }; + Ok(vec![ + DownloadInfo { + tool: "ffmpeg", + available: ff.is_some(), + version: Some(m.ffmpeg.version.clone()), + size: ff.map(|a| a.iter().map(|x| x.size).sum()), + license: m.ffmpeg.license.clone(), + license_url: m.ffmpeg.license_url.clone(), + source: m.ffmpeg.sources.get(os).cloned().unwrap_or_default(), + installed: data_dir.join("tools").join("bin").join(format!("ffmpeg{exe}")).is_file(), + folder: "tools/bin".into(), + }, + DownloadInfo { + tool: "chrome", + available: m.chrome.platforms.contains_key(t), + version: None, + size: None, + license: m.chrome.license.clone(), + license_url: m.chrome.license_url.clone(), + source: "https://googlechromelabs.github.io/chrome-for-testing/".into(), + installed: data_dir.join("tools").join("chrome").join(crate::policy::chrome_executable()).is_file(), + folder: "tools/chrome".into(), + }, + ]) +} + +/// Progress of a download job, sent to the window as `tool-download`. +#[derive(Debug, Clone, Serialize)] +pub struct Event { + pub job: String, + pub tool: String, + /// resolve, download, verify, unpack, install, done, error, cancelled + pub stage: &'static str, + pub received: u64, + pub total: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub message: Option, +} + +/// Running download jobs and their cancel flags. +#[derive(Default)] +pub struct Jobs { + flags: Mutex>>, +} + +impl Jobs { + pub fn start(&self, job: &str) -> Result, String> { + let mut m = self.flags.lock().map_err(|_| "jobs poisoned")?; + if m.contains_key(job) { + return Err("this download is already running".into()); + } + let f = Arc::new(AtomicBool::new(false)); + m.insert(job.to_string(), f.clone()); + Ok(f) + } + pub fn finish(&self, job: &str) { + if let Ok(mut m) = self.flags.lock() { + m.remove(job); + } + } + pub fn cancel(&self, job: &str) -> bool { + self.flags.lock().ok().and_then(|m| m.get(job).map(|f| f.store(true, Ordering::SeqCst))).is_some() + } +} + +/// The Chrome for Testing download of a platform from the endpoint JSON: (version, url). +pub fn chrome_download(endpoint_json: &serde_json::Value, channel: &str, platform: &str) -> Result<(String, Url), String> { + let ch = &endpoint_json["channels"][channel]; + let version = ch["version"].as_str().ok_or("Chrome for Testing: no version in the endpoint answer")?.to_string(); + let url = ch["downloads"]["chrome"] + .as_array() + .and_then(|a| a.iter().find(|d| d["platform"].as_str() == Some(platform))) + .and_then(|d| d["url"].as_str()) + .ok_or_else(|| format!("Chrome for Testing: no download for {platform}"))?; + let u = Url::parse(url).map_err(|e| e.to_string())?; + if !download_allowed(&u) { + return Err(format!("Chrome for Testing: unexpected download host {}", u.host_str().unwrap_or("?"))); + } + Ok((version, u)) +} + +fn client() -> Result { + let _ = rustls::crypto::ring::default_provider().install_default(); + reqwest::Client::builder() + .user_agent(concat!("storebound-desktop/", env!("CARGO_PKG_VERSION"))) + .redirect(reqwest::redirect::Policy::custom(|a| { + if a.previous().len() > 8 { + a.error("too many redirects") + } else if download_allowed(a.url()) { + a.follow() + } else { + a.error("redirect to a host outside the download list") + } + })) + .connect_timeout(std::time::Duration::from_secs(20)) + .build() + .map_err(|e| e.to_string()) +} + +/// Streams `url` into `dest`, returning its SHA-256 (hex). `progress(received_in_this_file)` after each chunk. +async fn fetch(c: &reqwest::Client, url: &Url, dest: &Path, cancel: &AtomicBool, mut progress: impl FnMut(u64)) -> Result { + if !download_allowed(url) { + return Err(format!("not an allowed download host: {}", url.host_str().unwrap_or("?"))); + } + let mut resp = c.get(url.clone()).send().await.map_err(|e| format!("download: {e}"))?; + if !resp.status().is_success() { + return Err(format!("download: {} answered {}", url.host_str().unwrap_or("?"), resp.status())); + } + let mut f = fs::File::create(dest).map_err(|e| e.to_string())?; + let mut h = Sha256::new(); + let mut got: u64 = 0; + while let Some(chunk) = resp.chunk().await.map_err(|e| format!("download: {e}"))? { + if cancel.load(Ordering::SeqCst) { + return Err("cancelled".into()); + } + h.update(&chunk); + f.write_all(&chunk).map_err(|e| e.to_string())?; + got += chunk.len() as u64; + progress(got); + } + f.flush().map_err(|e| e.to_string())?; + Ok(hex(&h.finalize())) +} + +/// Unpacks an archive into `dir`. macOS: ditto (keeps the app bundles' symlinks and signatures intact); tar.xz: the +/// system tar; other zips: the zip crate, which refuses entries outside `dir`. +pub fn unpack(archive: &Path, kind: &str, dir: &Path) -> Result<(), String> { + fs::create_dir_all(dir).map_err(|e| e.to_string())?; + let run = |cmd: &mut std::process::Command| -> Result<(), String> { + let out = cmd.output().map_err(|e| e.to_string())?; + if out.status.success() { + Ok(()) + } else { + Err(String::from_utf8_lossy(&out.stderr).trim().chars().take(300).collect()) + } + }; + match kind { + "tar.xz" => run(std::process::Command::new("tar").arg("-xJf").arg(archive).arg("-C").arg(dir)), + "zip" if cfg!(target_os = "macos") => run(std::process::Command::new("/usr/bin/ditto").arg("-x").arg("-k").arg(archive).arg(dir)), + "zip" => { + let f = fs::File::open(archive).map_err(|e| e.to_string())?; + let mut z = zip::ZipArchive::new(f).map_err(|e| e.to_string())?; + z.extract(dir).map_err(|e| e.to_string()) + } + k => Err(format!("unknown archive kind {k}")), + } +} + +/// `rel` inside `base`, refusing absolute paths and `..`. +pub fn inside(base: &Path, rel: &str) -> Result { + let p = Path::new(rel); + if p.is_absolute() || p.components().any(|c| !matches!(c, std::path::Component::Normal(_))) { + return Err(format!("unsafe path in the manifest: {rel}")); + } + Ok(base.join(p)) +} + +fn first_line(exe: &Path, arg: &str) -> Option { + let out = std::process::Command::new(exe).arg(arg).output().ok()?; + let text = if out.stdout.is_empty() { out.stderr } else { out.stdout }; + String::from_utf8_lossy(&text).lines().next().map(|s| s.trim().to_string()) +} + +#[cfg(unix)] +fn make_executable(p: &Path) -> Result<(), String> { + use std::os::unix::fs::PermissionsExt; + fs::set_permissions(p, fs::Permissions::from_mode(0o755)).map_err(|e| e.to_string()) +} +#[cfg(not(unix))] +fn make_executable(_: &Path) -> Result<(), String> { + Ok(()) +} + +/// Google's code signature on the unpacked Chrome for Testing. +fn verify_chrome(root: &Path, exe: &Path, c: &Chrome) -> Result<(), String> { + if cfg!(target_os = "macos") { + let app = root.join("Google Chrome for Testing.app"); + let v = std::process::Command::new("/usr/bin/codesign").args(["--verify", "--deep", "--strict"]).arg(&app).output().map_err(|e| e.to_string())?; + if !v.status.success() { + return Err(format!("code signature invalid: {}", String::from_utf8_lossy(&v.stderr).trim())); + } + let d = std::process::Command::new("/usr/bin/codesign").args(["-dv", "--verbose=2"]).arg(&app).output().map_err(|e| e.to_string())?; + let text = String::from_utf8_lossy(&d.stderr); + if !text.lines().any(|l| l.trim() == format!("TeamIdentifier={}", c.mac_team_id)) { + return Err("not signed by Google (team identifier differs)".into()); + } + Ok(()) + } else if cfg!(windows) { + let script = format!( + "$s = Get-AuthenticodeSignature -LiteralPath '{}'; if ($s.Status -eq 'Valid' -and $s.SignerCertificate.Subject -like '*O={}*') {{ exit 0 }} else {{ exit 1 }}", + exe.display().to_string().replace('\'', "''"), + c.windows_signer + ); + let st = std::process::Command::new("powershell").args(["-NoProfile", "-NonInteractive", "-Command", &script]).status().map_err(|e| e.to_string())?; + if st.success() { + Ok(()) + } else { + Err(format!("Authenticode signature by {} not found", c.windows_signer)) + } + } else { + match first_line(exe, "--version") { + Some(l) if l.starts_with("Google Chrome for Testing") => Ok(()), + other => Err(format!("unexpected version banner: {}", other.unwrap_or_default())), + } + } +} + +/// Installs `tool` ("ffmpeg" or "chrome") into `/tools`. `emit` gets every progress event. +pub async fn install(data_dir: &Path, tool: &str, job: &str, cancel: Arc, emit: impl Fn(Event) + Send + Sync) -> Result { + let m = manifest()?; + let tools = data_dir.join("tools"); + let staging = tools.join(format!(".staging-{job}")); + let _ = fs::remove_dir_all(&staging); + fs::create_dir_all(&staging).map_err(|e| e.to_string())?; + let ev = |stage: &'static str, received: u64, total: Option, message: Option| emit(Event { job: job.into(), tool: tool.into(), stage, received, total, message }); + let res = async { + let c = client()?; + match tool { + "ffmpeg" => { + let archives = m.ffmpeg.targets.get(target_triple()).ok_or("no ffmpeg build for this platform in the manifest")?; + let total: u64 = archives.iter().map(|a| a.size).sum(); + let mut done: u64 = 0; + let bin = staging.join("bin"); + fs::create_dir_all(&bin).map_err(|e| e.to_string())?; + for (i, a) in archives.iter().enumerate() { + let url = Url::parse(&a.url).map_err(|e| e.to_string())?; + let file = staging.join(format!("a{i}.{}", a.kind)); + let sum = fetch(&c, &url, &file, &cancel, |n| ev("download", done + n, Some(total), None)).await?; + ev("verify", done + a.size, Some(total), None); + if !sum.eq_ignore_ascii_case(&a.sha256) { + let _ = fs::remove_file(&file); + return Err(format!("checksum mismatch for {} (deleted)", url.path())); + } + done += a.size; + ev("unpack", done, Some(total), None); + let out = staging.join(format!("u{i}")); + unpack(&file, &a.kind, &out)?; + for f in &a.files { + let src = inside(&out, &f.from)?; + let dst = inside(&bin, &f.to)?; + if !src.is_file() { + return Err(format!("{} missing in the archive", f.from)); + } + fs::rename(&src, &dst).or_else(|_| fs::copy(&src, &dst).map(|_| ())).map_err(|e| e.to_string())?; + make_executable(&dst)?; + } + let _ = fs::remove_file(&file); + } + let exe = if cfg!(windows) { "ffmpeg.exe" } else { "ffmpeg" }; + let banner = first_line(&bin.join(exe), "-version").unwrap_or_default(); + if !banner.starts_with("ffmpeg version") { + return Err(format!("the downloaded ffmpeg does not run: {banner}")); + } + ev("install", total, Some(total), None); + let dest = tools.join("bin"); + fs::create_dir_all(&dest).map_err(|e| e.to_string())?; + for e in fs::read_dir(&bin).map_err(|e| e.to_string())? { + let e = e.map_err(|e| e.to_string())?; + let target = dest.join(e.file_name()); + let _ = fs::remove_file(&target); + fs::rename(e.path(), &target).map_err(|e| e.to_string())?; + } + Ok(banner) + } + "chrome" => { + let platform = m.chrome.platforms.get(target_triple()).ok_or("Chrome for Testing has no build for this platform")?; + ev("resolve", 0, None, None); + let endpoint = Url::parse(&m.chrome.endpoint).map_err(|e| e.to_string())?; + if !download_allowed(&endpoint) { + return Err("endpoint host not allowed".into()); + } + let json: serde_json::Value = c.get(endpoint).send().await.map_err(|e| e.to_string())?.json().await.map_err(|e| e.to_string())?; + let (version, url) = chrome_download(&json, &m.chrome.channel, platform)?; + let head = c.head(url.clone()).send().await.ok(); + let total = head.and_then(|h| h.content_length()); + let file = staging.join("chrome.zip"); + fetch(&c, &url, &file, &cancel, |n| ev("download", n, total, Some(version.clone()))).await?; + ev("unpack", total.unwrap_or(0), total, None); + let out = staging.join("u"); + unpack(&file, "zip", &out)?; + let _ = fs::remove_file(&file); + let root = out.join(format!("chrome-{platform}")); + let exe = root.join(crate::policy::chrome_executable()); + if !exe.is_file() { + return Err("Chrome for Testing archive has an unexpected layout".into()); + } + ev("verify", total.unwrap_or(0), total, None); + verify_chrome(&root, &exe, &m.chrome)?; + ev("install", total.unwrap_or(0), total, None); + let dest = tools.join("chrome"); + let _ = fs::remove_dir_all(&dest); + fs::rename(&root, &dest).map_err(|e| e.to_string())?; + Ok(format!("Chrome for Testing {version}")) + } + other => Err(format!("unknown tool {other}")), + } + } + .await; + let _ = fs::remove_dir_all(&staging); + match &res { + Ok(v) => ev("done", 0, None, Some(v.clone())), + Err(e) if e == "cancelled" => ev("cancelled", 0, None, None), + Err(e) => ev("error", 0, None, Some(e.clone())), + } + res +} + +/// Removes an app-managed tool. +pub fn uninstall(data_dir: &Path, tool: &str) -> Result<(), String> { + let tools = data_dir.join("tools"); + match tool { + "ffmpeg" => { + for n in ["ffmpeg", "ffprobe", "ffmpeg.exe", "ffprobe.exe"] { + let _ = fs::remove_file(tools.join("bin").join(n)); + } + Ok(()) + } + "chrome" => { + let p = tools.join("chrome"); + if p.exists() { + fs::remove_dir_all(p).map_err(|e| e.to_string())?; + } + Ok(()) + } + other => Err(format!("unknown tool {other}")), + } +} diff --git a/apps/desktop/src-tauri/src/lib.rs b/apps/desktop/src-tauri/src/lib.rs index 1700aa5..23d79ce 100644 --- a/apps/desktop/src-tauri/src/lib.rs +++ b/apps/desktop/src-tauri/src/lib.rs @@ -1,8 +1,10 @@ //! storebound desktop: a Tauri shell around the storebound CLI, which runs as a sidecar (`storebound serve --stdio`). //! The GUI never talks to a store itself; every store call is a storebound command. pub mod accounts; +pub mod cache; pub mod commands; pub mod confirm; +pub mod downloads; pub mod policy; pub mod sidecar; pub mod store; @@ -91,6 +93,32 @@ fn handle_deep_link(app: &AppHandle, urls: &[url::Url]) { } } +/// Debug builds only: STOREBOUND_DEV_PROJECTS (folders separated by the OS path separator) seeds the app folder list +/// once, for screenshots and manual checks with the demo apps. Release builds ignore it. +fn dev_seed(data_dir: &std::path::Path) { + if !cfg!(debug_assertions) { + return; + } + let Some(v) = std::env::var_os("STOREBOUND_DEV_PROJECTS") else { return }; + let mut st = store::load(data_dir); + let mut changed = false; + for dir in std::env::split_paths(&v) { + let dir = std::fs::canonicalize(&dir).unwrap_or(dir); + let path = dir.to_string_lossy().to_string(); + if !commands::has_config(&dir) || st.projects.iter().any(|p| p.path == path) { + continue; + } + let name = dir.file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_default(); + if let Ok(id) = store::new_id() { + st.projects.push(store::Project { id, path, name, play_account: None, apple_account: None, added: store::now_iso() }); + changed = true; + } + } + if changed { + let _ = store::save(data_dir, &st); + } +} + pub fn run() { linux_webkit_env(); let app = tauri::Builder::default() @@ -106,11 +134,15 @@ pub fn run() { .plugin(tauri_plugin_process::init()) .setup(|app| { let data_dir = app.path().app_data_dir()?; + dev_seed(&data_dir); + let cache = cache::Cache::open(&data_dir).or_else(|_| cache::Cache::in_memory()).map_err(std::io::Error::other)?; app.manage(commands::AppState { sidecar: sidecar::Sidecar::default(), saved: Mutex::new(store::load(&data_dir)), data_dir, secrets: Box::new(accounts::Keychain), + cache, + downloads: downloads::Jobs::default(), }); #[cfg(target_os = "macos")] app.set_menu(menu(app.handle())?)?; @@ -144,9 +176,13 @@ pub fn run() { commands::app_info, commands::sidecar_status, commands::sidecar_call, + commands::cache_get, + commands::cache_clear, + commands::job_cancel, commands::write_with_confirm, commands::projects_list, commands::project_add, + commands::project_init, commands::project_remove, commands::project_set_accounts, commands::accounts_list, @@ -155,6 +191,9 @@ pub fn run() { commands::account_remove, commands::portfolio_status, commands::open_external, + commands::tools_info, + commands::tools_install, + commands::tools_uninstall, ]) .build(tauri::generate_context!()) .expect("storebound desktop failed to start"); diff --git a/apps/desktop/src-tauri/src/sidecar.rs b/apps/desktop/src-tauri/src/sidecar.rs index 98e574c..ae3c93f 100644 --- a/apps/desktop/src-tauri/src/sidecar.rs +++ b/apps/desktop/src-tauri/src/sidecar.rs @@ -2,6 +2,9 @@ //! plugin to the webview), with the fixed arguments in policy::SIDECAR_ARGS. One process per app run, started on //! first use and again after it stops. Its first request is `session.hello` with a fresh 32-byte secret, so only this //! process can mint the confirm tokens the sidecar accepts for real writes. +//! +//! Jobs: the webview may tag a call with a short job id. Progress notifications of that request then carry the job id +//! (so the right toast or screen shows them), and `cancel_job` sends `$/cancelRequest` for every request of the job. use std::collections::HashMap; use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; use std::sync::{Arc, Mutex}; @@ -54,11 +57,19 @@ pub struct Descriptor { } type Pending = Arc>>>>; +/// Request id -> the webview's job id. +type Jobs = Arc>>; + +/// Job ids come from the webview and are echoed back in events: short and plain only. +pub fn valid_job(job: &str) -> bool { + !job.is_empty() && job.len() <= 64 && job.bytes().all(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b':' | b'.')) +} pub struct Running { child: Mutex>, secret: [u8; 32], pending: Pending, + jobs: Jobs, next: AtomicU64, alive: Arc, pub version: String, @@ -95,9 +106,36 @@ impl Running { } pub async fn call(&self, method: &str, params: Value) -> Result { + self.call_job(method, params, None).await + } + + /// A call whose progress events carry `job`, and which `cancel_job` can stop. + pub async fn call_job(&self, method: &str, params: Value, job: Option<&str>) -> Result { let id = self.next.fetch_add(1, Ordering::SeqCst); - let rx = self.send(id, method, ¶ms)?; - rx.await.map_err(|_| Fail::host("the storebound sidecar stopped"))? + if let Some(j) = job { + if let Ok(mut m) = self.jobs.lock() { + m.insert(id, j.to_string()); + } + } + let res = match self.send(id, method, ¶ms) { + Ok(rx) => rx.await.map_err(|_| Fail::host("the storebound sidecar stopped")).and_then(|r| r), + Err(e) => Err(e), + }; + if job.is_some() { + if let Ok(mut m) = self.jobs.lock() { + m.remove(&id); + } + } + res + } + + /// Cancels every running request of a job (`$/cancelRequest`; they answer -32800). Returns how many. + pub fn cancel_job(&self, job: &str) -> usize { + let ids: Vec = self.jobs.lock().map(|m| m.iter().filter(|(_, j)| j.as_str() == job).map(|(id, _)| *id).collect()).unwrap_or_default(); + for id in &ids { + self.notify("$/cancelRequest", json!({ "id": id })); + } + ids.len() } pub fn notify(&self, method: &str, params: Value) { @@ -134,8 +172,10 @@ pub struct Sidecar { /// Progress of a running request, forwarded to the main window as `sidecar-progress`. #[derive(Clone, Serialize)] -struct Progress { - params: Value, +pub struct Progress { + pub params: Value, + #[serde(skip_serializing_if = "Option::is_none")] + pub job: Option, } impl Sidecar { @@ -152,6 +192,12 @@ impl Sidecar { Ok(r) } + /// The running sidecar, if there is one (never starts it). + pub async fn current(&self) -> Option> { + self.current.lock().await.as_ref().filter(|r| r.alive()).cloned() + } + + /// Stops the sidecar; the next call starts a fresh one (after a tool install, so it sees the new tool paths). pub async fn shutdown(&self) { if let Some(r) = self.current.lock().await.take() { r.kill(); @@ -196,9 +242,10 @@ async fn start(app: &AppHandle) -> Result { .set_process_group(true); let (mut rx, child) = cmd.spawn().map_err(|e| Fail::host(format!("sidecar did not start: {e}")))?; let pending: Pending = Arc::new(Mutex::new(HashMap::new())); + let jobs: Jobs = Arc::new(Mutex::new(HashMap::new())); let alive = Arc::new(AtomicBool::new(true)); - let (p, a, handle) = (pending.clone(), alive.clone(), app.clone()); + let (p, a, handle, jb) = (pending.clone(), alive.clone(), app.clone(), jobs.clone()); tauri::async_runtime::spawn(async move { let mut out: Vec = Vec::new(); let mut err: Vec = Vec::new(); @@ -208,7 +255,7 @@ async fn start(app: &AppHandle) -> Result { out.extend_from_slice(&bytes); while let Some(nl) = out.iter().position(|b| *b == b'\n') { let line: Vec = out.drain(..=nl).collect(); - route(&handle, &p, &line); + route(&handle, &p, &jb, &line); } } CommandEvent::Stderr(bytes) => { @@ -237,6 +284,7 @@ async fn start(app: &AppHandle) -> Result { child: Mutex::new(Some(child)), secret, pending, + jobs, next: AtomicU64::new(1), alive, version: String::new(), @@ -274,7 +322,7 @@ async fn start(app: &AppHandle) -> Result { Ok(running) } -fn route(app: &AppHandle, pending: &Pending, line: &[u8]) { +fn route(app: &AppHandle, pending: &Pending, jobs: &Jobs, line: &[u8]) { let Ok(msg) = serde_json::from_slice::(line) else { return }; if let Some(id) = msg.get("id").and_then(Value::as_u64) { let tx = pending.lock().ok().and_then(|mut m| m.remove(&id)); @@ -290,6 +338,7 @@ fn route(app: &AppHandle, pending: &Pending, line: &[u8]) { let _ = tx.send(res); } } else if msg.get("method").and_then(Value::as_str) == Some("progress") { - let _ = app.emit_to(policy::MAIN, "sidecar-progress", Progress { params: msg["params"].clone() }); + let job = msg["params"]["requestId"].as_u64().and_then(|id| jobs.lock().ok().and_then(|m| m.get(&id).cloned())); + let _ = app.emit_to(policy::MAIN, "sidecar-progress", Progress { params: msg["params"].clone(), job }); } } diff --git a/apps/desktop/src-tauri/tests/d2.rs b/apps/desktop/src-tauri/tests/d2.rs new file mode 100644 index 0000000..c5b886f --- /dev/null +++ b/apps/desktop/src-tauri/tests/d2.rs @@ -0,0 +1,137 @@ +//! Desktop D2: offline cache, tool download manifest and rules, job ids. +use std::io::Write; + +use serde_json::json; +use storebound_desktop::cache::{key, Cache}; +use storebound_desktop::downloads::{self, chrome_download, download_allowed, inside, manifest, unpack}; +use storebound_desktop::sidecar::valid_job; +use url::Url; + +#[test] +fn cache_keeps_the_last_read_per_project_method_and_args() { + let c = Cache::in_memory().unwrap(); + assert_eq!(c.get("p1", "health", &json!({})).unwrap(), None); + c.put("p1", "health", &json!({}), &json!({ "ok": true, "n": 1 }), "2026-10-10T10:00:00Z").unwrap(); + c.put("p1", "health", &json!({}), &json!({ "ok": true, "n": 2 }), "2026-10-10T11:00:00Z").unwrap(); + c.put("p1", "reviews.list", &json!({ "days": 14, "store": "play" }), &json!([1]), "2026-10-10T09:00:00Z").unwrap(); + c.put("p2", "health", &json!({}), &json!({ "n": 9 }), "2026-10-10T08:00:00Z").unwrap(); + let e = c.get("p1", "health", &json!({})).unwrap().unwrap(); + assert_eq!((e.value["n"].clone(), e.at.as_str()), (json!(2), "2026-10-10T11:00:00Z")); + // argument order does not matter (canonical JSON), values do + assert!(c.get("p1", "reviews.list", &json!({ "store": "play", "days": 14 })).unwrap().is_some()); + assert!(c.get("p1", "reviews.list", &json!({ "store": "apple", "days": 14 })).unwrap().is_none()); + assert_eq!(key("a", "m", &json!({ "b": 1, "a": 2 })), key("a", "m", &json!({ "a": 2, "b": 1 }))); + assert_eq!(c.stamps("p1").unwrap().len(), 2); + c.forget_project("p1").unwrap(); + assert!(c.get("p1", "health", &json!({})).unwrap().is_none()); + assert!(c.get("p2", "health", &json!({})).unwrap().is_some()); + c.clear().unwrap(); + assert!(c.get("p2", "health", &json!({})).unwrap().is_none()); +} + +#[test] +fn cache_survives_a_reopen() { + let dir = tempfile::tempdir().unwrap(); + Cache::open(dir.path()).unwrap().put("p", "m", &json!({}), &json!({ "x": 1 }), "t").unwrap(); + assert_eq!(Cache::open(dir.path()).unwrap().get("p", "m", &json!({})).unwrap().unwrap().value, json!({ "x": 1 })); +} + +#[test] +fn manifest_pins_every_desktop_target_with_hashes() { + let m = manifest().unwrap(); + for t in ["aarch64-apple-darwin", "x86_64-apple-darwin", "x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu", "x86_64-pc-windows-msvc"] { + let list = m.ffmpeg.targets.get(t).unwrap_or_else(|| panic!("no ffmpeg for {t}")); + let mut outs = vec![]; + for a in list { + let u = Url::parse(&a.url).unwrap(); + assert!(download_allowed(&u), "{}", a.url); + assert!(!a.url.contains("/latest/"), "pinned URLs only: {}", a.url); + assert_eq!(a.sha256.len(), 64); + assert!(a.sha256.chars().all(|c| c.is_ascii_hexdigit())); + assert!(a.size > 1_000_000); + assert!(a.kind == "zip" || a.kind == "tar.xz"); + for f in &a.files { + inside(std::path::Path::new("/x"), &f.from).unwrap(); + outs.push(f.to.clone()); + } + } + outs.sort(); + let exe = if t.contains("windows") { ".exe" } else { "" }; + assert_eq!(outs, vec![format!("ffmpeg{exe}"), format!("ffprobe{exe}")], "{t}"); + } + assert!(download_allowed(&Url::parse(&m.chrome.endpoint).unwrap())); + assert_eq!(m.chrome.mac_team_id, "EQHXZ8M8AV"); + assert!(m.ffmpeg.license.contains("GPL")); +} + +#[test] +fn downloads_only_over_https_from_the_listed_hosts() { + for ok in ["https://ffmpeg.martin-riedl.de/download/x.zip", "https://github.com/BtbN/FFmpeg-Builds/releases/download/a/b.zip", "https://storage.googleapis.com/chrome-for-testing-public/1/mac-arm64/chrome-mac-arm64.zip"] { + assert!(download_allowed(&Url::parse(ok).unwrap()), "{ok}"); + } + for bad in ["http://ffmpeg.martin-riedl.de/x.zip", "https://evil.test/x.zip", "https://github.com.evil.test/x", "https://storage.googleapis.com:8443/x", "file:///tmp/x.zip"] { + assert!(!download_allowed(&Url::parse(bad).unwrap()), "{bad}"); + } + assert!(!download_allowed(&Url::parse(&format!("https://{}@github.com/x", "u")).unwrap())); +} + +#[test] +fn chrome_for_testing_endpoint_answer() { + let j = json!({ "channels": { "Stable": { "version": "141.0.7390.54", "downloads": { "chrome": [ + { "platform": "linux64", "url": "https://storage.googleapis.com/chrome-for-testing-public/141.0.7390.54/linux64/chrome-linux64.zip" }, + { "platform": "mac-arm64", "url": "https://storage.googleapis.com/chrome-for-testing-public/141.0.7390.54/mac-arm64/chrome-mac-arm64.zip" } + ] } } } }); + let (v, u) = chrome_download(&j, "Stable", "mac-arm64").unwrap(); + assert_eq!(v, "141.0.7390.54"); + assert!(u.as_str().ends_with("chrome-mac-arm64.zip")); + assert!(chrome_download(&j, "Stable", "win64").is_err()); + let evil = json!({ "channels": { "Stable": { "version": "1", "downloads": { "chrome": [{ "platform": "win64", "url": "https://evil.test/chrome.zip" }] } } } }); + assert!(chrome_download(&evil, "Stable", "win64").is_err()); +} + +#[test] +fn archive_paths_stay_inside() { + let base = std::path::Path::new("/data/tools/.staging"); + assert!(inside(base, "ffmpeg-n9/bin/ffmpeg").is_ok()); + for bad in ["../ffmpeg", "/usr/bin/ffmpeg", "a/../../b", "./x"] { + assert!(inside(base, bad).is_err(), "{bad}"); + } +} + +#[test] +fn zip_unpack_refuses_entries_outside_the_folder() { + if cfg!(target_os = "macos") { + return; // macOS unpacks with ditto, which never writes outside the target folder + } + let dir = tempfile::tempdir().unwrap(); + let zip_path = dir.path().join("evil.zip"); + { + let f = std::fs::File::create(&zip_path).unwrap(); + let mut z = zip::ZipWriter::new(f); + z.start_file("../escape.txt", zip::write::SimpleFileOptions::default()).unwrap(); + z.write_all(b"x").unwrap(); + z.finish().unwrap(); + } + let out = dir.path().join("out"); + let _ = unpack(&zip_path, "zip", &out); + assert!(!dir.path().join("escape.txt").exists()); +} + +#[test] +fn download_info_lists_both_tools() { + let dir = tempfile::tempdir().unwrap(); + let info = downloads::info(dir.path()).unwrap(); + assert_eq!(info.iter().map(|i| i.tool).collect::>(), vec!["ffmpeg", "chrome"]); + assert!(info.iter().all(|i| !i.installed)); + assert!(info[0].available && info[0].size.unwrap_or(0) > 0); +} + +#[test] +fn job_ids_are_short_and_plain() { + for ok in ["portfolio-1", "dl:ffmpeg:3", "a.b_c"] { + assert!(valid_job(ok), "{ok}"); + } + for bad in ["", "a b", "x\"y", "