File size: 19,189 Bytes
82be208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
import { parseToolArgs } from '$lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/parsers/_shared';
import {
	parseEditFileMeta,
	parseEditFileTitleMeta
} from '$lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/parsers/edit-file';
import { parseExecShellCommandMeta } from '$lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/parsers/exec-shell-command';
import { parseFileGlobSearchMeta } from '$lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/parsers/file-glob-search';
import { parseGrepSearchMeta } from '$lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/parsers/grep-search';
import { parseReadFileMeta } from '$lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/parsers/read-file';
import { parseRunJavascriptMeta } from '$lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/parsers/run-javascript';
import {
	parseWriteFileMeta,
	parseWriteFileTitleMeta
} from '$lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/parsers/write-file';
import { AgenticSectionType, BuiltInTool } from '$lib/enums';
import type { AgenticSection, WriteFileMeta } from '$lib/types';
import { abbreviateHome, formatCwdMessage, lastPathSegment, parseCwdMessage } from '$lib/utils';
import { describe, expect, it } from 'vitest';

function makeSection(
	overrides: Partial<AgenticSection> = {},
	toolName = BuiltInTool.SERVER_READ_FILE
): AgenticSection {
	return {
		content: '',
		toolArgs: JSON.stringify({ path: '/foo.txt' }),
		toolName,
		toolResult: undefined,
		type: AgenticSectionType.TOOL_CALL,
		...overrides
	};
}

describe('lastPathSegment', () => {
	it('returns the last segment of an absolute path', () => {
		expect(lastPathSegment('/Users/me/code/my-project')).toBe('my-project');
	});

	it('returns the last segment of a tilde-relative path', () => {
		expect(lastPathSegment('~/git/llama.brand')).toBe('llama.brand');
	});

	it('strips trailing slashes', () => {
		expect(lastPathSegment('/foo/bar/')).toBe('bar');
	});

	it('strips multiple trailing slashes', () => {
		expect(lastPathSegment('/foo/bar///')).toBe('bar');
	});

	it('returns the input unchanged when there is no slash', () => {
		expect(lastPathSegment('project')).toBe('project');
	});

	it('returns tilde when only tilde is given', () => {
		expect(lastPathSegment('~/')).toBe('~');
	});
});

describe('abbreviateHome', () => {
	it('abbreviates paths under home with a tilde', () => {
		expect(abbreviateHome('/Users/al/Documents/x.txt', '/Users/al')).toBe('~/Documents/x.txt');
	});

	it('abbreviates home itself to a bare tilde', () => {
		expect(abbreviateHome('/Users/al', '/Users/al')).toBe('~');
	});

	it('returns paths outside home unchanged', () => {
		expect(abbreviateHome('/opt/project', '/Users/al')).toBe('/opt/project');
	});

	it('does not abbreviate a mere prefix match', () => {
		expect(abbreviateHome('/Users/alice/x', '/Users/al')).toBe('/Users/alice/x');
	});

	it('returns the path unchanged when home is unknown', () => {
		expect(abbreviateHome('/Users/al/Documents', null)).toBe('/Users/al/Documents');
	});
});

describe('formatCwdMessage / parseCwdMessage', () => {
	it('formats a cwd change matching the UI text, with a file link', () => {
		expect(formatCwdMessage('/Users/al/Documents', '/Users/al')).toBe(
			'Set working directory to [file:///Users/al/Documents](~/Documents).'
		);
	});

	it('falls back to the basename display when home is unknown', () => {
		expect(formatCwdMessage('/opt/project', null)).toBe(
			'Set working directory to [file:///opt/project](project).'
		);
	});

	it('round-trips through the parser', () => {
		const info = parseCwdMessage(formatCwdMessage('/Users/al/Documents', '/Users/al'));

		expect(info?.path).toBe('/Users/al/Documents');
		expect(info?.display).toBe('~/Documents');
	});

	it('parses a cwd message even when guidance follows the link', () => {
		expect(
			parseCwdMessage(
				'Set working directory to [file:///a/b](~/b). Tool calls run with this as their working directory.'
			)
		).toEqual({ display: '~/b', path: '/a/b' });
	});

	it('parses the cleared marker', () => {
		expect(parseCwdMessage('Working directory cleared')).toEqual({ display: '', path: null });
	});

	it('returns null for non-cwd content', () => {
		expect(parseCwdMessage('hello there')).toBeNull();
	});
});

