From d73ad7eb1df4463a92f0db464bd63b6f1b89f033 Mon Sep 17 00:00:00 2001 From: poli0981 <127664709+poli0981@users.noreply.github.com> Date: Tue, 4 Aug 2026 15:18:34 +0700 Subject: [PATCH] test: generate the L3 mock payloads from the real wire types MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TESTING ยง2 wants the frontend's IPC mocked "with recorded core payloads โ€” the same JSON as L1 fixtures, guaranteeing UI and core agree on shapes". Hand-writing that JSON defeats the guarantee it is asking for: rename a field in model.rs and the component tests keep passing against a shape the app never sends. So `cargo run -p xtask -- ipc-fixtures` serializes real `Dist`/`OutdatedDist`/`Pin` values into `ui/src/test/fixtures/`, and a test fails when the committed files no longer match โ€” the same mechanism and the same reasoning as `bindings.rs`. Verified by breaking one on purpose: the test names the file and the command that fixes it. It has already earned itself. `PinMode` is externally tagged, so a `Hold` pin is `{"hold":{"version":"1.26.4"}}` while an `Exclude` is the bare string `"exclude"`, and `sizeBytes` is *absent* rather than null on the editable row. Both are things hand-written mock JSON gets wrong, and both are exactly what the ๐Ÿ”’ chip and the em-dash cell have to render. Generated into `ui/` rather than read out of `crates/` behind a Vite alias โ€” same choice as `generated.ts`, and it saves a build-time path that would have to agree with vite.config.ts. A second test guards the fixture *contents*, not just their shape: six rows chosen so that every rule S2's table implements has a subject โ€” an up-to-date row for dimming, a row with no size, both pin modes on screen at once, and two of the four outdated rows pinned so "N pinned excluded" is a real subtraction rather than 0. Editing the scenario without noticing is how a component test quietly stops covering the case its name claims. Co-Authored-By: Claude Opus 5 --- crates/pipdock-core/src/fixtures.rs | 201 +++++++++++++++++++++++++ crates/pipdock-core/src/lib.rs | 1 + ui/src/test/fixtures/pin_list.json | 15 ++ ui/src/test/fixtures/pkg_list.json | 50 ++++++ ui/src/test/fixtures/pkg_outdated.json | 22 +++ xtask/src/main.rs | 38 ++++- 6 files changed, 326 insertions(+), 1 deletion(-) create mode 100644 crates/pipdock-core/src/fixtures.rs create mode 100644 ui/src/test/fixtures/pin_list.json create mode 100644 ui/src/test/fixtures/pkg_list.json create mode 100644 ui/src/test/fixtures/pkg_outdated.json diff --git a/crates/pipdock-core/src/fixtures.rs b/crates/pipdock-core/src/fixtures.rs new file mode 100644 index 0000000..bac7d23 --- /dev/null +++ b/crates/pipdock-core/src/fixtures.rs @@ -0,0 +1,201 @@ +//! The payloads the frontend's L3 tests mock `@/ipc` with. +//! +//! TESTING ยง2 asks for "IPC mocked at the typed wrapper layer with recorded core payloads โ€” the +//! same JSON as L1 fixtures, guaranteeing UI and core agree on shapes". Hand-writing that JSON +//! would defeat the guarantee: rename a field in `model.rs` and the component tests keep passing +//! against a shape the app never sends. So the fixtures are **serialized from the real types**, +//! and a test fails when the committed files no longer match โ€” the same mechanism, and the same +//! reasoning, as [`crate::bindings`]. +//! +//! Generated into `ui/` rather than read out of `crates/` behind a Vite alias, for the same reason +//! `generated.ts` is: one directory the frontend owns, no build-time path that has to agree with +//! `vite.config.ts`. +//! +//! The *shapes* come from the types. The *contents* are a deliberate scenario, modelled on the +//! SP-5 dogfood environment, chosen so every rule S2's table has to implement is exercised by at +//! least one row: +//! +//! | Row | Exercises | +//! |---|---| +//! | `numpy` | outdated **and** held at a version โ€” the `UPDATE` badge and a `Hold` ๐Ÿ”’ together | +//! | `scipy` | outdated and `Exclude`-pinned โ€” excluded from *Select all* | +//! | `pandas` | outdated and unpinned โ€” the one *Select all* may actually take | +//! | `requests` | outdated and unpinned, so "N pinned excluded" is 2 of 4 rather than 1 of 2 | +//! | `certifi` | up to date โ€” the dimming rule | +//! | `editable-lib` | up to date and **no `sizeBytes`** โ€” the em-dash cell | + +use crate::model::{Dist, OutdatedDist, PkgName, Version}; +use crate::pins::{Pin, PinMode}; + +/// Directory the fixtures are written to, relative to the repository root. +pub const OUTPUT_DIR: &str = "ui/src/test/fixtures"; + +/// Build a `Dist` the way the probe would. +fn dist(name: &str, version: &str, requires: &[&str], size: Option) -> Dist { + Dist { + // Every name here is a literal in this file, so a parse failure is a typo in the fixture + // rather than anything a user could cause. + name: PkgName::parse(name).unwrap_or_else(|e| panic!("fixture name {name:?}: {e:?}")), + version: Version(version.to_owned()), + requires_dist: requires.iter().map(|s| (*s).to_owned()).collect(), + requires_python: Some(">=3.10".to_owned()), + size_bytes: size, + } +} + +fn outdated(name: &str, current: &str, latest: &str) -> OutdatedDist { + OutdatedDist { + name: PkgName::parse(name).unwrap_or_else(|e| panic!("fixture name {name:?}: {e:?}")), + current: Version(current.to_owned()), + latest: Version(latest.to_owned()), + } +} + +/// What `pkg_list` returns. +fn pkg_list() -> Vec { + vec![ + dist("certifi", "2024.2.2", &[], Some(163_840)), + // No size: an editable install's RECORD lists its import shim, not its sources, so the + // probe reports nothing rather than a few hundred bytes (see `probe.py`). + dist("editable-lib", "0.3.0", &[], None), + dist("numpy", "1.26.4", &[], Some(62_914_560)), + dist( + "pandas", + "2.1.4", + &["numpy<2,>=1.26.0", "python-dateutil>=2.8.2"], + Some(46_137_344), + ), + dist("requests", "2.28.0", &["urllib3<3,>=1.21.1"], Some(133_120)), + dist( + "scipy", + "1.11.4", + &["numpy<1.28.0,>=1.21.6"], + Some(58_720_256), + ), + ] +} + +/// What `pkg_outdated` returns โ€” a strict subset of the names above, which is the join the +/// Installed table performs. +fn pkg_outdated() -> Vec { + vec![ + outdated("numpy", "1.26.4", "2.5.1"), + outdated("pandas", "2.1.4", "2.3.0"), + outdated("requests", "2.28.0", "2.32.3"), + outdated("scipy", "1.11.4", "1.14.1"), + ] +} + +/// What `pin_list` returns. Two of the four outdated rows are pinned, so *Select all* has a +/// non-trivial number to report and the two `PinMode` variants are both on screen. +fn pin_list() -> Vec { + vec![ + Pin { + pkg: PkgName::parse("numpy").unwrap_or_else(|_| unreachable!()), + mode: PinMode::Hold { + version: Version("1.26.4".to_owned()), + }, + reason: Some("scipy 1.11.4 needs numpy < 1.28".to_owned()), + }, + Pin { + pkg: PkgName::parse("scipy").unwrap_or_else(|_| unreachable!()), + mode: PinMode::Exclude, + reason: None, + }, + ] +} + +/// Every fixture, as `(file name, contents)`. +/// +/// # Errors +/// Propagates serialization failures, which can only mean a type in this module stopped being +/// `Serialize` โ€” a compile-time mistake surfacing at runtime. +pub fn ipc_fixtures() -> serde_json::Result> { + Ok(vec![ + ("pkg_list.json", render(&pkg_list())?), + ("pkg_outdated.json", render(&pkg_outdated())?), + ("pin_list.json", render(&pin_list())?), + ]) +} + +/// Pretty JSON with a trailing newline, so the files read as source and diff cleanly. +fn render(value: &T) -> serde_json::Result { + let mut out = serde_json::to_string_pretty(value)?; + out.push('\n'); + Ok(out) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn the_committed_ipc_fixtures_are_current() { + // The same guarantee as `bindings::the_committed_bindings_are_current`, for the same + // reason: without it the L3 tests drift silently into asserting against a shape the app + // does not send, and stay green while doing it. + let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) + .join("..") + .join(".."); + for (name, expected) in ipc_fixtures().expect("fixtures serialize") { + let path = root.join(OUTPUT_DIR).join(name); + let committed = std::fs::read_to_string(&path).unwrap_or_else(|e| { + panic!( + "{} is missing ({e}); run `cargo run -p xtask -- ipc-fixtures`", + path.display() + ) + }); + assert_eq!( + committed.replace("\r\n", "\n"), + expected, + "{name} is stale โ€” run `cargo run -p xtask -- ipc-fixtures`" + ); + } + } + + #[test] + fn the_scenario_still_exercises_every_rule_the_table_implements() { + // Guards the fixture *contents*, not just their shape. Editing the scenario without + // reading this is how a component test quietly stops covering the case it names. + let (dists, outdated, pins) = (pkg_list(), pkg_outdated(), pin_list()); + + let outdated_names: Vec<&str> = outdated.iter().map(|o| o.name.as_str()).collect(); + let pinned_names: Vec<&str> = pins.iter().map(|p| p.pkg.as_str()).collect(); + + // Every outdated row must have an installed row, or the join drops it. + for name in &outdated_names { + assert!( + dists.iter().any(|d| &d.name.as_str() == name), + "{name} is outdated but not installed โ€” the join would lose it" + ); + } + // Something up to date, so the dimming rule has a subject. + assert!( + dists + .iter() + .any(|d| !outdated_names.contains(&d.name.as_str())), + "no up-to-date row, so nothing exercises dimming" + ); + // Something with no size, so the em-dash cell has a subject. + assert!( + dists.iter().any(|d| d.size_bytes.is_none()), + "no row without a size, so nothing exercises the unknown-size cell" + ); + // Both pin modes on screen, so the ๐Ÿ”’ chip has to tell them apart. + assert!(pins.iter().any(|p| matches!(p.mode, PinMode::Exclude))); + assert!(pins.iter().any(|p| matches!(p.mode, PinMode::Hold { .. }))); + // Pinned *and* outdated, so "N pinned excluded" is a real subtraction rather than 0. + let excluded = outdated_names + .iter() + .filter(|n| pinned_names.contains(n)) + .count(); + assert_eq!( + excluded, 2, + "Select all should exclude exactly 2 of the outdated rows" + ); + assert!( + outdated_names.len() > excluded, + "Select all would select nothing" + ); + } +} diff --git a/crates/pipdock-core/src/lib.rs b/crates/pipdock-core/src/lib.rs index 86d5b70..c8ce861 100644 --- a/crates/pipdock-core/src/lib.rs +++ b/crates/pipdock-core/src/lib.rs @@ -29,6 +29,7 @@ pub mod engine; pub mod envs; pub mod errors; pub mod exec; +pub mod fixtures; pub mod flow; pub mod graph; pub mod health; diff --git a/ui/src/test/fixtures/pin_list.json b/ui/src/test/fixtures/pin_list.json new file mode 100644 index 0000000..396ad1d --- /dev/null +++ b/ui/src/test/fixtures/pin_list.json @@ -0,0 +1,15 @@ +[ + { + "pkg": "numpy", + "mode": { + "hold": { + "version": "1.26.4" + } + }, + "reason": "scipy 1.11.4 needs numpy < 1.28" + }, + { + "pkg": "scipy", + "mode": "exclude" + } +] diff --git a/ui/src/test/fixtures/pkg_list.json b/ui/src/test/fixtures/pkg_list.json new file mode 100644 index 0000000..5f8caff --- /dev/null +++ b/ui/src/test/fixtures/pkg_list.json @@ -0,0 +1,50 @@ +[ + { + "name": "certifi", + "version": "2024.2.2", + "requiresDist": [], + "requiresPython": ">=3.10", + "sizeBytes": 163840 + }, + { + "name": "editable-lib", + "version": "0.3.0", + "requiresDist": [], + "requiresPython": ">=3.10" + }, + { + "name": "numpy", + "version": "1.26.4", + "requiresDist": [], + "requiresPython": ">=3.10", + "sizeBytes": 62914560 + }, + { + "name": "pandas", + "version": "2.1.4", + "requiresDist": [ + "numpy<2,>=1.26.0", + "python-dateutil>=2.8.2" + ], + "requiresPython": ">=3.10", + "sizeBytes": 46137344 + }, + { + "name": "requests", + "version": "2.28.0", + "requiresDist": [ + "urllib3<3,>=1.21.1" + ], + "requiresPython": ">=3.10", + "sizeBytes": 133120 + }, + { + "name": "scipy", + "version": "1.11.4", + "requiresDist": [ + "numpy<1.28.0,>=1.21.6" + ], + "requiresPython": ">=3.10", + "sizeBytes": 58720256 + } +] diff --git a/ui/src/test/fixtures/pkg_outdated.json b/ui/src/test/fixtures/pkg_outdated.json new file mode 100644 index 0000000..beb0784 --- /dev/null +++ b/ui/src/test/fixtures/pkg_outdated.json @@ -0,0 +1,22 @@ +[ + { + "name": "numpy", + "current": "1.26.4", + "latest": "2.5.1" + }, + { + "name": "pandas", + "current": "2.1.4", + "latest": "2.3.0" + }, + { + "name": "requests", + "current": "2.28.0", + "latest": "2.32.3" + }, + { + "name": "scipy", + "current": "1.11.4", + "latest": "1.14.1" + } +] diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 46675d8..55c0890 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -28,16 +28,52 @@ fn main() -> ExitCode { ExitCode::FAILURE } }, + Some("ipc-fixtures") => match ipc_fixtures() { + Ok(written) => { + for (name, changed) in written { + println!( + "{} {}/{name}", + if changed { "wrote" } else { "unchanged" }, + pipdock_core::fixtures::OUTPUT_DIR + ); + } + ExitCode::SUCCESS + } + Err(e) => { + eprintln!("error: {e}"); + ExitCode::FAILURE + } + }, other => { if let Some(name) = other { eprintln!("error: unknown task {name:?}"); } - eprintln!("usage: cargo run -p xtask -- bindings"); + eprintln!("usage: cargo run -p xtask -- "); ExitCode::FAILURE } } } +/// Regenerate the L3 mock payloads. Returns which files changed. +fn ipc_fixtures() -> Result, Box> { + let dir = repo_root().join(pipdock_core::fixtures::OUTPUT_DIR); + std::fs::create_dir_all(&dir)?; + + let mut out = Vec::new(); + for (name, generated) in pipdock_core::fixtures::ipc_fixtures()? { + let path = dir.join(name); + // Normalised, so a CRLF checkout does not look like a change on every run. + let current = std::fs::read_to_string(&path).unwrap_or_default(); + if current.replace("\r\n", "\n") == generated { + out.push((name, false)); + continue; + } + std::fs::write(&path, generated)?; + out.push((name, true)); + } + Ok(out) +} + /// Regenerate the TypeScript bindings. Returns whether the file changed. fn bindings() -> Result> { let path = repo_root().join(pipdock_core::bindings::OUTPUT_PATH);