|
1 | | -import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
2 | 2 |
|
3 | 3 | const { clientMock, restMock } = vi.hoisted(() => ({ |
4 | 4 | clientMock: { |
@@ -127,6 +127,7 @@ vi.mock('../commands/thread-setup.js', () => ({ |
127 | 127 |
|
128 | 128 | import { handleDiscordMessageCreate } from '../index.js'; |
129 | 129 | import { handleSkillAutocomplete } from '../commands/skill.js'; |
| 130 | +import { config as discordConfig } from '../config.js'; |
130 | 131 | import { |
131 | 132 | applyMessageControlDirectives, |
132 | 133 | getAvailableBackendCapabilities, |
@@ -469,3 +470,119 @@ describe('handleDiscordMessageCreate', () => { |
469 | 470 | }); |
470 | 471 | }); |
471 | 472 | }); |
| 473 | + |
| 474 | +describe('allowedChannelIds filter', () => { |
| 475 | + afterEach(() => { |
| 476 | + discordConfig.allowedChannelIds = []; |
| 477 | + }); |
| 478 | + |
| 479 | + it('drops messages from channels not in the allowlist', async () => { |
| 480 | + discordConfig.allowedChannelIds = ['allowed-channel']; |
| 481 | + |
| 482 | + const message = createBaseMessage(); |
| 483 | + message.channel.id = 'other-channel'; |
| 484 | + |
| 485 | + await handleDiscordMessageCreate(message, { |
| 486 | + botUser: { id: 'relay-bot' }, |
| 487 | + hasOpenStickyThreadSession: () => false, |
| 488 | + runThreadConversation: vi.fn(), |
| 489 | + ensureMentionThread: vi.fn(), |
| 490 | + promptThreadSetup: vi.fn(), |
| 491 | + applySetupResult: vi.fn(), |
| 492 | + }); |
| 493 | + |
| 494 | + expect(message.react).not.toHaveBeenCalled(); |
| 495 | + }); |
| 496 | + |
| 497 | + it('accepts messages from allowed channels', async () => { |
| 498 | + discordConfig.allowedChannelIds = ['allowed-channel']; |
| 499 | + |
| 500 | + const message = createBaseMessage(); |
| 501 | + message.channel.id = 'allowed-channel'; |
| 502 | + |
| 503 | + const ensureMentionThread = vi.fn(async () => ({ |
| 504 | + id: 'thread-1', |
| 505 | + send: vi.fn(async () => undefined), |
| 506 | + })); |
| 507 | + |
| 508 | + await handleDiscordMessageCreate(message, { |
| 509 | + botUser: { id: 'relay-bot' }, |
| 510 | + hasOpenStickyThreadSession: () => false, |
| 511 | + runThreadConversation: vi.fn(async () => true), |
| 512 | + ensureMentionThread, |
| 513 | + promptThreadSetup: vi.fn(async () => ({ kind: 'skip' })), |
| 514 | + applySetupResult: vi.fn(), |
| 515 | + }); |
| 516 | + |
| 517 | + expect(message.react).toHaveBeenCalled(); |
| 518 | + }); |
| 519 | + |
| 520 | + it('accepts thread messages whose parent is in the allowlist', async () => { |
| 521 | + discordConfig.allowedChannelIds = ['allowed-channel']; |
| 522 | + |
| 523 | + const message = createBaseMessage(); |
| 524 | + message.channel = { |
| 525 | + id: 'thread-in-allowed', |
| 526 | + parentId: 'allowed-channel', |
| 527 | + isThread: () => true, |
| 528 | + send: vi.fn(async () => undefined), |
| 529 | + }; |
| 530 | + |
| 531 | + await handleDiscordMessageCreate(message, { |
| 532 | + botUser: { id: 'relay-bot' }, |
| 533 | + hasOpenStickyThreadSession: () => true, |
| 534 | + runThreadConversation: vi.fn(async () => true), |
| 535 | + ensureMentionThread: vi.fn(), |
| 536 | + promptThreadSetup: vi.fn(), |
| 537 | + applySetupResult: vi.fn(), |
| 538 | + }); |
| 539 | + |
| 540 | + expect(message.react).toHaveBeenCalled(); |
| 541 | + }); |
| 542 | + |
| 543 | + it('drops thread messages whose parent is not in the allowlist', async () => { |
| 544 | + discordConfig.allowedChannelIds = ['allowed-channel']; |
| 545 | + |
| 546 | + const message = createBaseMessage(); |
| 547 | + message.channel = { |
| 548 | + id: 'thread-in-other', |
| 549 | + parentId: 'other-channel', |
| 550 | + isThread: () => true, |
| 551 | + send: vi.fn(async () => undefined), |
| 552 | + }; |
| 553 | + |
| 554 | + await handleDiscordMessageCreate(message, { |
| 555 | + botUser: { id: 'relay-bot' }, |
| 556 | + hasOpenStickyThreadSession: () => true, |
| 557 | + runThreadConversation: vi.fn(), |
| 558 | + ensureMentionThread: vi.fn(), |
| 559 | + promptThreadSetup: vi.fn(), |
| 560 | + applySetupResult: vi.fn(), |
| 561 | + }); |
| 562 | + |
| 563 | + expect(message.react).not.toHaveBeenCalled(); |
| 564 | + }); |
| 565 | + |
| 566 | + it('allows all channels when allowedChannelIds is empty', async () => { |
| 567 | + discordConfig.allowedChannelIds = []; |
| 568 | + |
| 569 | + const message = createBaseMessage(); |
| 570 | + message.channel.id = 'any-channel'; |
| 571 | + |
| 572 | + const ensureMentionThread = vi.fn(async () => ({ |
| 573 | + id: 'thread-1', |
| 574 | + send: vi.fn(async () => undefined), |
| 575 | + })); |
| 576 | + |
| 577 | + await handleDiscordMessageCreate(message, { |
| 578 | + botUser: { id: 'relay-bot' }, |
| 579 | + hasOpenStickyThreadSession: () => false, |
| 580 | + runThreadConversation: vi.fn(async () => true), |
| 581 | + ensureMentionThread, |
| 582 | + promptThreadSetup: vi.fn(async () => ({ kind: 'skip' })), |
| 583 | + applySetupResult: vi.fn(), |
| 584 | + }); |
| 585 | + |
| 586 | + expect(message.react).toHaveBeenCalled(); |
| 587 | + }); |
| 588 | +}); |
0 commit comments