pub mod feed;
pub mod form;
pub mod message;
pub mod ops;
pub mod sanitize;
pub mod store;
pub mod thread;
pub mod view;
pub mod wire;
use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use crate::devsbd::notify::{Level, Message, Record};
pub use feed::{Block, FeedItem, Field, ItemKind, Marker, MAX_FEED};
pub use form::{Answer, FormRecord, FormState};
pub use message::MessageSend;
pub use sanitize::sanitize;
pub use thread::{Action, Compose, State, ThreadPut};
pub use view::View;
pub const THREADS_PER_OWNER: usize = 200;
pub const MAX_NOTES: usize = MAX_FEED;
pub const VERSION: u32 = 3;
const V2: u32 = 2;
pub const RETENTION: u64 = 14 * 86_400;
pub const MAX_EVENTS: usize = 100;
pub const MAX_REPLY: usize = 2000;
pub fn is_url(link: &str) -> bool {
["http://", "https://"]
.iter()
.any(|p| link.len() > p.len() && link[..p.len()].eq_ignore_ascii_case(p))
}
fn one_line(text: &str) -> String {
text.split_whitespace().collect::<Vec<_>>().join(" ")
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Note {
pub id: u64,
pub record: Record,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Kind {
#[default]
Notify,
Thread,
}
const UNRESOLVED_OWNER: &str = "name:";
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum EventKind {
Action,
Reply,
Done,
Reopen,
Submit,
}
impl EventKind {
pub fn as_str(self) -> &'static str {
match self {
EventKind::Action => "action",
EventKind::Reply => "reply",
EventKind::Done => "done",
EventKind::Reopen => "reopen",
EventKind::Submit => "submit",
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Event {
pub id: String,
pub seq: u64,
pub kind: EventKind,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub action: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub text: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub message: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub form: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub answers: Option<serde_json::Value>,
pub at: u64,
}
impl Event {
fn of(kind: EventKind) -> Event {
Event { id: String::new(), seq: 0, kind, action: None, text: None, message: None, form: None, answers: None, at: 0 }
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Thread {
pub id: u64,
pub owner: String,
pub owner_name: String,
pub key: Option<String>,
pub kind: Kind,
pub archived: bool,
pub unread: bool,
pub notes: Vec<Note>,
pub title: String,
pub link: Option<String>,
pub state: Option<State>,
pub status: Option<String>,
pub child: Option<String>,
pub actions: Vec<Action>,
pub compose: Option<Compose>,
pub updated_at: u64,
pub feed: Vec<FeedItem>,
pub events: Vec<Event>,
}
impl Thread {
pub fn head(&self) -> Option<&Record> {
self.notes.first().map(|n| &n.record)
}
pub fn changed_at(&self) -> u64 {
self.updated_at.max(self.head().map_or(0, |r| r.at))
}
pub fn needs_you(&self) -> bool {
!self.archived
&& match self.kind {
Kind::Notify => self.unread,
Kind::Thread => self.state == Some(State::NeedsYou),
}
}
pub fn to_put(&self) -> ThreadPut {
ThreadPut {
key: self.key.clone().unwrap_or_default(),
title: self.title.clone(),
link: self.link.clone(),
state: self.state.unwrap_or_default(),
status: self.status.clone(),
child: self.child.clone(),
actions: self.actions.clone(),
compose: self.compose.clone(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Op {
MarkRead(u64),
MarkNotifyRead,
RemoveThread(u64),
ClearNotify,
Act { thread: u64, action: String },
Reply { thread: u64, text: String },
MarkDone(u64),
Reopen(u64),
SaveDraft { thread: u64, message: String, answers: BTreeMap<String, Answer> },
Submit { thread: u64, message: String, answers: BTreeMap<String, Answer> },
}
impl Op {
pub fn enqueues_event(&self) -> bool {
matches!(self, Op::Act { .. } | Op::Reply { .. } | Op::MarkDone(_) | Op::Reopen(_) | Op::Submit { .. })
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Inbox {
pub threads: Vec<Thread>,
next_id: u64,
}
impl Inbox {
fn next_id(&mut self) -> u64 {
let id = self.next_id;
self.next_id += 1;
id
}
pub fn push(&mut self, owner: String, owner_name: String, record: Record, unread: bool) {
let note = Note { id: self.next_id(), record };
let key = note.record.key.clone();
let existing = key.as_ref().and_then(|key| {
self.threads
.iter()
.position(|t| t.owner == owner && t.kind == Kind::Notify && t.key.as_ref() == Some(key))
});
let thread = match existing {
Some(pos) => {
let mut t = self.threads.remove(pos);
t.notes.insert(0, note);
t.notes.truncate(MAX_NOTES);
t.unread = unread;
t.owner_name = owner_name;
t.archived = false;
t
}
None => {
let id = self.next_id();
Thread {
id,
owner: owner.clone(),
owner_name,
key,
notes: vec![note],
unread,
..Thread::default()
}
}
};
let id = thread.id;
self.threads.insert(0, thread);
self.enforce_cap(&owner, id);
}
pub fn put(&mut self, owner: &str, owner_name: &str, at: u64, put: ThreadPut) -> PutOutcome {
let pos = self.threads.iter().position(|t| {
t.owner == owner && t.kind == Kind::Thread && t.key.as_deref() == Some(put.key.as_str())
});
let Some(pos) = pos else {
let id = self.next_id();
let mut thread = Thread {
id,
owner: owner.to_string(),
owner_name: owner_name.to_string(),
key: Some(put.key.clone()),
kind: Kind::Thread,
unread: true,
updated_at: at,
..Thread::default()
};
apply_put(&mut thread, put);
let entered = thread.state == Some(State::NeedsYou);
self.threads.insert(0, thread);
self.enforce_cap(owner, id);
return PutOutcome::Applied { created: true, entered_needs_you: entered };
};
let old = &self.threads[pos];
let state = old.state != Some(put.state);
let status = old.status != put.status;
let cosmetic = old.owner_name != owner_name
|| old.title != put.title
|| old.link != put.link
|| old.child != put.child
|| old.actions != put.actions
|| old.compose != put.compose
|| old.archived;
if !(state || status || cosmetic) {
return PutOutcome::Unchanged;
}
let entered = state && put.state == State::NeedsYou;
let news = state || status;
let mut markers = Vec::new();
if state {
markers.push(Marker::State { from: old.state.unwrap_or_default(), to: put.state });
}
if status {
markers.push(Marker::Status { from: old.status.clone(), to: put.status.clone() });
}
let mut thread = self.threads.remove(pos);
thread.owner_name = owner_name.to_string();
thread.archived = false;
thread.unread = thread.unread || news;
thread.updated_at = at;
for marker in markers {
let seq = self.next_id();
feed::push(&mut thread.feed, FeedItem { seq, at, kind: ItemKind::Marker(marker) });
}
feed::cap(&mut thread.feed, MAX_FEED);
apply_put(&mut thread, put);
let id = thread.id;
self.threads.insert(0, thread);
self.enforce_cap(owner, id);
PutOutcome::Applied { created: false, entered_needs_you: entered }
}
pub fn thread_rm(&mut self, owner: &str, key: &str) -> bool {
let before = self.threads.len();
self.threads
.retain(|t| !(t.owner == owner && t.kind == Kind::Thread && t.key.as_deref() == Some(key)));
before != self.threads.len()
}
pub fn archive_owner(&mut self, owner: &str) -> usize {
let mut archived = 0;
for t in self.threads.iter_mut().filter(|t| t.owner == owner && !t.archived) {
t.archived = true;
archived += 1;
}
archived
}
pub fn prune(&mut self, now: u64) -> usize {
let before = self.threads.len();
self.threads.retain(|t| {
let retired = t.archived || (t.state == Some(State::Done) && t.events.is_empty());
!(retired && now.saturating_sub(t.changed_at()) > RETENTION)
});
before - self.threads.len()
}
pub fn apply_sink(&mut self, owner: &str, owner_name: &str, now: u64, action: SinkAction) -> Option<Shown> {
self.prune(now);
match sanitize_action(action) {
SinkAction::Push(record) => {
let shown = Shown {
line: format!("{owner_name}: {}", one_line(&record.msg)),
popup: Some(ShownPopup {
key: record.key.clone(),
level: record.level,
body: match &record.link {
Some(link) => format!("{}\n{link}", record.msg),
None => record.msg.clone(),
},
}),
};
self.push(owner.to_string(), owner_name.to_string(), record, true);
Some(shown)
}
SinkAction::Rm { key } => {
self.thread_rm(owner, &key);
None
}
SinkAction::Put { at, put } => {
let key = put.key.clone();
let line = match &put.status {
Some(status) => format!("{owner_name}: {} — {status}", put.title),
None => format!("{owner_name}: {}", put.title),
};
let body = put.title.clone();
match self.put(owner, owner_name, at, put) {
PutOutcome::Unchanged => None,
PutOutcome::Applied { entered_needs_you, .. } => Some(Shown {
line,
popup: entered_needs_you.then(|| ShownPopup {
key: Some(format!("thread:{key}")),
level: Level::Warn,
body,
}),
}),
}
}
SinkAction::Send { at, send } => {
let key = send.thread.clone();
let text = feed::markdown_of(&send.blocks);
match self.send(owner, at, send) {
None => {
let why = format!("thread send rejected: no thread `{key}`; put it first");
self.apply_sink(owner, owner_name, now, refused(at, &key, &why))
}
Some(feed::Sent::Unchanged | feed::Sent::Edited { form_opened: false }) => None,
Some(feed::Sent::Inserted | feed::Sent::Edited { form_opened: true }) => {
let t = self.owner_thread(owner, &key).map(|pos| &self.threads[pos])?;
let summary = one_line(&text);
let line = match summary.is_empty() {
true => format!("{owner_name}: {}", t.title),
false => format!("{owner_name}: {} — {summary}", t.title),
};
let popup = (t.state == Some(State::NeedsYou)).then(|| ShownPopup {
key: Some(format!("thread:{key}")),
level: Level::Warn,
body: match text.is_empty() {
true => t.title.clone(),
false => format!("{}\n{text}", t.title),
},
});
Some(Shown { line, popup })
}
}
}
SinkAction::Withdraw { thread, id } => {
self.withdraw(owner, &thread, &id);
None
}
}
}
fn owner_thread(&self, owner: &str, key: &str) -> Option<usize> {
self.threads
.iter()
.position(|t| t.owner == owner && t.kind == Kind::Thread && t.key.as_deref() == Some(key))
}
pub fn send(&mut self, owner: &str, at: u64, send: MessageSend) -> Option<feed::Sent> {
let pos = self.owner_thread(owner, &send.thread)?;
let mut thread = self.threads.remove(pos);
let sent = feed::send(&mut thread.feed, &send.id, send.blocks, || self.next_id(), at);
match sent {
feed::Sent::Unchanged => self.threads.insert(pos, thread),
feed::Sent::Edited { form_opened: false } => {
thread.updated_at = thread.updated_at.max(at);
self.threads.insert(pos, thread);
}
feed::Sent::Inserted | feed::Sent::Edited { form_opened: true } => {
feed::cap(&mut thread.feed, MAX_FEED);
thread.unread = true;
thread.updated_at = thread.updated_at.max(at);
self.threads.insert(0, thread);
}
}
Some(sent)
}
pub fn withdraw(&mut self, owner: &str, thread: &str, id: &str) -> bool {
match self.owner_thread(owner, thread) {
Some(pos) => feed::withdraw(&mut self.threads[pos].feed, id),
None => false,
}
}
pub fn apply(&mut self, op: &Op, now: u64, client: &str) {
let client = client.to_string();
match op {
Op::Act { thread, action } => self.user_op(*thread, now, |t| {
let a = t.actions.iter().find(|a| &a.id == action)?;
if !a.enqueues_event() {
return None;
}
let pressed = ItemKind::Action { action: a.id.clone(), label: a.label.clone(), client: client.clone() };
Some(match a.done {
true => UserOp {
state: Some(State::Done),
items: vec![pressed, ItemKind::Marker(Marker::Done { client })],
event: Event { action: Some(a.id.clone()), ..Event::of(EventKind::Done) },
submitted: None,
},
false => UserOp {
state: None,
items: vec![pressed],
event: Event { action: Some(a.id.clone()), ..Event::of(EventKind::Action) },
submitted: None,
},
})
}),
Op::Reply { thread, text } => self.user_op(*thread, now, |t| {
let text = text.trim();
if t.compose.is_none() || text.is_empty() {
return None;
}
let text: String = text.chars().take(MAX_REPLY).collect();
Some(UserOp {
state: None,
items: vec![ItemKind::Reply { text: text.clone(), client }],
event: Event { text: Some(text), ..Event::of(EventKind::Reply) },
submitted: None,
})
}),
Op::MarkDone(thread) => self.user_op(*thread, now, |t| {
(t.state != Some(State::Done)).then(|| UserOp {
state: Some(State::Done),
items: vec![ItemKind::Marker(Marker::Done { client })],
event: Event::of(EventKind::Done),
submitted: None,
})
}),
Op::Reopen(thread) => self.user_op(*thread, now, |t| {
(t.state == Some(State::Done)).then(|| UserOp {
state: Some(State::Active),
items: vec![ItemKind::Marker(Marker::Reopen { client })],
event: Event::of(EventKind::Reopen),
submitted: None,
})
}),
Op::SaveDraft { thread, message, answers } => {
let Some(t) = self.threads.iter_mut().find(|t| t.id == *thread) else { return };
if t.kind != Kind::Thread || t.archived {
return;
}
let Some((form, record)) = feed::form_of(&t.feed, message) else { return };
let Ok(answers) = form::validate_draft(form, record, answers) else { return };
if let Some(record) = feed::form_record_mut(&mut t.feed, message) {
record.draft.extend(answers);
}
}
Op::Submit { thread, message, answers } => self.user_op(*thread, now, |t| {
let (form, record) = feed::form_of(&t.feed, message)?;
let resolved = form::resolve_submission(form, record, answers).ok()?;
Some(UserOp {
state: None,
items: vec![ItemKind::Submission {
message: message.clone(),
form: form.id.clone(),
answers: resolved.clone(),
client: client.clone(),
}],
event: Event {
message: Some(message.clone()),
form: Some(form.id.clone()),
answers: Some(form::answers_json(form, &resolved)),
..Event::of(EventKind::Submit)
},
submitted: Some((message.clone(), FormState::Submitted { at: now, answers: resolved, client })),
})
}),
Op::MarkNotifyRead => {
for t in self.threads.iter_mut().filter(|t| t.kind == Kind::Notify) {
t.unread = false;
}
}
Op::MarkRead(id) => {
if let Some(t) = self.threads.iter_mut().find(|t| t.id == *id) {
t.unread = false;
}
}
Op::RemoveThread(id) => self.threads.retain(|t| t.id != *id),
Op::ClearNotify => self.threads.retain(|t| t.kind != Kind::Notify),
}
}
fn user_op(&mut self, id: u64, now: u64, decide: impl FnOnce(&Thread) -> Option<UserOp>) {
let Some(pos) = self.threads.iter().position(|t| t.id == id) else { return };
let t = &self.threads[pos];
if t.kind != Kind::Thread || t.archived {
return;
}
let Some(op) = decide(t) else { return };
let event_id = self.mint_event_id(now);
let seqs: Vec<u64> = op.items.iter().map(|_| self.next_id()).collect();
let event_seq = self.next_id();
let t = &mut self.threads[pos];
if let Some(state) = op.state {
t.state = Some(state);
}
t.updated_at = t.updated_at.max(now);
if let Some((message, state)) = op.submitted {
if let Some(record) = feed::form_record_mut(&mut t.feed, &message) {
record.state = state;
record.draft.clear();
}
}
for (seq, kind) in seqs.into_iter().zip(op.items) {
feed::push(&mut t.feed, FeedItem { seq, at: now, kind });
}
feed::cap(&mut t.feed, MAX_FEED);
t.events.push(Event { id: event_id, seq: event_seq, at: now, ..op.event });
if t.events.len() > MAX_EVENTS {
let extra = t.events.len() - MAX_EVENTS;
t.events.drain(..extra);
}
}
fn mint_event_id(&self, now: u64) -> String {
use std::hash::{BuildHasher, Hasher};
loop {
let mut h = std::collections::hash_map::RandomState::new().build_hasher();
h.write_u64(now);
let id = format!("e-{now:010}-{:04x}", h.finish() as u16);
if !self.threads.iter().any(|t| t.events.iter().any(|e| e.id == id)) {
return id;
}
}
}
pub fn events_for(&self, owner: &str) -> Vec<(String, Event)> {
let mut out: Vec<(String, Event)> = self
.threads
.iter()
.filter(|t| t.owner == owner && t.kind == Kind::Thread)
.flat_map(|t| {
let key = t.key.clone().unwrap_or_default();
t.events.iter().map(move |e| (key.clone(), e.clone()))
})
.collect();
out.sort_by_key(|(_, e)| e.seq);
out
}
pub fn ack(&mut self, owner: &str, ids: &[String]) -> usize {
let mut dropped = 0;
for t in self.threads.iter_mut().filter(|t| t.owner == owner) {
let before = t.events.len();
t.events.retain(|e| !ids.contains(&e.id));
dropped += before - t.events.len();
}
dropped
}
pub fn threads_for(&self, owner: &str) -> Vec<&Thread> {
self.threads
.iter()
.filter(|t| t.owner == owner && t.kind == Kind::Thread && !t.archived)
.collect()
}
fn enforce_cap(&mut self, owner: &str, keep: u64) {
while self.threads.iter().filter(|t| t.owner == owner).count() > THREADS_PER_OWNER {
let mine = |t: &Thread| t.owner == owner && t.id != keep;
let pos = self
.oldest_thread(|t| mine(t) && t.archived)
.or_else(|| self.oldest_thread(|t| mine(t) && t.state == Some(State::Done) && t.events.is_empty()))
.or_else(|| self.oldest_thread(|t| mine(t) && t.events.is_empty()));
let Some(pos) = pos else { return };
self.threads.remove(pos);
}
}
fn oldest_thread(&self, pick: impl Fn(&Thread) -> bool) -> Option<usize> {
self.threads
.iter()
.enumerate()
.filter(|(_, t)| pick(t))
.min_by_key(|(_, t)| t.changed_at())
.map(|(i, _)| i)
}
pub fn needs_you(&self) -> usize {
self.threads.iter().filter(|t| t.needs_you()).count()
}
pub fn needs_you_for(&self, owner: &str) -> usize {
self.threads.iter().filter(|t| t.needs_you() && t.owner == owner).count()
}
pub fn to_json(&self) -> Result<String, String> {
let saved = SavedInbox {
version: VERSION,
next_id: self.next_id,
threads: self
.threads
.iter()
.map(|t| SavedThread {
id: t.id,
owner: t.owner.clone(),
owner_name: t.owner_name.clone(),
key: t.key.clone(),
kind: t.kind,
archived: t.archived,
unread: t.unread,
title: t.title.clone(),
link: t.link.clone(),
state: t.state,
status: t.status.clone(),
child: t.child.clone(),
actions: t.actions.clone(),
compose: t.compose.clone(),
updated_at: t.updated_at,
feed: t.feed.clone(),
events: t.events.clone(),
notes: t
.notes
.iter()
.map(|n| SavedNote {
seq: n.id,
level: n.record.level.as_str().to_string(),
at: n.record.at,
link: n.record.link.clone(),
msg: n.record.msg.clone(),
})
.collect(),
})
.collect(),
};
serde_json::to_string_pretty(&saved).map(|mut s| {
s.push('\n');
s
}).map_err(|e| e.to_string())
}
pub fn from_json(text: &str) -> Result<Inbox, String> {
#[derive(Deserialize)]
struct Probe {
version: Option<u32>,
}
let probe: Probe = serde_json::from_str(text).map_err(|e| e.to_string())?;
match probe.version {
Some(VERSION) => {}
Some(v) => return Err(format!("store version {v}, this build reads version {VERSION}")),
None => return Err("no `version`".into()),
}
let saved: SavedInbox = serde_json::from_str(text).map_err(|e| e.to_string())?;
let mut inbox = Inbox::default();
for t in saved.threads {
let notes = t
.notes
.into_iter()
.map(|n| {
let level = Level::parse(&n.level).ok_or_else(|| format!("bad level `{}`", n.level))?;
let record = Record { level, key: t.key.clone(), link: n.link, msg: n.msg, at: n.at };
Ok(Note { id: n.seq, record })
})
.collect::<Result<Vec<_>, String>>()?;
inbox.threads.push(Thread {
id: t.id,
owner: t.owner,
owner_name: t.owner_name,
key: t.key,
kind: t.kind,
archived: t.archived,
unread: t.unread,
notes,
title: t.title,
link: t.link,
state: t.state,
status: t.status,
child: t.child,
actions: t.actions,
compose: t.compose,
updated_at: t.updated_at,
feed: t.feed,
events: t.events,
});
}
inbox.next_id = saved.next_id.max(inbox.max_id().map_or(0, |m| m + 1));
Ok(inbox)
}
pub fn import_v2(text: &str, names: &BTreeMap<String, String>) -> Result<Inbox, String> {
let saved: V2Inbox = toml::from_str(text).map_err(|e| e.to_string())?;
let mut inbox = Inbox::default();
for t in saved.threads.into_iter().filter(|t| t.kind == Kind::Notify) {
let v1_key = t.notes.first().and_then(|n| n.key.clone());
let mut notes = t
.notes
.into_iter()
.map(|n| {
let level = Level::parse(&n.level).ok_or_else(|| format!("bad level `{}`", n.level))?;
Ok(Note { id: n.seq, record: Record { level, key: None, link: n.link, msg: n.msg, at: n.at } })
})
.collect::<Result<Vec<_>, String>>()?;
if notes.is_empty() {
continue;
}
notes.truncate(MAX_NOTES);
let (owner, owner_name, key) = match t.instance {
Some(name) => {
let owner =
names.get(&name).cloned().unwrap_or_else(|| format!("{UNRESOLVED_OWNER}{name}"));
(owner, name, v1_key)
}
None => (t.owner, t.owner_name, t.key),
};
let id = if saved.version >= V2 { t.id } else { inbox.next_id() };
let notes = notes
.into_iter()
.map(|n| Note { record: Record { key: key.clone(), ..n.record }, ..n })
.collect();
inbox.threads.push(Thread {
id,
archived: t.archived || owner.starts_with(UNRESOLVED_OWNER),
owner,
owner_name,
key,
unread: t.unread,
notes,
..Thread::default()
});
}
inbox.next_id = inbox.next_id.max(inbox.max_id().map_or(0, |m| m + 1));
Ok(inbox)
}
fn max_id(&self) -> Option<u64> {
self.threads
.iter()
.flat_map(|t| {
std::iter::once(t.id)
.chain(t.notes.iter().map(|n| n.id))
.chain(t.feed.iter().map(|i| i.seq))
.chain(t.events.iter().map(|e| e.seq))
})
.max()
}
}
struct UserOp {
state: Option<State>,
items: Vec<ItemKind>,
event: Event,
submitted: Option<(String, FormState)>,
}
pub fn rfc3339(at: u64) -> String {
let (days, secs) = ((at / 86_400) as i64, at % 86_400);
let z = days + 719_468;
let era = z.div_euclid(146_097);
let doe = z - era * 146_097;
let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
let mp = (5 * doy + 2) / 153;
let day = doy - (153 * mp + 2) / 5 + 1;
let month = if mp < 10 { mp + 3 } else { mp - 9 };
let year = yoe + era * 400 + i64::from(month <= 2);
format!(
"{year:04}-{month:02}-{day:02}T{:02}:{:02}:{:02}Z",
secs / 3600,
secs % 3600 / 60,
secs % 60
)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PutOutcome {
Unchanged,
Applied {
created: bool,
entered_needs_you: bool,
},
}
fn apply_put(thread: &mut Thread, put: ThreadPut) {
thread.title = put.title;
thread.link = put.link;
thread.state = Some(put.state);
thread.status = put.status;
thread.child = put.child;
thread.actions = put.actions;
thread.compose = put.compose;
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SinkAction {
Push(Record),
Put { at: u64, put: ThreadPut },
Rm { key: String },
Send { at: u64, send: MessageSend },
Withdraw { thread: String, id: String },
}
pub fn decide(instance: &str, declares_inbox: bool, message: Message) -> SinkAction {
let message = match message {
Message::Notify(record) => return SinkAction::Push(record),
thread_op => thread_op,
};
let verb = match &message {
Message::Notify(_) => unreachable!("returned above"),
Message::ThreadPut { .. } => "put",
Message::ThreadRm { .. } => "rm",
Message::ThreadSend { .. } => "send",
Message::ThreadWithdraw { .. } => "withdraw",
};
let (at, key) = (message.at(), message.key().unwrap_or_default().to_string());
if !declares_inbox {
let why = format!("`{instance}` doesn't declare inbox = true (add it to the sandbox in devsandboxes.toml)");
return refused(at, &key, &format!("thread {verb} denied: {why}"));
}
let rejected = |why: &str| refused(at, &key, &format!("thread {verb} rejected: {why}"));
match message {
Message::Notify(_) => unreachable!("returned above"),
Message::ThreadRm { key, .. } => SinkAction::Rm { key },
Message::ThreadWithdraw { key, id, .. } => SinkAction::Withdraw { thread: key, id },
Message::ThreadPut { body, .. } => match thread::parse(&body) {
Ok(put) if put.key != key => rejected(&format!("body key `{}` is not the queued key", put.key)),
Ok(put) => SinkAction::Put { at, put },
Err(why) => rejected(&why),
},
Message::ThreadSend { id, body, .. } => match message::parse(&body) {
Ok(send) if send.thread != key => {
rejected(&format!("body thread `{}` is not the queued thread", send.thread))
}
Ok(send) if send.id != id => rejected(&format!("body id `{}` is not the queued id", send.id)),
Ok(send) => SinkAction::Send { at, send },
Err(why) => rejected(&why),
},
}
}
fn sanitize_action(action: SinkAction) -> SinkAction {
use thread::HostVerb;
let opt = |s: Option<String>| s.map(|s| sanitize(&s));
match action {
SinkAction::Push(r) => SinkAction::Push(Record {
key: opt(r.key),
link: opt(r.link),
msg: sanitize(&r.msg),
..r
}),
SinkAction::Rm { key } => SinkAction::Rm { key: sanitize(&key) },
SinkAction::Withdraw { thread, id } => SinkAction::Withdraw { thread: sanitize(&thread), id: sanitize(&id) },
SinkAction::Send { at, send } => {
let blocks = send
.blocks
.into_iter()
.map(|b| match b {
Block::Markdown { text } => Block::Markdown { text: sanitize(&text) },
Block::Fields { items } => Block::Fields {
items: items
.into_iter()
.map(|f| Field { label: sanitize(&f.label), value: sanitize(&f.value) })
.collect(),
},
Block::Form(f) => Block::Form(form::sanitized(f)),
})
.collect();
let send = MessageSend { thread: sanitize(&send.thread), id: sanitize(&send.id), blocks };
SinkAction::Send { at, send }
}
SinkAction::Put { at, put } => {
let actions = put
.actions
.into_iter()
.map(|a| {
let host = a.host.map(|h| match h {
HostVerb::Vscode(v) => HostVerb::Vscode(thread::Vscode { path: opt(v.path), ..v }),
HostVerb::Open(o) => HostVerb::Open(thread::Open { url: sanitize(&o.url) }),
other => other,
});
Action { id: sanitize(&a.id), label: sanitize(&a.label), host, ..a }
})
.collect();
let put = ThreadPut {
key: sanitize(&put.key),
title: sanitize(&put.title),
link: opt(put.link),
status: opt(put.status),
child: opt(put.child),
actions,
compose: put.compose.map(|c| Compose { placeholder: opt(c.placeholder), hint: opt(c.hint) }),
..put
};
SinkAction::Put { at, put }
}
}
}
fn refused(at: u64, key: &str, msg: &str) -> SinkAction {
SinkAction::Push(Record {
level: Level::Error,
key: Some(format!("thread:{key}")),
link: None,
msg: msg.to_string(),
at,
})
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Shown {
pub line: String,
pub popup: Option<ShownPopup>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ShownPopup {
pub key: Option<String>,
pub level: Level,
pub body: String,
}
#[derive(Serialize, Deserialize)]
struct SavedInbox {
version: u32,
#[serde(default)]
next_id: u64,
#[serde(default)]
threads: Vec<SavedThread>,
}
#[derive(Serialize, Deserialize)]
struct SavedThread {
id: u64,
owner: String,
owner_name: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
key: Option<String>,
#[serde(default)]
kind: Kind,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
archived: bool,
#[serde(default)]
unread: bool,
#[serde(default, skip_serializing_if = "String::is_empty")]
title: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
link: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
state: Option<State>,
#[serde(default, skip_serializing_if = "Option::is_none")]
status: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
child: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
actions: Vec<Action>,
#[serde(default, skip_serializing_if = "Option::is_none")]
compose: Option<Compose>,
#[serde(default, skip_serializing_if = "is_zero")]
updated_at: u64,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
feed: Vec<FeedItem>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
events: Vec<Event>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
notes: Vec<SavedNote>,
}
fn is_zero(n: &u64) -> bool {
*n == 0
}
#[derive(Serialize, Deserialize)]
struct SavedNote {
seq: u64,
level: String,
at: u64,
#[serde(default, skip_serializing_if = "Option::is_none")]
link: Option<String>,
msg: String,
}
#[derive(Deserialize)]
struct V2Inbox {
#[serde(default)]
version: u32,
#[serde(default, rename = "thread")]
threads: Vec<V2Thread>,
}
#[derive(Deserialize)]
struct V2Thread {
#[serde(default)]
id: u64,
#[serde(default)]
owner: String,
#[serde(default)]
owner_name: String,
#[serde(default)]
key: Option<String>,
#[serde(default)]
instance: Option<String>,
#[serde(default)]
kind: Kind,
#[serde(default)]
archived: bool,
#[serde(default)]
unread: bool,
#[serde(default, rename = "note")]
notes: Vec<V2Note>,
}
#[derive(Deserialize)]
struct V2Note {
seq: u64,
level: String,
at: u64,
#[serde(default)]
key: Option<String>,
#[serde(default)]
link: Option<String>,
msg: String,
}
#[cfg(test)]
mod tests {
use super::*;
fn rec(msg: &str, key: Option<&str>, link: Option<&str>) -> Record {
Record {
level: Level::Info,
key: key.map(str::to_string),
link: link.map(str::to_string),
msg: msg.into(),
at: 0,
}
}
fn push(inbox: &mut Inbox, owner: &str, record: Record, unread: bool) {
inbox.push(format!("{owner}-id"), owner.into(), record, unread);
}
fn msgs(inbox: &Inbox) -> Vec<(&str, &str)> {
inbox
.threads
.iter()
.map(|t| (t.owner_name.as_str(), t.head().unwrap().msg.as_str()))
.collect()
}
fn names(pairs: &[(&str, &str)]) -> BTreeMap<String, String> {
pairs.iter().map(|(n, id)| (n.to_string(), id.to_string())).collect()
}
fn put_body(key: &str) -> ThreadPut {
ThreadPut {
key: key.into(),
title: "PR 1".into(),
state: State::Active,
status: Some("running ci".into()),
..ThreadPut::default()
}
}
fn feed(t: &Thread) -> Vec<String> {
t.feed
.iter()
.map(|i| match &i.kind {
ItemKind::Message { id, blocks, edited, withdrawn, .. } => {
let tags = match (edited, withdrawn) {
(_, true) => " (withdrawn)",
(true, false) => " (edited)",
_ => "",
};
format!("message {id}: {}{tags}", feed::markdown_of(blocks))
}
ItemKind::Reply { text, client } => format!("reply {text} [{client}]"),
ItemKind::Submission { message, form, answers, client } => {
format!("submission {message}/{form}: {} answers [{client}]", answers.len())
}
ItemKind::Action { action, label, client } => format!("action {action} {label} [{client}]"),
ItemKind::Marker(Marker::Done { client }) => format!("done [{client}]"),
ItemKind::Marker(Marker::Reopen { client }) => format!("reopen [{client}]"),
ItemKind::Marker(Marker::State { from, to }) => format!("state {} -> {}", from.as_str(), to.as_str()),
ItemKind::Marker(Marker::Status { from, to }) => {
format!("status {} -> {}", from.as_deref().unwrap_or("-"), to.as_deref().unwrap_or("-"))
}
})
.collect()
}
fn find<'a>(inbox: &'a Inbox, key: &str) -> Option<&'a Thread> {
inbox.threads.iter().find(|t| t.key.as_deref() == Some(key))
}
fn owned(inbox: &Inbox, owner: &str) -> usize {
inbox.threads.iter().filter(|t| t.owner == owner).count()
}
#[test]
fn put_creates_a_thread_with_an_empty_feed() {
let mut inbox = Inbox::default();
let out = inbox.put("web-id", "web", 100, put_body("pr-1"));
assert_eq!(out, PutOutcome::Applied { created: true, entered_needs_you: false });
let t = &inbox.threads[0];
assert_eq!((t.kind, t.unread, t.archived), (Kind::Thread, true, false));
assert!(t.notes.is_empty(), "an owner thread has no records");
assert_eq!(t.changed_at(), 100);
assert_eq!(t.state, Some(State::Active));
assert_eq!(t.status.as_deref(), Some("running ci"));
assert!(t.feed.is_empty(), "the header says it all: no markers on creation");
}
#[test]
fn an_identical_put_changes_nothing() {
let mut inbox = Inbox::default();
let full = ThreadPut { compose: Some(Compose::default()), ..put_body("pr-1") };
inbox.put("web-id", "web", 100, full.clone());
let before = inbox.clone();
assert_eq!(inbox.put("web-id", "web", 200, full), PutOutcome::Unchanged);
assert_eq!(inbox, before);
}
#[test]
fn a_put_leaves_markers_for_state_and_status_only() {
let mut inbox = Inbox::default();
inbox.put("web-id", "web", 100, put_body("pr-1"));
inbox.put("web-id", "web", 100, put_body("other"));
inbox.threads.iter_mut().for_each(|t| t.unread = false);
let changed = ThreadPut { state: State::NeedsYou, status: Some("review".into()), ..put_body("pr-1") };
let out = inbox.put("web-id", "web", 300, changed.clone());
assert_eq!(out, PutOutcome::Applied { created: false, entered_needs_you: true });
let t = &inbox.threads[0];
assert_eq!(t.key.as_deref(), Some("pr-1"), "a changed thread moves to the top");
assert!(t.unread);
assert_eq!(t.updated_at, 300);
assert_eq!(feed(t), ["state active -> needs-you", "status running ci -> review"]);
inbox.threads[0].unread = false;
let cosmetic = ThreadPut {
title: "PR 1 (renamed)".into(),
link: Some("https://x/1".into()),
actions: vec![Action { id: "go".into(), label: "Go".into(), ..Action::default() }],
compose: Some(Compose { placeholder: Some("p".into()), hint: Some("h".into()) }),
..changed.clone()
};
assert!(matches!(inbox.put("web-id", "web", 400, cosmetic), PutOutcome::Applied { .. }));
let t = &inbox.threads[0];
assert_eq!((t.title.as_str(), t.compose.as_ref().unwrap().hint.as_deref()), ("PR 1 (renamed)", Some("h")));
assert!(!t.unread, "a new title is not news");
assert_eq!(t.feed.len(), 2, "and leaves no marker");
for status in ["posting", "posted"] {
inbox.put("web-id", "web", 500, ThreadPut { status: Some(status.into()), ..changed.clone() });
}
let t = find(&inbox, "pr-1").unwrap();
assert_eq!(feed(t), ["state active -> needs-you", "status running ci -> posted"]);
assert_eq!(t.feed[1].at, 500);
inbox.put("web-id", "web", 600, ThreadPut { status: Some("running ci".into()), ..changed });
assert_eq!(feed(find(&inbox, "pr-1").unwrap()), ["state active -> needs-you"]);
}
#[test]
fn puts_leave_an_old_header_message_alone() {
let mut inbox = Inbox::default();
inbox.put("web-id", "web", 1, put_body("pr-1"));
inbox.threads[0].feed.push(FeedItem::markdown(50, 1, "header-message", "ci started"));
assert_eq!(inbox.put("web-id", "web", 2, put_body("pr-1")), PutOutcome::Unchanged);
inbox.put("web-id", "web", 3, ThreadPut { status: Some("merged".into()), ..put_body("pr-1") });
let t = &inbox.threads[0];
assert_eq!(feed(t), ["message header-message: ci started", "status running ci -> merged"]);
}
#[test]
fn entering_needs_you_is_reported_once() {
let mut inbox = Inbox::default();
let needs = ThreadPut { state: State::NeedsYou, ..put_body("pr-1") };
let out = inbox.put("web-id", "web", 10, needs.clone());
assert_eq!(out, PutOutcome::Applied { created: true, entered_needs_you: true });
let again = ThreadPut { status: Some("still".into()), ..needs.clone() };
assert_eq!(
inbox.put("web-id", "web", 20, again),
PutOutcome::Applied { created: false, entered_needs_you: false }
);
let active = ThreadPut { state: State::Active, status: Some("still".into()), ..needs.clone() };
inbox.put("web-id", "web", 30, active);
let back = ThreadPut { status: Some("still".into()), ..needs };
assert_eq!(
inbox.put("web-id", "web", 40, back),
PutOutcome::Applied { created: false, entered_needs_you: true }
);
}
#[test]
fn keys_are_per_owner_and_per_kind() {
let mut inbox = Inbox::default();
push(&mut inbox, "web", rec("note", Some("pr-1"), None), true);
inbox.put("web-id", "web", 10, put_body("pr-1"));
inbox.put("other-id", "other", 10, put_body("pr-1"));
assert_eq!(inbox.threads.len(), 3, "a notify record never joins an owner thread");
assert!(inbox.thread_rm("web-id", "pr-1"));
assert!(!inbox.thread_rm("web-id", "pr-1"), "already gone");
assert_eq!(inbox.threads.len(), 2);
assert!(find(&inbox, "pr-1").is_some_and(|t| t.kind == Kind::Notify || t.owner == "other-id"));
}
#[test]
fn archived_and_done_threads_age_out() {
let mut inbox = Inbox::default();
let day = 86_400;
inbox.put("web-id", "web", day, ThreadPut { state: State::Done, ..put_body("old-done") });
inbox.put("web-id", "web", day, put_body("old-active"));
inbox.put("gone-id", "gone", day, put_body("orphan"));
push(&mut inbox, "gone", Record { at: day, ..rec("note", None, None) }, true);
assert_eq!(inbox.archive_owner("gone-id"), 2, "threads and records alike");
assert_eq!(inbox.archive_owner("gone-id"), 0, "idempotent");
assert!(inbox.threads.iter().filter(|t| t.owner == "gone-id").all(|t| t.archived));
assert_eq!(inbox.prune(day + RETENTION), 0);
assert_eq!(inbox.prune(day + RETENTION + 1), 3);
assert_eq!(inbox.threads.len(), 1);
assert_eq!(inbox.threads[0].key.as_deref(), Some("old-active"));
inbox.put("web-id", "web", day, ThreadPut { state: State::Done, ..put_body("unacked") });
let event = Event { id: "e-0000086400-abcd".into(), seq: 0, kind: EventKind::Done, action: None, text: None, message: None, form: None, answers: None, at: day };
inbox.threads.iter_mut().find(|t| t.key.as_deref() == Some("unacked")).unwrap().events.push(event);
assert_eq!(inbox.prune(day + RETENTION + 1), 0);
inbox.put("web-id", "web", day, put_body("revived"));
inbox.archive_owner("web-id");
inbox.put("web-id", "web", day, put_body("revived"));
assert!(!find(&inbox, "revived").unwrap().archived);
}
#[test]
fn the_thread_cap_evicts_archived_then_done_then_the_oldest() {
let mut inbox = Inbox::default();
let bare = |key: &str, state: State| ThreadPut { key: key.into(), title: "t".into(), state, ..ThreadPut::default() };
inbox.put("w-id", "w", 1, bare("archived", State::Active));
inbox.archive_owner("w-id");
inbox.put("w-id", "w", 2, bare("done", State::Done));
inbox.put("w-id", "w", 3, ThreadPut { compose: Some(Compose::default()), ..bare("pending", State::Active) });
let pending = find(&inbox, "pending").unwrap().id;
inbox.apply(&Op::Reply { thread: pending, text: "hi".into() }, 3, "tui");
inbox.put("w-id", "w", 4, bare("live", State::Active));
inbox.put("x-id", "x", 0, bare("theirs", State::Done));
let mut next = 0;
let mut fill = |inbox: &mut Inbox, n: usize| {
for _ in 0..n {
push(inbox, "w", Record { at: 10 + next, ..rec(&format!("n{next}"), None, None) }, true);
next += 1;
}
};
fill(&mut inbox, THREADS_PER_OWNER - 4);
assert_eq!(owned(&inbox, "w-id"), THREADS_PER_OWNER);
assert!(find(&inbox, "archived").is_some(), "within the cap: nothing goes");
fill(&mut inbox, 1);
assert!(find(&inbox, "archived").is_none(), "archived goes first");
fill(&mut inbox, 1);
assert!(find(&inbox, "done").is_none(), "then done");
fill(&mut inbox, 1);
assert!(find(&inbox, "live").is_none(), "then the oldest without events");
assert!(find(&inbox, "pending").is_some(), "the one holding an event outlasts it");
fill(&mut inbox, 1);
assert!(!inbox.threads.iter().filter_map(|t| t.head()).any(|r| r.msg == "n0"), "then the oldest record");
assert_eq!(owned(&inbox, "w-id"), THREADS_PER_OWNER);
assert!(find(&inbox, "theirs").is_some());
fill(&mut inbox, THREADS_PER_OWNER);
assert!(find(&inbox, "pending").is_some(), "a thread holding events is never evicted");
}
#[test]
fn a_feed_keeps_its_newest_300_and_a_notify_thread_its_newest_records() {
let mut inbox = Inbox::default();
let put = ThreadPut { compose: Some(Compose::default()), ..put_body("pr-1") };
inbox.put("web-id", "web", 1, put.clone());
inbox.put("web-id", "web", 2, ThreadPut { state: State::NeedsYou, ..put });
let id = find(&inbox, "pr-1").unwrap().id;
for i in 0..MAX_FEED {
inbox.apply(&Op::Reply { thread: id, text: format!("r{i}") }, 10, "tui");
}
let t = find(&inbox, "pr-1").unwrap();
assert_eq!(t.feed.len(), MAX_FEED);
assert_eq!(feed(t)[0], "reply r0 [tui]", "the state marker went first");
inbox.apply(&Op::Reply { thread: id, text: "last".into() }, 10, "tui");
let t = find(&inbox, "pr-1").unwrap();
assert_eq!((t.feed.len(), feed(t)[0].as_str()), (MAX_FEED, "reply r1 [tui]"));
for i in 0..MAX_NOTES + 2 {
push(&mut inbox, "web", rec(&format!("n{i}"), Some("k"), None), true);
}
let n = find(&inbox, "k").unwrap();
assert_eq!(n.notes.len(), MAX_NOTES);
assert_eq!(n.notes.last().unwrap().record.msg, "n2", "the oldest two went");
}
#[test]
fn the_sink_denies_rejects_and_applies() {
let body = r#"{"key":"pr-1","title":"t","state":"active"}"#;
let put = Message::ThreadPut { at: 7, key: "pr-1".into(), body: body.into() };
let SinkAction::Push(r) = decide("web", false, put.clone()) else { panic!("not refused") };
assert_eq!((r.level, r.key.as_deref(), r.at), (Level::Error, Some("thread:pr-1"), 7));
assert_eq!(
r.msg,
"thread put denied: `web` doesn't declare inbox = true (add it to the sandbox in devsandboxes.toml)"
);
assert!(matches!(decide("web", true, put), SinkAction::Put { at: 7, .. }));
let bad = Message::ThreadPut { at: 8, key: "pr-1".into(), body: r#"{"key":"pr-1"}"#.into() };
let SinkAction::Push(r) = decide("web", true, bad) else { panic!("not rejected") };
assert!(r.msg.starts_with("thread put rejected:"), "{}", r.msg);
let swapped = r#"{"key":"pr-2","title":"t","state":"active"}"#;
let mismatch = Message::ThreadPut { at: 8, key: "pr-1".into(), body: swapped.into() };
let SinkAction::Push(r) = decide("web", true, mismatch) else { panic!("not rejected") };
assert!(r.msg.contains("is not the queued key"), "{}", r.msg);
for (field, why) in [(r#""message":"m""#, "`message` was removed"), (r#""reply":{}"#, "`reply` was renamed `compose`")] {
let body = format!(r#"{{"key":"pr-1","title":"t","state":"active",{field}}}"#);
let old = Message::ThreadPut { at: 8, key: "pr-1".into(), body };
let SinkAction::Push(r) = decide("web", true, old) else { panic!("not rejected") };
assert_eq!(r.level, Level::Error);
assert!(r.msg.starts_with(&format!("thread put rejected: {why}")), "{}", r.msg);
}
let rm = Message::ThreadRm { at: 9, key: "pr-1".into() };
assert_eq!(decide("web", true, rm.clone()), SinkAction::Rm { key: "pr-1".into() });
let SinkAction::Push(r) = decide("web", false, rm) else { panic!("not refused") };
assert!(r.msg.contains("thread rm denied"), "{}", r.msg);
let note = rec("hi", None, None);
assert_eq!(decide("web", false, Message::Notify(note.clone())), SinkAction::Push(note));
}
#[test]
fn the_sink_surfaces_only_what_is_new() {
let mut inbox = Inbox::default();
let action = |state: &str, status: &str, at: u64| {
let body = format!(r#"{{"key":"pr-1","title":"PR 1","state":"{state}","status":"{status}"}}"#);
decide("web", true, Message::ThreadPut { at, key: "pr-1".into(), body })
};
let shown = inbox.apply_sink("web-id", "web", 1000, action("active", "ci", 10)).unwrap();
assert_eq!(shown.line, "web: PR 1 — ci");
assert!(shown.popup.is_none());
assert!(inbox.apply_sink("web-id", "web", 1000, action("active", "ci", 11)).is_none());
let shown = inbox.apply_sink("web-id", "web", 1000, action("needs-you", "ci", 12)).unwrap();
let popup = shown.popup.unwrap();
assert_eq!((popup.key.as_deref(), popup.level), (Some("thread:pr-1"), Level::Warn));
assert_eq!(popup.body, "PR 1");
let shown = inbox.apply_sink("web-id", "web", 1000, action("needs-you", "merged", 13)).unwrap();
assert!(shown.popup.is_none());
let note = decide("web", false, Message::Notify(rec("hi", None, Some("https://x"))));
let shown = inbox.apply_sink("web-id", "web", 1000, note).unwrap();
assert_eq!(shown.line, "web: hi");
assert_eq!(shown.popup.unwrap().body, "hi\nhttps://x");
let rm = SinkAction::Rm { key: "pr-1".into() };
assert!(inbox.apply_sink("web-id", "web", 1000, rm).is_none());
assert!(find(&inbox, "pr-1").is_none());
}
#[test]
fn the_sink_strips_control_characters() {
let mut inbox = Inbox::default();
let note = rec("\x1b]0;pwned\x07hi\u{9b}2J\u{202E}", Some("k\x1b"), Some("https://x\x1b[0m"));
let shown = inbox.apply_sink("web-id", "web", 1000, SinkAction::Push(note)).unwrap();
assert_eq!(shown.line, "web: ]0;pwnedhi2J");
let popup = shown.popup.unwrap();
assert_eq!((popup.key.as_deref(), popup.body.as_str()), (Some("k"), "]0;pwnedhi2J\nhttps://x[0m"));
let r = inbox.threads[0].head().unwrap();
assert_eq!((r.msg.as_str(), r.link.as_deref()), ("]0;pwnedhi2J", Some("https://x[0m")));
let put = ThreadPut {
title: "t\x1b[1m".into(),
status: Some("s\x07".into()),
actions: vec![Action { id: "go".into(), label: "Go\x1b".into(), ..Action::default() }],
compose: Some(Compose { placeholder: Some("p\x1b".into()), hint: Some("h\x07".into()) }),
..put_body("pr-1")
};
inbox.apply_sink("web-id", "web", 1000, SinkAction::Put { at: 5, put });
let t = find(&inbox, "pr-1").unwrap();
assert_eq!(t.title, "t[1m");
assert_eq!(t.status.as_deref(), Some("s"));
assert_eq!(t.actions[0].label, "Go");
let compose = t.compose.as_ref().unwrap();
assert_eq!((compose.placeholder.as_deref(), compose.hint.as_deref()), (Some("p"), Some("h")));
}
fn send_body(thread: &str, id: &str, text: &str) -> String {
format!(r#"{{"thread":"{thread}","id":"{id}","blocks":[{{"type":"markdown","text":"{text}"}}]}}"#)
}
fn send_msg(at: u64, thread: &str, id: &str, text: &str) -> Message {
Message::ThreadSend { at, key: thread.into(), id: id.into(), body: send_body(thread, id, text) }
}
#[test]
fn the_sink_authorizes_and_checks_sends_and_withdraws() {
let send = send_msg(7, "pr-1", "run-1", "hi");
let SinkAction::Push(r) = decide("web", false, send.clone()) else { panic!("not refused") };
assert_eq!((r.level, r.key.as_deref(), r.at), (Level::Error, Some("thread:pr-1"), 7));
assert!(r.msg.starts_with("thread send denied: `web` doesn't declare inbox = true"), "{}", r.msg);
let SinkAction::Send { at: 7, send: s } = decide("web", true, send) else { panic!("not applied") };
assert_eq!((s.thread.as_str(), s.id.as_str()), ("pr-1", "run-1"));
assert_eq!(s.blocks, [Block::Markdown { text: "hi".into() }]);
let reject = |m: Message| match decide("web", true, m) {
SinkAction::Push(r) => r.msg,
other => panic!("not rejected: {other:?}"),
};
let unknown = r#"{"thread":"pr-1","id":"a","blocks":[{"type":"poll","id":"f"}]}"#;
let msg = reject(Message::ThreadSend { at: 8, key: "pr-1".into(), id: "a".into(), body: unknown.into() });
assert!(msg.starts_with("thread send rejected: unknown variant `poll`"), "{msg}");
let msg = reject(Message::ThreadSend { at: 8, key: "pr-2".into(), id: "a".into(), body: send_body("pr-1", "a", "x") });
assert!(msg.contains("body thread `pr-1` is not the queued thread"), "{msg}");
let msg = reject(Message::ThreadSend { at: 8, key: "pr-1".into(), id: "b".into(), body: send_body("pr-1", "a", "x") });
assert!(msg.contains("body id `a` is not the queued id"), "{msg}");
let w = Message::ThreadWithdraw { at: 9, key: "pr-1".into(), id: "a".into() };
assert_eq!(decide("web", true, w.clone()), SinkAction::Withdraw { thread: "pr-1".into(), id: "a".into() });
let SinkAction::Push(r) = decide("web", false, w) else { panic!("not refused") };
assert!(r.msg.starts_with("thread withdraw denied:"), "{}", r.msg);
}
#[test]
fn sends_insert_edit_and_withdraw_through_the_sink() {
let mut inbox = Inbox::default();
let sink = |inbox: &mut Inbox, m: Message| inbox.apply_sink("web-id", "web", 1000, decide("web", true, m));
let shown = sink(&mut inbox, send_msg(5, "pr-1", "a", "early")).unwrap();
assert_eq!(shown.line, "web: thread send rejected: no thread `pr-1`; put it first");
assert_eq!(shown.popup.unwrap().level, Level::Error);
assert_eq!(inbox.threads.len(), 1);
assert_eq!(inbox.threads[0].kind, Kind::Notify);
let put = r#"{"key":"pr-1","title":"PR 1","state":"active"}"#;
sink(&mut inbox, Message::ThreadPut { at: 6, key: "pr-1".into(), body: put.into() });
let id = find(&inbox, "pr-1").unwrap().id;
inbox.apply(&Op::MarkRead(id), 1000, "tui");
let shown = sink(&mut inbox, send_msg(10, "pr-1", "a", "one")).unwrap();
assert_eq!(shown.line, "web: PR 1 — one");
assert!(shown.popup.is_none());
let t = find(&inbox, "pr-1").unwrap();
assert!(t.unread);
assert_eq!(inbox.threads[0].id, id, "moved to the top");
assert_eq!(feed(t), ["message a: one"]);
sink(&mut inbox, send_msg(11, "pr-1", "b", "two"));
inbox.apply(&Op::MarkRead(id), 1000, "tui");
assert!(sink(&mut inbox, send_msg(12, "pr-1", "a", "one")).is_none());
assert!(sink(&mut inbox, send_msg(13, "pr-1", "a", "uno")).is_none());
let t = find(&inbox, "pr-1").unwrap();
assert!(!t.unread, "an edit isn't news");
assert_eq!(feed(t), ["message a: uno (edited)", "message b: two"]);
assert_eq!(t.feed[0].at, 10, "first-insert time kept");
assert!(sink(&mut inbox, Message::ThreadWithdraw { at: 14, key: "pr-1".into(), id: "b".into() }).is_none());
assert!(sink(&mut inbox, Message::ThreadWithdraw { at: 14, key: "pr-1".into(), id: "zz".into() }).is_none());
assert!(sink(&mut inbox, Message::ThreadWithdraw { at: 14, key: "nope".into(), id: "b".into() }).is_none());
let t = find(&inbox, "pr-1").unwrap();
assert_eq!(feed(t), ["message a: uno (edited)", "message b: two (withdrawn)"]);
assert_eq!(inbox.threads.len(), 2, "no error record for an unknown withdraw");
assert!(sink(&mut inbox, send_msg(15, "pr-1", "b", "two")).is_none());
assert_eq!(feed(find(&inbox, "pr-1").unwrap()), ["message a: uno (edited)", "message b: two (edited)"]);
let put = r#"{"key":"pr-1","title":"PR 1","state":"needs-you"}"#;
sink(&mut inbox, Message::ThreadPut { at: 16, key: "pr-1".into(), body: put.into() });
let popup = sink(&mut inbox, send_msg(17, "pr-1", "c", "your call")).unwrap().popup.unwrap();
assert_eq!((popup.key.as_deref(), popup.level, popup.body.as_str()), (Some("thread:pr-1"), Level::Warn, "PR 1\nyour call"));
assert!(sink(&mut inbox, send_msg(18, "pr-1", "c", "your call, edited")).is_none());
let shown = inbox.apply_sink("other-id", "other", 1000, decide("other", true, send_msg(19, "pr-1", "x", "hi"))).unwrap();
assert!(shown.line.contains("no thread `pr-1`"), "{}", shown.line);
}
#[test]
fn the_sink_strips_control_characters_from_messages() {
let mut inbox = Inbox::default();
inbox.apply_sink("web-id", "web", 1000, SinkAction::Put { at: 5, put: put_body("pr-1") });
let send = MessageSend {
thread: "pr-1".into(),
id: "a".into(),
blocks: vec![
Block::Markdown { text: "a\x1b[1mb".into() },
Block::Fields { items: vec![Field { label: "L\x07".into(), value: "v\u{9b}2J".into() }] },
],
};
inbox.apply_sink("web-id", "web", 1000, SinkAction::Send { at: 6, send });
let ItemKind::Message { blocks, .. } = &find(&inbox, "pr-1").unwrap().feed.last().unwrap().kind else { panic!() };
assert_eq!(
blocks,
&[
Block::Markdown { text: "a[1mb".into() },
Block::Fields { items: vec![Field { label: "L".into(), value: "v2J".into() }] },
]
);
}
fn send_form(at: u64, thread: &str, id: &str, text: &str, form: &str) -> Message {
let body = format!(r#"{{"thread":"{thread}","id":"{id}","blocks":[{{"type":"markdown","text":"{text}"}},{form}]}}"#);
Message::ThreadSend { at, key: thread.into(), id: id.into(), body }
}
fn form_state(inbox: &Inbox, key: &str, message: &str) -> Option<FormRecord> {
let t = find(inbox, key).unwrap();
feed::form_of(&t.feed, message).map(|(_, r)| r.clone()).or_else(|| {
t.feed.iter().find_map(|i| match &i.kind {
ItemKind::Message { id, form, .. } if id == message => form.clone(),
_ => None,
})
})
}
#[test]
fn forms_through_the_sink() {
let mut inbox = Inbox::default();
let sink = |inbox: &mut Inbox, m: Message| inbox.apply_sink("web-id", "web", 1000, decide("web", true, m));
let put = r#"{"key":"pr-1","title":"PR 1","state":"needs-you"}"#;
sink(&mut inbox, Message::ThreadPut { at: 1, key: "pr-1".into(), body: put.into() });
let id = find(&inbox, "pr-1").unwrap().id;
sink(&mut inbox, send_msg(2, "pr-1", "run-1", "no form yet"));
inbox.apply(&Op::MarkRead(id), 1000, "tui");
let confirm = r#"{"type":"form","id":"f","questions":[{"id":"ok","label":"Merge?","type":"confirm"},{"id":"why","label":"Why","type":"text"}]}"#;
let shown = sink(&mut inbox, send_form(3, "pr-1", "run-1", "no form yet", confirm)).unwrap();
assert_eq!(shown.popup.unwrap().body, "PR 1\nno form yet");
assert!(find(&inbox, "pr-1").unwrap().unread);
assert_eq!(form_state(&inbox, "pr-1", "run-1"), Some(FormRecord::open("f")));
inbox.apply(&Op::MarkRead(id), 1000, "tui");
assert!(sink(&mut inbox, send_form(4, "pr-1", "run-1", "no form yet", confirm)).is_none());
let draft = [("ok".to_string(), Answer::Confirm(true)), ("why".to_string(), Answer::Text("because".into()))];
inbox.apply(&Op::SaveDraft { thread: id, message: "run-1".into(), answers: draft.into() }, 1000, "tui");
let changed = r#"{"type":"form","id":"f","questions":[{"id":"ok","label":"Merge now?","type":"confirm"}]}"#;
assert!(sink(&mut inbox, send_form(5, "pr-1", "run-1", "no form yet", changed)).is_none(), "a quiet edit");
let r = form_state(&inbox, "pr-1", "run-1").unwrap();
assert_eq!(r.draft, [("ok".to_string(), Answer::Confirm(true))].into());
assert!(!find(&inbox, "pr-1").unwrap().unread);
sink(&mut inbox, Message::ThreadWithdraw { at: 6, key: "pr-1".into(), id: "run-1".into() });
assert_eq!(form_state(&inbox, "pr-1", "run-1").unwrap().state, FormState::Withdrawn);
let put = r#"{"key":"pr-1","title":"PR 1","state":"active"}"#;
sink(&mut inbox, Message::ThreadPut { at: 7, key: "pr-1".into(), body: put.into() });
let shown = sink(&mut inbox, send_form(8, "pr-1", "run-2", "again", confirm)).unwrap();
assert!(shown.popup.is_none());
let dirty = r#"{"type":"form","id":"g","title":"T\tx","questions":[{"id":"q","label":"L\tx","context":"a\r\nb","type":"confirm","yes":"Y\tx"}]}"#;
sink(&mut inbox, send_form(9, "pr-1", "run-3", "x", dirty));
let t = find(&inbox, "pr-1").unwrap();
let (f, _) = feed::form_of(&t.feed, "run-3").unwrap();
assert_eq!(f.title.as_deref(), Some("T x"));
assert_eq!((f.questions[0].label.as_str(), f.questions[0].context.as_deref()), ("L x", Some("a\nb")));
assert!(matches!(&f.questions[0].kind, form::QuestionKind::Confirm { yes: Some(y), .. } if y == "Y x"));
let escape = r#"{"type":"form","id":"h","title":"T\u001b[1m","questions":[{"id":"q","label":"L","type":"confirm"}]}"#;
assert!(sink(&mut inbox, send_form(10, "pr-1", "run-4", "x", escape)).unwrap().line.contains("control character"));
}
fn with_example_form(inbox: &mut Inbox) -> u64 {
inbox.apply_sink("web-id", "web", 1000, SinkAction::Put { at: 1, put: put_body("pr-1") });
let body = format!(r#"{{"thread":"pr-1","id":"run-1","blocks":[{}]}}"#, form::tests::EXAMPLE);
let SinkAction::Send { at, send } = decide("web", true, Message::ThreadSend { at: 2, key: "pr-1".into(), id: "run-1".into(), body }) else {
panic!("refused")
};
inbox.apply_sink("web-id", "web", 1000, SinkAction::Send { at, send });
find(inbox, "pr-1").unwrap().id
}
#[test]
fn drafts_merge_quietly_and_bad_ones_are_dropped() {
let mut inbox = Inbox::default();
let id = with_example_form(&mut inbox);
let draft = |inbox: &mut Inbox, pairs: &[(&str, Answer)]| {
let answers = pairs.iter().map(|(k, v)| (k.to_string(), v.clone())).collect();
inbox.apply(&Op::SaveDraft { thread: id, message: "run-1".into(), answers }, 50, "tui");
};
draft(&mut inbox, &[("notes", Answer::Text("a".into()))]);
draft(&mut inbox, &[("c-3726888733", Answer::Choice("skip".into()))]);
draft(&mut inbox, &[("notes", Answer::Text("b\x1b".into()))]);
let r = form_state(&inbox, "pr-1", "run-1").unwrap();
assert_eq!(
r.draft,
[("c-3726888733".to_string(), Answer::Choice("skip".into())), ("notes".to_string(), Answer::Text("b".into()))].into()
);
draft(&mut inbox, &[("notes", Answer::Text("c".into())), ("c-3726888733", Answer::Choice("nope".into()))]);
draft(&mut inbox, &[("unknown", Answer::Text("x".into()))]);
assert_eq!(form_state(&inbox, "pr-1", "run-1").unwrap(), r);
let t = find(&inbox, "pr-1").unwrap();
assert!(t.events.is_empty());
assert_eq!(t.feed.len(), 1);
}
#[test]
fn a_submission_freezes_the_form_and_enqueues_one_event() {
let mut inbox = Inbox::default();
let id = with_example_form(&mut inbox);
let draft = [("notes".to_string(), Answer::Text("from the draft".into()))].into();
inbox.apply(&Op::SaveDraft { thread: id, message: "run-1".into(), answers: draft }, 50, "tui");
let given = [("c-3726888733".to_string(), Answer::Choice("nope".into()))].into();
inbox.apply(&Op::Submit { thread: id, message: "run-1".into(), answers: given }, 60, "tui");
assert!(find(&inbox, "pr-1").unwrap().events.is_empty());
let given = [("c-3726888733".to_string(), Answer::Choice("skip".into()))].into();
inbox.apply(&Op::Submit { thread: id, message: "run-1".into(), answers: given }, 70, "api:x");
let t = find(&inbox, "pr-1").unwrap();
let answers = serde_json::json!({"c-3726888733": "skip", "c-3726888733-text": null, "notes": "from the draft"});
let [e] = &t.events[..] else { panic!("{:?}", t.events) };
assert_eq!(e.kind, EventKind::Submit);
assert_eq!((e.message.as_deref(), e.form.as_deref(), e.answers.as_ref(), e.at), (Some("run-1"), Some("drafts"), Some(&answers), 70));
assert_eq!(feed(t).last().unwrap(), "submission run-1/drafts: 2 answers [api:x]");
let r = form_state(&inbox, "pr-1", "run-1").unwrap();
let FormState::Submitted { at: 70, client, answers: stored } = &r.state else { panic!("{r:?}") };
assert_eq!(client, "api:x");
assert_eq!(form::answers_json(&form::tests::example(), stored), answers);
assert!(r.draft.is_empty());
inbox.apply(&Op::Submit { thread: id, message: "run-1".into(), answers: BTreeMap::new() }, 80, "tui");
let notes = [("notes".to_string(), Answer::Text("late".into()))].into();
inbox.apply(&Op::SaveDraft { thread: id, message: "run-1".into(), answers: notes }, 80, "tui");
let t = find(&inbox, "pr-1").unwrap();
assert_eq!(t.events.len(), 1);
assert_eq!(form_state(&inbox, "pr-1", "run-1").unwrap(), r);
inbox.apply(&Op::Submit { thread: id, message: "nope".into(), answers: BTreeMap::new() }, 80, "tui");
assert_eq!(find(&inbox, "pr-1").unwrap().events.len(), 1);
let json = serde_json::to_string(&find(&inbox, "pr-1").unwrap().events).unwrap();
assert!(!json.contains("api:x"), "{json}");
}
#[test]
fn push_is_newest_first() {
let mut inbox = Inbox::default();
push(&mut inbox, "a", rec("one", None, None), true);
push(&mut inbox, "b", rec("two", None, None), true);
assert_eq!(msgs(&inbox), [("b", "two"), ("a", "one")]);
}
#[test]
fn same_key_threads_per_owner() {
let mut inbox = Inbox::default();
push(&mut inbox, "a", rec("pr 1 v1", Some("pr-1"), None), false);
push(&mut inbox, "a", rec("other", None, None), false);
push(&mut inbox, "b", rec("b pr 1", Some("pr-1"), None), false);
push(&mut inbox, "a", rec("pr 1 v2", Some("pr-1"), None), true);
assert_eq!(msgs(&inbox), [("a", "pr 1 v2"), ("b", "b pr 1"), ("a", "other")]);
let t = &inbox.threads[0];
assert_eq!(t.notes.iter().map(|n| n.record.msg.as_str()).collect::<Vec<_>>(), ["pr 1 v2", "pr 1 v1"]);
assert!(t.unread);
assert_eq!(inbox.threads.iter().filter(|t| t.unread).count(), 1);
}
#[test]
fn a_renamed_owner_keeps_its_threads() {
let mut inbox = Inbox::default();
inbox.push("id-1".into(), "web".into(), rec("v1", Some("k"), None), true);
inbox.push("id-1".into(), "web-2".into(), rec("v2", Some("k"), None), true);
assert_eq!(inbox.threads.len(), 1);
assert_eq!(inbox.threads[0].owner_name, "web-2");
assert_eq!(inbox.needs_you_for("id-1"), 1);
assert_eq!(inbox.needs_you_for("web"), 0);
}
#[test]
fn ops_remove_by_identity() {
let mut inbox = Inbox::default();
push(&mut inbox, "a", rec("v1", Some("k"), None), true);
push(&mut inbox, "a", rec("v2", Some("k"), None), true);
push(&mut inbox, "a", rec("solo", None, None), true);
push(&mut inbox, "b", rec("b1", None, None), true);
let thread_id = inbox.threads.iter().find(|t| t.key.is_some()).unwrap().id;
inbox.apply(&Op::RemoveThread(thread_id), 0, "tui");
assert_eq!(msgs(&inbox), [("b", "b1"), ("a", "solo")]);
inbox.apply(&Op::RemoveThread(thread_id), 0, "tui");
inbox.apply(&Op::MarkRead(thread_id), 0, "tui");
assert_eq!(msgs(&inbox), [("b", "b1"), ("a", "solo")]);
inbox.put("a-id", "a", 10, put_body("pr-1"));
inbox.apply(&Op::MarkNotifyRead, 0, "tui");
let unread: Vec<Kind> = inbox.threads.iter().filter(|t| t.unread).map(|t| t.kind).collect();
assert_eq!(unread, [Kind::Thread]);
inbox.apply(&Op::ClearNotify, 0, "tui");
assert_eq!(inbox.threads.len(), 1, "the owner thread stays");
}
#[test]
fn needs_you_counts_and_targeted_ops() {
let mut inbox = Inbox::default();
push(&mut inbox, "a", rec("unread note", None, None), true);
push(&mut inbox, "a", rec("read note", Some("k"), None), false);
inbox.put("a-id", "a", 10, ThreadPut { state: State::NeedsYou, ..put_body("asks") });
inbox.put("a-id", "a", 10, put_body("busy"));
inbox.put("b-id", "b", 10, ThreadPut { state: State::Done, ..put_body("over") });
assert_eq!(inbox.needs_you(), 2);
assert_eq!(inbox.needs_you_for("a-id"), 2);
assert_eq!(inbox.needs_you_for("b-id"), 0);
let note = inbox.threads.iter().find(|t| t.key.is_none()).unwrap().id;
inbox.apply(&Op::MarkRead(note), 0, "tui");
inbox.apply(&Op::MarkRead(u64::MAX), 0, "tui");
assert_eq!(inbox.needs_you(), 1);
let asks = find(&inbox, "asks").unwrap().id;
inbox.apply(&Op::MarkRead(asks), 0, "tui");
assert_eq!(inbox.needs_you(), 1);
inbox.archive_owner("a-id");
assert_eq!(inbox.needs_you(), 0);
inbox.apply(&Op::ClearNotify, 0, "tui");
assert_eq!(inbox.threads.len(), 3);
assert!(inbox.threads.iter().all(|t| t.kind == Kind::Thread));
}
#[test]
fn json_roundtrip() {
let mut inbox = Inbox::default();
push(&mut inbox, "a", rec("v1", Some("k"), Some("https://x/1")), true);
push(&mut inbox, "b", rec("multi\nline \"q\"", None, None), true);
push(&mut inbox, "a", Record { level: Level::Warn, at: 7, ..rec("v2", Some("k"), None) }, true);
push(&mut inbox, "b", rec("read", None, None), false);
let text = inbox.to_json().unwrap();
assert!(text.contains("\"version\": 3"), "{text}");
let loaded = Inbox::from_json(&text).unwrap();
assert_eq!(loaded, inbox);
assert_eq!(loaded.to_json().unwrap(), text, "byte for byte, so an unchanged store isn't rewritten");
let mut loaded = loaded;
let max_saved = inbox.threads.iter().flat_map(|t| &t.notes).map(|n| n.id).max().unwrap();
push(&mut loaded, "a", rec("later", None, None), true);
assert!(loaded.threads[0].notes[0].id > max_saved);
let v4 = text.replace("\"version\": 3", "\"version\": 4");
assert!(Inbox::from_json(&v4).unwrap_err().contains("version 4"));
assert!(Inbox::from_json("{}").is_err());
assert!(Inbox::from_json("").is_err());
let bad_level = r#"{"version":3,"threads":[{"id":1,"owner":"a","owner_name":"a","notes":[{"seq":0,"level":"loud","at":0,"msg":"x"}]}]}"#;
assert!(Inbox::from_json(bad_level).is_err());
}
#[test]
fn json_roundtrip_with_threads() {
let mut inbox = Inbox::default();
push(&mut inbox, "a", rec("a note", None, None), true);
let full = ThreadPut {
key: "pr-1".into(),
title: "#1 feat/x".into(),
link: Some("https://x/1".into()),
state: State::NeedsYou,
status: Some("review".into()),
child: Some("pr-1".into()),
actions: vec![
Action {
id: "open".into(),
label: "Open draft".into(),
host: Some(thread::HostVerb::Vscode(thread::Vscode {
path: Some("a/b.md".into()),
line: Some(3),
col: None,
})),
..Action::default()
},
Action { id: "post".into(), label: "Post".into(), notify: true, ..Action::default() },
Action { id: "fin".into(), label: "Finish".into(), done: true, ..Action::default() },
],
compose: Some(Compose { placeholder: Some("next run".into()), hint: Some("starts a run".into()) }),
};
inbox.put("a-id", "a", 7, full.clone());
let md = vec![feed::Block::Markdown { text: "multi\nline \"q\"".into() }];
inbox.send("a-id", 7, MessageSend { thread: "pr-1".into(), id: "m".into(), blocks: md });
let id = find(&inbox, "pr-1").unwrap().id;
inbox.apply(&Op::Act { thread: id, action: "post".into() }, 8, "api:x");
inbox.apply(&Op::Reply { thread: id, text: "multi \"q\" \\ x".into() }, 8, "cli");
inbox.apply(&Op::Act { thread: id, action: "fin".into() }, 8, "tui");
inbox.apply(&Op::Reopen(id), 8, "tui");
inbox.withdraw("a-id", "pr-1", "m");
inbox.put("a-id", "a", 9, ThreadPut { status: Some("merged".into()), ..full });
inbox.archive_owner("a-id");
let text = inbox.to_json().unwrap();
assert!(text.contains("\"events\""), "{text}");
let loaded = Inbox::from_json(&text).unwrap();
assert_eq!(loaded, inbox, "{text}");
assert_eq!(
feed(find(&loaded, "pr-1").unwrap()),
[
"message m: multi\nline \"q\" (withdrawn)",
"action post Post [api:x]",
"reply multi \"q\" \\ x [cli]",
"action fin Finish [tui]",
"done [tui]",
"reopen [tui]",
"state active -> needs-you",
"status review -> merged",
]
);
let max = inbox.max_id().unwrap();
assert!(loaded.next_id > max);
}
#[test]
fn imports_v2_notify_threads_and_drops_owner_threads() {
let v2 = r#"version = 2
[[thread]]
id = 4
owner = "web-id"
owner_name = "web"
key = "pr-1"
unread = true
[[thread.note]]
seq = 9
level = "warn"
at = 7
link = "https://x/1"
msg = "v2"
[[thread.note]]
seq = 3
level = "info"
at = 1
msg = "v1"
[[thread]]
id = 5
owner = "web-id"
owner_name = "web"
key = "pr-1"
kind = "thread"
unread = true
title = "PR 1"
state = "needs-you"
message = "look"
updated_at = 10
[thread.reply]
placeholder = "p"
[[thread.action]]
id = "go"
label = "Go"
[[thread.entry]]
seq = 20
at = 10
kind = "message"
text = "look"
[[thread.event]]
id = "e-0000000010-abcd"
seq = 21
kind = "reply"
text = "hi"
at = 10
[[thread]]
id = 6
owner = "gone-id"
owner_name = "gone"
archived = true
unread = false
[[thread.note]]
seq = 2
level = "error"
at = 0
msg = "old"
"#;
let mut inbox = Inbox::import_v2(v2, &BTreeMap::new()).unwrap();
assert_eq!(inbox.threads.len(), 2, "the owner thread is dropped");
let web = &inbox.threads[0];
assert_eq!((web.id, web.kind, web.owner.as_str(), web.key.as_deref()), (4, Kind::Notify, "web-id", Some("pr-1")));
assert_eq!(web.notes.iter().map(|n| n.id).collect::<Vec<_>>(), [9, 3]);
assert_eq!(web.head().unwrap().key.as_deref(), Some("pr-1"));
assert_eq!((web.head().unwrap().level, web.head().unwrap().link.as_deref()), (Level::Warn, Some("https://x/1")));
assert!(web.unread && !web.archived);
let gone = &inbox.threads[1];
assert!(gone.archived && !gone.unread);
assert!(inbox.events_for("web-id").is_empty(), "its events went with it");
push(&mut inbox, "web", rec("new", None, None), true);
assert!(inbox.threads[0].notes[0].id > 9 && inbox.threads[0].id > 6);
assert_eq!(Inbox::from_json(&inbox.to_json().unwrap()).unwrap(), inbox);
assert!(Inbox::import_v2("version = 2\n[[thread]]\nowner = \"a\"\n[[thread.note]]\nseq = 0\nlevel = \"loud\"\nat = 0\nmsg = \"x\"\n", &BTreeMap::new()).is_err());
assert!(Inbox::import_v2("", &BTreeMap::new()).unwrap().threads.is_empty());
}
fn asking(inbox: &mut Inbox) -> u64 {
let put = ThreadPut {
state: State::NeedsYou,
actions: vec![
Action { id: "post".into(), label: "Post replies".into(), ..Action::default() },
Action {
id: "look".into(),
label: "Look".into(),
host: Some(thread::HostVerb::Terminal(thread::NoArgs {})),
..Action::default()
},
Action { id: "fin".into(), label: "Finish".into(), done: true, ..Action::default() },
],
compose: Some(Compose::default()),
..put_body("pr-1")
};
inbox.put("web-id", "web", 100, put);
find(inbox, "pr-1").unwrap().id
}
fn events(inbox: &Inbox) -> Vec<(EventKind, Option<&str>, Option<&str>)> {
let t = find(inbox, "pr-1").unwrap();
t.events.iter().map(|e| (e.kind, e.action.as_deref(), e.text.as_deref())).collect()
}
#[test]
fn user_ops_add_feed_items_with_their_client_and_an_event() {
let mut inbox = Inbox::default();
let id = asking(&mut inbox);
inbox.apply(&Op::Act { thread: id, action: "post".into() }, 200, "tui");
inbox.apply(&Op::Reply { thread: id, text: " rename it ".into() }, 201, "api:my-ext");
let t = find(&inbox, "pr-1").unwrap();
assert_eq!(t.state, Some(State::NeedsYou), "a plain action leaves the state to the dispatcher");
assert_eq!(t.updated_at, 201);
assert_eq!(feed(t), ["action post Post replies [tui]", "reply rename it [api:my-ext]"]);
assert_eq!(events(&inbox), [
(EventKind::Action, Some("post"), None),
(EventKind::Reply, None, Some("rename it")),
]);
inbox.apply(&Op::Act { thread: id, action: "fin".into() }, 202, "cli");
assert_eq!(find(&inbox, "pr-1").unwrap().state, Some(State::Done));
assert_eq!(events(&inbox)[2], (EventKind::Done, Some("fin"), None));
let snapshot = inbox.clone();
inbox.apply(&Op::MarkDone(id), 203, "tui");
assert_eq!(inbox, snapshot);
inbox.apply(&Op::Reopen(id), 204, "tui");
inbox.apply(&Op::Reopen(id), 205, "tui");
assert_eq!(find(&inbox, "pr-1").unwrap().state, Some(State::Active));
inbox.apply(&Op::MarkDone(id), 206, "tui");
let t = find(&inbox, "pr-1").unwrap();
assert_eq!(t.state, Some(State::Done));
assert_eq!(feed(t)[2..], ["action fin Finish [cli]", "done [cli]", "reopen [tui]", "done [tui]"]);
assert_eq!(events(&inbox)[3..], [(EventKind::Reopen, None, None), (EventKind::Done, None, None)]);
assert_eq!(t.events.iter().map(|e| e.at).collect::<Vec<_>>(), [200, 201, 202, 204, 206]);
}
#[test]
fn the_client_never_reaches_the_owner() {
let mut inbox = Inbox::default();
let id = asking(&mut inbox);
inbox.apply(&Op::Act { thread: id, action: "post".into() }, 1, "api:secret-gui");
inbox.apply(&Op::Reply { thread: id, text: "hi".into() }, 2, "api:secret-gui");
inbox.apply(&Op::Act { thread: id, action: "fin".into() }, 3, "api:secret-gui");
inbox.apply(&Op::Reopen(id), 4, "api:secret-gui");
let events = inbox.events_for("web-id");
assert_eq!(events.len(), 4);
let json = serde_json::to_string(&events).unwrap();
assert!(!json.contains("client") && !json.contains("secret-gui"), "{json}");
let ls = serde_json::to_string(&inbox.threads_for("web-id")[0].to_put()).unwrap();
assert!(!ls.contains("secret-gui"), "{ls}");
assert!(inbox.to_json().unwrap().contains("api:secret-gui"));
}
#[test]
fn user_ops_that_do_nothing() {
let mut inbox = Inbox::default();
let id = asking(&mut inbox);
let snapshot = inbox.clone();
for op in [
Op::Act { thread: id, action: "look".into() },
Op::Act { thread: id, action: "gone".into() },
Op::Reply { thread: id, text: " \n ".into() },
Op::Reopen(id),
Op::MarkDone(u64::MAX),
] {
inbox.apply(&op, 300, "tui");
}
assert_eq!(inbox, snapshot);
inbox.put("web-id", "web", 100, put_body("plain"));
let plain = find(&inbox, "plain").unwrap().id;
inbox.apply(&Op::Reply { thread: plain, text: "hi".into() }, 300, "tui");
assert!(find(&inbox, "plain").unwrap().events.is_empty());
push(&mut inbox, "web", rec("note", None, None), true);
let note = inbox.threads.iter().find(|t| t.kind == Kind::Notify).unwrap().id;
inbox.apply(&Op::MarkDone(note), 300, "tui");
assert!(inbox.threads.iter().all(|t| t.events.is_empty() || t.key.as_deref() == Some("pr-1")));
inbox.archive_owner("web-id");
let snapshot = inbox.clone();
inbox.apply(&Op::Act { thread: id, action: "post".into() }, 300, "tui");
inbox.apply(&Op::MarkDone(id), 300, "tui");
assert_eq!(inbox, snapshot);
}
#[test]
fn a_long_reply_is_cut() {
let mut inbox = Inbox::default();
let id = asking(&mut inbox);
inbox.apply(&Op::Reply { thread: id, text: "é".repeat(MAX_REPLY + 5) }, 1, "tui");
assert_eq!(events(&inbox)[0].2.unwrap().chars().count(), MAX_REPLY);
}
#[test]
fn event_ids_have_the_shape_and_are_unique() {
let mut inbox = Inbox::default();
let id = asking(&mut inbox);
for _ in 0..MAX_EVENTS {
inbox.apply(&Op::Act { thread: id, action: "post".into() }, 1_790_900_001, "tui");
}
let t = find(&inbox, "pr-1").unwrap();
let mut ids: Vec<&str> = t.events.iter().map(|e| e.id.as_str()).collect();
assert!(ids.iter().all(|id| crate::devsbd::control::valid_event_id(id)), "{ids:?}");
assert!(ids.iter().all(|id| id.starts_with("e-1790900001-")));
ids.sort_unstable();
ids.dedup();
assert_eq!(ids.len(), MAX_EVENTS);
inbox.apply(&Op::Reply { thread: id, text: "x".into() }, 5, "tui");
let last = &find(&inbox, "pr-1").unwrap().events.last().unwrap().id;
assert!(last.starts_with("e-0000000005-"), "{last}");
}
#[test]
fn events_are_bounded_per_thread_oldest_first() {
let mut inbox = Inbox::default();
let id = asking(&mut inbox);
for i in 0..MAX_EVENTS + 3 {
inbox.apply(&Op::Reply { thread: id, text: format!("r{i}") }, i as u64, "tui");
}
let t = find(&inbox, "pr-1").unwrap();
assert_eq!(t.events.len(), MAX_EVENTS);
assert_eq!(t.events[0].text.as_deref(), Some("r3"), "the oldest three went");
}
#[test]
fn ack_is_idempotent_and_scoped_to_the_owner() {
let mut inbox = Inbox::default();
let id = asking(&mut inbox);
inbox.put("other-id", "other", 100, ThreadPut { compose: Some(Compose::default()), ..put_body("o") });
let other = find(&inbox, "o").unwrap().id;
inbox.apply(&Op::Reply { thread: other, text: "theirs".into() }, 10, "tui");
inbox.apply(&Op::Reply { thread: id, text: "one".into() }, 11, "tui");
inbox.apply(&Op::Act { thread: id, action: "post".into() }, 11, "tui");
let mine = inbox.events_for("web-id");
assert_eq!(mine.iter().map(|(k, e)| (k.as_str(), e.kind)).collect::<Vec<_>>(), [
("pr-1", EventKind::Reply),
("pr-1", EventKind::Action),
]);
let theirs = inbox.events_for("other-id");
assert_eq!(theirs.len(), 1);
assert_eq!(inbox.ack("web-id", &[theirs[0].1.id.clone()]), 0);
assert_eq!(inbox.events_for("other-id").len(), 1);
let first = vec![mine[0].1.id.clone()];
assert_eq!(inbox.ack("web-id", &first), 1);
assert_eq!(inbox.ack("web-id", &first), 0, "acking again is harmless");
assert_eq!(inbox.events_for("web-id").len(), 1);
}
#[test]
fn events_survive_a_put() {
let mut inbox = Inbox::default();
let id = asking(&mut inbox);
inbox.apply(&Op::Act { thread: id, action: "fin".into() }, 200, "tui");
let items = find(&inbox, "pr-1").unwrap().feed.len();
let again = ThreadPut { status: Some("posting".into()), ..find(&inbox, "pr-1").unwrap().to_put() };
let again = ThreadPut { state: State::NeedsYou, ..again };
inbox.put("web-id", "web", 300, again);
let t = find(&inbox, "pr-1").unwrap();
assert_eq!(t.state, Some(State::NeedsYou), "the dispatcher owns the meaning");
assert_eq!(t.events.len(), 1);
assert_eq!(t.feed.len(), items + 2, "state + status markers appended, none lost");
assert_eq!(feed(t)[items..], ["state done -> needs-you", "status running ci -> posting"]);
}
#[test]
fn thread_ls_shape_round_trips_the_put() {
let mut inbox = Inbox::default();
asking(&mut inbox);
inbox.put("web-id", "web", 100, put_body("gone"));
inbox.put("other-id", "other", 100, put_body("theirs"));
push(&mut inbox, "web", rec("note", Some("pr-1"), None), true);
let mine: Vec<ThreadPut> = inbox.threads_for("web-id").iter().map(|t| t.to_put()).collect();
assert_eq!(mine.len(), 2, "owner threads only, the owner's only");
for put in &mine {
let json = serde_json::to_string(put).unwrap();
assert_eq!(thread::parse(&json).as_ref(), Ok(put), "{json}");
assert!(!json.contains("null"), "absent fields are left out: {json}");
}
let json = serde_json::to_string(&mine[1]).unwrap();
assert!(json.contains(r#""compose":{}"#) && !json.contains("reply"), "{json}");
for put in mine {
assert_eq!(inbox.put("web-id", "web", 999, put), PutOutcome::Unchanged);
}
inbox.archive_owner("web-id");
assert!(inbox.threads_for("web-id").is_empty());
}
#[test]
fn rfc3339_dates() {
assert_eq!(rfc3339(0), "1970-01-01T00:00:00Z");
assert_eq!(rfc3339(1_709_208_000), "2024-02-29T12:00:00Z");
assert_eq!(rfc3339(1_790_900_001), "2026-10-02T00:13:21Z");
assert_eq!(rfc3339(951_782_400), "2000-02-29T00:00:00Z");
assert_eq!(rfc3339(1_735_689_599), "2024-12-31T23:59:59Z");
}
#[test]
fn imports_v1() {
let v1 = "\
[[thread]]
instance = \"web\"
unread = true
[[thread.note]]
seq = 5
level = \"warn\"
at = 7
key = \"pr-1\"
link = \"https://x/1\"
msg = \"v2\"
[[thread.note]]
seq = 2
level = \"info\"
at = 1
key = \"pr-1\"
msg = \"v1\"
[[thread]]
instance = \"gone\"
[[thread.note]]
seq = 1
level = \"info\"
at = 0
msg = \"orphan\"
";
let mut inbox = Inbox::import_v2(v1, &names(&[("web", "web-abc1")])).unwrap();
assert_eq!(inbox.threads.len(), 2);
let web = &inbox.threads[0];
assert_eq!((web.owner.as_str(), web.owner_name.as_str()), ("web-abc1", "web"));
assert_eq!(web.key.as_deref(), Some("pr-1"));
assert_eq!(web.notes.iter().map(|n| n.id).collect::<Vec<_>>(), [5, 2]);
assert_eq!(web.head().unwrap().key.as_deref(), Some("pr-1"), "the key stays on every record");
assert_eq!(web.head().unwrap().level, Level::Warn);
assert!(web.unread);
assert!(!web.archived);
let gone = &inbox.threads[1];
assert_eq!((gone.owner.as_str(), gone.owner_name.as_str()), ("name:gone", "gone"));
assert!(!gone.unread);
assert!(gone.archived);
assert_ne!(web.id, gone.id);
push(&mut inbox, "web", rec("new", None, None), true);
assert!(inbox.threads[0].notes[0].id > 5);
assert_eq!(Inbox::from_json(&inbox.to_json().unwrap()).unwrap(), inbox);
}
}