391 lines
13 KiB
Python
Raw Normal View History

# retoor <retoor@molodetz.nl>
import time
import pytest
import requests
from tests.conftest import BASE_URL
from devplacepy.database import init_db, get_table
from devplacepy import project_files as pf
from devplacepy.project_files import ProjectFileError
@pytest.fixture(autouse=True)
def _init_db_project_file_lines():
init_db()
yield
_pid = [0]
def _project():
_pid[0] += 1
pid = f"plines-{_pid[0]}"
user = {"uid": f"plines-owner-{_pid[0]}"}
return pid, user
_counter_project_file_lines = [0]
def _signup_project_file_lines():
_counter_project_file_lines[0] += 1
name = f"pl{int(time.time() * 1000)}{_counter_project_file_lines[0]}"
requests.post(
f"{BASE_URL}/auth/signup",
data={
"username": name,
"email": f"{name}@t.dev",
"password": "secret123",
"confirm_password": "secret123",
Add the trust and safety subsystem and the App Store compliance work Implements the moderation and consent obligations a social platform carries, so the web version and any client that speaks to it enforce the same rules. Moderation core (services/moderation/, database/moderation.py): a reportable target registry, the content filter and its choke points, the report queue with atomic resolution, enforcement actions, consent tracking, maturity gating, and account deletion with a grace window. Surfaces: POST /reports plus the member report list, /admin/moderation and the per-report admin view, /workspaces, terms acceptance at /auth/terms, consent and account deletion under /profile, the report button and dialog partials, the maturity gate, and the moderation stylesheet and ReportDialog client. Every user-generated surface stays reportable by construction: new content tables are registered in REPORTABLE_TARGETS or listed in UNREPORTABLE_TABLES with a reason, and the registry test fails the suite on anything left unclassified. Docs: community guidelines, content moderation, intellectual property, privacy, terms, contact, and the admin-only moderation operations page, plus the moderation API group and the Devii moderation actions. Compliance record: applecomp.md is the requirement register, applechanges.md the gap analysis against this codebase, and appleimpl.md the implementation design they resolve to. Tests cover the report flow, admin moderation, consent, account deletion, terms acceptance, workspaces, and the registry invariant across the unit, api, and e2e tiers.
2026-08-09 00:18:20 +02:00
"birth_date": "1990-01-01",
"accept_terms": "1",
},
allow_redirects=True,
)
return name, get_table("users").find_one(username=name)["api_key"]
def _h_project_file_lines(key):
return {"X-API-KEY": key, "Accept": "application/json"}
def _create_project_project_file_lines(key, title):
r = requests.post(
f"{BASE_URL}/projects/create",
headers=_h_project_file_lines(key),
data={
"title": title,
"description": "lines test",
"project_type": "software",
"status": "In Development",
},
)
assert r.status_code == 200, r.text
return r.json()["data"]
def _write_project_file_lines(key, slug, path, content):
r = requests.post(
f"{BASE_URL}/projects/{slug}/files/write",
headers=_h_project_file_lines(key),
data={"path": path, "content": content},
allow_redirects=False,
)
assert r.status_code in (200, 302), r.text
def _raw_project_file_lines(key, slug, path):
return requests.get(
f"{BASE_URL}/projects/{slug}/files/raw", headers=_h_project_file_lines(key), params={"path": path}
).json()
class _FakeResponse:
def __init__(self, status_code=200):
self.status_code = status_code
class _FakeClient:
authenticated = True
username = "u"
def __init__(self):
self.calls = []
async def call(
self, method, path, params=None, data=None, file_field=None, headers=None
):
self.calls.append((method, path))
return _FakeResponse()
def _make_dispatcher():
import devplacepy.services.devii.actions.dispatcher as disp
from devplacepy.services.devii.actions.catalog import PLATFORM_CATALOG
d = disp.Dispatcher.__new__(disp.Dispatcher)
d._actions = PLATFORM_CATALOG.by_name()
d._client = _FakeClient()
d._read_files = set()
return disp, d
from playwright.sync_api import expect
from devplacepy.database import get_table
_counter_project_files = [0]
def _signup_project_files():
_counter_project_files[0] += 1
name = f"pf{int(time.time() * 1000)}{_counter_project_files[0]}"
session = requests.Session()
session.post(
f"{BASE_URL}/auth/signup",
data={
"username": name,
"email": f"{name}@t.dev",
"password": "secret123",
"confirm_password": "secret123",
Add the trust and safety subsystem and the App Store compliance work Implements the moderation and consent obligations a social platform carries, so the web version and any client that speaks to it enforce the same rules. Moderation core (services/moderation/, database/moderation.py): a reportable target registry, the content filter and its choke points, the report queue with atomic resolution, enforcement actions, consent tracking, maturity gating, and account deletion with a grace window. Surfaces: POST /reports plus the member report list, /admin/moderation and the per-report admin view, /workspaces, terms acceptance at /auth/terms, consent and account deletion under /profile, the report button and dialog partials, the maturity gate, and the moderation stylesheet and ReportDialog client. Every user-generated surface stays reportable by construction: new content tables are registered in REPORTABLE_TARGETS or listed in UNREPORTABLE_TABLES with a reason, and the registry test fails the suite on anything left unclassified. Docs: community guidelines, content moderation, intellectual property, privacy, terms, contact, and the admin-only moderation operations page, plus the moderation API group and the Devii moderation actions. Compliance record: applecomp.md is the requirement register, applechanges.md the gap analysis against this codebase, and appleimpl.md the implementation design they resolve to. Tests cover the report flow, admin moderation, consent, account deletion, terms acceptance, workspaces, and the registry invariant across the unit, api, and e2e tiers.
2026-08-09 00:18:20 +02:00
"birth_date": "1990-01-01",
"accept_terms": "1",
},
allow_redirects=True,
)
key = get_table("users").find_one(username=name)["api_key"]
return name, key
def _h_project_files(key):
return {"X-API-KEY": key, "Accept": "application/json"}
def _create_project_project_files(key, title):
r = requests.post(
f"{BASE_URL}/projects/create",
headers=_h_project_files(key),
data={
"title": title,
"description": "filesystem test",
"project_type": "software",
"status": "In Development",
},
)
assert r.status_code == 200, r.text
return r.json()["data"]
def _write_project_files(key, slug, path, content):
return requests.post(
f"{BASE_URL}/projects/{slug}/files/write",
headers=_h_project_files(key),
data={"path": path, "content": content},
allow_redirects=False,
)
def _mkdir(key, slug, path):
return requests.post(
f"{BASE_URL}/projects/{slug}/files/mkdir",
headers=_h_project_files(key),
data={"path": path},
allow_redirects=False,
)
def _move(key, slug, from_path, to_path):
return requests.post(
f"{BASE_URL}/projects/{slug}/files/move",
headers=_h_project_files(key),
data={"from_path": from_path, "to_path": to_path},
allow_redirects=False,
)
def _delete(key, slug, path):
return requests.post(
f"{BASE_URL}/projects/{slug}/files/delete",
headers=_h_project_files(key),
data={"path": path},
allow_redirects=False,
)
def _list(slug, key=None):
return requests.get(
f"{BASE_URL}/projects/{slug}/files",
headers=_h_project_files(key) if key else {"Accept": "application/json"},
)
def _raw_project_files(slug, path, key=None):
return requests.get(
f"{BASE_URL}/projects/{slug}/files/raw",
params={"path": path},
headers=_h_project_files(key) if key else {"Accept": "application/json"},
)
def _paths(slug, key=None):
return sorted(f["path"] for f in _list(slug, key).json()["files"])
def _make_project_ui(page, title):
page.goto(f"{BASE_URL}/projects", wait_until="domcontentloaded")
page.locator("#create-project-btn").click()
page.fill("#title", title)
page.fill("#description", "Project for filesystem UI test")
page.click("button:has-text('Create Project')")
page.wait_for_url(f"{BASE_URL}/projects/*", wait_until="domcontentloaded")
return page.url
def _open_files(page, title):
proj_url = _make_project_ui(page, title)
slug = proj_url.rstrip("/").split("/")[-1]
page.goto(proj_url + "/files", wait_until="domcontentloaded")
return slug
def _dialog_fill(page, value):
page.locator(".dialog-overlay.visible .dialog-input").wait_for(state="visible")
page.fill(".dialog-overlay.visible .dialog-input", value)
page.click(".dialog-overlay.visible .dialog-confirm")
def _dialog_confirm(page):
page.locator(".dialog-overlay.visible .dialog-confirm").wait_for(state="visible")
page.click(".dialog-overlay.visible .dialog-confirm")
def _dialog_cancel(page):
page.locator(".dialog-overlay.visible .dialog-cancel").wait_for(state="visible")
page.click(".dialog-overlay.visible .dialog-cancel")
def _new_folder(page, name):
page.click("#pf-new-folder")
_dialog_fill(page, name)
page.wait_for_selector(f".pf-node-row:has-text('{name.split('/')[-1]}')")
def _new_file(page, name):
page.click("#pf-new-file")
_dialog_fill(page, name)
def _row(page, name):
return page.locator(f".pf-node-row:has-text('{name}')").first
def _alice_key():
return get_table("users").find_one(username="alice_test")["api_key"]
def test_read_lines_range_and_total():
pid, u = _project()
pf.write_text_file(pid, u, "f.txt", "a\nb\nc\nd")
out = pf.read_lines(pid, "f.txt", 2, 3)
assert out["lines"] == ["b", "c"]
assert out["total_lines"] == 4
assert out["content"] == "b\nc"
def test_read_lines_open_end_clamps():
pid, u = _project()
pf.write_text_file(pid, u, "f.txt", "a\nb\nc")
out = pf.read_lines(pid, "f.txt", 2, None)
assert out["lines"] == ["b", "c"]
assert out["end"] == 3
def test_replace_lines_middle():
pid, u = _project()
pf.write_text_file(pid, u, "f.txt", "a\nb\nc\nd")
pf.replace_lines(pid, "f.txt", 2, 3, "X\nY\nZ")
assert pf.read_file(pid, "f.txt")["content"] == "a\nX\nY\nZ\nd"
def test_replace_lines_empty_content_deletes():
pid, u = _project()
pf.write_text_file(pid, u, "f.txt", "a\nb\nc")
pf.replace_lines(pid, "f.txt", 2, 2, "")
assert pf.read_file(pid, "f.txt")["content"] == "a\nc"
def test_insert_lines_prepend_and_append_positions():
pid, u = _project()
pf.write_text_file(pid, u, "f.txt", "a\nb")
pf.insert_lines(pid, "f.txt", 1, "TOP")
pf.insert_lines(pid, "f.txt", 99, "BOTTOM")
assert pf.read_file(pid, "f.txt")["content"] == "TOP\na\nb\nBOTTOM"
def test_delete_lines_range():
pid, u = _project()
pf.write_text_file(pid, u, "f.txt", "a\nb\nc\nd")
pf.delete_lines(pid, "f.txt", 2, 3)
assert pf.read_file(pid, "f.txt")["content"] == "a\nd"
def test_append_preserves_trailing_newline():
pid, u = _project()
pf.write_text_file(pid, u, "g.txt", "one\ntwo\n")
pf.append_lines(pid, "g.txt", "three")
assert pf.read_file(pid, "g.txt")["content"] == "one\ntwo\nthree\n"
def test_append_without_trailing_adds_newline_between():
pid, u = _project()
pf.write_text_file(pid, u, "g.txt", "one")
pf.append_lines(pid, "g.txt", "two")
assert pf.read_file(pid, "g.txt")["content"] == "one\ntwo"
def test_append_honors_content_trailing_newline():
pid, u = _project()
pf.write_text_file(pid, u, "g.txt", "a\nb")
pf.append_lines(pid, "g.txt", "c\nd\n")
assert pf.read_file(pid, "g.txt")["content"] == "a\nb\nc\nd\n"
def test_replace_last_line_honors_content_trailing_newline():
pid, u = _project()
pf.write_text_file(pid, u, "f.txt", "a\nb\nc")
pf.replace_lines(pid, "f.txt", 3, 3, "C\n")
assert pf.read_file(pid, "f.txt")["content"] == "a\nb\nC\n"
def test_insert_at_end_honors_content_trailing_newline():
pid, u = _project()
pf.write_text_file(pid, u, "f.txt", "a\nb")
pf.insert_lines(pid, "f.txt", 3, "c\n")
assert pf.read_file(pid, "f.txt")["content"] == "a\nb\nc\n"
def test_interior_replace_does_not_add_trailing_newline():
pid, u = _project()
pf.write_text_file(pid, u, "f.txt", "a\nb\nc\nd")
pf.replace_lines(pid, "f.txt", 2, 2, "X\n")
assert pf.read_file(pid, "f.txt")["content"] == "a\nX\nc\nd"
def test_line_ops_reject_missing_dir_and_binary():
pid, u = _project()
pf.make_dir(pid, u, "adir")
pf.store_upload(pid, u, "", "blob.bin", bytes(range(64)))
with pytest.raises(ProjectFileError):
pf.read_lines(pid, "nope.txt")
with pytest.raises(ProjectFileError):
pf.replace_lines(pid, "adir", 1, 1, "x")
with pytest.raises(ProjectFileError):
pf.append_lines(pid, "blob.bin", "x")
def test_replace_out_of_range_raises():
pid, u = _project()
pf.write_text_file(pid, u, "f.txt", "a\nb")
with pytest.raises(ProjectFileError):
pf.replace_lines(pid, "f.txt", 9, 9, "x")
def test_append_respects_max_chars():
pid, u = _project()
pf.write_text_file(pid, u, "f.txt", "x")
with pytest.raises(ProjectFileError):
pf.append_lines(pid, "f.txt", "y" * (pf.MAX_TEXT_CHARS + 10))
def test_normalize_blocks_traversal_in_line_ops():
pid, u = _project()
with pytest.raises(ProjectFileError):
pf.read_lines(pid, "../escape.txt")
def test_write_blocked_until_read(monkeypatch):
from tests.conftest import run_async
disp, d = _make_dispatcher()
monkeypatch.setattr(disp, "format_response", lambda r: "ok")
monkeypatch.setattr(disp, "record_mutation", lambda name: None)
monkeypatch.setattr(disp, "get_store", lambda: None)
actions = d._actions
write = actions["project_write_file"]
read = actions["project_read_file"]
args = {"project_slug": "p", "path": "src/app.py", "content": "x"}
from devplacepy.services.devii.errors import ToolInputError
with pytest.raises(ToolInputError):
run_async(d._run_http(write, args))
assert d._client.calls == [
("GET", "/projects/p/files/raw")
], "write blocked before read should only trigger a file-exists check"
run_async(d._run_http(read, {"project_slug": "p", "path": "src/app.py"}))
run_async(d._run_http(write, args))
assert ("POST", "/projects/p/files/write") in d._client.calls
def test_guard_path_normalization_matches(monkeypatch):
from tests.conftest import run_async
disp, d = _make_dispatcher()
monkeypatch.setattr(disp, "format_response", lambda r: "ok")
monkeypatch.setattr(disp, "record_mutation", lambda name: None)
monkeypatch.setattr(disp, "get_store", lambda: None)
actions = d._actions
# read with a messy path, write with the clean path - normalization should unify them
run_async(
d._run_http(
actions["project_read_file"], {"project_slug": "p", "path": "/src//app.py"}
)
)
run_async(
d._run_http(
actions["project_write_file"],
{"project_slug": "p", "path": "src/app.py", "content": "x"},
)
)
assert ("POST", "/projects/p/files/write") in d._client.calls
def test_devii_actions_registered():
from devplacepy.services.devii.actions.catalog import ACTIONS
names = {a.name for a in ACTIONS}
assert {
"project_list_files",
"project_read_file",
"project_write_file",
"project_upload_file",
"project_make_dir",
"project_move_file",
"project_delete_file",
} <= names
def test_docs_group_present():
from devplacepy import docs_api
group = next((g for g in docs_api.API_GROUPS if g["slug"] == "project-files"), None)
assert group is not None
ids = {e["id"] for e in group["endpoints"]}
assert "project-files-write" in ids and "project-files-list" in ids