describe('parseToolArgs (shared)', () => {
	it('returns null when the section has no toolArgs', () => {
		const result = parseToolArgs(
			BuiltInTool.SERVER_READ_FILE,
			makeSection({ toolArgs: undefined })
		);

		expect(result).toBeNull();
	});

	it('returns null when the tool name does not match', () => {
		const result = parseToolArgs(
			BuiltInTool.SERVER_READ_FILE,
			makeSection({ toolArgs: '{"path":"/x"}' }, BuiltInTool.SERVER_WRITE_FILE)
		);

		expect(result).toBeNull();
	});

	it('returns null when args are not valid final JSON (partial: false)', () => {
		const result = parseToolArgs(
			BuiltInTool.SERVER_READ_FILE,
			makeSection({ toolArgs: '{"path": "/foo.tx' })
		);

		expect(result).toBeNull();
	});

	it('returns parsed args when valid final JSON', () => {
		const result = parseToolArgs(
			BuiltInTool.SERVER_READ_FILE,
			makeSection({ toolArgs: '{"path":"/foo.txt"}' })
		);

		expect(result).toEqual({ path: '/foo.txt' });
	});

	it('accepts partial JSON when partial: true', () => {
		const result = parseToolArgs(
			BuiltInTool.SERVER_READ_FILE,
			makeSection({ toolArgs: '{"path": "/foo.tx' }),
			{ partial: true }
		);

		expect(result).toEqual({ path: '/foo.tx' });
	});
});

describe('parseWriteFileMeta', () => {
	it('returns null for sections with a different tool name', () => {
		expect(
			parseWriteFileMeta(
				makeSection({
					toolArgs: '{"path":"/x","content":"y"}',
					toolName: BuiltInTool.SERVER_READ_FILE
				})
			)
		).toBeNull();
	});

	it('returns null when args have no path-like field', () => {
		expect(
			parseWriteFileMeta(
				makeSection({ toolArgs: '{"content":"x"}', toolName: BuiltInTool.SERVER_WRITE_FILE })
			)
		).toBeNull();
	});

	it('accepts partial args (renders incrementally as content streams in)', () => {
		const meta = parseWriteFileMeta(
			makeSection({ toolArgs: '{"path":"/foo.t', toolName: BuiltInTool.SERVER_WRITE_FILE })
		);

		expect(meta?.filePath).toBe('/foo.t');
	});

	it('returns file path, language, content, bytes, resultMessage', () => {
		const meta = parseWriteFileMeta(
			makeSection(
				{
					toolArgs: '{"path":"/foo.ts","content":"x"}',
					toolName: BuiltInTool.SERVER_WRITE_FILE,
					toolResult: '{"result":"wrote","bytes":42}'
				},
				BuiltInTool.SERVER_WRITE_FILE
			)
		);

		expect(meta).toMatchObject<Partial<WriteFileMeta>>({
			bytesWritten: 42,
			content: 'x',
			filePath: '/foo.ts',
			language: expect.any(String),
			resultMessage: 'wrote'
		});
	});

	it('surfaces errorMessage from the result blob', () => {
		const meta = parseWriteFileMeta(
			makeSection({
				toolArgs: '{"path":"/foo","content":"x"}',
				toolName: BuiltInTool.SERVER_WRITE_FILE,
				toolResult: '{"error":"permission denied"}'
			})
		);

		expect(meta?.errorMessage).toBe('permission denied');
	});
});

describe('parseWriteFileTitleMeta', () => {
	it('matches the full meta for path, language and result fields', () => {
		const args = JSON.stringify({ content: 'x'.repeat(50_000), path: '/foo.ts' });
		const toolResult = '{"result":"wrote","bytes":42}';
		const section = makeSection(
			{ toolArgs: args, toolName: BuiltInTool.SERVER_WRITE_FILE, toolResult },
			BuiltInTool.SERVER_WRITE_FILE
		);
		const full = parseWriteFileMeta(section);
		const title = parseWriteFileTitleMeta(section);

		expect(title?.filePath).toBe(full?.filePath);
		expect(title?.fileName).toBe(full?.fileName);
		expect(title?.language).toBe(full?.language);
		expect(title?.bytesWritten).toBe(full?.bytesWritten);
		expect(title?.resultMessage).toBe(full?.resultMessage);
		expect(title?.errorMessage).toBe(full?.errorMessage);
	});

	it('extracts a path with escaped characters without parsing the content blob', () => {
		const section = makeSection(
			{
				toolArgs: '{"path":"/a\\nb\\"c/d.ts","content":"x"}',
				toolName: BuiltInTool.SERVER_WRITE_FILE
			},
			BuiltInTool.SERVER_WRITE_FILE
		);

		expect(parseWriteFileTitleMeta(section)?.filePath).toBe('/a\nb"c/d.ts');
	});

	it('falls back to the full parse for args the extractor can not see', () => {
		const section = makeSection(
			{
				// key written with an escaped unicode escape sequence in the name
				toolArgs: '{"\\u0070ath":"/foo.ts","content":"x"}',
				toolName: BuiltInTool.SERVER_WRITE_FILE
			},
			BuiltInTool.SERVER_WRITE_FILE
		);

		expect(parseWriteFileTitleMeta(section)?.filePath).toBe('/foo.ts');
	});

	it('accepts partial args like the full parser', () => {
		const section = makeSection(
			{ toolArgs: '{"path":"/foo.t', toolName: BuiltInTool.SERVER_WRITE_FILE },
			BuiltInTool.SERVER_WRITE_FILE
		);

		expect(parseWriteFileTitleMeta(section)?.filePath).toBe('/foo.t');
	});

	it('returns null for sections with a different tool name', () => {
		expect(
			parseWriteFileTitleMeta(
				makeSection({
					toolArgs: '{"path":"/x","content":"y"}',
					toolName: BuiltInTool.SERVER_READ_FILE
				})
			)
		).toBeNull();
	});
});

