From 7963f4001427d46be7cdfb70afab43a3b9e4aae0 Mon Sep 17 00:00:00 2001 From: sasha Date: Thu, 23 Apr 2026 14:23:22 +0300 Subject: [PATCH 1/2] Expand test coverage and add cross-platform CI matrix Adds unit, integration, and end-to-end tests, and rewrites the CI workflow so the full test suite runs on ubuntu-latest, macos-latest, and windows-latest with cargo caching. - End-to-end CLI workflow test covering add/list/copy/pop/pin/unpin against a shared queue file - Pin/unpin error-path tests (unknown id, short id, idempotent pin) - TUI render tests using ratatui TestBackend - TUI map_key unit tests for keyboard routing across panes - qcli-platform tests for config_path, images_dir, nested app-dir creation, and FakeClipboard overwrite/default behavior - Mutex-guarded env-var access in paths tests to avoid parallel-runner flakes - CI: lint (fmt + clippy) stays single-OS; test job fans out to the three release platforms with per-OS cargo caches --- .github/workflows/ci.yml | 28 +++++- CHANGELOG.md | 7 ++ crates/qcli-bin/tests/cli_pin.rs | 33 +++++++ crates/qcli-bin/tests/cli_workflow.rs | 135 ++++++++++++++++++++++++++ crates/qcli-platform/src/clipboard.rs | 14 +++ crates/qcli-platform/src/paths.rs | 61 +++++++++++- crates/qcli-tui/src/render.rs | 92 ++++++++++++++++++ crates/qcli-tui/src/runtime.rs | 116 ++++++++++++++++++++++ 8 files changed, 479 insertions(+), 7 deletions(-) create mode 100644 crates/qcli-bin/tests/cli_workflow.rs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2a9630e..85f7fbd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,9 +7,13 @@ 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 @@ -17,7 +21,25 @@ jobs: 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 diff --git a/CHANGELOG.md b/CHANGELOG.md index 6df4097..86c63ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/crates/qcli-bin/tests/cli_pin.rs b/crates/qcli-bin/tests/cli_pin.rs index 2a9c302..c414fdf 100644 --- a/crates/qcli-bin/tests/cli_pin.rs +++ b/crates/qcli-bin/tests/cli_pin.rs @@ -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"); +} diff --git a/crates/qcli-bin/tests/cli_workflow.rs b/crates/qcli-bin/tests/cli_workflow.rs new file mode 100644 index 0000000..bf902cf --- /dev/null +++ b/crates/qcli-bin/tests/cli_workflow.rs @@ -0,0 +1,135 @@ +//! 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 three prompts (one pinned) — mix of arg and stdin. + q(&dir).args(["add", "first prompt"]).assert().success(); + q(&dir) + .args(["add"]) + .write_stdin("second prompt\n") + .assert() + .success(); + q(&dir) + .args(["add", "sticky note", "--pin"]) + .assert() + .success(); + + // List shows all three and the pinned marker. + q(&dir) + .args(["list"]) + .assert() + .success() + .stdout(predicates::str::contains("first prompt")) + .stdout(predicates::str::contains("second prompt")) + .stdout(predicates::str::contains("sticky note")) + .stdout(predicates::str::contains("[P]")); + + // copy --next returns the first unpinned (first prompt) and does NOT remove 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")) + .stdout(predicates::str::contains("sticky note")); + + // Pin the remaining unpinned prompt, then unpin the original pinned. + let second_id = short_id_of(&dir, "second prompt"); + q(&dir).args(["pin", &second_id]).assert().success(); + let sticky_id = short_id_of(&dir, "sticky note"); + q(&dir).args(["unpin", &sticky_id]).assert().success(); + + // After pin/unpin, JSON list preserves two entries with toggled pinned flags. + let out = q(&dir).args(["list", "--json"]).output().unwrap(); + let stdout = String::from_utf8(out.stdout).unwrap(); + let parsed: serde_json::Value = serde_json::from_str(&stdout).unwrap(); + let arr = parsed.as_array().expect("list --json must be an array"); + assert_eq!(arr.len(), 2); + let by_text: std::collections::HashMap<&str, bool> = arr + .iter() + .map(|v| (v["text"].as_str().unwrap(), v["pinned"].as_bool().unwrap())) + .collect(); + assert!(by_text["second prompt"], "second should now be pinned"); + assert!(!by_text["sticky note"], "sticky should now be unpinned"); + + // copy --next skips the now-pinned second and returns the only unpinned (sticky note). + q(&dir) + .args(["copy", "--next", "--stdout"]) + .assert() + .success() + .stdout(predicates::str::starts_with("sticky note")); + + // pop by full id removes even pinned entries. + q(&dir) + .args(["pop", &second_id, "--stdout"]) + .assert() + .success() + .stdout(predicates::str::starts_with("second prompt")); + + // pop the remaining unpinned, then verify the queue drains to empty. + q(&dir) + .args(["pop", "--next", "--stdout"]) + .assert() + .success() + .stdout(predicates::str::starts_with("sticky note")); + q(&dir) + .args(["list"]) + .assert() + .stdout(predicates::str::contains("(queue empty)")); +} diff --git a/crates/qcli-platform/src/clipboard.rs b/crates/qcli-platform/src/clipboard.rs index 2079067..0973788 100644 --- a/crates/qcli-platform/src/clipboard.rs +++ b/crates/qcli-platform/src/clipboard.rs @@ -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()); + } } diff --git a/crates/qcli-platform/src/paths.rs b/crates/qcli-platform/src/paths.rs index 042b8a3..9f10872 100644 --- a/crates/qcli-platform/src/paths.rs +++ b/crates/qcli-platform/src/paths.rs @@ -46,23 +46,76 @@ pub fn images_dir() -> std::io::Result { #[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(); } } diff --git a/crates/qcli-tui/src/render.rs b/crates/qcli-tui/src/render.rs index 3df0d51..3d6a7f0 100644 --- a/crates/qcli-tui/src/render.rs +++ b/crates/qcli-tui/src/render.rs @@ -173,3 +173,95 @@ fn render_footer(f: &mut Frame, app: &App, area: Rect) { fn dim() -> Style { Style::default().fg(MUTED) } + +#[cfg(test)] +mod tests { + use super::*; + use crate::app::App; + use qcli_core::Queue; + use ratatui::{backend::TestBackend, buffer::Buffer, Terminal}; + + fn buffer_as_text(buf: &Buffer) -> String { + let mut out = String::new(); + for y in 0..buf.area.height { + for x in 0..buf.area.width { + out.push_str(buf[(x, y)].symbol()); + } + out.push('\n'); + } + out + } + + fn render(app: &App, cursor_on: bool) -> String { + let mut terminal = Terminal::new(TestBackend::new(80, 20)).unwrap(); + terminal.draw(|f| draw(f, app, cursor_on)).unwrap(); + buffer_as_text(terminal.backend().buffer()) + } + + #[test] + fn empty_queue_renders_composer_placeholder_and_footer_hints() { + let app = App::new(Queue::new()); + let text = render(&app, false); + assert!( + text.contains("type a prompt"), + "expected composer placeholder; got:\n{text}" + ); + assert!( + text.contains("enter send/add"), + "expected footer hints; got:\n{text}" + ); + } + + #[test] + fn queue_renders_all_prompts() { + let mut queue = Queue::new(); + queue.add_text("alpha prompt").unwrap(); + queue.add_text("beta prompt").unwrap(); + let app = App::new(queue); + let text = render(&app, true); + assert!(text.contains("alpha prompt"), "missing alpha; got:\n{text}"); + assert!(text.contains("beta prompt"), "missing beta; got:\n{text}"); + } + + #[test] + fn composer_text_is_rendered() { + let mut app = App::new(Queue::new()); + app.composer = "typed here".to_string(); + let text = render(&app, true); + assert!( + text.contains("typed here"), + "missing composer; got:\n{text}" + ); + } + + #[test] + fn status_message_replaces_footer_hints() { + let mut app = App::new(Queue::new()); + app.status = "copied 5 chars".to_string(); + let text = render(&app, false); + assert!( + text.contains("copied 5 chars"), + "status not shown; got:\n{text}" + ); + assert!( + !text.contains("enter send/add"), + "default hints should be hidden; got:\n{text}" + ); + } + + #[test] + fn preview_shows_only_first_line_of_multiline_prompt() { + let mut queue = Queue::new(); + queue.add_text("line one\nline two\nline three").unwrap(); + let app = App::new(queue); + let text = render(&app, true); + assert!( + text.contains("line one"), + "first line missing; got:\n{text}" + ); + assert!( + !text.contains("line two"), + "subsequent lines leaked into queue row; got:\n{text}" + ); + } +} diff --git a/crates/qcli-tui/src/runtime.rs b/crates/qcli-tui/src/runtime.rs index 170b1ec..6f4195c 100644 --- a/crates/qcli-tui/src/runtime.rs +++ b/crates/qcli-tui/src/runtime.rs @@ -116,3 +116,119 @@ fn restore_terminal(term: &mut Terminal>) -> Result<()> execute!(term.backend_mut(), Clear(ClearType::All))?; Ok(()) } + +#[cfg(test)] +mod tests { + use super::*; + + fn key(code: KeyCode) -> KeyEvent { + KeyEvent::new(code, KeyModifiers::NONE) + } + + fn with_mods(code: KeyCode, mods: KeyModifiers) -> KeyEvent { + KeyEvent::new(code, mods) + } + + #[test] + fn q_in_queue_pane_quits() { + assert_eq!( + map_key(key(KeyCode::Char('q')), Pane::Queue), + Some(Input::Quit) + ); + } + + #[test] + fn q_in_composer_pane_is_a_char() { + assert_eq!( + map_key(key(KeyCode::Char('q')), Pane::Composer), + Some(Input::Char('q')) + ); + } + + #[test] + fn ctrl_c_always_quits() { + assert_eq!( + map_key( + with_mods(KeyCode::Char('c'), KeyModifiers::CONTROL), + Pane::Queue + ), + Some(Input::Quit) + ); + assert_eq!( + map_key( + with_mods(KeyCode::Char('c'), KeyModifiers::CONTROL), + Pane::Composer + ), + Some(Input::Quit) + ); + } + + #[test] + fn ctrl_s_is_submit() { + assert_eq!( + map_key( + with_mods(KeyCode::Char('s'), KeyModifiers::CONTROL), + Pane::Composer + ), + Some(Input::CtrlS) + ); + } + + #[test] + fn tab_esc_enter_backspace_are_mapped() { + assert_eq!(map_key(key(KeyCode::Tab), Pane::Queue), Some(Input::Tab)); + assert_eq!(map_key(key(KeyCode::Esc), Pane::Composer), Some(Input::Esc)); + assert_eq!( + map_key(key(KeyCode::Enter), Pane::Queue), + Some(Input::Enter) + ); + assert_eq!( + map_key(key(KeyCode::Backspace), Pane::Composer), + Some(Input::Backspace) + ); + } + + #[test] + fn arrow_keys_map_to_up_down() { + assert_eq!(map_key(key(KeyCode::Up), Pane::Queue), Some(Input::Up)); + assert_eq!(map_key(key(KeyCode::Down), Pane::Queue), Some(Input::Down)); + } + + #[test] + fn j_k_navigate_in_queue_pane_only() { + assert_eq!( + map_key(key(KeyCode::Char('j')), Pane::Queue), + Some(Input::Down) + ); + assert_eq!( + map_key(key(KeyCode::Char('k')), Pane::Queue), + Some(Input::Up) + ); + assert_eq!( + map_key(key(KeyCode::Char('j')), Pane::Composer), + Some(Input::Char('j')) + ); + assert_eq!( + map_key(key(KeyCode::Char('k')), Pane::Composer), + Some(Input::Char('k')) + ); + } + + #[test] + fn printable_chars_pass_through_in_composer() { + assert_eq!( + map_key(key(KeyCode::Char('x')), Pane::Composer), + Some(Input::Char('x')) + ); + assert_eq!( + map_key(key(KeyCode::Char(' ')), Pane::Composer), + Some(Input::Char(' ')) + ); + } + + #[test] + fn unmapped_keys_return_none() { + assert_eq!(map_key(key(KeyCode::F(5)), Pane::Queue), None); + assert_eq!(map_key(key(KeyCode::Home), Pane::Composer), None); + } +} From 22093b79949bb97a67af27f43a2a2597d2212e8f Mon Sep 17 00:00:00 2001 From: sasha Date: Thu, 23 Apr 2026 14:31:02 +0300 Subject: [PATCH 2/2] Fix cli_workflow test to match HEAD copy --next semantics MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The initial test assumed `copy --next` skips pinned prompts, but in HEAD it returns the first prompt in queue order regardless of pinned state. On CI — which sees only committed code — this caused copy --next to return the pinned "sticky note" when the test expected "first prompt". Rewrite the scenario so copy --next is only invoked when no pinned prompt is at the head. The test still exercises every subcommand (add, list, copy, pop, pin, unpin), pop's skip-pinned behavior, and the stdin/arg add paths — just without assuming copy also skips. --- crates/qcli-bin/tests/cli_workflow.rs | 86 +++++++++++++++------------ 1 file changed, 49 insertions(+), 37 deletions(-) diff --git a/crates/qcli-bin/tests/cli_workflow.rs b/crates/qcli-bin/tests/cli_workflow.rs index bf902cf..e3f9f4c 100644 --- a/crates/qcli-bin/tests/cli_workflow.rs +++ b/crates/qcli-bin/tests/cli_workflow.rs @@ -43,29 +43,24 @@ fn full_user_journey_persists_across_commands() { .assert() .failure(); - // Add three prompts (one pinned) — mix of arg and stdin. + // 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(); - q(&dir) - .args(["add", "sticky note", "--pin"]) - .assert() - .success(); - // List shows all three and the pinned marker. + // 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("sticky note")) - .stdout(predicates::str::contains("[P]")); + .stdout(predicates::str::contains("[P]").not()); - // copy --next returns the first unpinned (first prompt) and does NOT remove it. + // copy --next returns the head (first) without removing it. q(&dir) .args(["copy", "--next", "--stdout"]) .assert() @@ -86,48 +81,65 @@ fn full_user_journey_persists_across_commands() { .args(["list"]) .assert() .stdout(predicates::str::contains("first prompt").not()) - .stdout(predicates::str::contains("second prompt")) - .stdout(predicates::str::contains("sticky note")); - - // Pin the remaining unpinned prompt, then unpin the original pinned. - let second_id = short_id_of(&dir, "second prompt"); - q(&dir).args(["pin", &second_id]).assert().success(); - let sticky_id = short_id_of(&dir, "sticky note"); - q(&dir).args(["unpin", &sticky_id]).assert().success(); + .stdout(predicates::str::contains("second prompt")); - // After pin/unpin, JSON list preserves two entries with toggled pinned flags. - let out = q(&dir).args(["list", "--json"]).output().unwrap(); - let stdout = String::from_utf8(out.stdout).unwrap(); - let parsed: serde_json::Value = serde_json::from_str(&stdout).unwrap(); - let arr = parsed.as_array().expect("list --json must be an array"); - assert_eq!(arr.len(), 2); - let by_text: std::collections::HashMap<&str, bool> = arr - .iter() - .map(|v| (v["text"].as_str().unwrap(), v["pinned"].as_bool().unwrap())) - .collect(); - assert!(by_text["second prompt"], "second should now be pinned"); - assert!(!by_text["sticky note"], "sticky should now be unpinned"); - - // copy --next skips the now-pinned second and returns the only unpinned (sticky note). + // Add a pinned prompt; list now shows both and the pinned marker. q(&dir) - .args(["copy", "--next", "--stdout"]) + .args(["add", "sticky note", "--pin"]) .assert() - .success() - .stdout(predicates::str::starts_with("sticky note")); + .success(); + q(&dir) + .args(["list"]) + .assert() + .stdout(predicates::str::contains("sticky note")) + .stdout(predicates::str::contains("second prompt")) + .stdout(predicates::str::contains("[P]")); - // pop by full id removes even pinned entries. + // pop --next skips the pinned sticky note and returns the remaining unpinned. q(&dir) - .args(["pop", &second_id, "--stdout"]) + .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()); - // pop the remaining unpinned, then verify the queue drains to empty. + // 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()