|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { CallToolResultSchema } from '@modelcontextprotocol/sdk/types.js'; |
| 3 | +import { scrapeTool } from '../../shared/src/tools/scrape.js'; |
| 4 | +import type { Server } from '@modelcontextprotocol/sdk/server/index.js'; |
| 5 | +import type { IScrapingClients, StrategyConfigFactory } from '../../shared/src/server.js'; |
| 6 | +import { ResourceStorageFactory } from '../../shared/src/storage/index.js'; |
| 7 | + |
| 8 | +// Mock dependencies |
| 9 | +vi.mock('../../shared/src/scraping-strategies.js', () => ({ |
| 10 | + scrapeWithStrategy: vi.fn().mockResolvedValue({ |
| 11 | + success: true, |
| 12 | + content: '<h1>Test Content</h1><p>This is test content.</p>', |
| 13 | + source: 'native', |
| 14 | + }), |
| 15 | +})); |
| 16 | + |
| 17 | +vi.mock('../../shared/src/storage/index.js', () => ({ |
| 18 | + ResourceStorageFactory: { |
| 19 | + create: vi.fn().mockResolvedValue({ |
| 20 | + findByUrlAndExtract: vi.fn().mockResolvedValue([]), |
| 21 | + writeMulti: vi.fn().mockResolvedValue({ |
| 22 | + raw: 'scraped://test.com/page_2024-01-01T00:00:00Z', |
| 23 | + cleaned: 'scraped://test.com/page_2024-01-01T00:00:00Z/cleaned', |
| 24 | + extracted: null, |
| 25 | + }), |
| 26 | + }), |
| 27 | + reset: vi.fn(), |
| 28 | + }, |
| 29 | +})); |
| 30 | + |
| 31 | +vi.mock('../../shared/src/extract/index.js', () => ({ |
| 32 | + ExtractClientFactory: { |
| 33 | + isAvailable: vi.fn().mockReturnValue(false), |
| 34 | + createFromEnv: vi.fn().mockReturnValue(null), |
| 35 | + }, |
| 36 | +})); |
| 37 | + |
| 38 | +vi.mock('../../shared/src/clean/index.js', () => ({ |
| 39 | + createCleaner: vi.fn().mockReturnValue({ |
| 40 | + clean: vi.fn().mockResolvedValue('# Test Content\n\nThis is test content.'), |
| 41 | + }), |
| 42 | +})); |
| 43 | + |
| 44 | +describe('Resource Shape Validation', () => { |
| 45 | + let mockServer: Server; |
| 46 | + let mockClientsFactory: () => IScrapingClients; |
| 47 | + let mockStrategyConfigFactory: StrategyConfigFactory; |
| 48 | + |
| 49 | + beforeEach(() => { |
| 50 | + vi.clearAllMocks(); |
| 51 | + ResourceStorageFactory.reset(); |
| 52 | + |
| 53 | + mockServer = {} as Server; |
| 54 | + mockClientsFactory = () => ({}) as IScrapingClients; |
| 55 | + mockStrategyConfigFactory = () => ({ |
| 56 | + getAllDomainConfigs: vi.fn().mockReturnValue([]), |
| 57 | + getDomainConfig: vi.fn().mockReturnValue(null), |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should return properly formatted embedded resource for saveAndReturn mode', async () => { |
| 62 | + const tool = scrapeTool(mockServer, mockClientsFactory, mockStrategyConfigFactory); |
| 63 | + |
| 64 | + const result = await tool.handler({ |
| 65 | + url: 'https://test.com/page', |
| 66 | + resultHandling: 'saveAndReturn', |
| 67 | + }); |
| 68 | + |
| 69 | + // Validate against MCP SDK schema |
| 70 | + const validation = CallToolResultSchema.safeParse(result); |
| 71 | + |
| 72 | + if (!validation.success) { |
| 73 | + console.error('Validation error:', JSON.stringify(validation.error, null, 2)); |
| 74 | + } |
| 75 | + |
| 76 | + expect(validation.success).toBe(true); |
| 77 | + |
| 78 | + // Check the specific structure |
| 79 | + expect(result.content).toHaveLength(1); |
| 80 | + expect(result.content[0].type).toBe('resource'); |
| 81 | + |
| 82 | + // The resource should be wrapped in a resource property |
| 83 | + expect(result.content[0]).toHaveProperty('resource'); |
| 84 | + expect(result.content[0].resource).toMatchObject({ |
| 85 | + uri: expect.stringContaining('scraped://'), |
| 86 | + name: 'https://test.com/page', |
| 87 | + mimeType: 'text/markdown', |
| 88 | + description: expect.stringContaining('Scraped content from'), |
| 89 | + text: expect.stringContaining('Test Content'), |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should return properly formatted resource_link for saveOnly mode', async () => { |
| 94 | + const tool = scrapeTool(mockServer, mockClientsFactory, mockStrategyConfigFactory); |
| 95 | + |
| 96 | + const result = await tool.handler({ |
| 97 | + url: 'https://test.com/page', |
| 98 | + resultHandling: 'saveOnly', |
| 99 | + }); |
| 100 | + |
| 101 | + // Validate against MCP SDK schema |
| 102 | + const validation = CallToolResultSchema.safeParse(result); |
| 103 | + expect(validation.success).toBe(true); |
| 104 | + |
| 105 | + // Check the specific structure |
| 106 | + expect(result.content).toHaveLength(1); |
| 107 | + expect(result.content[0].type).toBe('resource_link'); |
| 108 | + expect(result.content[0]).not.toHaveProperty('resource'); |
| 109 | + expect(result.content[0]).toMatchObject({ |
| 110 | + type: 'resource_link', |
| 111 | + uri: expect.stringContaining('scraped://'), |
| 112 | + name: 'https://test.com/page', |
| 113 | + mimeType: 'text/markdown', |
| 114 | + description: expect.stringContaining('Scraped content from'), |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should return properly formatted text for returnOnly mode', async () => { |
| 119 | + const tool = scrapeTool(mockServer, mockClientsFactory, mockStrategyConfigFactory); |
| 120 | + |
| 121 | + const result = await tool.handler({ |
| 122 | + url: 'https://test.com/page', |
| 123 | + resultHandling: 'returnOnly', |
| 124 | + }); |
| 125 | + |
| 126 | + // Validate against MCP SDK schema |
| 127 | + const validation = CallToolResultSchema.safeParse(result); |
| 128 | + expect(validation.success).toBe(true); |
| 129 | + |
| 130 | + // Check the specific structure |
| 131 | + expect(result.content).toHaveLength(1); |
| 132 | + expect(result.content[0].type).toBe('text'); |
| 133 | + expect(result.content[0]).toMatchObject({ |
| 134 | + type: 'text', |
| 135 | + text: expect.stringContaining('Test Content'), |
| 136 | + }); |
| 137 | + expect(result.content[0]).not.toHaveProperty('resource'); |
| 138 | + }); |
| 139 | +}); |
0 commit comments