use super::super::fixture::{World, fake_lernie, seed_world, world};
use super::super::inbox_composer::{deposit, drain};
use super::super::screen::Screen;
use super::{SAID, converge_ws, faded_user, fills, quick, say, shot};
use crate::cli_outbound::Cli;
use std::os::unix::fs::PermissionsExt;
use std::path::Path;
use tempfile::tempdir;
fn queue_rows(out: &egui::FullOutput) -> Vec<String> {
crate::paint_probe::painted_of(out)
.into_iter()
.filter(|(text, _)| text.starts_with('✉'))
.map(|(text, _)| text)
.collect()
}
fn said(out: &egui::FullOutput) -> usize {
crate::paint_probe::painted_of(out)
.into_iter()
.filter(|(text, _)| text.contains(SAID))
.count()
}
fn depositing_lernie(dir: &Path, ws: &Path) -> Cli {
let path = dir.join("lernie");
let inbox = ws.join("inbox/c-1");
std::fs::write(
&path,
format!(
"#!/bin/sh\ncase \"$1\" in\nmessage) mkdir -p '{}' && \
printf -- '---\\nfrom: user\\ndeposited_at: t1\\n---\\n%s\\n' \"$4\" \
> '{}/user-002.md';;\nesac\nexit 0\n",
inbox.display(),
inbox.display()
),
)
.unwrap();
let mut perms = std::fs::metadata(&path).unwrap().permissions();
perms.set_mode(0o755);
std::fs::set_permissions(&path, perms).unwrap();
Cli::new(path)
}
fn queued() -> World {
let mut world = quick(world());
seed_world(&world);
let ws = world.ws.clone();
world.model.focus_agent(&ws, "c-1");
world.model.select_tab(crate::keymap::InspectorTab::Steps);
world
}
#[test]
fn one_message_is_one_queue_row_faded_then_solid_then_gone() {
let bin = tempdir().unwrap();
let mut world = queued();
let screen = Screen::with_lernie(fake_lernie(bin.path()));
let before = shot(&screen, &mut world);
assert_eq!(
queue_rows(&before),
["✉ user · t0"],
"the fixture's one landed deposit, and nothing else"
);
say(&screen, &mut world);
let inbox = world.ws.join("inbox/c-1");
assert_eq!(
std::fs::read_dir(&inbox).map_or(0, |d| d.flatten().count()),
1,
"no file yet: whatever the queue shows is yog's own word"
);
let pending = shot(&screen, &mut world);
assert_eq!(queue_rows(&pending).len(), 2, "the echo took a seat");
assert_eq!(said(&pending), 1, "and says what was typed, once");
assert!(
fills(&pending).contains(&faded_user()),
"faded: not yet a statement (§11)"
);
deposit(&world, "user-002.md", "t1", SAID);
converge_ws(&mut world);
let landed = shot(&screen, &mut world);
assert_eq!(
queue_rows(&landed),
["✉ user · t0", "✉ user · t1"],
"still two rows: the deposit took the echo's seat rather than a new one"
);
assert_eq!(
said(&landed),
1,
"the operator's words are on the glass once"
);
assert!(
!fills(&landed).contains(&faded_user()),
"and at full strength: that same row, brightened (§7.2)"
);
drain(&world);
let messages = world.ws.join("agents/c-1/messages");
std::fs::write(messages.join("003-user.md"), SAID).unwrap();
converge_ws(&mut world);
let delivered = shot(&screen, &mut world);
assert!(
queue_rows(&delivered).is_empty(),
"the queue is empty: {:?}",
queue_rows(&delivered)
);
}
#[test]
fn the_substrates_own_write_never_earns_a_second_row() {
let bin = tempdir().unwrap();
let mut world = queued();
let ws = world.ws.clone();
let screen = Screen::with_lernie(depositing_lernie(bin.path(), &ws));
shot(&screen, &mut world);
say(&screen, &mut world);
assert_eq!(
std::fs::read_dir(ws.join("inbox/c-1")).map_or(0, |d| d.flatten().count()),
2,
"the piped verb wrote its deposit before the receipt came back"
);
converge_ws(&mut world);
let out = shot(&screen, &mut world);
assert_eq!(
queue_rows(&out),
["✉ user · t0", "✉ user · t1"],
"two deposits, two rows — the echo never earned a third"
);
assert_eq!(said(&out), 1, "and one row carries what was typed");
}