Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,39 @@ on:

env:
CARGO_TERM_COLOR: always
RUSTFLAGS: "-D warnings"

jobs:
check:
# Fast job: run fmt + clippy once on Linux. These are OS-agnostic, so a
# matrix would just burn minutes.
lint:
name: fmt + clippy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
- uses: Swatinem/rust-cache@v2

with:
key: lint
- run: cargo fmt --all -- --check
- run: cargo clippy --workspace --all-targets -- -D warnings
- run: cargo test --workspace

# Run the full workspace test suite on every supported OS. Release also
# targets these three platforms, so the test matrix mirrors it.
test:
name: test (${{ matrix.os }})
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2
with:
key: test-${{ matrix.os }}
- run: cargo build --workspace --all-targets --locked
- run: cargo test --workspace --locked
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ All notable changes to this project will be documented in this file.
- Pipe-friendly I/O: stdin input, stdout output, JSON output
- LLM provider scaffolding: OpenAI, Anthropic, Codex (dormant)
- CI workflow: fmt, clippy, test on push and PR
- CI test matrix across ubuntu-latest, macos-latest, windows-latest with cargo caching; fmt + clippy run once on Linux
- End-to-end CLI workflow integration test (`crates/qcli-bin/tests/cli_workflow.rs`) covering add/list/copy/pop/pin/unpin against a shared queue file
- CLI error-path tests for pin/unpin (unknown id, short-id rejection, pin idempotency)
- TUI render tests using `ratatui::backend::TestBackend`
- TUI `map_key` unit tests covering keyboard routing across panes
- Additional `qcli-platform` tests for `config_path`, `images_dir`, nested app-dir creation, and `FakeClipboard` overwrite/default behavior
- Mutex-guarded env-var access in `qcli-platform::paths` tests to avoid flakes from cargo's parallel test runner
33 changes: 33 additions & 0 deletions crates/qcli-bin/tests/cli_pin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,36 @@ fn pin_unknown_id_fails() {
.failure()
.stderr(predicates::str::contains("not found"));
}

#[test]
fn unpin_unknown_id_fails() {
let dir = TempDir::new().unwrap();
q(&dir)
.args(["unpin", "deadbeef"])
.assert()
.failure()
.stderr(predicates::str::contains("not found"));
}

#[test]
fn pin_short_id_rejected() {
let dir = TempDir::new().unwrap();
q(&dir).args(["add", "x"]).assert().success();
q(&dir)
.args(["pin", "abc"])
.assert()
.failure()
.stderr(predicates::str::contains("too short"));
}

#[test]
fn pin_already_pinned_is_idempotent() {
let dir = TempDir::new().unwrap();
q(&dir).args(["add", "stuck", "--pin"]).assert().success();
let id = short_id_of(&dir, "stuck");
q(&dir).args(["pin", &id]).assert().success();
let out = q(&dir).args(["list"]).output().unwrap();
let stdout = String::from_utf8(out.stdout).unwrap();
let line = stdout.lines().find(|l| l.contains("stuck")).unwrap();
assert!(line.contains("[P]"), "still pinned after second pin");
}
147 changes: 147 additions & 0 deletions crates/qcli-bin/tests/cli_workflow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
//! End-to-end CLI workflow: exercises the full user journey across subcommands
//! (add, list, pin, unpin, copy, pop) sharing a single persisted queue file.

use assert_cmd::Command;
use predicates::prelude::PredicateBooleanExt;
use tempfile::TempDir;

fn q(dir: &TempDir) -> Command {
let mut cmd = Command::cargo_bin("q").unwrap();
cmd.env("QCLI_APP_DIR", dir.path());
cmd
}

fn short_id_of(dir: &TempDir, marker: &str) -> String {
let out = q(dir).args(["list"]).output().unwrap();
let stdout = String::from_utf8(out.stdout).unwrap();
stdout
.lines()
.find(|line| line.contains(marker))
.unwrap_or_else(|| panic!("marker `{marker}` not found in list output:\n{stdout}"))
.split_whitespace()
.find(|s| s.chars().count() == 8)
.expect("no 8-char id column")
.to_string()
}

