use taim_core::input_method::{Purpose, SurroundingText};
use taim_core::semantics::{Node, Role, Value as SemanticValue};
use taim_core::widget::Id;
use taim_core::{Rectangle, Size};
use taim_widget::text_input::Value;
const SECRET: &str = "correct horse battery staple";
fn node(role: Role) -> Node {
Node::new(
Id::unique(),
Rectangle::with_size(Size::new(100.0, 20.0)),
role,
)
}
#[test]
fn a_secure_field_discloses_nothing_through_surrounding_text() {
let disclosure = SurroundingText::for_field(SECRET, 7..12, true);
assert_eq!(disclosure, SurroundingText::Redacted);
assert!(disclosure.is_redacted());
let formatted = format!("{disclosure:?}");
assert!(!formatted.contains("correct"));
assert!(!formatted.contains(SECRET));
}
#[test]
fn a_non_secure_field_still_supplies_surrounding_text_to_the_ime() {
let disclosure = SurroundingText::for_field("hello", 0..5, false);
assert!(!disclosure.is_redacted());
assert_eq!(
disclosure,
SurroundingText::Visible {
content: "hello".to_owned(),
selection: 0..5,
}
);
}
#[test]
fn a_secure_field_node_refuses_a_semantic_value() {
let secure = node(Role::SecureField).with_value(SemanticValue::Text(SECRET.into()));
assert_eq!(
secure.value(),
None,
"a SecureField must never carry a semantic value"
);
let formatted = format!("{secure:?}");
assert!(
!formatted.contains("correct"),
"the secret leaked through the node's own debug output: {formatted}"
);
}
#[test]
fn a_secure_field_refuses_a_value_no_matter_the_construction_order() {
let a = node(Role::SecureField)
.with_name("Password")
.with_value(SemanticValue::Text(SECRET.into()));
let b = node(Role::SecureField)
.with_value(SemanticValue::Text(SECRET.into()))
.with_name("Password");
assert_eq!(a.value(), None);
assert_eq!(b.value(), None);
assert!(a.name.is_some());
assert!(b.name.is_some());
}
#[test]
fn an_ordinary_text_field_still_discloses_its_value() {
let ordinary = node(Role::TextField).with_value(SemanticValue::Text("hello".into()));
assert_eq!(
ordinary.value(),
Some(&SemanticValue::Text("hello".into())),
"an ordinary field must remain readable by a screen reader"
);
}
#[test]
fn a_value_never_discloses_its_content_through_debug() {
let secret = Value::new(SECRET);
let formatted = format!("{secret:?}");
assert!(
!formatted.contains("correct"),
"Value's Debug disclosed its content: {formatted}"
);
assert!(!formatted.contains(SECRET));
assert!(
!formatted.contains('h') || !formatted.contains('o'),
"Value's Debug appears to be printing graphemes: {formatted}"
);
}
#[test]
fn a_value_does_not_disclose_its_length_through_debug() {
let short = Value::new("ab");
let long = Value::new(SECRET);
assert_eq!(
format!("{short:?}"),
format!("{long:?}"),
"Debug output differs by length, disclosing how long the secret is"
);
}
#[test]
fn debug_redaction_survives_being_nested_inside_another_struct() {
#[derive(Debug)]
#[allow(dead_code)]
struct FormState {
username: String,
password: Value,
}
let state = FormState {
username: "alice".to_owned(),
password: Value::new(SECRET),
};
let formatted = format!("{state:?}");
assert!(
!formatted.contains("correct"),
"the secret leaked through the containing struct: {formatted}"
);
assert!(formatted.contains("alice"));
}
#[test]
fn a_secure_value_masks_every_grapheme_when_rendered_or_copied() {
let masked = Value::new(SECRET).secure();
let rendered = masked.to_string();
assert!(!rendered.contains("correct"));
assert!(
rendered.chars().all(|c| c == 'โข'),
"a masked value must contain nothing but bullets, got: {rendered}"
);
assert_eq!(masked.len(), Value::new(SECRET).len());
}
#[test]
fn masking_a_multi_codepoint_grapheme_yields_exactly_one_bullet() {
let family = Value::new("๐จโ๐ฉโ๐งโ๐ฆ");
assert_eq!(family.len(), 1);
assert_eq!(family.secure().to_string(), "โข");
let combining = Value::new("e\u{0301}");
assert_eq!(combining.len(), 1);
assert_eq!(combining.secure().to_string(), "โข");
}
#[test]
fn the_ime_purpose_marks_secure_fields_for_platform_capture_policy() {
assert_ne!(Purpose::Secure, Purpose::Normal);
assert_ne!(Purpose::Secure, Purpose::Terminal);
assert_eq!(Purpose::default(), Purpose::Normal);
}
#[test]
fn revealing_a_password_is_an_explicit_act_that_produces_a_distinct_value() {
let value = Value::new(SECRET);
let masked = value.secure().to_string();
let revealed = value.to_string();
assert_ne!(masked, revealed);
assert_eq!(
revealed, SECRET,
"an explicit reveal shows the real content"
);
assert!(!masked.contains("correct"));
}
#[test]
fn a_pathological_grapheme_cluster_does_not_explode_the_value() {
let zalgo: String = std::iter::once('e')
.chain(std::iter::repeat_n('\u{0301}', 2_000))
.collect();
let value = Value::new(&zalgo);
assert_eq!(
value.len(),
1,
"a combining run must collapse to one grapheme, not {}",
value.len()
);
assert_eq!(value.secure().to_string(), "โข");
}
#[test]
fn a_long_untrusted_string_stays_proportional_and_terminates() {
let long = "a".repeat(100_000);
let value = Value::new(&long);
assert_eq!(value.len(), 100_000);
assert_eq!(value.secure().len(), 100_000);
assert_eq!(value.previous_start_of_word(0), 0);
assert!(value.next_end_of_word(0) <= value.len());
}