|
| 1 | +import { jest, describe, test, expect, beforeEach, afterEach } from '@jest/globals'; |
| 2 | + |
| 3 | +// Mock all the heavy dependencies that ProbeAgent uses |
| 4 | +jest.mock('@ai-sdk/anthropic', () => ({})); |
| 5 | +jest.mock('@ai-sdk/openai', () => ({})); |
| 6 | +jest.mock('@ai-sdk/google', () => ({})); |
| 7 | +jest.mock('@ai-sdk/amazon-bedrock', () => ({})); |
| 8 | +jest.mock('ai', () => ({ |
| 9 | + generateText: jest.fn(), |
| 10 | + streamText: jest.fn(), |
| 11 | + tool: jest.fn((config) => ({ |
| 12 | + name: config.name, |
| 13 | + description: config.description, |
| 14 | + inputSchema: config.inputSchema, |
| 15 | + execute: config.execute |
| 16 | + })) |
| 17 | +})); |
| 18 | + |
| 19 | +import { ProbeAgent } from '../../src/agent/ProbeAgent.js'; |
| 20 | +import { writeFileSync, unlinkSync, existsSync, mkdirSync, rmSync } from 'fs'; |
| 21 | +import { join } from 'path'; |
| 22 | + |
| 23 | +describe('ReadImage Tool', () => { |
| 24 | + let testDir; |
| 25 | + let agent; |
| 26 | + let testImagePath; |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + // Create a test directory structure |
| 30 | + testDir = join(process.cwd(), 'test-readimage-temp'); |
| 31 | + if (!existsSync(testDir)) { |
| 32 | + mkdirSync(testDir, { recursive: true }); |
| 33 | + } |
| 34 | + |
| 35 | + // Create a simple 1x1 PNG image |
| 36 | + const simplePng = Buffer.from([ |
| 37 | + 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, |
| 38 | + 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, |
| 39 | + 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, |
| 40 | + 0x08, 0x06, 0x00, 0x00, 0x00, 0x1F, 0x15, 0xC4, |
| 41 | + 0x89, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x44, 0x41, |
| 42 | + 0x54, 0x78, 0x9C, 0x62, 0x00, 0x02, 0x00, 0x00, |
| 43 | + 0x05, 0x00, 0x01, 0x0D, 0x0A, 0x2D, 0xB4, 0x00, |
| 44 | + 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, |
| 45 | + 0x42, 0x60, 0x82 |
| 46 | + ]); |
| 47 | + |
| 48 | + testImagePath = join(testDir, 'test-screenshot.png'); |
| 49 | + writeFileSync(testImagePath, simplePng); |
| 50 | + |
| 51 | + // Initialize agent with the test directory |
| 52 | + agent = new ProbeAgent({ |
| 53 | + debug: false, |
| 54 | + path: testDir |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + afterEach(() => { |
| 59 | + // Cleanup |
| 60 | + if (existsSync(testDir)) { |
| 61 | + rmSync(testDir, { recursive: true, force: true }); |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + describe('Tool availability', () => { |
| 66 | + test('readImage tool should be available in toolImplementations', () => { |
| 67 | + expect(agent.toolImplementations).toHaveProperty('readImage'); |
| 68 | + expect(agent.toolImplementations.readImage).toHaveProperty('execute'); |
| 69 | + expect(typeof agent.toolImplementations.readImage.execute).toBe('function'); |
| 70 | + }); |
| 71 | + |
| 72 | + test('readImage tool should be in allowed tools by default', () => { |
| 73 | + expect(agent.allowedTools.isEnabled('readImage')).toBe(true); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('Tool execution', () => { |
| 78 | + test('should successfully load image when given valid path', async () => { |
| 79 | + const result = await agent.toolImplementations.readImage.execute({ |
| 80 | + path: testImagePath |
| 81 | + }); |
| 82 | + |
| 83 | + expect(result).toContain('Image loaded successfully'); |
| 84 | + expect(result).toContain(testImagePath); |
| 85 | + |
| 86 | + // Verify image was actually loaded into pendingImages |
| 87 | + expect(agent.pendingImages.has(testImagePath)).toBe(true); |
| 88 | + |
| 89 | + // Verify it can be retrieved |
| 90 | + const loadedImages = agent.getCurrentImages(); |
| 91 | + expect(loadedImages.length).toBeGreaterThan(0); |
| 92 | + expect(loadedImages[0]).toMatch(/^data:image\/png;base64,/); |
| 93 | + }); |
| 94 | + |
| 95 | + test('should throw error when path parameter is missing', async () => { |
| 96 | + await expect( |
| 97 | + agent.toolImplementations.readImage.execute({}) |
| 98 | + ).rejects.toThrow('Image path is required'); |
| 99 | + }); |
| 100 | + |
| 101 | + test('should throw error when image file does not exist', async () => { |
| 102 | + const nonExistentPath = join(testDir, 'nonexistent.png'); |
| 103 | + |
| 104 | + await expect( |
| 105 | + agent.toolImplementations.readImage.execute({ |
| 106 | + path: nonExistentPath |
| 107 | + }) |
| 108 | + ).rejects.toThrow(); |
| 109 | + }); |
| 110 | + |
| 111 | + test('should handle relative paths correctly', async () => { |
| 112 | + // Create image in a subdirectory |
| 113 | + const subDir = join(testDir, 'images'); |
| 114 | + mkdirSync(subDir, { recursive: true }); |
| 115 | + |
| 116 | + const simplePng = Buffer.from([ |
| 117 | + 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, |
| 118 | + 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, |
| 119 | + 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, |
| 120 | + 0x08, 0x06, 0x00, 0x00, 0x00, 0x1F, 0x15, 0xC4, |
| 121 | + 0x89, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x44, 0x41, |
| 122 | + 0x54, 0x78, 0x9C, 0x62, 0x00, 0x02, 0x00, 0x00, |
| 123 | + 0x05, 0x00, 0x01, 0x0D, 0x0A, 0x2D, 0xB4, 0x00, |
| 124 | + 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, |
| 125 | + 0x42, 0x60, 0x82 |
| 126 | + ]); |
| 127 | + |
| 128 | + const imagePath = join(subDir, 'relative.png'); |
| 129 | + writeFileSync(imagePath, simplePng); |
| 130 | + |
| 131 | + const result = await agent.toolImplementations.readImage.execute({ |
| 132 | + path: imagePath |
| 133 | + }); |
| 134 | + |
| 135 | + expect(result).toContain('Image loaded successfully'); |
| 136 | + expect(agent.pendingImages.has(imagePath)).toBe(true); |
| 137 | + }); |
| 138 | + |
| 139 | + test('should support multiple image formats', async () => { |
| 140 | + const formats = ['test.png', 'test.jpg', 'test.jpeg', 'test.webp', 'test.bmp']; |
| 141 | + |
| 142 | + // Create a simple PNG for all tests (format validation happens elsewhere) |
| 143 | + const simplePng = Buffer.from([ |
| 144 | + 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, |
| 145 | + 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, |
| 146 | + 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, |
| 147 | + 0x08, 0x06, 0x00, 0x00, 0x00, 0x1F, 0x15, 0xC4, |
| 148 | + 0x89, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x44, 0x41, |
| 149 | + 0x54, 0x78, 0x9C, 0x62, 0x00, 0x02, 0x00, 0x00, |
| 150 | + 0x05, 0x00, 0x01, 0x0D, 0x0A, 0x2D, 0xB4, 0x00, |
| 151 | + 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, |
| 152 | + 0x42, 0x60, 0x82 |
| 153 | + ]); |
| 154 | + |
| 155 | + for (const filename of formats) { |
| 156 | + const imagePath = join(testDir, filename); |
| 157 | + writeFileSync(imagePath, simplePng); |
| 158 | + |
| 159 | + const result = await agent.toolImplementations.readImage.execute({ |
| 160 | + path: imagePath |
| 161 | + }); |
| 162 | + |
| 163 | + expect(result).toContain('Image loaded successfully'); |
| 164 | + expect(agent.pendingImages.has(imagePath)).toBe(true); |
| 165 | + } |
| 166 | + }); |
| 167 | + |
| 168 | + test('should not load the same image twice', async () => { |
| 169 | + // Load image first time |
| 170 | + await agent.toolImplementations.readImage.execute({ |
| 171 | + path: testImagePath |
| 172 | + }); |
| 173 | + |
| 174 | + const imagesAfterFirst = agent.getCurrentImages().length; |
| 175 | + |
| 176 | + // Load same image again |
| 177 | + await agent.toolImplementations.readImage.execute({ |
| 178 | + path: testImagePath |
| 179 | + }); |
| 180 | + |
| 181 | + const imagesAfterSecond = agent.getCurrentImages().length; |
| 182 | + |
| 183 | + // Should still have same number of images (no duplicate) |
| 184 | + expect(imagesAfterSecond).toBe(imagesAfterFirst); |
| 185 | + }); |
| 186 | + }); |
| 187 | + |
| 188 | + describe('Security', () => { |
| 189 | + test('should respect allowed folders security', async () => { |
| 190 | + // Create agent with restricted allowed folders |
| 191 | + const restrictedAgent = new ProbeAgent({ |
| 192 | + debug: false, |
| 193 | + path: testDir, |
| 194 | + allowedFolders: [testDir] // Only allow test directory |
| 195 | + }); |
| 196 | + |
| 197 | + // Try to load image outside allowed folder |
| 198 | + const outsidePath = '/tmp/malicious.png'; |
| 199 | + |
| 200 | + await expect( |
| 201 | + restrictedAgent.toolImplementations.readImage.execute({ |
| 202 | + path: outsidePath |
| 203 | + }) |
| 204 | + ).rejects.toThrow(); |
| 205 | + }); |
| 206 | + |
| 207 | + test('should validate file size limits', async () => { |
| 208 | + // The loadImageIfValid method should enforce MAX_IMAGE_FILE_SIZE (20MB) |
| 209 | + // This test verifies the tool respects that limit |
| 210 | + const result = await agent.toolImplementations.readImage.execute({ |
| 211 | + path: testImagePath |
| 212 | + }); |
| 213 | + |
| 214 | + expect(result).toContain('Image loaded successfully'); |
| 215 | + }); |
| 216 | + }); |
| 217 | + |
| 218 | + describe('Integration with message flow', () => { |
| 219 | + test('loaded images should be available in getCurrentImages', async () => { |
| 220 | + agent.clearLoadedImages(); |
| 221 | + |
| 222 | + await agent.toolImplementations.readImage.execute({ |
| 223 | + path: testImagePath |
| 224 | + }); |
| 225 | + |
| 226 | + const images = agent.getCurrentImages(); |
| 227 | + expect(images.length).toBe(1); |
| 228 | + expect(images[0]).toMatch(/^data:image\/png;base64,/); |
| 229 | + }); |
| 230 | + |
| 231 | + test('should work alongside automatic image processing from tool results', async () => { |
| 232 | + // Clear any existing images |
| 233 | + agent.clearLoadedImages(); |
| 234 | + |
| 235 | + // Simulate tool result that mentions an image |
| 236 | + const toolResultWithImage = `Found the file at ${testImagePath}`; |
| 237 | + await agent.processImageReferences(toolResultWithImage); |
| 238 | + |
| 239 | + const imagesFromAutomatic = agent.getCurrentImages().length; |
| 240 | + |
| 241 | + // Now explicitly read another image |
| 242 | + const anotherImage = join(testDir, 'another.png'); |
| 243 | + const simplePng = Buffer.from([ |
| 244 | + 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, |
| 245 | + 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, |
| 246 | + 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, |
| 247 | + 0x08, 0x06, 0x00, 0x00, 0x00, 0x1F, 0x15, 0xC4, |
| 248 | + 0x89, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x44, 0x41, |
| 249 | + 0x54, 0x78, 0x9C, 0x62, 0x00, 0x02, 0x00, 0x00, |
| 250 | + 0x05, 0x00, 0x01, 0x0D, 0x0A, 0x2D, 0xB4, 0x00, |
| 251 | + 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, |
| 252 | + 0x42, 0x60, 0x82 |
| 253 | + ]); |
| 254 | + writeFileSync(anotherImage, simplePng); |
| 255 | + |
| 256 | + await agent.toolImplementations.readImage.execute({ |
| 257 | + path: anotherImage |
| 258 | + }); |
| 259 | + |
| 260 | + const totalImages = agent.getCurrentImages().length; |
| 261 | + expect(totalImages).toBeGreaterThan(imagesFromAutomatic); |
| 262 | + }); |
| 263 | + }); |
| 264 | +}); |
0 commit comments