#[test]
fn full_user_journey_persists_across_commands() {
let dir = TempDir::new().unwrap();

// Empty queue: list reports it cleanly, copy/pop refuse.
q(&dir)
.args(["list"])
.assert()
.success()
.stdout(predicates::str::contains("(queue empty)"));
q(&dir)
.args(["copy", "--next", "--stdout"])
.assert()
.failure();
q(&dir)
.args(["pop", "--next", "--stdout"])
.assert()
.failure();

// Add two unpinned prompts (arg + stdin), newline-trimmed.
q(&dir).args(["add", "first prompt"]).assert().success();
q(&dir)
.args(["add"])
.write_stdin("second prompt\n")
.assert()
.success();

// List shows both, no pinned marker yet.
q(&dir)
.args(["list"])
.assert()
.success()
.stdout(predicates::str::contains("first prompt"))
.stdout(predicates::str::contains("second prompt"))
.stdout(predicates::str::contains("[P]").not());

// copy --next returns the head (first) without removing it.
q(&dir)
.args(["copy", "--next", "--stdout"])
.assert()
.success()
.stdout(predicates::str::starts_with("first prompt"));
q(&dir)
.args(["list"])
.assert()
.stdout(predicates::str::contains("first prompt"));

// pop --next removes the first unpinned and returns its text.
q(&dir)
.args(["pop", "--next", "--stdout"])
.assert()
.success()
.stdout(predicates::str::starts_with("first prompt"));
q(&dir)
.args(["list"])
.assert()
.stdout(predicates::str::contains("first prompt").not())
.stdout(predicates::str::contains("second prompt"));

// Add a pinned prompt; list now shows both and the pinned marker.
q(&dir)
.args(["add", "sticky note", "--pin"])
.assert()
.success();
q(&dir)
.args(["list"])
.assert()
.stdout(predicates::str::contains("sticky note"))
.stdout(predicates::str::contains("second prompt"))
.stdout(predicates::str::contains("[P]"));

// pop --next skips the pinned sticky note and returns the remaining unpinned.
q(&dir)
.args(["pop", "--next", "--stdout"])
.assert()
.success()
.stdout(predicates::str::starts_with("second prompt"));
q(&dir)
.args(["list"])
.assert()
.stdout(predicates::str::contains("sticky note"))
.stdout(predicates::str::contains("second prompt").not());

// With only pinned remaining, pop --next reports no unpinned available.
q(&dir)
.args(["pop", "--next", "--stdout"])
.assert()
.failure()
.stderr(predicates::str::contains("no unpinned"));

// Resolve the pinned prompt's id and flip it unpinned.
let sticky_id = short_id_of(&dir, "sticky note");
q(&dir).args(["unpin", &sticky_id]).assert().success();

// JSON list confirms single entry with pinned=false.
let out = q(&dir).args(["list", "--json"]).output().unwrap();
let parsed: serde_json::Value =
serde_json::from_str(&String::from_utf8(out.stdout).unwrap()).unwrap();
let arr = parsed.as_array().expect("list --json must be an array");
assert_eq!(arr.len(), 1);
assert_eq!(arr[0]["text"], "sticky note");
assert!(
!arr[0]["pinned"].as_bool().unwrap(),
"sticky should now be unpinned"
);

// Pin it again via the subcommand, then pop it by id (pop by id removes
// even pinned prompts).
q(&dir).args(["pin", &sticky_id]).assert().success();
q(&dir)
.args(["pop", &sticky_id, "--stdout"])
.assert()
.success()
.stdout(predicates::str::starts_with("sticky note"));

// Queue drains to empty.
q(&dir)
.args(["list"])
.assert()
.stdout(predicates::str::contains("(queue empty)"));
}
14 changes: 14 additions & 0 deletions crates/qcli-platform/src/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,18 @@ mod tests {
cb.set_text("hello").unwrap();
assert_eq!(cb.last.as_deref(), Some("hello"));
}

#[test]
fn fake_clipboard_overwrites_on_subsequent_set() {
let mut cb = FakeClipboard::new();
cb.set_text("first").unwrap();
cb.set_text("second").unwrap();
assert_eq!(cb.last.as_deref(), Some("second"));
}

#[test]
fn fake_clipboard_default_is_empty() {
let cb = FakeClipboard::default();
assert!(cb.last.is_none());
}
}
61 changes: 57 additions & 4 deletions crates/qcli-platform/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,76 @@ pub fn images_dir() -> std::io::Result<PathBuf> {
#[cfg(test)]
mod tests {
use super::*;
use std::sync::Mutex;
use tempfile::TempDir;

// Serializes env-var mutation across tests in this module. Cargo runs
// unit tests in parallel within a single binary, and QCLI_APP_DIR is
// process-wide; without this guard concurrent tests could observe each
// other's value and flake.
static ENV_GUARD: Mutex<()> = Mutex::new(());

fn set_app_dir(path: &std::path::Path) {
std::env::set_var("QCLI_APP_DIR", path);
}

fn clear_app_dir() {
std::env::remove_var("QCLI_APP_DIR");
}

#[test]
fn override_env_is_honored() {
let _guard = ENV_GUARD.lock().unwrap();
let tmp = TempDir::new().unwrap();
std::env::set_var("QCLI_APP_DIR", tmp.path());
set_app_dir(tmp.path());
let dir = app_dir().unwrap();
assert_eq!(dir, tmp.path());
std::env::remove_var("QCLI_APP_DIR");
clear_app_dir();
}

#[test]
fn queue_path_ends_in_queue_json() {
let _guard = ENV_GUARD.lock().unwrap();
let tmp = TempDir::new().unwrap();
std::env::set_var("QCLI_APP_DIR", tmp.path());
set_app_dir(tmp.path());
let p = queue_path().unwrap();
assert_eq!(p.file_name().and_then(|s| s.to_str()), Some("queue.json"));
std::env::remove_var("QCLI_APP_DIR");
assert_eq!(p.parent(), Some(tmp.path()));
clear_app_dir();
}

#[test]
fn config_path_ends_in_config_json() {
let _guard = ENV_GUARD.lock().unwrap();
let tmp = TempDir::new().unwrap();
set_app_dir(tmp.path());
let p = config_path().unwrap();
assert_eq!(p.file_name().and_then(|s| s.to_str()), Some("config.json"));
assert_eq!(p.parent(), Some(tmp.path()));
clear_app_dir();
}

#[test]
fn images_dir_is_created_under_app_dir() {
let _guard = ENV_GUARD.lock().unwrap();
let tmp = TempDir::new().unwrap();
set_app_dir(tmp.path());
let dir = images_dir().unwrap();
assert_eq!(dir, tmp.path().join("images"));
assert!(dir.is_dir(), "images_dir should create the directory");
clear_app_dir();
}

#[test]
fn app_dir_creates_missing_override_directory() {
let _guard = ENV_GUARD.lock().unwrap();
let tmp = TempDir::new().unwrap();
let nested = tmp.path().join("nested").join("app");
assert!(!nested.exists());
set_app_dir(&nested);
let dir = app_dir().unwrap();
assert_eq!(dir, nested);
assert!(nested.is_dir());
clear_app_dir();
}
}
Loading
Loading