Skip to content

Commit 33210b5

Browse files
Add tests for tsjs script generation
1 parent 54a0f3d commit 33210b5

1 file changed

Lines changed: 102 additions & 0 deletions

File tree

  • crates/trusted-server-core/src

crates/trusted-server-core/src/tsjs.rs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,105 @@ pub fn tsjs_deferred_script_tags(module_ids: &[&str]) -> String {
6464
.map(|id| tsjs_deferred_script_tag(id))
6565
.collect::<String>()
6666
}
67+
68+
#[cfg(test)]
69+
mod tests {
70+
use super::*;
71+
72+
const UNIFIED_SCRIPT_PREFIX: &str = "/static/tsjs=tsjs-unified.min.js?v=";
73+
74+
fn assert_sha256_hex(hash: &str) {
75+
assert_eq!(hash.len(), 64, "should include a SHA-256 hex hash");
76+
assert!(
77+
hash.chars()
78+
.all(|character| matches!(character, '0'..='9' | 'a'..='f')),
79+
"should include only lowercase hex characters"
80+
);
81+
}
82+
83+
#[test]
84+
fn tsjs_script_src_includes_stable_cache_busting_hash() {
85+
let module_ids = ["core", "lockr", "permutive"];
86+
87+
let src = tsjs_script_src(&module_ids);
88+
let hash = src
89+
.strip_prefix(UNIFIED_SCRIPT_PREFIX)
90+
.expect("should include unified script URL prefix");
91+
92+
assert_sha256_hex(hash);
93+
assert_eq!(
94+
src,
95+
tsjs_script_src(&module_ids),
96+
"should produce a stable URL for identical module IDs"
97+
);
98+
}
99+
100+
#[test]
101+
fn tsjs_script_tag_uses_generated_src_and_trusted_server_id() {
102+
let module_ids = ["core", "lockr"];
103+
let src = tsjs_script_src(&module_ids);
104+
let tag = tsjs_script_tag(&module_ids);
105+
106+
assert_eq!(
107+
tag.matches("<script").count(),
108+
1,
109+
"should emit one opening script tag"
110+
);
111+
assert_eq!(
112+
tag.matches("</script>").count(),
113+
1,
114+
"should emit one closing script tag"
115+
);
116+
assert!(
117+
tag.contains(r#"id="trustedserver-js""#),
118+
"should include trusted server script ID"
119+
);
120+
assert_eq!(
121+
tag,
122+
format!(r#"<script src="{src}" id="trustedserver-js"></script>"#),
123+
"should use generated script source"
124+
);
125+
}
126+
127+
#[test]
128+
fn tsjs_deferred_script_src_includes_module_cache_busting_hash() {
129+
let src = tsjs_deferred_script_src("prebid");
130+
let hash = src
131+
.strip_prefix("/static/tsjs=tsjs-prebid.min.js?v=")
132+
.expect("should include deferred script URL prefix");
133+
134+
assert_sha256_hex(hash);
135+
}
136+
137+
#[test]
138+
fn tsjs_deferred_script_tag_uses_generated_src_and_defer() {
139+
let src = tsjs_deferred_script_src("prebid");
140+
let tag = tsjs_deferred_script_tag("prebid");
141+
142+
assert_eq!(
143+
tag,
144+
format!(r#"<script src="{src}" defer></script>"#),
145+
"should use generated deferred script source"
146+
);
147+
}
148+
149+
#[test]
150+
fn tsjs_deferred_script_tags_concatenates_tags_in_order() {
151+
let module_ids = ["prebid", "lockr"];
152+
153+
assert_eq!(
154+
tsjs_deferred_script_tags(&module_ids),
155+
format!(
156+
"{}{}",
157+
tsjs_deferred_script_tag("prebid"),
158+
tsjs_deferred_script_tag("lockr")
159+
),
160+
"should concatenate deferred script tags in module order"
161+
);
162+
assert_eq!(
163+
tsjs_deferred_script_tags(&[]),
164+
"",
165+
"should return empty output for no deferred modules"
166+
);
167+
}
168+
}

0 commit comments

Comments
 (0)