describe('parseEditFileTitleMeta', () => {
	it('matches the full meta for path and result fields', () => {
		const section = makeSection(
			{
				toolArgs: '{"path":"/foo.ts","edits":[{"old_text":"a","new_text":"b"}]}' + ' '.repeat(0),
				toolName: BuiltInTool.SERVER_EDIT_FILE,
				toolResult: '{"result":"ok","edits_applied":1}'
			},
			BuiltInTool.SERVER_EDIT_FILE
		);
		const full = parseEditFileMeta(section);
		const title = parseEditFileTitleMeta(section);

		expect(title?.filePath).toBe(full?.filePath);
		expect(title?.fileName).toBe(full?.fileName);
		expect(title?.editsApplied).toBe(full?.editsApplied);
		expect(title?.resultMessage).toBe(full?.resultMessage);
		expect(title?.errorMessage).toBe(full?.errorMessage);
	});

	it('surfaces errorMessage from the result blob without parsing args', () => {
		const section = makeSection(
			{
				toolArgs: '{"path":"/foo.ts","edits":[]}',
				toolName: BuiltInTool.SERVER_EDIT_FILE,
				toolResult: '{"error":"permission denied"}'
			},
			BuiltInTool.SERVER_EDIT_FILE
		);

		expect(parseEditFileTitleMeta(section)?.errorMessage).toBe('permission denied');
	});

	it('returns null when args have no path-like field', () => {
		expect(
			parseEditFileTitleMeta(
				makeSection({ toolArgs: '{"edits":[]}', toolName: BuiltInTool.SERVER_EDIT_FILE })
			)
		).toBeNull();
	});
});

describe('parseEditFileMeta', () => {
	it('parses edits array and applies editsApplied from the result', () => {
		const section = makeSection(
			{
				toolArgs:
					'{"path":"/foo.ts","edits":[{"old_text":"a","new_text":"b"},{"old_text":"c","new_text":"d"}]}',
				toolName: BuiltInTool.SERVER_EDIT_FILE,
				toolResult: '{"result":"ok","edits_applied":2}'
			},
			BuiltInTool.SERVER_EDIT_FILE
		);
		const meta = parseEditFileMeta(section);

		expect(meta?.edits).toEqual([
			{ newText: 'b', oldText: 'a' },
			{ newText: 'd', oldText: 'c' }
		]);
		expect(meta?.editsApplied).toBe(2);
		expect(meta?.resultMessage).toBe('ok');
	});

	it('drops edits with empty old_text', () => {
		const section = makeSection(
			{
				toolArgs: '{"path":"/foo","edits":[{"old_text":""},{"old_text":"a","new_text":""}]}',
				toolName: BuiltInTool.SERVER_EDIT_FILE
			},
			BuiltInTool.SERVER_EDIT_FILE
		);
		const meta = parseEditFileMeta(section);

		// First entry is dropped (empty old_text). Second is kept
		// (empty new_text is fine - it's the "delete" case).
		expect(meta?.edits).toEqual([{ newText: '', oldText: 'a' }]);
	});

	it('errorMessage wins over result message', () => {
		const section = makeSection(
			{
				toolArgs: '{"path":"/foo"}',
				toolName: BuiltInTool.SERVER_EDIT_FILE,
				toolResult: '{"error":"bad path","result":"ok"}'
			},
			BuiltInTool.SERVER_EDIT_FILE
		);
		const meta = parseEditFileMeta(section);

		expect(meta?.errorMessage).toBe('bad path');
		expect(meta?.resultMessage).toBeUndefined();
	});
});

