-
Notifications
You must be signed in to change notification settings - Fork 1
test: add vitest coverage for tool render modules #123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { renderToolResult } from './registry.js'; | ||
|
|
||
| /** Common XSS payloads for renderer escaping assertions. */ | ||
| export const XSS_SCRIPT = '<script>alert(1)</script>'; | ||
| export const XSS_IMG = '<img onerror=alert(1)>'; | ||
|
|
||
| /** | ||
| * Assert raw HTML fragments are escaped (not present verbatim). | ||
| */ | ||
| export function expectNoRawHtml(html, rawFragments) { | ||
| for (const frag of rawFragments) { | ||
| expect(html).not.toContain(frag); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Assert an HTML-escaped fragment appears in output. | ||
| */ | ||
| export function expectEscaped(html, escapedFragment) { | ||
| expect(html).toContain(escapedFragment); | ||
| } | ||
|
|
||
| /** | ||
| * Shared behavioral tests for summary-only tool_result renderers (Edited/Plan/Wrote). | ||
| */ | ||
| export function describeSummaryOnlyResult( | ||
| render, | ||
| { suiteName, resultType, label, samplePath }, | ||
| ) { | ||
| describe(suiteName, () => { | ||
| it(`renders ${label.toLowerCase()} file path in summary`, () => { | ||
| const html = render({ result_type: resultType, file_path: samplePath }); | ||
| expect(html).toContain(`${label}: ${samplePath}`); | ||
| expect(html).toContain('tool-result'); | ||
| }); | ||
|
|
||
| it('handles missing file path', () => { | ||
| const html = render({ result_type: resultType }); | ||
| expect(html).toContain(`${label}:`); | ||
| expect(html).not.toContain('undefined'); | ||
| }); | ||
|
|
||
| it('escapes HTML in file path via registry', () => { | ||
| const html = renderToolResult({ | ||
| result_type: resultType, | ||
| file_path: XSS_SCRIPT, | ||
| }); | ||
| expectNoRawHtml(html, [XSS_SCRIPT]); | ||
| }); | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { renderBashResult } from './bash.js'; | ||
| import { renderToolResult } from '../registry.js'; | ||
| import { expectNoRawHtml, XSS_IMG } from '../test_helpers.js'; | ||
|
|
||
| describe('renderBashResult', () => { | ||
| it('renders success status with stdout', () => { | ||
| const html = renderBashResult({ | ||
| result_type: 'bash', | ||
| exit_code: 0, | ||
| stdout: 'hello\nworld', | ||
| }); | ||
| expect(html).toContain('Bash Result (success)'); | ||
| expect(html).toContain('hello'); | ||
| expect(html).toContain('stdout'); | ||
| expect(html).toContain('tool-result'); | ||
| }); | ||
|
|
||
| it('renders error status with stderr', () => { | ||
| const html = renderBashResult({ | ||
| result_type: 'bash', | ||
| exit_code: 1, | ||
| is_error: true, | ||
| stderr: 'command failed', | ||
| }); | ||
| expect(html).toContain('error (exit 1)'); | ||
| expect(html).toContain('command failed'); | ||
| expect(html).toContain('stderr'); | ||
| }); | ||
|
|
||
| it('renders interrupted status', () => { | ||
| const html = renderBashResult({ result_type: 'bash', interrupted: true }); | ||
| expect(html).toContain('Bash Result (interrupted)'); | ||
| }); | ||
|
|
||
| it('escapes HTML in stdout and stderr', () => { | ||
| const html = renderToolResult({ | ||
| result_type: 'bash', | ||
| exit_code: 0, | ||
| stdout: XSS_IMG, | ||
| stderr: '<script>x</script>', | ||
| }); | ||
| expectNoRawHtml(html, [XSS_IMG, '<script>']); | ||
| expect(html).toContain('<img'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { renderToolResultFallback } from './fallback.js'; | ||
| import { renderToolResult } from '../registry.js'; | ||
| import { expectNoRawHtml, XSS_SCRIPT } from '../test_helpers.js'; | ||
|
|
||
| describe('renderToolResultFallback', () => { | ||
| it('renders unknown result type and JSON payload', () => { | ||
| const html = renderToolResultFallback({ | ||
| result_type: 'custom_widget', | ||
| widget_id: 99, | ||
| label: 'test', | ||
| }); | ||
| expect(html).toContain('Unknown tool result: custom_widget'); | ||
| expect(html).toContain('"widget_id"'); | ||
| expect(html).toContain('99'); | ||
| expect(html).toContain('tool-result'); | ||
| }); | ||
|
|
||
| it('uses unknown dispatch key when result_type is missing', () => { | ||
| const html = renderToolResultFallback({ payload: 'data' }); | ||
| expect(html).toContain('Unknown tool result:'); | ||
| expect(html).toContain('tool-result'); | ||
| }); | ||
|
|
||
| it('escapes HTML in JSON fallback body', () => { | ||
| const html = renderToolResult({ | ||
| result_type: 'mystery', | ||
| content: XSS_SCRIPT, | ||
| }); | ||
| expectNoRawHtml(html, [XSS_SCRIPT]); | ||
| expect(html).toContain('<script>'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { renderFileEditResult } from './file_edit.js'; | ||
| import { describeSummaryOnlyResult } from '../test_helpers.js'; | ||
|
|
||
| describeSummaryOnlyResult(renderFileEditResult, { | ||
| suiteName: 'renderFileEditResult', | ||
| resultType: 'file_edit', | ||
| label: 'Edited', | ||
| samplePath: 'src/app.js', | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { renderFileWriteResult } from './file_write.js'; | ||
| import { renderToolResult } from '../registry.js'; | ||
| import { expectNoRawHtml, XSS_SCRIPT } from '../test_helpers.js'; | ||
|
|
||
| describe('renderFileWriteResult', () => { | ||
| it('renders written file path in summary', () => { | ||
| const html = renderFileWriteResult({ | ||
| result_type: 'file_write', | ||
| file_path: 'output/data.json', | ||
| }); | ||
| expect(html).toContain('Wrote: output/data.json'); | ||
| expect(html).toContain('tool-result'); | ||
| }); | ||
|
|
||
| it('handles missing file path', () => { | ||
| const html = renderFileWriteResult({ result_type: 'file_write' }); | ||
| expect(html).toContain('Wrote:'); | ||
| expect(html).not.toContain('undefined'); | ||
| }); | ||
|
|
||
| it('escapes HTML in file path via registry', () => { | ||
| const html = renderToolResult({ | ||
| result_type: 'file_write', | ||
| file_path: XSS_SCRIPT, | ||
| }); | ||
| expectNoRawHtml(html, [XSS_SCRIPT]); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { renderGlobResult } from './glob.js'; | ||
|
|
||
| describe('renderGlobResult', () => { | ||
| it('renders file count in summary', () => { | ||
| const html = renderGlobResult({ | ||
| result_type: 'glob', | ||
| num_files: 12, | ||
| }); | ||
| expect(html).toContain('Glob: 12 files found'); | ||
| expect(html).toContain('tool-result'); | ||
| }); | ||
|
|
||
| it('shows truncated flag when set', () => { | ||
| const html = renderGlobResult({ | ||
| result_type: 'glob', | ||
| num_files: 100, | ||
| truncated: true, | ||
| }); | ||
| expect(html).toContain('(truncated)'); | ||
| }); | ||
|
|
||
| it('defaults to zero files when num_files is absent', () => { | ||
| const html = renderGlobResult({ result_type: 'glob' }); | ||
| expect(html).toContain('Glob: 0 files found'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { renderGrepResult } from './grep.js'; | ||
|
|
||
| describe('renderGrepResult', () => { | ||
| it('renders file and line counts in summary', () => { | ||
| const html = renderGrepResult({ | ||
| result_type: 'grep', | ||
| num_files: 5, | ||
| num_lines: 42, | ||
| }); | ||
| expect(html).toContain('Grep: 5 files, 42 lines'); | ||
| expect(html).toContain('tool-result'); | ||
| }); | ||
|
|
||
| it('defaults counts to zero when absent', () => { | ||
| const html = renderGrepResult({ result_type: 'grep' }); | ||
| expect(html).toContain('Grep: 0 files, 0 lines'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { renderPlanResult } from './plan.js'; | ||
| import { describeSummaryOnlyResult } from '../test_helpers.js'; | ||
|
|
||
| describeSummaryOnlyResult(renderPlanResult, { | ||
| suiteName: 'renderPlanResult', | ||
| resultType: 'plan', | ||
| label: 'Plan', | ||
| samplePath: '.cursor/plans/sprint.md', | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { renderTaskResult } from './task.js'; | ||
| import { renderToolResult } from '../registry.js'; | ||
| import { expectNoRawHtml, XSS_SCRIPT } from '../test_helpers.js'; | ||
|
|
||
| describe('renderTaskResult', () => { | ||
| it('renders completed task with duration and token stats', () => { | ||
| const totalTokens = 1500; | ||
| const html = renderTaskResult({ | ||
| result_type: 'task', | ||
| status: 'completed', | ||
| total_duration_ms: 2500, | ||
| total_tokens: totalTokens, | ||
| total_tool_use_count: 3, | ||
| }); | ||
| expect(html).toContain('Task completed'); | ||
| expect(html).toContain('2.5s'); | ||
| expect(html).toContain(`${totalTokens.toLocaleString()} tokens`); | ||
| expect(html).toContain('3 tool calls'); | ||
| expect(html).toContain('tool-result'); | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| it('prefers retrieval status summary when set', () => { | ||
| const html = renderTaskResult({ | ||
| result_type: 'task', | ||
| retrieval_status: 'found', | ||
| status: 'completed', | ||
| }); | ||
| expect(html).toContain('Task retrieval: found'); | ||
| expect(html).not.toContain('Task completed'); | ||
| }); | ||
|
|
||
| it('prefers description summary when set', () => { | ||
| const html = renderTaskResult({ | ||
| result_type: 'task', | ||
| description: 'explore auth module', | ||
| }); | ||
| expect(html).toContain('Task launched: explore auth module'); | ||
| }); | ||
|
|
||
| it('escapes HTML in description via registry', () => { | ||
| const html = renderToolResult({ | ||
| result_type: 'task', | ||
| description: XSS_SCRIPT, | ||
| }); | ||
| expectNoRawHtml(html, [XSS_SCRIPT]); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { renderTodoWriteResult } from './todo_write.js'; | ||
| import { renderToolResult } from '../registry.js'; | ||
| import { expectNoRawHtml, XSS_SCRIPT } from '../test_helpers.js'; | ||
|
|
||
| describe('renderTodoWriteResult', () => { | ||
| it('renders todo items with emoji status icons', () => { | ||
| const html = renderTodoWriteResult({ | ||
| result_type: 'todo_write', | ||
| todos: [ | ||
| { status: 'pending', content: 'write tests' }, | ||
| { status: 'completed', content: 'fix bug' }, | ||
| { status: 'in_progress', content: 'review PR' }, | ||
| ], | ||
| }); | ||
| expect(html).toContain('Todos updated (3 items)'); | ||
| expect(html).toContain('write tests'); | ||
| expect(html).toContain('fix bug'); | ||
| expect(html).toContain('review PR'); | ||
| expect(html).toContain('tool-result'); | ||
| }); | ||
|
|
||
| it('uses todo_count when todos array is absent', () => { | ||
| const html = renderTodoWriteResult({ | ||
| result_type: 'todo_write', | ||
| todo_count: 5, | ||
| }); | ||
| expect(html).toContain('Todos updated (5 items)'); | ||
| }); | ||
|
|
||
| it('escapes HTML in todo content', () => { | ||
| const html = renderToolResult({ | ||
| result_type: 'todo_write', | ||
| todos: [{ status: 'pending', content: XSS_SCRIPT }], | ||
| }); | ||
| expectNoRawHtml(html, [XSS_SCRIPT]); | ||
| expect(html).toContain('<script>'); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.