Feature/perf ui logic (#13)
CI / test (push) Canceled after 0s

## Summary

Nhánh `feature/perf-ui-logic`: tối ưu hiệu năng/UI, sửa lỗi workspace và điều hướng, và làm cho công tắc **"Block network for agent-run commands"** chặn thật mọi đường ra mạng của app, **trừ nhà cung cấp AI**.

**Chặn mạng (b78d483, 8c497cf, 10b8379)**
- Bộ kiểm tra chung `application/network/network_guard.py`, nối vào cấu hình đang chạy ở Composition Root: đổi công tắc trong Settings là có hiệu lực ngay.
- Lệnh shell của agent và task script chạy trong **Windows AppContainer không có quyền mạng**: kernel chặn socket, ping, DNS, Invoke-WebRequest… Không cần quyền admin. Không cô lập được thì lệnh bị từ chối, không chạy khi mạng còn mở. macOS dùng `sandbox-exec`, Linux dùng `unshare --net`.
- Bật chặn thì: dừng MCP đang chạy, không khởi động server mới, từ chối lời gọi connector; Microsoft 365 (đăng nhập, Graph, đồng bộ cloud, rules, mail), Teams, nút Test REST/Jira/MCP, link đính kèm task, pip tự cài và tài nguyên web trong xem trước HTML đều bị từ chối.
- Vẫn dùng được: chat, tải danh sách model, thử model; tool OneDrive đã đồng bộ trên máy.
- Công tắc **mặc định tắt** khi mở app lần đầu; nhãn giữ nguyên như cũ.
- Xem trước HTML trong tab Folder giờ hiện được ảnh/CSS/JS từ web khi mạng mở (trước đây trang `file://` không tải được).
- Sửa lỗi app văng khi chuyển tab Graph → Folder: profile WebEngine của trang xem trước bị huỷ trước trang (`0xc0000409` trong Qt6Core.dll); giờ dùng một profile chung thuộc QApplication.
- Không cấp quyền AppContainer kế thừa lên thư mục chứa PySide6 (nếu có, Chromium không nạp được `Qt6WebEngineCore.dll` và tab Graph trắng).
- Cột mục lục trong Settings tính độ rộng theo kiểu chữ của mục đang chọn, "Sandbox Security Layer" không còn bị cắt.

**Các commit khác trong nhánh**
- `b7a41b3` mỗi thư mục làm việc chỉ thuộc về một project · `bbdf146` bật nút Sửa project khi đã có project đang mở
- `35f24e0`, `cc8d5c8`, `2e3e719`, `c699beb` canh hàng / khoảng cách thanh điều hướng
- `2759ed9` không refresh workspace khi chuyển tab Cowork · `7607f44` checkpoint hiệu năng và UI
- `8548c1e` chặn tool mạng của agent · `caf3b74` renderer GraphRAG native trên macOS · `c00b83c` khoảng cách metadata hàng project · `a04f8a9` ẩn picker workspace cloud

## Change Type

- [x] Cowork feature
- [x] Bug fix
- [ ] Core AI contribution
- [x] Test / hardening
- [x] Performance
- [ ] Documentation

## Related Work

Cowork Task:

Core Repo: http://34.143.229.138/gitea-admin/fsg-ai-core-assets

Core AI Issue:

Core Task:

Related PR:

## Scope

What is intentionally included?
- Mọi đường ra mạng do app tự mở, trừ nhà cung cấp AI (xem Summary).
- Test: `tests/test_network_guard_lanes.py` (có bài chạy AppContainer thật trên Windows), `tests/ui/test_html_preview_remote_images.py`.

What is intentionally NOT included?
- Chặn cả nhà cung cấp AI / chạy model trên máy (Phương án 2).
- Terminal người dùng tự gõ trong tab Folder, sinh ảnh, cơ chế tự tin chứng chỉ lạ (`tls_trust`).
- Huy hiệu trạng thái "đang chặn" trên thanh trên cùng.

## Validation

- [x] Unit tests
- [x] Integration tests
- [x] Manual verification
- [x] Regression check

Commands / evidence:
- `python -m pytest tests/test_network_guard_lanes.py tests/test_sandbox_block_network.py tests/ui -q` → chỉ còn 1 bài fail, fail cả trên `b7a41b3` (nhãn `ProjectRow` 'Project' chưa dịch, `tests/ui/test_i18n_khong_con_chu_cu.py`).
- `python -m pytest tests -q --ignore=tests/ui` → 4 bài fail, cả 4 cũng fail trên `b7a41b3` (`test_canonical_audit_logger`, 2 bài `test_mcp_audit_security`, `test_monitoring_tab_container`).
- Chạy cả `tests` trong một lượt thì treo ở các test dựng MainWindow trong `tests/ui`; `b7a41b3` cũng treo đúng chỗ đó.
- `check_imports.py` và `check_orphan_modules.py` PASS. `check_loc.py` báo 9 file quá dài, giống hệt trước khi sửa (không file nào do nhánh này làm dài thêm).
- Kiểm tra tay trên Windows 11: trong AppContainer, Python báo `WinError 10013`, ping/nslookup/PowerShell/curl đều không ra được mạng; cmd, git, python chạy bình thường.
- Kiểm tra tay trên Windows 11: xem trước HTML tải được 4/4 tài nguyên web khi mạng mở, 0/4 khi bật chặn; tab Graph hoạt động; tạo/huỷ trang xem trước nhiều lần không còn cảnh báo profile của Qt.

## Security Impact

Permission / credential / network / customer data impact:
- Network: khi bật công tắc, chỉ nhà cung cấp AI còn ra mạng; nội dung chat vẫn gửi tới nhà cung cấp AI.
- Permission: lần đầu chạy lệnh trong sandbox, app **thêm quyền (ACE) cho SID AppContainer** trên thư mục làm việc (ghi), thư mục cài Python gốc (đọc), gốc venv và `Scripts` (đọc). Không xoá quyền nào. Thư mục chứa PySide6 không bao giờ nhận quyền kế thừa; một quyền kế thừa sai trên venv (từ bản dev trước) được tự gỡ.
- Credential: không đổi. Khi chặn, trạng thái đăng nhập M365 được đọc thẳng từ kho token trên máy, không dựng MSAL.

## Compatibility

- [x] No breaking change
- [ ] Breaking change documented

Ghi chú: `block_network` mặc định đổi từ bật sang tắt cho cấu hình mới; máy đã lưu `true` thì giữ nguyên. Khi đang chặn, lệnh dùng công cụ cài trong thư mục người dùng (ngoài Program Files) có thể báo Access denied; thư viện trong venv của app không dùng được trong sandbox.

## Reviewer Notes

- `infrastructure/sandbox/appcontainer_process.py` gọi Win32 bằng ctypes (CreateAppContainerProfile, CreateProcessW với SECURITY_CAPABILITIES) và dùng `icacls` để cấp quyền: nên xem kỹ phần cấp quyền.
- `tests/conftest.py` thêm fixture autouse gỡ `network_guard` sau mỗi test, vì `build_context()` gắn cổng này ở mức process.
- `core/task_executors.py` đang đúng bằng trần LOC nên `_run_script` được tách sang `core/task_script.py`.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: minhanhpkpro <minhanhpkpro@gmail.com>
Co-authored-by: Duy Le Huu <duylh19@fpt.com>
Co-authored-by: thanhnv <thanhnv.ip@gmail.com>
Reviewed-on: #13
This commit was merged in pull request #13.
This commit is contained in:
2026-09-20 12:26:03 +00:00
co-authored by minhanhpkpro duylh19 thanhnv
parent cbae2604db
commit a03a740ea1
57 changed files with 2326 additions and 122 deletions
@@ -0,0 +1,51 @@
"""Xem trước HTML trong tab Folder phải hiện được ảnh lấy từ web khi mạng mở.
Tệp được nạp với base URL ``file://``; Qt mặc định cấm trang cục bộ tải bất kỳ
tài nguyên web nào nếu ``LocalContentCanAccessRemoteUrls`` tắt, nên ảnh
``<img src="https://...">`` không bao giờ hiện, kể cả khi đã mở Internet.
Việc chặn khi bật "Chặn mạng" là của bộ chặn request, không phải của cờ này.
"""
from __future__ import annotations
import pytest
pytest.importorskip("PySide6.QtWebEngineWidgets", reason="cần Qt WebEngine")
from cowork_local.presentation.shared import HAS_WEB_ENGINE # noqa: E402
pytestmark = pytest.mark.skipif(not HAS_WEB_ENGINE, reason="WebEngine không dùng được ở đây")
def test_trang_xem_truoc_cho_phep_tai_anh_tu_web(qapp):
from PySide6.QtWebEngineCore import QWebEngineSettings
from PySide6.QtWebEngineWidgets import QWebEngineView
from cowork_local.presentation.folder import offline_web_page as owp
view = QWebEngineView()
try:
owp.install_offline_page(view)
profile = view.page().profile()
assert profile is owp.preview_profile()
assert profile.settings().testAttribute(
QWebEngineSettings.WebAttribute.LocalContentCanAccessRemoteUrls)
assert isinstance(owp._blocker, owp.RemoteRequestBlocker)
finally:
view.deleteLater()
def test_profile_song_lau_hon_moi_trang(qapp):
"""Profile do view sở hữu bị huỷ TRƯỚC trang: Qt cảnh báo "Release of
profile requested but WebEnginePage still not deleted" rồi app có thể văng
(0xc0000409 trong Qt6Core.dll) khi chuyển tab. Profile phải thuộc về app."""
from PySide6.QtWebEngineWidgets import QWebEngineView
from cowork_local.presentation.folder import offline_web_page as owp
view = QWebEngineView()
try:
owp.install_offline_page(view)
assert view.page().parent() is view
assert owp.preview_profile().parent() is qapp
finally:
view.deleteLater()
+135
View File
@@ -0,0 +1,135 @@
"""Ba hàng cuối thanh rail phải thẳng hàng: Dashboard, Giám sát, Cài đặt.
Dashboard và Giám sát là hàng của ``QTreeWidget`` (``#navrailBottom``), còn Cài
đặt là một ``QPushButton`` tự dựng lấy icon + chữ trong ``top_bar.py``. Hai cách
vẽ khác nhau nên không có gì tự giữ cho chúng thẳng hàng — phải chốt bằng test.
Lần lệch gần nhất: ``srow.setSpacing(_NAV_ROW_GAP + 4)`` (=10) cộng với 10px lề
trái và hộp icon 22px đặt chữ "Cài đặt" ở x=42, trong khi delegate của cây đặt
chữ ở x=35 — thụt phải 7px, thấy rõ bằng mắt trên thanh rail.
Cách đo: render thanh rail ra ảnh rồi tìm cột mực đầu tiên, vì đó đúng là thứ
người dùng nhìn thấy. Trước khi đo, ba hàng được ép về **cùng một icon và cùng
một chữ** — chữ khác nhau thì phần nhô trái của glyph đầu tiên ("D" so với "G"
so với "C") đã lệch nhau vài pixel, và bài test sẽ đo hình dáng chữ chứ không
đo bố cục.
"""
from __future__ import annotations
import os
import pytest
pytest.importorskip("PySide6", reason="cần PySide6 để dựng cửa sổ thật")
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
#: Bỏ qua cột mực nằm sát mép trái: hàng đang được chọn có thêm vạch
#: ``border-left: 2px solid $accent`` (theme/qss.py), không phải icon.
_BO_QUA_MEP_TRAI = 5
@pytest.fixture(scope="module")
def window(qapp, tmp_path_factory):
"""Cửa sổ thật, có nạp stylesheet đúng như ``app.py`` làm.
Không nạp thì ``QTreeWidget::item { padding: 6px 10px }`` không áp, hàng của
cây thụt về 0 còn nút Cài đặt vẫn giữ lề 10px của layout — bài test sẽ đỏ vì
thiếu theme chứ không vì lỗi bố cục.
"""
from cowork_local.presentation.shell.bootstrap import build_config, build_context
from cowork_local.presentation.shell.main_window import MainWindow
from cowork_local.theme import set_active_theme, stylesheet
config_path = tmp_path_factory.mktemp("cfg") / "config.json"
build_config(config_path)
ctx = build_context(config_path)
css_cu = qapp.styleSheet()
set_active_theme(ctx.config.theme)
qapp.setStyleSheet(stylesheet(ctx.config.theme))
win = MainWindow(ctx)
win.resize(1280, 800)
win.show()
for _ in range(3):
qapp.processEvents()
yield win
win.close()
qapp.setStyleSheet(css_cu)
def _cum_muc(img, y0: int, y1: int):
"""Các cụm cột có mực trong dải ``[y0, y1)``, dạng ``[(x_đầu, x_cuối), ...]``.
Màu nền lấy ở cột sát mép phải cùng dòng y, nên hàng đang được tô nền chọn
vẫn so sánh đúng.
"""
w = img.width()
co_muc = [any(img.pixel(x, y) != img.pixel(w - 3, y) for y in range(y0, y1))
for x in range(w)]
cum, dau = [], None
for x, c in enumerate(co_muc):
if c and dau is None:
dau = x
elif not c and dau is not None:
if x - dau >= 2:
cum.append((dau, x - 1))
dau = None
if dau is not None:
cum.append((dau, w - 1))
return [c for c in cum if c[0] >= _BO_QUA_MEP_TRAI]
def _ep_ba_hang_ve_cung_hinh(window):
"""Cho ba hàng cùng icon và cùng chữ, để chỉ còn bố cục là khác biệt."""
from cowork_local.ui.icons import icon as _icon
for i in range(2):
it = window.nav_bottom.topLevelItem(i)
it.setIcon(0, _icon("gear"))
it.setText(0, "M")
window._nav_settings_icon.setPixmap(_icon("gear").pixmap(16, 16))
window._nav_settings_text.setText("M")
def _vi_tri_ba_hang(qapp, window):
"""``{tên hàng: (x_icon, x_chữ)}`` đo từ ảnh render của thanh rail."""
from PySide6.QtCore import QPoint
_ep_ba_hang_ve_cung_hinh(window)
for _ in range(3):
qapp.processEvents()
ref = window._nav_wrap
img = ref.grab().toImage()
ket = {}
for i, ten in ((0, "Dashboard"), (1, "Giám sát")):
it = window.nav_bottom.topLevelItem(i)
r = window.nav_bottom.visualItemRect(it)
y = window.nav_bottom.viewport().mapTo(ref, QPoint(0, r.y())).y()
cum = _cum_muc(img, y + 4, y + r.height() - 4)
assert len(cum) >= 2, f"{ten}: không tìm thấy đủ icon và chữ để đo"
ket[ten] = (cum[0][0], cum[1][0])
btn = window._nav_settings_btn
y = btn.mapTo(ref, QPoint(0, 0)).y()
cum = _cum_muc(img, y + 4, y + btn.height() - 4)
assert len(cum) >= 2, "Cài đặt: không tìm thấy đủ icon và chữ để đo"
ket["Cài đặt"] = (cum[0][0], cum[1][0])
return ket
def test_icon_ba_hang_thang_hang(qapp, window):
"""Icon của ba hàng phải bắt đầu ở cùng một cột."""
vi_tri = _vi_tri_ba_hang(qapp, window)
x = {ten: v[0] for ten, v in vi_tri.items()}
assert len(set(x.values())) == 1, f"icon lệch nhau: {x}"
def test_chu_ba_hang_thang_hang(qapp, window):
"""Chữ của ba hàng phải bắt đầu ở cùng một cột.
Đây là bài đỏ trước khi sửa: Cài đặt ở 42, hai hàng kia ở 35.
"""
vi_tri = _vi_tri_ba_hang(qapp, window)
x = {ten: v[1] for ten, v in vi_tri.items()}
assert len(set(x.values())) == 1, f"chữ lệch nhau: {x}"
+128
View File
@@ -0,0 +1,128 @@
"""Nút "Sửa project" phải sáng ngay khi đã có project đang mở.
Triệu chứng: mở app, chọn một project rồi vào sub-tab Project — biểu mẫu hiện
đủ tên, mô tả, thư mục làm việc, nhưng nút "Sửa project" vẫn mờ, không bấm được.
Nguyên nhân: trạng thái bật/mờ của ba nút chỉ được tính trong
``set_project_editable``, mà đổi project KHÔNG đi qua hàm đó — ``_load_current``
chỉ nạp lại biểu mẫu. ``_sync_project_buttons`` có chạy khi đổi sub-tab nhưng
ngày trước chỉ chỉnh ẩn/hiện, nên nút hiện ra mang theo trạng thái mờ tính từ
lúc dựng cửa sổ, khi chưa project nào được chọn.
Các bài dưới đây chốt cả bốn trạng thái: chưa có project → mờ; có project →
sáng; đi vòng qua sub-tab khác rồi quay lại → vẫn sáng; đang sửa dở → mờ lại
(nếu không thì "đang sửa" và "chưa sửa" trông giống hệt nhau).
Hai quy ước bắt buộc, lấy từ ``test_project_editing.py`` và
``test_project_gate_subtabs.py`` ngay cạnh:
* **Không tạo, không xoá project nào.** ``core/projects.py`` gắn
``PROJECTS_DIR`` vào ``~/.cowork_local`` THẬT, nên tạo project trong test là
ghi vào dữ liệu đang dùng của người chạy test. Trạng thái "đã có project"
được đặt thẳng vào ``_current_id`` — đúng biến mà ba nút đọc.
* **Một cửa sổ cho cả module.** Dựng ``MainWindow`` cho từng bài làm cả bộ
``tests/ui`` chết giữa chừng (Qt đổ stack trace, không phải test nào fail),
nên fixture ở đây là ``scope="module"`` và mỗi bài tự đặt trạng thái đầu vào
của mình.
"""
from __future__ import annotations
import pytest
pytest.importorskip("PySide6", reason="cần PySide6 để dựng cửa sổ thật")
_PID_GIA = "project-test-khong-ghi-dia"
@pytest.fixture(scope="module")
def ws(qapp, tmp_path_factory):
"""Màn Workspace của một MainWindow thật, dùng chung cho cả module."""
from cowork_local.presentation.shell.bootstrap import build_config, build_context
from cowork_local.presentation.shell.main_window import MainWindow
config_path = tmp_path_factory.mktemp("cfg") / "config.json"
build_config(config_path)
window = MainWindow(build_context(config_path))
yield window.workspace
window.close()
def _nap_project(qapp, ws, pid: str) -> None:
"""Đi đúng đường ``_load_current`` đi khi người dùng chọn một project.
Cố ý KHÔNG gọi ``set_project_editable``: chính vì ``_load_current`` không
gọi nó mà lỗi mới tồn tại. Gọi nó ở đây là bài test tự tay bật lại nút rồi
khẳng định nút đang bật — nó sẽ xanh cả trên bản chưa sửa.
``_project_editable`` đặt thẳng về ``False`` vì cửa sổ dùng chung cho cả
module: bài trước có thể đã để form ở chế độ sửa, mà nạp một project mới
thì form luôn ở chế độ chỉ-xem.
"""
ws.tabs.setCurrentIndex(ws._project_tab_idx)
ws._project_editable = False
ws._current_id = pid
ws.project_selected.emit(pid)
qapp.processEvents()
def _chua_co_project(qapp, ws) -> None:
"""Trạng thái chưa chọn project nào, đang ở sub-tab Project."""
_nap_project(qapp, ws, "")
def _mo_mot_project(qapp, ws) -> None:
"""Trạng thái đang mở một project."""
_nap_project(qapp, ws, _PID_GIA)
def test_chua_co_project_thi_nut_sua_mo(qapp, ws):
"""Chưa chọn project thì không có gì để sửa — đây là hành vi phải giữ."""
_chua_co_project(qapp, ws)
assert ws._edit_btn.isEnabled() is False
assert ws._edit_btn.isHidden() is True
def test_da_co_project_thi_nut_sua_sang(qapp, ws):
"""Bài đỏ trước khi sửa: nút hiện ra nhưng vẫn mờ."""
_mo_mot_project(qapp, ws)
assert ws._edit_btn.isHidden() is False, "nút phải hiện khi đã có project"
assert ws._edit_btn.isEnabled() is True, "nút phải bấm được khi đã có project"
def test_quay_lai_tab_project_thi_nut_van_sang(qapp, ws):
"""Đúng thao tác trong ảnh người dùng gửi: rời tab Project rồi quay lại."""
if ws.tabs.count() < 2:
pytest.skip("bản dựng này chỉ có một sub-tab, không đi vòng được")
_mo_mot_project(qapp, ws)
ws.tabs.setCurrentIndex(1 if ws._project_tab_idx == 0 else 0)
qapp.processEvents()
ws.tabs.setCurrentIndex(ws._project_tab_idx)
qapp.processEvents()
assert ws._edit_btn.isHidden() is False
assert ws._edit_btn.isEnabled() is True
def test_dang_sua_thi_nut_sua_mo_lai_va_nut_luu_sang(qapp, ws):
"""Chống sửa quá tay: "Sửa project" chỉ sáng khi CHƯA ở chế độ sửa."""
_mo_mot_project(qapp, ws)
ws.set_project_editable(True)
qapp.processEvents()
assert ws._edit_btn.isEnabled() is False
assert ws._save_btn.isEnabled() is True
assert ws._browse_btn.isEnabled() is True
def test_bo_chon_project_thi_nut_sua_mo_lai(qapp, ws):
"""Xoá project đang mở đưa ``_current_id`` về rỗng — nút phải mờ lại."""
_mo_mot_project(qapp, ws)
ws._current_id = ""
ws.project_selected.emit("")
qapp.processEvents()
assert ws._edit_btn.isEnabled() is False
assert ws._edit_btn.isHidden() is True
+223
View File
@@ -0,0 +1,223 @@
"""Mỗi thư mục làm việc chỉ được thuộc về MỘT project.
Thư mục làm việc vừa là sandbox (agent chỉ được đọc/ghi bên trong nó) vừa là
kho kiến thức chung của project (file ở gốc thư mục được mọi đoạn chat tự đọc).
Hai project trỏ vào cùng một thư mục là đọc lẫn dữ liệu của nhau và ghi đè lên
nhau — đúng điều mà docstring đầu ``core/projects.py`` nói sandbox sinh ra để
ngăn, nhưng trước đây không có gì chặn.
Hai nhóm bài:
* **Luật** — ``folder_conflict`` nhận diện trùng, kể cả khác hoa thường, khác
kiểu dấu phân cách, và LỒNG NHAU (đứng ở thư mục cha thì vẫn với tới được
file của project con).
* **Giao diện** — nhãn cảnh báo dưới ô "Thư mục làm việc" hiện đúng lúc, vì dữ
liệu cũ có thể đã trùng sẵn và luật mới chỉ chặn từ lúc chọn trở đi.
"""
from __future__ import annotations
import os
import pytest
from cowork_local.core import projects as projects_mod
from cowork_local.core.projects import Project, WORKSPACES_DIR, folder_conflict
def _kho(monkeypatch, *ds: Project) -> None:
"""Giả lập kho project, không đụng ``~/.cowork_local`` thật."""
monkeypatch.setattr(projects_mod, "list_projects", lambda directory=None: list(ds))
# ---- luật: nhận diện trùng ----------------------------------------------
def test_trung_y_het_thi_bi_bat(monkeypatch, tmp_path):
_kho(monkeypatch, Project(project_id="a", name="A", output_dir=str(tmp_path)))
khac = folder_conflict(str(tmp_path), ignore_id="b")
assert khac is not None and khac.project_id == "a"
def test_khac_hoa_thuong_va_dau_phan_cach_van_la_trung(monkeypatch, tmp_path):
"""Trên Windows ``D:/Work`` và ``d:/work`` là cùng một thư mục."""
_kho(monkeypatch, Project(project_id="a", name="A", output_dir=str(tmp_path)))
lech = str(tmp_path).replace(os.sep, "/")
if os.name == "nt":
lech = lech.upper()
assert folder_conflict(lech, ignore_id="b") is not None
def test_thu_muc_con_nam_trong_thu_muc_cua_project_khac_la_trung(monkeypatch, tmp_path):
"""Project kia đứng ở thư mục cha thì vẫn đọc/ghi được thư mục con này."""
_kho(monkeypatch, Project(project_id="a", name="A", output_dir=str(tmp_path)))
assert folder_conflict(str(tmp_path / "con"), ignore_id="b") is not None
def test_thu_muc_cha_chua_thu_muc_cua_project_khac_la_trung(monkeypatch, tmp_path):
"""Chiều ngược lại cũng phải bắt: chọn thư mục cha là ôm trọn project kia."""
_kho(monkeypatch, Project(project_id="a", name="A",
output_dir=str(tmp_path / "con")))
assert folder_conflict(str(tmp_path), ignore_id="b") is not None
def test_ten_na_na_nhung_khong_long_nhau_thi_khong_trung(monkeypatch, tmp_path):
"""Bẫy của so sánh tiền tố: ``work2`` KHÔNG nằm trong ``work``."""
_kho(monkeypatch, Project(project_id="a", name="A",
output_dir=str(tmp_path / "work")))
assert folder_conflict(str(tmp_path / "work2"), ignore_id="b") is None
def test_project_chua_dat_thu_muc_rieng_van_dang_chiem_thu_muc_quan_ly(monkeypatch):
"""``output_dir`` rỗng không có nghĩa là "chưa chiếm chỗ nào": project vẫn
đang dùng thư mục quản lý sẵn, và chính nó hay bị chọn nhầm."""
_kho(monkeypatch, Project(project_id="a", name="A", output_dir=""))
assert folder_conflict(str(WORKSPACES_DIR / "a"), ignore_id="b") is not None
def test_giu_nguyen_thu_muc_cua_chinh_no_thi_khong_phai_trung(monkeypatch, tmp_path):
_kho(monkeypatch, Project(project_id="a", name="A", output_dir=str(tmp_path)))
assert folder_conflict(str(tmp_path), ignore_id="a") is None
def test_thu_muc_chua_ai_dung_thi_di_qua(monkeypatch, tmp_path):
_kho(monkeypatch, Project(project_id="a", name="A",
output_dir=str(tmp_path / "cua-a")))
assert folder_conflict(str(tmp_path / "cua-b"), ignore_id="b") is None
def test_duong_dan_rong_khong_bi_coi_la_trung(monkeypatch, tmp_path):
"""Ô trống là "chưa chọn", không phải "trùng" — khác hẳn nhau."""
_kho(monkeypatch, Project(project_id="a", name="A", output_dir=str(tmp_path)))
assert folder_conflict("", ignore_id="b") is None
assert folder_conflict(" ", ignore_id="b") is None
# ---- i18n: ba key mới phải đủ ba ngôn ngữ -------------------------------
@pytest.mark.parametrize("key", [
"workspace.folder_taken_title", "workspace.folder_taken_body",
"workspace.folder_shared_warning",
])
def test_key_moi_co_du_ba_ngon_ngu(key):
from cowork_local import i18n
entry = i18n.STRINGS[key]
for lang in ("en", "ja", "vi"):
assert entry.get(lang), f"{key} thiếu {lang}"
# ---- giao diện: cảnh báo cho dữ liệu đã trùng sẵn -----------------------
pytest.importorskip("PySide6", reason="cần PySide6 để dựng cửa sổ thật")
@pytest.fixture(scope="module")
def ws(qapp, tmp_path_factory):
"""Một cửa sổ cho cả module — dựng nhiều MainWindow làm Qt chết giữa chừng."""
from cowork_local.presentation.shell.bootstrap import build_config, build_context
from cowork_local.presentation.shell.main_window import MainWindow
config_path = tmp_path_factory.mktemp("cfg") / "config.json"
build_config(config_path)
window = MainWindow(build_context(config_path))
yield window.workspace
window.close()
def _mo_project(qapp, ws, monkeypatch, dang_mo: Project, *nhung_cai_khac: Project):
"""Mở ``dang_mo`` trên biểu mẫu, với kho chứa cả các project còn lại."""
_kho(monkeypatch, dang_mo, *nhung_cai_khac)
monkeypatch.setattr(projects_mod, "load_project",
lambda pid, directory=None: dang_mo if pid == dang_mo.project_id else None)
ws._current_id = dang_mo.project_id
ws.project_selected.emit(dang_mo.project_id)
qapp.processEvents()
def test_canh_bao_hien_khi_project_dang_dung_chung_thu_muc(qapp, ws, monkeypatch, tmp_path):
"""Đúng trạng thái trong ảnh người dùng gửi: hai project cùng một thư mục."""
_mo_project(qapp, ws, monkeypatch,
Project(project_id="b", name="test3", output_dir=str(tmp_path)),
Project(project_id="a", name="test2", output_dir=str(tmp_path)))
assert ws._folder_warn_lbl.isHidden() is False
assert "test2" in ws._folder_warn_lbl.text()
def test_khong_canh_bao_khi_thu_muc_rieng(qapp, ws, monkeypatch, tmp_path):
_mo_project(qapp, ws, monkeypatch,
Project(project_id="b", name="test3", output_dir=str(tmp_path / "b")),
Project(project_id="a", name="test2", output_dir=str(tmp_path / "a")))
assert ws._folder_warn_lbl.isHidden() is True
def test_nhan_canh_bao_nam_ngay_duoi_o_thu_muc_lam_viec(ws):
"""Cảnh báo phải ở cạnh thứ nó nói tới, không rơi xuống cuối biểu mẫu."""
from cowork_local.presentation.workspace.project_editing import _row_layout_of
from cowork_local.presentation.workspace.project_folder_rules import _layout_chua
hang = _row_layout_of(ws.folder_lbl)
layout, vi_tri = _layout_chua(ws.folder_lbl.parentWidget().layout(), hang)
assert layout.itemAt(vi_tri + 1).widget() is ws._folder_warn_lbl
# ---- hành vi: chọn thư mục đã thuộc project khác thì KHÔNG được ghi ------
def test_chon_thu_muc_trung_thi_khong_ghi_gi(qapp, ws, monkeypatch, tmp_path):
"""Đây là cổng chặn thật, ở đúng nút "Đổi" mà người dùng bấm."""
from PySide6.QtWidgets import QFileDialog, QMessageBox
from cowork_local.ui import workspace_tab as wt
cua_toi = Project(project_id="b", name="test3", output_dir=str(tmp_path / "b"))
cua_nguoi_khac = Project(project_id="a", name="test2", output_dir=str(tmp_path / "a"))
_mo_project(qapp, ws, monkeypatch, cua_toi, cua_nguoi_khac)
da_ghi = []
monkeypatch.setattr(projects_mod, "save_project",
lambda project, directory=None: da_ghi.append(project))
da_bao = []
monkeypatch.setattr(QMessageBox, "warning",
staticmethod(lambda *a, **k: da_bao.append(a)))
# Người dùng chọn đúng thư mục của project kia.
monkeypatch.setattr(QFileDialog, "getExistingDirectory",
staticmethod(lambda *a, **k: str(tmp_path / "a")))
wt.WorkspaceTab._pick_folder(ws)
assert da_ghi == [], "đã ghi đè output_dir dù thư mục thuộc project khác"
assert cua_toi.output_dir == str(tmp_path / "b"), "thư mục cũ bị đổi mất"
assert da_bao, "chặn im lặng — người dùng không biết vì sao không đổi được"
def test_chon_thu_muc_tu_do_thi_van_doi_duoc(qapp, ws, monkeypatch, tmp_path):
"""Chặn một chiều là hỏng tính năng — thư mục chưa ai dùng phải đổi được."""
from PySide6.QtWidgets import QFileDialog
from cowork_local.ui import workspace_tab as wt
cua_toi = Project(project_id="b", name="test3", output_dir=str(tmp_path / "b"))
cua_nguoi_khac = Project(project_id="a", name="test2", output_dir=str(tmp_path / "a"))
_mo_project(qapp, ws, monkeypatch, cua_toi, cua_nguoi_khac)
da_ghi = []
monkeypatch.setattr(projects_mod, "save_project",
lambda project, directory=None: da_ghi.append(project))
monkeypatch.setattr(QFileDialog, "getExistingDirectory",
staticmethod(lambda *a, **k: str(tmp_path / "hoan-toan-moi")))
wt.WorkspaceTab._pick_folder(ws)
assert len(da_ghi) == 1
assert cua_toi.output_dir == str(tmp_path / "hoan-toan-moi")