-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-queue-full.js
More file actions
226 lines (188 loc) · 5.64 KB
/
test-queue-full.js
File metadata and controls
226 lines (188 loc) · 5.64 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
#!/usr/bin/env node
/**
* Test prompt queue with multiple prompts
*/
class TestTerminal {
constructor() {
this.buffer = 'Welcome to Claude Code!\n\nHuman: ';
this.state = 'ready';
this.responseCount = 0;
}
getBufferContent() {
return this.buffer;
}
sendInput(text) {
if (text === '\r') {
if (this.state === 'ready') {
this.state = 'processing';
this.processPrompt();
}
} else {
this.buffer += text;
}
}
processPrompt() {
this.responseCount++;
const promptNum = this.responseCount;
// Simulate Claude Code response
setTimeout(() => {
this.buffer += `\n\nAssistant: Processing prompt ${promptNum}...`;
// Simulate typing response
setTimeout(() => {
this.buffer += `\nI've completed task ${promptNum}.`;
// Response complete, show Human: prompt
setTimeout(() => {
this.buffer += `\n\nHuman: `;
this.state = 'ready';
console.log(` ✓ Prompt ${promptNum} completed`);
}, 500);
}, 800);
}, 300);
}
}
// Mock prompt queue store
class PromptQueueStore {
constructor() {
this.items = [];
this.isRunning = false;
}
addPrompt(text) {
const id = `prompt-${Date.now()}-${Math.random()}`;
this.items.push({ id, text, status: 'pending' });
return id;
}
getNextPending() {
return this.items.find(item => item.status === 'pending');
}
setPromptStatus(id, status) {
const item = this.items.find(i => i.id === id);
if (item) {
item.status = status;
console.log(` [Store] Prompt ${id.substring(0, 20)}... status -> ${status}`);
}
}
removePrompt(id) {
const index = this.items.findIndex(i => i.id === id);
if (index !== -1) {
const removed = this.items.splice(index, 1)[0];
console.log(` [Store] Removed prompt: "${removed.text}"`);
console.log(` [Store] Remaining prompts: ${this.items.length}`);
}
}
start() {
this.isRunning = true;
}
stop() {
this.isRunning = false;
console.log(' [Store] Queue stopped');
}
}
// Test the queue processing
async function testQueueProcessing() {
console.log('=== Testing Complete Queue Processing ===\n');
const terminal = new TestTerminal();
const store = new PromptQueueStore();
// Add multiple prompts
const prompts = [
'First task: Check the weather',
'Second task: Write a function',
'Third task: Run tests',
'Fourth task: Deploy to production'
];
console.log('Adding prompts to queue:');
prompts.forEach(prompt => {
const id = store.addPrompt(prompt);
console.log(` Added: "${prompt}"`);
});
console.log(`\nTotal prompts in queue: ${store.items.length}\n`);
// Start the queue
store.start();
console.log('Queue started\n');
// Simulate the queue processing logic
let lastBufferLength = 0;
let stableCount = 0;
async function checkAndProcess() {
if (!store.isRunning) {
console.log('\nQueue stopped, ending simulation');
return;
}
const buffer = terminal.getBufferContent();
const isReady = checkIsReady(buffer);
if (isReady) {
const nextPrompt = store.getNextPending();
if (nextPrompt) {
console.log(`\n[Processing] Sending prompt: "${nextPrompt.text}"`);
store.setPromptStatus(nextPrompt.id, 'active');
// Send prompt
terminal.sendInput(nextPrompt.text);
terminal.sendInput('\r');
// Wait for completion
setTimeout(() => {
checkCompletion(nextPrompt.id);
}, 1500);
} else {
console.log('\nNo more pending prompts');
store.stop();
// Final cleanup
const completed = store.items.filter(i => i.status === 'completed');
console.log(`\nRemoving ${completed.length} completed prompts...`);
completed.forEach(item => {
store.removePrompt(item.id);
});
console.log(`\nFinal queue state: ${store.items.length} items remaining`);
}
} else {
setTimeout(checkAndProcess, 500);
}
}
function checkCompletion(promptId) {
const checkInterval = setInterval(() => {
const buffer = terminal.getBufferContent();
const bufferLength = buffer.length;
// Check for stability
if (bufferLength === lastBufferLength) {
stableCount++;
} else {
stableCount = 0;
}
lastBufferLength = bufferLength;
// If stable and ready, mark as completed
if (stableCount > 2 && checkIsReady(buffer)) {
clearInterval(checkInterval);
store.setPromptStatus(promptId, 'completed');
// Auto-remove completed prompt
setTimeout(() => {
store.removePrompt(promptId);
// Continue to next
const hasPending = store.items.some(i => i.status === 'pending');
if (hasPending) {
console.log(' Continuing to next prompt...');
setTimeout(checkAndProcess, 1000);
} else {
console.log('\n✓ All prompts processed!');
store.stop();
}
}, 300);
}
}, 500);
// Timeout after 10 seconds
setTimeout(() => {
clearInterval(checkInterval);
console.log(' [Timeout] Marking as completed');
store.setPromptStatus(promptId, 'completed');
setTimeout(() => {
store.removePrompt(promptId);
checkAndProcess();
}, 300);
}, 10000);
}
// Helper function
function checkIsReady(buffer) {
const trimmed = buffer.toLowerCase().trim();
return trimmed.endsWith('human:') || trimmed.endsWith('h:');
}
// Start processing
checkAndProcess();
}
// Run the test
testQueueProcessing().catch(console.error);