-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathtest_cli_tool_edit_note_integration.py
More file actions
351 lines (300 loc) · 9.1 KB
/
test_cli_tool_edit_note_integration.py
File metadata and controls
351 lines (300 loc) · 9.1 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
"""Integration tests for `basic-memory tool edit-note`."""
import json
from typer.testing import CliRunner
from basic_memory.cli.main import app as cli_app
runner = CliRunner()
def _write_note(title: str, folder: str, content: str, project: str | None = None) -> dict:
args = [
"tool",
"write-note",
"--title",
title,
"--folder",
folder,
"--content",
content,
]
if project:
args.extend(["--project", project])
result = runner.invoke(cli_app, args)
assert result.exit_code == 0, result.output
return json.loads(result.stdout)
def _read_note(identifier: str, project: str | None = None) -> dict:
args = ["tool", "read-note", identifier]
if project:
args.extend(["--project", project])
result = runner.invoke(cli_app, args)
assert result.exit_code == 0, result.output
return json.loads(result.stdout)
def test_edit_note_append_success(app, app_config, test_project, config_manager):
"""append operation adds content to the end of the note."""
note = _write_note(
"Edit Append Note",
"edit-tests",
"# Append\n\nBASE_APPEND_MARKER",
)
result = runner.invoke(
cli_app,
[
"tool",
"edit-note",
note["permalink"],
"--operation",
"append",
"--content",
"\nAPPENDED_MARKER",
],
)
assert result.exit_code == 0, result.output
updated = _read_note(note["permalink"])
assert updated["content"].index("APPENDED_MARKER") > updated["content"].index(
"BASE_APPEND_MARKER"
)
def test_edit_note_prepend_success(app, app_config, test_project, config_manager):
"""prepend operation inserts content before existing body content."""
note = _write_note(
"Edit Prepend Note",
"edit-tests",
"# Prepend\n\nBASE_PREPEND_MARKER",
)
result = runner.invoke(
cli_app,
[
"tool",
"edit-note",
note["permalink"],
"--operation",
"prepend",
"--content",
"PREPENDED_MARKER\n",
],
)
assert result.exit_code == 0, result.output
updated = _read_note(note["permalink"])
assert updated["content"].index("PREPENDED_MARKER") < updated["content"].index(
"BASE_PREPEND_MARKER"
)
def test_edit_note_find_replace_success_with_expected_count(
app, app_config, test_project, config_manager
):
"""find_replace succeeds when expected replacement count matches actual count."""
note = _write_note(
"Edit Replace Note",
"edit-tests",
"# Replace\n\nFIND_ME_MARKER and FIND_ME_MARKER",
)
result = runner.invoke(
cli_app,
[
"tool",
"edit-note",
note["permalink"],
"--operation",
"find_replace",
"--content",
"REPLACED_MARKER",
"--find-text",
"FIND_ME_MARKER",
"--expected-replacements",
"2",
],
)
assert result.exit_code == 0, result.output
updated = _read_note(note["permalink"])
assert "FIND_ME_MARKER" not in updated["content"]
assert updated["content"].count("REPLACED_MARKER") == 2
def test_edit_note_find_replace_fails_without_find_text(
app, app_config, test_project, config_manager
):
"""find_replace requires --find-text."""
note = _write_note(
"Edit Missing Find Note",
"edit-tests",
"# Missing Find\n\nOriginal",
)
result = runner.invoke(
cli_app,
[
"tool",
"edit-note",
note["permalink"],
"--operation",
"find_replace",
"--content",
"Replacement",
],
)
assert result.exit_code != 0
assert "find_text parameter is required for find_replace operation" in result.output
def test_edit_note_replace_section_success(app, app_config, test_project, config_manager):
"""replace_section updates exactly the targeted section body."""
note = _write_note(
"Edit Section Note",
"edit-tests",
"# Header\n\n## Keep\nKeep body\n\n## Target Section\nOld section body\n\n## After\nAfter body",
)
result = runner.invoke(
cli_app,
[
"tool",
"edit-note",
note["permalink"],
"--operation",
"replace_section",
"--content",
"New section body",
"--section",
"## Target Section",
],
)
assert result.exit_code == 0, result.output
updated = _read_note(note["permalink"])
assert "New section body" in updated["content"]
assert "Old section body" not in updated["content"]
assert "## After" in updated["content"]
def test_edit_note_replace_section_fails_without_section(
app, app_config, test_project, config_manager
):
"""replace_section requires --section."""
note = _write_note(
"Edit Missing Section Note",
"edit-tests",
"# Missing Section\n\nBody",
)
result = runner.invoke(
cli_app,
[
"tool",
"edit-note",
note["permalink"],
"--operation",
"replace_section",
"--content",
"Replacement body",
],
)
assert result.exit_code != 0
assert "section parameter is required for section-based operations" in result.output
def test_edit_note_append_creates_nonexistent_note_cli(
app, app_config, test_project, config_manager
):
"""append to a non-existent note via CLI should auto-create and include fileCreated."""
result = runner.invoke(
cli_app,
[
"tool",
"edit-note",
"cli-tests/auto-created-note",
"--operation",
"append",
"--content",
"# Auto Created\n\nCreated via CLI append.",
],
)
assert result.exit_code == 0, result.output
data = json.loads(result.stdout)
assert data["fileCreated"] is True
assert data["operation"] == "append"
assert data["title"] is not None
# Verify the note is readable
read_data = _read_note(data["permalink"])
assert "Auto Created" in read_data["content"]
def test_edit_note_json_format_contract(app, app_config, test_project, config_manager):
"""JSON output returns metadata keys required by contract."""
note = _write_note(
"Edit JSON Note",
"edit-tests",
"# JSON\n\nBody",
)
result = runner.invoke(
cli_app,
[
"tool",
"edit-note",
note["permalink"],
"--operation",
"append",
"--content",
"\nJSON_MARKER",
],
)
assert result.exit_code == 0, result.output
data = json.loads(result.stdout)
assert set(data.keys()) == {
"title",
"permalink",
"file_path",
"operation",
"checksum",
"fileCreated",
}
assert data["operation"] == "append"
assert data["fileCreated"] is False
assert data["title"] == "Edit JSON Note"
def test_edit_note_backend_failure_returns_nonzero(app, app_config, test_project, config_manager):
"""Edit should return non-zero when backend edit operation fails."""
note = _write_note(
"Edit Backend Failure Note",
"edit-tests",
"# Failure\n\nGamma",
)
result = runner.invoke(
cli_app,
[
"tool",
"edit-note",
note["permalink"],
"--operation",
"find_replace",
"--find-text",
"Gamma",
"--content",
"Delta",
"--expected-replacements",
"2",
],
)
assert result.exit_code != 0
def test_edit_note_project_and_routing_flag_parity(app, app_config, test_project, config_manager):
"""edit-note supports --project/--local and validates --local/--cloud conflict."""
note = _write_note(
"Edit Project Flag Note",
"edit-tests",
"# Project Flag\n\nPROJECT_FLAG_MARKER",
project=test_project.name,
)
success = runner.invoke(
cli_app,
[
"tool",
"edit-note",
note["permalink"],
"--operation",
"append",
"--content",
"\nPROJECT_UPDATE_MARKER",
"--project",
test_project.name,
"--local",
],
)
assert success.exit_code == 0, success.output
assert "No such option" not in success.output
updated = _read_note(note["permalink"], project=test_project.name)
assert "PROJECT_UPDATE_MARKER" in updated["content"]
conflict = runner.invoke(
cli_app,
[
"tool",
"edit-note",
note["permalink"],
"--operation",
"append",
"--content",
"ignored",
"--local",
"--cloud",
],
)
assert conflict.exit_code != 0
assert "Cannot specify both --local and --cloud" in conflict.output