Hồi quy đã vá
-------------
F-12 Kéo–thả hoặc dán tệp vào ô chat ném NameError. R08 tách `_Input` sang
`chat_input_box.py` nhưng để `_paths_from_mime()` ở lại
`composer_widget.py`, nên hai hàm sự kiện Qt gọi một cái tên không tồn
tại. Bốn hàm dùng chung chuyển sang `composer_mime.py` — module thứ ba
là chỗ duy nhất không lặp lại được lỗi này. Đo lại: cả thả lẫn dán đều
gắn 1 tệp, khớp bản trước refactor.
F-01 Đổi provider thì bộ chọn model AI-Edit không làm gì. Hook cũ kiểm
`folder.ai_model_combo`, thuộc tính R08-T12 đã dời sang
`ai_panel.resolver`. Làm mới vô điều kiện, đúng như tab cũ: lần lấy đầu
tiên hỏng thì đổi provider chính là lúc phải thử lại.
F-07 Hàng chọn kỳ của Dashboard bị đẩy xuống dưới các thẻ số liệu. Hàng này
lọc CẢ BA thẻ con chứ không riêng biểu đồ, nên để nó nằm dưới là bắt
người dùng đọc con số trước khi thấy con số đó tính cho kỳ nào. Kèm
theo: `TokenUsageCardWidget` bị bỏ sót `setContentsMargins(0,0,0,0)`
mà hai thẻ con còn lại đã có, đẩy cả hàng thẻ lệch 9px.
`check_layout_geometry` nay khớp TỪNG BYTE với bản trước refactor.
F-11 Hai lớp khai trùng tên phương thức; Python giữ bản sau nên bản đầu là
mã chết. `co4e_tab.py::showEvent` bản đầu gọi `_narrow_guard.attach()`
và không bao giờ chạy.
Tách file (F-09)
----------------
Bốn file chạm trần 400 dòng, mỗi lần cắt ra một trách nhiệm thật:
graph_renderer.py -> graph_scene_builder.py + graph_export.py
co4e_workflow_service.py -> co4e_run_history.py
json_config_repository.py -> config_sections.py
agents_admin_tab.py -> shared/agent_kind_visuals.py
File cuối còn xoá 3 bản sao của hàm đã có trong `shared/formatters.py`,
giống hệt đến từng dòng — nay định dạng thời gian và avatar không lệch nhau
giữa các bảng Giám sát nữa.
Docstring
---------
41,6% -> 100% (3.478/3.478 định nghĩa production), kể cả module dormant và
phương thức dunder. Toàn bộ phần bổ sung viết bằng tiếng Việt; comment tiếng
Anh có sẵn giữ nguyên — dịch ngược là một đợt riêng.
Seam chưa nối dây (F-05)
------------------------
9 seam mang nhãn `SEAM · dựng <ngày>` kèm hai câu: được nối khi nào, và để
dormant thì hỏng gì. Ngày lấy từ lịch sử git, không phải hạn tự đặt. Gate O
đọc nhãn đó và nhắc khi quá 30 ngày.
859 test xanh · 4/4 cổng CASAN · 19/24 checker khớp từng byte bản cũ.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
214 lines
7.5 KiB
Python
214 lines
7.5 KiB
Python
"""EPIC R08-T12: WorkspaceFileTree / DocumentPreviewManager / FolderTab,
|
|
real Qt offscreen.
|
|
|
|
Scope note: the AI-Edit panel's send -> plan -> edit -> apply pipeline
|
|
(``ai_edit_pipeline.py``) runs on a real ``core.worker.AgentWorker`` QThread
|
|
and had ZERO existing tests before this task (confirmed by grep — nothing
|
|
under tests/ exercised ``ui/folder_tab.py``'s AI methods except the routing
|
|
surface test, fixed alongside this task in
|
|
``tests/integration/test_routing_surfaces.py``). Driving that full async
|
|
pipeline end to end is out of scope here; what's covered is everything that
|
|
doesn't need a live QThread: navigation, preview rendering, and — the
|
|
concrete R08-T12 deliverable — that saves/creates actually go through
|
|
``FileWorkspaceService`` (containment enforced, not just "writes a file").
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
|
|
|
|
from cowork_local.config import AppConfig # noqa: E402
|
|
from cowork_local.state import AppContext # noqa: E402
|
|
|
|
pytest.importorskip("PySide6", reason="Qt is required for the integration suite")
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def qt_app():
|
|
from PySide6.QtWidgets import QApplication
|
|
|
|
return QApplication.instance() or QApplication([])
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def khong_goi_mang(monkeypatch):
|
|
"""Chặn mọi lượt gọi mạng thật từ bộ chọn model của panel AI-Edit.
|
|
|
|
Mở panel AI-Edit sẽ gọi ``AiEditModelResolver.refresh()``, và hàm đó
|
|
dựng ``AgentWorker`` (QThread) đi hỏi ``list_models`` của provider qua
|
|
HTTP. Trong test, luồng nền đó sống lâu hơn chính test đã tạo ra nó:
|
|
nó vẫn đang chờ mạng khi test kết thúc, rồi vỡ giữa lúc thu gom rác ở
|
|
một test khác chạy sau — cả tiến trình pytest chết với
|
|
``Windows fatal exception``, và chỗ báo lỗi không liên quan gì tới
|
|
nguyên nhân. Việc của module này là điều hướng và hiển thị, không phải
|
|
nạp danh sách model, nên vô hiệu hoá cả hai đường ra mạng.
|
|
"""
|
|
from cowork_local.presentation.folder.ai_edit_model_resolver import AiEditModelResolver
|
|
|
|
monkeypatch.setattr(AiEditModelResolver, "refresh", lambda self: None)
|
|
monkeypatch.setattr(AiEditModelResolver, "_scan_all_image_models",
|
|
lambda self, then_suggest=False: None)
|
|
|
|
|
|
@pytest.fixture
|
|
def ctx(qt_app, tmp_path: Path):
|
|
return AppContext(AppConfig.load(tmp_path / "config.json"))
|
|
|
|
|
|
@pytest.fixture
|
|
def root(tmp_path: Path) -> Path:
|
|
d = tmp_path / "workspace"
|
|
d.mkdir()
|
|
return d
|
|
|
|
|
|
# ---- WorkspaceFileTree ----------------------------------------------------- #
|
|
def test_workspace_file_tree_set_root_updates_label_and_model(qt_app, root):
|
|
from cowork_local.presentation.folder.workspace_file_tree import WorkspaceFileTree
|
|
|
|
tree = WorkspaceFileTree(str(root))
|
|
other = root.parent / "other-root"
|
|
other.mkdir()
|
|
|
|
tree.set_root(str(other))
|
|
|
|
assert tree.root == str(other)
|
|
assert tree.path_lbl.text() == str(other)
|
|
|
|
|
|
def test_workspace_file_tree_ignores_an_invalid_root(qt_app, root):
|
|
from cowork_local.presentation.folder.workspace_file_tree import WorkspaceFileTree
|
|
|
|
tree = WorkspaceFileTree(str(root))
|
|
tree.set_root(str(root / "does-not-exist"))
|
|
|
|
assert tree.root == str(root) # unchanged
|
|
|
|
|
|
def test_workspace_file_tree_click_emits_file_selected(qt_app, root):
|
|
from cowork_local.presentation.folder.workspace_file_tree import WorkspaceFileTree
|
|
|
|
f = root / "a.txt"
|
|
f.write_text("hello", encoding="utf-8")
|
|
tree = WorkspaceFileTree(str(root))
|
|
seen = []
|
|
tree.file_selected.connect(seen.append)
|
|
|
|
index = tree.model.index(str(f))
|
|
tree._on_tree_clicked(index)
|
|
|
|
assert seen and os.path.normpath(seen[0]) == os.path.normpath(str(f))
|
|
|
|
|
|
# ---- DocumentPreviewManager ------------------------------------------------- #
|
|
def test_open_file_renders_code_into_the_editor(qt_app, root):
|
|
from cowork_local.presentation.folder.document_preview_manager import DocumentPreviewManager
|
|
|
|
f = root / "script.py"
|
|
f.write_text("print('hi')\n", encoding="utf-8")
|
|
preview = DocumentPreviewManager(str(root))
|
|
|
|
preview.open_file(str(f))
|
|
|
|
assert preview.stack.currentWidget() is preview.editor
|
|
assert preview.editor.toPlainText() == "print('hi')\n"
|
|
assert preview.current_file == str(f)
|
|
|
|
|
|
def test_open_file_on_a_binary_file_shows_the_placeholder(qt_app, root):
|
|
from cowork_local.presentation.folder.document_preview_manager import DocumentPreviewManager
|
|
|
|
f = root / "data.bin"
|
|
f.write_bytes(b"\x00\x01\x02binary")
|
|
preview = DocumentPreviewManager(str(root))
|
|
|
|
preview.open_file(str(f))
|
|
|
|
assert preview.stack.currentWidget() is preview._placeholder
|
|
|
|
|
|
def test_save_writes_through_file_workspace_service(qt_app, root):
|
|
"""The concrete R08-T12/R06-T05 deliverable: FileWorkspaceService (built
|
|
at R06, unused until this task) is now the actual write path."""
|
|
from cowork_local.presentation.folder.document_preview_manager import DocumentPreviewManager
|
|
|
|
f = root / "note.txt"
|
|
f.write_text("old", encoding="utf-8")
|
|
preview = DocumentPreviewManager(str(root))
|
|
preview.open_file(str(f))
|
|
|
|
preview.editor.setPlainText("new content")
|
|
preview.save()
|
|
|
|
assert f.read_text(encoding="utf-8") == "new content"
|
|
|
|
|
|
def test_create_new_file_writes_inside_root_via_file_workspace_service(qt_app, root):
|
|
from cowork_local.presentation.folder.document_preview_manager import DocumentPreviewManager
|
|
|
|
preview = DocumentPreviewManager(str(root))
|
|
|
|
dest = preview.create_new_file("sub/new.md", "# hello")
|
|
|
|
assert dest == str(root / "sub" / "new.md")
|
|
assert (root / "sub" / "new.md").read_text(encoding="utf-8") == "# hello"
|
|
assert preview.current_file == dest # opened it, kept the AI chat (reset_ai=False)
|
|
|
|
|
|
def test_create_new_file_rejects_a_path_escaping_the_root(qt_app, root):
|
|
from cowork_local.presentation.folder.document_preview_manager import DocumentPreviewManager
|
|
|
|
preview = DocumentPreviewManager(str(root))
|
|
messages = []
|
|
preview.status_message.connect(messages.append)
|
|
|
|
dest = preview.create_new_file("../escaped.txt", "nope")
|
|
|
|
assert dest is None
|
|
assert not (root.parent / "escaped.txt").exists()
|
|
assert messages # a save_error status was emitted
|
|
|
|
|
|
def test_write_content_persists_and_refreshes_html_preview(qt_app, root):
|
|
from cowork_local.presentation.folder.document_preview_manager import DocumentPreviewManager
|
|
|
|
f = root / "page.html"
|
|
f.write_text("<p>old</p>", encoding="utf-8")
|
|
preview = DocumentPreviewManager(str(root))
|
|
preview.open_file(str(f)) # HTML opens in preview mode by default
|
|
|
|
preview.write_content("<p>new</p>")
|
|
|
|
assert f.read_text(encoding="utf-8") == "<p>new</p>"
|
|
|
|
|
|
# ---- FolderTab shell -------------------------------------------------------- #
|
|
def test_folder_tab_builds_and_wires_tree_to_preview(ctx, root):
|
|
from cowork_local.presentation.folder.folder_tab import FolderTab
|
|
|
|
tab = FolderTab(ctx)
|
|
tab.set_root(str(root))
|
|
f = root / "hello.py"
|
|
f.write_text("x = 1\n", encoding="utf-8")
|
|
|
|
tab.tree.file_selected.emit(str(f)) # simulate a tree click
|
|
|
|
assert tab.preview.current_file == str(f)
|
|
assert tab.preview.editor.toPlainText() == "x = 1\n"
|
|
|
|
|
|
def test_folder_tab_ai_panel_toggle_updates_button_badge(ctx, root):
|
|
from cowork_local.presentation.folder.folder_tab import FolderTab
|
|
|
|
tab = FolderTab(ctx)
|
|
assert tab.ai_panel.isHidden()
|
|
|
|
tab.ai_btn.setChecked(True)
|
|
tab._toggle_ai_panel()
|
|
|
|
assert not tab.ai_panel.isHidden()
|