|
| 1 | +import { describe, expect, test } from 'bun:test' |
| 2 | +import React from 'react' |
| 3 | +import { renderToStaticMarkup } from 'react-dom/server' |
| 4 | + |
| 5 | +import { initializeThemeStore } from '../../../hooks/use-theme' |
| 6 | +import { chatThemes } from '../../../utils/theme-system' |
| 7 | +import { getToolComponent, renderToolComponent } from '../registry' |
| 8 | + |
| 9 | +import type { ToolBlock } from '../types' |
| 10 | + |
| 11 | +initializeThemeStore() |
| 12 | + |
| 13 | +const createToolBlock = ( |
| 14 | + operation: Record<string, unknown>, |
| 15 | +): ToolBlock & { toolName: 'apply_patch' } => ({ |
| 16 | + type: 'tool', |
| 17 | + toolName: 'apply_patch', |
| 18 | + toolCallId: 'apply-patch-test-id', |
| 19 | + input: { operation }, |
| 20 | +}) |
| 21 | + |
| 22 | +const renderOptions = { |
| 23 | + availableWidth: 80, |
| 24 | + indentationOffset: 0, |
| 25 | + labelWidth: 0, |
| 26 | +} |
| 27 | + |
| 28 | +describe('ApplyPatchComponent', () => { |
| 29 | + test('is registered for apply_patch tool calls', () => { |
| 30 | + expect(getToolComponent('apply_patch')).toBeDefined() |
| 31 | + }) |
| 32 | + |
| 33 | + test('renders create_file operation', () => { |
| 34 | + const toolBlock = createToolBlock({ |
| 35 | + type: 'create_file', |
| 36 | + path: 'src/new-file.ts', |
| 37 | + diff: '@@\n+export const value = 1\n', |
| 38 | + }) |
| 39 | + |
| 40 | + const result = renderToolComponent(toolBlock, chatThemes.dark, renderOptions) |
| 41 | + |
| 42 | + expect(result).toBeDefined() |
| 43 | + expect(result?.content).toBeDefined() |
| 44 | + |
| 45 | + const markup = renderToStaticMarkup(result?.content as React.ReactElement) |
| 46 | + expect(markup).toContain('Create') |
| 47 | + expect(markup).toContain('src/new-file.ts') |
| 48 | + }) |
| 49 | + |
| 50 | + test('renders update_file operation with diff content', () => { |
| 51 | + const toolBlock = createToolBlock({ |
| 52 | + type: 'update_file', |
| 53 | + path: 'src/existing.ts', |
| 54 | + diff: '@@\n-oldLine\n+newLine\n', |
| 55 | + }) |
| 56 | + |
| 57 | + const result = renderToolComponent(toolBlock, chatThemes.dark, renderOptions) |
| 58 | + |
| 59 | + expect(result).toBeDefined() |
| 60 | + expect(result?.content).toBeDefined() |
| 61 | + |
| 62 | + const markup = renderToStaticMarkup(result?.content as React.ReactElement) |
| 63 | + expect(markup).toContain('Edit') |
| 64 | + expect(markup).toContain('src/existing.ts') |
| 65 | + expect(markup).toContain('-oldLine') |
| 66 | + expect(markup).toContain('+newLine') |
| 67 | + }) |
| 68 | + |
| 69 | + test('renders delete_file operation', () => { |
| 70 | + const toolBlock = createToolBlock({ |
| 71 | + type: 'delete_file', |
| 72 | + path: 'src/remove-me.ts', |
| 73 | + }) |
| 74 | + |
| 75 | + const result = renderToolComponent(toolBlock, chatThemes.dark, renderOptions) |
| 76 | + |
| 77 | + expect(result).toBeDefined() |
| 78 | + expect(result?.content).toBeDefined() |
| 79 | + |
| 80 | + const markup = renderToStaticMarkup(result?.content as React.ReactElement) |
| 81 | + expect(markup).toContain('Delete') |
| 82 | + expect(markup).toContain('src/remove-me.ts') |
| 83 | + }) |
| 84 | +}) |
0 commit comments