"""Import/export bundles (main's tests/test_profile_bundle.py over HTTP).""" from helpers import new_id def test_profile_round_trip(c, profile): presets = c.get("/presets").json() a = next(iter(presets)) sid = new_id(c.post("/sequences", json={"name": "pytest-seq", "lanes": [[{"preset_id": a, "beats": 2}]]})) zid = c.get(f"/profiles/{profile}").json()["zones"][0] c.put(f"/zones/{zid}", json={"sequence_ids": [sid]}) c.put(f"/palettes/{c.get(f'/profiles/{profile}').json()['palette_id']}", json={"colors": ["#123456"]}) bundle = c.get(f"/profiles/{profile}/export").json() assert bundle["version"] == 1 and bundle["kind"] == "profile" assert bundle["palette"]["colors"] == ["#123456"] assert zid in bundle["zones"] and sid in bundle["sequences"] assert set(presets) == set(bundle["presets"]) resp = c.post("/profiles/import", json={"bundle": bundle, "name": "pytest-imported"}) assert resp.status_code == 201 new = resp.json()["id"] assert resp.json()[new]["name"] == "pytest-imported" assert c.get("/profiles/current").json()["id"] == new # applied by default imported = c.get("/presets").json() assert len(imported) == len(presets) and not set(imported) & set(presets) nz = c.get(f"/profiles/{new}").json()["zones"] assert len(nz) == 1 and nz[0] != zid zone = c.get(f"/zones/{nz[0]}").json() new_sid = zone["sequence_ids"][0] assert new_sid != sid seq = c.get(f"/sequences/{new_sid}").json() assert seq["lanes"][0][0]["preset_id"] in imported pal = c.get(f"/palettes/{c.get(f'/profiles/{new}').json()['palette_id']}").json() assert pal["colors"] == ["#123456"] c.delete(f"/profiles/{new}") def test_profile_import_rejects_other_bundles(c, profile): assert c.post("/profiles/import", json={"version": 2, "kind": "profile", "profile": {}}).status_code == 400 assert c.post("/profiles/import", json={"version": 1, "kind": "preset", "profile": {}}).status_code == 400 assert c.post("/profiles/import", json={"version": 1, "kind": "profile"}).status_code == 400 def test_export_unknown_profile(c): assert c.get("/profiles/999999/export").status_code == 404 def test_preset_round_trip(c, profile): a = next(iter(c.get("/presets").json())) bundle = c.get(f"/presets/{a}/export").json() assert bundle["kind"] == "preset" resp = c.post("/presets/import", json={"bundle": bundle}) assert resp.status_code == 201 new = new_id(resp) assert new != a assert resp.json()[new]["name"] == bundle["preset"]["name"] assert resp.json()[new]["profile_id"] == profile assert c.post("/presets/import", json={"kind": "sequence"}).status_code == 400 def test_sequence_round_trip(c, profile): a = next(iter(c.get("/presets").json())) sid = new_id(c.post("/sequences", json={"name": "pytest-seq", "lanes": [[{"preset_id": a, "beats": 3}]]})) bundle = c.get(f"/sequences/{sid}/export").json() assert bundle["kind"] == "sequence" and a in bundle["presets"] resp = c.post("/sequences/import", json={"bundle": bundle}) assert resp.status_code == 201 new = new_id(resp) step = resp.json()[new]["lanes"][0][0] assert step["beats"] == 3 and step["preset_id"] != a assert step["preset_id"] in c.get("/presets").json() def test_profile_import_folds_animation_ids(c, profile): """Older bundles list a zone's one-shots apart, in animation_ids: they land in sequence_ids.""" a = next(iter(c.get("/presets").json())) sid = new_id(c.post("/sequences", json={"name": "pytest-seq", "lanes": [[{"preset_id": a, "beats": 2}]]})) oid = new_id(c.post("/sequences", json={"name": "pytest-once", "loop": False, "lanes": [[{"preset_id": a, "beats": 1}]]})) zid = c.get(f"/profiles/{profile}").json()["zones"][0] c.put(f"/zones/{zid}", json={"sequence_ids": [sid, oid]}) bundle = c.get(f"/profiles/{profile}/export").json() z = bundle["zones"][zid] z["sequence_ids"], z["animation_ids"] = [sid], [oid] # as an older controller exported it new = c.post("/profiles/import", json={"bundle": bundle, "name": "pytest-imported"}).json()["id"] zone = c.get(f"/zones/{c.get(f'/profiles/{new}').json()['zones'][0]}").json() assert "animation_ids" not in zone names = [c.get(f"/sequences/{s}").json()["name"] for s in zone["sequence_ids"]] assert names == ["pytest-seq", "pytest-once"] c.delete(f"/profiles/{new}")