use super::*;
use assay_evidence::store::BundleStore;
use assay_evidence::Bytes;
async fn seeded_store(bundle_id: &str, size: usize) -> ObjectStoreBundleStore {
let store = ObjectStoreBundleStore::memory();
store
.put_bundle(bundle_id, Bytes::from(vec![b'x'; size]))
.await
.expect("seed");
store
}
fn entries(dir: &Path) -> Vec<String> {
let mut names: Vec<String> = std::fs::read_dir(dir)
.expect("read dir")
.map(|e| e.expect("entry").file_name().to_string_lossy().into_owned())
.collect();
names.sort();
names
}
#[tokio::test]
async fn an_oversized_single_pull_writes_no_file() {
let id = "sha256:toobig";
let store = seeded_store(id, 10_000).await;
let out = tempfile::tempdir().expect("tmp");
let code = pull_single(&store, id, out.path(), false, StreamCeiling::new(100))
.await
.expect("a ceiling refusal is an exit code, not an error");
assert_eq!(code, 2, "a refused download exits 2");
assert!(
entries(out.path()).is_empty(),
"a refused download must leave the output directory empty, found {:?}",
entries(out.path())
);
}
#[tokio::test]
async fn an_oversized_run_pull_writes_no_file() {
let id = "sha256:toobig";
let run = "run-42";
let store = seeded_store(id, 10_000).await;
store.link_run_bundle(run, id).await.expect("link");
let out = tempfile::tempdir().expect("tmp");
let out_dir = out.path().to_path_buf();
let code = pull_run(&store, run, &out_dir, false, StreamCeiling::new(100))
.await
.expect("run pull returns a code");
assert_eq!(code, 1, "a run with a refused bundle reports errors");
assert!(
entries(out.path()).is_empty(),
"no bundle in the run may be materialized, found {:?}",
entries(out.path())
);
}
#[tokio::test]
async fn a_bundle_within_the_ceiling_is_written_intact() {
let id = "sha256:fits";
let store = seeded_store(id, 4096).await;
let out = tempfile::tempdir().expect("tmp");
let code = pull_single(
&store,
id,
out.path(),
false,
StreamCeiling::new(100 * 1024),
)
.await
.expect("ordinary download");
assert_eq!(code, 0);
let written = entries(out.path());
assert_eq!(written.len(), 1, "exactly one file, found {written:?}");
let body = std::fs::read(out.path().join(&written[0])).expect("read back");
assert_eq!(body, vec![b'x'; 4096], "bytes must round-trip");
}
#[tokio::test]
async fn the_ceiling_boundary_holds_through_the_cli_path() {
let out = tempfile::tempdir().expect("tmp");
let exact = seeded_store("sha256:exact", 100).await;
assert_eq!(
pull_single(
&exact,
"sha256:exact",
out.path(),
false,
StreamCeiling::new(100)
)
.await
.expect("exact"),
0
);
assert_eq!(entries(out.path()).len(), 1);
let out2 = tempfile::tempdir().expect("tmp");
let over = seeded_store("sha256:over", 101).await;
assert_eq!(
pull_single(
&over,
"sha256:over",
out2.path(),
false,
StreamCeiling::new(100)
)
.await
.expect("over"),
2
);
assert!(entries(out2.path()).is_empty());
}
#[tokio::test]
async fn a_missing_bundle_still_exits_two_and_writes_nothing() {
let store = ObjectStoreBundleStore::memory();
let out = tempfile::tempdir().expect("tmp");
let code = pull_single(
&store,
"sha256:nonexistent",
out.path(),
false,
StreamCeiling::new(100 * 1024),
)
.await
.expect("not found is an exit code");
assert_eq!(code, 2);
assert!(entries(out.path()).is_empty());
}