-
Notifications
You must be signed in to change notification settings - Fork 8
Add test coverage for error mapping and tsjs helpers #648
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,3 +64,107 @@ pub fn tsjs_deferred_script_tags(module_ids: &[&str]) -> String { | |
| .map(|id| tsjs_deferred_script_tag(id)) | ||
| .collect::<String>() | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| fn assert_sha256_hex_hash(src: &str) { | ||
| let hash = src | ||
| .rsplit_once("?v=") | ||
| .expect("should have cache-busting query parameter") | ||
| .1; | ||
|
|
||
| assert_eq!(hash.len(), 64, "should use a SHA-256 hex hash"); | ||
| assert!( | ||
| hash.chars().all(|character| character.is_ascii_hexdigit()), | ||
| "should use only ASCII hex digits", | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn tsjs_script_src_formats_unified_bundle_url_with_hash() { | ||
| let creative_src = tsjs_script_src(&["creative"]); | ||
| let creative_prebid_src = tsjs_script_src(&["creative", "prebid"]); | ||
|
|
||
| assert!( | ||
| creative_src.starts_with("/static/tsjs=tsjs-unified.min.js?v="), | ||
| "should include the unified bundle path and cache-busting parameter", | ||
| ); | ||
| assert_sha256_hex_hash(&creative_src); | ||
| assert_ne!( | ||
| creative_src, creative_prebid_src, | ||
| "should generate different cache-busting URLs for different module sets", | ||
| ); | ||
| } | ||
|
ChristianPavilonis marked this conversation as resolved.
|
||
|
|
||
| #[test] | ||
| fn tsjs_script_tag_wraps_source_in_a_single_tag() { | ||
| let module_ids = ["creative"]; | ||
| let expected_src = tsjs_script_src(&module_ids); | ||
|
|
||
| assert_eq!( | ||
| tsjs_script_tag(&module_ids), | ||
| format!("<script src=\"{expected_src}\" id=\"trustedserver-js\"></script>"), | ||
| "should render the injected trustedserver script tag", | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn tsjs_unified_helpers_use_all_module_ids() { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔧 wrench — Test name advertises "use all module IDs" but the body never verifies that contract. It only checks URL prefix, SHA-256 hex hash structure, and that Fix — make the contract executable by asserting equivalence with the manual all-modules call: let unified = tsjs_unified_script_src();
let manual = tsjs_script_src(&all_module_ids());
assert_eq!(
unified, manual,
"unified helpers must hash every registered module",
);
assert!(
unified.starts_with("/static/tsjs=tsjs-unified.min.js?v="),
"should include the unified bundle path and cache-busting parameter",
);
assert_sha256_hex_hash(&unified);
assert_eq!(
tsjs_unified_script_tag(),
format!("<script src=\"{unified}\" id=\"trustedserver-js\"></script>"),
"should wrap the unified bundle source in the standard script tag",
);This closes the gap: any divergence between |
||
| let src = tsjs_unified_script_src(); | ||
|
|
||
| assert!( | ||
| src.starts_with("/static/tsjs=tsjs-unified.min.js?v="), | ||
| "should include the unified bundle path and cache-busting parameter", | ||
| ); | ||
| assert_sha256_hex_hash(&src); | ||
| assert_eq!( | ||
| tsjs_unified_script_tag(), | ||
| format!("<script src=\"{src}\" id=\"trustedserver-js\"></script>"), | ||
| "should wrap the unified bundle source in the standard script tag", | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn tsjs_deferred_helpers_format_single_module_urls_and_tags() { | ||
| let module_id = "prebid"; | ||
| let expected_hash = single_module_hash(module_id).expect("should hash known module"); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔧 wrench — This re-introduces the same tautology pattern the prior review flagged for Fix — mirror the unified-bundle approach: assert URL prefix and SHA-256 hex hash structure, not equality with another call to the same hash function. For the tag wrap test, derive let src = tsjs_deferred_script_src("prebid");
assert!(
src.starts_with("/static/tsjs=tsjs-prebid.min.js?v="),
"should include the deferred module path and cache-busting parameter",
);
assert_sha256_hex_hash(&src);
assert_eq!(
tsjs_deferred_script_tag("prebid"),
format!("<script src=\"{src}\" defer></script>"),
"should render a deferred script tag for the module",
); |
||
| let expected_src = format!("/static/tsjs=tsjs-{module_id}.min.js?v={expected_hash}"); | ||
|
|
||
| assert_eq!( | ||
| tsjs_deferred_script_src(module_id), | ||
| expected_src, | ||
| "should include the deferred module path and hash", | ||
| ); | ||
| assert_eq!( | ||
| tsjs_deferred_script_tag(module_id), | ||
| format!("<script src=\"{expected_src}\" defer></script>"), | ||
| "should render a deferred script tag for the module", | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn tsjs_deferred_script_src_documents_unregistered_module_fallback() { | ||
| assert_eq!( | ||
| tsjs_deferred_script_src("unknown-module"), | ||
| "/static/tsjs=tsjs-unknown-module.min.js?v=", | ||
| "should preserve the current fallback until callers validate registered modules", | ||
| ); | ||
| } | ||
|
ChristianPavilonis marked this conversation as resolved.
|
||
|
|
||
| #[test] | ||
| fn tsjs_deferred_script_tags_concatenates_tags_in_input_order() { | ||
| let module_ids = ["prebid", "creative"]; | ||
| let expected = module_ids | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔧 wrench — Test body is byte-identical to the production function body (tsjs.rs:62-65): // production
module_ids.iter().map(|id| tsjs_deferred_script_tag(id)).collect::<String>()
// test
let expected = module_ids.iter().map(|id| tsjs_deferred_script_tag(id)).collect::<String>();
assert_eq!(tsjs_deferred_script_tags(&module_ids), expected, ...);Any symmetric regression in either side passes the test. The test name claims to verify input-order concatenation, but the test never actually checks that the modules appear in input order — it just checks that re-running the same expression yields the same result. Fix — assert the property (order preservation, count of tags), not equality with a re-execution of the same code: let result = tsjs_deferred_script_tags(&["prebid", "creative"]);
let prebid_pos = result.find("tsjs-prebid").expect("should contain prebid script");
let creative_pos = result.find("tsjs-creative").expect("should contain creative script");
assert!(prebid_pos < creative_pos, "should preserve input order");
assert_eq!(
result.matches("<script ").count(),
2,
"should emit one tag per module",
); |
||
| .iter() | ||
| .map(|id| tsjs_deferred_script_tag(id)) | ||
| .collect::<String>(); | ||
|
|
||
| assert_eq!( | ||
| tsjs_deferred_script_tags(&module_ids), | ||
| expected, | ||
| "should concatenate one deferred script tag per module in order", | ||
| ); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.