|
| 1 | +import type { CallToolResult } from '@modelcontextprotocol/sdk/types.js'; |
| 2 | +import { VerifyAfterEditTool } from './verify-after-edit.tool.js'; |
| 3 | +import { createMockRelayClient } from '../__test-utils__/mock-relay-client.js'; |
| 4 | +import { MCP_TOOLS } from './tool.defs.js'; |
| 5 | + |
| 6 | +const annotationId = 'ann_A7bCd9Ef_1700000000000'; |
| 7 | + |
| 8 | +describe('VerifyAfterEditTool', () => { |
| 9 | + it('declares the verify_after_edit canonical name', () => { |
| 10 | + const tool = new VerifyAfterEditTool(createMockRelayClient()); |
| 11 | + expect(tool.name).toBe(MCP_TOOLS.VERIFY_AFTER_EDIT); |
| 12 | + expect(tool.name).toBe('domscribe.verify.afterEdit'); |
| 13 | + }); |
| 14 | + |
| 15 | + it('forwards componentStyles, boundingRect, and screenshotRef to the relay', async () => { |
| 16 | + const verifyAnnotation = vi.fn().mockResolvedValue({ |
| 17 | + success: true, |
| 18 | + annotationId, |
| 19 | + result: { |
| 20 | + annotationId, |
| 21 | + verdict: 'match', |
| 22 | + pixelDiffRatio: 0, |
| 23 | + pixelDiffPixels: 0, |
| 24 | + componentStylesDelta: {}, |
| 25 | + boundingRectDelta: {}, |
| 26 | + screenshotRef: 'blob://post-edit/abc', |
| 27 | + capturedAt: '2025-01-01T00:00:00.000Z', |
| 28 | + }, |
| 29 | + }); |
| 30 | + const mockClient = createMockRelayClient({ verifyAnnotation }); |
| 31 | + const tool = new VerifyAfterEditTool(mockClient); |
| 32 | + |
| 33 | + await tool.toolCallback({ |
| 34 | + annotationId, |
| 35 | + postEditComponentStyles: { computed: { color: 'rgb(0, 0, 0)' } }, |
| 36 | + postEditBoundingRect: { |
| 37 | + x: 0, |
| 38 | + y: 0, |
| 39 | + width: 10, |
| 40 | + height: 10, |
| 41 | + top: 0, |
| 42 | + right: 10, |
| 43 | + bottom: 10, |
| 44 | + left: 0, |
| 45 | + }, |
| 46 | + screenshotRef: 'blob://post-edit/abc', |
| 47 | + }); |
| 48 | + |
| 49 | + expect(verifyAnnotation).toHaveBeenCalledWith(annotationId, { |
| 50 | + componentStyles: { computed: { color: 'rgb(0, 0, 0)' } }, |
| 51 | + boundingRect: { |
| 52 | + x: 0, |
| 53 | + y: 0, |
| 54 | + width: 10, |
| 55 | + height: 10, |
| 56 | + top: 0, |
| 57 | + right: 10, |
| 58 | + bottom: 10, |
| 59 | + left: 0, |
| 60 | + }, |
| 61 | + screenshotRef: 'blob://post-edit/abc', |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('returns the relay verdict as structured content', async () => { |
| 66 | + const verifyAnnotation = vi.fn().mockResolvedValue({ |
| 67 | + success: true, |
| 68 | + annotationId, |
| 69 | + result: { |
| 70 | + annotationId, |
| 71 | + verdict: 'partial', |
| 72 | + pixelDiffRatio: 0.005, |
| 73 | + pixelDiffPixels: 250, |
| 74 | + componentStylesDelta: { color: ['red', 'blue'] }, |
| 75 | + boundingRectDelta: {}, |
| 76 | + capturedAt: '2025-01-01T00:00:00.000Z', |
| 77 | + }, |
| 78 | + }); |
| 79 | + const tool = new VerifyAfterEditTool( |
| 80 | + createMockRelayClient({ verifyAnnotation }), |
| 81 | + ); |
| 82 | + |
| 83 | + const result: CallToolResult = await tool.toolCallback({ annotationId }); |
| 84 | + |
| 85 | + expect(result.structuredContent).toMatchObject({ |
| 86 | + success: true, |
| 87 | + result: { verdict: 'partial' }, |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + it('hints at retry when the verdict is regression or no_change', async () => { |
| 92 | + const verifyAnnotation = vi.fn().mockResolvedValue({ |
| 93 | + success: true, |
| 94 | + annotationId, |
| 95 | + result: { |
| 96 | + annotationId, |
| 97 | + verdict: 'no_change', |
| 98 | + pixelDiffRatio: 0, |
| 99 | + pixelDiffPixels: 0, |
| 100 | + componentStylesDelta: {}, |
| 101 | + boundingRectDelta: {}, |
| 102 | + capturedAt: '2025-01-01T00:00:00.000Z', |
| 103 | + reason: 'edit did not land', |
| 104 | + }, |
| 105 | + }); |
| 106 | + const tool = new VerifyAfterEditTool( |
| 107 | + createMockRelayClient({ verifyAnnotation }), |
| 108 | + ); |
| 109 | + |
| 110 | + const result: CallToolResult = await tool.toolCallback({ annotationId }); |
| 111 | + const structured = result.structuredContent as { nextStep: string }; |
| 112 | + |
| 113 | + expect(structured.nextStep).toMatch(/retry/i); |
| 114 | + expect(structured.nextStep).toContain('edit did not land'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('returns an MCP error result when the relay call throws', async () => { |
| 118 | + const verifyAnnotation = vi.fn().mockRejectedValue(new Error('relay down')); |
| 119 | + const tool = new VerifyAfterEditTool( |
| 120 | + createMockRelayClient({ verifyAnnotation }), |
| 121 | + ); |
| 122 | + |
| 123 | + const result: CallToolResult = await tool.toolCallback({ annotationId }); |
| 124 | + |
| 125 | + expect(result.isError).toBe(true); |
| 126 | + }); |
| 127 | + |
| 128 | + it('NEVER inlines screenshot bytes — the serialized tool output stays small even with a long screenshotRef', async () => { |
| 129 | + const longRef = 'blob://post-edit/' + 'x'.repeat(64); |
| 130 | + const verifyAnnotation = vi.fn().mockResolvedValue({ |
| 131 | + success: true, |
| 132 | + annotationId, |
| 133 | + result: { |
| 134 | + annotationId, |
| 135 | + verdict: 'match', |
| 136 | + pixelDiffRatio: 0, |
| 137 | + pixelDiffPixels: 0, |
| 138 | + componentStylesDelta: {}, |
| 139 | + boundingRectDelta: {}, |
| 140 | + screenshotRef: longRef, |
| 141 | + capturedAt: '2025-01-01T00:00:00.000Z', |
| 142 | + }, |
| 143 | + }); |
| 144 | + const tool = new VerifyAfterEditTool( |
| 145 | + createMockRelayClient({ verifyAnnotation }), |
| 146 | + ); |
| 147 | + |
| 148 | + const result: CallToolResult = await tool.toolCallback({ |
| 149 | + annotationId, |
| 150 | + screenshotRef: longRef, |
| 151 | + }); |
| 152 | + const serialized = JSON.stringify(result.structuredContent); |
| 153 | + |
| 154 | + expect(serialized).not.toMatch(/base64/i); |
| 155 | + expect(serialized).not.toMatch(/data:image/i); |
| 156 | + expect(serialized).toContain(longRef); |
| 157 | + expect(serialized.length).toBeLessThan(2048); |
| 158 | + }); |
| 159 | +}); |
0 commit comments