|
6 | 6 |
|
7 | 7 | import assert from 'node:assert'; |
8 | 8 | import {describe, it, afterEach} from 'node:test'; |
| 9 | +import zlib from 'node:zlib'; |
9 | 10 |
|
10 | 11 | import sinon from 'sinon'; |
11 | 12 |
|
@@ -138,6 +139,50 @@ describe('performance', () => { |
138 | 139 | ); |
139 | 140 | }); |
140 | 141 | }); |
| 142 | + |
| 143 | + it('supports filePath', async () => { |
| 144 | + const rawData = loadTraceAsBuffer('basic-trace.json.gz'); |
| 145 | + // rawData is the decompressed buffer (based on loadTraceAsBuffer implementation). |
| 146 | + // We want to simulate saving it as a .gz file, so the tool should compress it. |
| 147 | + const expectedCompressedData = zlib.gzipSync(rawData); |
| 148 | + |
| 149 | + await withMcpContext(async (response, context) => { |
| 150 | + const filePath = 'test-trace.json.gz'; |
| 151 | + const selectedPage = context.getSelectedPage(); |
| 152 | + sinon.stub(selectedPage, 'url').callsFake(() => 'https://www.test.com'); |
| 153 | + sinon.stub(selectedPage, 'goto').callsFake(() => Promise.resolve(null)); |
| 154 | + sinon.stub(selectedPage.tracing, 'start'); |
| 155 | + sinon.stub(selectedPage.tracing, 'stop').resolves(rawData); |
| 156 | + const saveFileStub = sinon |
| 157 | + .stub(context, 'saveFile') |
| 158 | + .resolves({filename: filePath}); |
| 159 | + |
| 160 | + const clock = sinon.useFakeTimers({shouldClearNativeTimers: true}); |
| 161 | + try { |
| 162 | + const handlerPromise = startTrace.handler( |
| 163 | + {params: {reload: true, autoStop: true, filePath}}, |
| 164 | + response, |
| 165 | + context, |
| 166 | + ); |
| 167 | + await clock.tickAsync(6_000); |
| 168 | + await handlerPromise; |
| 169 | + |
| 170 | + assert.ok( |
| 171 | + response.responseLines.includes( |
| 172 | + `The raw trace data was saved to ${filePath}.`, |
| 173 | + ), |
| 174 | + ); |
| 175 | + sinon.assert.calledOnce(saveFileStub); |
| 176 | + const [savedData, savedPath] = saveFileStub.firstCall.args; |
| 177 | + assert.strictEqual(savedPath, filePath); |
| 178 | + // Compare the saved data with expected compressed data |
| 179 | + // We can't compare buffers directly with strictEqual easily if they are different instances, but deepStrictEqual works for Buffers. |
| 180 | + assert.deepStrictEqual(savedData, expectedCompressedData); |
| 181 | + } finally { |
| 182 | + clock.restore(); |
| 183 | + } |
| 184 | + }); |
| 185 | + }); |
141 | 186 | }); |
142 | 187 |
|
143 | 188 | describe('performance_analyze_insight', () => { |
@@ -275,5 +320,31 @@ describe('performance', () => { |
275 | 320 | t.assert.snapshot?.(response.responseLines.join('\n')); |
276 | 321 | }); |
277 | 322 | }); |
| 323 | + |
| 324 | + it('supports filePath', async () => { |
| 325 | + const rawData = loadTraceAsBuffer('basic-trace.json.gz'); |
| 326 | + await withMcpContext(async (response, context) => { |
| 327 | + const filePath = 'test-trace.json'; |
| 328 | + context.setIsRunningPerformanceTrace(true); |
| 329 | + const selectedPage = context.getSelectedPage(); |
| 330 | + const stopTracingStub = sinon |
| 331 | + .stub(selectedPage.tracing, 'stop') |
| 332 | + .resolves(rawData); |
| 333 | + const saveFileStub = sinon |
| 334 | + .stub(context, 'saveFile') |
| 335 | + .resolves({filename: filePath}); |
| 336 | + |
| 337 | + await stopTrace.handler({params: {filePath}}, response, context); |
| 338 | + |
| 339 | + sinon.assert.calledOnce(stopTracingStub); |
| 340 | + sinon.assert.calledOnce(saveFileStub); |
| 341 | + sinon.assert.calledWith(saveFileStub, rawData, filePath); |
| 342 | + assert.ok( |
| 343 | + response.responseLines.includes( |
| 344 | + `The raw trace data was saved to ${filePath}.`, |
| 345 | + ), |
| 346 | + ); |
| 347 | + }); |
| 348 | + }); |
278 | 349 | }); |
279 | 350 | }); |
0 commit comments