live/host_action.py: GUI Blender 5.3 proof of host rows, undo steps, results and human-only absence
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
740a2c84c3
commit
c1aacd9f7b
4 changed files with 462 additions and 3 deletions
|
|
@ -582,7 +582,7 @@ fn a_host_intent_out_of_bounds_exits_1_and_leaves_the_store_byte_identical() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn a_human_only_operator_is_unsupported_and_a_published_host_action_is_written() {
|
||||
fn an_operator_left_out_of_the_file_is_unsupported_and_a_published_host_action_is_written() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let path = host_store(dir.path());
|
||||
let before = std::fs::read(&path).unwrap();
|
||||
|
|
|
|||
241
surfaces/blender/tests/live/host_action.py
Normal file
241
surfaces/blender/tests/live/host_action.py
Normal file
|
|
@ -0,0 +1,241 @@
|
|||
"""Live proof, in GUI Blender with the add-on installed as an extension, that a host add-on's brokkr_actions.json adds
|
||||
rows the agent turn admits, runs and undoes like a built-in, and that an operator left out of the file stays human-only.
|
||||
|
||||
BROKKR_SCRATCH=<scratch dir: the blend file is saved here, so the default '//.brokkr/blender-editor.ccmp' lands here>
|
||||
BROKKR_ADDON_MODULE=<default bl_ext.user_default.brokkr_bridge, the extension install>
|
||||
BROKKR_CULTLIB=<CultLib packages dir>
|
||||
blender --python host_action.py -- <out>
|
||||
|
||||
Run it in Blender with private BLENDER_USER_CONFIG, BLENDER_USER_EXTENSIONS and BLENDER_USER_SCRIPTS directories
|
||||
(Blender 5.3 ignores BLENDER_USER_RESOURCE_PATH; without them the operator's own extensions folder is used). The script
|
||||
refuses to run when they are unset. It copies tests/fixtures/brokkr_host_fixture into BLENDER_USER_SCRIPTS/addons and
|
||||
enables it there; it writes nothing else outside BROKKR_SCRATCH. Every intent is put into the node database as
|
||||
brokkr-ctl does and answered by the add-on's own 1 s drain (the sink is on). Proves, with wm.undo_stack as the counter:
|
||||
|
||||
* the snapshot lists the fixture's four rows (host, no apply) and no hostActionErrors;
|
||||
* add_empty is accepted as exactly one new undo step, undo removes the empty, redo brings it back;
|
||||
* report is accepted with hostResult equal to what the operator wrote, no new step, humanEditSeq unchanged;
|
||||
* refuse is failed with the operator's text and cancel is failed, each with no new step;
|
||||
* apply (registered, left out of the file) and wm.save_mainfile are denied Unsupported with no step, the scene
|
||||
unchanged and no 'Applied' object.
|
||||
|
||||
Writes <out>.result.json, then quits Blender.
|
||||
"""
|
||||
import importlib
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
import traceback
|
||||
from pathlib import Path
|
||||
|
||||
import bpy
|
||||
|
||||
for _name in ("BLENDER_USER_CONFIG", "BLENDER_USER_EXTENSIONS", "BLENDER_USER_SCRIPTS"):
|
||||
if not os.environ.get(_name): # this Blender ignores BLENDER_USER_RESOURCE_PATH: without these, her own folders are used
|
||||
raise SystemExit(f"{_name} is not set: run this in private BLENDER_USER_CONFIG, BLENDER_USER_EXTENSIONS and BLENDER_USER_SCRIPTS directories")
|
||||
if os.path.normcase(os.path.abspath(bpy.utils.user_resource("EXTENSIONS"))) != os.path.normcase(os.path.abspath(os.environ["BLENDER_USER_EXTENSIONS"])):
|
||||
raise SystemExit("Blender's extensions folder is not BLENDER_USER_EXTENSIONS; refusing to touch it")
|
||||
if os.path.normcase(os.path.abspath(bpy.utils.user_resource("SCRIPTS"))) != os.path.normcase(os.path.abspath(os.environ["BLENDER_USER_SCRIPTS"])):
|
||||
raise SystemExit("Blender's scripts folder is not BLENDER_USER_SCRIPTS; refusing to touch it")
|
||||
|
||||
SCRATCH = Path(os.environ["BROKKR_SCRATCH"]).resolve()
|
||||
SCRATCH.mkdir(parents=True, exist_ok=True)
|
||||
OUT = sys.argv[sys.argv.index("--") + 1]
|
||||
MODULE = os.environ.get("BROKKR_ADDON_MODULE", "bl_ext.user_default.brokkr_bridge")
|
||||
HOST = "brokkr_host_fixture"
|
||||
FIXTURE = Path(__file__).resolve().parents[1] / "fixtures" / HOST
|
||||
R = {"blender": bpy.app.version_string, "module": MODULE, "checks": []}
|
||||
|
||||
addons_dir = Path(os.environ["BLENDER_USER_SCRIPTS"]) / "addons"
|
||||
addons_dir.mkdir(parents=True, exist_ok=True)
|
||||
if (addons_dir / HOST).exists():
|
||||
shutil.rmtree(addons_dir / HOST)
|
||||
shutil.copytree(FIXTURE, addons_dir / HOST, ignore=shutil.ignore_patterns("__pycache__"))
|
||||
bpy.utils.refresh_script_paths()
|
||||
|
||||
bpy.ops.preferences.addon_enable(module=MODULE)
|
||||
bpy.ops.preferences.addon_enable(module=HOST)
|
||||
addon = importlib.import_module(MODULE)
|
||||
bt = importlib.import_module(MODULE + ".blender_target")
|
||||
prefs = bpy.context.preferences.addons[MODULE].preferences
|
||||
bpy.ops.wm.save_as_mainfile(filepath=str(SCRATCH / "live.blend"))
|
||||
CACHE = str(SCRATCH / "queue.ccmp")
|
||||
prefs.cultmesh_cache_path = CACHE
|
||||
prefs.debug_mirror_root = ""
|
||||
prefs.auto_drain_commands = True # the sink
|
||||
prefs.cultlib_py_src = os.environ.get("BROKKR_CULTLIB", prefs.cultlib_py_src)
|
||||
adapter = addon.target()
|
||||
|
||||
|
||||
def check(name, ok, detail=None):
|
||||
R["checks"].append({"name": name, "ok": bool(ok), "detail": detail})
|
||||
|
||||
|
||||
def win():
|
||||
return bpy.context.temp_override(window=bpy.context.window_manager.windows[0])
|
||||
|
||||
|
||||
def steps_now():
|
||||
return [s.name for s in bpy.context.window_manager.undo_stack.steps]
|
||||
|
||||
|
||||
def undo():
|
||||
with win():
|
||||
bpy.ops.ed.undo()
|
||||
|
||||
|
||||
def redo():
|
||||
with win():
|
||||
bpy.ops.ed.redo()
|
||||
|
||||
|
||||
def node():
|
||||
return adapter._open_node(CACHE, prefs.cultlib_py_src)
|
||||
|
||||
|
||||
N = [0]
|
||||
|
||||
|
||||
def queue(**cmd):
|
||||
N[0] += 1
|
||||
cid = "host-%04d" % N[0]
|
||||
n, docs = node()
|
||||
cmd.update(commandId=cid, schema=bt.COMMAND_INTENT_SCHEMA, requestedBy="nibu/hands")
|
||||
n.database.put(docs["command_intent"], f"blender/commands/{cid}", cmd)
|
||||
return cid
|
||||
|
||||
|
||||
def receipt(cid):
|
||||
n, _ = node()
|
||||
return n.database.snapshot().get(bt.COMMAND_RECEIPT_SCHEMA, {}).get(f"blender/receipts/{cid}")
|
||||
|
||||
|
||||
def names():
|
||||
return sorted(o.name for o in bpy.data.objects)
|
||||
|
||||
|
||||
S = {}
|
||||
WAIT = {"ticks": 0}
|
||||
|
||||
|
||||
def until(pred, limit=40):
|
||||
def step():
|
||||
WAIT["ticks"] += 1
|
||||
if pred():
|
||||
WAIT["ticks"] = 0
|
||||
return True
|
||||
if WAIT["ticks"] > limit:
|
||||
raise TimeoutError("the drain did not receipt in time")
|
||||
return False
|
||||
return step
|
||||
|
||||
|
||||
def once(fn):
|
||||
def step():
|
||||
fn()
|
||||
return True
|
||||
return step
|
||||
|
||||
|
||||
def send(key, **fields):
|
||||
S[key + ":before"] = {"steps": steps_now(), "names": names(), "seq": adapter.human_edit_seq}
|
||||
S[key] = queue(**fields)
|
||||
|
||||
|
||||
def done(key):
|
||||
return lambda: receipt(S[key]) is not None
|
||||
|
||||
|
||||
def same_world(key):
|
||||
before = S[key + ":before"]
|
||||
return (steps_now() == before["steps"] and names() == before["names"] and adapter.human_edit_seq == before["seq"])
|
||||
|
||||
|
||||
def snapshot_check():
|
||||
snapshot = adapter.capture_snapshot(bpy.context, True)
|
||||
rows = snapshot["hostActions"]
|
||||
expect = [f"{HOST}.{n}" for n in ("add_empty", "report", "refuse", "cancel")]
|
||||
check("the snapshot lists the fixture's four rows with their host and none other (apply is left out)",
|
||||
sorted(r["name"] for r in rows) == sorted(expect) and {r["host"] for r in rows} == {HOST}, [r["name"] for r in rows])
|
||||
check("the snapshot carries no hostActionErrors", snapshot["hostActionErrors"] == [], snapshot["hostActionErrors"])
|
||||
check("the operators are registered by the legacy add-on, apply included",
|
||||
all(hasattr(getattr(bpy.ops, HOST), n) for n in ("add_empty", "report", "refuse", "cancel", "apply")))
|
||||
|
||||
|
||||
def add_check():
|
||||
r = receipt(S["add"])
|
||||
before = S["add:before"]
|
||||
after = steps_now()
|
||||
check("add_empty is accepted", r["status"] == "accepted", r)
|
||||
check("add_empty is exactly one new undo step carrying the receipt's label",
|
||||
len(after) == len(before["steps"]) + 1 and after[-1] == r["undoLabel"] and r["undoLabel"].startswith(f"Brokkr nibu/hands: {HOST}.add_empty "),
|
||||
{"before": before["steps"][-2:], "after": after[-2:], "label": r["undoLabel"]})
|
||||
check("add_empty made the empty with the declared name and size",
|
||||
"LiveEmpty" in bpy.data.objects and abs(bpy.data.objects["LiveEmpty"].empty_display_size - 2.0) < 1e-6, names())
|
||||
check("add_empty is the agent's write: humanEditSeq did not move", adapter.human_edit_seq == before["seq"])
|
||||
S["add:names"] = names()
|
||||
undo()
|
||||
check("one undo removes the empty", "LiveEmpty" not in bpy.data.objects, names())
|
||||
redo()
|
||||
check("redo brings it back", names() == S["add:names"], names())
|
||||
|
||||
|
||||
def report_check():
|
||||
r = receipt(S["report"])
|
||||
expect = f"objects: {len(bpy.data.objects)}\nscenes: {len(bpy.data.scenes)}"
|
||||
check("report is accepted with hostResult equal to what the operator wrote", r["status"] == "accepted" and r["hostResult"] == expect, [r["status"], r["hostResult"], expect])
|
||||
check("report pushed no undo step, added no object and left humanEditSeq alone", same_world("report"), [steps_now()[-2:], S["report:before"]["steps"][-2:]])
|
||||
|
||||
|
||||
def failure_check(key, text):
|
||||
def run():
|
||||
r = receipt(S[key])
|
||||
check(f"{key} is failed{' with ' + repr(text) if text else ''}", r["status"] == "failed" and text in r["message"], r)
|
||||
check(f"{key} pushed no undo step and changed nothing", same_world(key) and r["undoLabel"] == "", r["undoLabel"])
|
||||
return run
|
||||
|
||||
|
||||
def denied_check(key, action):
|
||||
def run():
|
||||
r = receipt(S[key])
|
||||
check(f"{action} is denied Unsupported", r["status"] == "denied" and "Unsupported Blender command action" in r["message"], r)
|
||||
check(f"{action} ran nothing: no step, no new object, no 'Applied'", same_world(key) and "Applied" not in bpy.data.objects, names())
|
||||
return run
|
||||
|
||||
|
||||
steps = [
|
||||
once(snapshot_check),
|
||||
once(lambda: send("add", action=f"{HOST}.add_empty", name="LiveEmpty", size=2)), until(done("add")), once(add_check),
|
||||
once(lambda: send("report", action=f"{HOST}.report")), until(done("report")), once(report_check),
|
||||
once(lambda: send("refuse", action=f"{HOST}.refuse")), until(done("refuse")), once(failure_check("refuse", "the fixture refuses")),
|
||||
once(lambda: send("cancel", action=f"{HOST}.cancel")), until(done("cancel")), once(failure_check("cancel", "cancelled; nothing changed")),
|
||||
once(lambda: send("apply", action=f"{HOST}.apply")), until(done("apply")), once(denied_check("apply", f"{HOST}.apply")),
|
||||
once(lambda: send("save", action="wm.save_mainfile")), until(done("save")), once(denied_check("save", "wm.save_mainfile")),
|
||||
]
|
||||
i = 0
|
||||
|
||||
|
||||
def write():
|
||||
R["ok"] = bool(R["checks"]) and all(c["ok"] for c in R["checks"]) and not R.get("errors")
|
||||
with open(OUT + ".result.json", "w") as f:
|
||||
json.dump(R, f, indent=1, default=str)
|
||||
|
||||
|
||||
def tick():
|
||||
global i
|
||||
if i < len(steps):
|
||||
try:
|
||||
if steps[i]():
|
||||
i += 1
|
||||
except Exception:
|
||||
R.setdefault("errors", []).append({"step": i, "error": traceback.format_exc()})
|
||||
i = len(steps)
|
||||
write()
|
||||
return 0.5
|
||||
write()
|
||||
bpy.ops.wm.quit_blender()
|
||||
return None
|
||||
|
||||
|
||||
bpy.app.timers.register(tick, first_interval=3.0)
|
||||
218
surfaces/blender/tests/live/host_action.result.json
Normal file
218
surfaces/blender/tests/live/host_action.result.json
Normal file
|
|
@ -0,0 +1,218 @@
|
|||
{
|
||||
"blender": "5.3.0",
|
||||
"module": "bl_ext.user_default.brokkr_bridge",
|
||||
"checks": [
|
||||
{
|
||||
"name": "the snapshot lists the fixture's four rows with their host and none other (apply is left out)",
|
||||
"ok": true,
|
||||
"detail": [
|
||||
"brokkr_host_fixture.add_empty",
|
||||
"brokkr_host_fixture.report",
|
||||
"brokkr_host_fixture.refuse",
|
||||
"brokkr_host_fixture.cancel"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "the snapshot carries no hostActionErrors",
|
||||
"ok": true,
|
||||
"detail": []
|
||||
},
|
||||
{
|
||||
"name": "the operators are registered by the legacy add-on, apply included",
|
||||
"ok": true,
|
||||
"detail": null
|
||||
},
|
||||
{
|
||||
"name": "add_empty is accepted",
|
||||
"ok": true,
|
||||
"detail": {
|
||||
"schema": "brokkr.blender.command_receipt.v0",
|
||||
"commandId": "host-0001",
|
||||
"action": "brokkr_host_fixture.add_empty",
|
||||
"status": "accepted",
|
||||
"message": "Operator brokkr_host_fixture.add_empty finished.",
|
||||
"objectName": "",
|
||||
"requestedBy": "nibu/hands",
|
||||
"note": "",
|
||||
"undoLabel": "Brokkr nibu/hands: brokkr_host_fixture.add_empty ost-0001",
|
||||
"lookPath": "",
|
||||
"hostResult": "",
|
||||
"observedAt": "2026-11-28T05:15:08.910684+00:00"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "add_empty is exactly one new undo step carrying the receipt's label",
|
||||
"ok": true,
|
||||
"detail": {
|
||||
"before": [
|
||||
"Original"
|
||||
],
|
||||
"after": [
|
||||
"Original",
|
||||
"Brokkr nibu/hands: brokkr_host_fixture.add_empty ost-0001"
|
||||
],
|
||||
"label": "Brokkr nibu/hands: brokkr_host_fixture.add_empty ost-0001"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "add_empty made the empty with the declared name and size",
|
||||
"ok": true,
|
||||
"detail": [
|
||||
"Camera",
|
||||
"Cube",
|
||||
"Light",
|
||||
"LiveEmpty"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "add_empty is the agent's write: humanEditSeq did not move",
|
||||
"ok": true,
|
||||
"detail": null
|
||||
},
|
||||
{
|
||||
"name": "one undo removes the empty",
|
||||
"ok": true,
|
||||
"detail": [
|
||||
"Camera",
|
||||
"Cube",
|
||||
"Light"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "redo brings it back",
|
||||
"ok": true,
|
||||
"detail": [
|
||||
"Camera",
|
||||
"Cube",
|
||||
"Light",
|
||||
"LiveEmpty"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "report is accepted with hostResult equal to what the operator wrote",
|
||||
"ok": true,
|
||||
"detail": [
|
||||
"accepted",
|
||||
"objects: 4\nscenes: 1",
|
||||
"objects: 4\nscenes: 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "report pushed no undo step, added no object and left humanEditSeq alone",
|
||||
"ok": true,
|
||||
"detail": [
|
||||
[
|
||||
"Original",
|
||||
"Brokkr nibu/hands: brokkr_host_fixture.add_empty ost-0001"
|
||||
],
|
||||
[
|
||||
"Original",
|
||||
"Brokkr nibu/hands: brokkr_host_fixture.add_empty ost-0001"
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "refuse is failed with 'the fixture refuses'",
|
||||
"ok": true,
|
||||
"detail": {
|
||||
"schema": "brokkr.blender.command_receipt.v0",
|
||||
"commandId": "host-0003",
|
||||
"action": "brokkr_host_fixture.refuse",
|
||||
"status": "failed",
|
||||
"message": "Error: the fixture refuses",
|
||||
"objectName": "",
|
||||
"requestedBy": "nibu/hands",
|
||||
"note": "",
|
||||
"undoLabel": "",
|
||||
"lookPath": "",
|
||||
"hostResult": "",
|
||||
"observedAt": "2026-11-28T05:15:13.022948+00:00"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "refuse pushed no undo step and changed nothing",
|
||||
"ok": true,
|
||||
"detail": ""
|
||||
},
|
||||
{
|
||||
"name": "cancel is failed with 'cancelled; nothing changed'",
|
||||
"ok": true,
|
||||
"detail": {
|
||||
"schema": "brokkr.blender.command_receipt.v0",
|
||||
"commandId": "host-0004",
|
||||
"action": "brokkr_host_fixture.cancel",
|
||||
"status": "failed",
|
||||
"message": "Operator brokkr_host_fixture.cancel cancelled; nothing changed.",
|
||||
"objectName": "",
|
||||
"requestedBy": "nibu/hands",
|
||||
"note": "",
|
||||
"undoLabel": "",
|
||||
"lookPath": "",
|
||||
"hostResult": "",
|
||||
"observedAt": "2026-11-28T05:15:14.080400+00:00"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "cancel pushed no undo step and changed nothing",
|
||||
"ok": true,
|
||||
"detail": ""
|
||||
},
|
||||
{
|
||||
"name": "brokkr_host_fixture.apply is denied Unsupported",
|
||||
"ok": true,
|
||||
"detail": {
|
||||
"schema": "brokkr.blender.command_receipt.v0",
|
||||
"commandId": "host-0005",
|
||||
"action": "brokkr_host_fixture.apply",
|
||||
"status": "denied",
|
||||
"message": "Unsupported Blender command action: brokkr_host_fixture.apply",
|
||||
"objectName": "",
|
||||
"requestedBy": "nibu/hands",
|
||||
"note": "",
|
||||
"undoLabel": "",
|
||||
"lookPath": "",
|
||||
"hostResult": "",
|
||||
"observedAt": "2026-11-28T05:15:16.137129+00:00"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "brokkr_host_fixture.apply ran nothing: no step, no new object, no 'Applied'",
|
||||
"ok": true,
|
||||
"detail": [
|
||||
"Camera",
|
||||
"Cube",
|
||||
"Light",
|
||||
"LiveEmpty"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "wm.save_mainfile is denied Unsupported",
|
||||
"ok": true,
|
||||
"detail": {
|
||||
"schema": "brokkr.blender.command_receipt.v0",
|
||||
"commandId": "host-0006",
|
||||
"action": "wm.save_mainfile",
|
||||
"status": "denied",
|
||||
"message": "Unsupported Blender command action: wm.save_mainfile",
|
||||
"objectName": "",
|
||||
"requestedBy": "nibu/hands",
|
||||
"note": "",
|
||||
"undoLabel": "",
|
||||
"lookPath": "",
|
||||
"hostResult": "",
|
||||
"observedAt": "2026-11-28T05:15:18.194603+00:00"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "wm.save_mainfile ran nothing: no step, no new object, no 'Applied'",
|
||||
"ok": true,
|
||||
"detail": [
|
||||
"Camera",
|
||||
"Cube",
|
||||
"Light",
|
||||
"LiveEmpty"
|
||||
]
|
||||
}
|
||||
],
|
||||
"ok": true
|
||||
}
|
||||
|
|
@ -372,7 +372,7 @@ class HostActionTests(unittest.TestCase):
|
|||
self.assertEqual(len(self.bpy.undo.pushes), 1)
|
||||
self.assertTrue(self.bpy.undo.pushes[0].endswith("(failed)"))
|
||||
|
||||
def test_a_human_only_operator_and_a_c_operator_are_unsupported_and_leave_no_trace(self):
|
||||
def test_an_operator_left_out_of_the_file_and_a_c_operator_are_unsupported_and_leave_no_trace(self):
|
||||
for action in (f"{HOST}.apply", "wm.save_mainfile", "brokkr.stop_agents"):
|
||||
receipt = self.run_action(action=action)
|
||||
self.assertEqual(receipt["status"], "denied", action)
|
||||
|
|
@ -385,7 +385,7 @@ class HostActionTests(unittest.TestCase):
|
|||
self.assertEqual(self.send("add_empty", name="E")["status"], "denied")
|
||||
self.assertEqual(self.send("report")["status"], "accepted")
|
||||
|
||||
def test_the_snapshot_lists_the_admitted_rows_with_their_host_and_no_human_only_row(self):
|
||||
def test_the_snapshot_lists_the_admitted_rows_with_their_host_and_no_left_out_row(self):
|
||||
self.bpy.data = NS(objects=[], scenes=[], collections=[], materials=[], images=[], filepath="")
|
||||
self.bpy.app = NS(version=(5, 3, 0), background=False)
|
||||
context = NS(selected_objects=[], active_object=None, scene=None, mode="OBJECT")
|
||||
|
|
|
|||
Loading…
Reference in a new issue