|
| 1 | +"""Regression test for issue #50: NULL bubble value crashes GET /tabs. |
| 2 | +
|
| 3 | +A cursorDiskKV row with a NULL value column previously caused |
| 4 | +json.loads(None) -> TypeError, which propagated as a 500 response. |
| 5 | +The fix uses ``_try_loads_kv_value`` in ``services/workspace_tabs.py`` so |
| 6 | +NULL / unparseable cursorDiskKV values are skipped without raising. |
| 7 | +""" |
| 8 | + |
| 9 | +import json |
| 10 | +import os |
| 11 | +import sqlite3 |
| 12 | +import tempfile |
| 13 | +import unittest |
| 14 | + |
| 15 | +from services.workspace_tabs import assemble_workspace_tabs |
| 16 | + |
| 17 | +# cursorDiskKV keys use typed prefixes; tabs[].id is the bare suffix only |
| 18 | +# (assemble_workspace_tabs: composer_id = row["key"].split(":")[1]). |
| 19 | +COMPOSER_ID = "composer-abc" |
| 20 | +COMPOSER_KV_KEY = f"composerData:{COMPOSER_ID}" |
| 21 | + |
| 22 | + |
| 23 | +class TestNullBubbleValueDoesNotCrashTabs(unittest.TestCase): |
| 24 | + def setUp(self): |
| 25 | + self.tmp = tempfile.TemporaryDirectory() |
| 26 | + base = self.tmp.name |
| 27 | + |
| 28 | + # Minimal workspaceStorage layout expected by assemble_workspace_tabs. |
| 29 | + ws_dir = os.path.join(base, "workspaceStorage") |
| 30 | + os.makedirs(ws_dir) |
| 31 | + |
| 32 | + # Global storage with a cursorDiskKV table containing a NULL-value bubble row. |
| 33 | + global_dir = os.path.join(base, "globalStorage") |
| 34 | + os.makedirs(global_dir) |
| 35 | + db_path = os.path.join(global_dir, "state.vscdb") |
| 36 | + conn = sqlite3.connect(db_path) |
| 37 | + conn.execute("CREATE TABLE cursorDiskKV ([key] TEXT PRIMARY KEY, value TEXT)") |
| 38 | + conn.execute( |
| 39 | + "INSERT INTO cursorDiskKV ([key], value) VALUES (?, ?)", |
| 40 | + (f"bubbleId:{COMPOSER_ID}:bubble-null", None), # NULL value — the crash case |
| 41 | + ) |
| 42 | + # Healthy bubble that should surface in the assembled tab. |
| 43 | + conn.execute( |
| 44 | + "INSERT INTO cursorDiskKV ([key], value) VALUES (?, ?)", |
| 45 | + ( |
| 46 | + f"bubbleId:{COMPOSER_ID}:bubble-ok", |
| 47 | + json.dumps({"type": 1, "text": "hello world", "createdAt": 1739200000000}), |
| 48 | + ), |
| 49 | + ) |
| 50 | + # Composer referencing the healthy bubble — required for a tab to be built. |
| 51 | + conn.execute( |
| 52 | + "INSERT INTO cursorDiskKV ([key], value) VALUES (?, ?)", |
| 53 | + ( |
| 54 | + COMPOSER_KV_KEY, |
| 55 | + json.dumps({ |
| 56 | + "name": "Test Chat", |
| 57 | + "modelConfig": {"modelName": "gpt-4o"}, |
| 58 | + "fullConversationHeadersOnly": [{"bubbleId": "bubble-ok", "type": 1}], |
| 59 | + "lastUpdatedAt": 1739300000000, |
| 60 | + "createdAt": 1739200000000, |
| 61 | + }), |
| 62 | + ), |
| 63 | + ) |
| 64 | + conn.commit() |
| 65 | + conn.close() |
| 66 | + |
| 67 | + self.workspace_path = ws_dir |
| 68 | + |
| 69 | + def tearDown(self): |
| 70 | + self.tmp.cleanup() |
| 71 | + |
| 72 | + def test_null_bubble_row_is_skipped_without_exception(self): |
| 73 | + """assemble_workspace_tabs must not raise when a bubble row has NULL value.""" |
| 74 | + try: |
| 75 | + _payload, status = assemble_workspace_tabs( |
| 76 | + workspace_id="global", |
| 77 | + workspace_path=self.workspace_path, |
| 78 | + rules=[], |
| 79 | + ) |
| 80 | + except TypeError as exc: |
| 81 | + self.fail(f"NULL bubble row raised TypeError: {exc}") |
| 82 | + |
| 83 | + self.assertEqual(status, 200, "NULL bubble row must not turn tabs load into an error response") |
| 84 | + |
| 85 | + def test_healthy_bubbles_still_load_when_null_row_present(self): |
| 86 | + """The healthy bubble surfaces in a tab even when a NULL row is present.""" |
| 87 | + payload, status = assemble_workspace_tabs( |
| 88 | + workspace_id="global", |
| 89 | + workspace_path=self.workspace_path, |
| 90 | + rules=[], |
| 91 | + ) |
| 92 | + self.assertEqual(status, 200, "tabs endpoint must succeed when only the null bubble row is bad") |
| 93 | + self.assertIsInstance(payload, dict, "tabs response must be a JSON object envelope") |
| 94 | + tabs = payload.get("tabs", []) |
| 95 | + self.assertEqual(len(tabs), 1, f"Expected exactly one tab for {COMPOSER_ID}") |
| 96 | + |
| 97 | + tab = tabs[0] |
| 98 | + # GET /tabs and workspace.html ?tab= use bare composer id, not the KV key. |
| 99 | + self.assertEqual(tab["id"], COMPOSER_ID, "tab id must be bare composer id (KV key suffix only)") |
| 100 | + self.assertNotEqual(tab["id"], COMPOSER_KV_KEY, "tab id must not include composerData: prefix") |
| 101 | + self.assertEqual(tab["title"], "Test Chat", "composer name from seeded cursorDiskKV row") |
| 102 | + self.assertIn("bubbles", tab, "tab payload must include bubbles for the conversation view") |
| 103 | + self.assertIn("codeBlockDiffs", tab, "tab payload must include codeBlockDiffs field (may be empty)") |
| 104 | + |
| 105 | + bubbles = tab["bubbles"] |
| 106 | + self.assertEqual(len(bubbles), 1, "Expected exactly one bubble (null row skipped)") |
| 107 | + bubble = bubbles[0] |
| 108 | + self.assertEqual(bubble["type"], "user", "header type 1 maps to user bubble") |
| 109 | + self.assertEqual(bubble["text"], "hello world", "healthy bubble text must surface in the tab") |
| 110 | + self.assertIn("timestamp", bubble, "bubble must carry a timestamp for ordering/display") |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + unittest.main() |
0 commit comments