Repository navigation
Expand file tree
/
Copy pathtest.js
More file actions
143 lines (127 loc) · 4.93 KB
/
Copy pathtest.js
File metadata and controls
143 lines (127 loc) · 4.93 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
#!/usr/bin/env node
const { parseArgs, findChromeBinary, isPortInUse } = require('./cli.js');
const assert = require('assert');
const fs = require('fs');
console.log('Running chrome-ctl tests...\n');
// Test 1: parseArgs - basic command
{
const { flags, command, commandArgs } = parseArgs(['navigate', 'https://example.com']);
assert.strictEqual(command, 'navigate');
assert.strictEqual(commandArgs[0], 'https://example.com');
assert.strictEqual(flags.port, 9222);
assert.strictEqual(flags.timeout, 30000);
assert.strictEqual(flags.tab, null);
assert.strictEqual(flags.headless, false);
console.log('✓ parseArgs: basic command');
}
// Test 2: parseArgs - global flags before command
{
const { flags, command, commandArgs } = parseArgs(['--port', '9223', '--timeout', '60000', 'navigate', 'https://example.com']);
assert.strictEqual(command, 'navigate');
assert.strictEqual(commandArgs[0], 'https://example.com');
assert.strictEqual(flags.port, 9223);
assert.strictEqual(flags.timeout, 60000);
console.log('✓ parseArgs: global flags before command');
}
// Test 3: parseArgs - global flags after command
{
const { flags, command, commandArgs } = parseArgs(['navigate', '--port', '9224', 'https://example.com']);
assert.strictEqual(command, 'navigate');
assert.strictEqual(commandArgs[0], 'https://example.com');
assert.strictEqual(flags.port, 9224);
console.log('✓ parseArgs: global flags after command');
}
// Test 4: parseArgs - tab targeting
{
const { flags, command, commandArgs } = parseArgs(['--tab', 'abc123', 'navigate', 'https://example.com']);
assert.strictEqual(command, 'navigate');
assert.strictEqual(flags.tab, 'abc123');
assert.strictEqual(commandArgs[0], 'https://example.com');
console.log('✓ parseArgs: tab targeting');
}
// Test 5: parseArgs - headless flag
{
const { flags, command } = parseArgs(['--headless', 'launch']);
assert.strictEqual(command, 'launch');
assert.strictEqual(flags.headless, true);
console.log('✓ parseArgs: headless flag');
}
// Test 6: parseArgs - multiple command args
{
const { flags, command, commandArgs } = parseArgs(['type', '#input', 'hello', 'world']);
assert.strictEqual(command, 'type');
assert.strictEqual(commandArgs[0], '#input');
assert.strictEqual(commandArgs[1], 'hello');
assert.strictEqual(commandArgs[2], 'world');
console.log('✓ parseArgs: multiple command args');
}
// Test 7: parseArgs - no command (help)
{
const { flags, command, commandArgs } = parseArgs([]);
assert.strictEqual(command, null);
assert.strictEqual(commandArgs.length, 0);
console.log('✓ parseArgs: no command (help)');
}
// Test 8: parseArgs - flags mixed with args
{
const { flags, command, commandArgs } = parseArgs(['navigate', '--timeout', '5000', 'https://slow.com', '--port', '9225']);
assert.strictEqual(command, 'navigate');
assert.strictEqual(commandArgs[0], 'https://slow.com');
assert.strictEqual(flags.timeout, 5000);
assert.strictEqual(flags.port, 9225);
console.log('✓ parseArgs: flags mixed with args');
}
// Test 9: findChromeBinary - basic detection
{
const binary = findChromeBinary();
assert(typeof binary === 'string');
assert(binary.length > 0);
console.log(`✓ findChromeBinary: detected "${binary}"`);
}
// Test 10: findChromeBinary - respects env override
{
const originalBinary = process.env.CHROME_BINARY;
process.env.CHROME_BINARY = '/custom/chrome';
const binary = findChromeBinary();
assert.strictEqual(binary, '/custom/chrome');
if (originalBinary) {
process.env.CHROME_BINARY = originalBinary;
} else {
delete process.env.CHROME_BINARY;
}
console.log('✓ findChromeBinary: respects CHROME_BINARY env');
}
// Test 11: isPortInUse - mock test (manual verification needed)
{
// This requires a running Chrome instance to test properly
// For now, we just verify the function exists and is callable
assert(typeof isPortInUse === 'function');
console.log('✓ isPortInUse: function exists (live test requires Chrome running)');
}
// Test 12: Cross-platform home dir detection
{
const homeDir = process.env.HOME || process.env.USERPROFILE;
assert(homeDir, 'Should detect home directory');
console.log(`✓ Cross-platform home dir: detected "${homeDir}"`);
}
// Test 13: parseArgs - all flags together
{
const { flags, command, commandArgs } = parseArgs([
'--port', '9999',
'--timeout', '10000',
'--tab', 'xyz789',
'--headless',
'navigate',
'https://test.com'
]);
assert.strictEqual(command, 'navigate');
assert.strictEqual(flags.port, 9999);
assert.strictEqual(flags.timeout, 10000);
assert.strictEqual(flags.tab, 'xyz789');
assert.strictEqual(flags.headless, true);
assert.strictEqual(commandArgs[0], 'https://test.com');
console.log('✓ parseArgs: all flags together');
}
console.log('\n✅ All tests passed!\n');
console.log('Note: isPortInUse requires a live Chrome instance for full testing.');
console.log('Run with Chrome on port 9222 to verify port-in-use detection.\n');