-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-e2e-full-stack.js
More file actions
149 lines (123 loc) · 3.71 KB
/
Copy pathrun-e2e-full-stack.js
File metadata and controls
149 lines (123 loc) · 3.71 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
import { spawn } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import path from 'node:path';
import readline from 'node:readline';
import { runTelegramE2E } from './run-e2e.js';
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const BOT_READY_PATTERN = /Bike Ride Bot v[\d.]+(?:-[^\s]+)? started in development mode/;
const BOT_READY_TIMEOUT_MS = 30000;
const BOT_SHUTDOWN_TIMEOUT_MS = 5000;
function prefixedLog(prefix, message) {
console.log(`${prefix} ${message}`);
}
function prefixedError(prefix, message) {
console.error(`${prefix} ${message}`);
}
async function withPrefixedConsole(prefix, callback) {
const original = {
log: console.log,
info: console.info,
warn: console.warn,
error: console.error
};
const wrap = method => (...args) => method(`${prefix} ${args.join(' ')}`);
console.log = wrap(original.log);
console.info = wrap(original.info);
console.warn = wrap(original.warn);
console.error = wrap(original.error);
try {
return await callback();
} finally {
console.log = original.log;
console.info = original.info;
console.warn = original.warn;
console.error = original.error;
}
}
function pipeProcessOutput(stream, prefix, onLine) {
const reader = readline.createInterface({ input: stream });
reader.on('line', line => {
prefixedLog(prefix, line);
onLine?.(line);
});
return reader;
}
function startBotProcess() {
return spawn(process.execPath, ['src/index.js'], {
cwd: repoRoot,
env: {
...process.env,
NODE_ENV: 'development'
},
stdio: ['ignore', 'pipe', 'pipe']
});
}
async function waitForBotReady(botProcess) {
await new Promise((resolve, reject) => {
let settled = false;
const finish = callback => value => {
if (settled) {
return;
}
settled = true;
clearTimeout(timeout);
callback(value);
};
const succeed = finish(resolve);
const fail = finish(reject);
const onOutputLine = line => {
if (BOT_READY_PATTERN.test(line)) {
succeed();
}
};
const stdoutReader = pipeProcessOutput(botProcess.stdout, '[bot]', onOutputLine);
const stderrReader = pipeProcessOutput(botProcess.stderr, '[bot]', onOutputLine);
const cleanup = () => {
stdoutReader.close();
stderrReader.close();
};
const timeout = setTimeout(() => {
cleanup();
fail(new Error(`Timed out waiting for bot startup log: ${BOT_READY_PATTERN}`));
}, BOT_READY_TIMEOUT_MS);
botProcess.once('error', error => {
cleanup();
fail(error);
});
botProcess.once('exit', code => {
cleanup();
fail(new Error(`Bot process exited before readiness with code ${code}`));
});
});
}
async function stopBotProcess(botProcess) {
if (!botProcess || botProcess.killed || botProcess.exitCode !== null) {
return;
}
await new Promise(resolve => {
const timeout = setTimeout(() => {
botProcess.kill('SIGKILL');
}, BOT_SHUTDOWN_TIMEOUT_MS);
botProcess.once('exit', () => {
clearTimeout(timeout);
resolve();
});
botProcess.kill('SIGINT');
});
}
async function main() {
prefixedLog('[runner]', 'Starting local dev bot process...');
const botProcess = startBotProcess();
try {
await waitForBotReady(botProcess);
prefixedLog('[runner]', 'Bot is ready. Starting Telegram E2E suite...');
await withPrefixedConsole('[e2e]', () => runTelegramE2E());
} finally {
prefixedLog('[runner]', 'Stopping local dev bot process...');
await stopBotProcess(botProcess);
}
}
main().catch(error => {
prefixedError('[runner]', `Telegram E2E runner failed: ${error.stack || error.message}`);
process.exitCode = 1;
});