import { describe, expect, it } from 'vitest';
import type { Redis } from 'ioredis';
import {
  deleteConfirmation,
  getConfirmation,
  setConfirmation,
  takeConfirmation
} from './confirmation-store.js';

function createMemoryRedis(): Redis {
  const store = new Map<string, string>();
  return {
    async set(key: string, value: string) {
      store.set(key, value);
      return 'OK';
    },
    async get(key: string) {
      return store.get(key) ?? null;
    },
    async del(key: string) {
      store.delete(key);
      return 1;
    }
  } as unknown as Redis;
}

describe('confirmation-store', () => {
  it('stores and retrieves pending confirmations', async () => {
    const redis = createMemoryRedis();
    await setConfirmation(redis, 'moderation', 'tok-1', { action: 'ban', userId: '1' }, 60);
    await expect(getConfirmation(redis, 'moderation', 'tok-1')).resolves.toEqual({
      action: 'ban',
      userId: '1'
    });
  });

  it('takeConfirmation deletes the entry', async () => {
    const redis = createMemoryRedis();
    await setConfirmation(redis, 'moderation', 'tok-2', { action: 'purge' }, 60);
    await expect(takeConfirmation(redis, 'moderation', 'tok-2')).resolves.toEqual({
      action: 'purge'
    });
    await expect(getConfirmation(redis, 'moderation', 'tok-2')).resolves.toBeNull();
  });

  it('deleteConfirmation removes pending state', async () => {
    const redis = createMemoryRedis();
    await setConfirmation(redis, 'guildbackup', 'tok-3', { action: 'delete' }, 60);
    await deleteConfirmation(redis, 'guildbackup', 'tok-3');
    await expect(getConfirmation(redis, 'guildbackup', 'tok-3')).resolves.toBeNull();
  });
});
