import { createHash } from 'node:crypto';
import { describe, expect, it } from 'vitest';
import { moduleForCommand } from './module-gates.js';

describe('moduleForCommand', () => {
  it('maps moderation commands', () => {
    expect(moduleForCommand('ban')).toBe('moderation');
    expect(moduleForCommand('warn')).toBe('moderation');
    expect(moduleForCommand('case')).toBe('moderation');
  });

  it('maps redis-only and prisma modules', () => {
    expect(moduleForCommand('giveaway')).toBe('giveaways');
    expect(moduleForCommand('tag')).toBe('tags');
    expect(moduleForCommand('backup')).toBe('guildbackup');
    expect(moduleForCommand('economy' as string)).toBeNull();
    expect(moduleForCommand('balance')).toBe('economy');
  });

  it('leaves core commands unmapped', () => {
    expect(moduleForCommand('help')).toBeNull();
    expect(moduleForCommand('info')).toBeNull();
    expect(moduleForCommand('about')).toBeNull();
    expect(moduleForCommand('gdpr')).toBeNull();
  });
});

describe('feature flag rollout hashing', () => {
  it('is stable for the same guild/key pair', () => {
    const a = createHash('sha256').update('moderation:guild-1').digest().readUInt32BE(0) % 100;
    const b = createHash('sha256').update('moderation:guild-1').digest().readUInt32BE(0) % 100;
    expect(a).toBe(b);
  });
});
