STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
test_layerx_feedtwin_library.py
Go to the documentation of this file.
1"""Layer X lists the feed twin's library (2026-10-03: "pull from it and trust it")."""
2
3import json
4import shutil
5
6from engine.layerx import sources
7
8
10 shipped = sources.shipped_drawings_dir()
11 lib = tmp_path / "library"
12 (lib / "blobs").mkdir(parents=True)
13 he = shipped / "copv_study_he.json"
14 # One drawing the twin pulled from pid-designer (a changed copy), one byte-identical to a shipped file.
15 changed = json.loads(he.read_text())
16 changed["nodes"][0]["data"]["label"] = "COPV-PULLED"
17 (lib / "blobs" / "pulled.json").write_text(json.dumps(changed))
18 shutil.copy(he, lib / "blobs" / "same.json")
19 (lib / "manifest.json").write_text(json.dumps([
20 {"kind": "diagram", "name": "stand from pid-designer", "filename": "pulled.json", "source": "pid-designer:local/x",
21 "imported_at": "2026-10-01T10:00:00+00:00"},
22 {"kind": "diagram", "name": "copv_study_he", "filename": "same.json", "source": "shipped:copv_study_he.json",
23 "imported_at": "2026-09-12T04:03:32+00:00"},
24 {"kind": "engine", "name": "an engine", "filename": "e.yaml"},
25 ]))
26 monkeypatch.setenv("LAYERX_FEEDTWIN_LIBRARY", str(lib))
27 sources._SHIPPED_CACHE.clear()
28 listed = sources.DrawingStore(None).list()
29 names = [d.name for d in listed]
30 assert "stand from pid-designer" in names
31 assert names.count("copv_study_he") == 1 # the same bytes are one drawing
32 pulled = next(d for d in listed if d.name == "stand from pid-designer")
33 assert pulled.source.startswith("feed-twin library")
34 assert sources.DrawingStore(None).get(pulled.id) is not None
35
36
38 """The engine-design-api container's environment, as Dockerfile.api and docker-compose.yml set it, and
39 where its files come from: the image's COPY destinations and the compose volume mounts."""
40 import re
41 from pathlib import Path
42
43 import yaml
44
45 root = Path(__file__).resolve().parents[2]
46 docker = (root / "EngineDesign" / "Dockerfile.api").read_text()
47 env = {}
48 for block in re.findall(r"^ENV ((?:.*\\\n)*.*)$", docker, flags=re.M):
49 for k, v in re.findall(r"(\w+)=(\S+)", block.replace("\\\n", " ")):
50 env[k] = v
51 copies = [dest.rstrip("/") for _src, dest in re.findall(r"^COPY (?:--\S+ )*(\S+) (\S+)$", docker, flags=re.M)]
52 compose = yaml.safe_load((root / "docker-compose.yml").read_text())["services"]
53 api = compose["engine-design-api"]
54 env.update({k: str(v) for k, v in (api.get("environment") or {}).items()})
55 mounts = {m.split(":")[0]: m.split(":")[1] for m in api.get("volumes") or []}
56 return env, copies, mounts, compose
57
58
60 """In compose, Layer X resolved the twin's library and sample set to /feed-twin/backend/..., which the
61 image does not have: production Layer X listed none of the cockpit's drawings or pulls, and no featured
62 stand (R003). Every drawing source must be set to a place the container has."""
63 env, copies, mounts, compose = _image_env()
64 for key in ("LAYERX_DRAWINGS", "LAYERX_STATEMACHINES", "LAYERX_SAMPLE", "LAYERX_FEEDTWIN_LIBRARY"):
65 path = env.get(key)
66 assert path, f"{key} is not set for engine-design-api"
67 in_image = any(path == c or path.startswith(c + "/") for c in copies)
68 on_volume = any(path == m or path.startswith(m + "/") for m in mounts.values())
69 assert in_image or on_volume, f"{key}={path} is neither copied into the image nor on a mounted volume"
70 # The library is the twin's own: the same directory on the same volume as feed-twin-api's.
71 twin = compose["feed-twin-api"]
72 twin_mounts = {m.split(":")[0]: m.split(":")[1] for m in twin.get("volumes") or []}
73 lib = twin["environment"]["FEEDTWIN_LIBRARY"]
74 volume = next(v for v, m in twin_mounts.items() if lib.startswith(m + "/"))
75 assert mounts.get(volume) == twin_mounts[volume] and env["LAYERX_FEEDTWIN_LIBRARY"] == lib
76
77
79 """The rail's 'Dome regulator' row was blank and its hover said 'droop — psi (from the drawing)' while the
80 burn ran the twin's 20 psi library default (feedtwin.pid.network FALLBACKS)."""
81 he = json.loads((sources.shipped_drawings_dir() / "copv_study_gn2.json").read_text())
82 drawn = sources.summarize(he, "gn2")
83 reg = next(r for r in drawn["regulators"] if r["id"] == "PR_D")
84 assert reg["flow_droop_psi"] == 8.3 and reg["flow_droop_default"] is False
85 node = next(n for n in he["nodes"] if n["id"] == "PR_D")
86 del node["data"]["params"]["flow_droop"]
87 reg = next(r for r in sources.summarize(he, "gn2")["regulators"] if r["id"] == "PR_D")
88 assert reg["flow_droop_psi"] == 20.0 and reg["flow_droop_default"] is True
89
90
92 monkeypatch.setenv("LAYERX_SAMPLE", str(tmp_path / "nowhere"))
93 monkeypatch.setenv("LAYERX_FEEDTWIN_LIBRARY", str(tmp_path))
94 problems = sources.source_problems()
95 assert any("LAYERX_SAMPLE" in p and "nowhere" in p for p in problems)
96 assert not any("LAYERX_FEEDTWIN_LIBRARY" in p for p in problems)
test_a_missing_drawing_source_is_said_not_listed_as_nothing(tmp_path, monkeypatch)
test_the_feed_twins_library_is_listed_and_deduplicated(tmp_path, monkeypatch)