"""Checks of the web app, without calling claude: .venv/bin/python app/server/check.py [name]""" import json import os import shutil import socket import sys import tempfile import threading import time import urllib.request from pathlib import Path from urllib.error import HTTPError HERE = Path(__file__).resolve().parent sys.path.insert(0, str(HERE)) import uvicorn # noqa: E402 import server # noqa: E402 import station # noqa: E402 def temp_folder(): return Path(tempfile.mkdtemp()) def check_server(): # the real server on a free port: the guard (Host, declared JSON), a read with socket.socket() as so: so.bind(("127.0.0.1", 0)) port = so.getsockname()[1] srv = uvicorn.Server(uvicorn.Config(server.app, host="127.0.0.1", port=port, log_level="error")) threading.Thread(target=srv.run, daemon=True).start() while not srv.started: time.sleep(0.05) base = f"http://127.0.0.1:{port}" def call(path, body=None, headers=None, method=None): r = urllib.request.Request(base + path, json.dumps(body).encode() if body is not None else None, {"Content-Type": "application/json", **(headers or {})}, method=method) try: with urllib.request.urlopen(r) as rep: return rep.status except HTTPError as e: return e.code try: assert call("/api/stations", headers={"Host": "evil.example:80"}) == 403 assert call("/api/labels", {"station": "x"}, {"Content-Type": "text/plain"}) == 403 assert call("/api/stations") == 200 assert call("/api/analyses") == 200 assert call("/api/support") == 200 assert call("/api/support", {"station": "x"}) == 400 assert call("/api/analyses/UNKNOWN/optimize", {}) == 400 finally: srv.should_exit = True def refuses(f, *a): try: f(*a) except station.Refusal as e: return str(e) raise AssertionError(f"{f.__name__}{a} did not refuse") def check_station(): # the data of the Data page, on the real data; the labels in a temporary folder assert "NIEDERHORN" in station.stations() im = station.images("NIEDERHORN", "2026-09-25", "2026-09-26") assert im["images"] and im["positions"] and set(im["corr"]) == set(im["positions"]), list(im) assert all(len(c) == len(im["images"]) for c in im["corr"].values()) assert station.image("NIEDERHORN", im["images"][0]["name"]).is_file() refuses(station.image, "NIEDERHORN", "../../../.env") refuses(station.images, "../API", "", "") assert station.images("LENZSPITZE", "2026-10-26", "2026-10-27")["images"] c = station.curves("LENZSPITZE", "2026-10-26", "2026-10-27") titles = [p["title"] for p in c["plots"]] assert "Relative Corrected Vertical Shift" in titles and any(t.startswith("System: ") for t in titles), titles ev = station.detectors("LENZSPITZE", "2026-10-26", "2026-10-27") # the detectors of the journal, ~10 s assert ev and {"start", "end", "family", "description", "sensors"} <= set(ev[0]), ev[:1] assert all(e["message"] and e["action"] and set(e["targets"] + e["references"]) == set(e["sensors"]) for e in ev), ev[:1] assert all(e["end"] >= "2026-10-26T00:00Z" and e["start"] < "2026-10-27T00:00Z" for e in ev), [(e["start"], e["end"]) for e in ev] refuses(station.pump, {"stations": ["A;rm"]}) # the pumping (extraction/pumping.py, checked there): a refusal assert not any("SIM_CCID" in t or t.startswith("System: ip") for t in titles) assert all(len(c["T"][tr["t"]]) == len(tr["y"]) for p in c["plots"] for tr in p["traces"]) if run := next((s for s in station.stations() if station.RUN in s), None): # a run of data_diff/, read as a station c = station.curves(run, "", "") assert c["plots"] and station.images(run, c["A"], c["B"])["images"], run assert station.label_file(run, "auto").name == station.serial(run) + ".jsonl" assert "data_diff" in refuses(station.pumped, run) before, station.LABELS = station.LABELS, temp_folder() try: e = station.add({"station": "NIEDERHORN", "folder": "auto", "start": "2026-09-25T01:00Z", "end": "2026-09-25T02:00Z", "family": "Visibility", "description": "fog"}) assert e["id"] == "NIEDERHORN-1" and next(iter(e)) == "id", e refuses(station.update, {"station": "NIEDERHORN", "folder": "auto", "before": e, # labeling/scripts/validate.py "start": e["start"], "end": e["end"], "family": "", "description": "rain"}) e2 = station.update({"station": "NIEDERHORN", "folder": "auto", "before": e, "start": e["start"], "end": e["end"], "family": "Obstruction", "description": "rain"}) assert e2 == {"id": "NIEDERHORN-1", "start": e["start"], "end": e["end"], "family": "Obstruction", "description": "rain"}, e2 assert station.read("NIEDERHORN", "auto") == [e2] assert "no longer in the file" in refuses(station.delete, {"station": "NIEDERHORN", "folder": "auto", "before": e}) station.delete({"station": "NIEDERHORN", "folder": "auto", "before": e2}) assert station.read("NIEDERHORN", "auto") == [] refuses(station.add, {"station": "NIEDERHORN", "folder": "../x", "start": "a", "end": "b", "description": "x"}) refuses(station.add, {"station": "NIEDERHORN", "folder": "manual", "start": "2026-09-25T01:00Z", # the expert's "end": "2026-09-25T02:00Z", "family": "Visibility", "description": "fog"}) refuses(station.add, {"station": "NIEDERHORN", "start": "2026-09-25T01:00Z", "end": "2026-09-25T02:00Z", "family": "Visibility", "description": "a shift of 3 px"}) refuses(station.add, {"station": "NIEDERHORN", "start": "2026-09-25T02:00Z", "end": "2026-09-25T01:00Z", "description": "x"}) finally: shutil.rmtree(station.LABELS) station.LABELS = before def check_optimization(): # the Optimize button (analysis.optimize), without claude: a fake process that ends only on a signal, a finished # analysis copied into a temporary folder; the lock (one optimization at a time, no analysis or message meanwhile), the # state read again after a restart import analysis end = threading.Event() launched = [] class FakeProcess: returncode = 0 stdout = iter(lambda: end.wait() and "", "") # nothing, until the signal def __init__(self, args, *a, **k): launched.append((args, k)) def wait(self): return 0 before = analysis.FOLDER, analysis.subprocess.Popen, analysis.OPTIMIZER, analysis.SESSIONS, analysis.AGENTS key = os.environ.get("ANTHROPIC_API_KEY") os.environ["ANTHROPIC_API_KEY"] = "test-key" root = temp_folder() analysis.FOLDER, analysis.OPTIMIZER, analysis.AGENTS = root / "analyses", root / "optimizer", root / "agents" analysis.SESSIONS = analysis.OPTIMIZER / "sessions" analysis.subprocess.Popen = FakeProcess try: for name in ("NORDEND_a", "NORDEND_b"): (analysis.FOLDER / name / "work").mkdir(parents=True) (analysis.FOLDER / name / "request.json").write_text(json.dumps( {"station": "NORDEND", "start": "2026-11-04T21:12Z", "end": "2026-11-07T14:12Z", "hints": ""})) (analysis.FOLDER / name / "report.md").write_text("# report") assert analysis.state("NORDEND_a")["optimization"] is None # without Optimizer, then without agents: a clear refusal, not an error trace assert "optimizer/" in refuses(analysis.optimize, "NORDEND_a") (analysis.OPTIMIZER / ".claude" / "skills" / "optimize").mkdir(parents=True) (analysis.OPTIMIZER / ".claude" / "skills" / "optimize" / "SKILL.md").write_text("optimize") assert "agents/" in refuses(analysis.optimize, "NORDEND_a") (analysis.AGENTS / ".claude-plugin").mkdir(parents=True) (analysis.AGENTS / ".claude-plugin" / "plugin.json").write_text("{}") assert "optimizer/" in refuses(analysis.optimize, "NORDEND_a") # without the links of Optimizer for link in ("data", "labeling", "detectors"): (analysis.OPTIMIZER / link).mkdir() assert "optimizer/" in refuses(analysis.optimize, "NORDEND_a") # without its tools link (analysis.OPTIMIZER / "tools").mkdir() assert analysis.optimize("NORDEND_a") == {} assert analysis.state("NORDEND_a")["optimization"]["state"] == "running" refuses(analysis.optimize, "NORDEND_a") refuses(analysis.optimize, "NORDEND_b") refuses(analysis.launch, {"station": "NORDEND", "start": "2026-11-04T00:00Z", "end": "2026-11-05T00:00Z"}) refuses(analysis.message, "NORDEND_b", {"text": "continue"}) end.set() while analysis.state("NORDEND_a")["optimization"]["state"] == "running": time.sleep(0.05) assert analysis.state("NORDEND_a")["optimization"]["state"] == "done" assert len(launched) == 1, launched # a single call: /optimize in Optimizer, on the analysis args, k = launched[0] request = args[2] assert request.startswith("/optimize ANALYSIS: labeling/auto/analyses/NORDEND_a/. OUT: sessions/NORDEND_a_") \ and "Caller's context:" in request, request assert k["cwd"] == analysis.OPTIMIZER.resolve() and "--plugin-dir" in args, (args, k) assert all("ANTHROPIC_API_KEY" not in kw["env"] for _, kw in launched), "an API key reaches claude" # isolated from the settings of the workstation (the user's plugins, hooks, MCP servers) assert all(a[a.index("--setting-sources") + 1] == "project,local" and "--strict-mcp-config" in a for a, _ in launched) analysis.analyses["NORDEND_b"]["state"] = "running" # a running analysis blocks the optimization refuses(analysis.optimize, "NORDEND_a") # after a restart: the stream ends with {"type": "end"} -> done; without that line -> interrupted; the summary of # the detectors and that of the tools in one page stream = analysis.FOLDER / "NORDEND_a" / "work" / "optimization.jsonl" assert stream.read_text().endswith('{"type": "end", "state": "done"}\n'), stream.read_text() analysis.analyses.clear() analysis.optimizations.clear() assert analysis.state("NORDEND_a")["optimization"]["state"] == "done" stream.write_text('{"type": "assistant", "message": {"content": []}}\n') analysis.optimizations.clear() assert analysis.state("NORDEND_a")["optimization"]["state"] == "interrupted" (analysis.SESSIONS / "NORDEND_a_20261113T1000Z").mkdir(parents=True) (analysis.SESSIONS / "NORDEND_a_20261113T1000Z" / "summary.md").write_text("# optimize\n\n**Result**: none") # a new analysis of the same range (NORDEND_a_143000): its session is not the summary of NORDEND_a (analysis.SESSIONS / "NORDEND_a_143000_20261113T1100Z").mkdir(parents=True) (analysis.SESSIONS / "NORDEND_a_143000_20261113T1100Z" / "summary.md").write_text("# other analysis") assert analysis._summary_file("NORDEND_a").parent.name == "NORDEND_a_20261113T1000Z", analysis._summary_file("NORDEND_a") (analysis.FOLDER / "NORDEND_a" / "tools.md").write_text("# Tools\n\n**Tools changed**: none") assert analysis.state("NORDEND_a")["optimization"]["summary"] page = analysis.summary("NORDEND_a", False) assert "Result" in page and "Tools changed" in page, page assert analysis.state("NORDEND_b")["optimization"] is None finally: end.set() os.environ.pop("ANTHROPIC_API_KEY") if key is not None: os.environ["ANTHROPIC_API_KEY"] = key shutil.rmtree(root) analysis.FOLDER, analysis.subprocess.Popen, analysis.OPTIMIZER, analysis.SESSIONS, analysis.AGENTS = before analysis.analyses.clear() analysis.optimizations.clear() def check_analysis(): # "Analyze the range" then a message (analysis.launch, analysis.message), without claude: the analyst of the agents # repository, its parameters, and the resumption of its session with the same agent import analysis launched = [] class FakeProcess: returncode = 0 def __init__(self, args, *a, **k): launched.append((args, k)) self.stdout = iter([]) def wait(self): return 0 (station.ROOT / "out").mkdir(exist_ok=True) # under the root: the request cites the output folder relative to it before = analysis.FOLDER, analysis.subprocess.Popen, analysis.submit analysis.FOLDER = Path(tempfile.mkdtemp(dir=station.ROOT / "out")) analysis.subprocess.Popen = FakeProcess analysis.submit = lambda *a: 0 # otherwise the test rewrites the real labeling/auto/NORDEND.jsonl key = os.environ.get("ANTHROPIC_API_KEY") os.environ["ANTHROPIC_API_KEY"] = "test-key" # the agent runs on the subscription, never on an API key try: name = analysis.launch({"station": "NORDEND", "start": "2026-11-05T06:00Z", "end": "2026-11-05T12:00Z"})["name"] while analysis.analyses[name]["state"] == "running": time.sleep(0.05) assert analysis.analyses[name]["state"] == "done", analysis.analyses[name] assert json.loads((analysis.FOLDER / name / "request.json").read_text()) == \ {"station": "NORDEND", "start": "2026-11-05T06:00Z", "end": "2026-11-05T12:00Z", "hints": ""} args, k = launched[0] assert args[args.index("--agent") + 1] == "agents:analyst" and "--plugin-dir" in args, args assert "ANTHROPIC_API_KEY" not in k["env"] and "PATH" in k["env"], sorted(k.get("env") or {}) assert args[2].startswith("Station: data/NORDEND/. Range: 2026-11-05T06:00Z → 2026-11-05T12:00Z (UTC). Hints: none. ") \ and "DETECTORS: no" in args[2] and "INSTRUCTIONS: instructions/analyst.md" in args[2], args[2] assert args[args.index("--setting-sources") + 1] == "project,local" and "--strict-mcp-config" in args, args (analysis.FOLDER / name / "work" / "session").write_text("s-1") analysis.message(name, {"text": "continue"}) while analysis.analyses[name]["state"] == "running": time.sleep(0.05) args, k = launched[1] assert "ANTHROPIC_API_KEY" not in k["env"], sorted(k.get("env") or {}) assert args[args.index("--resume") + 1] == "s-1" and args[args.index("--agent") + 1] == "agents:analyst" \ and "--plugin-dir" in args, args agents, analysis.AGENTS = analysis.AGENTS, analysis.FOLDER / "no-agents" # the agents/ link is missing: clear refusal try: assert "agents/" in refuses(analysis.launch, {"station": "NORDEND", "start": "2026-11-06T06:00Z", "end": "2026-11-06T12:00Z"}) finally: analysis.AGENTS = agents finally: os.environ.pop("ANTHROPIC_API_KEY") if key is not None: os.environ["ANTHROPIC_API_KEY"] = key shutil.rmtree(analysis.FOLDER) analysis.FOLDER, analysis.subprocess.Popen, analysis.submit = before analysis.analyses.clear() def check_validation(): # "Validate labels" (analysis.validate): review on all the labels of the analysis, in its labels.jsonl (which says # whether it is validated) and submitted with their id; the others untouched; an edit in the page removes it from one # label, there too, and the analysis is no longer validated import analysis before = station.LABELS, analysis.FOLDER station.LABELS = temp_folder() analysis.FOLDER = station.LABELS / "auto" / "analyses" try: for name, report in (("NORDEND_a", True), ("NORDEND_b", False)): (analysis.FOLDER / name / "work").mkdir(parents=True) (analysis.FOLDER / name / "request.json").write_text(json.dumps({"station": "NORDEND"})) if report: (analysis.FOLDER / name / "report.md").write_text("# report") mine = [{"start": "2026-11-05T06:00Z", "end": "2026-11-05T07:00Z", "family": "Visibility", "description": f"fog {i}"} for i in (1, 2)] other = {"id": "NORDEND-3", "start": "2026-11-05T06:00Z", "end": "2026-11-05T07:00Z", "family": "Visibility", "description": "rain"} station.write(analysis.FOLDER / "NORDEND_a" / "labels.jsonl", mine) station.write(station.LABELS / "auto" / "NORDEND.jsonl", [{"id": f"NORDEND-{i}"} | e | {"analysis": "NORDEND_a"} for i, e in enumerate(mine, 1)] + [other]) analysis.state("NORDEND_a") assert analysis.state("NORDEND_a")["validated"] is None assert "finished" in refuses(analysis.validate, "NORDEND_b") # interrupted, no report assert analysis.validate("NORDEND_a") == {"validated": 2} theirs = analysis._labels(analysis.FOLDER / "NORDEND_a") assert all(e.get("review") for e in theirs), theirs labels = station.read("NORDEND", "auto") assert {e["id"]: bool(e.get("review")) for e in labels} == \ {"NORDEND-1": True, "NORDEND-2": True, "NORDEND-3": False}, labels # the ids kept assert analysis.state("NORDEND_a")["validated"] == theirs[0]["review"] assert {a["name"]: a["validated"] for a in analysis.list_analyses()} == \ {"NORDEND_a": theirs[0]["review"], "NORDEND_b": None} # the sidebar edited = next(e for e in labels if e["id"] == "NORDEND-1") station.update({"station": "NORDEND", "before": edited, "family": "Visibility", "description": "fog again"}) assert analysis.state("NORDEND_a")["validated"] is None assert [e["description"] for e in analysis._labels(analysis.FOLDER / "NORDEND_a")] == ["fog again", "fog 2"] assert analysis.validate("NORDEND_a") == {"validated": 2} # validated again, the edit and the ids kept assert sorted((e["id"], e["description"]) for e in station.read("NORDEND", "auto")) == \ [("NORDEND-1", "fog again"), ("NORDEND-2", "fog 2"), ("NORDEND-3", "rain")] station.write(analysis.FOLDER / "NORDEND_a" / "labels.jsonl", []) assert "no label" in refuses(analysis.validate, "NORDEND_a") finally: shutil.rmtree(station.LABELS) station.LABELS, analysis.FOLDER = before analysis.analyses.clear() def check_support(): # "Ask the support" then a message (support.launch, support.message), without claude: the support of the agents # repository, its request (the question, its instructions, its absolute folder), the long commands it is given, the # resumption of its session, its answer in the page; the state read again after a restart import analysis import support launched = [] class FakeProcess: returncode = 0 def __init__(self, args, *a, **k): launched.append((args, k)) self.stdout = iter(['{"type":"system","subtype":"init","session_id":"s-1"}\n']) def wait(self): return 0 root = temp_folder() before = support.FOLDER, analysis.subprocess.Popen, analysis.AGENTS support.FOLDER, analysis.AGENTS, analysis.subprocess.Popen = root / "support", root / "agents", FakeProcess key = os.environ.get("ANTHROPIC_API_KEY") os.environ["ANTHROPIC_API_KEY"] = "test-key" ask = {"station": "NORDEND", "start": "2026-11-05T06:00Z", "end": "2026-11-05T12:00Z", "question": "Why the jump?"} try: assert "agents/" in refuses(support.launch, ask) (analysis.AGENTS / "agents").mkdir(parents=True) (analysis.AGENTS / "agents" / "support.md").write_text("support") assert "question" in refuses(support.launch, ask | {"question": " "}) name = support.launch(ask)["name"] while support.supports[name]["state"] == "running": time.sleep(0.05) assert support.state(name)["state"] == "done" and support.state(name)["report"] is None, support.state(name) args, k = launched[0] assert args[args.index("--agent") + 1] == "agents:support" and "Edit" in args[args.index("--allowedTools") + 1], args assert "ANTHROPIC_API_KEY" not in k["env"] and k["env"]["BASH_MAX_TIMEOUT_MS"] == "10800000", sorted(k["env"]) out = support.FOLDER / name assert f"Output folder: {out}/." in args[2] and f"INSTRUCTIONS: {support.INSTRUCTIONS}." in args[2] \ and args[2].endswith("Question: Why the jump?") and support.INSTRUCTIONS.is_file(), args[2] assert (out / "work" / "session").read_text() == "s-1" assert support.launch(ask)["name"].startswith(name + "_") # another question on the same range, next to it (out / "answer.md").write_text("# NORDEND: a jump\n\n![curves](fig.png)") (out / "fig.png").write_bytes(b"png") assert support.state(name)["report"] and "

