use std::path::PathBuf;
use horon::{Horon, HoronConfig};
use tempfile::NamedTempFile;
fn temp_path() -> PathBuf {
NamedTempFile::new().unwrap().into_temp_path().to_path_buf()
}
fn build_scripted_file() -> Vec<u8> {
let path = temp_path();
let sem_dims: u8 = 20;
let gf = Horon::open_with_config(&path, HoronConfig {
dimension: 4,
semantic_dims: sem_dims,
compression: false,
auto_compact_threshold: 0,
..Default::default()
}).unwrap();
for i in 0..30 {
let key = format!("/det/branch_{}/leaf_{:02}", i % 3, i);
gf.put(&key, format!("payload-{:04}", i * 7).as_bytes()).unwrap();
gf.set_meta(&key, "tier", &format!("{}", i % 5)).unwrap();
gf.set_meta(&key, "alpha", "constant").unwrap();
let mut coords = vec![0u8; sem_dims as usize * 16];
for d in 0..4 {
let off = (16 + d) * 16;
let val = (((i * 31 + d * 7) % 97) as i128) << 60; coords[off..off + 16].copy_from_slice(&val.to_le_bytes());
}
gf.set_semantic(&key, coords).unwrap();
}
for i in (0..30).step_by(5) {
gf.remove(&format!("/det/branch_{}/leaf_{:02}", i % 3, i)).unwrap();
}
gf.compact().unwrap();
for i in 30..40 {
gf.put(&format!("/det/wal_tail_{}", i), format!("tail-{}", i).as_bytes()).unwrap();
}
gf.flush().unwrap();
drop(gf);
std::fs::read(&path).unwrap()
}
fn build_scripted_quantized_file() -> Vec<u8> {
let path = temp_path();
let sem_dims: u8 = 20;
let gf = Horon::open_with_config(&path, HoronConfig {
dimension: 4,
semantic_dims: sem_dims,
compression: false,
auto_compact_threshold: 0,
quantized_semantic: true,
..Default::default()
}).unwrap();
for i in 0..30 {
let key = format!("/det/branch_{}/leaf_{:02}", i % 3, i);
gf.put(&key, format!("payload-{:04}", i * 7).as_bytes()).unwrap();
gf.set_meta(&key, "tier", &format!("{}", i % 5)).unwrap();
gf.set_meta(&key, "alpha", "constant").unwrap();
let mut coords = vec![0u8; sem_dims as usize * 16];
for d in 0..4 {
let off = (16 + d) * 16;
let val = (((i * 31 + d * 7) % 97) as i128) << 57; coords[off..off + 16].copy_from_slice(&val.to_le_bytes());
}
gf.set_semantic(&key, coords).unwrap();
}
for i in (0..30).step_by(5) {
gf.remove(&format!("/det/branch_{}/leaf_{:02}", i % 3, i)).unwrap();
}
gf.compact().unwrap();
for i in 30..40 {
gf.put(&format!("/det/wal_tail_{}", i), format!("tail-{}", i).as_bytes()).unwrap();
}
gf.flush().unwrap();
drop(gf);
std::fs::read(&path).unwrap()
}
#[test]
fn same_script_same_bytes() {
let a = build_scripted_file();
let b = build_scripted_file();
assert_eq!(a.len(), b.len(), "independent builds differ in length");
assert!(a == b, "independent builds of the same script must be byte-identical");
}
#[test]
fn same_script_same_bytes_quantized() {
let a = build_scripted_quantized_file();
let b = build_scripted_quantized_file();
assert_eq!(a.len(), b.len(), "independent quantized builds differ in length");
assert!(a == b, "independent quantized builds must be byte-identical");
}
#[test]
fn golden_file_crc_quantized() {
const GOLDEN_CRC_QUANTIZED: u32 = 0xAA7E_4AD3;
let bytes = build_scripted_quantized_file();
let crc = crc32fast::hash(&bytes);
assert_eq!(
crc, GOLDEN_CRC_QUANTIZED,
"quantized file bytes diverge from the committed golden (len {}, got 0x{:08X}). \
On a new platform this means the determinism claim is BROKEN; after a \
deliberate format change, update GOLDEN_CRC_QUANTIZED in the same commit.",
bytes.len(), crc
);
}
#[test]
fn golden_file_crc() {
const GOLDEN_CRC: u32 = 0x69C5_E988;
let bytes = build_scripted_file();
let crc = crc32fast::hash(&bytes);
assert_eq!(
crc, GOLDEN_CRC,
"file bytes diverge from the committed golden (len {}, got 0x{:08X}). \
On a new platform this means the determinism claim is BROKEN; after a \
deliberate format change, update GOLDEN_CRC in the same commit.",
bytes.len(), crc
);
}