Files
cowork-local/tests/unit/test_workspace_session.py
T
f9f6bc01fd
CI / test (push) Canceled after 0s
Feature/delta team/epic r04 (#7)
## Summary

epic r04 - begin refactor

## Change Type

- [x] Cowork feature
- [ ] Bug fix
- [ ] Core AI contribution
- [ ] Test / hardening
- [ ] 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?

What is intentionally NOT included?

## Validation

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

Commands / evidence:

## Security Impact

Permission / credential / network / customer data impact:

## Compatibility

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

## Reviewer Notes

Anything Cowork reviewers should pay attention to.

---------

Co-authored-by: Anh Tran Nguyen Minh <anhtnm1@fpt.com>
Co-authored-by: Huong Le Thi Thien <huongltt35@fpt.com>
Co-authored-by: Nam Pham Dinh Thanh <nampdt@fpt.com>
Co-authored-by: Vu Dam Tuan <vudt15@fpt.com>
Co-authored-by: Hiep Ha Van <hiephv3@fpt.com>
Co-authored-by: Lam Hoang Van <lamhv7@fpt.com>
Reviewed-on: #7
Co-authored-by: Duy Le Huu <duylh19@fpt.com>
2026-08-31 05:15:13 +00:00

65 lines
2.3 KiB
Python

"""EPIC R06-T01: WorkspaceSession is a frozen snapshot, captured once, that a
turn keeps using regardless of what the UI does to the live project
selection afterwards - see the module docstring for the race this replaces.
"""
from __future__ import annotations
from pathlib import Path
import pytest
from cowork_local.domain.workspaces import WorkspaceSession
class _FakeProject:
def __init__(self, project_id: str, root: Path):
self.project_id = project_id
self._root = root
def workspace_dir(self) -> Path:
return self._root
def test_from_project_derives_sandbox_dir_under_the_workspace_root(tmp_path):
project = _FakeProject("proj-a", tmp_path)
session = WorkspaceSession.from_project(project)
assert session.project_id == "proj-a"
assert session.workspace_root == tmp_path
assert session.sandbox_dir == tmp_path / ".scratch"
assert session.allowed_paths == (tmp_path,)
def test_is_allowed_true_for_the_root_and_descendants(tmp_path):
session = WorkspaceSession.from_project(_FakeProject("p", tmp_path))
nested = tmp_path / "sub" / "file.txt"
nested.parent.mkdir(parents=True)
nested.write_text("x", encoding="utf-8")
assert session.is_allowed(tmp_path) is True
assert session.is_allowed(nested) is True
def test_is_allowed_false_outside_the_workspace(tmp_path):
session = WorkspaceSession.from_project(_FakeProject("p", tmp_path / "a"))
outside = tmp_path / "b" / "secret.txt"
assert session.is_allowed(outside) is False
def test_two_sessions_from_different_projects_stay_independent(tmp_path):
"""The exact race this snapshot exists to prevent: a turn holding session
A must never start accepting paths that belong to session B, no matter
what the (mutable, shared) AppContext does after the snapshot was taken."""
session_a = WorkspaceSession.from_project(_FakeProject("a", tmp_path / "a"))
session_b = WorkspaceSession.from_project(_FakeProject("b", tmp_path / "b"))
assert session_a.is_allowed(tmp_path / "b" / "file.txt") is False
assert session_b.is_allowed(tmp_path / "a" / "file.txt") is False
def test_unscoped_session_has_no_project_id(tmp_path):
session = WorkspaceSession.unscoped(tmp_path)
assert session.project_id == ""
assert session.is_allowed(tmp_path / "code.py") is True