Remove legacy Discord token aliases
This commit is contained in:
@@ -3,7 +3,15 @@ import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
|
||||
// --- Mocks ---
|
||||
|
||||
// Mock registry (registerChannel runs at import time)
|
||||
vi.mock('./registry.js', () => ({ registerChannel: vi.fn() }));
|
||||
const registeredChannelFactories = vi.hoisted(
|
||||
() => new Map<string, (...args: any[]) => any>(),
|
||||
);
|
||||
|
||||
vi.mock('./registry.js', () => ({
|
||||
registerChannel: vi.fn((name: string, factory: (...args: any[]) => any) => {
|
||||
registeredChannelFactories.set(name, factory);
|
||||
}),
|
||||
}));
|
||||
|
||||
// Mock env reader (used by the factory, not needed in unit tests)
|
||||
vi.mock('../env.js', () => ({
|
||||
@@ -113,6 +121,8 @@ vi.mock('discord.js', () => {
|
||||
});
|
||||
|
||||
import { DiscordChannel, DiscordChannelOpts } from './discord.js';
|
||||
import { registerChannel } from './registry.js';
|
||||
import { getEnv } from '../env.js';
|
||||
import { logger } from '../logger.js';
|
||||
|
||||
// --- Test helpers ---
|
||||
@@ -214,6 +224,31 @@ describe('DiscordChannel', () => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
describe('channel registration', () => {
|
||||
it('warns when only legacy owner token names are configured', () => {
|
||||
const ownerFactory = registeredChannelFactories.get('discord');
|
||||
|
||||
vi.mocked(getEnv).mockImplementation((key: string) => {
|
||||
if (key === 'DISCORD_BOT_TOKEN') return 'legacy-owner-token';
|
||||
return undefined;
|
||||
});
|
||||
|
||||
expect(ownerFactory).toBeTypeOf('function');
|
||||
expect(ownerFactory?.(createTestOpts() as any)).toBeNull();
|
||||
expect(logger.warn).toHaveBeenCalledWith(
|
||||
{
|
||||
role: 'owner',
|
||||
canonicalKey: 'DISCORD_OWNER_BOT_TOKEN',
|
||||
legacyKeys: ['DISCORD_BOT_TOKEN'],
|
||||
},
|
||||
'Discord: legacy bot token names detected; rename them to canonical role-based names',
|
||||
);
|
||||
expect(logger.warn).toHaveBeenCalledWith(
|
||||
'Discord: DISCORD_OWNER_BOT_TOKEN not set',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
// --- Connection lifecycle ---
|
||||
|
||||
describe('connection lifecycle', () => {
|
||||
|
||||
@@ -43,31 +43,24 @@ const DISCORD_ARBITER_LEGACY_TOKEN_KEYS = [
|
||||
'DISCORD_CODEX_REVIEW_BOT_TOKEN',
|
||||
];
|
||||
|
||||
function getConfiguredLegacyDiscordTokenKeys(keys: string[]): string[] {
|
||||
return keys.filter((key) => !!getEnv(key));
|
||||
}
|
||||
|
||||
function getRoleTokenOrLogMigrationError(
|
||||
function warnIfLegacyDiscordTokenConfigured(
|
||||
canonicalKey: string,
|
||||
legacyKeys: string[],
|
||||
role: 'owner' | 'reviewer' | 'arbiter',
|
||||
): string {
|
||||
const canonicalValue = getEnv(canonicalKey) || '';
|
||||
if (canonicalValue) return canonicalValue;
|
||||
): void {
|
||||
if (getEnv(canonicalKey)) return;
|
||||
|
||||
const configuredLegacyKeys = getConfiguredLegacyDiscordTokenKeys(legacyKeys);
|
||||
if (configuredLegacyKeys.length > 0) {
|
||||
logger.error(
|
||||
{
|
||||
role,
|
||||
canonicalKey,
|
||||
legacyKeys: configuredLegacyKeys,
|
||||
},
|
||||
'Discord: legacy service-based bot token names are no longer supported; rename them to canonical role-based names',
|
||||
);
|
||||
}
|
||||
const configuredLegacyKeys = legacyKeys.filter((key) => !!getEnv(key));
|
||||
if (configuredLegacyKeys.length === 0) return;
|
||||
|
||||
return '';
|
||||
logger.warn(
|
||||
{
|
||||
role,
|
||||
canonicalKey,
|
||||
legacyKeys: configuredLegacyKeys,
|
||||
},
|
||||
'Discord: legacy bot token names detected; rename them to canonical role-based names',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -834,11 +827,12 @@ export class DiscordChannel implements Channel {
|
||||
}
|
||||
|
||||
registerChannel(DISCORD_OWNER_CHANNEL, (opts: ChannelOpts) => {
|
||||
const token = getRoleTokenOrLogMigrationError(
|
||||
warnIfLegacyDiscordTokenConfigured(
|
||||
DISCORD_OWNER_TOKEN_KEY,
|
||||
DISCORD_OWNER_LEGACY_TOKEN_KEYS,
|
||||
'owner',
|
||||
);
|
||||
const token = getEnv(DISCORD_OWNER_TOKEN_KEY) || '';
|
||||
if (!token) {
|
||||
logger.warn('Discord: DISCORD_OWNER_BOT_TOKEN not set');
|
||||
return null;
|
||||
@@ -847,12 +841,13 @@ registerChannel(DISCORD_OWNER_CHANNEL, (opts: ChannelOpts) => {
|
||||
});
|
||||
|
||||
registerChannel(DISCORD_REVIEWER_CHANNEL, (opts: ChannelOpts) => {
|
||||
const ownerToken = getEnv(DISCORD_OWNER_TOKEN_KEY) || '';
|
||||
const token = getRoleTokenOrLogMigrationError(
|
||||
warnIfLegacyDiscordTokenConfigured(
|
||||
DISCORD_REVIEWER_TOKEN_KEY,
|
||||
DISCORD_REVIEWER_LEGACY_TOKEN_KEYS,
|
||||
'reviewer',
|
||||
);
|
||||
const ownerToken = getEnv(DISCORD_OWNER_TOKEN_KEY) || '';
|
||||
const token = getEnv(DISCORD_REVIEWER_TOKEN_KEY) || '';
|
||||
if (!token) return null;
|
||||
if (token === ownerToken) {
|
||||
logger.warn(
|
||||
@@ -871,13 +866,14 @@ registerChannel(DISCORD_REVIEWER_CHANNEL, (opts: ChannelOpts) => {
|
||||
});
|
||||
|
||||
registerChannel(DISCORD_ARBITER_CHANNEL, (opts: ChannelOpts) => {
|
||||
const ownerToken = getEnv(DISCORD_OWNER_TOKEN_KEY) || '';
|
||||
const reviewerToken = getEnv(DISCORD_REVIEWER_TOKEN_KEY) || '';
|
||||
const token = getRoleTokenOrLogMigrationError(
|
||||
warnIfLegacyDiscordTokenConfigured(
|
||||
DISCORD_ARBITER_TOKEN_KEY,
|
||||
DISCORD_ARBITER_LEGACY_TOKEN_KEYS,
|
||||
'arbiter',
|
||||
);
|
||||
const ownerToken = getEnv(DISCORD_OWNER_TOKEN_KEY) || '';
|
||||
const reviewerToken = getEnv(DISCORD_REVIEWER_TOKEN_KEY) || '';
|
||||
const token = getEnv(DISCORD_ARBITER_TOKEN_KEY) || '';
|
||||
if (!token) return null;
|
||||
if (token === ownerToken || token === reviewerToken) {
|
||||
logger.warn(
|
||||
|
||||
Reference in New Issue
Block a user