Skip to content

Commit 6969509

Browse files
Add tests for tsjs script generation (#757)
1 parent 395b73e commit 6969509

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

  • crates/trusted-server-core/src

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,19 @@ mod tests {
139139
);
140140
}
141141

142+
#[test]
143+
fn tsjs_script_src_is_stable_for_identical_module_ids() {
144+
let module_ids = ["core", "lockr", "permutive"];
145+
let src = tsjs_script_src(&module_ids);
146+
147+
assert_sha256_hex_hash(hash_query_value(&src));
148+
assert_eq!(
149+
src,
150+
tsjs_script_src(&module_ids),
151+
"should produce a stable URL for identical module IDs"
152+
);
153+
}
154+
142155
#[test]
143156
fn tsjs_script_tag_wraps_source_in_single_trustedserver_tag() {
144157
let module_ids = ["creative"];
@@ -219,4 +232,38 @@ mod tests {
219232
"should preserve caller-provided deferred module order"
220233
);
221234
}
235+
236+
#[test]
237+
fn tsjs_unified_script_src_and_tag_include_cache_busting_hash() {
238+
let src = tsjs_unified_script_src();
239+
240+
assert!(
241+
src.starts_with("/static/tsjs=tsjs-unified.min.js?v="),
242+
"should include unified script URL prefix"
243+
);
244+
assert_sha256_hex_hash(hash_query_value(&src));
245+
assert_eq!(
246+
tsjs_unified_script_tag(),
247+
format!(r#"<script src="{src}" id="trustedserver-js"></script>"#),
248+
"should wrap the unified source in a trusted server script tag"
249+
);
250+
}
251+
252+
#[test]
253+
fn tsjs_script_src_differs_for_different_module_sets() {
254+
assert_ne!(
255+
tsjs_script_src(&["lockr"]),
256+
tsjs_script_src(&["lockr", "permutive"]),
257+
"should bust the cache when the module set content changes"
258+
);
259+
}
260+
261+
#[test]
262+
fn tsjs_deferred_script_src_has_empty_hash_for_unknown_module() {
263+
assert_eq!(
264+
tsjs_deferred_script_src("does-not-exist"),
265+
"/static/tsjs=tsjs-does-not-exist.min.js?v=",
266+
"should fall back to an empty cache-busting hash for an unknown module"
267+
);
268+
}
222269
}

0 commit comments

Comments
 (0)