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_activity.py · 7.1 KB · 189 lines
| 1 | """Tests for object_activity: the activity feed as a fold over the existing |
| 2 | record-change ledger written by object_records.py (see object_activity.py's |
| 3 | module docstring). No new storage: these tests write through |
| 4 | object_records.create/update/delete_collection_record with different |
| 5 | actors and assert recent_activity() reads that same ledger back out. |
| 6 | """ |
| 7 | |
| 8 | from pathlib import Path |
| 9 | |
| 10 | import object_activity |
| 11 | import object_records |
| 12 | |
| 13 | |
| 14 | def write_records(data_dir: Path, collection: str, content: str) -> Path: |
| 15 | path = data_dir / "collections" / collection / "records.tsv" |
| 16 | path.parent.mkdir(parents=True, exist_ok=True) |
| 17 | path.write_text(content) |
| 18 | return path |
| 19 | |
| 20 | |
| 21 | def test_recent_activity_scopes_to_one_actor_newest_first_with_titles(tmp_path): |
| 22 | data_dir = tmp_path / "data" |
| 23 | write_records(data_dir, "notes", "id\ttitle\n") |
| 24 | write_records(data_dir, "tasks", "id\ttitle\n") |
| 25 | |
| 26 | object_records.create_collection_record( |
| 27 | "notes", {"id": "n1", "title": "Alice's first note"}, |
| 28 | base_dir=data_dir, roots=[], actor="alice", |
| 29 | ) |
| 30 | object_records.create_collection_record( |
| 31 | "tasks", {"id": "t1", "title": "Bob's task"}, |
| 32 | base_dir=data_dir, roots=[], actor="bob", |
| 33 | ) |
| 34 | object_records.update_collection_record( |
| 35 | "notes", "n1", {"title": "Alice's edited note"}, |
| 36 | base_dir=data_dir, roots=[], actor="alice", |
| 37 | ) |
| 38 | |
| 39 | feed = object_activity.recent_activity(base_dir=data_dir, actor="alice") |
| 40 | |
| 41 | assert [entry["action"] for entry in feed] == ["update", "create"] |
| 42 | assert {entry["actor"] for entry in feed} == {"alice"} |
| 43 | assert feed[0]["title"] == "Alice's edited note" |
| 44 | assert feed[0]["collection"] == "notes" |
| 45 | assert feed[0]["record_id"] == "n1" |
| 46 | assert feed[1]["title"] == "Alice's first note" |
| 47 | # A feed is a signal, not a data dump: no full snapshot leaks through. |
| 48 | assert "before" not in feed[0] and "after" not in feed[0] |
| 49 | |
| 50 | |
| 51 | def test_recent_activity_merges_across_collections_newest_first(tmp_path): |
| 52 | data_dir = tmp_path / "data" |
| 53 | write_records(data_dir, "notes", "id\ttitle\n") |
| 54 | write_records(data_dir, "tasks", "id\ttitle\n") |
| 55 | |
| 56 | object_records.create_collection_record( |
| 57 | "notes", {"id": "n1", "title": "First"}, base_dir=data_dir, roots=[], actor="alice", |
| 58 | ) |
| 59 | object_records.create_collection_record( |
| 60 | "tasks", {"id": "t1", "title": "Second"}, base_dir=data_dir, roots=[], actor="alice", |
| 61 | ) |
| 62 | |
| 63 | feed = object_activity.recent_activity(base_dir=data_dir, actor="alice") |
| 64 | |
| 65 | assert [entry["collection"] for entry in feed] == ["tasks", "notes"] |
| 66 | |
| 67 | |
| 68 | def test_recent_activity_title_falls_back_through_field_order(tmp_path): |
| 69 | data_dir = tmp_path / "data" |
| 70 | write_records(data_dir, "contacts", "id\tname\n") |
| 71 | write_records(data_dir, "orders", "id\tnumber\n") |
| 72 | write_records(data_dir, "widgets", "id\n") |
| 73 | |
| 74 | object_records.create_collection_record( |
| 75 | "contacts", {"id": "c1", "name": "Ada"}, base_dir=data_dir, roots=[], actor="alice", |
| 76 | ) |
| 77 | object_records.create_collection_record( |
| 78 | "orders", {"id": "o1", "number": "PO-1"}, base_dir=data_dir, roots=[], actor="alice", |
| 79 | ) |
| 80 | object_records.create_collection_record( |
| 81 | "widgets", {"id": "w1"}, base_dir=data_dir, roots=[], actor="alice", |
| 82 | ) |
| 83 | |
| 84 | feed = object_activity.recent_activity(base_dir=data_dir, actor="alice") |
| 85 | titles_by_collection = {entry["collection"]: entry["title"] for entry in feed} |
| 86 | |
| 87 | assert titles_by_collection["contacts"] == "Ada" |
| 88 | assert titles_by_collection["orders"] == "PO-1" |
| 89 | # No title/name/number/subject field on the snapshot -> falls back to id. |
| 90 | assert titles_by_collection["widgets"] == "w1" |
| 91 | |
| 92 | |
| 93 | def test_recent_activity_title_falls_back_to_before_snapshot_on_delete(tmp_path): |
| 94 | data_dir = tmp_path / "data" |
| 95 | write_records(data_dir, "notes", "id\ttitle\nn1\tDoomed note\n") |
| 96 | |
| 97 | object_records.delete_collection_record( |
| 98 | "notes", "n1", base_dir=data_dir, roots=[], actor="alice", |
| 99 | ) |
| 100 | |
| 101 | feed = object_activity.recent_activity(base_dir=data_dir, actor="alice") |
| 102 | |
| 103 | assert feed[0]["action"] == "delete" |
| 104 | assert feed[0]["title"] == "Doomed note" |
| 105 | |
| 106 | |
| 107 | def test_recent_activity_excludes_shell_commands_and_ai_usage(tmp_path): |
| 108 | data_dir = tmp_path / "data" |
| 109 | write_records(data_dir, "notes", "id\ttitle\n") |
| 110 | write_records(data_dir, "shell_commands", "id\tcommand\n") |
| 111 | write_records(data_dir, "ai_usage", "id\ttokens\n") |
| 112 | |
| 113 | object_records.create_collection_record( |
| 114 | "notes", {"id": "n1", "title": "A note"}, base_dir=data_dir, roots=[], actor="alice", |
| 115 | ) |
| 116 | object_records.create_collection_record( |
| 117 | "shell_commands", {"id": "s1", "command": "ls"}, base_dir=data_dir, roots=[], actor="alice", |
| 118 | ) |
| 119 | object_records.create_collection_record( |
| 120 | "ai_usage", {"id": "u1", "tokens": "100"}, base_dir=data_dir, roots=[], actor="alice", |
| 121 | ) |
| 122 | |
| 123 | feed = object_activity.recent_activity(base_dir=data_dir, actor="alice") |
| 124 | |
| 125 | assert [entry["collection"] for entry in feed] == ["notes"] |
| 126 | |
| 127 | |
| 128 | def test_recent_activity_only_returns_the_given_actors_entries(tmp_path): |
| 129 | data_dir = tmp_path / "data" |
| 130 | write_records(data_dir, "notes", "id\ttitle\n") |
| 131 | |
| 132 | object_records.create_collection_record( |
| 133 | "notes", {"id": "n1", "title": "Alice's note"}, base_dir=data_dir, roots=[], actor="alice", |
| 134 | ) |
| 135 | object_records.create_collection_record( |
| 136 | "notes", {"id": "n2", "title": "Bob's note"}, base_dir=data_dir, roots=[], actor="bob", |
| 137 | ) |
| 138 | |
| 139 | alice_feed = object_activity.recent_activity(base_dir=data_dir, actor="alice") |
| 140 | bob_feed = object_activity.recent_activity(base_dir=data_dir, actor="bob") |
| 141 | |
| 142 | assert [entry["record_id"] for entry in alice_feed] == ["n1"] |
| 143 | assert [entry["record_id"] for entry in bob_feed] == ["n2"] |
| 144 | |
| 145 | |
| 146 | def test_recent_activity_respects_limit(tmp_path): |
| 147 | data_dir = tmp_path / "data" |
| 148 | write_records(data_dir, "notes", "id\ttitle\n") |
| 149 | for i in range(5): |
| 150 | object_records.create_collection_record( |
| 151 | "notes", {"id": f"n{i}", "title": f"Note {i}"}, |
| 152 | base_dir=data_dir, roots=[], actor="alice", |
| 153 | ) |
| 154 | |
| 155 | feed = object_activity.recent_activity(base_dir=data_dir, actor="alice", limit=2) |
| 156 | |
| 157 | assert len(feed) == 2 |
| 158 | assert [entry["record_id"] for entry in feed] == ["n4", "n3"] |
| 159 | |
| 160 | |
| 161 | def test_recent_activity_returns_empty_list_when_no_changes_exist(tmp_path): |
| 162 | data_dir = tmp_path / "data" |
| 163 | |
| 164 | feed = object_activity.recent_activity(base_dir=data_dir, actor="alice") |
| 165 | |
| 166 | assert feed == [] |
| 167 | |
| 168 | |
| 169 | def test_recent_activity_without_actor_returns_every_actors_entries(tmp_path): |
| 170 | data_dir = tmp_path / "data" |
| 171 | write_records(data_dir, "notes", "id\ttitle\n") |
| 172 | |
| 173 | object_records.create_collection_record( |
| 174 | "notes", {"id": "n1", "title": "Alice's note"}, base_dir=data_dir, roots=[], actor="alice", |
| 175 | ) |
| 176 | object_records.create_collection_record( |
| 177 | "notes", {"id": "n2", "title": "Bob's note"}, base_dir=data_dir, roots=[], actor="bob", |
| 178 | ) |
| 179 | |
| 180 | feed = object_activity.recent_activity(base_dir=data_dir) |
| 181 | |
| 182 | assert {entry["actor"] for entry in feed} == {"alice", "bob"} |
| 183 | |
| 184 | |
| 185 | def test_recent_activity_rejects_limit_below_one(tmp_path): |
| 186 | import pytest |
| 187 | |
| 188 | with pytest.raises(ValueError): |
| 189 | object_activity.recent_activity(base_dir=tmp_path / "data", limit=0) |