feat(domain): R5.6 prepared, fingerprint convention and registry inert, switch patch
canonical_json (RFC 8785), sheet_fingerprint behind feature fingerprint, TRUSTED_ALIASES (one equine pair), equivalence and check_declared; tests against the RFC vectors, the V8 numbers, an independent Node reference and the archives. No consumer wired: the switch is a rehearsed patch under .claude/tasks/plans.
This commit is contained in:
1 parent
1f8873dbce
commit
3b9a395d43
12 files changed
+1004
-1
No files matched your search
@@ -0,0 +1,40 @@
|
||||
{
|
||||
"frozen_from": "Node v24.21.0",
|
||||
"cases": [
|
||||
{
|
||||
"input": "[1e21, 1e-7, 1E+2, 0.1, -0, 5e-324, 1.7976931348623157e308, 123456789012345678901, 9007199254740993, 100, 1.5e-6, 0.000001, 1e20, 999999999999999999999]",
|
||||
"canonical": "[1e+21,1e-7,100,0.1,0,5e-324,1.7976931348623157e+308,123456789012345680000,9007199254740992,100,0.0000015,0.000001,100000000000000000000,1e+21]",
|
||||
"sha256": "454757d41a64c2a76e8fd9c555ce7f7732241ac8d192a731ccb880d5cdde1f25"
|
||||
},
|
||||
{
|
||||
"input": "{\"\\ud83d\\ude00\": 1, \"\\ufb33\": 2, \"\\u00e9\": 3, \"e\\u0301\": 4, \"Z\": 5, \"a\": 6, \"\": 7, \"aa\": 8, \"\\u0080\": 9}",
|
||||
"canonical": "{\"\":7,\"Z\":5,\"a\":6,\"aa\":8,\"é\":4,\"\":9,\"é\":3,\"😀\":1,\"דּ\":2}",
|
||||
"sha256": "781d348d29b6dc2fcb75ed58f847fbc19ebb6f2dd526e5aa67e130a074a7200a"
|
||||
},
|
||||
{
|
||||
"input": "{\"s\": \"\\u0000\\u0001\\u001f\\b\\f\\n\\r\\t\\\"\\\\/\\u007f\\u2028\\u2029\\u00e9e\\u0301\\ud83d\\ude00\"}",
|
||||
"canonical": "{\"s\":\"\\u0000\\u0001\\u001f\\b\\f\\n\\r\\t\\\"\\\\/
éé😀\"}",
|
||||
"sha256": "ecaac6f9c64eb5f89acb33f7f98b840e3c07b8c91551e184af89aa1da702d0bb"
|
||||
},
|
||||
{
|
||||
"input": "{\"b\": {\"z\": [true, false, null, {\"y\": 1, \"x\": [ ]}], \"a\": {}}, \"a\": [[[]]]}",
|
||||
"canonical": "{\"a\":[[[]]],\"b\":{\"a\":{},\"z\":[true,false,null,{\"x\":[],\"y\":1}]}}",
|
||||
"sha256": "326c7cd13dafbbe0412b029969bf0b2994bdeea90313ca59b2cc4c5015c15447"
|
||||
},
|
||||
{
|
||||
"input": "{\"geometry\": {\"cx\": 743.0451049804688, \"cy\": 469.1115417480469, \"r\": 12}, \"name\": {\"fr\": \"Atlas (C1)\", \"en\": \"Atlas\"}}",
|
||||
"canonical": "{\"geometry\":{\"cx\":743.0451049804688,\"cy\":469.1115417480469,\"r\":12},\"name\":{\"en\":\"Atlas\",\"fr\":\"Atlas (C1)\"}}",
|
||||
"sha256": "0fa3704e87fb9f065b20bdd8d70c66705a20a33366f6ae8546adeb83ac1053f0"
|
||||
},
|
||||
{
|
||||
"input": "{\"k\": 1, \"k\": 2}",
|
||||
"canonical": "{\"k\":2}",
|
||||
"sha256": "1ddca3d1f7a33ce87c75de54e2b6a7ee0e52cf11cb8b8041e102b8477d5b1a23"
|
||||
},
|
||||
{
|
||||
"input": "[-1.0, 2.50, 1e0, -1e-0, 0.1e1, 12345678.9, 4.35, 0.3, 1e-6, 1e-5]",
|
||||
"canonical": "[-1,2.5,1,-1,1,12345678.9,4.35,0.3,0.000001,0.00001]",
|
||||
"sha256": "a2d500ce5093d2214d6e7b78163853211d5c8a43317ca81f0eac4f7aebd650e5"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,256 @@
|
||||
//! R5.6 preparation (owner 2026-10-24 « arbitrage D-R5.6 »): the convention
|
||||
//! `sha256-rfc8785-validated-v1` and the trusted alias registry, PREPARED, NOT ACTIVE. The RFC
|
||||
//! 8785 vectors, the V8 number oracle, the independent Node differential, the draft's frozen
|
||||
//! value, the registry's contract and a census of the historical archives (JSON fixtures and
|
||||
//! every fixture PDF's attachment) under the comparison the five consumers will use.
|
||||
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
|
||||
|
||||
mod common;
|
||||
|
||||
use faunosteo_core::domain::js::parse_json;
|
||||
use faunosteo_core::domain::sheet::fingerprint::{
|
||||
AliasRefusal, Equivalence, FINGERPRINT_CONVENTION, FingerprintError, TRUSTED_ALIASES,
|
||||
canonical_json, check_declared, equivalence, sheet_fingerprint,
|
||||
};
|
||||
use faunosteo_core::pdf::read::probe::{ProbeLimits, read_attached_document};
|
||||
use serde_json::Value;
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
const SHEET: &str = "503dc301-a8b1-50d4-9e44-0919b2fc2916";
|
||||
const ALIAS: &str = "27c7383c445c73dc2001ebf4794a3ea63d416910bbbaa0d7848dd720de7c2660";
|
||||
const CANONICAL: &str = "12d788727c1e532641bd5b3a7ecf39dbdd25e70350f8a76be5ca917b262874e2";
|
||||
|
||||
fn oracle(name: &str) -> Value {
|
||||
serde_json::from_str(&common::repo_file(&format!(
|
||||
"crates/core/tests/oracles/r5/{name}"
|
||||
)))
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn sha256(text: &str) -> String {
|
||||
Sha256::digest(text.as_bytes())
|
||||
.iter()
|
||||
.map(|b| format!("{b:02x}"))
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_rfc_8785_vectors_are_reproduced() {
|
||||
let vectors = oracle("fingerprints.json")["rfc8785_vectors"].clone();
|
||||
for vector in vectors.as_array().unwrap() {
|
||||
let input = parse_json(vector["input"].as_str().unwrap()).unwrap();
|
||||
assert_eq!(
|
||||
canonical_json(&input).unwrap(),
|
||||
vector["canonical"].as_str().unwrap()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn numbers_print_as_v8_on_the_whole_oracle() {
|
||||
let rows = oracle("numbers.json")["rows"].clone();
|
||||
let rows = rows.as_array().unwrap();
|
||||
assert_eq!(rows.len(), 44_322);
|
||||
for row in rows {
|
||||
let bits = u64::from_str_radix(row[0].as_str().unwrap(), 16).unwrap();
|
||||
let value = Value::from(f64::from_bits(bits));
|
||||
assert_eq!(
|
||||
canonical_json(&value).unwrap(),
|
||||
row[1].as_str().unwrap(),
|
||||
"{bits:016x}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_independent_node_reference_agrees_case_by_case() {
|
||||
let frozen = oracle("fingerprint-differential.json");
|
||||
for case in frozen["cases"].as_array().unwrap() {
|
||||
let input = parse_json(case["input"].as_str().unwrap()).unwrap();
|
||||
let canonical = canonical_json(&input).unwrap();
|
||||
assert_eq!(canonical, case["canonical"].as_str().unwrap());
|
||||
assert_eq!(sha256(&canonical), case["sha256"].as_str().unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unicode_is_kept_and_ordered_by_utf16_units() {
|
||||
// U+FB33 sorts after U+1F600 by code point, before it by UTF-16 units (0xFB33 > 0xD83D).
|
||||
let value = parse_json(r#"{"דּ":1,"😀":2,"é":3,"é":4}"#).unwrap();
|
||||
assert_eq!(
|
||||
canonical_json(&value).unwrap(),
|
||||
"{\"e\u{301}\":4,\"\u{e9}\":3,\"\u{1F600}\":2,\"\u{FB33}\":1}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_draft_gives_the_frozen_validated_value() {
|
||||
let text = common::repo_file("supabase/seed/anatomical-sheets/equine/draft/document.json");
|
||||
assert_eq!(sha256(&text), ALIAS);
|
||||
assert_eq!(sheet_fingerprint(&text).unwrap(), CANONICAL);
|
||||
let artifacts = oracle("fingerprints.json")["artifacts"].clone();
|
||||
let draft = artifacts
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.find(|a| a["file"].as_str().unwrap().ends_with("draft/document.json"))
|
||||
.unwrap()
|
||||
.clone();
|
||||
assert_eq!(draft["b_rfc8785_validated"], CANONICAL);
|
||||
assert_eq!(draft["today_file_bytes"], ALIAS);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_invalid_or_malformed_document_has_no_fingerprint() {
|
||||
let text = common::repo_file("supabase/seed/anatomical-sheets/equine/draft/document.json");
|
||||
let mut value: Value = serde_json::from_str(&text).unwrap();
|
||||
let first = value["annotations"][0]["id"].clone();
|
||||
value["annotations"][1]["id"] = first;
|
||||
let duplicated = serde_json::to_string(&value).unwrap();
|
||||
assert!(matches!(sheet_fingerprint(&duplicated), Err(FingerprintError::Invalid(n)) if n > 0));
|
||||
assert!(matches!(
|
||||
sheet_fingerprint("[1]"),
|
||||
Err(FingerprintError::Shape(_))
|
||||
));
|
||||
assert!(matches!(
|
||||
sheet_fingerprint("{"),
|
||||
Err(FingerprintError::Parse(_))
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_registry_holds_one_pair_bound_to_the_equine_sheet() {
|
||||
assert_eq!(TRUSTED_ALIASES.len(), 1);
|
||||
assert_eq!(FINGERPRINT_CONVENTION, "sha256-rfc8785-validated-v1");
|
||||
assert_eq!(
|
||||
equivalence(SHEET, CANONICAL, CANONICAL),
|
||||
Equivalence::Identical
|
||||
);
|
||||
assert_eq!(
|
||||
equivalence(SHEET, CANONICAL, ALIAS),
|
||||
Equivalence::TrustedAlias
|
||||
);
|
||||
assert_eq!(
|
||||
equivalence(SHEET, ALIAS, CANONICAL),
|
||||
Equivalence::TrustedAlias
|
||||
);
|
||||
let other_sheet = "00000000-0000-0000-0000-000000000000";
|
||||
assert_eq!(
|
||||
equivalence(other_sheet, CANONICAL, ALIAS),
|
||||
Equivalence::Different
|
||||
);
|
||||
let other_content = "a".repeat(64);
|
||||
assert_eq!(
|
||||
equivalence(SHEET, &other_content, ALIAS),
|
||||
Equivalence::Different
|
||||
);
|
||||
assert_eq!(
|
||||
equivalence(SHEET, &other_content, CANONICAL),
|
||||
Equivalence::Different
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn declared_metadata_never_creates_an_equivalence() {
|
||||
let aliases = vec![ALIAS.to_owned()];
|
||||
assert_eq!(
|
||||
check_declared(SHEET, CANONICAL, FINGERPRINT_CONVENTION, &aliases),
|
||||
Ok(())
|
||||
);
|
||||
assert_eq!(
|
||||
check_declared(SHEET, CANONICAL, FINGERPRINT_CONVENTION, &[]),
|
||||
Ok(())
|
||||
);
|
||||
let forged = vec!["b".repeat(64)];
|
||||
assert_eq!(
|
||||
check_declared(SHEET, CANONICAL, FINGERPRINT_CONVENTION, &forged),
|
||||
Err(AliasRefusal::Untrusted("b".repeat(64)))
|
||||
);
|
||||
let foreign = "00000000-0000-0000-0000-000000000000";
|
||||
assert!(check_declared(foreign, CANONICAL, FINGERPRINT_CONVENTION, &aliases).is_err());
|
||||
assert!(check_declared(SHEET, &"c".repeat(64), FINGERPRINT_CONVENTION, &aliases).is_err());
|
||||
assert!(
|
||||
check_declared(
|
||||
SHEET,
|
||||
ALIAS,
|
||||
FINGERPRINT_CONVENTION,
|
||||
&[CANONICAL.to_owned()]
|
||||
)
|
||||
.is_err()
|
||||
);
|
||||
assert_eq!(
|
||||
check_declared(SHEET, CANONICAL, "sha256-prettier-file-bytes-v0", &aliases),
|
||||
Err(AliasRefusal::UnknownConvention(
|
||||
"sha256-prettier-file-bytes-v0".into()
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
/// Every `sheet` reference of a report found in `value` (bare report or envelope).
|
||||
fn stamps(value: &Value, found: &mut Vec<(String, String)>) {
|
||||
match value {
|
||||
Value::Object(map) => {
|
||||
if let (Some(id), Some(sha)) = (
|
||||
map.get("sheetId").and_then(Value::as_str),
|
||||
map.get("documentSha256").and_then(Value::as_str),
|
||||
) {
|
||||
found.push((id.to_owned(), sha.to_owned()));
|
||||
}
|
||||
map.values().for_each(|item| stamps(item, found));
|
||||
}
|
||||
Value::Array(items) => items.iter().for_each(|item| stamps(item, found)),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn files(dir: &std::path::Path, out: &mut Vec<std::path::PathBuf>) {
|
||||
for entry in std::fs::read_dir(dir).unwrap() {
|
||||
let path = entry.unwrap().path();
|
||||
if path.is_dir() {
|
||||
files(&path, out);
|
||||
} else {
|
||||
out.push(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn historical_archives_open_as_the_same_document_only_through_the_registered_pair() {
|
||||
let mut paths = Vec::new();
|
||||
files(
|
||||
std::path::Path::new(&common::repo_path("apps/app/tests/fixtures")),
|
||||
&mut paths,
|
||||
);
|
||||
let mut found = Vec::new();
|
||||
let mut pdfs = 0;
|
||||
for path in &paths {
|
||||
let bytes = std::fs::read(path).unwrap();
|
||||
let text = match path.extension().and_then(|e| e.to_str()) {
|
||||
Some("json") => String::from_utf8(bytes).ok(),
|
||||
Some("pdf") => read_attached_document(&bytes, ProbeLimits::UNBOUNDED)
|
||||
.ok()
|
||||
.map(|located| {
|
||||
pdfs += 1;
|
||||
located.text.into_owned()
|
||||
}),
|
||||
_ => None,
|
||||
};
|
||||
if let Some(value) = text.and_then(|t: String| serde_json::from_str::<Value>(&t).ok()) {
|
||||
stamps(&value, &mut found);
|
||||
}
|
||||
}
|
||||
assert!(pdfs > 50, "{pdfs} attachments read");
|
||||
let mut same = 0;
|
||||
for (sheet, sha) in &found {
|
||||
let verdict = equivalence(sheet, sha, CANONICAL);
|
||||
let expected = sheet == SHEET && sha == ALIAS;
|
||||
assert_eq!(verdict.same_document(), expected, "{sheet} {sha}");
|
||||
same += usize::from(expected);
|
||||
}
|
||||
// Every authentic stamp of the equine draft is the alias; the synthetic ones stay different.
|
||||
println!(
|
||||
"census: {pdfs} PDF attachments, {} stamps, {same} alias",
|
||||
found.len()
|
||||
);
|
||||
assert!(same > 30, "{same} of {}", found.len());
|
||||
}
|
||||
Reference in new issue
Block a user