|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | +import os |
| 5 | +import sqlite3 |
| 6 | +import sys |
| 7 | +import tempfile |
| 8 | +import unittest |
| 9 | + |
| 10 | +REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 11 | +if REPO_ROOT not in sys.path: |
| 12 | + sys.path.insert(0, REPO_ROOT) |
| 13 | + |
| 14 | +from services.workspace_listing import list_workspace_projects |
| 15 | + |
| 16 | + |
| 17 | +def _make_workspace_storage(parent: str, *, layout_as_dict: bool) -> str: |
| 18 | + ws_root = os.path.join(parent, "workspaceStorage") |
| 19 | + global_root = os.path.join(parent, "globalStorage") |
| 20 | + os.makedirs(ws_root, exist_ok=True) |
| 21 | + os.makedirs(global_root, exist_ok=True) |
| 22 | + |
| 23 | + ws_dir = os.path.join(ws_root, "ws-a") |
| 24 | + os.makedirs(ws_dir, exist_ok=True) |
| 25 | + target_folder = os.path.join(parent, "real-project") |
| 26 | + os.makedirs(target_folder, exist_ok=True) |
| 27 | + with open(os.path.join(ws_dir, "workspace.json"), "w") as f: |
| 28 | + json.dump({"folder": f"file://{target_folder}"}, f) |
| 29 | + sqlite3.connect(os.path.join(ws_dir, "state.vscdb")).close() |
| 30 | + |
| 31 | + layout_payload: object |
| 32 | + if layout_as_dict: |
| 33 | + layout_payload = {"rootPath": target_folder} |
| 34 | + else: |
| 35 | + layout_payload = json.dumps({"rootPath": target_folder}) |
| 36 | + |
| 37 | + gdb = os.path.join(global_root, "state.vscdb") |
| 38 | + conn = sqlite3.connect(gdb) |
| 39 | + conn.execute("CREATE TABLE cursorDiskKV ([key] TEXT PRIMARY KEY, value TEXT)") |
| 40 | + conn.execute( |
| 41 | + "INSERT INTO cursorDiskKV ([key], value) VALUES (?, ?)", |
| 42 | + ( |
| 43 | + "composerData:cmp-1", |
| 44 | + json.dumps({ |
| 45 | + "name": "Test composer", |
| 46 | + "createdAt": 1_715_000_000_000, |
| 47 | + "lastUpdatedAt": 1_715_000_500_000, |
| 48 | + "fullConversationHeadersOnly": [{"bubbleId": "b-1"}], |
| 49 | + }), |
| 50 | + ), |
| 51 | + ) |
| 52 | + conn.execute( |
| 53 | + "INSERT INTO cursorDiskKV ([key], value) VALUES (?, ?)", |
| 54 | + ( |
| 55 | + "messageRequestContext:cmp-1:ctx-1", |
| 56 | + json.dumps({"projectLayouts": [layout_payload]}), |
| 57 | + ), |
| 58 | + ) |
| 59 | + conn.execute( |
| 60 | + "INSERT INTO cursorDiskKV ([key], value) VALUES (?, ?)", |
| 61 | + ("bubbleId:cmp-1:b-1", json.dumps({"type": "user", "text": "hello"})), |
| 62 | + ) |
| 63 | + conn.commit() |
| 64 | + conn.close() |
| 65 | + return ws_root |
| 66 | + |
| 67 | + |
| 68 | +class TestProjectLayoutsDictShape(unittest.TestCase): |
| 69 | + def _assert_assigned_to_workspace(self, ws_root: str) -> None: |
| 70 | + projects = list_workspace_projects(ws_root, rules=[]) |
| 71 | + ids = [p["id"] for p in projects] |
| 72 | + self.assertIn("ws-a", ids, msg=f"expected composer routed to ws-a, got {ids}") |
| 73 | + |
| 74 | + def test_string_shaped_layout(self): |
| 75 | + with tempfile.TemporaryDirectory() as tmp: |
| 76 | + ws_root = _make_workspace_storage(tmp, layout_as_dict=False) |
| 77 | + self._assert_assigned_to_workspace(ws_root) |
| 78 | + |
| 79 | + def test_dict_shaped_layout(self): |
| 80 | + with tempfile.TemporaryDirectory() as tmp: |
| 81 | + ws_root = _make_workspace_storage(tmp, layout_as_dict=True) |
| 82 | + self._assert_assigned_to_workspace(ws_root) |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + unittest.main() |
0 commit comments