/**
 * Votes module unit tests — validates VoteDoc shape and route exports.
 */

import { describe, it, expect } from 'vitest';
import type { VoteDoc } from './types.js';

describe('VoteDoc', () => {
  it('accepts a valid vote document', () => {
    const doc: VoteDoc = {
      id: `vote_${crypto.randomUUID()}`,
      itemId: 'trk_abc123',
      productId: 'lysnrai',
      userId: 'usr_xyz',
      createdAt: new Date().toISOString(),
    };

    expect(doc.id).toMatch(/^vote_/);
    expect(doc.itemId).toBe('trk_abc123');
    expect(doc.productId).toBe('lysnrai');
    expect(doc.userId).toBe('usr_xyz');
    expect(doc.createdAt).toBeTruthy();
  });

  it('generates unique IDs with crypto.randomUUID', () => {
    const ids = new Set(Array.from({ length: 100 }, () => `vote_${crypto.randomUUID()}`));
    expect(ids.size).toBe(100);
  });

  it('id format uses UUID (no Math.random)', () => {
    const id = `vote_${crypto.randomUUID()}`;
    // UUID v4 pattern after prefix
    expect(id).toMatch(
      /^vote_[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/
    );
  });

  it('createdAt is ISO 8601 format', () => {
    const doc: VoteDoc = {
      id: `vote_${crypto.randomUUID()}`,
      itemId: 'trk_test',
      productId: 'lysnrai',
      userId: 'usr_test',
      createdAt: new Date().toISOString(),
    };
    // ISO 8601 pattern
    expect(doc.createdAt).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/);
  });
});

describe('voteRoutes export', () => {
  it('exports voteRoutes function', { timeout: 15_000 }, async () => {
    const mod = await import('./routes.js');
    expect(typeof mod.voteRoutes).toBe('function');
  });
});