NORDEND: a jump

" in support.answer(name, False) assert analysis.file(name, "fig.png", support.FOLDER).is_file() refuses(analysis.file, name, "../../agents/agents/support.md", support.FOLDER) while any(s["state"] == "running" for s in support.supports.values()): time.sleep(0.05) support.message(name, {"text": "And the night before?"}) while support.supports[name]["state"] == "running": time.sleep(0.05) args = launched[-1][0] assert args[args.index("--resume") + 1] == "s-1" and args[2].startswith("And the night before?"), args support.supports.clear() # after a restart: the thread read again in the log, done with its answer s = support.state(name) assert s["state"] == "done" and s["lines"] == ["› And the night before?"], s assert {x["name"]: x["state"] for x in support.list_supports()}[name] == "done" # the sidebar refuses(support.state, "../analyses") finally: os.environ.pop("ANTHROPIC_API_KEY") if key is not None: os.environ["ANTHROPIC_API_KEY"] = key shutil.rmtree(root) support.FOLDER, analysis.subprocess.Popen, analysis.AGENTS = before support.supports.clear() def check_conversations(): # the Conversations page (conversations.py): the list, the transcript rendered safely, the files, the pumping refused # on a bad range; without LibreDesk (the pumping is libredesk/conversations/, tested there) import conversations root, before = temp_folder(), conversations.FOLDER conversations.FOLDER = root try: c = root / "42" (c / "attachments").mkdir(parents=True) (c / "conversation.json").write_text(json.dumps({"reference_number": "42", "subject": "A jump", "status": "Open", "updated_at": "2026-11-18T09:00:00Z", "contact": {"first_name": "Jean", "last_name": "", "email": "j@x.ch"}})) (c / "messages.json").write_text(json.dumps([ {"type": "incoming", "created_at": "2026-11-18T08:00:00Z", "attachments": [{"url": "attachments/p.png"}]}, {"type": "activity", "created_at": "2026-11-18T08:05:00Z", "attachments": []}, {"type": "outgoing", "created_at": "2026-11-18T08:10:00Z", "attachments": [{"url": "attachments/x.svg"}]}])) (c / "transcript.md").write_text("# A jump\n\n\n\n![p](attachments/p.png)\n") (c / "attachments" / "p.png").write_bytes(b"png") (c / "attachments" / "x.svg").write_text("") (root / "pumped.json").write_text("{}") # not a conversation assert conversations.list_conversations() == [{"reference": "42", "subject": "A jump", "contact": "Jean", "status": "Open", "last": "2026-11-18T08:10:00Z", "messages": 2, "attachments": 2}], conversations.list_conversations() page = conversations.transcript("42", False) assert "