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_reconciles.py · 8.0 KB · 210 lines
| 1 | import json |
| 2 | |
| 3 | import pytest |
| 4 | |
| 5 | import object_package_baselines |
| 6 | import object_reconciles |
| 7 | import object_schemas |
| 8 | |
| 9 | |
| 10 | def _object_record(base_dir, **overrides): |
| 11 | payload = { |
| 12 | "package": "app-articles", |
| 13 | "target_version": "0.3.0", |
| 14 | "baseline_version": "0.2.0", |
| 15 | "artifact": {"kind": "object", "id": "site_articles"}, |
| 16 | "mine": "def GET(request): return {'v': 'mine'}\n", |
| 17 | "theirs": "def GET(request): return {'v': 'theirs'}\n", |
| 18 | "base_sha": "deadbeef", |
| 19 | "base_dir": base_dir, |
| 20 | } |
| 21 | payload.update(overrides) |
| 22 | return object_reconciles.create_reconcile(**payload) |
| 23 | |
| 24 | |
| 25 | def test_validate_reconcile_id(): |
| 26 | assert object_reconciles.validate_reconcile_id("a" * 32) is True |
| 27 | assert object_reconciles.validate_reconcile_id("A" * 32) is False |
| 28 | assert object_reconciles.validate_reconcile_id("a" * 31) is False |
| 29 | assert object_reconciles.validate_reconcile_id("../../etc/passwd") is False |
| 30 | assert object_reconciles.validate_reconcile_id(None) is False |
| 31 | assert object_reconciles.validate_reconcile_id(12345) is False |
| 32 | |
| 33 | |
| 34 | def test_reconcile_path_rejects_invalid_id(tmp_path): |
| 35 | with pytest.raises(ValueError): |
| 36 | object_reconciles.reconcile_path("not-a-valid-id", base_dir=tmp_path) |
| 37 | |
| 38 | |
| 39 | def test_reconcile_path_shape(tmp_path): |
| 40 | record = _object_record(tmp_path) |
| 41 | path = object_reconciles.reconcile_path(record["id"], base_dir=tmp_path) |
| 42 | assert path == tmp_path / "reconciles" / f"{record['id']}.json" |
| 43 | assert path.is_file() |
| 44 | |
| 45 | |
| 46 | def test_create_get_roundtrip(tmp_path): |
| 47 | record = _object_record(tmp_path) |
| 48 | |
| 49 | assert object_reconciles.validate_reconcile_id(record["id"]) |
| 50 | assert record["status"] == "pending" |
| 51 | assert record["resolution"] is None |
| 52 | assert record["package"] == "app-articles" |
| 53 | assert record["artifact"] == {"kind": "object", "id": "site_articles"} |
| 54 | |
| 55 | loaded = object_reconciles.get_reconcile(record["id"], base_dir=tmp_path) |
| 56 | assert loaded == record |
| 57 | |
| 58 | |
| 59 | def test_get_reconcile_missing_or_invalid_returns_none(tmp_path): |
| 60 | assert object_reconciles.get_reconcile("a" * 32, base_dir=tmp_path) is None |
| 61 | assert object_reconciles.get_reconcile("not-valid", base_dir=tmp_path) is None |
| 62 | |
| 63 | |
| 64 | def test_list_reconciles_empty_when_missing_dir(tmp_path): |
| 65 | assert object_reconciles.list_reconciles(base_dir=tmp_path) == [] |
| 66 | assert object_reconciles.count_pending(base_dir=tmp_path) == 0 |
| 67 | |
| 68 | |
| 69 | def test_list_and_count_filter_by_status_and_package(tmp_path): |
| 70 | rec1 = _object_record(tmp_path, created_at="2026-01-01T00:00:00Z") |
| 71 | rec2 = _object_record( |
| 72 | tmp_path, |
| 73 | package="app-notes", |
| 74 | artifact={"kind": "object", "id": "site_notes"}, |
| 75 | created_at="2026-01-02T00:00:00Z", |
| 76 | ) |
| 77 | |
| 78 | all_records = object_reconciles.list_reconciles(base_dir=tmp_path) |
| 79 | assert [r["id"] for r in all_records] == [rec1["id"], rec2["id"]] |
| 80 | |
| 81 | assert object_reconciles.count_pending(base_dir=tmp_path) == 2 |
| 82 | assert object_reconciles.count_pending(base_dir=tmp_path, package="app-articles") == 1 |
| 83 | assert object_reconciles.count_pending(base_dir=tmp_path, package="nonexistent") == 0 |
| 84 | |
| 85 | by_package = object_reconciles.list_reconciles(base_dir=tmp_path, package="app-notes") |
| 86 | assert [r["id"] for r in by_package] == [rec2["id"]] |
| 87 | |
| 88 | object_reconciles.resolve_reconcile(rec1["id"], "keep_mine", base_dir=tmp_path) |
| 89 | assert object_reconciles.count_pending(base_dir=tmp_path) == 1 |
| 90 | resolved = object_reconciles.list_reconciles(base_dir=tmp_path, status="resolved") |
| 91 | assert [r["id"] for r in resolved] == [rec1["id"]] |
| 92 | |
| 93 | |
| 94 | def test_resolve_reconcile_rejects_invalid_choice(tmp_path): |
| 95 | record = _object_record(tmp_path) |
| 96 | with pytest.raises(ValueError): |
| 97 | object_reconciles.resolve_reconcile(record["id"], "bogus", base_dir=tmp_path) |
| 98 | |
| 99 | |
| 100 | def test_resolve_reconcile_missing_raises(tmp_path): |
| 101 | with pytest.raises(ValueError): |
| 102 | object_reconciles.resolve_reconcile("b" * 32, "keep_mine", base_dir=tmp_path) |
| 103 | |
| 104 | |
| 105 | def test_resolve_reconcile_already_resolved_raises(tmp_path): |
| 106 | record = _object_record(tmp_path) |
| 107 | object_reconciles.resolve_reconcile(record["id"], "keep_mine", base_dir=tmp_path) |
| 108 | with pytest.raises(ValueError): |
| 109 | object_reconciles.resolve_reconcile(record["id"], "keep_mine", base_dir=tmp_path) |
| 110 | |
| 111 | |
| 112 | def test_resolve_take_theirs_writes_live_object_and_stamps_baseline(tmp_path): |
| 113 | object_root = tmp_path / "objects" |
| 114 | (object_root / "site").mkdir(parents=True) |
| 115 | (object_root / "site" / "articles.py").write_text("def GET(request): return {'v': 'mine'}\n") |
| 116 | data_dir = tmp_path / "data" |
| 117 | |
| 118 | record = _object_record(data_dir) |
| 119 | |
| 120 | resolved = object_reconciles.resolve_reconcile( |
| 121 | record["id"], |
| 122 | "take_theirs", |
| 123 | base_dir=data_dir, |
| 124 | object_roots=[object_root], |
| 125 | resolved_at="2026-07-08T00:00:00Z", |
| 126 | ) |
| 127 | |
| 128 | assert resolved["status"] == "resolved" |
| 129 | assert resolved["resolution"] == {"choice": "take_theirs", "resolved_at": "2026-07-08T00:00:00Z"} |
| 130 | assert (object_root / "site" / "articles.py").read_text() == "def GET(request): return {'v': 'theirs'}\n" |
| 131 | |
| 132 | baseline = object_package_baselines.load_baseline("app-articles", base_dir=data_dir) |
| 133 | assert baseline["version"] == "0.3.0" |
| 134 | assert baseline["objects"]["site_articles"] == object_package_baselines.sha256_text( |
| 135 | "def GET(request): return {'v': 'theirs'}\n" |
| 136 | ) |
| 137 | |
| 138 | |
| 139 | def test_resolve_keep_mine_leaves_live_object_and_stamps_baseline(tmp_path): |
| 140 | object_root = tmp_path / "objects" |
| 141 | (object_root / "site").mkdir(parents=True) |
| 142 | (object_root / "site" / "articles.py").write_text("def GET(request): return {'v': 'mine'}\n") |
| 143 | data_dir = tmp_path / "data" |
| 144 | |
| 145 | record = _object_record(data_dir) |
| 146 | |
| 147 | resolved = object_reconciles.resolve_reconcile( |
| 148 | record["id"], |
| 149 | "keep_mine", |
| 150 | base_dir=data_dir, |
| 151 | object_roots=[object_root], |
| 152 | resolved_at="2026-07-08T00:00:00Z", |
| 153 | ) |
| 154 | |
| 155 | assert resolved["status"] == "resolved" |
| 156 | assert resolved["resolution"]["choice"] == "keep_mine" |
| 157 | # Live content is untouched. |
| 158 | assert (object_root / "site" / "articles.py").read_text() == "def GET(request): return {'v': 'mine'}\n" |
| 159 | |
| 160 | # The baseline is still stamped to sha_theirs/target_version, so live now |
| 161 | # reads as customized against the new upstream baseline. |
| 162 | baseline = object_package_baselines.load_baseline("app-articles", base_dir=data_dir) |
| 163 | assert baseline["version"] == "0.3.0" |
| 164 | assert baseline["objects"]["site_articles"] == object_package_baselines.sha256_text( |
| 165 | "def GET(request): return {'v': 'theirs'}\n" |
| 166 | ) |
| 167 | |
| 168 | |
| 169 | def test_resolve_schema_take_theirs_replaces_live_schema(tmp_path): |
| 170 | data_dir = tmp_path / "data" |
| 171 | mine_schema = object_schemas.normalize_schema( |
| 172 | "contacts", |
| 173 | {"name": "contacts", "fields": [{"name": "id", "type": "text"}]}, |
| 174 | source="manual", |
| 175 | ) |
| 176 | object_schemas.replace_schema("contacts", mine_schema, base_dir=data_dir) |
| 177 | theirs_schema = object_schemas.normalize_schema( |
| 178 | "contacts", |
| 179 | { |
| 180 | "name": "contacts", |
| 181 | "fields": [ |
| 182 | {"name": "id", "type": "text"}, |
| 183 | {"name": "email", "type": "text"}, |
| 184 | ], |
| 185 | }, |
| 186 | source="manual", |
| 187 | ) |
| 188 | |
| 189 | record = object_reconciles.create_reconcile( |
| 190 | package="app-crm", |
| 191 | target_version="0.2.0", |
| 192 | baseline_version="0.1.0", |
| 193 | artifact={"kind": "schema", "collection": "contacts"}, |
| 194 | mine=json.dumps(mine_schema, indent=2, sort_keys=True), |
| 195 | theirs=json.dumps(theirs_schema, indent=2, sort_keys=True), |
| 196 | base_sha=None, |
| 197 | base_dir=data_dir, |
| 198 | ) |
| 199 | |
| 200 | resolved = object_reconciles.resolve_reconcile( |
| 201 | record["id"], "take_theirs", base_dir=data_dir, resolved_at="2026-07-08T00:00:00Z" |
| 202 | ) |
| 203 | |
| 204 | assert resolved["status"] == "resolved" |
| 205 | live = object_schemas.get_schema("contacts", base_dir=data_dir) |
| 206 | assert {f["name"] for f in live["fields"]} == {"id", "email"} |
| 207 | |
| 208 | baseline = object_package_baselines.load_baseline("app-crm", base_dir=data_dir) |
| 209 | assert baseline["schemas"]["contacts"] == object_package_baselines.canonical_schema_hash(theirs_schema) |
| 210 | assert baseline["version"] == "0.2.0" |