use crate::brain::agent::service::helpers::{detect_text_repetition, strip_fenced_code};
fn twin_sql_answer() -> String {
let q = |table: &str| {
format!(
"```sql\n\
SELECT\n \
COUNT(*) FILTER (WHERE intake->>'roomsMin' IS NULL) AS omitted,\n \
COUNT(*) FILTER (WHERE intake->>'roomsMin' = '0') AS zero,\n \
COUNT(*) AS total\n\
FROM {table} WHERE intake IS NOT NULL;\n\
```\n"
)
};
format!(
"First table:\n{}\nNow the second:\n{}\nThat is the whole picture.\n",
q("leads_table"),
q("demands_table")
)
}
#[test]
fn two_near_identical_queries_are_not_a_loop() {
let answer = twin_sql_answer();
assert!(
detect_text_repetition(&answer, 120),
"premise changed: the raw text no longer trips the detector"
);
assert!(
!detect_text_repetition(&strip_fenced_code(&answer), 120),
"repeated code blocks must not read as a provider loop"
);
}
#[test]
fn prose_repetition_is_still_detected() {
let looping = "I should check the config now. I should check the config now. \
I should check the config now. I should check the config now."
.repeat(3);
assert!(detect_text_repetition(&strip_fenced_code(&looping), 60));
}
#[test]
fn a_loop_around_a_code_block_is_still_detected() {
let text = "Let me try that again exactly as before now.\n\
```sh\necho one\n```\n\
Let me try that again exactly as before now.\n\
```sh\necho two\n```\n\
Let me try that again exactly as before now.\n";
assert!(detect_text_repetition(&strip_fenced_code(text), 40));
}
#[test]
fn an_unclosed_fence_masks_to_the_end() {
let text = "Here is the query:\n```sql\nSELECT 1;\nSELECT 1;\nSELECT 1;\n";
let masked = strip_fenced_code(text);
assert!(masked.contains("Here is the query:"));
assert!(!masked.contains("SELECT 1;"), "got: {masked:?}");
}
#[test]
fn text_without_fences_is_unchanged() {
let plain = "A plain answer with no code at all.\nSecond line.\n";
assert_eq!(strip_fenced_code(plain), plain);
}