askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
test_app_forum_detail_retrofit.py · 4.7 KB · 117 lines
| 1 | """Stage-6 retrofit of packages/app-forum: the 338-line bespoke |
| 2 | site_forum_topic.py page is replaced by a seeded 59 detail view composed of |
| 3 | three GENERIC block renderers -- an editable+deletable `detail` block (the |
| 4 | topic), a `related` block (FLAT replies by topic_id -- deliberately flat, not |
| 5 | nested), and a `form` block with a FK locked to the page's topic (reply |
| 6 | compose). Flat, because nested discussion buries new replies and orphans |
| 7 | subtrees on delete; a "poll" over flat replies is a rollup, not a new feature. |
| 8 | """ |
| 9 | |
| 10 | import json |
| 11 | from pathlib import Path |
| 12 | |
| 13 | import object_packages |
| 14 | import object_schemas |
| 15 | |
| 16 | PACKAGES_ROOT = Path(__file__).resolve().parents[1] / "packages" |
| 17 | APP_FORUM_DIR = PACKAGES_ROOT / "app-forum" |
| 18 | |
| 19 | |
| 20 | def _seed_rows(name): |
| 21 | import csv |
| 22 | |
| 23 | with open(APP_FORUM_DIR / "seed" / f"{name}.tsv", newline="") as fh: |
| 24 | return list(csv.DictReader(fh, delimiter="\t")) |
| 25 | |
| 26 | |
| 27 | def test_manifest_drops_bespoke_topic_page_and_seeds_the_detail_view(): |
| 28 | package = object_packages.get_package("app-forum", root=PACKAGES_ROOT) |
| 29 | assert {obj["id"] for obj in package["objects"]} == {"site_forum"} |
| 30 | assert {entry["collection"] for entry in package["seed"]} == { |
| 31 | "forum_categories", "forum_topics", "forum_replies", "views", "site_routes", |
| 32 | } |
| 33 | |
| 34 | |
| 35 | def test_forum_topic_object_file_is_deleted(): |
| 36 | assert not (APP_FORUM_DIR / "objects" / "site" / "forum_topic.py").exists() |
| 37 | |
| 38 | |
| 39 | def test_topic_view_composes_detail_flat_related_and_fk_locked_form(): |
| 40 | rows = _seed_rows("views") |
| 41 | assert len(rows) == 1 |
| 42 | view = rows[0] |
| 43 | assert view["id"] == "view_forum_topic" |
| 44 | assert view["route"] == "/forum/topics/{topic_id}" |
| 45 | blocks = json.loads(view["blocks"]) |
| 46 | kinds = [b["kind"] for b in blocks] |
| 47 | assert kinds == ["detail", "related", "form"] |
| 48 | |
| 49 | detail, related, form = blocks |
| 50 | # 1. editable+deletable topic detail |
| 51 | assert detail["collection"] == "forum_topics" |
| 52 | assert detail["record_id"] == "$record_id" |
| 53 | assert detail["editable"] is True and detail["deletable"] is True |
| 54 | |
| 55 | # 2. FLAT replies (a plain related list by topic_id -- no nesting) |
| 56 | assert related["collection"] == "forum_replies" |
| 57 | assert related["fk_field"] == "topic_id" |
| 58 | assert related["match"] == "$record_id" |
| 59 | |
| 60 | # 3. reply compose with topic_id locked to the page's topic |
| 61 | assert form["collection"] == "forum_replies" |
| 62 | assert form["fixed"] == {"topic_id": "$record_id"} |
| 63 | |
| 64 | |
| 65 | def test_permalink_route_points_to_view_render(): |
| 66 | rows = _seed_rows("site_routes") |
| 67 | assert len(rows) == 1 |
| 68 | assert rows[0]["pattern"] == "/forum/topics/{topic_id}" |
| 69 | assert rows[0]["object_id"] == "site_view_render" |
| 70 | |
| 71 | |
| 72 | def test_ai_summary_is_conditionally_visible_via_visible_when(): |
| 73 | """The orphaned ai_summary field now shows on the topic detail, but only |
| 74 | when is_ai_summarized -- via the field's own visible_when, not page code.""" |
| 75 | schema = json.loads((APP_FORUM_DIR / "schemas" / "forum_topics.json").read_text()) |
| 76 | ai = next(f for f in schema["fields"] if f["name"] == "ai_summary") |
| 77 | assert ai["visible_when"] == {"field": "is_ai_summarized", "equals": "true"} |
| 78 | |
| 79 | |
| 80 | def test_reply_compose_form_is_flat_no_parent_id(): |
| 81 | """Flat replies: parent_id is dropped from the compose form's |
| 82 | forms.default, so no one composes a nested reply. The field itself stays |
| 83 | on the schema (existing data), it is just never offered.""" |
| 84 | schema = json.loads((APP_FORUM_DIR / "schemas" / "forum_replies.json").read_text()) |
| 85 | assert schema["forms"]["default"]["fields"] == ["topic_id", "content"] |
| 86 | assert "parent_id" in {f["name"] for f in schema["fields"]} |
| 87 | |
| 88 | |
| 89 | def test_permissions_no_longer_reference_the_removed_object(): |
| 90 | payload = json.loads((APP_FORUM_DIR / "permissions" / "rules.json").read_text()) |
| 91 | object_ids = {rule.get("object_id") for rule in payload["rules"]} |
| 92 | assert "site_forum_topic" not in object_ids |
| 93 | assert "site_forum" in object_ids |
| 94 | |
| 95 | |
| 96 | def test_dry_run_is_safe(tmp_path): |
| 97 | object_root = tmp_path / "objects" |
| 98 | object_root.mkdir() |
| 99 | plan = object_packages.dry_run_package( |
| 100 | "app-forum", root=PACKAGES_ROOT, base_dir=tmp_path / "data", object_roots=[object_root], |
| 101 | ) |
| 102 | assert plan["safe_to_install"] is True |
| 103 | assert plan["warnings"] == [] |
| 104 | |
| 105 | |
| 106 | def test_visible_when_survives_install(tmp_path): |
| 107 | """The condition must survive normalization/install -- else non-summarized |
| 108 | topics would show a blank AI Summary row.""" |
| 109 | data_dir = tmp_path / "data" |
| 110 | object_root = tmp_path / "objects" |
| 111 | object_root.mkdir() |
| 112 | object_packages.install_package( |
| 113 | "app-forum", root=PACKAGES_ROOT, base_dir=data_dir, object_roots=[object_root], |
| 114 | ) |
| 115 | schema = object_schemas.get_schema("forum_topics", base_dir=data_dir) |
| 116 | ai = next(f for f in schema["fields"] if f["name"] == "ai_summary") |
| 117 | assert ai.get("visible_when") == {"field": "is_ai_summarized", "equals": "true"} |