feat: annuleren taak toegevoegd
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -3,6 +3,7 @@ from __future__ import annotations
|
||||
import asyncio
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
import time
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
@@ -29,6 +30,18 @@ class FailingFilesystemAdapter(FilesystemAdapter):
|
||||
raise OSError("forced copy failure")
|
||||
|
||||
|
||||
class BlockingCopyFilesystemAdapter(FilesystemAdapter):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.entered = threading.Event()
|
||||
self.release = threading.Event()
|
||||
|
||||
def copy_file(self, source: str, destination: str, on_progress: callable | None = None) -> None:
|
||||
self.entered.set()
|
||||
self.release.wait(timeout=2.0)
|
||||
return super().copy_file(source=source, destination=destination, on_progress=on_progress)
|
||||
|
||||
|
||||
class CopyApiGoldenTest(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.temp_dir = tempfile.TemporaryDirectory()
|
||||
@@ -72,11 +85,21 @@ class CopyApiGoldenTest(unittest.TestCase):
|
||||
while time.time() < deadline:
|
||||
response = self._request("GET", f"/api/tasks/{task_id}")
|
||||
body = response.json()
|
||||
if body["status"] in {"completed", "failed"}:
|
||||
if body["status"] in {"completed", "failed", "cancelled"}:
|
||||
return body
|
||||
time.sleep(0.02)
|
||||
self.fail("task did not reach terminal state in time")
|
||||
|
||||
def _wait_for_status(self, task_id: str, statuses: set[str], timeout_s: float = 2.0) -> dict:
|
||||
deadline = time.time() + timeout_s
|
||||
while time.time() < deadline:
|
||||
response = self._request("GET", f"/api/tasks/{task_id}")
|
||||
body = response.json()
|
||||
if body["status"] in statuses:
|
||||
return body
|
||||
time.sleep(0.02)
|
||||
self.fail(f"task did not reach one of {sorted(statuses)} in time")
|
||||
|
||||
def test_copy_success_create_task_shape(self) -> None:
|
||||
src = self.root / "source.txt"
|
||||
src.write_text("hello", encoding="utf-8")
|
||||
@@ -189,6 +212,40 @@ class CopyApiGoldenTest(unittest.TestCase):
|
||||
self.assertEqual((self.root / "dest" / "file.txt").read_text(encoding="utf-8"), "F")
|
||||
self.assertEqual((self.root / "dest" / "docs" / "nested" / "note.txt").read_text(encoding="utf-8"), "N")
|
||||
|
||||
def test_copy_batch_cancelled_after_current_file_finishes(self) -> None:
|
||||
blocking_fs = BlockingCopyFilesystemAdapter()
|
||||
path_guard = PathGuard({"storage1": str(self.root), "storage2": str(self.root)})
|
||||
self._set_services(path_guard=path_guard, filesystem=blocking_fs)
|
||||
(self.root / "a.txt").write_text("A", encoding="utf-8")
|
||||
(self.root / "b.txt").write_text("B", encoding="utf-8")
|
||||
(self.root / "dest").mkdir()
|
||||
|
||||
response = self._request(
|
||||
"POST",
|
||||
"/api/files/copy",
|
||||
{
|
||||
"sources": ["storage1/a.txt", "storage1/b.txt"],
|
||||
"destination_base": "storage1/dest",
|
||||
},
|
||||
)
|
||||
|
||||
task_id = response.json()["task_id"]
|
||||
self.assertTrue(blocking_fs.entered.wait(timeout=2.0))
|
||||
running = self._wait_for_status(task_id, {"running"})
|
||||
self.assertEqual(running["current_item"], str(self.root / "a.txt"))
|
||||
|
||||
cancel_response = self._request("POST", f"/api/tasks/{task_id}/cancel")
|
||||
self.assertEqual(cancel_response.status_code, 200)
|
||||
self.assertEqual(cancel_response.json()["status"], "cancelling")
|
||||
|
||||
blocking_fs.release.set()
|
||||
detail = self._wait_task(task_id)
|
||||
self.assertEqual(detail["status"], "cancelled")
|
||||
self.assertEqual(detail["done_items"], 1)
|
||||
self.assertEqual(detail["total_items"], 2)
|
||||
self.assertTrue((self.root / "dest" / "a.txt").exists())
|
||||
self.assertFalse((self.root / "dest" / "b.txt").exists())
|
||||
|
||||
def test_copy_source_not_found(self) -> None:
|
||||
response = self._request(
|
||||
"POST",
|
||||
|
||||
@@ -3,6 +3,7 @@ from __future__ import annotations
|
||||
import asyncio
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
import time
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
@@ -33,6 +34,18 @@ class FailOnSecondCopyFilesystemAdapter(FilesystemAdapter):
|
||||
super().copy_file(source=source, destination=destination, on_progress=on_progress)
|
||||
|
||||
|
||||
class BlockingDuplicateFilesystemAdapter(FilesystemAdapter):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.entered = threading.Event()
|
||||
self.release = threading.Event()
|
||||
|
||||
def copy_file(self, source: str, destination: str, on_progress: callable | None = None) -> None:
|
||||
self.entered.set()
|
||||
self.release.wait(timeout=2.0)
|
||||
super().copy_file(source=source, destination=destination, on_progress=on_progress)
|
||||
|
||||
|
||||
class DuplicateApiGoldenTest(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.temp_dir = tempfile.TemporaryDirectory()
|
||||
@@ -75,11 +88,21 @@ class DuplicateApiGoldenTest(unittest.TestCase):
|
||||
while time.time() < deadline:
|
||||
response = self._request("GET", f"/api/tasks/{task_id}")
|
||||
body = response.json()
|
||||
if body["status"] in {"completed", "failed"}:
|
||||
if body["status"] in {"completed", "failed", "cancelled"}:
|
||||
return body
|
||||
time.sleep(0.02)
|
||||
self.fail("task did not reach terminal state in time")
|
||||
|
||||
def _wait_for_status(self, task_id: str, statuses: set[str], timeout_s: float = 2.0) -> dict:
|
||||
deadline = time.time() + timeout_s
|
||||
while time.time() < deadline:
|
||||
response = self._request("GET", f"/api/tasks/{task_id}")
|
||||
body = response.json()
|
||||
if body["status"] in statuses:
|
||||
return body
|
||||
time.sleep(0.02)
|
||||
self.fail(f"task did not reach one of {sorted(statuses)} in time")
|
||||
|
||||
def test_duplicate_single_file_success(self) -> None:
|
||||
(self.root / "note.txt").write_text("hello", encoding="utf-8")
|
||||
|
||||
@@ -132,6 +155,36 @@ class DuplicateApiGoldenTest(unittest.TestCase):
|
||||
self.assertEqual((self.root / "a copy.txt").read_text(encoding="utf-8"), "A")
|
||||
self.assertEqual((self.root / "docs copy" / "nested" / "b.txt").read_text(encoding="utf-8"), "B")
|
||||
|
||||
def test_duplicate_multi_select_cancelled_after_current_item_finishes(self) -> None:
|
||||
blocking_fs = BlockingDuplicateFilesystemAdapter()
|
||||
path_guard = PathGuard({"storage1": str(self.root), "storage2": str(self.root)})
|
||||
self._set_services(path_guard=path_guard, filesystem=blocking_fs)
|
||||
(self.root / "a.txt").write_text("A", encoding="utf-8")
|
||||
(self.root / "b.txt").write_text("B", encoding="utf-8")
|
||||
|
||||
response = self._request(
|
||||
"POST",
|
||||
"/api/files/duplicate",
|
||||
{"paths": ["storage1/a.txt", "storage1/b.txt"]},
|
||||
)
|
||||
|
||||
task_id = response.json()["task_id"]
|
||||
self.assertTrue(blocking_fs.entered.wait(timeout=2.0))
|
||||
running = self._wait_for_status(task_id, {"running"})
|
||||
self.assertEqual(running["current_item"], str(self.root / "a.txt"))
|
||||
|
||||
cancel_response = self._request("POST", f"/api/tasks/{task_id}/cancel")
|
||||
self.assertEqual(cancel_response.status_code, 200)
|
||||
self.assertEqual(cancel_response.json()["status"], "cancelling")
|
||||
|
||||
blocking_fs.release.set()
|
||||
detail = self._wait_task(task_id)
|
||||
self.assertEqual(detail["status"], "cancelled")
|
||||
self.assertEqual(detail["done_items"], 1)
|
||||
self.assertEqual(detail["total_items"], 2)
|
||||
self.assertTrue((self.root / "a copy.txt").exists())
|
||||
self.assertFalse((self.root / "b copy.txt").exists())
|
||||
|
||||
def test_duplicate_collision_resolution_for_files_and_directories(self) -> None:
|
||||
(self.root / "report.txt").write_text("R", encoding="utf-8")
|
||||
(self.root / "report copy.txt").write_text("existing", encoding="utf-8")
|
||||
|
||||
@@ -3,6 +3,7 @@ from __future__ import annotations
|
||||
import asyncio
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
import time
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
@@ -22,6 +23,18 @@ from backend.app.services.task_service import TaskService
|
||||
from backend.app.tasks_runner import TaskRunner
|
||||
|
||||
|
||||
class BlockingDeleteFilesystemAdapter(FilesystemAdapter):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.entered = threading.Event()
|
||||
self.release = threading.Event()
|
||||
|
||||
def delete_file(self, path: Path) -> None:
|
||||
self.entered.set()
|
||||
self.release.wait(timeout=2.0)
|
||||
super().delete_file(path)
|
||||
|
||||
|
||||
class FileOpsApiGoldenTest(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.temp_dir = tempfile.TemporaryDirectory()
|
||||
@@ -84,11 +97,21 @@ class FileOpsApiGoldenTest(unittest.TestCase):
|
||||
while time.time() < deadline:
|
||||
response = self._get(f"/api/tasks/{task_id}")
|
||||
body = response.json()
|
||||
if body["status"] in {"completed", "failed"}:
|
||||
if body["status"] in {"completed", "failed", "cancelled"}:
|
||||
return body
|
||||
time.sleep(0.02)
|
||||
self.fail("task did not reach terminal state in time")
|
||||
|
||||
def _wait_for_status(self, task_id: str, statuses: set[str], timeout_s: float = 2.0) -> dict:
|
||||
deadline = time.time() + timeout_s
|
||||
while time.time() < deadline:
|
||||
response = self._get(f"/api/tasks/{task_id}")
|
||||
body = response.json()
|
||||
if body["status"] in statuses:
|
||||
return body
|
||||
time.sleep(0.02)
|
||||
self.fail(f"task did not reach one of {sorted(statuses)} in time")
|
||||
|
||||
def test_mkdir_success(self) -> None:
|
||||
response = self._post(
|
||||
"/api/files/mkdir",
|
||||
@@ -272,6 +295,54 @@ class FileOpsApiGoldenTest(unittest.TestCase):
|
||||
self.assertEqual(detail["status"], "completed")
|
||||
self.assertFalse(target.exists())
|
||||
|
||||
def test_delete_file_cancelled_after_current_delete_finishes(self) -> None:
|
||||
blocking_fs = BlockingDeleteFilesystemAdapter()
|
||||
path_guard = PathGuard({"storage1": str(self.root)})
|
||||
service = FileOpsService(path_guard=path_guard, filesystem=blocking_fs)
|
||||
delete_service = DeleteTaskService(
|
||||
path_guard=path_guard,
|
||||
repository=self.repo,
|
||||
runner=TaskRunner(repository=self.repo, filesystem=blocking_fs),
|
||||
)
|
||||
task_service = TaskService(repository=self.repo)
|
||||
|
||||
async def _override_file_ops_service() -> FileOpsService:
|
||||
return service
|
||||
|
||||
async def _override_delete_task_service() -> DeleteTaskService:
|
||||
return delete_service
|
||||
|
||||
async def _override_task_service() -> TaskService:
|
||||
return task_service
|
||||
|
||||
app.dependency_overrides[get_file_ops_service] = _override_file_ops_service
|
||||
app.dependency_overrides[get_delete_task_service] = _override_delete_task_service
|
||||
app.dependency_overrides[get_task_service] = _override_task_service
|
||||
|
||||
target = self.scope / "delete_later.txt"
|
||||
target.write_text("z", encoding="utf-8")
|
||||
|
||||
response = self._post(
|
||||
"/api/files/delete",
|
||||
{"path": "storage1/scope/delete_later.txt"},
|
||||
)
|
||||
|
||||
task_id = response.json()["task_id"]
|
||||
self.assertTrue(blocking_fs.entered.wait(timeout=2.0))
|
||||
running = self._wait_for_status(task_id, {"running"})
|
||||
self.assertEqual(running["current_item"], str(self.scope / "delete_later.txt"))
|
||||
|
||||
cancel_response = self._post(f"/api/tasks/{task_id}/cancel", {})
|
||||
self.assertEqual(cancel_response.status_code, 200)
|
||||
self.assertEqual(cancel_response.json()["status"], "cancelling")
|
||||
|
||||
blocking_fs.release.set()
|
||||
detail = self._wait_task(task_id)
|
||||
self.assertEqual(detail["status"], "cancelled")
|
||||
self.assertEqual(detail["done_items"], 1)
|
||||
self.assertEqual(detail["total_items"], 1)
|
||||
self.assertFalse(target.exists())
|
||||
|
||||
def test_delete_empty_directory_success(self) -> None:
|
||||
target = self.scope / "empty_dir"
|
||||
target.mkdir()
|
||||
|
||||
@@ -49,6 +49,18 @@ class BlockingArchiveBuildFileOpsService(FileOpsService):
|
||||
super()._write_download_target_to_zip(archive, resolved_target, on_each_item=on_each_item)
|
||||
|
||||
|
||||
class BlockingCopyFilesystemAdapter(FilesystemAdapter):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.entered = threading.Event()
|
||||
self.release = threading.Event()
|
||||
|
||||
def copy_file(self, source: str, destination: str, on_progress=None) -> None:
|
||||
self.entered.set()
|
||||
self.release.wait(timeout=2.0)
|
||||
return super().copy_file(source=source, destination=destination, on_progress=on_progress)
|
||||
|
||||
|
||||
class HistoryApiGoldenTest(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.temp_dir = tempfile.TemporaryDirectory()
|
||||
@@ -82,7 +94,7 @@ class HistoryApiGoldenTest(unittest.TestCase):
|
||||
delete_service = DeleteTaskService(path_guard=self.path_guard, repository=self.task_repo, runner=runner, history_repository=self.history_repo)
|
||||
duplicate_service = DuplicateTaskService(path_guard=self.path_guard, repository=self.task_repo, runner=runner, history_repository=self.history_repo)
|
||||
move_service = MoveTaskService(path_guard=self.path_guard, repository=self.task_repo, runner=runner, history_repository=self.history_repo)
|
||||
task_service = TaskService(repository=self.task_repo)
|
||||
task_service = TaskService(repository=self.task_repo, history_repository=self.history_repo)
|
||||
history_service = HistoryService(repository=self.history_repo)
|
||||
|
||||
async def _override_file_ops_service() -> FileOpsService:
|
||||
@@ -138,6 +150,16 @@ class HistoryApiGoldenTest(unittest.TestCase):
|
||||
time.sleep(0.02)
|
||||
self.fail('task did not reach terminal state in time')
|
||||
|
||||
def _wait_for_status(self, task_id: str, statuses: set[str], timeout_s: float = 2.0) -> dict:
|
||||
deadline = time.time() + timeout_s
|
||||
while time.time() < deadline:
|
||||
response = self._request('GET', f'/api/tasks/{task_id}')
|
||||
body = response.json()
|
||||
if body['status'] in statuses:
|
||||
return body
|
||||
time.sleep(0.02)
|
||||
self.fail(f"task did not reach one of {sorted(statuses)} in time")
|
||||
|
||||
def test_get_history_empty_list(self) -> None:
|
||||
response = self._request('GET', '/api/history')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
@@ -207,6 +229,35 @@ class HistoryApiGoldenTest(unittest.TestCase):
|
||||
self.assertEqual(history[0]['source'], 'storage1/source.txt')
|
||||
self.assertEqual(history[0]['destination'], 'storage1/copied.txt')
|
||||
|
||||
def test_copy_cancelled_history_item(self) -> None:
|
||||
blocking_fs = BlockingCopyFilesystemAdapter()
|
||||
self._set_services(blocking_fs)
|
||||
(self.root1 / 'a.txt').write_text('A', encoding='utf-8')
|
||||
(self.root1 / 'b.txt').write_text('B', encoding='utf-8')
|
||||
(self.root1 / 'dest').mkdir()
|
||||
|
||||
response = self._request(
|
||||
'POST',
|
||||
'/api/files/copy',
|
||||
{'sources': ['storage1/a.txt', 'storage1/b.txt'], 'destination_base': 'storage1/dest'},
|
||||
)
|
||||
|
||||
task_id = response.json()['task_id']
|
||||
self.assertTrue(blocking_fs.entered.wait(timeout=2.0))
|
||||
self._wait_for_status(task_id, {'running'})
|
||||
cancel_response = self._request('POST', f'/api/tasks/{task_id}/cancel')
|
||||
self.assertEqual(cancel_response.status_code, 200)
|
||||
self.assertEqual(cancel_response.json()['status'], 'cancelling')
|
||||
blocking_fs.release.set()
|
||||
detail = self._wait_task(task_id)
|
||||
|
||||
self.assertEqual(detail['status'], 'cancelled')
|
||||
history = self._request('GET', '/api/history').json()['items']
|
||||
self.assertEqual(history[0]['operation'], 'copy')
|
||||
self.assertEqual(history[0]['status'], 'cancelled')
|
||||
self.assertEqual(history[0]['source'], '2 items')
|
||||
self.assertEqual(history[0]['destination'], 'storage1/dest')
|
||||
|
||||
def test_move_failed_history_item(self) -> None:
|
||||
src = self.root1 / 'source.txt'
|
||||
src.write_text('hello', encoding='utf-8')
|
||||
@@ -334,6 +385,7 @@ class HistoryApiGoldenTest(unittest.TestCase):
|
||||
cancel = self._request('POST', f"/api/files/download/archive/{response.json()['task_id']}/cancel")
|
||||
release.set()
|
||||
self._wait_task(response.json()['task_id'])
|
||||
time.sleep(0.05)
|
||||
history = self._request('GET', '/api/history').json()['items']
|
||||
|
||||
self.assertEqual(cancel.status_code, 200)
|
||||
|
||||
@@ -3,6 +3,7 @@ from __future__ import annotations
|
||||
import asyncio
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
import time
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
@@ -38,6 +39,18 @@ class FailingBatchFilesystemAdapter(FilesystemAdapter):
|
||||
super().move_directory(source, destination)
|
||||
|
||||
|
||||
class BlockingMoveFilesystemAdapter(FilesystemAdapter):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.entered = threading.Event()
|
||||
self.release = threading.Event()
|
||||
|
||||
def move_file(self, source: str, destination: str) -> None:
|
||||
self.entered.set()
|
||||
self.release.wait(timeout=2.0)
|
||||
super().move_file(source, destination)
|
||||
|
||||
|
||||
class MoveApiGoldenTest(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.temp_dir = tempfile.TemporaryDirectory()
|
||||
@@ -83,11 +96,21 @@ class MoveApiGoldenTest(unittest.TestCase):
|
||||
while time.time() < deadline:
|
||||
response = self._request("GET", f"/api/tasks/{task_id}")
|
||||
body = response.json()
|
||||
if body["status"] in {"completed", "failed"}:
|
||||
if body["status"] in {"completed", "failed", "cancelled"}:
|
||||
return body
|
||||
time.sleep(0.02)
|
||||
self.fail("task did not reach terminal state in time")
|
||||
|
||||
def _wait_for_status(self, task_id: str, statuses: set[str], timeout_s: float = 2.0) -> dict:
|
||||
deadline = time.time() + timeout_s
|
||||
while time.time() < deadline:
|
||||
response = self._request("GET", f"/api/tasks/{task_id}")
|
||||
body = response.json()
|
||||
if body["status"] in statuses:
|
||||
return body
|
||||
time.sleep(0.02)
|
||||
self.fail(f"task did not reach one of {sorted(statuses)} in time")
|
||||
|
||||
def test_move_success_same_root_create_task_shape_and_completed(self) -> None:
|
||||
src = self.root1 / "source.txt"
|
||||
src.write_text("hello", encoding="utf-8")
|
||||
@@ -225,6 +248,42 @@ class MoveApiGoldenTest(unittest.TestCase):
|
||||
self.assertFalse(source_file.exists())
|
||||
self.assertFalse(source_dir.exists())
|
||||
|
||||
def test_move_batch_cancelled_after_current_file_finishes(self) -> None:
|
||||
blocking_fs = BlockingMoveFilesystemAdapter()
|
||||
path_guard = PathGuard({"storage1": str(self.root1), "storage2": str(self.root2)})
|
||||
self._set_services(path_guard=path_guard, filesystem=blocking_fs)
|
||||
(self.root1 / "a.txt").write_text("A", encoding="utf-8")
|
||||
(self.root1 / "b.txt").write_text("B", encoding="utf-8")
|
||||
target = self.root1 / "target"
|
||||
target.mkdir()
|
||||
|
||||
response = self._request(
|
||||
"POST",
|
||||
"/api/files/move",
|
||||
{
|
||||
"sources": ["storage1/a.txt", "storage1/b.txt"],
|
||||
"destination_base": "storage1/target",
|
||||
},
|
||||
)
|
||||
|
||||
task_id = response.json()["task_id"]
|
||||
self.assertTrue(blocking_fs.entered.wait(timeout=2.0))
|
||||
running = self._wait_for_status(task_id, {"running"})
|
||||
self.assertEqual(running["current_item"], str(self.root1 / "a.txt"))
|
||||
|
||||
cancel_response = self._request("POST", f"/api/tasks/{task_id}/cancel")
|
||||
self.assertEqual(cancel_response.status_code, 200)
|
||||
self.assertEqual(cancel_response.json()["status"], "cancelling")
|
||||
|
||||
blocking_fs.release.set()
|
||||
detail = self._wait_task(task_id)
|
||||
self.assertEqual(detail["status"], "cancelled")
|
||||
self.assertEqual(detail["done_items"], 1)
|
||||
self.assertEqual(detail["total_items"], 2)
|
||||
self.assertTrue((target / "a.txt").exists())
|
||||
self.assertTrue((self.root1 / "b.txt").exists())
|
||||
self.assertFalse((target / "b.txt").exists())
|
||||
|
||||
def test_move_batch_cross_root_directories_blocked(self) -> None:
|
||||
first = self.root1 / "first-dir"
|
||||
second = self.root1 / "second-dir"
|
||||
|
||||
@@ -40,6 +40,14 @@ class TasksApiGoldenTest(unittest.TestCase):
|
||||
|
||||
return asyncio.run(_run())
|
||||
|
||||
def _post(self, url: str, payload: dict | None = None) -> httpx.Response:
|
||||
async def _run() -> httpx.Response:
|
||||
transport = httpx.ASGITransport(app=app)
|
||||
async with httpx.AsyncClient(transport=transport, base_url="http://testserver") as client:
|
||||
return await client.post(url, json=payload)
|
||||
|
||||
return asyncio.run(_run())
|
||||
|
||||
def _insert_task(
|
||||
self,
|
||||
*,
|
||||
@@ -265,6 +273,78 @@ class TasksApiGoldenTest(unittest.TestCase):
|
||||
self.assertEqual(body["total_items"], 1)
|
||||
self.assertEqual(body["current_item"], "storage1/trash.txt")
|
||||
|
||||
def test_cancel_running_delete_task_returns_cancelling(self) -> None:
|
||||
self._insert_task(
|
||||
task_id="task-delete",
|
||||
operation="delete",
|
||||
status="running",
|
||||
source="storage1/trash.txt",
|
||||
destination="",
|
||||
created_at="2026-03-10T10:00:00Z",
|
||||
started_at="2026-03-10T10:00:01Z",
|
||||
done_items=0,
|
||||
total_items=1,
|
||||
current_item="storage1/trash.txt",
|
||||
)
|
||||
|
||||
response = self._post("/api/tasks/task-delete/cancel")
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
body = response.json()
|
||||
self.assertEqual(body["operation"], "delete")
|
||||
self.assertEqual(body["status"], "cancelling")
|
||||
self.assertEqual(body["current_item"], "storage1/trash.txt")
|
||||
|
||||
def test_cancel_completed_task_rejected(self) -> None:
|
||||
self._insert_task(
|
||||
task_id="task-copy",
|
||||
operation="copy",
|
||||
status="completed",
|
||||
source="storage1/a.txt",
|
||||
destination="storage2/a.txt",
|
||||
created_at="2026-03-10T10:00:00Z",
|
||||
finished_at="2026-03-10T10:00:04Z",
|
||||
)
|
||||
|
||||
response = self._post("/api/tasks/task-copy/cancel")
|
||||
|
||||
self.assertEqual(response.status_code, 409)
|
||||
self.assertEqual(
|
||||
response.json(),
|
||||
{
|
||||
"error": {
|
||||
"code": "task_not_cancellable",
|
||||
"message": "Task cannot be cancelled",
|
||||
"details": {"task_id": "task-copy", "status": "completed"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
def test_cancel_download_task_rejected(self) -> None:
|
||||
self._insert_task(
|
||||
task_id="task-download",
|
||||
operation="download",
|
||||
status="preparing",
|
||||
source="single_directory_zip",
|
||||
destination="docs.zip",
|
||||
created_at="2026-03-10T10:00:00Z",
|
||||
started_at="2026-03-10T10:00:01Z",
|
||||
)
|
||||
|
||||
response = self._post("/api/tasks/task-download/cancel")
|
||||
|
||||
self.assertEqual(response.status_code, 409)
|
||||
self.assertEqual(
|
||||
response.json(),
|
||||
{
|
||||
"error": {
|
||||
"code": "task_not_cancellable",
|
||||
"message": "Task cannot be cancelled",
|
||||
"details": {"task_id": "task-download", "status": "preparing"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
def test_get_task_detail_ready_archive_download(self) -> None:
|
||||
self._insert_task(
|
||||
task_id="task-download-ready",
|
||||
|
||||
@@ -240,6 +240,8 @@ class UiSmokeGoldenTest(unittest.TestCase):
|
||||
self._extract_js_function(app_js, "formatTaskLine"),
|
||||
self._extract_js_function(app_js, "isActiveTask"),
|
||||
self._extract_js_function(app_js, "activeTasksFromItems"),
|
||||
self._extract_js_function(app_js, "taskIsCancellable"),
|
||||
self._extract_js_function(app_js, "cancelTaskRequest"),
|
||||
self._extract_js_function(app_js, "activeTaskChipLabel"),
|
||||
self._extract_js_function(app_js, "headerTaskRenderKey"),
|
||||
self._extract_js_function(app_js, "shouldPollHeaderTasks"),
|
||||
@@ -289,6 +291,8 @@ class UiSmokeGoldenTest(unittest.TestCase):
|
||||
textContent: "",
|
||||
innerHTML: "",
|
||||
children: [],
|
||||
disabled: false,
|
||||
onclick: null,
|
||||
scrollTop: 0,
|
||||
attributes: {{}},
|
||||
append(...nodes) {{
|
||||
@@ -329,6 +333,9 @@ class UiSmokeGoldenTest(unittest.TestCase):
|
||||
return value || "now";
|
||||
}}
|
||||
|
||||
async function refreshTasksSnapshot() {{}}
|
||||
function setError() {{}}
|
||||
|
||||
let headerTaskState = {{
|
||||
activeItems: [],
|
||||
popoverOpen: false,
|
||||
@@ -336,7 +343,7 @@ class UiSmokeGoldenTest(unittest.TestCase):
|
||||
lastRenderKey: "",
|
||||
}};
|
||||
const ACTIVE_TASK_OPERATIONS = new Set(["copy", "move", "duplicate", "delete"]);
|
||||
const ACTIVE_TASK_STATUSES = new Set(["queued", "running"]);
|
||||
const ACTIVE_TASK_STATUSES = new Set(["queued", "running", "cancelling"]);
|
||||
|
||||
{functions}
|
||||
|
||||
@@ -347,6 +354,7 @@ class UiSmokeGoldenTest(unittest.TestCase):
|
||||
{{ id: "d", operation: "download", status: "preparing", source: "/src/d", destination: "folder.zip" }},
|
||||
{{ id: "dup", operation: "duplicate", status: "queued", source: "/src/dup", destination: "/dst/dup" }},
|
||||
{{ id: "del", operation: "delete", status: "running", source: "/src/del", destination: "" }},
|
||||
{{ id: "stop", operation: "copy", status: "cancelling", source: "/src/stop", destination: "/dst/stop" }},
|
||||
{{ id: "e", operation: "copy", status: "completed", source: "/src/e", destination: "/dst/e" }},
|
||||
{{ id: "f", operation: "move", status: "failed", source: "/src/f", destination: "/dst/f" }},
|
||||
{{ id: "g", operation: "download", status: "ready", source: "/src/g", destination: "folder.zip" }},
|
||||
@@ -354,21 +362,28 @@ class UiSmokeGoldenTest(unittest.TestCase):
|
||||
];
|
||||
|
||||
const activeTasks = activeTasksFromItems(mixedTasks);
|
||||
assert(activeTasks.length === 4, "Only task-based file actions in queued or running should count as active");
|
||||
assert(activeTasks.length === 5, "Only active task-based file actions should count as active");
|
||||
assert(activeTasks.every((task) => isActiveTask(task)), "All filtered tasks should be active");
|
||||
assert(activeTasks.some((task) => task.operation === "delete"), "Delete should count once it uses the shared task flow");
|
||||
assert(activeTaskChipLabel(activeTasks.length) === "4 active tasks", "Chip label should reflect active task count");
|
||||
assert(activeTasks.some((task) => task.status === "cancelling"), "Cancelling tasks should remain visible while stopping");
|
||||
assert(activeTaskChipLabel(activeTasks.length) === "5 active tasks", "Chip label should reflect active task count");
|
||||
|
||||
updateHeaderTaskState(mixedTasks);
|
||||
assert(!elements["header-task-chip-container"].classList.contains("hidden"), "Chip should be visible with active tasks");
|
||||
assert(elements["header-task-chip-label"].textContent === "4 active tasks", "Chip label should render active task count");
|
||||
assert(elements["header-task-chip-label"].textContent === "5 active tasks", "Chip label should render active task count");
|
||||
assert(shouldPollHeaderTasks(), "Active tasks should enable header polling");
|
||||
|
||||
setHeaderTaskPopoverOpen(true);
|
||||
assert(headerTaskState.popoverOpen, "Popover should open when active tasks exist");
|
||||
assert(!elements["header-task-popover"].classList.contains("hidden"), "Popover should be visible when open");
|
||||
assert(elements["header-task-chip-btn"].attributes["aria-expanded"] === "true", "Chip button should expose expanded state");
|
||||
assert(elements["header-task-popover-list"].children.length === 4, "Popover should render only active file-action tasks");
|
||||
assert(elements["header-task-popover-list"].children.length === 5, "Popover should render only active file-action tasks");
|
||||
const firstActionButton = elements["header-task-popover-list"].children[0].children[3].children[0];
|
||||
const cancellingActionButton = elements["header-task-popover-list"].children[4].children[3].children[0];
|
||||
assert(firstActionButton.textContent === "Stop", "Queued/running tasks should expose a Stop action");
|
||||
assert(!firstActionButton.disabled, "Queued/running tasks should be cancellable");
|
||||
assert(cancellingActionButton.textContent === "Stopping...", "Cancelling tasks should show stopping state");
|
||||
assert(cancellingActionButton.disabled, "Cancelling tasks should not expose a second stop action");
|
||||
|
||||
updateHeaderTaskState([
|
||||
{{ id: "z1", operation: "copy", status: "completed", source: "/src/z1", destination: "/dst/z1" }},
|
||||
@@ -399,6 +414,8 @@ class UiSmokeGoldenTest(unittest.TestCase):
|
||||
self._extract_js_function(app_js, "formatTaskLine"),
|
||||
self._extract_js_function(app_js, "isActiveTask"),
|
||||
self._extract_js_function(app_js, "activeTasksFromItems"),
|
||||
self._extract_js_function(app_js, "taskIsCancellable"),
|
||||
self._extract_js_function(app_js, "cancelTaskRequest"),
|
||||
self._extract_js_function(app_js, "activeTaskChipLabel"),
|
||||
self._extract_js_function(app_js, "headerTaskRenderKey"),
|
||||
self._extract_js_function(app_js, "shouldPollHeaderTasks"),
|
||||
@@ -449,6 +466,8 @@ class UiSmokeGoldenTest(unittest.TestCase):
|
||||
textContent: "",
|
||||
innerHTML: "",
|
||||
children: [],
|
||||
disabled: false,
|
||||
onclick: null,
|
||||
scrollTop: 0,
|
||||
attributes: {{}},
|
||||
append(...nodes) {{
|
||||
@@ -489,6 +508,9 @@ class UiSmokeGoldenTest(unittest.TestCase):
|
||||
return value || "now";
|
||||
}}
|
||||
|
||||
async function refreshTasksSnapshot() {{}}
|
||||
function setError() {{}}
|
||||
|
||||
let state = {{ lastTaskCount: 0 }};
|
||||
let headerTaskState = {{
|
||||
activeItems: [],
|
||||
@@ -497,7 +519,7 @@ class UiSmokeGoldenTest(unittest.TestCase):
|
||||
lastRenderKey: "",
|
||||
}};
|
||||
const ACTIVE_TASK_OPERATIONS = new Set(["copy", "move", "duplicate", "delete"]);
|
||||
const ACTIVE_TASK_STATUSES = new Set(["queued", "running"]);
|
||||
const ACTIVE_TASK_STATUSES = new Set(["queued", "running", "cancelling"]);
|
||||
|
||||
{functions}
|
||||
|
||||
@@ -781,11 +803,13 @@ class UiSmokeGoldenTest(unittest.TestCase):
|
||||
self.assertIn('function formatTaskLine(task)', app_js)
|
||||
self.assertIn('let headerTaskState = {', app_js)
|
||||
self.assertIn('const ACTIVE_TASK_OPERATIONS = new Set(["copy", "move", "duplicate", "delete"]);', app_js)
|
||||
self.assertIn('const ACTIVE_TASK_STATUSES = new Set(["queued", "running"]);', app_js)
|
||||
self.assertIn('const ACTIVE_TASK_STATUSES = new Set(["queued", "running", "cancelling"]);', app_js)
|
||||
self.assertIn("The header chip reflects only user-visible file actions that use the shared task system.", app_js)
|
||||
self.assertIn('function headerTaskElements()', app_js)
|
||||
self.assertIn('function isActiveTask(task)', app_js)
|
||||
self.assertIn('function activeTasksFromItems(items)', app_js)
|
||||
self.assertIn('function taskIsCancellable(task)', app_js)
|
||||
self.assertIn('async function cancelTaskRequest(taskId)', app_js)
|
||||
self.assertIn('function activeTaskChipLabel(count)', app_js)
|
||||
self.assertIn('function shouldPollHeaderTasks()', app_js)
|
||||
self.assertIn('function scheduleHeaderTaskPolling()', app_js)
|
||||
|
||||
Reference in New Issue
Block a user