|
| 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 | +from flask import Flask |
| 11 | + |
| 12 | +REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 13 | +if REPO_ROOT not in sys.path: |
| 14 | + sys.path.insert(0, REPO_ROOT) |
| 15 | + |
| 16 | +from services.workspace_tabs import assemble_workspace_tabs |
| 17 | + |
| 18 | + |
| 19 | +def _seed_workspace(parent: str) -> str: |
| 20 | + ws_root = os.path.join(parent, "workspaceStorage") |
| 21 | + global_root = os.path.join(parent, "globalStorage") |
| 22 | + os.makedirs(ws_root, exist_ok=True) |
| 23 | + os.makedirs(global_root, exist_ok=True) |
| 24 | + |
| 25 | + ws_dir = os.path.join(ws_root, "ws-a") |
| 26 | + os.makedirs(ws_dir, exist_ok=True) |
| 27 | + with open(os.path.join(ws_dir, "workspace.json"), "w") as f: |
| 28 | + json.dump({"folder": "/tmp/proj"}, f) |
| 29 | + sqlite3.connect(os.path.join(ws_dir, "state.vscdb")).close() |
| 30 | + |
| 31 | + conn = sqlite3.connect(os.path.join(global_root, "state.vscdb")) |
| 32 | + conn.execute("CREATE TABLE cursorDiskKV ([key] TEXT PRIMARY KEY, value TEXT)") |
| 33 | + conn.execute( |
| 34 | + "INSERT INTO cursorDiskKV VALUES (?, ?)", |
| 35 | + ( |
| 36 | + "composerData:cmp-1", |
| 37 | + json.dumps({ |
| 38 | + "name": "Tab with bad header", |
| 39 | + "createdAt": 1_715_000_000_000, |
| 40 | + "lastUpdatedAt": 1_715_000_500_000, |
| 41 | + "fullConversationHeadersOnly": [ |
| 42 | + None, # malformed: non-dict |
| 43 | + "not a dict either", # malformed: string |
| 44 | + {"bubbleId": "b-good", "type": 1}, # healthy |
| 45 | + ], |
| 46 | + }), |
| 47 | + ), |
| 48 | + ) |
| 49 | + conn.execute( |
| 50 | + "INSERT INTO cursorDiskKV VALUES (?, ?)", |
| 51 | + ("bubbleId:cmp-1:b-good", json.dumps({"text": "hello"})), |
| 52 | + ) |
| 53 | + conn.commit() |
| 54 | + conn.close() |
| 55 | + return ws_root |
| 56 | + |
| 57 | + |
| 58 | +class TestNonDictHeaderDoesNotDropComposer(unittest.TestCase): |
| 59 | + def test_malformed_headers_skipped_composer_still_rendered(self) -> None: |
| 60 | + app = Flask(__name__) |
| 61 | + app.config["TESTING"] = True |
| 62 | + app.config["EXCLUSION_RULES"] = [] |
| 63 | + |
| 64 | + with tempfile.TemporaryDirectory() as tmp: |
| 65 | + ws_root = _seed_workspace(tmp) |
| 66 | + with app.test_request_context("/api/workspaces/global/tabs"): |
| 67 | + payload, status = assemble_workspace_tabs("global", ws_root, rules=[]) |
| 68 | + |
| 69 | + self.assertEqual(status, 200) |
| 70 | + ids = [t["id"] for t in payload.get("tabs", [])] |
| 71 | + self.assertIn("cmp-1", ids) |
| 72 | + |
| 73 | + |
| 74 | +def _seed_workspace_with_diff(parent: str, *, diff_timestamp: int | None) -> str: |
| 75 | + ws_root = os.path.join(parent, "workspaceStorage") |
| 76 | + global_root = os.path.join(parent, "globalStorage") |
| 77 | + os.makedirs(ws_root, exist_ok=True) |
| 78 | + os.makedirs(global_root, exist_ok=True) |
| 79 | + ws_dir = os.path.join(ws_root, "ws-a") |
| 80 | + os.makedirs(ws_dir, exist_ok=True) |
| 81 | + with open(os.path.join(ws_dir, "workspace.json"), "w") as f: |
| 82 | + json.dump({"folder": "/tmp/proj"}, f) |
| 83 | + sqlite3.connect(os.path.join(ws_dir, "state.vscdb")).close() |
| 84 | + |
| 85 | + bubble_ts = 1_715_000_500_000 |
| 86 | + conn = sqlite3.connect(os.path.join(global_root, "state.vscdb")) |
| 87 | + conn.execute("CREATE TABLE cursorDiskKV ([key] TEXT PRIMARY KEY, value TEXT)") |
| 88 | + conn.execute( |
| 89 | + "INSERT INTO cursorDiskKV VALUES (?, ?)", |
| 90 | + ( |
| 91 | + "composerData:cmp-d", |
| 92 | + json.dumps({ |
| 93 | + "name": "Tab with diff", |
| 94 | + "createdAt": 1_715_000_000_000, |
| 95 | + "lastUpdatedAt": bubble_ts, |
| 96 | + "fullConversationHeadersOnly": [{"bubbleId": "b1", "type": 1}], |
| 97 | + }), |
| 98 | + ), |
| 99 | + ) |
| 100 | + conn.execute( |
| 101 | + "INSERT INTO cursorDiskKV VALUES (?, ?)", |
| 102 | + ("bubbleId:cmp-d:b1", json.dumps({"text": "user msg", "createdAt": bubble_ts})), |
| 103 | + ) |
| 104 | + diff_payload: dict = {"filePath": "src/main.py", "command": "format"} |
| 105 | + if diff_timestamp is not None: |
| 106 | + diff_payload["timestamp"] = diff_timestamp |
| 107 | + conn.execute( |
| 108 | + "INSERT INTO cursorDiskKV VALUES (?, ?)", |
| 109 | + ("codeBlockDiff:cmp-d:diff1", json.dumps(diff_payload)), |
| 110 | + ) |
| 111 | + conn.commit() |
| 112 | + conn.close() |
| 113 | + return ws_root |
| 114 | + |
| 115 | + |
| 116 | +class TestSyntheticBubbleTimestampNotNow(unittest.TestCase): |
| 117 | + def test_synthetic_uses_diff_timestamp_when_present(self) -> None: |
| 118 | + app = Flask(__name__) |
| 119 | + app.config["TESTING"] = True |
| 120 | + app.config["EXCLUSION_RULES"] = [] |
| 121 | + diff_ts = 1_715_000_700_000 |
| 122 | + |
| 123 | + with tempfile.TemporaryDirectory() as tmp: |
| 124 | + ws_root = _seed_workspace_with_diff(tmp, diff_timestamp=diff_ts) |
| 125 | + with app.test_request_context("/api/workspaces/global/tabs"): |
| 126 | + payload, _ = assemble_workspace_tabs("global", ws_root, rules=[]) |
| 127 | + |
| 128 | + tab = next((t for t in payload["tabs"] if t["id"] == "cmp-d"), None) |
| 129 | + self.assertIsNotNone(tab) |
| 130 | + synthetic = next(b for b in tab["bubbles"] if b["text"].startswith("**Tool Action:**")) |
| 131 | + self.assertEqual(synthetic["timestamp"], diff_ts) |
| 132 | + |
| 133 | + def test_synthetic_falls_back_to_max_bubble_timestamp(self) -> None: |
| 134 | + app = Flask(__name__) |
| 135 | + app.config["TESTING"] = True |
| 136 | + app.config["EXCLUSION_RULES"] = [] |
| 137 | + |
| 138 | + with tempfile.TemporaryDirectory() as tmp: |
| 139 | + ws_root = _seed_workspace_with_diff(tmp, diff_timestamp=None) |
| 140 | + with app.test_request_context("/api/workspaces/global/tabs"): |
| 141 | + payload, _ = assemble_workspace_tabs("global", ws_root, rules=[]) |
| 142 | + |
| 143 | + tab = next(t for t in payload["tabs"] if t["id"] == "cmp-d") |
| 144 | + synthetic = next(b for b in tab["bubbles"] if b["text"].startswith("**Tool Action:**")) |
| 145 | + # synthetic must NOT use datetime.now() — it should be at or before |
| 146 | + # the latest real bubble (which is the 1_715_000_500_000 we seeded, |
| 147 | + # far in the past relative to today's time). |
| 148 | + now_ms = int(__import__("datetime").datetime.now().timestamp() * 1000) |
| 149 | + self.assertLess(synthetic["timestamp"], now_ms - 10_000_000_000) |
| 150 | + |
| 151 | + |
| 152 | +if __name__ == "__main__": |
| 153 | + unittest.main() |
0 commit comments