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
10 changes: 10 additions & 0 deletions crates/agentdeck-gui/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,8 @@ pub(crate) mod fake {
made: Mutex<u32>,
/// Why the next write fails, if it should.
failure: Mutex<Option<anyhow::Error>>,
/// Why the next snapshot read fails, if it should.
read_failure: Mutex<Option<anyhow::Error>>,
/// The workspaces asked for, in order.
pub made_workspaces: Mutex<Vec<Request>>,
/// The PR syncs asked for, in order: whether each was full.
Expand Down Expand Up @@ -708,6 +710,11 @@ pub(crate) mod fake {
*self.failure.lock().unwrap() = Some(anyhow::Error::msg(reason.to_owned()));
}

/// Fails the next snapshot read with `reason`.
pub fn fail_next_read(&self, reason: &str) {
*self.read_failure.lock().unwrap() = Some(anyhow::Error::msg(reason.to_owned()));
}

/// Fails the next write as the daemon refuses one: `message`, with `code`.
pub fn refuse_next(&self, code: &str, message: &str) {
let refusal = ApiError {
Expand Down Expand Up @@ -930,6 +937,9 @@ pub(crate) mod fake {
impl Api for FakeApi {
fn snapshot(&self, _cx: &App) -> Task<anyhow::Result<Snapshot>> {
*self.reads.lock().unwrap() += 1;
if let Some(err) = self.read_failure.lock().unwrap().take() {
return Task::ready(Err(err));
}
Task::ready(Ok(self.snapshot.lock().unwrap().clone()))
}

Expand Down
57 changes: 56 additions & 1 deletion crates/agentdeck-gui/src/delete_workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
//!
//! It says what deleting does when the workspace has any: its Repositories are unlinked and
//! its running panes stopped. No folder is touched: a workspace has none. Delete workspace
//! stays disabled until the name is typed exactly. It emits [`DeleteEvent`] for the window.
//! stays disabled until the name is typed exactly. Once deleted, the GUI forgets what it kept
//! of the workspace in its own state ([`gui_state`]). It emits [`DeleteEvent`] for the window.

use std::sync::Arc;

Expand All @@ -21,6 +22,7 @@ use gpui::{
use gpui_base::input::InputEvent;

use crate::api::Api;
use crate::gui_state;
use crate::overview::Snapshot;
use crate::workspace::failed;

Expand Down Expand Up @@ -155,6 +157,8 @@ impl DeleteWorkspaceDialog {
this.deleting = false;
match deleted {
Ok(()) => {
// What the GUI kept of it would never be read again.
gui_state::forget_workspace(cx, target.id);
this.handle.close(window, cx);
cx.emit(DeleteEvent::Deleted(target.id));
}
Expand Down Expand Up @@ -294,13 +298,17 @@ impl Render for DeleteWorkspaceDialog {
#[cfg(test)]
mod tests {
use std::cell::RefCell;
use std::collections::BTreeSet;
use std::path::PathBuf;
use std::rc::Rc;

use agentdeck_core::RepositoryId;
use brrritscold_core_gpui::Root;
use gpui::{TestAppContext, VisualTestContext};

use super::*;
use crate::api::fake::FakeApi;
use crate::gui_state::{self, GuiState, Side};
use crate::notice;
use crate::overview::fixtures::{id, workspace};

Expand Down Expand Up @@ -484,6 +492,53 @@ mod tests {
assert_eq!(shown.last().unwrap().title, "Couldn't delete tabs-a11y");
}

/// A side, a collapsed group and a dismissal for each of Workspaces `ns`, saved to a
/// `gui.json` the GUI reads and writes; the temp dir holds it.
fn remember(ns: &[u32], cx: &mut TestAppContext) -> (tempfile::TempDir, PathBuf) {
let dir = tempfile::tempdir().expect("a temp dir");
let path = dir.path().join(gui_state::FILE);
gui_state::save(&path, &remembering(ns)).expect("save");
cx.update(|cx| gui_state::init(Some(path.clone()), cx));
(dir, path)
}

fn remembering(ns: &[u32]) -> GuiState {
let mut state = GuiState::default();
for &n in ns {
state.set_side(id(n), Side::PullRequests);
(state.collapsed).set_workspace(id(n), RepositoryId::from(7), true);
state.dismiss(id(n), RepositoryId::from(8));
}
state
}

#[gpui::test]
fn should_forget_what_the_gui_kept_of_the_deleted_workspace(cx: &mut TestAppContext) {
let (_dir, path) = remember(&[1, 2], cx);
let (harness, cx) = Harness::new(Arc::new(FakeApi::with(snapshot())), cx);
harness.open(cx);
harness.type_name("tabs-a11y", cx);

harness.delete(cx);

assert_eq!(gui_state::load(&path), remembering(&[2]));
let kept = cx.update(|_, cx| gui_state::read(cx, GuiState::workspaces));
assert_eq!(kept, BTreeSet::from([id(2)]));
}

#[gpui::test]
fn should_forget_nothing_when_the_delete_fails(cx: &mut TestAppContext) {
let (_dir, path) = remember(&[1, 2], cx);
let (harness, cx) = Harness::new(Arc::new(FakeApi::with(snapshot())), cx);
harness.open(cx);
harness.type_name("tabs-a11y", cx);
harness.api.fail_next("connection refused");

harness.delete(cx);

assert_eq!(gui_state::load(&path), remembering(&[1, 2]));
}

#[gpui::test]
fn should_close_and_ask_for_an_archive_instead(cx: &mut TestAppContext) {
let (harness, cx) = Harness::new(Arc::new(FakeApi::with(snapshot())), cx);
Expand Down
140 changes: 131 additions & 9 deletions crates/agentdeck-gui/src/gui_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
//! The state is a GPUI [`Global`] that `main` sets with [`init`]; a view reads it with
//! [`read`] and changes it with [`update`], which writes the file at once. Without [`init`]
//! (a view test that doesn't ask for it) the state lives in memory only.
//!
//! What it keeps by Workspace id goes with the Workspace: [`forget_workspace`] once the GUI
//! deletes one, [`keep_workspaces`] once a read lists them all. Repository ids stay, since a
//! Repository comes back with the same GitHub id once linked again.

use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};
Expand All @@ -34,7 +38,8 @@ pub struct GuiState {
/// The side each Workspace showed last; a Workspace not named here opens on its Panes.
pub last_side: BTreeMap<WorkspaceId, Side>,
/// The Repositories each Workspace's Suggestions leave out, by GitHub's id: told Not now,
/// or unlinked in the GUI. Never forgotten; the picker's Suggested section ignores them.
/// or unlinked in the GUI. Forgotten only with the Workspace; the picker's Suggested
/// section ignores them.
pub dismissed: BTreeMap<WorkspaceId, BTreeSet<RepositoryId>>,
}

Expand Down Expand Up @@ -85,6 +90,22 @@ impl GuiState {
pub fn is_dismissed(&self, workspace: WorkspaceId, repository: RepositoryId) -> bool {
(self.dismissed.get(&workspace)).is_some_and(|ids| ids.contains(&repository))
}

/// The Workspaces it remembers anything of.
pub fn workspaces(&self) -> BTreeSet<WorkspaceId> {
let sides = self.last_side.keys();
let collapsed = self.collapsed.workspaces.keys();
let dismissed = self.dismissed.keys();
sides.chain(collapsed).chain(dismissed).copied().collect()
}

/// Forgets all it remembers of `workspace`: its side, its collapsed groups and its
/// dismissed Suggestions.
pub fn forget_workspace(&mut self, workspace: WorkspaceId) {
self.last_side.remove(&workspace);
self.collapsed.workspaces.remove(&workspace);
self.dismissed.remove(&workspace);
}
}

/// The Repository groups collapsed in each view that groups PRs by Repository. A group is
Expand Down Expand Up @@ -199,6 +220,31 @@ pub fn update(cx: &mut App, f: impl FnOnce(&mut GuiState)) {
}
}

/// Forgets `workspace`, once it is deleted.
pub fn forget_workspace(cx: &mut App, workspace: WorkspaceId) {
forget_workspaces(cx, BTreeSet::from([workspace]));
}

/// Forgets every Workspace `listed` doesn't hold: those deleted since. `listed` must be a
/// successful read of every Workspace, archived ones included. The file is written only when
/// a Workspace was forgotten.
pub fn keep_workspaces(cx: &mut App, listed: &BTreeSet<WorkspaceId>) {
let gone = read(cx, |state| &state.workspaces() - listed);
forget_workspaces(cx, gone);
}

/// Forgets each of `gone`, writing the file once; not at all when it remembers none of them.
fn forget_workspaces(cx: &mut App, gone: BTreeSet<WorkspaceId>) {
if read(cx, |state| state.workspaces().is_disjoint(&gone)) {
return;
}
update(cx, |state| {
for workspace in gone {
state.forget_workspace(workspace);
}
});
}

#[cfg(test)]
mod tests {
use gpui::TestAppContext;
Expand Down Expand Up @@ -281,10 +327,6 @@ mod tests {
mod side {
use super::*;

fn ws(n: u32) -> WorkspaceId {
format!("00000000-0000-7000-8000-{n:012}").parse().unwrap()
}

#[test]
fn should_open_a_workspace_not_seen_before_on_its_panes() {
assert_eq!(GuiState::default().side(ws(1)), Side::Panes);
Expand Down Expand Up @@ -320,10 +362,6 @@ mod tests {
mod dismissed {
use super::*;

fn ws(n: u32) -> WorkspaceId {
format!("00000000-0000-7000-8000-{n:012}").parse().unwrap()
}

#[test]
fn should_dismiss_a_repository_for_one_workspace_only() {
let mut state = GuiState::default();
Expand Down Expand Up @@ -370,6 +408,51 @@ mod tests {
}
}

/// Workspace `n`'s id.
fn ws(n: u32) -> WorkspaceId {
format!("00000000-0000-7000-8000-{n:012}").parse().unwrap()
}

/// What the GUI keeps for Workspaces `ns`: a side, a collapsed group and a dismissal
/// each, beside a collapsed group on the Pull requests view and Show finished.
fn remembering(ns: &[u32]) -> GuiState {
let mut state = collapsed(&[7]);
state.show_finished = true;
for &n in ns {
state.set_side(ws(n), Side::PullRequests);
state
.collapsed
.set_workspace(ws(n), RepositoryId::from(7), true);
state.dismiss(ws(n), RepositoryId::from(8));
}
state
}

mod forget_workspace {
use super::*;

#[test]
fn should_forget_its_side_collapsed_groups_and_dismissals_only() {
let mut state = remembering(&[1, 2]);

state.forget_workspace(ws(1));

assert_eq!(state, remembering(&[2]));
}

#[test]
fn should_name_every_workspace_it_remembers_anything_of() {
let mut state = remembering(&[]);
state.set_side(ws(1), Side::PullRequests);
state
.collapsed
.set_workspace(ws(2), RepositoryId::from(7), true);
state.dismiss(ws(3), RepositoryId::from(8));

assert_eq!(state.workspaces(), BTreeSet::from([ws(1), ws(2), ws(3)]));
}
}

mod save {
use super::*;

Expand Down Expand Up @@ -414,6 +497,45 @@ mod tests {
assert_eq!(read_back, collapsed(&[7]));
}

#[gpui::test]
fn should_write_the_file_back_without_a_forgotten_workspace(cx: &mut TestAppContext) {
let dir = tempfile::tempdir().expect("a temp dir");
let path = dir.path().join(FILE);
save(&path, &remembering(&[1, 2])).expect("save");
cx.update(|cx| init(Some(path.clone()), cx));

cx.update(|cx| forget_workspace(cx, ws(1)));

assert_eq!(load(&path), remembering(&[2]));
}

#[gpui::test]
fn should_write_the_file_back_with_only_the_workspaces_listed(cx: &mut TestAppContext) {
let dir = tempfile::tempdir().expect("a temp dir");
let path = dir.path().join(FILE);
save(&path, &remembering(&[1, 2, 3])).expect("save");
cx.update(|cx| init(Some(path.clone()), cx));

cx.update(|cx| keep_workspaces(cx, &BTreeSet::from([ws(2), ws(4)])));

assert_eq!(load(&path), remembering(&[2]));
assert_eq!(cx.read(|cx| read(cx, Clone::clone)), remembering(&[2]));
}

#[gpui::test]
fn should_not_write_when_every_workspace_is_listed(cx: &mut TestAppContext) {
let dir = tempfile::tempdir().expect("a temp dir");
let path = dir.path().join(FILE);
cx.update(|cx| init(Some(path.clone()), cx));

cx.update(|cx| keep_workspaces(cx, &BTreeSet::from([ws(1)])));

assert!(
!path.exists(),
"nothing was forgotten, so nothing is written"
);
}

#[gpui::test]
fn should_keep_changes_in_memory_without_a_file(cx: &mut TestAppContext) {
cx.update(|cx| {
Expand Down
Loading