Skip to content

Commit d668fdd

Browse files
committed
test(tui): retry inline-image materialization against parallel image-state wipes
1 parent 44f5617 commit d668fdd

1 file changed

Lines changed: 24 additions & 20 deletions

File tree

crates/jcode-tui/src/tui/ui_inline_image.rs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,18 +1033,30 @@ mod tests {
10331033
/// 1x1 transparent PNG used by the materialize tests below.
10341034
const MATERIALIZE_PNG_B64: &str = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==";
10351035

1036+
/// Materialize `id`, retrying against unrelated parallel tests that wipe
1037+
/// the process-global image state (`mermaid::clear_image_state()` runs on
1038+
/// diagram geometry changes, zoom, and session-picker paths). Each retry
1039+
/// re-stages the payload because `register_payload` is skipped while an
1040+
/// image is materialized and a wipe can clear both stores at once.
1041+
fn materialize_with_retry<F: Fn() -> bool>(id: u64, check: F) {
1042+
for _ in 0..100 {
1043+
register_payload(id, "image/png", MATERIALIZE_PNG_B64);
1044+
if materialize_visible(id) && check() {
1045+
return;
1046+
}
1047+
std::thread::sleep(std::time::Duration::from_millis(5));
1048+
}
1049+
panic!("image {id:#x} never reached the expected state despite retries");
1050+
}
1051+
10361052
#[test]
10371053
fn materialize_visible_probe_is_cheap_after_first_call() {
10381054
let id = mermaid::inline_image_id("image/png", MATERIALIZE_PNG_B64);
1039-
register_payload(id, "image/png", MATERIALIZE_PNG_B64);
1040-
assert!(materialize_visible(id), "first call decodes and caches");
10411055
// Steady state: the in-memory probe alone must report ready, without
10421056
// needing the payload registry at all.
1043-
assert!(
1044-
mermaid::inline_image_is_materialized(id),
1045-
"presence probe should hit after materialization"
1046-
);
1047-
assert!(materialize_visible(id), "repeat call stays true");
1057+
materialize_with_retry(id, || {
1058+
mermaid::inline_image_is_materialized(id) && materialize_visible(id)
1059+
});
10481060
}
10491061

10501062
#[test]
@@ -1053,13 +1065,10 @@ mod tests {
10531065
// materialized image has nothing to prewarm: prefetch must be a cheap
10541066
// no-op (no panic, no scheduling) and the image stays materialized.
10551067
let id = mermaid::inline_image_id("image/png", MATERIALIZE_PNG_B64);
1056-
register_payload(id, "image/png", MATERIALIZE_PNG_B64);
1057-
assert!(materialize_visible(id));
1058-
prefetch(id, 80, 10);
1059-
assert!(
1060-
mermaid::inline_image_is_materialized(id),
1061-
"prefetch must not disturb already-materialized state"
1062-
);
1068+
materialize_with_retry(id, || {
1069+
prefetch(id, 80, 10);
1070+
mermaid::inline_image_is_materialized(id)
1071+
});
10631072
}
10641073

10651074
#[test]
@@ -1068,12 +1077,7 @@ mod tests {
10681077
// Unsupported; a materialized image must still be drawable so the
10691078
// fallback renderers can run.
10701079
let id = mermaid::inline_image_id("image/png", MATERIALIZE_PNG_B64);
1071-
register_payload(id, "image/png", MATERIALIZE_PNG_B64);
1072-
assert!(materialize_visible(id));
1073-
assert!(
1074-
ensure_drawable(id, 80, 10),
1075-
"materialized image must be drawable on non-Kitty protocols"
1076-
);
1080+
materialize_with_retry(id, || ensure_drawable(id, 80, 10));
10771081
}
10781082

10791083
#[test]

0 commit comments

Comments
 (0)