Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

0 downloads

dbbasic-object-server/tests/test_attention_paths.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 signup: the person chooses the password and name on the confirm page 83c7aa6 2 weeks, 5 days ago History
test_attention_paths.py · 9.0 KB · 203 lines
1"""Every attention count must be openable.
2
3A count is a promise that something is waiting and that clicking it will
4show you the waiting things. Four of them were pointing at URLs nothing
5served -- /scans?status=extracted, /time-logs?status=submitted,
6/expenses?status=submitted -- and one was pointing at /orders, a path
7borrowed from a package it has nothing to do with, listing none of the
8rows it counted. The arithmetic was right and the link was a lie, which
9is the worst combination available: a number people learn to stop
10clicking teaches them to stop reading the band it sits in.
11
12The fix was four seeded views. The reason the fix stays fixed is this
13file, and specifically that it is a SWEEP rather than four cases. The
14gap did not appear because somebody wrote a bad path; it appeared
15because declaring a path and serving one are separate acts in separate
16files, and nothing had ever compared them. `_normalize_attention` says
17so out loud -- "whether anything answers it is a routing question this
18module deliberately does not pretend to answer" -- so the answering has
19to happen here, over every manifest on the box, including the ones
20nobody has written yet.
21
22Three properties, in order of how much they hurt when broken:
23
241. **The path resolves.** Against the union of every package's seeded
25 site_routes rows, plus the `site_*` convention, which is the same
26 pair object_server resolves against at request time.
272. **A site_view_render path has a view behind it.** A route to the
28 generic renderer whose `views` record is missing (or whose route
29 field disagrees) renders "View not found" -- a 404 with a 200's
30 clothes on, which the first property alone would happily pass.
313. **No attention path carries a query string.** This one is a finding
32 turned into a guard: the generic list builds its filter bar from the
33 schema's `views.filter_fields` and reads NOTHING from the URL, so
34 `?status=extracted` narrowed nothing and landed on the whole
35 collection while claiming to be a queue. Until a query param actually
36 filters something, a path that carries one is a promise the page
37 cannot keep, and an honest broader list beats a silent no-op.
38"""
39
40import csv
41import json
42from pathlib import Path
43from urllib.parse import urlsplit
44
45import object_site_routes
46
47PACKAGES = Path(__file__).resolve().parents[1] / "packages"
48
49
50def _manifests():
51 return {path.parent.name: json.loads(path.read_text())
52 for path in sorted(PACKAGES.glob("*/dbbasic-package.json"))}
53
54
55def _seed_rows(package_id, relative_path):
56 with open(PACKAGES / package_id / relative_path, newline="") as handle:
57 return list(csv.DictReader(handle, delimiter="\t"))
58
59
60def _seeded(collection):
61 """Every row any package seeds into one shared collection.
62
63 Routing is a whole-box property: app-returns' count is allowed to
64 lean on app-shipping's route and app-intake's on its own, so the
65 union is the only honest thing to resolve against.
66 """
67 rows = []
68 for package_id, manifest in _manifests().items():
69 for entry in manifest.get("seed") or []:
70 if entry.get("collection") == collection:
71 rows.extend(_seed_rows(package_id, entry["path"]))
72 return rows
73
74
75def _site_objects():
76 """Object ids that would satisfy convention routing (/about ->
77 site_about), which resolves BEFORE site_routes records do."""
78 return {entry["id"]
79 for manifest in _manifests().values()
80 for entry in manifest.get("objects") or []
81 if entry["id"].startswith("site_")}
82
83
84def _attention_sources():
85 return [(package_id, entry)
86 for package_id, manifest in _manifests().items()
87 for entry in manifest.get("attention") or []]
88
89
90def _resolve(path, routes, site_objects):
91 """What would actually answer this path, or None. Mirrors
92 object_server's own order: convention first, seeded routes second."""
93 convention = object_site_routes.convention_object_id(path)
94 if convention and convention in site_objects:
95 return convention
96 match = object_site_routes.match_records(path, routes)
97 return match[0] if match else None
98
99
100# --- the properties ----------------------------------------------------------
101
102def test_every_attention_path_is_a_path_some_package_actually_serves():
103 """The one that stops the gap coming back."""
104 routes = _seeded("site_routes")
105 site_objects = _site_objects()
106
107 dead = []
108 for package_id, entry in _attention_sources():
109 path = urlsplit(entry["path"]).path
110 if not _resolve(path, routes, site_objects):
111 dead.append(f"{package_id}:{entry['id']} -> {entry['path']}")
112 assert not dead, (
113 f"these counts link to URLs nothing serves: {dead}. Seed a view + a "
114 "site_routes row for the collection, or point the count at a page "
115 "that exists -- a count nobody can open is a notification.")
116
117
118def test_a_count_that_links_to_the_generic_renderer_has_a_view_behind_it():
119 """site_view_render resolves a capture-less path by matching it against
120 the `route` field of a `views` record. A seeded route with no seeded
121 view is a 404 wearing a 200: the page loads and says "View not
122 found", so the previous test passes and the human still cannot get
123 at their queue."""
124 routes = _seeded("site_routes")
125 site_objects = _site_objects()
126 view_routes = {row["route"] for row in _seeded("views") if row.get("route")}
127
128 missing = []
129 for package_id, entry in _attention_sources():
130 path = urlsplit(entry["path"]).path
131 if _resolve(path, routes, site_objects) != "site_view_render":
132 continue
133 if path not in view_routes:
134 missing.append(f"{package_id}:{entry['id']} -> {path}")
135 assert not missing, (
136 f"these counts route to site_view_render with no matching views "
137 f"record: {missing}. The views row's `route` field is what the "
138 "renderer matches the request path against.")
139
140
141def test_no_attention_path_pretends_to_filter_through_the_query_string():
142 """The generic list takes its filters from the schema's
143 `views.filter_fields`, rendered as a bar the reader picks from, and
144 reads nothing at all from the URL. A path carrying ?status=submitted
145 therefore lands on the entire collection while the badge beside it
146 says nine -- worse than an honest index, because the reader has no
147 way to tell the list is not the list they were promised.
148
149 Delete this test the day a list block resolves a query param into
150 its `where`. Until then it is the thing that keeps a plausible,
151 silently broken link out of the manifests.
152 """
153 lying = [f"{package_id}:{entry['id']} -> {entry['path']}"
154 for package_id, entry in _attention_sources()
155 if urlsplit(entry["path"]).query]
156 assert not lying, (
157 f"these counts carry a query string nothing honours: {lying}. Point "
158 "the count at the unfiltered index; the schema's filter_fields put "
159 "the status filter one click away on the page itself.")
160
161
162def test_a_count_bound_to_a_door_names_a_door_that_exists():
163 """`nav_id` is how a count gets onto its app tile, and it is also
164 what narrows the count to that door's audience. A nav_id naming
165 nothing does not fail loudly -- the count simply never decorates
166 anything -- so it is checked here rather than discovered later."""
167 declared = {entry["id"]
168 for manifest in _manifests().values()
169 for entry in manifest.get("nav") or []}
170
171 unknown = [f"{package_id}:{entry['id']} -> {entry['nav_id']}"
172 for package_id, entry in _attention_sources()
173 if entry.get("nav_id") and entry["nav_id"] not in declared]
174 assert not unknown, f"these counts decorate a door nobody declares: {unknown}"
175
176
177def test_the_four_queues_this_slice_was_about_now_have_their_own_index():
178 """A named regression guard beside the sweep, because the sweep would
179 also go green if somebody pointed all four counts back at /orders.
180
181 Each pair is (attention source, the collection its provider actually
182 counts). The list the page shows has to be the collection the count
183 counted; a badge that disagrees with the page it opens is worse than
184 no badge, and app-returns is the case that makes the point -- its
185 count reads `shipments`, not its own `return_authorizations`, so its
186 index is over shipments too.
187 """
188 expected = {
189 "scans_to_confirm": ("/scans", "scans"),
190 "time_to_approve": ("/time-logs", "time_logs"),
191 "expenses_to_approve": ("/expenses", "expenses"),
192 "returns_to_disposition": ("/returns", "shipments"),
193 }
194 by_route = {row["route"]: row for row in _seeded("views") if row.get("route")}
195 sources = {entry["id"]: entry for _package, entry in _attention_sources()}
196
197 for source_id, (path, collection) in expected.items():
198 entry = sources[source_id]
199 assert entry["path"] == path, source_id
200 blocks = json.loads(by_route[path]["blocks"])
201 listed = [block for block in blocks if block["kind"] == "list"]
202 assert len(listed) == 1, source_id
203 assert listed[0]["collection"] == collection, source_id