|
4 | 4 | * SPDX-License-Identifier: Apache-2.0 |
5 | 5 | */ |
6 | 6 | import assert from 'node:assert'; |
| 7 | +import fs from 'node:fs/promises'; |
| 8 | +import path from 'node:path'; |
7 | 9 | import {describe, it} from 'node:test'; |
8 | 10 |
|
9 | 11 | import {evaluateScript} from '../../src/tools/script.js'; |
@@ -152,5 +154,38 @@ describe('script', () => { |
152 | 154 | assert.strictEqual(JSON.parse(lineEvaluation), true); |
153 | 155 | }); |
154 | 156 | }); |
| 157 | + |
| 158 | + it('saves result to file when filePath is provided', async () => { |
| 159 | + const testFilePath = path.join(process.cwd(), 'test-script-result.json'); |
| 160 | + |
| 161 | + await withBrowser(async (response, context) => { |
| 162 | + const page = context.getSelectedPage(); |
| 163 | + await page.setContent(html`<div id="data">{"key":"value"}</div>`); |
| 164 | + |
| 165 | + await evaluateScript.handler( |
| 166 | + { |
| 167 | + params: { |
| 168 | + function: String(() => ({result: 'test', number: 42})), |
| 169 | + filePath: testFilePath, |
| 170 | + }, |
| 171 | + }, |
| 172 | + response, |
| 173 | + context, |
| 174 | + ); |
| 175 | + |
| 176 | + assert.strictEqual( |
| 177 | + response.responseLines[0], |
| 178 | + `Saved script result to ${testFilePath}.`, |
| 179 | + ); |
| 180 | + assert.strictEqual(response.responseLines.length, 1); |
| 181 | + |
| 182 | + // Verify file was created and contains expected JSON |
| 183 | + const fileContent = await fs.readFile(testFilePath, 'utf-8'); |
| 184 | + const parsed = JSON.parse(fileContent); |
| 185 | + assert.deepEqual(parsed, {result: 'test', number: 42}); |
| 186 | + }); |
| 187 | + |
| 188 | + await fs.unlink(testFilePath); |
| 189 | + }); |
155 | 190 | }); |
156 | 191 | }); |
0 commit comments