use super::*;
fn sentinel_identifier(content: &str) -> Option<String> {
let rest = content.split_once("const RequireNativeSetup_")?.1;
let ident: String = rest
.chars()
.take_while(|c| c.is_ascii_alphanumeric() || *c == '_')
.collect();
(!ident.is_empty()).then_some(ident)
}
fn version_ident_const(content: &str) -> Option<String> {
let rest = content.split_once("versionIdent")?.1;
let value = rest.split_once('"')?.1;
value.split_once('"').map(|(ident, _)| ident.to_owned())
}
fn native_setup_at(ident: &str, version: &str) -> String {
format!("package htmltomarkdown\n\nconst RequireNativeSetup_{ident} = \"{version}\"\n")
}
fn cmd_setup_at(ident: &str, version: &str) -> String {
format!("package main\n\nconst (\n\tmoduleVersion = \"{version}\"\n\tversionIdent = \"{ident}\"\n)\n")
}
#[test]
fn a_version_bump_moves_the_shim_ident_and_the_sentinel_to_the_same_identifier() {
let new_version = "3.11.4";
let go_version_ident = crate::core::version::to_go_version_ident(new_version);
let sentinel = sync_go_native_setup_sentinel(&native_setup_at("3_11_1", "3.11.1"), &go_version_ident, new_version)
.expect("the sentinel was stale, so a rewrite must have happened");
let shim = sync_go_cmd_setup_version_ident(&cmd_setup_at("3_11_1", "3.11.1"), &go_version_ident)
.expect("the versionIdent const was stale, so a rewrite must have happened");
assert_eq!(
sentinel_identifier(&sentinel).as_deref(),
version_ident_const(&shim).as_deref(),
"the shim references RequireNativeSetup_<versionIdent>; a mismatch is an undefined symbol"
);
assert_eq!(version_ident_const(&shim).as_deref(), Some("3_11_4"));
}
#[test]
fn the_pairing_assertion_detects_the_two_identifiers_drifting_apart() {
let sentinel = sync_go_native_setup_sentinel(&native_setup_at("3_11_1", "3.11.1"), "3_11_2", "3.11.2")
.expect("the sentinel changed");
let untouched_shim = cmd_setup_at("3_11_1", "3.11.1");
assert_eq!(sentinel_identifier(&sentinel).as_deref(), Some("3_11_2"));
assert_ne!(
sentinel_identifier(&sentinel).as_deref(),
version_ident_const(&untouched_shim).as_deref(),
"this is the #463 defect itself; if these compare equal the pairing test proves nothing"
);
}
#[test]
fn the_generated_shim_interpolates_the_same_const_the_rewriter_owns() {
let template = include_str!("../../../backends/go/templates/cmd_setup_main.go.jinja");
assert!(
template.contains(r#""var _ = %s.RequireNativeSetup_%s\n", bindingImportName, versionIdent"#),
"cmd_setup_main.go.jinja no longer builds the sentinel reference from versionIdent"
);
let native = include_str!("../../../backends/go/templates/native_setup.go.jinja");
assert!(
native.contains("const RequireNativeSetup_{{ version_ident }}"),
"native_setup.go.jinja no longer declares the sentinel the shim references"
);
}