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_object_governance.py · 7.7 KB · 170 lines
| 1 | """Governance introspection: the placement table live, and the workflow |
| 2 | diagram compiled from declarations. |
| 3 | |
| 4 | The claim under test is the one that separates this from every |
| 5 | hand-maintained workflow picture: because each kind of rule has exactly |
| 6 | one declared home, the diagram is COMPILED from the same declarations the |
| 7 | server enforces -- install a handler or add a transition and the next |
| 8 | render shows it, with no drawing to forget to update. |
| 9 | """ |
| 10 | |
| 11 | import pathlib |
| 12 | |
| 13 | from conftest import stage_collection |
| 14 | |
| 15 | import object_governance |
| 16 | |
| 17 | REPO_ROOT = pathlib.Path(__file__).resolve().parents[1] |
| 18 | PACKAGES = REPO_ROOT / "packages" |
| 19 | ROOTS = [PACKAGES / "app-payments" / "objects", PACKAGES / "app-invoices" / "objects"] |
| 20 | |
| 21 | |
| 22 | def setup_env(tmp_path, monkeypatch): |
| 23 | data_dir = tmp_path / "data" |
| 24 | for pkg, name in (("app-invoices", "invoices"), ("app-payments", "payments")): |
| 25 | stage_collection(data_dir, pkg, name) |
| 26 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 27 | return data_dir |
| 28 | |
| 29 | |
| 30 | def test_governs_answers_the_seven_layers_in_one_call(tmp_path, monkeypatch): |
| 31 | data_dir = setup_env(tmp_path, monkeypatch) |
| 32 | info = object_governance.governs("payments", base_dir=data_dir, roots=ROOTS) |
| 33 | |
| 34 | # gate: the schema names its hook, and the report says it fails closed |
| 35 | assert info["gates"][0]["object_id"] == "hook_payments" |
| 36 | assert "fails closed" in info["gates"][0]["note"] |
| 37 | # reactions: parsed from HANDLES source, never by importing the objects |
| 38 | reacting = {r["object_id"] for r in info["reactions"]} |
| 39 | assert {"system_books", "system_invoice_status"} <= reacting |
| 40 | # declarations: enums/relations/transitions from the schema itself |
| 41 | assert "invoice_id" in info["declarations"]["relations"] |
| 42 | assert info["declarations"]["relations"]["invoice_id"] == "invoices" |
| 43 | |
| 44 | |
| 45 | def test_governs_reads_transitions_with_their_guards(tmp_path, monkeypatch): |
| 46 | data_dir = setup_env(tmp_path, monkeypatch) |
| 47 | info = object_governance.governs("invoices", base_dir=data_dir, roots=ROOTS) |
| 48 | moves = info["declarations"]["transitions"]["status"] |
| 49 | targets = {t["to"] for t in moves["draft"]} |
| 50 | assert targets == {"sent", "void"} |
| 51 | # guards travel with the edge -- who may make the move is part of the map |
| 52 | assert moves["draft"][0]["when"] == {"owner_id": "$user_id"} |
| 53 | |
| 54 | |
| 55 | def test_handles_parser_accepts_the_real_source_shape(tmp_path, monkeypatch): |
| 56 | """books.py's HANDLES has a trailing comma -- legal Python, illegal |
| 57 | JSON, and exactly the kind of gap that silently empties an index.""" |
| 58 | setup_env(tmp_path, monkeypatch) |
| 59 | handlers = object_governance._handler_index(ROOTS) |
| 60 | by_id = {h["object_id"]: h for h in handlers} |
| 61 | assert "system_books" in by_id |
| 62 | events = {(e["collection"], e["action"]) for e in by_id["system_books"]["events"]} |
| 63 | assert ("invoices", "created") in events |
| 64 | assert ("payments", "updated") in events |
| 65 | |
| 66 | |
| 67 | def test_mermaid_compiles_states_guards_gates_and_reactions(tmp_path, monkeypatch): |
| 68 | data_dir = setup_env(tmp_path, monkeypatch) |
| 69 | source = object_governance.workflow_mermaid("payments", base_dir=data_dir, roots=ROOTS) |
| 70 | assert source.startswith("flowchart LR") |
| 71 | assert 'gate: hook_payments' in source |
| 72 | assert '-->|"on created"| system_books' in source |
| 73 | # a reaction node is declared once even when it fires on several actions |
| 74 | assert source.count('system_books[/"system_books"/]') == 1 |
| 75 | |
| 76 | invoices = object_governance.workflow_mermaid("invoices", base_dir=data_dir, roots=ROOTS) |
| 77 | assert 'invoices_draft -->|"owner_id=$user_id"| invoices_sent' in invoices |
| 78 | assert "system_invoice_portal_link" in invoices |
| 79 | |
| 80 | |
| 81 | def test_missing_layers_report_as_empty_not_as_errors(tmp_path, monkeypatch): |
| 82 | """A collection with no hooks simply has no gate rows -- that IS the |
| 83 | honest report, and a governance tool that raised on a plain collection |
| 84 | would never get used.""" |
| 85 | data_dir = tmp_path / "data" |
| 86 | stage_collection(data_dir, "app-links", "links") |
| 87 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 88 | info = object_governance.governs("links", base_dir=data_dir, roots=ROOTS) |
| 89 | assert info["gates"] == [] |
| 90 | assert info["reactions"] == [] |
| 91 | assert info["declarations"]["transitions"] == {} |
| 92 | source = object_governance.workflow_mermaid("links", base_dir=data_dir, roots=ROOTS) |
| 93 | assert "links" in source # renders a plain node, not a traceback |
| 94 | |
| 95 | |
| 96 | def test_unknown_collection_is_a_plain_empty_report(tmp_path, monkeypatch): |
| 97 | data_dir = tmp_path / "data" |
| 98 | data_dir.mkdir() |
| 99 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 100 | info = object_governance.governs("nothing_here", base_dir=data_dir, roots=ROOTS) |
| 101 | assert info["collection"] == "nothing_here" |
| 102 | assert info["gates"] == [] and info["views"] == [] |
| 103 | |
| 104 | |
| 105 | # --- the /flow page ----------------------------------------------------------- |
| 106 | |
| 107 | def _flow(payload): |
| 108 | import object_execution |
| 109 | import python_object_runtime |
| 110 | runtime = python_object_runtime.PythonObjectRuntime() |
| 111 | return object_execution.execute_object( |
| 112 | runtime, |
| 113 | object_execution.ObjectExecutionRequest("site_flow", method="GET", payload=payload), |
| 114 | roots=[PACKAGES / "app-views" / "objects"]).result |
| 115 | |
| 116 | |
| 117 | def test_flow_page_renders_the_compiled_picture(tmp_path, monkeypatch): |
| 118 | data_dir = setup_env(tmp_path, monkeypatch) |
| 119 | # The page compiles reactions from the SERVER's object roots (in |
| 120 | # production: the installed objects dir). Point the env at the package |
| 121 | # that carries system_books so the compiled picture has its reactions. |
| 122 | monkeypatch.setenv("DBBASIC_OBJECTS_DIR", str(PACKAGES / "app-payments" / "objects")) |
| 123 | anon = _flow({}) |
| 124 | assert "Sign in" in anon["body"] # the map is gated like the data |
| 125 | |
| 126 | index = _flow({"_identity": {"user_id": "dan"}}) |
| 127 | assert "/flow/invoices" in index["body"] |
| 128 | |
| 129 | detail = _flow({"_identity": {"user_id": "dan"}, "collection": "invoices"}) |
| 130 | body = detail["body"] |
| 131 | assert "<svg" in body # the drawn state machine |
| 132 | assert "owner_id = $user_id" in body # guards are readable, not hidden |
| 133 | assert "system_books" in body # reactions listed |
| 134 | assert "flowchart LR" in body # mermaid source for reuse |
| 135 | |
| 136 | |
| 137 | def test_flow_page_rejects_an_invalid_collection_name(tmp_path, monkeypatch): |
| 138 | setup_env(tmp_path, monkeypatch) |
| 139 | detail = _flow({"_identity": {"user_id": "dan"}, |
| 140 | "collection": "../../../etc/passwd"}) |
| 141 | # An unusable name falls back to the index rather than echoing anything. |
| 142 | assert "How things work" in detail["body"] |
| 143 | |
| 144 | |
| 145 | # --- the /urls page ------------------------------------------------------------- |
| 146 | |
| 147 | def test_urls_page_compiles_the_site_map(tmp_path, monkeypatch): |
| 148 | """A hand-written sitemap is wrong within a week; a compiled one cannot |
| 149 | be. Convention pages, routed patterns, view routes and the core API all |
| 150 | appear, and the anonymous gate holds -- the complete map of a server's |
| 151 | surface is reconnaissance data.""" |
| 152 | data_dir = setup_env(tmp_path, monkeypatch) |
| 153 | monkeypatch.setenv("DBBASIC_OBJECTS_DIR", |
| 154 | str(PACKAGES / "app-views" / "objects")) |
| 155 | import object_execution |
| 156 | import python_object_runtime |
| 157 | runtime = python_object_runtime.PythonObjectRuntime() |
| 158 | |
| 159 | def page(payload): |
| 160 | return object_execution.execute_object( |
| 161 | runtime, |
| 162 | object_execution.ObjectExecutionRequest("site_urls", method="GET", |
| 163 | payload=payload), |
| 164 | roots=[PACKAGES / "app-views" / "objects"]).result |
| 165 | |
| 166 | assert "Sign in" in page({})["body"] |
| 167 | body = page({"_identity": {"user_id": "dan"}})["body"] |
| 168 | assert "/webhooks/{name}" in body # core surface listed |
| 169 | assert "site_flow" in body # convention pages discovered |
| 170 | assert "convention" in body and "core" in body |