-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathlaunch-playwright-mcp.test.js
More file actions
159 lines (135 loc) · 4.46 KB
/
Copy pathlaunch-playwright-mcp.test.js
File metadata and controls
159 lines (135 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const test = require('node:test');
const { EventEmitter } = require('node:events');
const {
PLAYWRIGHT_MCP_PACKAGE,
buildMcpArgs,
launch,
resolveNpxCli,
} = require('../launch-playwright-mcp');
test('buildMcpArgs launches the exact reviewed Playwright MCP version', () => {
const expectedConfigPath = path.join(__dirname, '..', 'playwright-mcp-fullscreen.config.json');
const args = buildMcpArgs('chrome');
const configIndex = args.indexOf('--config');
assert.equal(PLAYWRIGHT_MCP_PACKAGE, '@playwright/mcp@0.0.78');
assert.deepEqual(
args.slice(0, 6),
[
'--yes',
'--ignore-scripts',
'--package=@playwright/mcp@0.0.78',
'playwright-mcp',
'--browser',
'chrome',
],
);
assert.equal(args.some((arg) => /@(latest|next|\^|~|\*)$/.test(arg)), false);
assert.equal(args.includes('--viewport-size'), false);
assert.notEqual(configIndex, -1);
assert.equal(args[configIndex + 1], expectedConfigPath);
});
test('buildMcpArgs preserves config paths with spaces and shell metacharacters as raw argv', () => {
const configPath = '/tmp/Power Pages $(echo unsafe); & [preview]/config\'s "quoted" path.json';
const args = buildMcpArgs('chrome', { configPath });
const configIndex = args.indexOf('--config');
assert.equal(args[configIndex + 1], configPath);
});
test('buildMcpArgs preserves Windows config paths without shell quoting', () => {
const configPath = 'C:\\Users\\Power User & Team\\Power Pages (Preview)\\playwright-mcp.config.json';
const args = buildMcpArgs('msedge', { configPath });
const configIndex = args.indexOf('--config');
assert.equal(args[configIndex + 1], configPath);
});
test('resolveNpxCli finds the Windows npm JavaScript entrypoint', () => {
const expected = 'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npx-cli.js';
const checked = [];
const resolved = resolveNpxCli({
execPath: 'C:\\Program Files\\nodejs\\node.exe',
platform: 'win32',
existsSync(candidate) {
checked.push(candidate);
return candidate === expected;
},
});
assert.equal(resolved, expected);
assert.deepEqual(checked, [
'C:\\Program Files\\lib\\node_modules\\npm\\bin\\npx-cli.js',
expected,
]);
});
test('fullscreen config maximizes the browser and uses the real viewport size', () => {
const configPath = path.join(__dirname, '..', 'playwright-mcp-fullscreen.config.json');
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
assert.deepEqual(config.browser.launchOptions.args, ['--start-maximized', '--start-fullscreen']);
assert.equal(config.browser.contextOptions.viewport, null);
});
test('launch uses Node and raw argv without a shell', () => {
let spawnCall;
const child = new EventEmitter();
launch({
browser: 'msedge',
npxCliPath: 'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npx-cli.js',
spawnFn(command, args, options) {
spawnCall = { command, args, options };
return child;
},
exitFn(code) {
spawnCall.exitCode = code;
},
});
assert.equal(spawnCall.command, process.execPath);
assert.deepEqual(
spawnCall.args.slice(0, 7),
[
'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npx-cli.js',
'--yes',
'--ignore-scripts',
'--package=@playwright/mcp@0.0.78',
'playwright-mcp',
'--browser',
'msedge',
],
);
assert.deepEqual(spawnCall.options, { stdio: 'inherit', shell: false });
child.emit('exit', 7);
assert.equal(spawnCall.exitCode, 7);
});
test('launch reports spawn errors and exits with failure', () => {
const child = new EventEmitter();
const exits = [];
let stderr = '';
launch({
browser: 'chrome',
npxCliPath: '/trusted/npm/bin/npx-cli.js',
spawnFn() {
return child;
},
exitFn(code) {
exits.push(code);
},
writeError(message) {
stderr += message;
},
});
child.emit('error', new Error('spawn ENOENT'));
assert.deepEqual(exits, [1]);
assert.match(stderr, /^Failed to start Playwright MCP: spawn ENOENT\n$/);
});
test('launch treats signal-only child exits as failures', () => {
const child = new EventEmitter();
let exitCode;
launch({
browser: 'chrome',
npxCliPath: '/trusted/npm/bin/npx-cli.js',
spawnFn() {
return child;
},
exitFn(code) {
exitCode = code;
},
});
child.emit('exit', null, 'SIGTERM');
assert.equal(exitCode, 1);
});