Repository navigation
Expand file tree
/
Copy pathtest-smart-mode.js
More file actions
executable file
·241 lines (207 loc) · 6.93 KB
/
Copy pathtest-smart-mode.js
File metadata and controls
executable file
·241 lines (207 loc) · 6.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env node
/**
* Smart Mode Test Script
* Tests the focus detection feature by:
* 1. Running the focus-checker binary directly
* 2. Simulating what would happen with different focus states
*/
import { execSync, spawn } from 'child_process';
import { existsSync, readFileSync, writeFileSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
import readline from 'readline';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const focusCheckerPath = join(__dirname, 'bin', 'focus-checker');
// ANSI colors
const colors = {
reset: '\x1b[0m',
green: '\x1b[32m',
yellow: '\x1b[33m',
red: '\x1b[31m',
cyan: '\x1b[36m',
dim: '\x1b[2m',
};
function log(color, ...args) {
console.log(color, ...args, colors.reset);
}
function checkFocus() {
if (!existsSync(focusCheckerPath)) {
return { error: 'not_found', message: 'focus-checker binary not found' };
}
try {
const result = execSync(focusCheckerPath, {
encoding: 'utf8',
timeout: 500,
stdio: ['pipe', 'pipe', 'pipe']
});
return JSON.parse(result);
} catch (e) {
// Try to parse output even on error
try {
const output = e.stdout?.toString() || e.stderr?.toString() || '';
if (output) {
return JSON.parse(output);
}
} catch (parseErr) {
// ignore
}
return { error: 'exec_failed', message: e.message };
}
}
function printFocusResult(focus) {
if (focus.error) {
if (focus.error === 'accessibility_not_granted') {
log(colors.yellow, '⚠️ Accessibility permission not granted');
log(colors.dim, ' Grant permission in System Settings > Privacy & Security > Accessibility');
if (focus.appName) {
log(colors.dim, ` Detected app: ${focus.appName} (${focus.appBundleId || 'no bundle ID'})`);
}
} else {
log(colors.red, `❌ Error: ${focus.error} - ${focus.message || ''}`);
}
return;
}
const isText = focus.isTextInput;
const icon = isText ? '✅' : '❌';
const status = isText ? 'IS text input' : 'NOT text input';
log(colors.cyan, `${icon} ${status}`);
log(colors.dim, ` App: ${focus.appName || 'unknown'}`);
log(colors.dim, ` Role: ${focus.role || 'none'}`);
log(colors.dim, ` Subrole: ${focus.subrole || 'none'}`);
log(colors.dim, ` Editable: ${focus.isEditable || false}`);
log(colors.dim, ` Has text range: ${focus.hasSelectedTextRange || false}`);
if (focus.debug) {
log(colors.dim, ` Debug: ${focus.debug}`);
}
}
function simulateTranscript(text, isTextInput) {
log(colors.cyan, `\n📝 Simulating transcript: "${text}"`);
log(colors.dim, ` Focus state: ${isTextInput ? 'IN text field' : 'NOT in text field'}`);
const lowerText = text.toLowerCase().trim();
// Check if it's a command
const isCommand = lowerText.startsWith('computer ') ||
lowerText === 'affirmative' ||
lowerText === 'retract';
if (isTextInput) {
// In text field - everything works
if (isCommand) {
log(colors.green, ` → Would execute command: ${lowerText}`);
} else {
log(colors.green, ` → Would type text: "${text}"`);
}
} else {
// Not in text field - commands only
if (isCommand) {
log(colors.green, ` → Would execute command: ${lowerText}`);
} else {
log(colors.yellow, ` → Would IGNORE (not a command, not in text field)`);
}
}
}
async function interactiveMode() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
console.log('\n=== Interactive Smart Mode Test ===');
console.log('Commands:');
console.log(' focus - Check current focus state');
console.log(' monitor - Continuously monitor focus (5 sec)');
console.log(' sim TEXT - Simulate transcript with current focus');
console.log(' simtext TEXT - Simulate transcript assuming IN text field');
console.log(' simno TEXT - Simulate transcript assuming NOT in text field');
console.log(' quit - Exit\n');
const prompt = () => {
rl.question('> ', (input) => {
const parts = input.trim().split(' ');
const cmd = parts[0].toLowerCase();
const args = parts.slice(1).join(' ');
switch (cmd) {
case 'focus':
const focus = checkFocus();
printFocusResult(focus);
break;
case 'monitor':
console.log('Monitoring focus for 5 seconds... Click around!');
let lastState = null;
const interval = setInterval(() => {
const f = checkFocus();
const stateKey = `${f.isTextInput}-${f.role}-${f.appName}`;
if (stateKey !== lastState) {
printFocusResult(f);
lastState = stateKey;
}
}, 200);
setTimeout(() => {
clearInterval(interval);
console.log('Monitoring complete.');
prompt();
}, 5000);
return;
case 'sim':
if (!args) {
console.log('Usage: sim TEXT');
} else {
const focus = checkFocus();
const isTextInput = focus.isTextInput === true;
simulateTranscript(args, isTextInput);
}
break;
case 'simtext':
if (!args) {
console.log('Usage: simtext TEXT');
} else {
simulateTranscript(args, true);
}
break;
case 'simno':
if (!args) {
console.log('Usage: simno TEXT');
} else {
simulateTranscript(args, false);
}
break;
case 'quit':
case 'exit':
case 'q':
rl.close();
process.exit(0);
return;
default:
if (input.trim()) {
console.log('Unknown command. Type "focus", "monitor", "sim TEXT", or "quit"');
}
}
prompt();
});
};
prompt();
}
async function main() {
console.log('=== Smart Mode Focus Detection Test ===\n');
console.log(`Focus checker: ${focusCheckerPath}`);
console.log(`Exists: ${existsSync(focusCheckerPath)}\n`);
// Initial focus check
console.log('--- Current Focus State ---');
const focus = checkFocus();
printFocusResult(focus);
// Show what would happen with sample transcripts
console.log('\n--- Sample Transcript Simulations ---');
const isTextInput = focus.isTextInput === true && !focus.error;
// Test various transcripts
const samples = [
'Hello world', // Regular text
'computer enter', // Command
'affirmative', // Special command without "computer"
'retract', // Undo command
'computer smart mode on', // Smart mode command
'This is a longer sentence that would be typed normally.',
];
for (const text of samples) {
simulateTranscript(text, isTextInput);
}
// Start interactive mode
await interactiveMode();
}
main().catch(console.error);