feat: add trait-based CLI integration for lore and beads

Implement the data layer with mockable CLI wrappers for testability:

CLI Traits (data/*.rs):
- LoreCli: Trait for lore --robot commands (get_me, health_check)
- BeadsCli: Trait for br commands (create, close, list)
- Both use #[automock] for unit testing without real CLI

Real Implementations:
- RealLoreCli: Shells to 'lore --robot me', parses JSON response
- RealBeadsCli: Shells to 'br create/close/list --json'

Type Definitions:
- LoreMeResponse: Full response from 'lore --robot me'
  - open_issues, open_mrs_authored, reviewing_mrs, activity
  - since_last_check with EventGroup for inbox functionality
  - All fields use #[serde(default)] for forward compatibility
- Bead: Task from br list (id, title, status, priority, issue_type)

Local State Management (data/state.rs):
- GitLabBeadMap: Deduplication mapping (GitLab event -> bead ID)
- MappedBead: Tracks miss_count for two-strike orphan detection
- DecisionLog: Append-only JSONL for learning from user choices
- Atomic writes via .tmp files + rename pattern

Tauri Commands (commands/mod.rs):
- greet: Placeholder for IPC testing
- get_lore_status: Exposes lore health to frontend

This establishes the CLI-over-library pattern from PLAN.md:
clean boundaries, no schema coupling, full testability via mocks.
This commit is contained in:
teernisse
2026-02-25 17:01:25 -05:00
parent 62ee08de29
commit bb1b608fbb
5 changed files with 755 additions and 0 deletions

328
src-tauri/src/data/lore.rs Normal file
View File

@@ -0,0 +1,328 @@
//! lore CLI integration
//!
//! Provides trait-based abstraction over the lore CLI for testability.
use serde::{Deserialize, Serialize};
use std::process::Command;
#[cfg(test)]
use mockall::automock;
/// Trait for interacting with lore CLI
///
/// This abstraction allows us to mock lore in tests.
#[cfg_attr(test, automock)]
pub trait LoreCli: Send + Sync {
/// Execute `lore --robot me` and return the parsed result
fn get_me(&self) -> Result<LoreMeResponse, LoreError>;
/// Execute `lore --robot health` and check if lore is healthy
fn health_check(&self) -> Result<bool, LoreError>;
}
/// Real implementation that shells out to lore CLI
#[derive(Debug, Default)]
pub struct RealLoreCli;
impl LoreCli for RealLoreCli {
fn get_me(&self) -> Result<LoreMeResponse, LoreError> {
let output = Command::new("lore")
.args(["--robot", "me"])
.output()
.map_err(|e| LoreError::ExecutionFailed(e.to_string()))?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
return Err(LoreError::CommandFailed(stderr.to_string()));
}
let stdout = String::from_utf8_lossy(&output.stdout);
serde_json::from_str(&stdout).map_err(|e| LoreError::ParseFailed(e.to_string()))
}
fn health_check(&self) -> Result<bool, LoreError> {
let output = Command::new("lore")
.args(["health", "--json"])
.output()
.map_err(|e| LoreError::ExecutionFailed(e.to_string()))?;
Ok(output.status.success())
}
}
/// Errors that can occur when interacting with lore
#[derive(Debug, Clone, thiserror::Error)]
pub enum LoreError {
#[error("Failed to execute lore command: {0}")]
ExecutionFailed(String),
#[error("lore command failed: {0}")]
CommandFailed(String),
#[error("Failed to parse lore output: {0}")]
ParseFailed(String),
}
/// Response from `lore --robot me`
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoreMeResponse {
pub ok: bool,
pub data: LoreMeData,
#[serde(default)]
pub meta: Option<LoreMeta>,
}
/// Metadata from lore response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoreMeta {
pub elapsed_ms: Option<i64>,
}
/// Data section of `lore --robot me`
///
/// Note: Field names match actual lore CLI output format
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoreMeData {
/// Issues assigned to you
#[serde(default)]
pub open_issues: Vec<LoreIssue>,
/// MRs you authored that are open
#[serde(default)]
pub open_mrs_authored: Vec<LoreMr>,
/// MRs where you're a reviewer
#[serde(default)]
pub reviewing_mrs: Vec<LoreMr>,
/// Recent activity across GitLab
#[serde(default)]
pub activity: Vec<LoreActivity>,
/// Events since last cursor check
#[serde(default)]
pub since_last_check: Option<SinceLastCheck>,
/// Summary counts
#[serde(default)]
pub summary: Option<LoreSummary>,
/// Your username
#[serde(default)]
pub username: Option<String>,
/// ISO timestamp since when activity is shown
#[serde(default)]
pub since_iso: Option<String>,
}
/// Summary statistics from lore
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoreSummary {
pub authored_mr_count: i64,
pub needs_attention_count: i64,
pub open_issue_count: i64,
pub project_count: i64,
pub reviewing_mr_count: i64,
}
/// A GitLab issue from lore
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoreIssue {
pub iid: i64,
pub title: String,
#[serde(default)]
pub project: String,
pub state: String,
pub web_url: String,
#[serde(default)]
pub labels: Vec<String>,
#[serde(default)]
pub attention_state: Option<String>,
#[serde(default)]
pub status_name: Option<String>,
#[serde(default)]
pub updated_at_iso: Option<String>,
}
/// A GitLab merge request from lore
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoreMr {
pub iid: i64,
pub title: String,
#[serde(default)]
pub project: String,
pub state: String,
pub web_url: String,
#[serde(default)]
pub labels: Vec<String>,
#[serde(default)]
pub attention_state: Option<String>,
#[serde(default)]
pub author_username: Option<String>,
#[serde(default)]
pub detailed_merge_status: Option<String>,
#[serde(default)]
pub draft: bool,
#[serde(default)]
pub updated_at_iso: Option<String>,
}
/// Recent activity item from lore
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoreActivity {
pub actor: String,
pub event_type: String,
pub entity_iid: i64,
pub entity_type: String,
pub project: String,
#[serde(default)]
pub summary: Option<String>,
#[serde(default)]
pub body_preview: Option<String>,
#[serde(default)]
pub is_own: bool,
#[serde(default)]
pub timestamp_iso: Option<String>,
}
/// Events since last lore cursor check
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SinceLastCheck {
#[serde(default)]
pub cursor_iso: Option<String>,
#[serde(default)]
pub groups: Vec<EventGroup>,
#[serde(default)]
pub total_event_count: i64,
}
/// A group of related events (e.g., all events on one MR)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EventGroup {
pub entity_iid: i64,
pub entity_title: String,
pub entity_type: String,
pub project: String,
#[serde(default)]
pub events: Vec<LoreEvent>,
}
/// A GitLab event from the since_last_check section
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoreEvent {
pub event_type: String,
#[serde(default)]
pub actor: Option<String>,
#[serde(default)]
pub summary: Option<String>,
#[serde(default)]
pub body_preview: Option<String>,
#[serde(default)]
pub timestamp_iso: Option<String>,
}
#[cfg(test)]
mod tests {
use super::*;
fn sample_lore_me_response() -> LoreMeResponse {
LoreMeResponse {
ok: true,
data: LoreMeData {
open_issues: vec![LoreIssue {
iid: 42,
title: "Fix authentication bug".to_string(),
project: "mygroup/myproject".to_string(),
state: "opened".to_string(),
web_url: "https://gitlab.com/mygroup/myproject/-/issues/42".to_string(),
labels: vec![],
attention_state: None,
status_name: None,
updated_at_iso: None,
}],
open_mrs_authored: vec![],
reviewing_mrs: vec![LoreMr {
iid: 100,
title: "Add new feature".to_string(),
project: "mygroup/myproject".to_string(),
state: "opened".to_string(),
web_url: "https://gitlab.com/mygroup/myproject/-/merge_requests/100"
.to_string(),
labels: vec![],
attention_state: Some("needs_attention".to_string()),
author_username: Some("johndoe".to_string()),
detailed_merge_status: None,
draft: false,
updated_at_iso: None,
}],
activity: vec![],
since_last_check: Some(SinceLastCheck {
cursor_iso: None,
groups: vec![],
total_event_count: 0,
}),
summary: None,
username: Some("testuser".to_string()),
since_iso: None,
},
meta: None,
}
}
#[test]
fn test_mock_lore_cli_returns_expected_data() {
let mut mock = MockLoreCli::new();
let expected = sample_lore_me_response();
let expected_clone = expected.clone();
mock.expect_get_me()
.times(1)
.returning(move || Ok(expected_clone.clone()));
let result = mock.get_me().unwrap();
assert!(result.ok);
assert_eq!(result.data.open_issues.len(), 1);
assert_eq!(result.data.reviewing_mrs.len(), 1);
assert_eq!(result.data.open_issues[0].iid, 42);
}
#[test]
fn test_mock_lore_cli_health_check() {
let mut mock = MockLoreCli::new();
mock.expect_health_check().times(1).returning(|| Ok(true));
assert!(mock.health_check().unwrap());
}
#[test]
fn test_mock_lore_cli_can_return_error() {
let mut mock = MockLoreCli::new();
mock.expect_get_me()
.times(1)
.returning(|| Err(LoreError::ExecutionFailed("lore not found".to_string())));
let result = mock.get_me();
assert!(result.is_err());
}
#[test]
fn test_lore_response_deserialize_empty() {
let json = r#"{
"ok": true,
"data": {
"open_issues": [],
"open_mrs_authored": [],
"reviewing_mrs": [],
"activity": [],
"since_last_check": null
}
}"#;
let response: LoreMeResponse = serde_json::from_str(json).unwrap();
assert!(response.ok);
assert!(response.data.open_issues.is_empty());
}
}