import 'reflect-metadata';

import { Test } from '@nestjs/testing';
import type { TestingModule } from '@nestjs/testing';
import request from 'supertest';
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';

describe('Default avatar assets (HTTP)', () => {
  const originalEnv = { ...process.env };

  let module: TestingModule | undefined;
  let app: any;

  beforeAll(async () => {
    vi.resetModules();

    process.env.GRIDPILOT_API_PERSISTENCE = 'inmemory';
    process.env.GRIDPILOT_API_BOOTSTRAP = 'false';
    delete process.env.DATABASE_URL;

    const { AppModule } = await import('../../app.module');

    module = await Test.createTestingModule({
      imports: [AppModule],
    }).compile();

    app = module.createNestApplication();
    await app.init();
  }, 20_000);

  afterAll(async () => {
    await app?.close();
    await module?.close();
    process.env = originalEnv;
    vi.restoreAllMocks();
  });

  it('serves male/female/neutral default avatar files from website public assets', async () => {
    const variants = ['male-default-avatar', 'female-default-avatar', 'neutral-default-avatar'] as const;

    for (const v of variants) {
      const res = await request(app.getHttpServer()).get(`/media/default/${v}`).expect(200);
      expect(res.headers['content-type']).toMatch(/image\/(jpeg|jpg)/);
      expect(Number(res.headers['content-length'] ?? 0)).toBeGreaterThan(0);
    }
  });
});

