Skip to content
Open
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
76 changes: 69 additions & 7 deletions e2e/team-review-fixes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,29 @@ test("a team profile on a proxy the member can't see reads Hidden proxy, and sta
await expect(toast(page, "won’t start")).toHaveCount(0);
});

test("a team folder deleted with its profiles stays while they wait in the team's trash, and says so", async ({ page }) => {
async function askToDeleteAds(page: Page) {
await openApp(page, { section: "profiles" });
await joinTeam(page, ADMIN_INVITE);
await expect(row(page, "tp-ads-1")).toBeVisible();
await page.locator("aside [data-folder='Ads']").click({ button: "right" });
await page.getByRole("menuitem", { name: "Delete folder" }).click();
await page.getByRole("alertdialog", { name: "Delete folder “Ads”?" }).getByRole("button", { name: "Delete profiles too" }).click();
return page.getByRole("alertdialog", { name: "Delete folder “Ads”?" });
}

function folderAndTrash(page: Page, profileId: string) {
return page.evaluate((id) => {
const { state } = (window as unknown as M).__shardxMock;
const trash = (state.teamServers[state.space.team_id].trash ?? []) as { id: string; kind: string }[];
return {
folder: state.profiles[id]?._meta.folder ?? null,
archived: trash.filter((entry) => entry.kind === "profile").map((entry) => entry.id).sort(),
};
}, profileId);
}

test("a team folder deleted with its profiles stays while they wait in the team's trash, and says so", async ({ page }) => {
const ask = await askToDeleteAds(page);
await ask.getByRole("button", { name: "Delete profiles too" }).click();
await expect(toast(page, "Deleted 3 profiles from “Ads”. The folder stays while the team keeps them in its trash.")).toBeVisible();
await expect(toast(page, "Couldn’t delete")).toHaveCount(0);
for (const id of ["tp-ads-1", "tp-ads-2", "tp-ads-3"]) await expect(row(page, id)).toHaveCount(0);
Expand All @@ -95,6 +111,50 @@ test("a team folder deleted with its profiles stays while they wait in the team'
await expect(toast(page, "Couldn’t delete")).toContainText("Profiles deleted from it are kept in the team’s trash, so the team server keeps the folder too.");
});

test("deleting a team's folder contents leaves a profile moved out while the confirmation was open", async ({ page }) => {
const ask = await askToDeleteAds(page);
await expect(ask).toContainText("It holds 3 profiles");
await page.evaluate(() => {
const mock = (window as unknown as M).__shardxMock;
const profile = mock.state.profiles["tp-ads-1"];
profile._meta.folder = "Shops";
profile._meta.rev++;
mock.emit("store-changed", "profiles");
});
await expect(row(page, "tp-ads-1").locator("[data-cell='folder']")).toHaveText("Shops");
await ask.getByRole("button", { name: "Delete profiles too" }).click();

await expect.poll(() => folderAndTrash(page, "tp-ads-1")).toEqual({ folder: "Shops", archived: ["tp-ads-2", "tp-ads-3"] });
await expect(row(page, "tp-ads-1").locator("[data-cell='folder']")).toHaveText("Shops");
});

test("a profile arriving during team folder cleanup stays in that folder", async ({ page }) => {
const ask = await askToDeleteAds(page);
await expect(ask).toContainText("It holds 3 profiles");
type WithCleanup = M & { __adsCleanupFinished?: boolean };
await page.evaluate(() => {
const w = window as unknown as WithCleanup;
const ipc = w.__TAURI_INTERNALS__;
const invoke = ipc.invoke;
ipc.invoke = (cmd, args) => {
if (cmd === "folder_delete" && args?.folder === "Ads") {
// The new arrival is on the server just before it receives the folder cleanup.
const profile = w.__shardxMock.state.profiles["tp-shop-1"];
profile._meta.folder = "Ads";
profile._meta.rev++;
ipc.invoke = invoke;
return invoke(cmd, args).finally(() => { w.__adsCleanupFinished = true; });
}
return invoke(cmd, args);
};
});
await ask.getByRole("button", { name: "Delete profiles too" }).click();

await page.waitForFunction(() => (window as unknown as WithCleanup).__adsCleanupFinished);
expect(await folderAndTrash(page, "tp-shop-1")).toEqual({ folder: "Ads", archived: ["tp-ads-1", "tp-ads-2", "tp-ads-3"] });
await expect(row(page, "tp-shop-1").locator("[data-cell='folder']")).toHaveText("Ads");
});

test("Create without Edit makes the profile in one write, on its proxy, with nothing left half done", async ({ page }) => {
await asMember(page, [...allow(TEAM, "see"), ...allow(SHOPS, "create")]);
await page.locator("main header").getByRole("button", { name: "New profile" }).click();
Expand Down Expand Up @@ -173,12 +233,14 @@ test("New folder, Import and Add proxies say why they are off for a member", asy
test("without Create in the team, nothing offers a new proxy or a new tag", async ({ page }) => {
await asMember(page, [...allow(TEAM, "see"), ...allow(SHOPS, "edit", "start")]);
const etsy = row(page, "tp-shop-1");
await etsy.click({ button: "right" });
await item(page, /^Change proxy/).click();
await expect(page.getByRole("button", { name: "Add a new proxy" })).toHaveCount(0);
await etsy.locator("[data-cell='status']").click();
await page.keyboard.press("p");
const proxies = page.getByRole("dialog", { name: /Proxy/ });
await expect(proxies).toBeVisible();
await expect(proxies.getByRole("button", { name: "Add a new proxy" })).toHaveCount(0);
await page.keyboard.press("Escape");
await etsy.click({ button: "right" });
await item(page, /^Edit tags/).click();
await expect(proxies).toHaveCount(0);
await page.keyboard.press("t");
const find = page.getByRole("combobox", { name: "Find or create a tag…" }).or(page.getByPlaceholder("Find a tag…"));
await expect(page.getByPlaceholder("Find a tag…")).toBeVisible();
await expect(page.getByText("No tags yet. Making one needs “Create” in this team.")).toBeVisible();
Expand Down
99 changes: 96 additions & 3 deletions src-tauri/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2417,6 +2417,99 @@ mod tests {
drop(t.srv);
}

#[tokio::test]
async fn a_folder_scoped_profile_delete_spares_a_profile_that_moves_out() {
let t = Team::new(&[allow(Scope::Team, Action::See), allow(Scope::Team, Action::Delete)]).await;
for id in ["p-before", "p-during", "p-stays"] {
t.profile(id, "Ads", id, json!({})).await;
}
t.enter().await;
let epoch = Some(crate::team::space_epoch());
t.profile("p-before", "Shops", "p-before", json!({})).await;
assert_eq!(crate::profile_delete("p-before".into(), epoch, Some("Ads".into())).await.unwrap(), Some(false));

// This time the move lands after the source check, just before the delete.
crate::space::hold::arm();
let delete = tokio::spawn(crate::profile_delete("p-during".into(), epoch, Some("Ads".into())));
crate::space::hold::wait_reached().await;
t.profile("p-during", "Shops", "p-during", json!({})).await;
crate::space::hold::release();
assert_eq!(delete.await.unwrap().unwrap(), Some(false));
for id in ["p-before", "p-during"] {
assert_eq!(t.on_server(id).await["folder"], "Shops");
}

// The source check reads only metadata, so See and Delete suffice.
assert_eq!(crate::profile_delete("p-stays".into(), epoch, Some("Ads".into())).await.unwrap(), Some(true));
let trash = t.admin_call(Method::GET, "/trash", None).await;
assert_eq!(trash.as_array().unwrap().len(), 1);
assert_eq!(trash[0]["id"], "p-stays");
drop(t.srv);
}

#[tokio::test]
async fn deleting_a_team_folder_and_keeping_profiles_spares_concurrent_moves() {
let t = Team::as_admin().await;
t.profile("p-first", "Ads", "A First", json!({})).await;
t.profile("p-moved", "Ads", "B Second", json!({})).await;
t.enter().await;
let epoch = Some(crate::team::space_epoch());

// Move the second profile after the operation listed the old members of the folder.
crate::space::hold::arm();
let delete = tokio::spawn(crate::folder_delete("Ads".into(), false, epoch, None));
crate::space::hold::wait_reached().await;
t.profile("p-moved", "Shops", "B Second", json!({})).await;
crate::space::hold::release();
assert_eq!(delete.await.unwrap().unwrap(), 1);
assert_eq!(t.on_server("p-first").await["folder"], "");
assert_eq!(t.on_server("p-moved").await["folder"], "Shops");
let folders = t.admin_call(Method::GET, "/folders", None).await;
assert!(!folders.as_array().unwrap().iter().any(|f| f["name"] == "Ads"), "{folders}");
drop(t.srv);
}

#[tokio::test]
async fn a_rename_fallback_does_not_move_profiles_from_another_admins_new_folder() {
let t = Team::as_admin().await;
t.profile("p-1", "Ads", "Shop", json!({})).await;
t.enter().await;
let epoch = Some(crate::team::space_epoch());

crate::space::hold::arm();
let rename = tokio::spawn(crate::folder_rename("Ads".into(), "Clients".into(), epoch));
crate::space::hold::wait_reached().await;
t.admin_call(Method::PATCH, "/admin/folders/Ads", Some(json!({ "name": "Shops" }))).await;
crate::space::hold::release();
assert_eq!(rename.await.unwrap().unwrap_err(), "no folder Ads in this team");
assert_eq!(t.on_server("p-1").await["folder"], "Shops");
let folders = t.admin_call(Method::GET, "/folders", None).await;
assert!(!folders.as_array().unwrap().iter().any(|f| f["name"] == "Clients"), "{folders}");
drop(t.srv);
}

#[tokio::test]
async fn a_new_arrival_keeps_a_team_folder_from_being_deleted() {
let t = Team::as_admin().await;
t.profile("p-first", "Ads", "A First", json!({})).await;
t.profile("p-arrived", "Shops", "B Second", json!({})).await;
t.enter().await;
let epoch = Some(crate::team::space_epoch());

crate::space::hold::arm();
let delete = tokio::spawn(crate::folder_delete("Ads".into(), true, epoch, None));
crate::space::hold::wait_reached().await;
t.profile("p-arrived", "Ads", "B Second", json!({})).await;
crate::space::hold::release();
assert_eq!(delete.await.unwrap().unwrap_err(), "the folder is not empty");
assert_eq!(crate::folder_delete("Ads".into(), false, epoch, Some(true)).await.unwrap_err(), "the folder is not empty");
assert_eq!(t.on_server("p-arrived").await["folder"], "Ads");
let trash = t.admin_call(Method::GET, "/trash", None).await;
assert_eq!(trash.as_array().unwrap().len(), 1);
assert_eq!(trash[0]["id"], "p-first");
drop(t.srv);
}

#[tokio::test]
async fn a_save_racing_a_space_switch_is_refused_as_space_changed() {
let t = Team::new(&[allow(folder("Ads"), Action::See), allow(folder("Ads"), Action::Edit)]).await;
Expand Down Expand Up @@ -2474,13 +2567,13 @@ mod tests {
t.enter().await;
let listed = |all: Value| all.as_array().unwrap().iter().any(|p| p["id"] == "p-1");

assert_eq!(crate::profile_delete("p-1".into(), None).await, Err(crate::space::NO_EPOCH.to_string()));
assert_eq!(crate::profile_delete("p-1".into(), None, None).await, Err(crate::space::NO_EPOCH.to_string()));
let stale = Some(crate::team::space_epoch() - 1);
let refused = crate::profile_delete("p-1".into(), stale).await;
let refused = crate::profile_delete("p-1".into(), stale, None).await;
assert_eq!(refused, Err(crate::space::error::SPACE_CHANGED.to_string()));
assert!(listed(t.admin_call(Method::GET, "/profiles", None).await), "nothing was deleted");

crate::profile_delete("p-1".into(), Some(crate::team::space_epoch())).await.unwrap();
crate::profile_delete("p-1".into(), Some(crate::team::space_epoch()), None).await.unwrap();
assert!(!listed(t.admin_call(Method::GET, "/profiles", None).await));
drop(t.srv);
}
Expand Down
18 changes: 14 additions & 4 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,9 +487,14 @@ pub fn save_profile_here(

/// Into the trash: this machine's for a week, or the team's on its server.
#[tauri::command]
async fn profile_delete(id: String, epoch: Option<u64>) -> Result<(), String> {
async fn profile_delete(id: String, epoch: Option<u64>, expected_folder: Option<String>) -> Result<Option<bool>, String> {
let t = ticket(epoch)?;
space::profile::remove(space::Door("profile_delete"), &t, &id).await.map_err(|e| e.to_string())
let door = space::Door("profile_delete");
if let Some(folder) = expected_folder {
return space::profile::remove_from_folder(door, &t, &id, &folder).await.map(Some).map_err(|e| e.to_string());
}
space::profile::remove(door, &t, &id).await.map_err(|e| e.to_string())?;
Ok(None)
}

/// The ticket for a write the interface asked under `epoch`.
Expand Down Expand Up @@ -1345,9 +1350,14 @@ async fn folder_rename(old: String, new: String, epoch: Option<u64>) -> Result<u

/// Delete folder; `delete_profiles` true → remove, false → unfile.
#[tauri::command]
async fn folder_delete(folder: String, delete_profiles: bool, epoch: Option<u64>) -> Result<usize, String> {
async fn folder_delete(folder: String, delete_profiles: bool, epoch: Option<u64>, empty_only: Option<bool>) -> Result<usize, String> {
let t = ticket(epoch)?;
space::profile::delete_folder(space::Door("folder_delete"), &t, &folder, delete_profiles)
let door = space::Door("folder_delete");
if empty_only == Some(true) {
space::profile::delete_empty_team_folder(door, &t, &folder).await.map_err(|e| e.to_string())?;
return Ok(0);
}
space::profile::delete_folder(door, &t, &folder, delete_profiles)
.await
.map_err(|e| e.to_string())
}
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/live_e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ mod device {
let ((r1, d1), (r2, d2)) = tokio::join!(one, two);
Ok(json!({ "first": show(r1, d1), "second": show(r2, d2) }))
}
"profile_delete" => crate::profile_delete(s(&a, "id"), epoch()).await.map(|_| Value::Null),
"profile_delete" => crate::profile_delete(s(&a, "id"), epoch(), a["expected_folder"].as_str().map(str::to_owned)).await.map(val),
"admin_member_reissue" => {
let m = crate::team_admin_member_reissue(team()?, s(&a, "member")).await?;
Ok(json!({ "member": val(&m.member), "invite": m.invite }))
Expand Down
12 changes: 12 additions & 0 deletions src-tauri/src/space/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,12 @@ pub mod profile {
}
}

/// Into the team Trash only while the profile is still in the selected folder.
pub async fn remove_from_folder(door: Door, t: &Ticket, id: &str, folder: &str) -> Result<bool> {
let team = t.team().ok_or(CmdError::NotOffered("this folder check requires a team space"))?;
Ok(write::remove_profile_from_folder(door, t, team, id, folder).await?)
}

/// Removes the profile and its browser data at once, past the trash.
// Only a run's step does this.
#[cfg_attr(not(feature = "automation"), allow(dead_code))]
Expand Down Expand Up @@ -649,6 +655,12 @@ pub mod profile {
Ok(n)
}

/// Remove the team folder after its selected profiles were handled separately.
pub async fn delete_empty_team_folder(door: Door, t: &Ticket, name: &str) -> Result<()> {
let team = t.team().ok_or(CmdError::NotOffered("this folder check requires a team space"))?;
Ok(write::delete_empty_folder(door, t, team, name).await?)
}

// This machine's own profiles, which only it starts: never a team's.

pub fn fill_missing_window(id: &str, window: serde_json::Value) -> Result<bool> {
Expand Down
Loading