alvin-bot 4.13.0 → 4.13.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +44 -0
- package/dist/services/process-manager.js +291 -0
- package/dist/web/doctor-api.js +59 -67
- package/dist/web/setup-api.js +52 -0
- package/package.json +1 -1
- package/test/process-manager.test.ts +186 -0
- package/test/slack-test-connection.test.ts +176 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,50 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to Alvin Bot are documented here.
|
|
4
4
|
|
|
5
|
+
## [4.13.1] — 2026-04-16
|
|
6
|
+
|
|
7
|
+
### 🐛 Patch: Slack Test Connection + PM2 → launchd migration for Maintenance UI
|
|
8
|
+
|
|
9
|
+
Two latent UI bugs surfaced during live Slack setup:
|
|
10
|
+
|
|
11
|
+
**Bug 1 — `/api/platforms/test-connection` returned "Unknown platform" for Slack.** The handler in `setup-api.ts` only knew about telegram/discord/signal/whatsapp. Users who entered a valid Bot Token (`xoxb-…`) + App Token (`xapp-…`) and clicked Test Connection got a confusing "Unknown platform" error — couldn't tell if their tokens were wrong or the feature was broken.
|
|
12
|
+
|
|
13
|
+
**Fix:** New `slack` case in the handler. Validates Bot Token via `https://slack.com/api/auth.test` (cheap, ~100ms). For App Token, checks the `xapp-` prefix as the quickest sanity check (Socket Mode can't actually be "pinged" without opening a persistent WebSocket). Returns the authenticated bot user + team name on success, or Slack's own `auth.test` error (e.g. `invalid_auth`, `token_expired`) on failure. Warns if App Token is missing or has wrong prefix even when Bot Token is valid — helps users notice they only configured half the pair.
|
|
14
|
+
|
|
15
|
+
**Bug 2 — Maintenance section's buttons were broken on macOS launchd installs.** Since v4.8 the macOS install runs under `launchd` (`com.alvinbot.app.plist`), not PM2. But `doctor-api.ts` kept calling `pm2 jlist`/`pm2 restart`/`pm2 stop`/`pm2 logs`. Results: status endpoint returned stale data from ghost PM2 entries (uptime/memory/cpu/restarts all wrong), Stop/Start buttons silently failed, log viewer was empty. The Restart button accidentally worked because it used `scheduleGracefulRestart` (launchd's `KeepAlive` auto-brings-back on exit).
|
|
16
|
+
|
|
17
|
+
**Fix:** New `src/services/process-manager.ts` abstraction that auto-detects the active supervisor per request:
|
|
18
|
+
- **launchd** (macOS) if `launchctl print gui/$UID/com.alvinbot.app` succeeds
|
|
19
|
+
- **pm2** (VPS / legacy installs) if `pm2 jlist` lists our process
|
|
20
|
+
- **standalone** if neither (fallback — only Restart works, since there's no supervisor to bring the process back)
|
|
21
|
+
|
|
22
|
+
Each manager implements `getStatus()`, `stop()`, `start()`, `getLogs()` with the right tooling:
|
|
23
|
+
- launchd: `launchctl print` + `ps -p <pid> -o %cpu=,%mem=,rss=,etime=` for resource stats, `launchctl bootout` / `bootstrap` for stop/start, `tail` on the known log paths for logs
|
|
24
|
+
- pm2: unchanged — `pm2 jlist` / `pm2 stop` / `pm2 start` / `pm2 logs`
|
|
25
|
+
- standalone: `process.uptime()` / `process.memoryUsage()` / manual log tailing
|
|
26
|
+
|
|
27
|
+
The WebUI routes (`/api/pm2/status`, `/api/pm2/action`, `/api/pm2/logs`) keep their names for compat but now dispatch via `detectProcessManager()`. Real-world verified against the running bot: detection returned `launchd`, PID/uptime/memory all correct from the actual launchd-managed process (not a stale PM2 ghost).
|
|
28
|
+
|
|
29
|
+
### Testing
|
|
30
|
+
|
|
31
|
+
- **Baseline**: 460 tests (v4.13.0)
|
|
32
|
+
- **New**:
|
|
33
|
+
- `test/slack-test-connection.test.ts` — 5 tests (no tokens set, auth.test accepts, auth.test rejects, App Token format warning, unknown platform regression)
|
|
34
|
+
- `test/process-manager.test.ts` — 10 tests (detection order, each manager's status parsing, stop/start command dispatch)
|
|
35
|
+
- **Total**: 475 tests, all green, TSC clean
|
|
36
|
+
- **Live verification**: ran `detectProcessManager().getStatus()` against the actual running bot → returned `launchd`, PID 4767 (matches `launchctl print pid = 4767`), uptime 655s, memory 76MB — all real data, not stale PM2 cache
|
|
37
|
+
|
|
38
|
+
### Files changed
|
|
39
|
+
|
|
40
|
+
- **NEW**: `src/services/process-manager.ts`, `test/slack-test-connection.test.ts`, `test/process-manager.test.ts`
|
|
41
|
+
- **Modified**: `src/web/setup-api.ts` (+slack case in test-connection), `src/web/doctor-api.ts` (routes use process-manager abstraction), `package.json` (4.13.0 → 4.13.1)
|
|
42
|
+
|
|
43
|
+
### Known limitations (deferred to v4.14)
|
|
44
|
+
|
|
45
|
+
- **Slack subagent support**: v4.13.0's `mcp__alvin__dispatch_agent` tool only activates on the Telegram handler (passes `alvinDispatchContext`). Slack users can receive normal replies but can't trigger background sub-agents yet. Requires extending `PendingAsyncAgent.chatId` to `number | string`, adding `platform` to the watcher's pending record, and making `subagent-delivery.ts` platform-aware. Tracked for v4.14.
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
5
49
|
## [4.13.0] — 2026-04-16
|
|
6
50
|
|
|
7
51
|
### ✨ Major: truly detached sub-agent dispatch via `alvin_dispatch_agent` MCP tool
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* v4.13.1 — Process manager abstraction for the Maintenance Web UI.
|
|
3
|
+
*
|
|
4
|
+
* History: the bot was originally PM2-managed. Since v4.8 the macOS
|
|
5
|
+
* install uses launchd (`com.alvinbot.app.plist`). The WebUI
|
|
6
|
+
* Maintenance section kept calling `pm2 jlist`/`pm2 restart`/...
|
|
7
|
+
* which returned "PM2 not available" for launchd users — all status,
|
|
8
|
+
* stop, start, and logs buttons were broken.
|
|
9
|
+
*
|
|
10
|
+
* This module auto-detects the active manager per request and
|
|
11
|
+
* routes commands accordingly:
|
|
12
|
+
*
|
|
13
|
+
* - launchd (macOS) — via `launchctl print` / `bootout` / `bootstrap`
|
|
14
|
+
* - pm2 (VPS / Linux) — via `pm2 jlist` / `pm2 stop` / `pm2 start`
|
|
15
|
+
* - standalone — no supervisor; only `scheduleGracefulRestart` works
|
|
16
|
+
*
|
|
17
|
+
* Restart is NOT on this interface — it always uses
|
|
18
|
+
* `scheduleGracefulRestart` (Grammy-safe) and relies on whichever
|
|
19
|
+
* supervisor is present to bring the process back. For "standalone",
|
|
20
|
+
* a restart effectively kills the process and the user has to run it
|
|
21
|
+
* again manually (we warn in the UI).
|
|
22
|
+
*/
|
|
23
|
+
import { execSync } from "node:child_process";
|
|
24
|
+
import os from "node:os";
|
|
25
|
+
import { resolve } from "node:path";
|
|
26
|
+
const LAUNCHD_LABEL = "com.alvinbot.app";
|
|
27
|
+
const LAUNCHD_PLIST = resolve(os.homedir(), "Library", "LaunchAgents", `${LAUNCHD_LABEL}.plist`);
|
|
28
|
+
const PM2_NAME = "alvin-bot";
|
|
29
|
+
// ── Detection ───────────────────────────────────────────────────
|
|
30
|
+
export function detectProcessManager(opts = {}) {
|
|
31
|
+
const platform = opts.platform ?? process.platform;
|
|
32
|
+
const uid = opts.uid ?? (typeof process.getuid === "function" ? process.getuid() : 0);
|
|
33
|
+
// Only try launchd on macOS
|
|
34
|
+
if (platform === "darwin") {
|
|
35
|
+
try {
|
|
36
|
+
const out = execSync(`launchctl print gui/${uid}/${LAUNCHD_LABEL}`, { encoding: "utf-8", timeout: 3000, stdio: "pipe" });
|
|
37
|
+
if (out && out.length > 0) {
|
|
38
|
+
return createLaunchdManager(uid);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
// Not loaded in launchd — fall through
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
// PM2 fallback (Linux VPS, or Mac installs that stayed on PM2)
|
|
46
|
+
try {
|
|
47
|
+
const out = execSync("pm2 jlist", {
|
|
48
|
+
encoding: "utf-8",
|
|
49
|
+
timeout: 3000,
|
|
50
|
+
stdio: "pipe",
|
|
51
|
+
});
|
|
52
|
+
const parsed = JSON.parse(out);
|
|
53
|
+
if (Array.isArray(parsed) &&
|
|
54
|
+
parsed.some((p) => p?.name === PM2_NAME)) {
|
|
55
|
+
return createPm2Manager();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
// pm2 not installed or didn't report our process
|
|
60
|
+
}
|
|
61
|
+
return createStandaloneManager();
|
|
62
|
+
}
|
|
63
|
+
function parseLaunchdPrint(text) {
|
|
64
|
+
const out = {};
|
|
65
|
+
// state = running
|
|
66
|
+
const stateMatch = text.match(/\bstate\s*=\s*(\S+)/);
|
|
67
|
+
if (stateMatch)
|
|
68
|
+
out.state = stateMatch[1];
|
|
69
|
+
// pid = 12345
|
|
70
|
+
const pidMatch = text.match(/\bpid\s*=\s*(\d+)/);
|
|
71
|
+
if (pidMatch)
|
|
72
|
+
out.pid = Number(pidMatch[1]);
|
|
73
|
+
// program = /path/to/node
|
|
74
|
+
const programMatch = text.match(/\bprogram\s*=\s*(\S+)/);
|
|
75
|
+
if (programMatch)
|
|
76
|
+
out.program = programMatch[1];
|
|
77
|
+
// working directory = /path
|
|
78
|
+
const cwdMatch = text.match(/\bworking directory\s*=\s*(\S+)/);
|
|
79
|
+
if (cwdMatch)
|
|
80
|
+
out.cwd = cwdMatch[1];
|
|
81
|
+
return out;
|
|
82
|
+
}
|
|
83
|
+
export function createLaunchdManager(uid) {
|
|
84
|
+
const service = `gui/${uid}/${LAUNCHD_LABEL}`;
|
|
85
|
+
return {
|
|
86
|
+
kind: "launchd",
|
|
87
|
+
async getStatus() {
|
|
88
|
+
try {
|
|
89
|
+
const out = execSync(`launchctl print ${service}`, {
|
|
90
|
+
encoding: "utf-8",
|
|
91
|
+
timeout: 3000,
|
|
92
|
+
stdio: "pipe",
|
|
93
|
+
});
|
|
94
|
+
const parsed = parseLaunchdPrint(out);
|
|
95
|
+
const pid = parsed.pid;
|
|
96
|
+
// Enrich with ps info if we have a PID
|
|
97
|
+
let memory;
|
|
98
|
+
let cpu;
|
|
99
|
+
let uptime;
|
|
100
|
+
if (pid) {
|
|
101
|
+
try {
|
|
102
|
+
// ps output: %cpu %mem rss etime
|
|
103
|
+
const psOut = execSync(`ps -p ${pid} -o %cpu=,%mem=,rss=,etime=`, { encoding: "utf-8", timeout: 2000, stdio: "pipe" }).trim();
|
|
104
|
+
const [cpuStr, , rssStr, etime] = psOut.split(/\s+/);
|
|
105
|
+
cpu = parseFloat(cpuStr) || 0;
|
|
106
|
+
memory = (parseInt(rssStr, 10) || 0) * 1024; // rss is kB
|
|
107
|
+
uptime = parseEtimeToMs(etime);
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
/* ps may fail if pid vanished — ignore */
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
kind: "launchd",
|
|
115
|
+
status: parsed.state === "running" ? "running" : parsed.state || "unknown",
|
|
116
|
+
pid,
|
|
117
|
+
uptime,
|
|
118
|
+
memory,
|
|
119
|
+
cpu,
|
|
120
|
+
execPath: parsed.program,
|
|
121
|
+
cwd: parsed.cwd,
|
|
122
|
+
nodeVersion: process.version,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
catch {
|
|
126
|
+
return { kind: "launchd", status: "not-loaded" };
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
async stop() {
|
|
130
|
+
// bootout removes the service from the domain, which stops it
|
|
131
|
+
// and disables KeepAlive until bootstrap is run again.
|
|
132
|
+
execSync(`launchctl bootout ${service}`, {
|
|
133
|
+
encoding: "utf-8",
|
|
134
|
+
timeout: 5000,
|
|
135
|
+
stdio: "pipe",
|
|
136
|
+
});
|
|
137
|
+
},
|
|
138
|
+
async start() {
|
|
139
|
+
// bootstrap re-registers the plist with the domain.
|
|
140
|
+
execSync(`launchctl bootstrap gui/${uid} ${JSON.stringify(LAUNCHD_PLIST).slice(1, -1)}`, { encoding: "utf-8", timeout: 5000, stdio: "pipe" });
|
|
141
|
+
},
|
|
142
|
+
async getLogs(lines = 30) {
|
|
143
|
+
// launchd redirects stdout/stderr to files — just tail them.
|
|
144
|
+
const logDir = resolve(process.env.ALVIN_DATA_DIR || resolve(os.homedir(), ".alvin-bot"), "logs");
|
|
145
|
+
const outLog = resolve(logDir, "alvin-bot.out.log");
|
|
146
|
+
const errLog = resolve(logDir, "alvin-bot.err.log");
|
|
147
|
+
try {
|
|
148
|
+
return execSync(`tail -n ${lines} ${outLog} ${errLog} 2>/dev/null`, {
|
|
149
|
+
encoding: "utf-8",
|
|
150
|
+
timeout: 3000,
|
|
151
|
+
stdio: "pipe",
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
return "No logs available.";
|
|
156
|
+
}
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
function parseEtimeToMs(etime) {
|
|
161
|
+
// ps etime format: "MM:SS", "HH:MM:SS", "D-HH:MM:SS"
|
|
162
|
+
if (!etime)
|
|
163
|
+
return undefined;
|
|
164
|
+
const parts = etime.split("-");
|
|
165
|
+
let days = 0;
|
|
166
|
+
let hms;
|
|
167
|
+
if (parts.length === 2) {
|
|
168
|
+
days = parseInt(parts[0], 10) || 0;
|
|
169
|
+
hms = parts[1];
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
hms = parts[0];
|
|
173
|
+
}
|
|
174
|
+
const bits = hms.split(":").map((x) => parseInt(x, 10) || 0);
|
|
175
|
+
let h = 0, m = 0, s = 0;
|
|
176
|
+
if (bits.length === 3)
|
|
177
|
+
[h, m, s] = bits;
|
|
178
|
+
else if (bits.length === 2)
|
|
179
|
+
[m, s] = bits;
|
|
180
|
+
else
|
|
181
|
+
return undefined;
|
|
182
|
+
return (((days * 24 + h) * 60 + m) * 60 + s) * 1000;
|
|
183
|
+
}
|
|
184
|
+
export function createPm2Manager() {
|
|
185
|
+
return {
|
|
186
|
+
kind: "pm2",
|
|
187
|
+
async getStatus() {
|
|
188
|
+
try {
|
|
189
|
+
const out = execSync("pm2 jlist", {
|
|
190
|
+
encoding: "utf-8",
|
|
191
|
+
timeout: 3000,
|
|
192
|
+
stdio: "pipe",
|
|
193
|
+
});
|
|
194
|
+
const procs = JSON.parse(out);
|
|
195
|
+
const me = procs.find((p) => p.name === PM2_NAME);
|
|
196
|
+
if (!me) {
|
|
197
|
+
return { kind: "pm2", status: "unknown" };
|
|
198
|
+
}
|
|
199
|
+
const env = me.pm2_env ?? {};
|
|
200
|
+
return {
|
|
201
|
+
kind: "pm2",
|
|
202
|
+
status: env.status || "unknown",
|
|
203
|
+
pid: me.pid,
|
|
204
|
+
uptime: env.pm_uptime ? Date.now() - env.pm_uptime : undefined,
|
|
205
|
+
memory: me.monit?.memory,
|
|
206
|
+
cpu: me.monit?.cpu,
|
|
207
|
+
restarts: env.restart_time ?? 0,
|
|
208
|
+
version: env.version,
|
|
209
|
+
nodeVersion: env.node_version || process.version,
|
|
210
|
+
execPath: env.pm_exec_path,
|
|
211
|
+
cwd: env.pm_cwd,
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
catch {
|
|
215
|
+
return { kind: "pm2", status: "unknown" };
|
|
216
|
+
}
|
|
217
|
+
},
|
|
218
|
+
async stop() {
|
|
219
|
+
execSync(`pm2 stop ${PM2_NAME}`, {
|
|
220
|
+
encoding: "utf-8",
|
|
221
|
+
timeout: 10_000,
|
|
222
|
+
stdio: "pipe",
|
|
223
|
+
});
|
|
224
|
+
},
|
|
225
|
+
async start() {
|
|
226
|
+
execSync(`pm2 start ${PM2_NAME}`, {
|
|
227
|
+
encoding: "utf-8",
|
|
228
|
+
timeout: 10_000,
|
|
229
|
+
stdio: "pipe",
|
|
230
|
+
});
|
|
231
|
+
},
|
|
232
|
+
async getLogs(lines = 30) {
|
|
233
|
+
try {
|
|
234
|
+
const raw = execSync(`pm2 logs ${PM2_NAME} --nostream --lines ${lines} 2>&1`, {
|
|
235
|
+
encoding: "utf-8",
|
|
236
|
+
timeout: 5000,
|
|
237
|
+
stdio: "pipe",
|
|
238
|
+
env: { ...process.env, FORCE_COLOR: "0", NO_COLOR: "1" },
|
|
239
|
+
});
|
|
240
|
+
// eslint-disable-next-line no-control-regex
|
|
241
|
+
return raw.replace(/\x1b\[[0-9;]*m/g, "");
|
|
242
|
+
}
|
|
243
|
+
catch {
|
|
244
|
+
return "No logs available.";
|
|
245
|
+
}
|
|
246
|
+
},
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
// ── standalone ──────────────────────────────────────────────────
|
|
250
|
+
export function createStandaloneManager() {
|
|
251
|
+
return {
|
|
252
|
+
kind: "standalone",
|
|
253
|
+
async getStatus() {
|
|
254
|
+
return {
|
|
255
|
+
kind: "standalone",
|
|
256
|
+
status: "running",
|
|
257
|
+
pid: process.pid,
|
|
258
|
+
uptime: process.uptime() * 1000,
|
|
259
|
+
memory: process.memoryUsage().rss,
|
|
260
|
+
nodeVersion: process.version,
|
|
261
|
+
execPath: process.execPath,
|
|
262
|
+
cwd: process.cwd(),
|
|
263
|
+
};
|
|
264
|
+
},
|
|
265
|
+
async stop() {
|
|
266
|
+
// No supervisor — just exit. User must restart manually.
|
|
267
|
+
setTimeout(() => process.exit(0), 300);
|
|
268
|
+
},
|
|
269
|
+
async start() {
|
|
270
|
+
// Cannot start ourselves if we're already running (nonsensical).
|
|
271
|
+
// Callers should not hit this path when status is "running".
|
|
272
|
+
throw new Error("standalone: cannot 'start' — no supervisor. Run the bot manually.");
|
|
273
|
+
},
|
|
274
|
+
async getLogs(lines = 30) {
|
|
275
|
+
// Standalone mode may or may not redirect stdout. Try the
|
|
276
|
+
// default ~/.alvin-bot/logs path first.
|
|
277
|
+
const logDir = resolve(process.env.ALVIN_DATA_DIR || resolve(os.homedir(), ".alvin-bot"), "logs");
|
|
278
|
+
const outLog = resolve(logDir, "alvin-bot.out.log");
|
|
279
|
+
try {
|
|
280
|
+
return execSync(`tail -n ${lines} ${outLog} 2>/dev/null`, {
|
|
281
|
+
encoding: "utf-8",
|
|
282
|
+
timeout: 3000,
|
|
283
|
+
stdio: "pipe",
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
catch {
|
|
287
|
+
return "No logs available (standalone mode — stdout not captured).";
|
|
288
|
+
}
|
|
289
|
+
},
|
|
290
|
+
};
|
|
291
|
+
}
|
package/dist/web/doctor-api.js
CHANGED
|
@@ -491,42 +491,42 @@ export async function handleDoctorAPI(req, res, urlPath, body) {
|
|
|
491
491
|
scheduleGracefulRestart(500);
|
|
492
492
|
return true;
|
|
493
493
|
}
|
|
494
|
-
// ──
|
|
495
|
-
//
|
|
494
|
+
// ── Process Control (v4.13.1: launchd/pm2/standalone auto-detect) ──
|
|
495
|
+
//
|
|
496
|
+
// Routes kept under `/api/pm2/*` for UI compat — the UI still calls
|
|
497
|
+
// those paths. Under the hood we now use the process-manager
|
|
498
|
+
// abstraction which auto-detects launchd (macOS native installs)
|
|
499
|
+
// or pm2 (VPS / legacy Mac installs) or standalone (neither).
|
|
500
|
+
// GET /api/pm2/status — Get process info via detected manager
|
|
496
501
|
if (urlPath === "/api/pm2/status") {
|
|
497
502
|
try {
|
|
498
|
-
const
|
|
499
|
-
const
|
|
500
|
-
|
|
501
|
-
const botProcess = processes.find((p) => p.name === "alvin-bot" ||
|
|
502
|
-
p.pm2_env?.pm_exec_path?.includes("alvin-bot")) || processes[0]; // fallback to first process
|
|
503
|
-
if (!botProcess) {
|
|
504
|
-
res.end(JSON.stringify({ error: "No PM2 process found" }));
|
|
505
|
-
return true;
|
|
506
|
-
}
|
|
507
|
-
const env = botProcess.pm2_env || {};
|
|
503
|
+
const { detectProcessManager } = await import("../services/process-manager.js");
|
|
504
|
+
const pm = detectProcessManager();
|
|
505
|
+
const status = await pm.getStatus();
|
|
508
506
|
res.end(JSON.stringify({
|
|
509
507
|
process: {
|
|
510
|
-
name:
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
508
|
+
name: "alvin-bot",
|
|
509
|
+
kind: status.kind,
|
|
510
|
+
pid: status.pid ?? 0,
|
|
511
|
+
status: status.status,
|
|
512
|
+
uptime: status.uptime ?? 0,
|
|
513
|
+
memory: status.memory ?? 0,
|
|
514
|
+
cpu: status.cpu ?? 0,
|
|
515
|
+
restarts: status.restarts ?? 0,
|
|
516
|
+
version: status.version || "?",
|
|
517
|
+
nodeVersion: status.nodeVersion || process.version,
|
|
518
|
+
execPath: status.execPath || "?",
|
|
519
|
+
cwd: status.cwd || "?",
|
|
521
520
|
},
|
|
522
521
|
}));
|
|
523
522
|
}
|
|
524
523
|
catch (err) {
|
|
525
|
-
|
|
524
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
525
|
+
res.end(JSON.stringify({ error: `Process manager detection failed: ${msg}` }));
|
|
526
526
|
}
|
|
527
527
|
return true;
|
|
528
528
|
}
|
|
529
|
-
// POST /api/pm2/action — Execute
|
|
529
|
+
// POST /api/pm2/action — Execute action via detected manager
|
|
530
530
|
if (urlPath === "/api/pm2/action" && req.method === "POST") {
|
|
531
531
|
try {
|
|
532
532
|
const { action } = JSON.parse(body);
|
|
@@ -536,41 +536,44 @@ export async function handleDoctorAPI(req, res, urlPath, body) {
|
|
|
536
536
|
res.end(JSON.stringify({ ok: false, error: `Invalid action: ${action}` }));
|
|
537
537
|
return true;
|
|
538
538
|
}
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
try {
|
|
542
|
-
const jlist = execSync("pm2 jlist", { encoding: "utf-8", timeout: 5000, stdio: "pipe" });
|
|
543
|
-
const procs = JSON.parse(jlist);
|
|
544
|
-
const found = procs.find((p) => p.name === "alvin-bot" || p.name === "alvin-bot");
|
|
545
|
-
if (found)
|
|
546
|
-
processName = found.name;
|
|
547
|
-
}
|
|
548
|
-
catch { /* use default */ }
|
|
539
|
+
const { detectProcessManager } = await import("../services/process-manager.js");
|
|
540
|
+
const pm = detectProcessManager();
|
|
549
541
|
if (action === "flush") {
|
|
550
|
-
|
|
542
|
+
// Truncate our own log files directly — works on both launchd
|
|
543
|
+
// and standalone. PM2's flush is also just truncation.
|
|
544
|
+
const logDir = resolve(DATA_DIR, "logs");
|
|
545
|
+
for (const f of ["alvin-bot.out.log", "alvin-bot.err.log"]) {
|
|
546
|
+
try {
|
|
547
|
+
fs.truncateSync(resolve(logDir, f), 0);
|
|
548
|
+
}
|
|
549
|
+
catch {
|
|
550
|
+
/* file may not exist — ignore */
|
|
551
|
+
}
|
|
552
|
+
}
|
|
551
553
|
res.end(JSON.stringify({ ok: true, message: "Logs flushed" }));
|
|
552
554
|
return true;
|
|
553
555
|
}
|
|
554
556
|
if (action === "stop") {
|
|
555
|
-
// Stop is special —
|
|
556
|
-
res.end(JSON.stringify({ ok: true, message:
|
|
557
|
+
// Stop is special — can't respond after we've killed ourselves.
|
|
558
|
+
res.end(JSON.stringify({ ok: true, message: `Bot is stopping (${pm.kind})...` }));
|
|
557
559
|
setTimeout(() => {
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
}
|
|
561
|
-
catch { /* process might already be dead */ }
|
|
560
|
+
pm.stop().catch(() => {
|
|
561
|
+
/* process might already be dead */
|
|
562
|
+
});
|
|
562
563
|
}, 300);
|
|
563
564
|
return true;
|
|
564
565
|
}
|
|
565
566
|
if (action === "start") {
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
res.end(JSON.stringify({ ok: true, message: "Bot started" }));
|
|
567
|
+
await pm.start();
|
|
568
|
+
res.end(JSON.stringify({ ok: true, message: `Bot started via ${pm.kind}` }));
|
|
569
569
|
return true;
|
|
570
570
|
}
|
|
571
571
|
if (action === "restart" || action === "reload") {
|
|
572
572
|
const { scheduleGracefulRestart } = await import("../services/restart.js");
|
|
573
|
-
res.end(JSON.stringify({
|
|
573
|
+
res.end(JSON.stringify({
|
|
574
|
+
ok: true,
|
|
575
|
+
message: `Bot is ${action === "restart" ? "restarting" : "reloading"} (${pm.kind})...`,
|
|
576
|
+
}));
|
|
574
577
|
scheduleGracefulRestart(500);
|
|
575
578
|
return true;
|
|
576
579
|
}
|
|
@@ -580,31 +583,20 @@ export async function handleDoctorAPI(req, res, urlPath, body) {
|
|
|
580
583
|
}
|
|
581
584
|
return true;
|
|
582
585
|
}
|
|
583
|
-
// GET /api/pm2/logs — Get recent
|
|
586
|
+
// GET /api/pm2/logs — Get recent logs via detected manager
|
|
584
587
|
if (urlPath === "/api/pm2/logs") {
|
|
585
588
|
try {
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
const procs = JSON.parse(jlist);
|
|
591
|
-
const found = procs.find((p) => p.name === "alvin-bot" || p.name === "alvin-bot");
|
|
592
|
-
if (found)
|
|
593
|
-
processName = found.name;
|
|
594
|
-
}
|
|
595
|
-
catch { /* use default */ }
|
|
596
|
-
let logs = execSync(`pm2 logs ${processName} --nostream --lines 30 2>&1`, {
|
|
597
|
-
encoding: "utf-8",
|
|
598
|
-
timeout: 5000,
|
|
599
|
-
stdio: "pipe",
|
|
600
|
-
env: { ...process.env, FORCE_COLOR: "0", NO_COLOR: "1" },
|
|
601
|
-
});
|
|
602
|
-
// Strip ANSI escape codes
|
|
603
|
-
logs = logs.replace(/\x1b\[[0-9;]*m/g, "");
|
|
604
|
-
res.end(JSON.stringify({ logs }));
|
|
589
|
+
const { detectProcessManager } = await import("../services/process-manager.js");
|
|
590
|
+
const pm = detectProcessManager();
|
|
591
|
+
const logs = await pm.getLogs(30);
|
|
592
|
+
res.end(JSON.stringify({ logs, kind: pm.kind }));
|
|
605
593
|
}
|
|
606
594
|
catch (err) {
|
|
607
|
-
res.end(JSON.stringify({
|
|
595
|
+
res.end(JSON.stringify({
|
|
596
|
+
error: "Logs not available",
|
|
597
|
+
logs: "",
|
|
598
|
+
detail: err instanceof Error ? err.message : String(err),
|
|
599
|
+
}));
|
|
608
600
|
}
|
|
609
601
|
return true;
|
|
610
602
|
}
|
package/dist/web/setup-api.js
CHANGED
|
@@ -874,6 +874,58 @@ export async function handleSetupAPI(req, res, urlPath, body) {
|
|
|
874
874
|
}
|
|
875
875
|
return true;
|
|
876
876
|
}
|
|
877
|
+
if (platformId === "slack") {
|
|
878
|
+
// v4.13.1 — Validate Slack config via auth.test (Bot Token) +
|
|
879
|
+
// format check on App Token (xapp-). We can't actually "ping"
|
|
880
|
+
// Socket Mode without opening a WebSocket, so we rely on the
|
|
881
|
+
// App Token prefix as the cheapest sanity check.
|
|
882
|
+
const botToken = process.env.SLACK_BOT_TOKEN;
|
|
883
|
+
const appToken = process.env.SLACK_APP_TOKEN;
|
|
884
|
+
if (!botToken) {
|
|
885
|
+
res.end(JSON.stringify({ ok: false, error: "SLACK_BOT_TOKEN not set" }));
|
|
886
|
+
return true;
|
|
887
|
+
}
|
|
888
|
+
try {
|
|
889
|
+
const apiRes = await fetch("https://slack.com/api/auth.test", {
|
|
890
|
+
method: "POST",
|
|
891
|
+
headers: {
|
|
892
|
+
"Authorization": `Bearer ${botToken}`,
|
|
893
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
894
|
+
},
|
|
895
|
+
});
|
|
896
|
+
const data = await apiRes.json();
|
|
897
|
+
if (!data.ok) {
|
|
898
|
+
res.end(JSON.stringify({
|
|
899
|
+
ok: false,
|
|
900
|
+
error: `Slack rejected Bot Token: ${data.error || "unknown error"}`,
|
|
901
|
+
}));
|
|
902
|
+
return true;
|
|
903
|
+
}
|
|
904
|
+
// Bot Token valid. Now check App Token format — Socket Mode
|
|
905
|
+
// requires it and the xapp- prefix is the quickest sanity check.
|
|
906
|
+
let warning = "";
|
|
907
|
+
if (!appToken) {
|
|
908
|
+
warning = " ⚠️ SLACK_APP_TOKEN not set — Socket Mode will fail.";
|
|
909
|
+
}
|
|
910
|
+
else if (!appToken.startsWith("xapp-")) {
|
|
911
|
+
warning =
|
|
912
|
+
" ⚠️ SLACK_APP_TOKEN has wrong prefix (expected xapp-) — Socket Mode will fail.";
|
|
913
|
+
}
|
|
914
|
+
res.end(JSON.stringify({
|
|
915
|
+
ok: true,
|
|
916
|
+
info: `@${data.user} on ${data.team} (team_id: ${data.team_id}, bot_id: ${data.bot_id})` +
|
|
917
|
+
warning,
|
|
918
|
+
}));
|
|
919
|
+
}
|
|
920
|
+
catch (err) {
|
|
921
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
922
|
+
res.end(JSON.stringify({
|
|
923
|
+
ok: false,
|
|
924
|
+
error: `Failed to reach slack.com/api/auth.test: ${msg}`,
|
|
925
|
+
}));
|
|
926
|
+
}
|
|
927
|
+
return true;
|
|
928
|
+
}
|
|
877
929
|
res.end(JSON.stringify({ ok: false, error: "Unknown platform" }));
|
|
878
930
|
}
|
|
879
931
|
catch (err) {
|
package/package.json
CHANGED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* v4.13.1 — process-manager abstraction tests.
|
|
3
|
+
*
|
|
4
|
+
* The maintenance section in the Web UI used to hard-wire PM2 commands
|
|
5
|
+
* (`pm2 jlist`, `pm2 restart`, `pm2 stop`, `pm2 logs ...`). Since v4.8
|
|
6
|
+
* the Mac install uses launchd (`com.alvinbot.app.plist`) — PM2 isn't
|
|
7
|
+
* running, so those calls returned "PM2 not available" and the buttons
|
|
8
|
+
* did nothing.
|
|
9
|
+
*
|
|
10
|
+
* This module abstracts the process manager and auto-detects which one
|
|
11
|
+
* is actually managing the bot. Detection order:
|
|
12
|
+
*
|
|
13
|
+
* 1. launchd (macOS) — if `launchctl print gui/$UID/com.alvinbot.app`
|
|
14
|
+
* succeeds AND the bot's actual running pid matches
|
|
15
|
+
* 2. PM2 — if `pm2 jlist` returns our process
|
|
16
|
+
* 3. standalone — neither detected; only the in-process graceful
|
|
17
|
+
* restart works (scheduleGracefulRestart — since there's no
|
|
18
|
+
* supervisor to bring it back, "stop" is effectively "kill")
|
|
19
|
+
*
|
|
20
|
+
* Each manager implements: getStatus(), stop(), start(), getLogs().
|
|
21
|
+
* Restart is intentionally NOT on the manager — it always routes through
|
|
22
|
+
* scheduleGracefulRestart() (Grammy-safe) and the supervisor auto-brings-
|
|
23
|
+
* back behaviour.
|
|
24
|
+
*/
|
|
25
|
+
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
|
|
26
|
+
|
|
27
|
+
interface ExecCall {
|
|
28
|
+
cmd: string;
|
|
29
|
+
opts?: unknown;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let execLog: ExecCall[] = [];
|
|
33
|
+
let execReturn: Record<string, string | Error> = {};
|
|
34
|
+
|
|
35
|
+
function stubExec() {
|
|
36
|
+
vi.doMock("node:child_process", () => ({
|
|
37
|
+
execSync: (cmd: string, opts?: unknown) => {
|
|
38
|
+
execLog.push({ cmd, opts });
|
|
39
|
+
// Find match by pattern — longest matching prefix wins
|
|
40
|
+
const matches = Object.keys(execReturn).filter((k) => cmd.includes(k));
|
|
41
|
+
matches.sort((a, b) => b.length - a.length);
|
|
42
|
+
const key = matches[0];
|
|
43
|
+
if (key) {
|
|
44
|
+
const v = execReturn[key];
|
|
45
|
+
if (v instanceof Error) throw v;
|
|
46
|
+
return v;
|
|
47
|
+
}
|
|
48
|
+
throw new Error(`execSync: no stub for ${cmd}`);
|
|
49
|
+
},
|
|
50
|
+
}));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
beforeEach(() => {
|
|
54
|
+
execLog = [];
|
|
55
|
+
execReturn = {};
|
|
56
|
+
vi.resetModules();
|
|
57
|
+
stubExec();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
afterEach(() => {
|
|
61
|
+
vi.doUnmock("node:child_process");
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
describe("detectProcessManager (v4.13.1)", () => {
|
|
65
|
+
it("detects 'launchd' when launchctl print succeeds on darwin", async () => {
|
|
66
|
+
execReturn["launchctl print"] = `gui/502/com.alvinbot.app = {
|
|
67
|
+
state = running
|
|
68
|
+
program = /opt/homebrew/bin/node
|
|
69
|
+
}`;
|
|
70
|
+
const mod = await import("../src/services/process-manager.js");
|
|
71
|
+
const pm = mod.detectProcessManager({ platform: "darwin" });
|
|
72
|
+
expect(pm.kind).toBe("launchd");
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("falls through to 'pm2' when launchd is not detected", async () => {
|
|
76
|
+
execReturn["launchctl print"] = new Error("Could not find service");
|
|
77
|
+
execReturn["pm2 jlist"] = JSON.stringify([
|
|
78
|
+
{ name: "alvin-bot", pid: 1234, pm2_env: { status: "online" } },
|
|
79
|
+
]);
|
|
80
|
+
const mod = await import("../src/services/process-manager.js");
|
|
81
|
+
const pm = mod.detectProcessManager({ platform: "linux" });
|
|
82
|
+
expect(pm.kind).toBe("pm2");
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("falls through to 'standalone' when neither is detected", async () => {
|
|
86
|
+
execReturn["launchctl print"] = new Error("not found");
|
|
87
|
+
execReturn["pm2 jlist"] = new Error("command not found");
|
|
88
|
+
const mod = await import("../src/services/process-manager.js");
|
|
89
|
+
const pm = mod.detectProcessManager({ platform: "linux" });
|
|
90
|
+
expect(pm.kind).toBe("standalone");
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("skips launchd detection on non-darwin platforms", async () => {
|
|
94
|
+
// No launchctl command should be issued on Linux
|
|
95
|
+
execReturn["pm2 jlist"] = JSON.stringify([
|
|
96
|
+
{ name: "alvin-bot", pid: 1234, pm2_env: { status: "online" } },
|
|
97
|
+
]);
|
|
98
|
+
const mod = await import("../src/services/process-manager.js");
|
|
99
|
+
const pm = mod.detectProcessManager({ platform: "linux" });
|
|
100
|
+
expect(pm.kind).toBe("pm2");
|
|
101
|
+
// Verify launchctl was NOT called
|
|
102
|
+
expect(execLog.some((e) => e.cmd.includes("launchctl"))).toBe(false);
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
describe("launchd process manager (v4.13.1)", () => {
|
|
107
|
+
it("getStatus parses launchctl print output for state + PID", async () => {
|
|
108
|
+
execReturn["launchctl print"] = `gui/502/com.alvinbot.app = {
|
|
109
|
+
active count = 1
|
|
110
|
+
state = running
|
|
111
|
+
program = /opt/homebrew/Cellar/node/25.9.0_1/bin/node
|
|
112
|
+
pid = 65432
|
|
113
|
+
program path = /usr/bin/node
|
|
114
|
+
working directory = /Users/alvin_de/Projects/alvin-bot
|
|
115
|
+
stdout path = /Users/alvin_de/.alvin-bot/logs/alvin-bot.out.log
|
|
116
|
+
}`;
|
|
117
|
+
const mod = await import("../src/services/process-manager.js");
|
|
118
|
+
const pm = mod.createLaunchdManager(502);
|
|
119
|
+
const status = await pm.getStatus();
|
|
120
|
+
expect(status.status).toBe("running");
|
|
121
|
+
expect(status.pid).toBe(65432);
|
|
122
|
+
expect(status.kind).toBe("launchd");
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("getStatus returns 'not-loaded' when service is not registered", async () => {
|
|
126
|
+
execReturn["launchctl print"] = new Error("Could not find service");
|
|
127
|
+
const mod = await import("../src/services/process-manager.js");
|
|
128
|
+
const pm = mod.createLaunchdManager(502);
|
|
129
|
+
const status = await pm.getStatus();
|
|
130
|
+
expect(status.status).toBe("not-loaded");
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("stop uses launchctl bootout", async () => {
|
|
134
|
+
execReturn["launchctl bootout"] = "";
|
|
135
|
+
const mod = await import("../src/services/process-manager.js");
|
|
136
|
+
const pm = mod.createLaunchdManager(502);
|
|
137
|
+
await pm.stop();
|
|
138
|
+
const stopCall = execLog.find((e) => e.cmd.includes("bootout"));
|
|
139
|
+
expect(stopCall).toBeDefined();
|
|
140
|
+
expect(stopCall!.cmd).toContain("gui/502/com.alvinbot.app");
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("start uses launchctl bootstrap", async () => {
|
|
144
|
+
execReturn["launchctl bootstrap"] = "";
|
|
145
|
+
const mod = await import("../src/services/process-manager.js");
|
|
146
|
+
const pm = mod.createLaunchdManager(502);
|
|
147
|
+
await pm.start();
|
|
148
|
+
const startCall = execLog.find((e) => e.cmd.includes("bootstrap"));
|
|
149
|
+
expect(startCall).toBeDefined();
|
|
150
|
+
expect(startCall!.cmd).toMatch(/com\.alvinbot\.app\.plist/);
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
describe("pm2 process manager (v4.13.1)", () => {
|
|
155
|
+
it("getStatus parses pm2 jlist for our process", async () => {
|
|
156
|
+
execReturn["pm2 jlist"] = JSON.stringify([
|
|
157
|
+
{
|
|
158
|
+
name: "alvin-bot",
|
|
159
|
+
pid: 9999,
|
|
160
|
+
pm2_env: {
|
|
161
|
+
status: "online",
|
|
162
|
+
pm_uptime: Date.now() - 60_000,
|
|
163
|
+
restart_time: 2,
|
|
164
|
+
},
|
|
165
|
+
monit: { memory: 123456, cpu: 1.5 },
|
|
166
|
+
},
|
|
167
|
+
]);
|
|
168
|
+
const mod = await import("../src/services/process-manager.js");
|
|
169
|
+
const pm = mod.createPm2Manager();
|
|
170
|
+
const status = await pm.getStatus();
|
|
171
|
+
expect(status.status).toBe("online");
|
|
172
|
+
expect(status.pid).toBe(9999);
|
|
173
|
+
expect(status.kind).toBe("pm2");
|
|
174
|
+
expect(status.restarts).toBe(2);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("getStatus returns 'unknown' if pm2 jlist does not include our process", async () => {
|
|
178
|
+
execReturn["pm2 jlist"] = JSON.stringify([
|
|
179
|
+
{ name: "other-service", pid: 1111, pm2_env: { status: "online" } },
|
|
180
|
+
]);
|
|
181
|
+
const mod = await import("../src/services/process-manager.js");
|
|
182
|
+
const pm = mod.createPm2Manager();
|
|
183
|
+
const status = await pm.getStatus();
|
|
184
|
+
expect(status.status).toBe("unknown");
|
|
185
|
+
});
|
|
186
|
+
});
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* v4.13.1 — `/api/platforms/test-connection` must accept `slack` as a
|
|
3
|
+
* platformId and validate the Bot Token via Slack's auth.test endpoint.
|
|
4
|
+
*
|
|
5
|
+
* Before v4.13.1, the handler only knew about telegram/discord/signal/
|
|
6
|
+
* whatsapp, so slack fell through to "Unknown platform" even when a
|
|
7
|
+
* valid xoxb- Bot Token was set.
|
|
8
|
+
*
|
|
9
|
+
* These tests hit the handler directly (no HTTP server spin-up) and stub
|
|
10
|
+
* global fetch so the Slack API is never actually contacted.
|
|
11
|
+
*/
|
|
12
|
+
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
|
|
13
|
+
import { EventEmitter } from "node:events";
|
|
14
|
+
import { Writable } from "node:stream";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Minimal request/response pair that the setup-api handler expects.
|
|
18
|
+
* We capture the body written via res.end(body) so the test can assert
|
|
19
|
+
* on the JSON payload.
|
|
20
|
+
*/
|
|
21
|
+
interface FakeIO {
|
|
22
|
+
req: EventEmitter & { method: string; url: string; headers: Record<string, string> };
|
|
23
|
+
res: Writable & { statusCode: number; headers: Record<string, string>; body: string };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function makeIO(method: string, url: string, body: string): FakeIO {
|
|
27
|
+
const req = new EventEmitter() as FakeIO["req"];
|
|
28
|
+
req.method = method;
|
|
29
|
+
req.url = url;
|
|
30
|
+
req.headers = {};
|
|
31
|
+
|
|
32
|
+
let captured = "";
|
|
33
|
+
const res = new Writable({
|
|
34
|
+
write(chunk, _enc, cb) {
|
|
35
|
+
captured += chunk.toString();
|
|
36
|
+
cb();
|
|
37
|
+
},
|
|
38
|
+
}) as FakeIO["res"];
|
|
39
|
+
res.statusCode = 200;
|
|
40
|
+
res.headers = {};
|
|
41
|
+
res.setHeader = (k: string, v: string) => {
|
|
42
|
+
res.headers[k.toLowerCase()] = v;
|
|
43
|
+
return res as any;
|
|
44
|
+
};
|
|
45
|
+
res.end = (b?: unknown) => {
|
|
46
|
+
if (b != null) captured += String(b);
|
|
47
|
+
res.body = captured;
|
|
48
|
+
return res as any;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
return { req, res };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
beforeEach(() => {
|
|
55
|
+
vi.resetModules();
|
|
56
|
+
// Prevent the setup-api module from crashing on BOT_ROOT etc.
|
|
57
|
+
process.env.BOT_TOKEN = "";
|
|
58
|
+
process.env.SLACK_BOT_TOKEN = "";
|
|
59
|
+
process.env.SLACK_APP_TOKEN = "";
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
afterEach(() => {
|
|
63
|
+
vi.unstubAllGlobals();
|
|
64
|
+
delete process.env.BOT_TOKEN;
|
|
65
|
+
delete process.env.SLACK_BOT_TOKEN;
|
|
66
|
+
delete process.env.SLACK_APP_TOKEN;
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
describe("POST /api/platforms/test-connection — slack (v4.13.1)", () => {
|
|
70
|
+
it("returns {ok:false, error: 'SLACK_BOT_TOKEN not set'} when no tokens configured", async () => {
|
|
71
|
+
const { handleSetupAPI } = await import("../src/web/setup-api.js");
|
|
72
|
+
const { req, res } = makeIO("POST", "/api/platforms/test-connection", "");
|
|
73
|
+
const body = JSON.stringify({ platformId: "slack" });
|
|
74
|
+
|
|
75
|
+
const handled = await handleSetupAPI(req as any, res as any, "/api/platforms/test-connection", body);
|
|
76
|
+
expect(handled).toBe(true);
|
|
77
|
+
const parsed = JSON.parse(res.body);
|
|
78
|
+
expect(parsed.ok).toBe(false);
|
|
79
|
+
expect(parsed.error).toMatch(/SLACK_BOT_TOKEN/);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("returns {ok:true, info: '...'} when Slack's auth.test accepts the token", async () => {
|
|
83
|
+
process.env.SLACK_BOT_TOKEN = "xoxb-fake-valid";
|
|
84
|
+
process.env.SLACK_APP_TOKEN = "xapp-fake-valid";
|
|
85
|
+
|
|
86
|
+
vi.stubGlobal(
|
|
87
|
+
"fetch",
|
|
88
|
+
vi.fn(async (url: string) => {
|
|
89
|
+
expect(url).toContain("slack.com/api/auth.test");
|
|
90
|
+
return {
|
|
91
|
+
ok: true,
|
|
92
|
+
json: async () => ({
|
|
93
|
+
ok: true,
|
|
94
|
+
url: "https://alev-b.slack.com/",
|
|
95
|
+
team: "Alev-B Workspace",
|
|
96
|
+
user: "alvinbot",
|
|
97
|
+
team_id: "T123",
|
|
98
|
+
user_id: "U456",
|
|
99
|
+
bot_id: "B789",
|
|
100
|
+
}),
|
|
101
|
+
};
|
|
102
|
+
}),
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
const { handleSetupAPI } = await import("../src/web/setup-api.js");
|
|
106
|
+
const { req, res } = makeIO("POST", "/api/platforms/test-connection", "");
|
|
107
|
+
const body = JSON.stringify({ platformId: "slack" });
|
|
108
|
+
await handleSetupAPI(req as any, res as any, "/api/platforms/test-connection", body);
|
|
109
|
+
|
|
110
|
+
const parsed = JSON.parse(res.body);
|
|
111
|
+
expect(parsed.ok).toBe(true);
|
|
112
|
+
expect(parsed.info).toMatch(/alvinbot|Alev-B/i);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("returns {ok:false} when Slack's auth.test rejects the token", async () => {
|
|
116
|
+
process.env.SLACK_BOT_TOKEN = "xoxb-fake-invalid";
|
|
117
|
+
process.env.SLACK_APP_TOKEN = "xapp-fake-invalid";
|
|
118
|
+
|
|
119
|
+
vi.stubGlobal(
|
|
120
|
+
"fetch",
|
|
121
|
+
vi.fn(async () => ({
|
|
122
|
+
ok: true,
|
|
123
|
+
json: async () => ({ ok: false, error: "invalid_auth" }),
|
|
124
|
+
})),
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
const { handleSetupAPI } = await import("../src/web/setup-api.js");
|
|
128
|
+
const { req, res } = makeIO("POST", "/api/platforms/test-connection", "");
|
|
129
|
+
const body = JSON.stringify({ platformId: "slack" });
|
|
130
|
+
await handleSetupAPI(req as any, res as any, "/api/platforms/test-connection", body);
|
|
131
|
+
|
|
132
|
+
const parsed = JSON.parse(res.body);
|
|
133
|
+
expect(parsed.ok).toBe(false);
|
|
134
|
+
expect(parsed.error).toMatch(/invalid_auth/);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("warns about missing/invalid App Token format when Bot Token is OK", async () => {
|
|
138
|
+
process.env.SLACK_BOT_TOKEN = "xoxb-fake-valid";
|
|
139
|
+
process.env.SLACK_APP_TOKEN = "xoxb-not-an-app-token"; // wrong prefix
|
|
140
|
+
|
|
141
|
+
vi.stubGlobal(
|
|
142
|
+
"fetch",
|
|
143
|
+
vi.fn(async () => ({
|
|
144
|
+
ok: true,
|
|
145
|
+
json: async () => ({
|
|
146
|
+
ok: true,
|
|
147
|
+
user: "alvinbot",
|
|
148
|
+
team: "x",
|
|
149
|
+
team_id: "T1",
|
|
150
|
+
user_id: "U1",
|
|
151
|
+
bot_id: "B1",
|
|
152
|
+
}),
|
|
153
|
+
})),
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
const { handleSetupAPI } = await import("../src/web/setup-api.js");
|
|
157
|
+
const { req, res } = makeIO("POST", "/api/platforms/test-connection", "");
|
|
158
|
+
const body = JSON.stringify({ platformId: "slack" });
|
|
159
|
+
await handleSetupAPI(req as any, res as any, "/api/platforms/test-connection", body);
|
|
160
|
+
|
|
161
|
+
const parsed = JSON.parse(res.body);
|
|
162
|
+
// Bot Token was valid, but we should still note the App Token format issue
|
|
163
|
+
expect(parsed.ok).toBe(true);
|
|
164
|
+
expect(parsed.info).toMatch(/App.?Token|xapp-/i);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("still rejects 'slack-workspace' or other typos as unknown (regression guard)", async () => {
|
|
168
|
+
const { handleSetupAPI } = await import("../src/web/setup-api.js");
|
|
169
|
+
const { req, res } = makeIO("POST", "/api/platforms/test-connection", "");
|
|
170
|
+
const body = JSON.stringify({ platformId: "slack-workspace" });
|
|
171
|
+
await handleSetupAPI(req as any, res as any, "/api/platforms/test-connection", body);
|
|
172
|
+
const parsed = JSON.parse(res.body);
|
|
173
|
+
expect(parsed.ok).toBe(false);
|
|
174
|
+
expect(parsed.error).toMatch(/Unknown platform/);
|
|
175
|
+
});
|
|
176
|
+
});
|