| 1 | use std::collections::BTreeSet; |
| 2 | use std::fs; |
| 3 | use std::path::{Path, PathBuf}; |
| 4 | |
| 5 | use serde_json::Value; |
| 6 | use sha2::{Digest, Sha256}; |
| 7 | |
| 8 | fn pin_dir() -> PathBuf { |
| 9 | Path::new(env!("CARGO_MANIFEST_DIR")).join("assets/dsh-creator") |
| 10 | } |
| 11 | |
| 12 | fn sha256(bytes: &[u8]) -> String { |
| 13 | Sha256::digest(bytes) |
| 14 | .iter() |
| 15 | .map(|byte| format!("{byte:02x}")) |
| 16 | .collect() |
| 17 | } |
| 18 | |
| 19 | fn walk(dir: &Path, out: &mut Vec<PathBuf>) { |
| 20 | for entry in fs::read_dir(dir).unwrap() { |
| 21 | let path = entry.unwrap().path(); |
| 22 | if path.is_dir() { |
| 23 | walk(&path, out); |
| 24 | } else { |
| 25 | out.push(path); |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | fn manifest() -> Value { |
| 31 | serde_json::from_slice(&fs::read(pin_dir().join("UPSTREAM.json")).unwrap()).unwrap() |
| 32 | } |
| 33 | |
| 34 | #[test] |
| 35 | fn every_pinned_skill_file_equals_its_recorded_hash_and_keeps_lf() { |
| 36 | let manifest = manifest(); |
| 37 | assert_eq!(manifest["version"], "0.2.2-rc.1"); |
| 38 | let files = manifest["files"].as_array().unwrap(); |
| 39 | assert_eq!(files.len(), 33); |
| 40 | for entry in files { |
| 41 | let path = entry["path"].as_str().unwrap(); |
| 42 | assert_eq!(entry["verbatim"], true, "{path}"); |
| 43 | let bytes = fs::read(pin_dir().join(path)).unwrap(); |
| 44 | assert_eq!( |
| 45 | bytes.len() as u64, |
| 46 | entry["bytes"].as_u64().unwrap(), |
| 47 | "{path}" |
| 48 | ); |
| 49 | assert_eq!(sha256(&bytes), entry["sha256"].as_str().unwrap(), "{path}"); |
| 50 | assert!(!bytes.contains(&b'\r'), "{path} must keep LF line endings"); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | #[test] |
| 55 | fn the_pin_directory_holds_exactly_the_listed_files() { |
| 56 | let manifest = manifest(); |
| 57 | let mut listed: BTreeSet<String> = manifest["files"] |
| 58 | .as_array() |
| 59 | .unwrap() |
| 60 | .iter() |
| 61 | .map(|entry| entry["path"].as_str().unwrap().to_string()) |
| 62 | .collect(); |
| 63 | listed.extend(["UPSTREAM.json".to_string(), "LICENSE".to_string()]); |
| 64 | let mut found = Vec::new(); |
| 65 | walk(&pin_dir(), &mut found); |
| 66 | let present: BTreeSet<String> = found |
| 67 | .iter() |
| 68 | .map(|path| { |
| 69 | path.strip_prefix(pin_dir()) |
| 70 | .unwrap() |
| 71 | .components() |
| 72 | .map(|part| part.as_os_str().to_string_lossy().into_owned()) |
| 73 | .collect::<Vec<_>>() |
| 74 | .join("/") |
| 75 | }) |
| 76 | .filter(|path| !path.starts_with("host-notes/")) |
| 77 | .collect(); |
| 78 | assert_eq!(present, listed); |
| 79 | } |
| 80 | |
| 81 | #[test] |
| 82 | fn the_licence_matches_the_recorded_hash() { |
| 83 | let manifest = manifest(); |
| 84 | let license = fs::read(pin_dir().join("LICENSE")).unwrap(); |
| 85 | assert_eq!( |
| 86 | sha256(&license), |
| 87 | manifest["license_sha256"].as_str().unwrap() |
| 88 | ); |
| 89 | assert!(license.starts_with(b"MIT License\n\nCopyright (c) 2026 DeepSeek\n")); |
| 90 | } |
| 91 |