Files
aigc-home/calliope-backend/tests/test_canvas_delete.py
T
施工员2号andmultica-agent 433fd719f1 feat(HEL-884): locate canvas occupancy, accurate card handling, close the delete loop
- GET /api/library/media/usage: read-only occupancy report per file —
  every live canvas card with canvas title, project/session scope, and
  the real consequence of deleting that card (deletes_file / keep reason)
- _path_on_canvas joins canvas: orphan nodes (canvas row gone, FK-cascade
  dead metadata) no longer count as occupancy; they are reported as
  invalid references with totals instead of blocking deletion
- canvas deleteNode: keep_file=true release-only variant (card removed,
  file always preserved); artifact cards with a live pending/running job
  refuse deletion (409) so completion events cannot resurrect them
- library UI: occupancy modal on kept(on_canvas) with locate / delete
  card (accurate impact text) / release-only actions, per-path free-file
  delete closing the loop, invalid-reference note, offset paging for
  large counts, library selection persisted across canvas round-trips
- canvas UI: ?locate=<node> deep-link centers the card (computed
  viewport, no store.fitView — its nodes write-back looped the graph
  rebuild effect), gold ring highlight + dismiss, back-to-library chip
- i18n keys for all 7 languages; e2e runner + 32-check real-browser
  regression on isolated data

Co-authored-by: multica-agent <github@multica.ai>
2026-10-26 23:48:15 +08:00

410 lines
14 KiB
Python

