-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_workspace_tabs_malformed_nested.py
More file actions
217 lines (185 loc) · 8.08 KB
/
Copy pathtest_workspace_tabs_malformed_nested.py
File metadata and controls
217 lines (185 loc) · 8.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
from __future__ import annotations
import json
import os
import sqlite3
import sys
import tempfile
import unittest
from unittest.mock import patch
from flask import Flask
REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if REPO_ROOT not in sys.path:
sys.path.insert(0, REPO_ROOT)
from services.workspace_tabs import assemble_workspace_tabs
def _seed_workspace(parent: str) -> str:
ws_root = os.path.join(parent, "workspaceStorage")
global_root = os.path.join(parent, "globalStorage")
os.makedirs(ws_root, exist_ok=True)
os.makedirs(global_root, exist_ok=True)
ws_dir = os.path.join(ws_root, "ws-a")
os.makedirs(ws_dir, exist_ok=True)
with open(os.path.join(ws_dir, "workspace.json"), "w") as f:
json.dump({"folder": "/tmp/proj"}, f)
sqlite3.connect(os.path.join(ws_dir, "state.vscdb")).close()
conn = sqlite3.connect(os.path.join(global_root, "state.vscdb"))
conn.execute("CREATE TABLE cursorDiskKV ([key] TEXT PRIMARY KEY, value TEXT)")
conn.execute(
"INSERT INTO cursorDiskKV VALUES (?, ?)",
(
"composerData:cmp-1",
json.dumps({
"name": "Tab with bad header",
"createdAt": 1_715_000_000_000,
"lastUpdatedAt": 1_715_000_500_000,
"fullConversationHeadersOnly": [
None, # malformed: non-dict
"not a dict either", # malformed: string
{"bubbleId": "b-good", "type": 1}, # healthy
],
}),
),
)
conn.execute(
"INSERT INTO cursorDiskKV VALUES (?, ?)",
("bubbleId:cmp-1:b-good", json.dumps({"text": "hello"})),
)
conn.commit()
conn.close()
return ws_root
class TestNonDictHeaderDoesNotDropComposer(unittest.TestCase):
def test_malformed_headers_skipped_composer_still_rendered(self) -> None:
app = Flask(__name__)
app.config["TESTING"] = True
app.config["EXCLUSION_RULES"] = []
with tempfile.TemporaryDirectory() as tmp:
ws_root = _seed_workspace(tmp)
with app.test_request_context("/api/workspaces/global/tabs"):
payload, status = assemble_workspace_tabs("global", ws_root, rules=[])
self.assertEqual(status, 200)
ids = [t["id"] for t in payload.get("tabs", [])]
self.assertIn("cmp-1", ids)
def _seed_workspace_with_diff(parent: str, *, diff_timestamp: int | None) -> str:
ws_root = os.path.join(parent, "workspaceStorage")
global_root = os.path.join(parent, "globalStorage")
os.makedirs(ws_root, exist_ok=True)
os.makedirs(global_root, exist_ok=True)
ws_dir = os.path.join(ws_root, "ws-a")
os.makedirs(ws_dir, exist_ok=True)
with open(os.path.join(ws_dir, "workspace.json"), "w") as f:
json.dump({"folder": "/tmp/proj"}, f)
sqlite3.connect(os.path.join(ws_dir, "state.vscdb")).close()
bubble_ts = 1_715_000_500_000
conn = sqlite3.connect(os.path.join(global_root, "state.vscdb"))
conn.execute("CREATE TABLE cursorDiskKV ([key] TEXT PRIMARY KEY, value TEXT)")
conn.execute(
"INSERT INTO cursorDiskKV VALUES (?, ?)",
(
"composerData:cmp-d",
json.dumps({
"name": "Tab with diff",
"createdAt": 1_715_000_000_000,
"lastUpdatedAt": bubble_ts,
"fullConversationHeadersOnly": [{"bubbleId": "b1", "type": 1}],
}),
),
)
conn.execute(
"INSERT INTO cursorDiskKV VALUES (?, ?)",
("bubbleId:cmp-d:b1", json.dumps({"text": "user msg", "createdAt": bubble_ts})),
)
diff_payload: dict = {"filePath": "src/main.py", "command": "format"}
if diff_timestamp is not None:
diff_payload["timestamp"] = diff_timestamp
conn.execute(
"INSERT INTO cursorDiskKV VALUES (?, ?)",
("codeBlockDiff:cmp-d:diff1", json.dumps(diff_payload)),
)
conn.commit()
conn.close()
return ws_root
class TestDiffsEmittedOnlyAsCodeBlockDiffs(unittest.TestCase):
"""codeBlockDiffs are the single representation on the wire — never
duplicated as synthetic ``Tool Action`` bubbles in tab.bubbles."""
def test_diffs_appear_in_code_block_diffs_field(self) -> None:
app = Flask(__name__)
app.config["TESTING"] = True
app.config["EXCLUSION_RULES"] = []
diff_ts = 1_715_000_700_000
with tempfile.TemporaryDirectory() as tmp:
ws_root = _seed_workspace_with_diff(tmp, diff_timestamp=diff_ts)
with app.test_request_context("/api/workspaces/global/tabs"):
payload, _ = assemble_workspace_tabs("global", ws_root, rules=[])
tab = next((t for t in payload["tabs"] if t["id"] == "cmp-d"), None)
self.assertIsNotNone(tab)
assert tab is not None
self.assertTrue(tab["codeBlockDiffs"], "expected diffs on tab.codeBlockDiffs")
def test_diffs_do_not_appear_as_synthetic_bubbles(self) -> None:
app = Flask(__name__)
app.config["TESTING"] = True
app.config["EXCLUSION_RULES"] = []
with tempfile.TemporaryDirectory() as tmp:
ws_root = _seed_workspace_with_diff(tmp, diff_timestamp=None)
with app.test_request_context("/api/workspaces/global/tabs"):
payload, _ = assemble_workspace_tabs("global", ws_root, rules=[])
tab = next(t for t in payload["tabs"] if t["id"] == "cmp-d")
tool_action_bubbles = [
b for b in tab["bubbles"] if (b.get("text") or "").startswith("**Tool Action:**")
]
self.assertEqual(tool_action_bubbles, [],
msg="diffs must not be double-represented as synthetic AI bubbles")
def _seed_workspace_with_tool_former(parent: str) -> str:
ws_root = os.path.join(parent, "workspaceStorage")
global_root = os.path.join(parent, "globalStorage")
os.makedirs(ws_root, exist_ok=True)
os.makedirs(global_root, exist_ok=True)
ws_dir = os.path.join(ws_root, "ws-a")
os.makedirs(ws_dir, exist_ok=True)
with open(os.path.join(ws_dir, "workspace.json"), "w") as f:
json.dump({"folder": "/tmp/proj"}, f)
sqlite3.connect(os.path.join(ws_dir, "state.vscdb")).close()
conn = sqlite3.connect(os.path.join(global_root, "state.vscdb"))
conn.execute("CREATE TABLE cursorDiskKV ([key] TEXT PRIMARY KEY, value TEXT)")
conn.execute(
"INSERT INTO cursorDiskKV VALUES (?, ?)",
(
"composerData:cmp-t",
json.dumps({
"name": "Tab with toolFormerData",
"createdAt": 1_715_000_000_000,
"lastUpdatedAt": 1_715_000_500_000,
"fullConversationHeadersOnly": [{"bubbleId": "b-t", "type": 2}],
}),
),
)
conn.execute(
"INSERT INTO cursorDiskKV VALUES (?, ?)",
(
"bubbleId:cmp-t:b-t",
json.dumps({
"text": "assistant message",
"createdAt": 1_715_000_400_000,
"toolFormerData": {"name": "tool-x"},
}),
),
)
conn.commit()
conn.close()
return ws_root
class TestParseToolCallNonDictReturn(unittest.TestCase):
def test_non_dict_parse_result_does_not_drop_composer(self) -> None:
app = Flask(__name__)
app.config["TESTING"] = True
app.config["EXCLUSION_RULES"] = []
with tempfile.TemporaryDirectory() as tmp:
ws_root = _seed_workspace_with_tool_former(tmp)
# Force _parse_tool_call to return None — the previous code
# would have stored ``tool_calls = [None]`` and crashed in the
# display-text fallback with ``NoneType.get``.
with patch("services.workspace_tabs._parse_tool_call", return_value=None):
with app.test_request_context("/api/workspaces/global/tabs"):
payload, status = assemble_workspace_tabs("global", ws_root, rules=[])
self.assertEqual(status, 200)
ids = [t["id"] for t in payload.get("tabs", [])]
self.assertIn("cmp-t", ids)
if __name__ == "__main__":
unittest.main()