From 2fdb2c4dcf8403a45d565972570c7bdf13b64efc Mon Sep 17 00:00:00 2001 From: Antoni Ivanov <262547018+tony-xmelon@users.noreply.github.com> Date: Fri, 11 Sep 2026 13:15:52 +0300 Subject: [PATCH 1/2] Use authoritative keys for preset mutations --- pyquadcortex/protocol/client.py | 95 +++++++++++++++----- tests/test_client.py | 150 +++++++++++++++++++++++++++----- tests/test_hardware_markers.py | 1 + 3 files changed, 205 insertions(+), 41 deletions(-) diff --git a/pyquadcortex/protocol/client.py b/pyquadcortex/protocol/client.py index 4e9e102..110a0bd 100644 --- a/pyquadcortex/protocol/client.py +++ b/pyquadcortex/protocol/client.py @@ -3964,39 +3964,92 @@ def save_current_preset( return e.name return None - def delete_preset(self, setlist_path: str, name: str): - """Delete the preset named ``name`` from the setlist at ``setlist_path``. - - Confirmed by capture: deleting "Test save to user - sl" from slot 28E sent ``File{action: DELETE, type: 0, folder{key: - , is_factory: false, files{key: "/.pb"}}}`` - the preset is addressed by its device FILE - PATH (name-based, ``.pb`` extension), NOT by slot index. + def _stored_preset(self, setlist_path: str, preset, listing=None): + """Resolve a name or listed ProductData to its authoritative file key.""" + if isinstance(preset, pa.ProductData): + entry = preset + else: + if listing is None: + listing = self.list_presets(setlist_path) + matches = [ + entry for entry in listing + if field_present(entry, "name") and entry.name == preset + ] + if not matches: + raise ValueError(f"preset {preset!r} is not in {setlist_path!r}") + if len(matches) != 1: + raise ValueError( + f"preset name {preset!r} is ambiguous in {setlist_path!r}; " + "pass the ProductData returned by list_presets()" + ) + entry = matches[0] + if not (field_present(entry, "key") and entry.key): + raise ValueError("stored preset has no authoritative product key") + return entry + + def delete_preset(self, setlist_path: str, name): + """Delete a stored preset from ``setlist_path``. + + ``name`` is either its exact display name or the ``ProductData`` + returned by :meth:`list_presets`. Cortex Control addresses the selection + by that catalog entry's authoritative, potentially opaque product key; + it does not reconstruct a path from the display name. """ + entry = self._stored_preset(setlist_path, name) msg = pa.FileMessage(action=pa.MessageAction.DELETE, type=0) msg.folder.key = setlist_path msg.folder.is_factory = False - msg.folder.files.add().key = f"{setlist_path}/{name}.pb" + msg.folder.files.add().key = entry.key return self._file_operation(msg) - def move_preset(self, setlist_path: str, name: str, to_position): - """Move the preset named ``name`` to slot ``to_position`` (same setlist). + def move_preset(self, setlist_path: str, name, to_position): + """Move or swap a stored preset into ``to_position`` in the same setlist. ``to_position`` is either the linear slot index or the slot name shown on - the unit (``"28D"``). - - Confirmed by capture: dragging "Darkglass AO900 - 2_1" onto slot 28D sent ``File{action: MOVE, type: 0, folder{key: - , files{key: "/.pb"}}, - to_folder{key: , files{index: 219}}}`` - source by FILE - PATH, destination by LINEAR slot index. + the unit (``"28D"``). An empty target uses File MOVE and an occupied + target uses File SWAP, matching Cortex Control's ``isSaved`` branch. + Occupancy is authoritative product-key presence, not display text. """ - msg = pa.FileMessage(action=pa.MessageAction.MOVE, type=0) + position = _as_position(to_position) + listing = self.list_presets(setlist_path, include_empty=True) + entry = self._stored_preset(setlist_path, name, listing) + if field_present(entry, "index") and entry.index == position: + raise ValueError("source and destination positions are identical") + target = next( + (item for item in listing + if field_present(item, "index") and item.index == position), + None, + ) + occupied = target is not None and field_present(target, "key") and bool(target.key) + action = pa.MessageAction.SWAP if occupied else pa.MessageAction.MOVE + msg = pa.FileMessage(action=action, type=0) msg.folder.key = setlist_path msg.folder.is_factory = False - msg.folder.files.add().key = f"{setlist_path}/{name}.pb" + if not occupied: + msg.folder.is_downloads = False + msg.folder.files.add().key = entry.key msg.to_folder.key = setlist_path - msg.to_folder.files.add().index = _as_position(to_position) + msg.to_folder.files.add().index = position + return self._file_operation(msg) + + def rename_preset(self, setlist_path: str, preset, name: str): + """Rename a stored preset using Cortex Control's sparse File UPDATE. + + The authoritative product key and instrument metadata come from the + selected catalog entry. This operation neither recalls nor re-saves the + preset. + """ + entry = self._stored_preset(setlist_path, preset) + msg = pa.FileMessage(action=pa.MessageAction.UPDATE, type=0) + msg.folder.key = setlist_path + msg.folder.is_factory = False + updated = msg.folder.files.add() + updated.key = entry.key + updated.name = name + updated.instrument = ( + entry.instrument if field_present(entry, "instrument") + else int(Instrument.NONE) + ) return self._file_operation(msg) diff --git a/tests/test_client.py b/tests/test_client.py index daa4377..fcf0cd9 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -427,10 +427,26 @@ def test_save_current_preset_sends_file_create_by_reference(): ) -def test_delete_preset_sends_file_delete_by_path(): - # CONFIRMED wire shape (Windows capture 2): delete addresses the preset by - # its device file path "/.pb", not by slot index. - qc = client.QuadCortex(FakeTransport()) +def _preset_listing(setlist, *entries): + listing = pa.FileMessage() + listing.folder.key = setlist + for values in entries: + entry = listing.folder.files.add() + for field, value in values.items(): + setattr(entry, field, value) + return listing + + +def test_delete_preset_uses_the_authoritative_catalog_key(): + setlist = "/media/p4/Presets/My Presets" + listing = _preset_listing(setlist, { + "index": 220, + "name": "Test save to user sl", + "key": "opaque-device-key-220", + }) + fake = FakeTransport() + fake.broadcast = listing + qc = client.QuadCortex(fake) qc.delete_preset("/media/p4/Presets/My Presets", "Test save to user sl") sent = qc._t.sent[-1] assert isinstance(sent, pa.FileMessage) @@ -438,26 +454,108 @@ def test_delete_preset_sends_file_delete_by_path(): assert sent.type == 0 assert sent.folder.key == "/media/p4/Presets/My Presets" assert sent.folder.is_factory is False - assert ( - sent.folder.files[0].key - == "/media/p4/Presets/My Presets/Test save to user sl.pb" + assert sent.folder.files[0].key == "opaque-device-key-220" + assert not sent.folder.files[0].HasField("name") + assert not sent.folder.files[0].HasField("index") + assert not sent.HasField("to_folder") + + +def test_move_preset_sends_file_move_to_an_empty_keyless_slot(): + setlist = "/media/p4/Presets/My Presets" + listing = _preset_listing( + setlist, + {"index": 218, "name": "Darkglass AO900 2_1", "key": "opaque-source"}, + {"index": 219, "name": "Unsaved"}, ) - - -def test_move_preset_sends_file_move(): - # CONFIRMED wire shape (Windows capture 2): source by file path, - # destination by linear slot index in to_folder. - qc = client.QuadCortex(FakeTransport()) + fake = FakeTransport() + fake.broadcast = listing + qc = client.QuadCortex(fake) qc.move_preset("/media/p4/Presets/My Presets", "Darkglass AO900 2_1", 219) sent = qc._t.sent[-1] assert isinstance(sent, pa.FileMessage) assert sent.action == pa.MessageAction.MOVE - assert ( - sent.folder.files[0].key - == "/media/p4/Presets/My Presets/Darkglass AO900 2_1.pb" - ) + assert sent.folder.is_downloads is False + assert sent.folder.files[0].key == "opaque-source" assert sent.to_folder.key == "/media/p4/Presets/My Presets" assert sent.to_folder.files[0].index == 219 + assert not sent.to_folder.HasField("is_factory") + + +def test_move_preset_uses_swap_for_an_occupied_slot(): + setlist = "/media/p4/Presets/Live" + listing = _preset_listing( + setlist, + {"index": 1, "name": "Source", "key": "opaque-source"}, + {"index": 2, "name": "Target", "key": "opaque-target"}, + ) + fake = FakeTransport() + fake.broadcast = listing + qc = client.QuadCortex(fake) + qc.move_preset(setlist, listing.folder.files[0], 2) + sent = fake.sent[-1] + assert sent.action == pa.MessageAction.SWAP + assert not sent.folder.HasField("is_downloads") + assert sent.folder.files[0].key == "opaque-source" + assert sent.to_folder.files[0].index == 2 + + +def test_move_preset_matches_the_captured_cortex_builder_bytes(): + setlist = "/media/p4/Presets/My Presets" + product_key = f"{setlist}/Numb.pb" + listing = _preset_listing( + setlist, + {"index": 55, "name": "Numb", "key": product_key}, + {"index": 54, "name": "Unsaved"}, + ) + fake = FakeTransport() + fake.broadcast = listing + qc = client.QuadCortex(fake) + qc.move_preset(setlist, listing.folder.files[0], 54) + assert fake.sent[-1].SerializeToString() == bytes.fromhex( + "08 04 18 00 22 4a 0a 1c 2f 6d 65 64 69 61 2f 70 34 2f 50 72 65 73 " + "65 74 73 2f 4d 79 20 50 72 65 73 65 74 73 20 00 28 00 3a 26 0a 24 " + "2f 6d 65 64 69 61 2f 70 34 2f 50 72 65 73 65 74 73 2f 4d 79 20 50 " + "72 65 73 65 74 73 2f 4e 75 6d 62 2e 70 62 2a 22 0a 1c 2f 6d 65 64 " + "69 61 2f 70 34 2f 50 72 65 73 65 74 73 2f 4d 79 20 50 72 65 73 65 " + "74 73 3a 02 10 36" + ) + + +def test_rename_preset_preserves_key_and_instrument_metadata(): + setlist = "/media/p4/Presets/Live" + listing = _preset_listing(setlist, { + "index": 3, + "name": "Old", + "key": "opaque-device-product-key", + "instrument": 2, + }) + fake = FakeTransport() + fake.broadcast = listing + qc = client.QuadCortex(fake) + qc.rename_preset(setlist, "Old", "Renamed") + sent = fake.sent[-1] + assert sent.action == pa.MessageAction.UPDATE + assert sent.folder.key == setlist + assert sent.folder.is_factory is False + renamed = sent.folder.files[0] + assert renamed.key == "opaque-device-product-key" + assert renamed.name == "Renamed" + assert renamed.instrument == 2 + assert not renamed.HasField("index") + assert not sent.HasField("to_folder") + + +def test_rename_preset_matches_cortex_control_builder_bytes(): + setlist = "/media/p4/Presets/Live" + entry = pa.ProductData(key="opaque-device-product-key", instrument=2) + qc = client.QuadCortex(FakeTransport()) + qc.rename_preset(setlist, entry, "Renamed") + assert qc._t.sent[-1].SerializeToString() == bytes.fromhex( + "08 01 18 00 22 42 0a 16 2f 6d 65 64 69 61 2f 70 34 2f 50 72 65 73 " + "65 74 73 2f 4c 69 76 65 20 00 3a 26 0a 19 6f 70 61 71 75 65 2d 64 " + "65 76 69 63 65 2d 70 72 6f 64 75 63 74 2d 6b 65 79 1a 07 52 65 6e " + "61 6d 65 64 78 02" + ) # -- session hello ------------------------------------------------------------- @@ -561,6 +659,11 @@ def test_find_preset_looks_a_preset_up_by_name(): def test_save_and_move_accept_slot_names(): fake = FakeTransport() + fake.broadcast = _preset_listing( + str(Setlist.USER), + {"index": 1, "name": "Some Preset", "key": "opaque-source"}, + {"index": 219, "name": "Unsaved"}, + ) qc = client.QuadCortex(fake) qc.save_current_preset(Setlist.USER, "30A", "Some Preset") @@ -790,9 +893,16 @@ def test_file_operations_do_not_raise_when_the_device_stays_silent(): # File ops are asynchronous and every host write is STALLed, so a missing reply # says nothing about success. Raising made callers wrap each one in # try/except and verify by re-reading anyway. - qc = client.QuadCortex(TimingOutTransport()) - assert qc.delete_preset(Setlist.USER, "Some Preset") is None - assert qc.move_preset(Setlist.USER, "Some Preset", "28D") is None + transport = TimingOutTransport() + transport.broadcast = _preset_listing( + str(Setlist.USER), + {"index": 1, "name": "Some Preset", "key": "opaque-source"}, + {"index": 219, "name": "Unsaved"}, + ) + qc = client.QuadCortex(transport) + source = transport.broadcast.folder.files[0] + assert qc.delete_preset(Setlist.USER, source) is None + assert qc.move_preset(Setlist.USER, source, "28D") is None assert qc.save_current_preset(Setlist.USER, "30A", "Some Preset") == "Some Preset" diff --git a/tests/test_hardware_markers.py b/tests/test_hardware_markers.py index cfbc5a6..4df5e7d 100644 --- a/tests/test_hardware_markers.py +++ b/tests/test_hardware_markers.py @@ -32,6 +32,7 @@ "delete_preset": "no hardware test yet; only scratch_preset's teardown deletes anything, and a teardown asserts nothing", "copy_preset": "no hardware test yet; it writes into a second setlist, so a failed run leaves a stray preset in the owner's Directory", "move_preset": "no hardware test yet; it rearranges the slots a player has laid out for a gig, which a restore cannot make invisible mid-run", + "rename_preset": "no Python same-commit hardware test yet; the exact Cortex File UPDATE previously failed firmware readback and needs a disposable stored preset", "read_preset": "no hardware test yet; it RECALLS the preset it reads, so it is a write in disguise and needs scratch_preset", "find_preset": "no hardware test yet; it needs a name that exists on the unit, which the loaded preset supplies - just not written", "wait_for_listing": "no hardware test yet; it is the polling wrapper round list_presets and says nothing until a write changes a listing", From 4311f6a6719fd600c7deccca55b125554d93fa4f Mon Sep 17 00:00:00 2001 From: Antoni Ivanov <262547018+tony-xmelon@users.noreply.github.com> Date: Fri, 11 Sep 2026 22:08:44 +0300 Subject: [PATCH 2/2] Narrow preset mutations to measured shapes --- changelog.md | 10 +++ docs/api.md | 2 +- docs/manual-coverage.md | 4 +- docs/protocol.md | 4 +- pyquadcortex/protocol/client.py | 112 ++++++++++++-------------------- tests/test_client.py | 106 +++++++++++------------------- tests/test_hardware_markers.py | 1 - 7 files changed, 95 insertions(+), 144 deletions(-) diff --git a/changelog.md b/changelog.md index d8b2b27..9cfbd6d 100644 --- a/changelog.md +++ b/changelog.md @@ -20,6 +20,16 @@ correction. ## Unreleased +### Preserve device-provided preset keys without adding a listing dependency + +`delete_preset()` and `move_preset()` now also accept the `ProductData` returned +by `list_presets()`, validate that its key belongs to the requested setlist, and +send that key unchanged. The exact-name form remains listing-free and preserves +the measured CorOS 4.0.1 `/.pb` wire shape, including hardware +cleanup when listing reads are silent. The captured move shape now also carries +its explicit `is_downloads: false` field. Unmeasured rename and occupied-slot +swap behavior are deliberately not exposed. + ### The first hardware run of the profile seam, and what it corrected Run 2026-09-07 on Quad Cortex, CorOS 4.0.1 / d14e, straight after the seam diff --git a/docs/api.md b/docs/api.md index c34a0ce..85a008d 100644 --- a/docs/api.md +++ b/docs/api.md @@ -75,7 +75,7 @@ already read and need no connection; calling them as methods raises | **Device list** | `pin_model()`, `unpin_model()`, `pinned_models()`, `master_volume()` | | **Neural Captures** | `captures()`, `list_irs()` to browse the library, `set_capture(cell, entry)` to place one. Creating a capture is the unit's own wizard - disconnect first, since a connected client suppresses it | | **Discovery** | `list_folders()` - every folder the device knows, including the factory Captures Library and plugin artist presets; `recents()`, `favorites()`, `add_favorite()`, `remove_favorite()` | -| **Manage presets** | `save_current_preset(setlist, slot, name)`, `delete_preset(setlist, name)`, `move_preset(setlist, name, to_slot)` | +| **Manage presets** | `save_current_preset(setlist, slot, name)`, `delete_preset(setlist, preset)`, `move_preset(setlist, preset, to_slot)`; `preset` may be the exact name or a `ProductData` returned by `list_presets()` | **Rows and columns are zero-based, and the unit displays rows 1 to 4.** `row=0` is the top row on screen and `row=2` is the one labelled 3. This matters more than it diff --git a/docs/manual-coverage.md b/docs/manual-coverage.md index 97d4fc8..47e4f2e 100644 --- a/docs/manual-coverage.md +++ b/docs/manual-coverage.md @@ -127,8 +127,8 @@ which this document had over-read as unreachable, and it answers a READ perfectl | Preset descriptive tags | n/a | not preserved by ANY save path - the unit's own Save As strips them too (factory 5D's six tags -> none), so they are build-chain/cloud metadata no library can write. The instrument category is separate, survives, and is fully mapped (`Instrument`: Guitar 1, Bass 2, Synth 3, Vocal 4, Other 5) | | Preset description, author, cloud id | no | ignored by a `Grid` update. The device stamps `author_name` from the signed-in cloud account on every save | | Preset volume and pan | n/a | ignored by every route tried, and the unit has no control for them - they read 1.0 and 0.5 on every preset. Inert fields, not a gap | -| Delete a preset | yes | `delete_preset()`, eventually consistent. `delete_setlist()` removes a whole setlist | -| Move a preset | yes | `move_preset()`, same-setlist only observed | +| Delete a preset | yes | `delete_preset()`, eventually consistent. Pass a listing's `ProductData` to preserve the device-provided key, or the exact display name for the measured 4.0.1 path shape. `delete_setlist()` removes a whole setlist | +| Move a preset | yes | `move_preset()`, same-setlist only observed; accepts the same device-provided `ProductData` or exact-name forms as delete | | Factory and My Presets setlists | yes | `Setlist.FACTORY`, `Setlist.USER` | | User folders / additional setlists | yes | `create_setlist()` makes them and `list_folders()` finds them; `list_presets()` accepts any key. CC#32's 'User folders' 2-12 are created, not built in | | Create a folder, nested navigation | yes | `create_setlist(name)`. The earlier failure was the path: setlists are siblings under `/media/p4/Presets`, not children of My Presets | diff --git a/docs/protocol.md b/docs/protocol.md index cbe3b80..6d0d5b6 100644 --- a/docs/protocol.md +++ b/docs/protocol.md @@ -2802,8 +2802,8 @@ screen; **captured only** = seen on the wire, with no independent read-back. | `set_scene_label` / `set_scene_color` | `SceneLabel` / `SceneColor{UPDATE, index, label/color}` | read-back | color is ARGB uint32; exact round-trip | | `copy_scene` | `SceneCopy{UPDATE, from_index, to_index, is_swap}` | read-back + on-unit | fully confirmed: `from_index` (copying B onto D produced B, not A), `is_swap` (scenes exchanged), and that the scene LABEL and COLOUR both travel with the state. Cortex Control cannot copy a scene, so the shape came from the device's own broadcast when copying on the unit | | `save_current_preset` | `File{CREATE, folder{key, files{index, name, instrument}}}` | read-back | snapshots the GRID; `preset_payload` is IGNORED for CREATE | -| `delete_preset` | `File{DELETE, folder{files{key: "/.pb"}}}` | read-back | works, but asynchronous: a listing within about 2 s is stale, about 5 s is reliable | -| `move_preset` | `File{MOVE, folder{files{key}}, to_folder{files{index}}}` | read-back | source by file path, destination by index; asynchronous like delete | +| `delete_preset` | `File{DELETE, folder{files{key: "/.pb"}}}` | three Cortex Control 4.0.1 captures + read-back | Every occupied entry in those captures exposed that exact key. The API also accepts the listing's `ProductData`, validates that its device-provided key belongs to the named setlist, and sends the key unchanged. Works, but asynchronously: a listing within about 2 s is stale, about 5 s is reliable | +| `move_preset` | `File{MOVE, folder{is_downloads: false, files{key}}, to_folder{files{index}}}` | Cortex Control 4.0.1 capture + read-back | Source by file path, destination by index; asynchronous like delete. The explicit false flag is present on the captured wire shape. The API accepts either the exact name or a listing `ProductData` and never waits for a listing before sending | | `set_param_scene_mode` | `Grid{UPDATE, ..., params{index, scene_mode}}` (flag ALONE) | read-back | promotes a parameter to scene-following; a value in the same message voids it | | `set_chain_output` | `Grid{UPDATE, preset{chains{row, out_portid}}}` | read-back | required for a new chain: the device never assigns an output on its own | | `set_param(Mixer(row), ...)` | `Grid{UPDATE, preset{chains{row, mixer{params{index, param_values}}}}}` | read-back | supports per-scene; how factory presets build scenes | diff --git a/pyquadcortex/protocol/client.py b/pyquadcortex/protocol/client.py index 110a0bd..8167e92 100644 --- a/pyquadcortex/protocol/client.py +++ b/pyquadcortex/protocol/client.py @@ -3964,92 +3964,62 @@ def save_current_preset( return e.name return None - def _stored_preset(self, setlist_path: str, preset, listing=None): - """Resolve a name or listed ProductData to its authoritative file key.""" - if isinstance(preset, pa.ProductData): - entry = preset - else: - if listing is None: - listing = self.list_presets(setlist_path) - matches = [ - entry for entry in listing - if field_present(entry, "name") and entry.name == preset - ] - if not matches: - raise ValueError(f"preset {preset!r} is not in {setlist_path!r}") - if len(matches) != 1: - raise ValueError( - f"preset name {preset!r} is ambiguous in {setlist_path!r}; " - "pass the ProductData returned by list_presets()" - ) - entry = matches[0] - if not (field_present(entry, "key") and entry.key): - raise ValueError("stored preset has no authoritative product key") - return entry + def _stored_preset_key(self, setlist_path: str, preset) -> str: + """Resolve a display name or validate a listed ProductData key.""" + if not isinstance(preset, pa.ProductData): + # Backward-compatible and deliberately listing-free: hardware + # teardown depends on a best-effort delete even when listing reads + # are silent. On measured 4.0.1 listings this reconstructed path is + # byte-for-byte the same value as the device-supplied key. + return f"{setlist_path.rstrip('/')}/{preset}.pb" + if not (field_present(preset, "key") and preset.key): + raise ControlNotDrivable( + "stored preset product key", + "the supplied ProductData carries no key, so the device did " + "not provide an address", + "pass a populated ProductData from list_presets(), or pass " + "the exact display name to use the measured 4.0.1 path shape", + ) + prefix = setlist_path.rstrip("/") + "/" + if not preset.key.startswith(prefix): + raise ValueError( + f"preset key {preset.key!r} does not belong to " + f"setlist {setlist_path!r}") + return preset.key - def delete_preset(self, setlist_path: str, name): + def delete_preset(self, setlist_path: str, preset): """Delete a stored preset from ``setlist_path``. - ``name`` is either its exact display name or the ``ProductData`` - returned by :meth:`list_presets`. Cortex Control addresses the selection - by that catalog entry's authoritative, potentially opaque product key; - it does not reconstruct a path from the display name. + ``preset`` is either its exact display name or the ``ProductData`` + returned by :meth:`list_presets`. On three CorOS 4.0.1 captures every + occupied entry's key was exactly ``/.pb``; passing a + listing entry avoids reconstructing it, while the name form remains a + listing-free compatibility path for best-effort cleanup. """ - entry = self._stored_preset(setlist_path, name) msg = pa.FileMessage(action=pa.MessageAction.DELETE, type=0) msg.folder.key = setlist_path msg.folder.is_factory = False - msg.folder.files.add().key = entry.key + msg.folder.files.add().key = self._stored_preset_key( + setlist_path, preset) return self._file_operation(msg) - def move_preset(self, setlist_path: str, name, to_position): - """Move or swap a stored preset into ``to_position`` in the same setlist. + def move_preset(self, setlist_path: str, preset, to_position): + """Move a stored preset into ``to_position`` in the same setlist. ``to_position`` is either the linear slot index or the slot name shown on - the unit (``"28D"``). An empty target uses File MOVE and an occupied - target uses File SWAP, matching Cortex Control's ``isSaved`` branch. - Occupancy is authoritative product-key presence, not display text. + the unit (``"28D"``). ``preset`` may be its exact display name or the + ``ProductData`` returned by :meth:`list_presets`; the latter supplies + the device's own key. Cortex Control 4.0.1 was captured adding + ``is_downloads: false`` to this MOVE shape. """ - position = _as_position(to_position) - listing = self.list_presets(setlist_path, include_empty=True) - entry = self._stored_preset(setlist_path, name, listing) - if field_present(entry, "index") and entry.index == position: - raise ValueError("source and destination positions are identical") - target = next( - (item for item in listing - if field_present(item, "index") and item.index == position), - None, - ) - occupied = target is not None and field_present(target, "key") and bool(target.key) - action = pa.MessageAction.SWAP if occupied else pa.MessageAction.MOVE - msg = pa.FileMessage(action=action, type=0) + msg = pa.FileMessage(action=pa.MessageAction.MOVE, type=0) msg.folder.key = setlist_path msg.folder.is_factory = False - if not occupied: - msg.folder.is_downloads = False - msg.folder.files.add().key = entry.key + msg.folder.is_downloads = False + msg.folder.files.add().key = self._stored_preset_key( + setlist_path, preset) msg.to_folder.key = setlist_path - msg.to_folder.files.add().index = position - return self._file_operation(msg) - - def rename_preset(self, setlist_path: str, preset, name: str): - """Rename a stored preset using Cortex Control's sparse File UPDATE. - - The authoritative product key and instrument metadata come from the - selected catalog entry. This operation neither recalls nor re-saves the - preset. - """ - entry = self._stored_preset(setlist_path, preset) - msg = pa.FileMessage(action=pa.MessageAction.UPDATE, type=0) - msg.folder.key = setlist_path - msg.folder.is_factory = False - updated = msg.folder.files.add() - updated.key = entry.key - updated.name = name - updated.instrument = ( - entry.instrument if field_present(entry, "instrument") - else int(Instrument.NONE) - ) + msg.to_folder.files.add().index = _as_position(to_position) return self._file_operation(msg) diff --git a/tests/test_client.py b/tests/test_client.py index fcf0cd9..cc8ac12 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -442,63 +442,71 @@ def test_delete_preset_uses_the_authoritative_catalog_key(): listing = _preset_listing(setlist, { "index": 220, "name": "Test save to user sl", - "key": "opaque-device-key-220", + "key": f"{setlist}/opaque-device-key-220", }) fake = FakeTransport() - fake.broadcast = listing qc = client.QuadCortex(fake) - qc.delete_preset("/media/p4/Presets/My Presets", "Test save to user sl") + qc.delete_preset(setlist, listing.folder.files[0]) sent = qc._t.sent[-1] assert isinstance(sent, pa.FileMessage) assert sent.action == pa.MessageAction.DELETE assert sent.type == 0 assert sent.folder.key == "/media/p4/Presets/My Presets" assert sent.folder.is_factory is False - assert sent.folder.files[0].key == "opaque-device-key-220" + assert sent.folder.files[0].key == f"{setlist}/opaque-device-key-220" assert not sent.folder.files[0].HasField("name") assert not sent.folder.files[0].HasField("index") assert not sent.HasField("to_folder") -def test_move_preset_sends_file_move_to_an_empty_keyless_slot(): +def test_delete_preset_by_name_stays_listing_free_for_hardware_cleanup(): + setlist = "/media/p4/Presets/My Presets" + qc = client.QuadCortex(FakeTransport()) + qc.delete_preset(setlist, "Test save to user sl") + assert qc._t.sent[-1].folder.files[0].key == \ + f"{setlist}/Test save to user sl.pb" + + +def test_a_product_key_must_belong_to_the_named_setlist(): + qc = client.QuadCortex(FakeTransport()) + wrong = pa.ProductData( + key="/media/p4/Presets/Factory Presets/Cali Basswalk.pb") + with pytest.raises(ValueError, match="does not belong"): + qc.delete_preset("/media/p4/Presets/My Presets", wrong) + assert qc._t.sent == [] + + +def test_a_product_without_a_key_refuses_with_auditable_evidence(): + qc = client.QuadCortex(FakeTransport()) + with pytest.raises(ControlNotDrivable) as caught: + qc.delete_preset("/media/p4/Presets/My Presets", + pa.ProductData(name="Unnamed")) + assert caught.value.control == "stored preset product key" + assert "no key" in caught.value.evidence + assert "list_presets" in caught.value.workaround + + +def test_move_preset_uses_an_authoritative_key_and_the_captured_flag(): setlist = "/media/p4/Presets/My Presets" listing = _preset_listing( setlist, - {"index": 218, "name": "Darkglass AO900 2_1", "key": "opaque-source"}, + {"index": 218, "name": "Darkglass AO900 2_1", + "key": f"{setlist}/opaque-source"}, {"index": 219, "name": "Unsaved"}, ) fake = FakeTransport() - fake.broadcast = listing qc = client.QuadCortex(fake) - qc.move_preset("/media/p4/Presets/My Presets", "Darkglass AO900 2_1", 219) + qc.move_preset(setlist, listing.folder.files[0], 219) sent = qc._t.sent[-1] assert isinstance(sent, pa.FileMessage) assert sent.action == pa.MessageAction.MOVE assert sent.folder.is_downloads is False - assert sent.folder.files[0].key == "opaque-source" + assert sent.folder.files[0].key == f"{setlist}/opaque-source" assert sent.to_folder.key == "/media/p4/Presets/My Presets" assert sent.to_folder.files[0].index == 219 assert not sent.to_folder.HasField("is_factory") -def test_move_preset_uses_swap_for_an_occupied_slot(): - setlist = "/media/p4/Presets/Live" - listing = _preset_listing( - setlist, - {"index": 1, "name": "Source", "key": "opaque-source"}, - {"index": 2, "name": "Target", "key": "opaque-target"}, - ) - fake = FakeTransport() - fake.broadcast = listing - qc = client.QuadCortex(fake) - qc.move_preset(setlist, listing.folder.files[0], 2) - sent = fake.sent[-1] - assert sent.action == pa.MessageAction.SWAP - assert not sent.folder.HasField("is_downloads") - assert sent.folder.files[0].key == "opaque-source" - assert sent.to_folder.files[0].index == 2 - - def test_move_preset_matches_the_captured_cortex_builder_bytes(): setlist = "/media/p4/Presets/My Presets" product_key = f"{setlist}/Numb.pb" @@ -508,7 +516,6 @@ def test_move_preset_matches_the_captured_cortex_builder_bytes(): {"index": 54, "name": "Unsaved"}, ) fake = FakeTransport() - fake.broadcast = listing qc = client.QuadCortex(fake) qc.move_preset(setlist, listing.folder.files[0], 54) assert fake.sent[-1].SerializeToString() == bytes.fromhex( @@ -521,43 +528,6 @@ def test_move_preset_matches_the_captured_cortex_builder_bytes(): ) -def test_rename_preset_preserves_key_and_instrument_metadata(): - setlist = "/media/p4/Presets/Live" - listing = _preset_listing(setlist, { - "index": 3, - "name": "Old", - "key": "opaque-device-product-key", - "instrument": 2, - }) - fake = FakeTransport() - fake.broadcast = listing - qc = client.QuadCortex(fake) - qc.rename_preset(setlist, "Old", "Renamed") - sent = fake.sent[-1] - assert sent.action == pa.MessageAction.UPDATE - assert sent.folder.key == setlist - assert sent.folder.is_factory is False - renamed = sent.folder.files[0] - assert renamed.key == "opaque-device-product-key" - assert renamed.name == "Renamed" - assert renamed.instrument == 2 - assert not renamed.HasField("index") - assert not sent.HasField("to_folder") - - -def test_rename_preset_matches_cortex_control_builder_bytes(): - setlist = "/media/p4/Presets/Live" - entry = pa.ProductData(key="opaque-device-product-key", instrument=2) - qc = client.QuadCortex(FakeTransport()) - qc.rename_preset(setlist, entry, "Renamed") - assert qc._t.sent[-1].SerializeToString() == bytes.fromhex( - "08 01 18 00 22 42 0a 16 2f 6d 65 64 69 61 2f 70 34 2f 50 72 65 73 " - "65 74 73 2f 4c 69 76 65 20 00 3a 26 0a 19 6f 70 61 71 75 65 2d 64 " - "65 76 69 63 65 2d 70 72 6f 64 75 63 74 2d 6b 65 79 1a 07 52 65 6e " - "61 6d 65 64 78 02" - ) - - # -- session hello ------------------------------------------------------------- @@ -661,7 +631,8 @@ def test_save_and_move_accept_slot_names(): fake = FakeTransport() fake.broadcast = _preset_listing( str(Setlist.USER), - {"index": 1, "name": "Some Preset", "key": "opaque-source"}, + {"index": 1, "name": "Some Preset", + "key": f"{Setlist.USER}/opaque-source"}, {"index": 219, "name": "Unsaved"}, ) qc = client.QuadCortex(fake) @@ -896,7 +867,8 @@ def test_file_operations_do_not_raise_when_the_device_stays_silent(): transport = TimingOutTransport() transport.broadcast = _preset_listing( str(Setlist.USER), - {"index": 1, "name": "Some Preset", "key": "opaque-source"}, + {"index": 1, "name": "Some Preset", + "key": f"{Setlist.USER}/opaque-source"}, {"index": 219, "name": "Unsaved"}, ) qc = client.QuadCortex(transport) diff --git a/tests/test_hardware_markers.py b/tests/test_hardware_markers.py index 4df5e7d..cfbc5a6 100644 --- a/tests/test_hardware_markers.py +++ b/tests/test_hardware_markers.py @@ -32,7 +32,6 @@ "delete_preset": "no hardware test yet; only scratch_preset's teardown deletes anything, and a teardown asserts nothing", "copy_preset": "no hardware test yet; it writes into a second setlist, so a failed run leaves a stray preset in the owner's Directory", "move_preset": "no hardware test yet; it rearranges the slots a player has laid out for a gig, which a restore cannot make invisible mid-run", - "rename_preset": "no Python same-commit hardware test yet; the exact Cortex File UPDATE previously failed firmware readback and needs a disposable stored preset", "read_preset": "no hardware test yet; it RECALLS the preset it reads, so it is a write in disguise and needs scratch_preset", "find_preset": "no hardware test yet; it needs a name that exists on the unit, which the loaded preset supplies - just not written", "wait_for_listing": "no hardware test yet; it is the polling wrapper round list_presets and says nothing until a write changes a listing",