"""Canvas node deletion — real delete with file cleanup rules.
Sandbox artifact nodes (uploads/ + scratch outputs) hard-delete their file;
project-owned or still-referenced files survive the node delete.
"""
from __future__ import annotations
from pathlib import Path
import calliope.config as config_module
from calliope.db import get_db
def _seed_story_entities(project_id: int) -> None:
conn = get_db(config_module.settings.db_path)
try:
conn.execute(
"INSERT INTO characters (project_id, name, appearance) VALUES (?, ?, ?)",
(project_id, "Kira", "tall fighter"),
)
conn.execute(
"INSERT INTO scenes (project_id, order_index, heading, action) VALUES (?, ?, ?, ?)",
(project_id, 1, "Round 1", "feint low, switch high"),
)
conn.commit()
finally:
conn.close()
def _ensure_sandbox_canvas(client) -> dict:
sid = client.post("/api/agent/sessions", json={}).json()["id"]
return client.post("/api/canvas", json={"agent_session_id": sid}).json()
def _create_artifact_node(canvas_id: int, path: str, *, job_id: int | None = None) -> int:
conn = get_db(config_module.settings.db_path)
try:
cur = conn.execute(
"""
INSERT INTO canvas_node (canvas_id, type, title, x, y, artifact_path, job_id, status)
VALUES (?, 'image', 'Artifact', 0, 0, ?, ?, 'done')
""",
(canvas_id, path, job_id),
)
conn.commit()
return int(cur.lastrowid)
finally:
conn.close()
def _project_folder(assets_dir: Path, project_id: int, kind: str = "image") -> Path:
d = assets_dir / str(project_id) / kind
d.mkdir(parents=True, exist_ok=True)
return d
def test_sandbox_artifact_delete_removes_file(client, monkeypatch):
"""uploads/ file: node delete unlinks the file (true library delete)."""
assets_dir = config_module.settings.assets_dir
up = assets_dir / "uploads"
up.mkdir(parents=True, exist_ok=True)
f = up / "aaaa1111-shot.png"
f.write_bytes(b"png")
graph = _ensure_sandbox_canvas(client)
canvas_id = graph["canvas"]["id"]
node_id = _create_artifact_node(canvas_id, str(f))
r = client.delete(f"/api/canvas/{canvas_id}/nodes/{node_id}")
assert r.status_code == 200
body = r.json()
assert body["ok"] is True
assert body["file_deleted"] is True
assert not f.exists()
conn = get_db(config_module.settings.db_path)
try:
assert (
conn.execute("SELECT 1 FROM canvas_node WHERE id = ?", (node_id,)).fetchone()
is None
)
finally:
conn.close()
def test_sandbox_scratch_output_delete_removes_file(client):
"""Scratch-project output folders are deletable library files too."""
assets_dir = config_module.settings.assets_dir
# ensure scratch project exists with the system status marker
conn = get_db(config_module.settings.db_path)
try:
conn.execute(
"INSERT INTO projects (title, idea, status)"
" VALUES ('Playground Scratch', NULL, 'system')"
)
conn.commit()
row = conn.execute(
"SELECT id FROM projects WHERE status = 'system' LIMIT 1"
).fetchone()
scratch_pid = int(row["id"])
finally:
conn.close()
f = _project_folder(assets_dir, scratch_pid) / "job-1-image.png"
f.write_bytes(b"png")
graph = _ensure_sandbox_canvas(client)
canvas_id = graph["canvas"]["id"]
node_id = _create_artifact_node(canvas_id, str(f))
r = client.delete(f"/api/canvas/{canvas_id}/nodes/{node_id}")
assert r.status_code == 200
assert r.json()["file_deleted"] is True
assert not f.exists()
def test_project_owned_artifact_file_survives(client):
"""A node pointing at a real project's output: card removed, file kept."""
pid = client.post("/api/projects", json={"title": "Alpha", "idea": "x"}).json()["id"]
assets_dir = config_module.settings.assets_dir
f = _project_folder(assets_dir, pid) / "job-9-image.png"
f.write_bytes(b"png")
graph = client.post("/api/canvas", json={"project_id": pid}).json()
canvas_id = graph["canvas"]["id"]
node_id = _create_artifact_node(canvas_id, str(f))
r = client.delete(f"/api/canvas/{canvas_id}/nodes/{node_id}")
assert r.status_code == 200
body = r.json()
assert body["ok"] is True
assert body["file_deleted"] is False
assert body["reason"] == "project_owned"
assert f.exists(), "project-owned file must never be deleted"
def test_referenced_file_survives(client):
"""File referenced by an entity row: node deleted, file kept."""
pid = client.post("/api/projects", json={"title": "Beta", "idea": "x"}).json()["id"]
_seed_story_entities(pid)
assets_dir = config_module.settings.assets_dir
up = assets_dir / "uploads"
up.mkdir(parents=True, exist_ok=True)
f = up / "bbbb2222-portrait.png"
f.write_bytes(b"png")
conn = get_db(config_module.settings.db_path)
try:
conn.execute(
"UPDATE characters SET portrait_path = ? WHERE project_id = ?",
(str(f), pid),
)
conn.commit()
finally:
conn.close()
graph = client.post("/api/canvas", json={"project_id": pid}).json()
canvas_id = graph["canvas"]["id"]
node_id = _create_artifact_node(canvas_id, str(f))
r = client.delete(f"/api/canvas/{canvas_id}/nodes/{node_id}")
assert r.status_code == 200
body = r.json()
assert body["file_deleted"] is False
assert body["reason"] == "referenced"
assert f.exists()
def test_entity_node_delete_tombstones(client):
"""Entity nodes keep tombstone semantics (no resurrection by auto-seed)."""
pid = client.post("/api/projects", json={"title": "Gamma", "idea": "x"}).json()["id"]
_seed_story_entities(pid)
graph = client.post("/api/canvas", json={"project_id": pid}).json()
canvas_id = graph["canvas"]["id"]
entity_node_id = next(
n["id"] for n in graph["nodes"] if n["type"] == "entity" and n["entity_type"] == "character"
)
r = client.delete(f"/api/canvas/{canvas_id}/nodes/{entity_node_id}")
assert r.status_code == 200
assert r.json()["file_deleted"] is False
conn = get_db(config_module.settings.db_path)
try:
row = conn.execute(
"SELECT deleted FROM canvas_node WHERE id = ?", (entity_node_id,)
).fetchone()
assert row is not None and row["deleted"] == 1
finally:
conn.close()
def test_delete_unknown_node_404(client):
sid = client.post("/api/agent/sessions", json={}).json()["id"]
g = client.post("/api/canvas", json={"agent_session_id": sid}).json()
r = client.delete(f"/api/canvas/{g['canvas']['id']}/nodes/99999")
assert r.status_code == 404
def test_delete_publishes_canvas_updated(client, monkeypatch):
"""Other tabs sync: node deletion publishes canvas.updated."""
events: list[tuple[str, dict]] = []
async def fake_publish(event_type: str, data: dict) -> None:
events.append((event_type, data))
import calliope.events.bus as bus_mod
monkeypatch.setattr(bus_mod.event_bus, "publish", fake_publish)
graph = _ensure_sandbox_canvas(client)
canvas_id = graph["canvas"]["id"]
node_id = _create_artifact_node(canvas_id, "")
r = client.delete(f"/api/canvas/{canvas_id}/nodes/{node_id}")
assert r.status_code == 200
updates = [d for (t, d) in events if t == "canvas.updated"]
assert updates and updates[-1]["reason"] == "node_deleted"
def test_node_delete_keeps_file_referenced_by_other_canvas_node(client):
"""Two cards sharing one file: deleting one keeps the file (other card)."""
assets_dir = config_module.settings.assets_dir
up = assets_dir / "uploads"
up.mkdir(parents=True, exist_ok=True)
f = up / "cccc3333-shared.png"
f.write_bytes(b"png")
graph = _ensure_sandbox_canvas(client)
canvas_id = graph["canvas"]["id"]
node_a = _create_artifact_node(canvas_id, str(f))
_create_artifact_node(canvas_id, str(f))
r = client.delete(f"/api/canvas/{canvas_id}/nodes/{node_a}")
assert r.status_code == 200
assert r.json()["file_deleted"] is False
assert r.json()["reason"] == "referenced"
assert f.exists(), "file still shown by the second card"
def test_delete_node_keep_file_releases_reference_only(client):
"""keep_file=true is the explicit release-only variant: card removed,
file always preserved even when it would otherwise be unlinked."""
assets_dir = config_module.settings.assets_dir
up = assets_dir / "uploads"
up.mkdir(parents=True, exist_ok=True)
f = up / "dddd4444-keep.png"
f.write_bytes(b"png")
graph = _ensure_sandbox_canvas(client)
canvas_id = graph["canvas"]["id"]
node_id = _create_artifact_node(canvas_id, str(f))
r = client.delete(f"/api/canvas/{canvas_id}/nodes/{node_id}?keep_file=true")
assert r.status_code == 200
body = r.json()
assert body["ok"] is True
assert body["file_deleted"] is False
assert body["reason"] is None
assert f.exists(), "release-only must preserve the original file"
conn = get_db(config_module.settings.db_path)
try:
assert (
conn.execute("SELECT 1 FROM canvas_node WHERE id = ?", (node_id,)).fetchone()
is None
), "card itself is really removed"
finally:
conn.close()
# The file is now unoccupied → library delete closes the loop.
r = client.request("DELETE", "/api/library/media", json={"paths": [str(f)]})
assert r.json()["results"][0]["status"] == "deleted"
assert not f.exists()
def _insert_job(project_id: int, status: str) -> int:
conn = get_db(config_module.settings.db_path)
try:
cur = conn.execute(
"INSERT INTO jobs (project_id, kind, status) VALUES (?, 'image', ?)",
(project_id, status),
)
conn.commit()
return int(cur.lastrowid)
finally:
conn.close()
def _any_project_id() -> int:
"""Any projects row for jobs.project_id (jobs only need a live FK)."""
conn = get_db(config_module.settings.db_path)
try:
row = conn.execute("SELECT id FROM projects ORDER BY id LIMIT 1").fetchone()
if row:
return int(row["id"])
cur = conn.execute(
"INSERT INTO projects (title, idea) VALUES ('Jobs host', 'x')"
)
conn.commit()
return int(cur.lastrowid)
finally:
conn.close()
def test_delete_node_blocked_while_job_running(client):
"""A running job's card must not be deletable — the completion event
would re-materialize it (resurrection after refresh)."""
graph = _ensure_sandbox_canvas(client)
canvas_id = graph["canvas"]["id"]
job_id = _insert_job(_any_project_id(), "running")
assets_dir = config_module.settings.assets_dir
up = assets_dir / "uploads"
up.mkdir(parents=True, exist_ok=True)
f = up / "eeee5555-running.png"
f.write_bytes(b"png")
conn = get_db(config_module.settings.db_path)
try:
conn.execute(
"INSERT INTO canvas_node (canvas_id, type, title, x, y, artifact_path, job_id, status)"
" VALUES (?, 'image', '生成中', 0, 0, ?, ?, 'running')",
(canvas_id, str(f), job_id),
)
node_id = int(
conn.execute(
"SELECT id FROM canvas_node WHERE canvas_id = ? ORDER BY id DESC LIMIT 1",
(canvas_id,),
).fetchone()["id"]
)
conn.commit()
finally:
conn.close()
r = client.delete(f"/api/canvas/{canvas_id}/nodes/{node_id}")
assert r.status_code == 409
assert f.exists(), "blocked delete must not remove the file"
# Job settles → delete goes through (and the done status never blocks).
conn = get_db(config_module.settings.db_path)
try:
conn.execute("UPDATE jobs SET status = 'done' WHERE id = ?", (job_id,))
conn.commit()
finally:
conn.close()
r = client.delete(f"/api/canvas/{canvas_id}/nodes/{node_id}")
assert r.status_code == 200
assert r.json()["file_deleted"] is True
def test_delete_node_stale_running_status_with_done_job_allowed(client):
"""The jobs row is authoritative: a stale node.status='running' left over
from a finished job must not block deletion forever."""
graph = _ensure_sandbox_canvas(client)
canvas_id = graph["canvas"]["id"]
job_id = _insert_job(_any_project_id(), "done")
assets_dir = config_module.settings.assets_dir
up = assets_dir / "uploads"
up.mkdir(parents=True, exist_ok=True)
f = up / "ffff6666-stale.png"
f.write_bytes(b"png")
conn = get_db(config_module.settings.db_path)
try:
conn.execute(
"INSERT INTO canvas_node (canvas_id, type, title, x, y, artifact_path, job_id, status)"
" VALUES (?, 'image', '旧状态卡', 0, 0, ?, ?, 'running')",
(canvas_id, str(f), job_id),
)
node_id = int(
conn.execute(
"SELECT id FROM canvas_node WHERE canvas_id = ? ORDER BY id DESC LIMIT 1",
(canvas_id,),
).fetchone()["id"]
)
conn.commit()
finally:
conn.close()
r = client.delete(f"/api/canvas/{canvas_id}/nodes/{node_id}")
assert r.status_code == 200
assert r.json()["file_deleted"] is True
def test_delete_node_no_job_still_blocks_on_live_status(client):
"""Cards without a jobs row (manually posted) fall back to node.status."""
graph = _ensure_sandbox_canvas(client)
canvas_id = graph["canvas"]["id"]
assets_dir = config_module.settings.assets_dir
up = assets_dir / "uploads"
up.mkdir(parents=True, exist_ok=True)
f = up / "aaaa7777-nostatusjob.png"
f.write_bytes(b"png")
conn = get_db(config_module.settings.db_path)
try:
conn.execute(
"INSERT INTO canvas_node (canvas_id, type, title, x, y, artifact_path, status)"
" VALUES (?, 'image', '排队卡', 0, 0, ?, 'queued')",
(canvas_id, str(f)),
)
node_id = int(
conn.execute(
"SELECT id FROM canvas_node WHERE canvas_id = ? ORDER BY id DESC LIMIT 1",
(canvas_id,),
).fetchone()["id"]
)
conn.commit()
finally:
conn.close()
r = client.delete(f"/api/canvas/{canvas_id}/nodes/{node_id}")
assert r.status_code == 409