mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-11-17 03:39:33 +01:00
152 lines
6.6 KiB
TypeScript
152 lines
6.6 KiB
TypeScript
// @vitest-environment node
|
|
/**
|
|
* @fileoverview The glue between WebServer and the Discord bridge: alerts reach it even with no
|
|
* Web Push subscription (with the rendered screen as the tail), and tab events reach it routed by
|
|
* kind. The bridge and routes are tested on their own; this pins the lines that make the feature
|
|
* exist. A real WebServer, never started, with the bridge replaced by a recorder.
|
|
*
|
|
* Port: N/A (no server start).
|
|
*/
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
const { sendNotification } = vi.hoisted(() => ({ sendNotification: vi.fn(async () => undefined) }));
|
|
vi.mock('web-push', () => ({
|
|
default: { sendNotification, setVapidDetails: vi.fn(), generateVAPIDKeys: vi.fn() },
|
|
}));
|
|
|
|
import { WebServer } from '../src/web/server.js';
|
|
import { SseEvent } from '../src/web/sse-events.js';
|
|
|
|
type Internals = {
|
|
pushStore: { getAll: () => never[]; dispose: () => void };
|
|
discordBridge: Record<string, ReturnType<typeof vi.fn>>;
|
|
sessions: Map<string, unknown>;
|
|
mux: { capturePaneText?: (name: string) => string | null };
|
|
sendPushNotifications: (event: string, data: Record<string, unknown>) => Promise<void>;
|
|
broadcast: (event: string, data: unknown) => void;
|
|
cleanupSession: (id: string, killMux?: boolean, reason?: string) => Promise<void>;
|
|
};
|
|
|
|
function server(): Internals {
|
|
const s = new WebServer(0, false, true, '127.0.0.1', 'box') as unknown as Internals;
|
|
s.pushStore = { getAll: () => [], dispose: () => undefined };
|
|
s.discordBridge = {
|
|
notify: vi.fn(async () => null),
|
|
sessionCreated: vi.fn(async () => undefined),
|
|
sessionRenamed: vi.fn(async () => undefined),
|
|
sessionDeleted: vi.fn(async () => undefined),
|
|
sessionIdle: vi.fn(async () => undefined),
|
|
stop: vi.fn(),
|
|
};
|
|
return s;
|
|
}
|
|
|
|
beforeEach(() => sendNotification.mockClear());
|
|
|
|
describe('sendPushNotifications -> Discord', () => {
|
|
it('posts a permission prompt with zero push subscriptions, with the rendered screen as the tail', async () => {
|
|
const s = server();
|
|
s.sessions.set('s1', { muxName: 'codeman-s1', textOutput: '', mode: 'claude' });
|
|
const capture = vi.fn(() => 'Which option?\n──────────\n 1. Cap GPUs\n\n 2. Cap CPU\n');
|
|
s.mux = { capturePaneText: capture };
|
|
await s.sendPushNotifications('hook:permission_prompt', {
|
|
sessionId: 's1',
|
|
sessionName: 'w1-app',
|
|
tool_name: 'Bash',
|
|
});
|
|
expect(s.discordBridge.notify).toHaveBeenCalledTimes(1);
|
|
const msg = s.discordBridge.notify.mock.calls[0][0];
|
|
expect(msg).toMatchObject({
|
|
event: 'hook:permission_prompt',
|
|
title: 'Permission Required',
|
|
body: '[w1-app] Tool: Bash',
|
|
urgency: 'critical',
|
|
sessionId: 's1',
|
|
sessionName: 'w1-app',
|
|
});
|
|
// A function, so the screen is read (a synchronous tmux exec) only once the bridge will post.
|
|
expect(typeof msg.tail).toBe('function');
|
|
expect(capture).not.toHaveBeenCalled();
|
|
expect(msg.tail()).toBe('Which option?\n 1. Cap GPUs\n 2. Cap CPU');
|
|
expect(capture).toHaveBeenCalledTimes(1);
|
|
expect(sendNotification).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('falls back to plain text output when there is no tmux pane, and to no tail for an unknown session', async () => {
|
|
const s = server();
|
|
s.sessions.set('s2', { muxName: null, textOutput: 'plain answer\n', mode: 'claude' });
|
|
await s.sendPushNotifications('hook:idle_prompt', { sessionId: 's2', sessionName: 'w2' });
|
|
expect(s.discordBridge.notify.mock.calls[0][0].tail()).toBe('plain answer');
|
|
await s.sendPushNotifications('hook:idle_prompt', { sessionId: 'gone', sessionName: 'w3' });
|
|
expect(s.discordBridge.notify.mock.calls[1][0].tail).toBeUndefined();
|
|
});
|
|
|
|
it('sends nothing for an event Web Push does not carry', async () => {
|
|
const s = server();
|
|
await s.sendPushNotifications('session:created', { sessionId: 's1', sessionName: 'w1' });
|
|
expect(s.discordBridge.notify).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe('broadcast -> Discord tab lifecycle', () => {
|
|
it('routes created, updated and deleted by kind, reading both payload shapes', () => {
|
|
const s = server();
|
|
s.broadcast(SseEvent.SessionCreated, { session: { id: 's1', name: 'w1', createdAt: 123 } });
|
|
expect(s.discordBridge.sessionCreated).toHaveBeenCalledWith('s1', 'w1', 123, false);
|
|
s.broadcast(SseEvent.SessionUpdated, { id: 's1', name: 'renamed' });
|
|
expect(s.discordBridge.sessionRenamed).toHaveBeenCalledWith('s1', 'renamed');
|
|
});
|
|
|
|
it('does not touch the thread on session:deleted, which a detach also fires', () => {
|
|
const s = server();
|
|
s.broadcast(SseEvent.SessionDeleted, { id: 's1' });
|
|
expect(s.discordBridge.sessionDeleted).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('tells the bridge about a real delete (killMux true) but not a detach (killMux false)', async () => {
|
|
const s = server();
|
|
await s.cleanupSession('gone', false);
|
|
expect(s.discordBridge.sessionDeleted).not.toHaveBeenCalled();
|
|
await s.cleanupSession('gone', true);
|
|
expect(s.discordBridge.sessionDeleted).toHaveBeenCalledWith('gone');
|
|
});
|
|
|
|
it('marks a resumed tab as resumed, so it is never sent the typed hint', () => {
|
|
const s = server();
|
|
s.broadcast(SseEvent.SessionCreated, { session: { id: 's9', name: 'w9', createdAt: 5, resumeSessionId: 'abc' } });
|
|
expect(s.discordBridge.sessionCreated).toHaveBeenCalledWith('s9', 'w9', 5, true);
|
|
s.broadcast(SseEvent.SessionCreated, { session: { id: 's8', name: 'w8', createdAt: 6 } });
|
|
expect(s.discordBridge.sessionCreated).toHaveBeenLastCalledWith('s8', 'w8', 6, false);
|
|
});
|
|
|
|
it('ignores an event with no session id', () => {
|
|
const s = server();
|
|
s.broadcast(SseEvent.SessionUpdated, {});
|
|
expect(s.discordBridge.sessionRenamed).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('allows the typed hint only for a local external CLI: not Claude, a shell, a remote or a Docker tab', () => {
|
|
const s = server();
|
|
s.sessions.set('cx', { mode: 'codex' });
|
|
s.sessions.set('cl', { mode: 'claude' });
|
|
s.sessions.set('sh', { mode: 'shell' });
|
|
s.sessions.set('rm', { mode: 'codex', remote: { label: 'box' } });
|
|
s.sessions.set('dk', { mode: 'codex', docker: { image: 'x' } });
|
|
for (const id of ['cx', 'cl', 'sh', 'rm', 'dk', 'missing']) s.broadcast(SseEvent.SessionIdle, { id });
|
|
expect(s.discordBridge.sessionIdle.mock.calls).toEqual([
|
|
['cx', true],
|
|
['cl', false],
|
|
['sh', false],
|
|
['rm', false],
|
|
['dk', false],
|
|
['missing', false],
|
|
]);
|
|
});
|
|
|
|
it('closes the bridge when the server stops', async () => {
|
|
const s = server();
|
|
await (s as unknown as { stop: () => Promise<void> }).stop();
|
|
expect(s.discordBridge.stop).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|