describe('parseReadFileMeta', () => {
	it('parses file name alone (no range)', () => {
		const meta = parseReadFileMeta(
			makeSection({ toolArgs: '{"path":"/foo.txt"}' }, BuiltInTool.SERVER_READ_FILE)
		);

		expect(meta?.fileName).toBe('foo.txt');
		expect(meta?.lineRange).toBeNull();
	});

	it('parses start_line + end_line into a range', () => {
		const meta = parseReadFileMeta(
			makeSection(
				{ toolArgs: '{"path":"/foo.ts","start_line":10,"end_line":20}' },
				BuiltInTool.SERVER_READ_FILE
			)
		);

		expect(meta?.lineRange).toEqual({ end: 20, start: 10 });
	});

	it('parses start_line + line_count into a range', () => {
		const meta = parseReadFileMeta(
			makeSection(
				{ toolArgs: '{"path":"/foo.ts","start_line":10,"line_count":5}' },
				BuiltInTool.SERVER_READ_FILE
			)
		);

		expect(meta?.lineRange).toEqual({ end: 14, start: 10 });
	});

	it('returns null when args cannot be parsed', () => {
		expect(
			parseReadFileMeta(makeSection({ toolArgs: '{bad' }, BuiltInTool.SERVER_READ_FILE))
		).toBeNull();
	});
});

describe('parseGrepSearchMeta', () => {
	it('returns null when path or pattern is missing', () => {
		expect(
			parseGrepSearchMeta(
				makeSection({ toolArgs: '{"pattern":"foo"}', toolName: BuiltInTool.SERVER_GREP_SEARCH })
			)
		).toBeNull();
		expect(
			parseGrepSearchMeta(
				makeSection({ toolArgs: '{"path":"/x"}', toolName: BuiltInTool.SERVER_GREP_SEARCH })
			)
		).toBeNull();
	});

	it('parses structured plain_text_response into matches', () => {
		const meta = parseGrepSearchMeta(
			makeSection(
				{
					toolArgs: '{"path":"/x","pattern":"foo"}',
					toolName: BuiltInTool.SERVER_GREP_SEARCH,
					toolResult: JSON.stringify({ plain_text_response: 'a.ts:hello\nb.ts:world' })
				},
				BuiltInTool.SERVER_GREP_SEARCH
			)
		);

		expect(meta?.matches).toHaveLength(2);
		expect(meta?.matches[0]).toEqual({ content: 'hello', file: 'a.ts' });
	});

	it('falls back to raw-text parsing when result is not JSON', () => {
		const meta = parseGrepSearchMeta(
			makeSection(
				{
					toolArgs: '{"path":"/x","pattern":"foo"}',
					toolName: BuiltInTool.SERVER_GREP_SEARCH,
					toolResult: 'a.ts:hello\nb.ts:world'
				},
				BuiltInTool.SERVER_GREP_SEARCH
			)
		);

		expect(meta?.matches).toHaveLength(2);
	});

	it('parses line numbers when return_line_numbers is true', () => {
		const meta = parseGrepSearchMeta(
			makeSection(
				{
					toolArgs: '{"path":"/x","pattern":"foo","return_line_numbers":true}',
					toolName: BuiltInTool.SERVER_GREP_SEARCH,
					toolResult: 'a.ts:12:hello'
				},
				BuiltInTool.SERVER_GREP_SEARCH
			)
		);

		expect(meta?.matches[0]).toEqual({ content: 'hello', file: 'a.ts', line: 12 });
		expect(meta?.showLineNumbers).toBe(true);
	});
});

describe('parseFileGlobSearchMeta', () => {
	it('falls back to raw-text parsing when result is not JSON', () => {
		const meta = parseFileGlobSearchMeta(
			makeSection(
				{
					toolArgs: '{"path":"/x"}',
					toolName: BuiltInTool.SERVER_FILE_GLOB_SEARCH,
					toolResult: 'a.ts\nb.ts'
				},
				BuiltInTool.SERVER_FILE_GLOB_SEARCH
			)
		);

		expect(meta?.matches).toEqual(['a.ts', 'b.ts']);
	});

	it('parses plain_text_response from a JSON object', () => {
		const meta = parseFileGlobSearchMeta(
			makeSection(
				{
					toolArgs: '{"path":"/x"}',
					toolName: BuiltInTool.SERVER_FILE_GLOB_SEARCH,
					toolResult: JSON.stringify({ plain_text_response: 'a.ts\nb.ts' })
				},
				BuiltInTool.SERVER_FILE_GLOB_SEARCH
			)
		);

		expect(meta?.matches).toEqual(['a.ts', 'b.ts']);
	});

	it('surfaces errorMessage from the result blob', () => {
		const meta = parseFileGlobSearchMeta(
			makeSection(
				{
					toolArgs: '{"path":"/x"}',
					toolName: BuiltInTool.SERVER_FILE_GLOB_SEARCH,
					toolResult: JSON.stringify({ error: 'permission denied' })
				},
				BuiltInTool.SERVER_FILE_GLOB_SEARCH
			)
		);

		expect(meta?.errorMessage).toBe('permission denied');
	});
});

