Files

144 lines
3.3 KiB
Rust

//! Records for the Honcho ledger's tests, and a ledger in a directory of its own. Do not edit.
#![allow(dead_code)] // each test file uses a different part of this module
use std::path::PathBuf;
use bxcore::{SessionId, Timestamp};
use memoryd::honcho::ledger::{Action, Item, Ledger, Record, Workspace};
use crate::tmp::TempDir;
/// A ledger file that does not exist yet, in a directory removed on drop.
pub struct Home {
pub path: PathBuf,
pub dir: TempDir,
}
impl Home {
pub fn new(tag: &str) -> Home {
let dir = TempDir::new(tag);
Home {
path: dir.path().join("honcho.jsonl"),
dir,
}
}
pub fn open(&self) -> Ledger {
Ledger::open(&self.path).unwrap()
}
/// A new ledger that has begun (`began()`): what `memoryd` makes at its first start, and so
/// what a test that reads its ledger back needs (M5c-1 task 15).
pub fn begun(&self) -> Ledger {
let mut ledger = self.open();
ledger.append(began()).unwrap();
ledger
}
/// The file's bytes as text; "" when there is no file.
pub fn text(&self) -> String {
std::fs::read_to_string(&self.path).unwrap_or_default()
}
/// Writes `text` as the whole file.
pub fn write(&self, text: &str) {
std::fs::write(&self.path, text).unwrap();
}
}
pub const TIME: &str = "2026-09-28T10:00:00.000Z";
pub fn time() -> Timestamp {
Timestamp::parse(TIME).unwrap()
}
pub fn sid(s: &str) -> SessionId {
SessionId::new(s).unwrap()
}
pub fn turn(session: &str, turn: u64) -> Item {
Item::Turn {
session: sid(session),
turn,
}
}
pub fn edit(id: &str, part: u32) -> Item {
Item::Edit {
id: id.to_string(),
part,
}
}
pub fn to_trusted(speaker: &str) -> Action {
Action::Turn {
workspace: Workspace::Trusted,
speaker: speaker.to_string(),
}
}
pub fn skip(why: &str) -> Action {
Action::Skip {
why: why.to_string(),
}
}
pub fn conclude(workspace: Workspace, text: &str) -> Action {
Action::Conclude {
workspace,
peer: "kyle".to_string(),
session: sid("chat-1"),
text: text.to_string(),
}
}
/// The deletion of the owner's (`kyle`'s) conclusion `text`, as `conclude` makes it.
pub fn unconclude(workspace: Workspace, text: &str) -> Action {
Action::Unconclude {
workspace,
peer: "kyle".to_string(),
text: text.to_string(),
}
}
/// When sync began, `TIME`.
pub fn began() -> Record {
Record::Began { time: time() }
}
/// `began()`'s line.
pub fn began_line() -> String {
line(&began())
}
pub fn decided(item: &Item, action: Action) -> Record {
Record::Decided {
time: time(),
item: item.clone(),
action,
}
}
pub fn sent(item: &Item, ids: &[&str]) -> Record {
Record::Sent {
time: time(),
item: item.clone(),
ids: ids.iter().map(|s| s.to_string()).collect(),
}
}
pub fn failed(item: &Item, status: Option<u16>) -> Record {
Record::Failed {
time: time(),
item: item.clone(),
status,
kind: "refused".to_string(),
}
}
/// `record`'s line, as `append` writes it.
pub fn line(record: &Record) -> String {
format!("{}\n", serde_json::to_string(record).unwrap())
}