|
5 | 5 | */ |
6 | 6 |
|
7 | 7 | import assert from 'node:assert'; |
| 8 | +import fs from 'node:fs'; |
| 9 | +import os from 'node:os'; |
| 10 | +import path from 'node:path'; |
8 | 11 | import {describe, it, afterEach} from 'node:test'; |
9 | 12 |
|
10 | 13 | import sinon from 'sinon'; |
11 | 14 |
|
12 | 15 | import { |
| 16 | + analyzeFile, |
13 | 17 | analyzeInsight, |
14 | 18 | startTrace, |
15 | 19 | stopTrace, |
@@ -276,4 +280,79 @@ describe('performance', () => { |
276 | 280 | }); |
277 | 281 | }); |
278 | 282 | }); |
| 283 | + |
| 284 | + describe('performance_analyze_file', () => { |
| 285 | + it('analyzes a trace file from the filesystem', async () => { |
| 286 | + const rawData = loadTraceAsBuffer('web-dev-with-commit.json.gz'); |
| 287 | + const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'mcp-trace-test-')); |
| 288 | + const tempFilePath = path.join(tempDir, 'trace.json'); |
| 289 | + fs.writeFileSync(tempFilePath, rawData); |
| 290 | + |
| 291 | + try { |
| 292 | + await withMcpContext(async (response, context) => { |
| 293 | + await analyzeFile.handler( |
| 294 | + {params: {filePath: tempFilePath}}, |
| 295 | + response, |
| 296 | + context, |
| 297 | + ); |
| 298 | + |
| 299 | + const output = response.responseLines.join('\n'); |
| 300 | + assert.ok( |
| 301 | + output.includes( |
| 302 | + `Successfully analyzed trace file: ${tempFilePath}`, |
| 303 | + ), |
| 304 | + ); |
| 305 | + assert.ok( |
| 306 | + output.includes('## Summary of Performance trace findings'), |
| 307 | + ); |
| 308 | + assert.ok(output.includes('URL: https://web.dev/')); |
| 309 | + assert.strictEqual(context.recordedTraces().length, 1); |
| 310 | + }); |
| 311 | + } finally { |
| 312 | + fs.rmSync(tempDir, {recursive: true}); |
| 313 | + } |
| 314 | + }); |
| 315 | + |
| 316 | + it('returns an error if the file does not exist', async () => { |
| 317 | + await withMcpContext(async (response, context) => { |
| 318 | + await analyzeFile.handler( |
| 319 | + {params: {filePath: '/nonexistent/path/trace.json'}}, |
| 320 | + response, |
| 321 | + context, |
| 322 | + ); |
| 323 | + |
| 324 | + assert.ok( |
| 325 | + response.responseLines |
| 326 | + .join('\n') |
| 327 | + .includes('An error occurred reading the trace file:'), |
| 328 | + ); |
| 329 | + assert.strictEqual(context.recordedTraces().length, 0); |
| 330 | + }); |
| 331 | + }); |
| 332 | + |
| 333 | + it('returns an error if the file is not valid JSON', async () => { |
| 334 | + const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'mcp-trace-test-')); |
| 335 | + const tempFilePath = path.join(tempDir, 'invalid.json'); |
| 336 | + fs.writeFileSync(tempFilePath, 'not valid json'); |
| 337 | + |
| 338 | + try { |
| 339 | + await withMcpContext(async (response, context) => { |
| 340 | + await analyzeFile.handler( |
| 341 | + {params: {filePath: tempFilePath}}, |
| 342 | + response, |
| 343 | + context, |
| 344 | + ); |
| 345 | + |
| 346 | + assert.ok( |
| 347 | + response.responseLines |
| 348 | + .join('\n') |
| 349 | + .includes('There was an error parsing the trace file:'), |
| 350 | + ); |
| 351 | + assert.strictEqual(context.recordedTraces().length, 0); |
| 352 | + }); |
| 353 | + } finally { |
| 354 | + fs.rmSync(tempDir, {recursive: true}); |
| 355 | + } |
| 356 | + }); |
| 357 | + }); |
279 | 358 | }); |
0 commit comments