describe('parseRunJavascriptMeta', () => {
	it('returns null when code is missing', () => {
		expect(
			parseRunJavascriptMeta(
				makeSection({ toolArgs: '{}', toolName: BuiltInTool.BROWSER_RUN_JAVASCRIPT })
			)
		).toBeNull();
	});

	it('reads code and timeout', () => {
		const meta = parseRunJavascriptMeta(
			makeSection(
				{
					toolArgs: '{"code":"Math.PI","timeout_ms":5000}',
					toolName: BuiltInTool.BROWSER_RUN_JAVASCRIPT
				},
				BuiltInTool.BROWSER_RUN_JAVASCRIPT
			)
		);

		expect(meta?.code).toBe('Math.PI');
		expect(meta?.timeoutMs).toBe(5000);
	});

	it('reads error field from a JSON-object result', () => {
		const meta = parseRunJavascriptMeta(
			makeSection(
				{
					toolArgs: '{"code":"throw new Error()"}',
					toolName: BuiltInTool.BROWSER_RUN_JAVASCRIPT,
					toolResult: JSON.stringify({ error: 'undefined is not a function' })
				},
				BuiltInTool.BROWSER_RUN_JAVASCRIPT
			)
		);

		expect(meta?.errorMessage).toBe('undefined is not a function');
	});

	it('does NOT treat a JSON-array result as an error', () => {
		// SandboxService returns successful output as a JSON array;
		// only JSON objects carry `error`. Raw arrays must round-trip
		// through unchanged.
		const meta = parseRunJavascriptMeta(
			makeSection(
				{
					toolArgs: '{"code":"[1,2,3]"}',
					toolName: BuiltInTool.BROWSER_RUN_JAVASCRIPT,
					toolResult: '[1,2,3]'
				},
				BuiltInTool.BROWSER_RUN_JAVASCRIPT
			)
		);

		expect(meta?.errorMessage).toBeUndefined();
	});

	it('scans a non-JSON string result for an `Error:` line', () => {
		const meta = parseRunJavascriptMeta(
			makeSection(
				{
					toolArgs: '{"code":"foo"}',
					toolName: BuiltInTool.BROWSER_RUN_JAVASCRIPT,
					toolResult: 'Error: undefined is not a function\n  at <anonymous>:1:1'
				},
				BuiltInTool.BROWSER_RUN_JAVASCRIPT
			)
		);

		expect(meta?.errorMessage).toBe('undefined is not a function');
	});
});

describe('parseExecShellCommandMeta', () => {
	it('reads command from the args', () => {
		const meta = parseExecShellCommandMeta(
			makeSection(
				{ toolArgs: '{"command":"ls -la"}', toolName: BuiltInTool.SERVER_EXEC_SHELL_COMMAND },
				BuiltInTool.SERVER_EXEC_SHELL_COMMAND
			)
		);

		expect(meta?.command).toBe('ls -la');
	});

	it('accepts cmd / shell_command aliases', () => {
		expect(
			parseExecShellCommandMeta(
				makeSection(
					{ toolArgs: '{"cmd":"ls"}', toolName: BuiltInTool.SERVER_EXEC_SHELL_COMMAND },
					BuiltInTool.SERVER_EXEC_SHELL_COMMAND
				)
			)?.command
		).toBe('ls');
		expect(
			parseExecShellCommandMeta(
				makeSection(
					{ toolArgs: '{"shell_command":"ls"}', toolName: BuiltInTool.SERVER_EXEC_SHELL_COMMAND },
					BuiltInTool.SERVER_EXEC_SHELL_COMMAND
				)
			)?.command
		).toBe('ls');
	});

	it('returns null when no command alias is present', () => {
		expect(
			parseExecShellCommandMeta(
				makeSection(
					{ toolArgs: '{"cwd":"/x"}', toolName: BuiltInTool.SERVER_EXEC_SHELL_COMMAND },
					BuiltInTool.SERVER_EXEC_SHELL_COMMAND
				)
			)
		).toBeNull();
	});
});