Files
llama.cpp/ui/apps/chat/tests/unit/tool-calls.test.ts
T
Aleksander Grygier 67cf8967eb ui: move Llama Chat to SvelteKit 3 and its built-in service worker (#32463)
* ui: replace deprecated packages and fix npm audit findings

Swap the mdast stub for @types/mdast, move the katex override to 0.19.0,
and override glob, source-map and html-encoding-sniffer under
workbox-build and http-server so installs no longer pull deprecated
packages. Apply the non-breaking npm audit fixes to the lockfile.

Assisted-by: Claude (Opus 5.5 Medium)

* ui: migrate Llama Chat to SvelteKit 3 and its built-in service worker

Move to SvelteKit 3, Vite 8, TypeScript 6 and Storybook 10.6. The
SvelteKit options move from svelte.config.js into sveltekit() in
vite.config.ts, $lib imports become #lib subpath imports from
package.json, and $app/environment becomes $app/env. Modules named
server.* or living under a server/ folder are server-only in SvelteKit 3,
so the llama-server components, store and enums move to llama-server
names.

@vite-pwa/sveltekit and Workbox give way to SvelteKit's own service
worker in src/service-worker, registered by usePwa(), which precaches the
app files and leaves API requests to the network. A small plugin emits
manifest.webmanifest. static/sw.js removes the worker of older builds, and
llama-server serves a self-removing service-worker.js as well as sw.js
when the built-in UI is off or replaced.

Assisted-by: Claude (Opus 5.5 Medium)

* ui: use SvelteKit 3's update detection and Svelte 5's current patterns

A page the service worker doesn't control now learns of a new build
from SvelteKit itself: `updated` from $app/state checks
_app/version.json on navigation, on focus and every hour. It replaces
the version Llama Chat kept in localStorage and compared by hand.
Reload on the update prompt pulls in the new worker and activates it
before reloading, so one click lands on the new build, whether or not
a worker controlled the page.

Following Svelte's current guidance:
- usePwa keeps its state in $state rather than a store, and no effect
  checks `browser`
- nearViewport, autoResizeTextarea and the custom CSS injection are
  attachments ({@attach}) rather than actions (use:)
- class lists use arrays rather than class: directives
- the chat contexts come from createContext, typed, in place of
  setContext/getContext with string keys
- ActionIconCopyToClipboard takes $props() rather than `export let`

Assisted-by: Claude (Opus 5.5 Medium)

* ui: move to ESLint 10

ESLint 9 is out of support. Every plugin the app uses supports 10:
typescript-eslint, eslint-plugin-svelte, perfectionist,
simple-import-sort, eslint-plugin-storybook and eslint-config-prettier.
@eslint/js and @eslint/compat follow.

ESLint 10's recommended rules ask for two things the code now does:
errors rethrown from a catch keep the caught error as their `cause`,
and no variable starts with a value that every path overwrites.

Assisted-by: Claude (Opus 5.5 Medium)

* ui: override minimatch to clear npm audit's last finding

ESLint 10.14, @eslint/config-array 0.23.5 and typescript-eslint 8.72
depend on minimatch 10, which GHSA-g3hv-75wg-m249 (denial of service
via globparts amplification) affects below 11.0.1. Their fixed releases
move to minimatch 11 but are all younger than the repo's 7-day
min-release-age, so an override pins minimatch 11.0.5, the newest the
rule allows. The three only call new Minimatch(), match() and
minimatch(), which minimatch 11 keeps. npm audit now finds nothing.

Assisted-by: Claude (Opus 5.5 Medium)
2026-11-17 11:43:36 +01:00

758 lines
22 KiB
TypeScript

import {
extractToolArgPath,
parseToolArgs
} from '#lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/parsers/_shared.ts';
import {
parseEditFileMeta,
parseEditFileTitleMeta
} from '#lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/parsers/edit-file.ts';
import { parseExecShellCommandMeta } from '#lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/parsers/exec-shell-command.ts';
import { parseFileGlobSearchMeta } from '#lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/parsers/file-glob-search.ts';
import { parseGrepSearchMeta } from '#lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/parsers/grep-search.ts';
import { parseReadFileMeta } from '#lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/parsers/read-file.ts';
import { parseRunJavascriptMeta } from '#lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/parsers/run-javascript.ts';
import {
parseWriteFileMeta,
parseWriteFileTitleMeta
} from '#lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageToolCall/parsers/write-file.ts';
import { CODE_BLOCK } from '#lib/constants/index.ts';
import { AgenticSectionType, BuiltInTool } from '#lib/enums/index.ts';
import type { AgenticSection, WriteFileMeta } from '#lib/types/index.ts';
import {
abbreviateHome,
formatCwdMessage,
lastPathSegment,
parseCwdMessage
} from '#lib/utils/index.ts';
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('extractToolArgPath', () => {
it('reads every path alias', () => {
expect(extractToolArgPath({ path: '/a' })).toBe('/a');
expect(extractToolArgPath({ file_path: '/b' })).toBe('/b');
expect(extractToolArgPath({ filePath: '/c' })).toBe('/c');
});
it('returns undefined when no path alias is a usable string', () => {
expect(extractToolArgPath({ content: 'x' })).toBeUndefined();
expect(extractToolArgPath({ path: '' })).toBeUndefined();
expect(extractToolArgPath({ path: 7 })).toBeUndefined();
expect(extractToolArgPath(null)).toBeUndefined();
expect(extractToolArgPath(undefined)).toBeUndefined();
});
});
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 content without a path when args have no path-like field', () => {
const meta = parseWriteFileMeta(
makeSection({ toolArgs: '{"content":"x"}', toolName: BuiltInTool.SERVER_WRITE_FILE })
);
expect(meta?.content).toBe('x');
expect(meta?.filePath).toBeUndefined();
expect(meta?.fileName).toBeUndefined();
expect(meta?.language).toBe(CODE_BLOCK.DEFAULT_LANGUAGE);
});
it('returns content streamed before the path has arrived', () => {
const meta = parseWriteFileMeta(
makeSection({ toolArgs: '{"content":"hello ', toolName: BuiltInTool.SERVER_WRITE_FILE })
);
expect(meta?.content).toBe('hello ');
});
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 meta without a path when args have no path-like field', () => {
const title = parseWriteFileTitleMeta(
makeSection({ toolArgs: '{"content":"x"}', toolName: BuiltInTool.SERVER_WRITE_FILE })
);
expect(title?.filePath).toBeUndefined();
expect(title?.fileName).toBeUndefined();
expect(title?.language).toBe(CODE_BLOCK.DEFAULT_LANGUAGE);
});
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 meta without a path when args have no path-like field', () => {
const title = parseEditFileTitleMeta(
makeSection({ toolArgs: '{"edits":[]}', toolName: BuiltInTool.SERVER_EDIT_FILE })
);
expect(title?.filePath).toBeUndefined();
expect(title?.fileName).toBeUndefined();
});
});
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();
});
it('still parses edits when args have no path-like field', () => {
const meta = parseEditFileMeta(
makeSection({
toolArgs: '{"edits":[{"old_text":"a","new_text":"b"}]}',
toolName: BuiltInTool.SERVER_EDIT_FILE
})
);
expect(meta?.edits).toEqual([{ newText: 'b', oldText: 'a' }]);
expect(meta?.filePath).toBeUndefined();
});
it('parses edits streamed before the path has arrived', () => {
const meta = parseEditFileMeta(
makeSection({
// second edit is still mid-stream: only its old_text has landed
toolArgs: '{"edits":[{"old_text":"a","new_text":"b"},{"old_text":"c',
toolName: BuiltInTool.SERVER_EDIT_FILE
})
);
expect(meta?.edits).toEqual([
{ newText: 'b', oldText: 'a' },
{ newText: '', oldText: 'c' }
]);
});
});
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();
});
it('surfaces a partially streamed command before the JSON closes', () => {
const meta = parseExecShellCommandMeta(
makeSection(
{ toolArgs: '{"command":"git sta', toolName: BuiltInTool.SERVER_EXEC_SHELL_COMMAND },
BuiltInTool.SERVER_EXEC_SHELL_COMMAND
)
);
expect(meta?.command).toBe('git sta');
});
it('keeps a dangling escape from breaking the partial parse', () => {
const meta = parseExecShellCommandMeta(
makeSection(
{ toolArgs: '{"command":"echo \\"he', toolName: BuiltInTool.SERVER_EXEC_SHELL_COMMAND },
BuiltInTool.SERVER_EXEC_SHELL_COMMAND
)
);
expect(meta?.command).toBe('echo "he');
});
it('returns null while the command value is still empty', () => {
expect(
parseExecShellCommandMeta(
makeSection(
{ toolArgs: '{"command":"', toolName: BuiltInTool.SERVER_EXEC_SHELL_COMMAND },
BuiltInTool.SERVER_EXEC_SHELL_COMMAND
)
)
).toBeNull();
});
});