Add upload/download of sud.json over the Sud websocket channel

Sud.download() returns the on-disk document; Sud.upload() validates,
persists, and resets the schedule, refusing mid-brew or malformed input
so the current schedule is never left half-applied.
This commit is contained in:
2026-06-20 22:14:27 +02:00
parent 6fb85415eb
commit db57f3305c
3 changed files with 99 additions and 8 deletions
@@ -0,0 +1,45 @@
import json
import shutil
import tempfile
from pathlib import Path
from components.sud import Sud, SudState
# Console-only walkthrough of Sud.download()/upload() (no plot - there's no
# numeric data here, just JSON plumbing). Runs against a scratch copy of a
# real sud.json so sude/sud_0010.json itself is never touched.
if __name__ == '__main__':
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "sud.json"
shutil.copy("sude/sud_0010.json", path)
sud = Sud(str(path))
print(f"Loaded '{sud.name}', {len(sud.schedule)} steps")
doc = sud.download()
assert doc['Name'] == sud.name
print("download() returned the on-disk document")
sud.state = SudState.RAMPING
assert sud.upload(doc) is False
print("upload() refused while RAMPING")
sud.state = SudState.IDLE
doc['Name'] = 'Sud-Renamed'
doc['steps'][0]['descr'] = 'Edited step'
assert sud.upload(doc) is True
assert sud.name == 'Sud-Renamed'
assert sud.schedule[0]['descr'] == 'Edited step'
assert sud.state == SudState.IDLE
print("upload() applied the edit and reset to IDLE")
on_disk = json.loads(path.read_text())
assert on_disk['Name'] == 'Sud-Renamed'
print("upload() persisted the edit to disk")
assert sud.upload({'Name': 'broken'}) is False
assert sud.name == 'Sud-Renamed'
print("upload() rejected malformed data, left schedule untouched")
print("All sud upload/download checks passed")