* refactor: implement channel architecture and dynamic setup - Introduced ChannelRegistry for dynamic channel loading - Decoupled WhatsApp from core index.ts and config.ts - Updated setup wizard to support ENABLED_CHANNELS selection - Refactored IPC and group registration to be channel-aware - Verified with 359 passing tests and clean typecheck * style: fix formatting in config.ts to pass CI * refactor(setup): full platform-agnostic transformation - Harmonized all instructional text and help prompts - Implemented conditional guards for WhatsApp-specific steps - Normalized CLI terminology across all 4 initial channels - Unified troubleshooting and verification logic - Verified 369 tests pass with clean typecheck * feat(skills): transform WhatsApp into a pluggable skill - Created .claude/skills/add-whatsapp with full 5-phase interactive setup - Fixed TS7006 'implicit any' error in IpcDeps - Added auto-creation of STORE_DIR to prevent crashes on fresh installs - Verified with 369 passing tests and clean typecheck * refactor(skills): move WhatsApp from core to pluggable skill - Move src/channels/whatsapp.ts to add-whatsapp skill add/ folder - Move src/channels/whatsapp.test.ts to skill add/ folder - Move src/whatsapp-auth.ts to skill add/ folder - Create modify/ for barrel file (src/channels/index.ts) - Create tests/ with skill package validation test - Update manifest with adds/modifies lists - Remove WhatsApp deps from core package.json (now skill-managed) - Remove WhatsApp-specific ghost language from types.ts - Update SKILL.md to reflect skill-apply workflow Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * refactor(skills): move setup/whatsapp-auth.ts into WhatsApp skill The WhatsApp auth setup step is channel-specific — move it from core to the add-whatsapp skill so core stays minimal. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * refactor(skills): convert Telegram skill to pluggable channel pattern Replace the old direct-integration approach (modifying src/index.ts, src/config.ts, src/routing.test.ts) with self-registration via the channel registry, matching the WhatsApp skill pattern. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix(skills): fix add-whatsapp build failure and improve auth flow - Add missing @types/qrcode-terminal to manifest npm_dependencies (build failed after skill apply without it) - Make QR-browser the recommended auth method (terminal QR too small, pairing codes expire too fast) - Remove "replace vs alongside" question — channels are additive - Add pairing code retry guidance and QR-browser fallback Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: remove hardcoded WhatsApp default and stale Baileys comment - ENABLED_CHANNELS now defaults to empty (fresh installs must configure channels explicitly via /setup; existing installs already have .env) - Remove Baileys-specific comment from storeMessageDirect() in db.ts Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * refactor(skills): convert Discord, Slack, Gmail skills to pluggable channel pattern All channel skills now use the same self-registration pattern: - registerChannel() factory at module load time - Barrel file append (src/channels/index.ts) instead of orchestrator modifications - No more *_ONLY flags (DISCORD_ONLY, SLACK_ONLY) — use ENABLED_CHANNELS instead - Removed ~2500 lines of old modify/ files (src/index.ts, src/config.ts, src/routing.test.ts) Gmail retains its container-runner.ts and agent-runner modifications (MCP mount + server config) since those are independent of channel wiring. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * refactor: use getRegisteredChannels instead of ENABLED_CHANNELS Remove the ENABLED_CHANNELS env var entirely. The orchestrator now iterates getRegisteredChannelNames() from the channel registry — channels self-register via barrel imports and their factories return null when credentials are missing, so unconfigured channels are skipped automatically. Deleted setup/channels.ts (and its tests) since its sole purpose was writing ENABLED_CHANNELS to .env. Refactored verify, groups, and environment setup steps to detect channels by credential presence instead of reading ENABLED_CHANNELS. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: add breaking change notice and whatsapp migration instructions CHANGELOG.md documents the pluggable channel architecture shift and provides migration steps for existing WhatsApp users. CLAUDE.md updated: Quick Context reflects multi-channel architecture, Key Files lists registry.ts instead of whatsapp.ts, and a new Troubleshooting section directs users to /add-whatsapp if WhatsApp stops connecting after upgrade. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: rewrite READMEs for pluggable multi-channel architecture Reflects the architectural shift from a hardcoded WhatsApp bot to a pluggable channel platform. Adds upgrading notice, Mermaid architecture diagram, CI/License/TypeScript/PRs badges, and clarifies that slash commands run inside the Claude Code CLI. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: move pluggable channel architecture details to SPEC.md Revert READMEs to original tone with only two targeted changes: - Add upgrading notice for WhatsApp breaking change - Mention pluggable channels in "What It Supports" Move Mermaid diagram, channel registry internals, factory pattern explanation, and self-registration walkthrough into docs/SPEC.md. Update stale WhatsApp-specific references in SPEC.md to be channel-agnostic. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: move upgrading notice to CHANGELOG, add changelog link Remove the "Upgrading from Pre-Pluggable Versions" section from README.md — breaking change details belong in the CHANGELOG. Add a Changelog section linking to CHANGELOG.md. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: expand CHANGELOG with full PR #500 changes Cover all changes: channel registry, WhatsApp moved to skill, removed core dependencies, all 5 skills simplified, orchestrator refactored, setup decoupled. Use Claude Code CLI instructions for migration. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * chore: bump version to 1.2.0 for pluggable channel architecture Minor version bump — new functionality (pluggable channels) with a managed migration path for existing WhatsApp users. Update version references in CHANGELOG and update skill. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * Fix skill application * fix: use slotted barrel file to prevent channel merge conflicts Pre-allocate a named comment slot for each channel in src/channels/index.ts, separated by blank lines. Each skill's modify file only touches its own slot, so three-way merges never conflict when applying multiple channels. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: resolve real chat ID during setup for token-based channels Instead of registering with `pending@telegram` (which never matches incoming messages), the setup skill now runs an inline bot that waits for the user to send /chatid, capturing the real chat ID before registration. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: setup delegates to channel skills, fix group sync and Discord metadata - Restructure setup SKILL.md to delegate channel setup to individual channel skills (/add-whatsapp, /add-telegram, etc.) instead of reimplementing auth/registration inline with broken placeholder JIDs - Move channel selection to step 5 where it's immediately acted on - Fix setup/groups.ts: write sync script to temp file instead of passing via node -e which broke on shell escaping of newlines - Fix Discord onChatMetadata missing channel and isGroup parameters - Add .tmp-* to .gitignore for temp sync script cleanup Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: align add-whatsapp skill with main setup patterns Add headless detection for auth method selection, structured inline error handling, dedicated number DM flow, and reorder questions to match main's trigger-first flow. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: add missing auth script to package.json The add-whatsapp skill adds src/whatsapp-auth.ts but doesn't add the corresponding npm script. Setup and SKILL.md reference `npm run auth` for WhatsApp QR terminal authentication. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: update Discord skill tests to match onChatMetadata signature The onChatMetadata callback now takes 5 arguments (jid, timestamp, name, channel, isGroup) but the Discord skill tests only expected 3. This caused skill application to roll back on test failure. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: replace 'pluggable' jargon with clearer language User-facing text now says "multi-channel" or describes what it does. Developer-facing text uses "self-registering" or "channel registry". Also removes extra badge row from README. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: align Chinese README with English version Remove extra badges, replace pluggable jargon, remove upgrade section (now in CHANGELOG), add missing intro line and changelog section, fix setup FAQ answer. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: warn on installed-but-unconfigured channels instead of silent skip Channels with missing credentials now emit WARN logs naming the exact missing variable, so misconfigurations surface instead of being hidden. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: simplify changelog to one-liner with compare link Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: add isMain flag and channel-prefixed group folders Replace MAIN_GROUP_FOLDER constant with explicit isMain boolean on RegisteredGroup. Group folders now use channel prefix convention (e.g., whatsapp_main, telegram_family-chat) to prevent cross-channel collisions. - Add isMain to RegisteredGroup type and SQLite schema (with migration) - Replace all folder-based main group checks with group.isMain - Add --is-main flag to setup/register.ts - Strip isMain from IPC payload (defense in depth) - Update MCP tool description for channel-prefixed naming - Update all channel SKILL.md files and documentation Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: gavrielc <gabicohen22@yahoo.com> Co-authored-by: Koshkoshinski <daniel.milliner@gmail.com>
852 lines
24 KiB
TypeScript
852 lines
24 KiB
TypeScript
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
|
|
|
|
// --- Mocks ---
|
|
|
|
// Mock registry (registerChannel runs at import time)
|
|
vi.mock('./registry.js', () => ({ registerChannel: vi.fn() }));
|
|
|
|
// Mock config
|
|
vi.mock('../config.js', () => ({
|
|
ASSISTANT_NAME: 'Jonesy',
|
|
TRIGGER_PATTERN: /^@Jonesy\b/i,
|
|
}));
|
|
|
|
// Mock logger
|
|
vi.mock('../logger.js', () => ({
|
|
logger: {
|
|
debug: vi.fn(),
|
|
info: vi.fn(),
|
|
warn: vi.fn(),
|
|
error: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
// Mock db
|
|
vi.mock('../db.js', () => ({
|
|
updateChatName: vi.fn(),
|
|
}));
|
|
|
|
// --- @slack/bolt mock ---
|
|
|
|
type Handler = (...args: any[]) => any;
|
|
|
|
const appRef = vi.hoisted(() => ({ current: null as any }));
|
|
|
|
vi.mock('@slack/bolt', () => ({
|
|
App: class MockApp {
|
|
eventHandlers = new Map<string, Handler>();
|
|
token: string;
|
|
appToken: string;
|
|
|
|
client = {
|
|
auth: {
|
|
test: vi.fn().mockResolvedValue({ user_id: 'U_BOT_123' }),
|
|
},
|
|
chat: {
|
|
postMessage: vi.fn().mockResolvedValue(undefined),
|
|
},
|
|
conversations: {
|
|
list: vi.fn().mockResolvedValue({
|
|
channels: [],
|
|
response_metadata: {},
|
|
}),
|
|
},
|
|
users: {
|
|
info: vi.fn().mockResolvedValue({
|
|
user: { real_name: 'Alice Smith', name: 'alice' },
|
|
}),
|
|
},
|
|
};
|
|
|
|
constructor(opts: any) {
|
|
this.token = opts.token;
|
|
this.appToken = opts.appToken;
|
|
appRef.current = this;
|
|
}
|
|
|
|
event(name: string, handler: Handler) {
|
|
this.eventHandlers.set(name, handler);
|
|
}
|
|
|
|
async start() {}
|
|
async stop() {}
|
|
},
|
|
LogLevel: { ERROR: 'error' },
|
|
}));
|
|
|
|
// Mock env
|
|
vi.mock('../env.js', () => ({
|
|
readEnvFile: vi.fn().mockReturnValue({
|
|
SLACK_BOT_TOKEN: 'xoxb-test-token',
|
|
SLACK_APP_TOKEN: 'xapp-test-token',
|
|
}),
|
|
}));
|
|
|
|
import { SlackChannel, SlackChannelOpts } from './slack.js';
|
|
import { updateChatName } from '../db.js';
|
|
import { readEnvFile } from '../env.js';
|
|
|
|
// --- Test helpers ---
|
|
|
|
function createTestOpts(
|
|
overrides?: Partial<SlackChannelOpts>,
|
|
): SlackChannelOpts {
|
|
return {
|
|
onMessage: vi.fn(),
|
|
onChatMetadata: vi.fn(),
|
|
registeredGroups: vi.fn(() => ({
|
|
'slack:C0123456789': {
|
|
name: 'Test Channel',
|
|
folder: 'test-channel',
|
|
trigger: '@Jonesy',
|
|
added_at: '2024-01-01T00:00:00.000Z',
|
|
},
|
|
})),
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function createMessageEvent(overrides: {
|
|
channel?: string;
|
|
channelType?: string;
|
|
user?: string;
|
|
text?: string;
|
|
ts?: string;
|
|
threadTs?: string;
|
|
subtype?: string;
|
|
botId?: string;
|
|
}) {
|
|
return {
|
|
channel: overrides.channel ?? 'C0123456789',
|
|
channel_type: overrides.channelType ?? 'channel',
|
|
user: overrides.user ?? 'U_USER_456',
|
|
text: 'text' in overrides ? overrides.text : 'Hello everyone',
|
|
ts: overrides.ts ?? '1704067200.000000',
|
|
thread_ts: overrides.threadTs,
|
|
subtype: overrides.subtype,
|
|
bot_id: overrides.botId,
|
|
};
|
|
}
|
|
|
|
function currentApp() {
|
|
return appRef.current;
|
|
}
|
|
|
|
async function triggerMessageEvent(event: ReturnType<typeof createMessageEvent>) {
|
|
const handler = currentApp().eventHandlers.get('message');
|
|
if (handler) await handler({ event });
|
|
}
|
|
|
|
// --- Tests ---
|
|
|
|
describe('SlackChannel', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
// --- Connection lifecycle ---
|
|
|
|
describe('connection lifecycle', () => {
|
|
it('resolves connect() when app starts', async () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
|
|
await channel.connect();
|
|
|
|
expect(channel.isConnected()).toBe(true);
|
|
});
|
|
|
|
it('registers message event handler on construction', () => {
|
|
const opts = createTestOpts();
|
|
new SlackChannel(opts);
|
|
|
|
expect(currentApp().eventHandlers.has('message')).toBe(true);
|
|
});
|
|
|
|
it('gets bot user ID on connect', async () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
|
|
await channel.connect();
|
|
|
|
expect(currentApp().client.auth.test).toHaveBeenCalled();
|
|
});
|
|
|
|
it('disconnects cleanly', async () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
|
|
await channel.connect();
|
|
expect(channel.isConnected()).toBe(true);
|
|
|
|
await channel.disconnect();
|
|
expect(channel.isConnected()).toBe(false);
|
|
});
|
|
|
|
it('isConnected() returns false before connect', () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
|
|
expect(channel.isConnected()).toBe(false);
|
|
});
|
|
});
|
|
|
|
// --- Message handling ---
|
|
|
|
describe('message handling', () => {
|
|
it('delivers message for registered channel', async () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
await channel.connect();
|
|
|
|
const event = createMessageEvent({ text: 'Hello everyone' });
|
|
await triggerMessageEvent(event);
|
|
|
|
expect(opts.onChatMetadata).toHaveBeenCalledWith(
|
|
'slack:C0123456789',
|
|
expect.any(String),
|
|
undefined,
|
|
'slack',
|
|
true,
|
|
);
|
|
expect(opts.onMessage).toHaveBeenCalledWith(
|
|
'slack:C0123456789',
|
|
expect.objectContaining({
|
|
id: '1704067200.000000',
|
|
chat_jid: 'slack:C0123456789',
|
|
sender: 'U_USER_456',
|
|
content: 'Hello everyone',
|
|
is_from_me: false,
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('only emits metadata for unregistered channels', async () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
await channel.connect();
|
|
|
|
const event = createMessageEvent({ channel: 'C9999999999' });
|
|
await triggerMessageEvent(event);
|
|
|
|
expect(opts.onChatMetadata).toHaveBeenCalledWith(
|
|
'slack:C9999999999',
|
|
expect.any(String),
|
|
undefined,
|
|
'slack',
|
|
true,
|
|
);
|
|
expect(opts.onMessage).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('skips non-text subtypes (channel_join, etc.)', async () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
await channel.connect();
|
|
|
|
const event = createMessageEvent({ subtype: 'channel_join' });
|
|
await triggerMessageEvent(event);
|
|
|
|
expect(opts.onMessage).not.toHaveBeenCalled();
|
|
expect(opts.onChatMetadata).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('allows bot_message subtype through', async () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
await channel.connect();
|
|
|
|
const event = createMessageEvent({
|
|
subtype: 'bot_message',
|
|
botId: 'B_OTHER_BOT',
|
|
text: 'Bot message',
|
|
});
|
|
await triggerMessageEvent(event);
|
|
|
|
expect(opts.onChatMetadata).toHaveBeenCalled();
|
|
});
|
|
|
|
it('skips messages with no text', async () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
await channel.connect();
|
|
|
|
const event = createMessageEvent({ text: undefined as any });
|
|
await triggerMessageEvent(event);
|
|
|
|
expect(opts.onMessage).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('detects bot messages by bot_id', async () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
await channel.connect();
|
|
|
|
const event = createMessageEvent({
|
|
subtype: 'bot_message',
|
|
botId: 'B_MY_BOT',
|
|
text: 'Bot response',
|
|
});
|
|
await triggerMessageEvent(event);
|
|
|
|
// Has bot_id so should be marked as bot message
|
|
expect(opts.onMessage).toHaveBeenCalledWith(
|
|
'slack:C0123456789',
|
|
expect.objectContaining({
|
|
is_from_me: true,
|
|
is_bot_message: true,
|
|
sender_name: 'Jonesy',
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('detects bot messages by matching bot user ID', async () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
await channel.connect();
|
|
|
|
const event = createMessageEvent({ user: 'U_BOT_123', text: 'Self message' });
|
|
await triggerMessageEvent(event);
|
|
|
|
expect(opts.onMessage).toHaveBeenCalledWith(
|
|
'slack:C0123456789',
|
|
expect.objectContaining({
|
|
is_from_me: true,
|
|
is_bot_message: true,
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('identifies IM channel type as non-group', async () => {
|
|
const opts = createTestOpts({
|
|
registeredGroups: vi.fn(() => ({
|
|
'slack:D0123456789': {
|
|
name: 'DM',
|
|
folder: 'dm',
|
|
trigger: '@Jonesy',
|
|
added_at: '2024-01-01T00:00:00.000Z',
|
|
},
|
|
})),
|
|
});
|
|
const channel = new SlackChannel(opts);
|
|
await channel.connect();
|
|
|
|
const event = createMessageEvent({
|
|
channel: 'D0123456789',
|
|
channelType: 'im',
|
|
});
|
|
await triggerMessageEvent(event);
|
|
|
|
expect(opts.onChatMetadata).toHaveBeenCalledWith(
|
|
'slack:D0123456789',
|
|
expect.any(String),
|
|
undefined,
|
|
'slack',
|
|
false, // IM is not a group
|
|
);
|
|
});
|
|
|
|
it('converts ts to ISO timestamp', async () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
await channel.connect();
|
|
|
|
const event = createMessageEvent({ ts: '1704067200.000000' });
|
|
await triggerMessageEvent(event);
|
|
|
|
expect(opts.onMessage).toHaveBeenCalledWith(
|
|
'slack:C0123456789',
|
|
expect.objectContaining({
|
|
timestamp: '2024-01-01T00:00:00.000Z',
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('resolves user name from Slack API', async () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
await channel.connect();
|
|
|
|
const event = createMessageEvent({ user: 'U_USER_456', text: 'Hello' });
|
|
await triggerMessageEvent(event);
|
|
|
|
expect(currentApp().client.users.info).toHaveBeenCalledWith({
|
|
user: 'U_USER_456',
|
|
});
|
|
expect(opts.onMessage).toHaveBeenCalledWith(
|
|
'slack:C0123456789',
|
|
expect.objectContaining({
|
|
sender_name: 'Alice Smith',
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('caches user names to avoid repeated API calls', async () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
await channel.connect();
|
|
|
|
// First message — API call
|
|
await triggerMessageEvent(createMessageEvent({ user: 'U_USER_456', text: 'First' }));
|
|
// Second message — should use cache
|
|
await triggerMessageEvent(createMessageEvent({
|
|
user: 'U_USER_456',
|
|
text: 'Second',
|
|
ts: '1704067201.000000',
|
|
}));
|
|
|
|
expect(currentApp().client.users.info).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('falls back to user ID when API fails', async () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
await channel.connect();
|
|
|
|
currentApp().client.users.info.mockRejectedValueOnce(new Error('API error'));
|
|
|
|
const event = createMessageEvent({ user: 'U_UNKNOWN', text: 'Hi' });
|
|
await triggerMessageEvent(event);
|
|
|
|
expect(opts.onMessage).toHaveBeenCalledWith(
|
|
'slack:C0123456789',
|
|
expect.objectContaining({
|
|
sender_name: 'U_UNKNOWN',
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('flattens threaded replies into channel messages', async () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
await channel.connect();
|
|
|
|
const event = createMessageEvent({
|
|
ts: '1704067201.000000',
|
|
threadTs: '1704067200.000000', // parent message ts — this is a reply
|
|
text: 'Thread reply',
|
|
});
|
|
await triggerMessageEvent(event);
|
|
|
|
// Threaded replies are delivered as regular channel messages
|
|
expect(opts.onMessage).toHaveBeenCalledWith(
|
|
'slack:C0123456789',
|
|
expect.objectContaining({
|
|
content: 'Thread reply',
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('delivers thread parent messages normally', async () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
await channel.connect();
|
|
|
|
const event = createMessageEvent({
|
|
ts: '1704067200.000000',
|
|
threadTs: '1704067200.000000', // same as ts — this IS the parent
|
|
text: 'Thread parent',
|
|
});
|
|
await triggerMessageEvent(event);
|
|
|
|
expect(opts.onMessage).toHaveBeenCalledWith(
|
|
'slack:C0123456789',
|
|
expect.objectContaining({
|
|
content: 'Thread parent',
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('delivers messages without thread_ts normally', async () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
await channel.connect();
|
|
|
|
const event = createMessageEvent({ text: 'Normal message' });
|
|
await triggerMessageEvent(event);
|
|
|
|
expect(opts.onMessage).toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
// --- @mention translation ---
|
|
|
|
describe('@mention translation', () => {
|
|
it('prepends trigger when bot is @mentioned via Slack format', async () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
await channel.connect(); // sets botUserId to 'U_BOT_123'
|
|
|
|
const event = createMessageEvent({
|
|
text: 'Hey <@U_BOT_123> what do you think?',
|
|
user: 'U_USER_456',
|
|
});
|
|
await triggerMessageEvent(event);
|
|
|
|
expect(opts.onMessage).toHaveBeenCalledWith(
|
|
'slack:C0123456789',
|
|
expect.objectContaining({
|
|
content: '@Jonesy Hey <@U_BOT_123> what do you think?',
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('does not prepend trigger when trigger pattern already matches', async () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
await channel.connect();
|
|
|
|
const event = createMessageEvent({
|
|
text: '@Jonesy <@U_BOT_123> hello',
|
|
user: 'U_USER_456',
|
|
});
|
|
await triggerMessageEvent(event);
|
|
|
|
// Content should be unchanged since it already matches TRIGGER_PATTERN
|
|
expect(opts.onMessage).toHaveBeenCalledWith(
|
|
'slack:C0123456789',
|
|
expect.objectContaining({
|
|
content: '@Jonesy <@U_BOT_123> hello',
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('does not translate mentions in bot messages', async () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
await channel.connect();
|
|
|
|
const event = createMessageEvent({
|
|
text: 'Echo: <@U_BOT_123>',
|
|
subtype: 'bot_message',
|
|
botId: 'B_MY_BOT',
|
|
});
|
|
await triggerMessageEvent(event);
|
|
|
|
// Bot messages skip mention translation
|
|
expect(opts.onMessage).toHaveBeenCalledWith(
|
|
'slack:C0123456789',
|
|
expect.objectContaining({
|
|
content: 'Echo: <@U_BOT_123>',
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('does not translate mentions for other users', async () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
await channel.connect();
|
|
|
|
const event = createMessageEvent({
|
|
text: 'Hey <@U_OTHER_USER> look at this',
|
|
user: 'U_USER_456',
|
|
});
|
|
await triggerMessageEvent(event);
|
|
|
|
// Mention is for a different user, not the bot
|
|
expect(opts.onMessage).toHaveBeenCalledWith(
|
|
'slack:C0123456789',
|
|
expect.objectContaining({
|
|
content: 'Hey <@U_OTHER_USER> look at this',
|
|
}),
|
|
);
|
|
});
|
|
});
|
|
|
|
// --- sendMessage ---
|
|
|
|
describe('sendMessage', () => {
|
|
it('sends message via Slack client', async () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
await channel.connect();
|
|
|
|
await channel.sendMessage('slack:C0123456789', 'Hello');
|
|
|
|
expect(currentApp().client.chat.postMessage).toHaveBeenCalledWith({
|
|
channel: 'C0123456789',
|
|
text: 'Hello',
|
|
});
|
|
});
|
|
|
|
it('strips slack: prefix from JID', async () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
await channel.connect();
|
|
|
|
await channel.sendMessage('slack:D9876543210', 'DM message');
|
|
|
|
expect(currentApp().client.chat.postMessage).toHaveBeenCalledWith({
|
|
channel: 'D9876543210',
|
|
text: 'DM message',
|
|
});
|
|
});
|
|
|
|
it('queues message when disconnected', async () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
|
|
// Don't connect — should queue
|
|
await channel.sendMessage('slack:C0123456789', 'Queued message');
|
|
|
|
expect(currentApp().client.chat.postMessage).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('queues message on send failure', async () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
await channel.connect();
|
|
|
|
currentApp().client.chat.postMessage.mockRejectedValueOnce(
|
|
new Error('Network error'),
|
|
);
|
|
|
|
// Should not throw
|
|
await expect(
|
|
channel.sendMessage('slack:C0123456789', 'Will fail'),
|
|
).resolves.toBeUndefined();
|
|
});
|
|
|
|
it('splits long messages at 4000 character boundary', async () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
await channel.connect();
|
|
|
|
// Create a message longer than 4000 chars
|
|
const longText = 'A'.repeat(4500);
|
|
await channel.sendMessage('slack:C0123456789', longText);
|
|
|
|
// Should be split into 2 messages: 4000 + 500
|
|
expect(currentApp().client.chat.postMessage).toHaveBeenCalledTimes(2);
|
|
expect(currentApp().client.chat.postMessage).toHaveBeenNthCalledWith(1, {
|
|
channel: 'C0123456789',
|
|
text: 'A'.repeat(4000),
|
|
});
|
|
expect(currentApp().client.chat.postMessage).toHaveBeenNthCalledWith(2, {
|
|
channel: 'C0123456789',
|
|
text: 'A'.repeat(500),
|
|
});
|
|
});
|
|
|
|
it('sends exactly-4000-char messages as a single message', async () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
await channel.connect();
|
|
|
|
const text = 'B'.repeat(4000);
|
|
await channel.sendMessage('slack:C0123456789', text);
|
|
|
|
expect(currentApp().client.chat.postMessage).toHaveBeenCalledTimes(1);
|
|
expect(currentApp().client.chat.postMessage).toHaveBeenCalledWith({
|
|
channel: 'C0123456789',
|
|
text,
|
|
});
|
|
});
|
|
|
|
it('splits messages into 3 parts when over 8000 chars', async () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
await channel.connect();
|
|
|
|
const longText = 'C'.repeat(8500);
|
|
await channel.sendMessage('slack:C0123456789', longText);
|
|
|
|
// 4000 + 4000 + 500 = 3 messages
|
|
expect(currentApp().client.chat.postMessage).toHaveBeenCalledTimes(3);
|
|
});
|
|
|
|
it('flushes queued messages on connect', async () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
|
|
// Queue messages while disconnected
|
|
await channel.sendMessage('slack:C0123456789', 'First queued');
|
|
await channel.sendMessage('slack:C0123456789', 'Second queued');
|
|
|
|
expect(currentApp().client.chat.postMessage).not.toHaveBeenCalled();
|
|
|
|
// Connect triggers flush
|
|
await channel.connect();
|
|
|
|
expect(currentApp().client.chat.postMessage).toHaveBeenCalledWith({
|
|
channel: 'C0123456789',
|
|
text: 'First queued',
|
|
});
|
|
expect(currentApp().client.chat.postMessage).toHaveBeenCalledWith({
|
|
channel: 'C0123456789',
|
|
text: 'Second queued',
|
|
});
|
|
});
|
|
});
|
|
|
|
// --- ownsJid ---
|
|
|
|
describe('ownsJid', () => {
|
|
it('owns slack: JIDs', () => {
|
|
const channel = new SlackChannel(createTestOpts());
|
|
expect(channel.ownsJid('slack:C0123456789')).toBe(true);
|
|
});
|
|
|
|
it('owns slack: DM JIDs', () => {
|
|
const channel = new SlackChannel(createTestOpts());
|
|
expect(channel.ownsJid('slack:D0123456789')).toBe(true);
|
|
});
|
|
|
|
it('does not own WhatsApp group JIDs', () => {
|
|
const channel = new SlackChannel(createTestOpts());
|
|
expect(channel.ownsJid('12345@g.us')).toBe(false);
|
|
});
|
|
|
|
it('does not own WhatsApp DM JIDs', () => {
|
|
const channel = new SlackChannel(createTestOpts());
|
|
expect(channel.ownsJid('12345@s.whatsapp.net')).toBe(false);
|
|
});
|
|
|
|
it('does not own Telegram JIDs', () => {
|
|
const channel = new SlackChannel(createTestOpts());
|
|
expect(channel.ownsJid('tg:123456')).toBe(false);
|
|
});
|
|
|
|
it('does not own unknown JID formats', () => {
|
|
const channel = new SlackChannel(createTestOpts());
|
|
expect(channel.ownsJid('random-string')).toBe(false);
|
|
});
|
|
});
|
|
|
|
// --- syncChannelMetadata ---
|
|
|
|
describe('syncChannelMetadata', () => {
|
|
it('calls conversations.list and updates chat names', async () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
|
|
currentApp().client.conversations.list.mockResolvedValue({
|
|
channels: [
|
|
{ id: 'C001', name: 'general', is_member: true },
|
|
{ id: 'C002', name: 'random', is_member: true },
|
|
{ id: 'C003', name: 'external', is_member: false },
|
|
],
|
|
response_metadata: {},
|
|
});
|
|
|
|
await channel.connect();
|
|
|
|
// connect() calls syncChannelMetadata internally
|
|
expect(updateChatName).toHaveBeenCalledWith('slack:C001', 'general');
|
|
expect(updateChatName).toHaveBeenCalledWith('slack:C002', 'random');
|
|
// Non-member channels are skipped
|
|
expect(updateChatName).not.toHaveBeenCalledWith('slack:C003', 'external');
|
|
});
|
|
|
|
it('handles API errors gracefully', async () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
|
|
currentApp().client.conversations.list.mockRejectedValue(
|
|
new Error('API error'),
|
|
);
|
|
|
|
// Should not throw
|
|
await expect(channel.connect()).resolves.toBeUndefined();
|
|
});
|
|
});
|
|
|
|
// --- setTyping ---
|
|
|
|
describe('setTyping', () => {
|
|
it('resolves without error (no-op)', async () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
|
|
// Should not throw — Slack has no bot typing indicator API
|
|
await expect(
|
|
channel.setTyping('slack:C0123456789', true),
|
|
).resolves.toBeUndefined();
|
|
});
|
|
|
|
it('accepts false without error', async () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
|
|
await expect(
|
|
channel.setTyping('slack:C0123456789', false),
|
|
).resolves.toBeUndefined();
|
|
});
|
|
});
|
|
|
|
// --- Constructor error handling ---
|
|
|
|
describe('constructor', () => {
|
|
it('throws when SLACK_BOT_TOKEN is missing', () => {
|
|
vi.mocked(readEnvFile).mockReturnValueOnce({
|
|
SLACK_BOT_TOKEN: '',
|
|
SLACK_APP_TOKEN: 'xapp-test-token',
|
|
});
|
|
|
|
expect(() => new SlackChannel(createTestOpts())).toThrow(
|
|
'SLACK_BOT_TOKEN and SLACK_APP_TOKEN must be set in .env',
|
|
);
|
|
});
|
|
|
|
it('throws when SLACK_APP_TOKEN is missing', () => {
|
|
vi.mocked(readEnvFile).mockReturnValueOnce({
|
|
SLACK_BOT_TOKEN: 'xoxb-test-token',
|
|
SLACK_APP_TOKEN: '',
|
|
});
|
|
|
|
expect(() => new SlackChannel(createTestOpts())).toThrow(
|
|
'SLACK_BOT_TOKEN and SLACK_APP_TOKEN must be set in .env',
|
|
);
|
|
});
|
|
});
|
|
|
|
// --- syncChannelMetadata pagination ---
|
|
|
|
describe('syncChannelMetadata pagination', () => {
|
|
it('paginates through multiple pages of channels', async () => {
|
|
const opts = createTestOpts();
|
|
const channel = new SlackChannel(opts);
|
|
|
|
// First page returns a cursor; second page returns no cursor
|
|
currentApp().client.conversations.list
|
|
.mockResolvedValueOnce({
|
|
channels: [
|
|
{ id: 'C001', name: 'general', is_member: true },
|
|
],
|
|
response_metadata: { next_cursor: 'cursor_page2' },
|
|
})
|
|
.mockResolvedValueOnce({
|
|
channels: [
|
|
{ id: 'C002', name: 'random', is_member: true },
|
|
],
|
|
response_metadata: {},
|
|
});
|
|
|
|
await channel.connect();
|
|
|
|
// Should have called conversations.list twice (once per page)
|
|
expect(currentApp().client.conversations.list).toHaveBeenCalledTimes(2);
|
|
expect(currentApp().client.conversations.list).toHaveBeenNthCalledWith(2,
|
|
expect.objectContaining({ cursor: 'cursor_page2' }),
|
|
);
|
|
|
|
// Both channels from both pages stored
|
|
expect(updateChatName).toHaveBeenCalledWith('slack:C001', 'general');
|
|
expect(updateChatName).toHaveBeenCalledWith('slack:C002', 'random');
|
|
});
|
|
});
|
|
|
|
// --- Channel properties ---
|
|
|
|
describe('channel properties', () => {
|
|
it('has name "slack"', () => {
|
|
const channel = new SlackChannel(createTestOpts());
|
|
expect(channel.name).toBe('slack');
|
|
});
|
|
});
|
|
});
|