|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import assert from 'node:assert'; |
| 8 | +import {spawnSync} from 'node:child_process'; |
| 9 | +import path from 'node:path'; |
| 10 | +import {describe, it, afterEach, beforeEach} from 'node:test'; |
| 11 | + |
| 12 | +const CLI_PATH = path.resolve('build/src/bin/chrome-devtools.js'); |
| 13 | + |
| 14 | +describe('chrome-devtools', () => { |
| 15 | + const START_ARGS = ['--headless', '--isolated']; |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + spawnSync('node', [CLI_PATH, 'stop']); |
| 19 | + }); |
| 20 | + |
| 21 | + afterEach(() => { |
| 22 | + spawnSync('node', [CLI_PATH, 'stop']); |
| 23 | + }); |
| 24 | + |
| 25 | + it('reports daemon status correctly', () => { |
| 26 | + let result = spawnSync('node', [CLI_PATH, 'status']); |
| 27 | + assert.strictEqual( |
| 28 | + result.stdout.toString(), |
| 29 | + 'chrome-devtools-mcp daemon is not running.\n', |
| 30 | + ); |
| 31 | + |
| 32 | + const startResult = spawnSync('node', [CLI_PATH, 'start', ...START_ARGS]); |
| 33 | + assert.strictEqual( |
| 34 | + startResult.status, |
| 35 | + 0, |
| 36 | + `start command failed: ${startResult.stderr.toString()}`, |
| 37 | + ); |
| 38 | + |
| 39 | + result = spawnSync('node', [CLI_PATH, 'status']); |
| 40 | + assert.strictEqual( |
| 41 | + result.stdout.toString(), |
| 42 | + 'chrome-devtools-mcp daemon is running.\n', |
| 43 | + ); |
| 44 | + }); |
| 45 | + |
| 46 | + it('can start and stop the daemon', () => { |
| 47 | + let result = spawnSync('node', [CLI_PATH, 'status']); |
| 48 | + assert.strictEqual( |
| 49 | + result.stdout.toString(), |
| 50 | + 'chrome-devtools-mcp daemon is not running.\n', |
| 51 | + ); |
| 52 | + |
| 53 | + const startResult = spawnSync('node', [CLI_PATH, 'start', ...START_ARGS]); |
| 54 | + assert.strictEqual( |
| 55 | + startResult.status, |
| 56 | + 0, |
| 57 | + `start command failed: ${startResult.stderr.toString()}`, |
| 58 | + ); |
| 59 | + |
| 60 | + result = spawnSync('node', [CLI_PATH, 'status']); |
| 61 | + assert.strictEqual( |
| 62 | + result.stdout.toString(), |
| 63 | + 'chrome-devtools-mcp daemon is running.\n', |
| 64 | + ); |
| 65 | + |
| 66 | + const stopResult = spawnSync('node', [CLI_PATH, 'stop']); |
| 67 | + assert.strictEqual( |
| 68 | + stopResult.status, |
| 69 | + 0, |
| 70 | + `stop command failed: ${stopResult.stderr.toString()}`, |
| 71 | + ); |
| 72 | + |
| 73 | + result = spawnSync('node', [CLI_PATH, 'status']); |
| 74 | + assert.strictEqual( |
| 75 | + result.stdout.toString(), |
| 76 | + 'chrome-devtools-mcp daemon is not running.\n', |
| 77 | + ); |
| 78 | + }); |
| 79 | + |
| 80 | + it('can invoke list_pages', async () => { |
| 81 | + // Daemon should not be running. |
| 82 | + let result = spawnSync('node', [CLI_PATH, 'status']); |
| 83 | + assert.strictEqual( |
| 84 | + result.stdout.toString(), |
| 85 | + 'chrome-devtools-mcp daemon is not running.\n', |
| 86 | + ); |
| 87 | + |
| 88 | + const startResult = spawnSync('node', [CLI_PATH, 'start', ...START_ARGS]); |
| 89 | + assert.strictEqual( |
| 90 | + startResult.status, |
| 91 | + 0, |
| 92 | + `start command failed: ${startResult.stderr.toString()}`, |
| 93 | + ); |
| 94 | + |
| 95 | + const listPagesResult = spawnSync('node', [CLI_PATH, 'list_pages']); |
| 96 | + assert.strictEqual( |
| 97 | + listPagesResult.status, |
| 98 | + 0, |
| 99 | + `list_pages command failed: ${listPagesResult.stderr.toString()}`, |
| 100 | + ); |
| 101 | + assert( |
| 102 | + listPagesResult.stdout.toString().includes('about:blank'), |
| 103 | + 'list_pages output is unexpected', |
| 104 | + ); |
| 105 | + |
| 106 | + // Daemon should now be running. |
| 107 | + result = spawnSync('node', [CLI_PATH, 'status']); |
| 108 | + assert.strictEqual( |
| 109 | + result.stdout.toString(), |
| 110 | + 'chrome-devtools-mcp daemon is running.\n', |
| 111 | + ); |
| 112 | + }); |
| 113 | + |
| 114 | + it('forwards disclaimers to stderr on start', () => { |
| 115 | + const result = spawnSync('node', [CLI_PATH, 'start', ...START_ARGS]); |
| 116 | + assert.strictEqual( |
| 117 | + result.status, |
| 118 | + 0, |
| 119 | + `start command failed: ${result.stderr.toString()}`, |
| 120 | + ); |
| 121 | + assert( |
| 122 | + result.stderr.toString().includes('chrome-devtools-mcp exposes content'), |
| 123 | + 'Disclaimer not found in stderr on start', |
| 124 | + ); |
| 125 | + }); |
| 126 | +}); |
0 commit comments