import { describe, expect, it } from 'vitest';

import { resolveGameApiConfigFromEnv } from '../src/config.js';

describe('resolveGameApiConfigFromEnv', () => {
    it('keeps the deployment profile identity separate from the scenario id', () => {
        const config = resolveGameApiConfigFromEnv({
            PROFILE: 'hwe',
            SCENARIO: '1010',
            GAME_PROFILE_NAME: 'hwe:2',
            GAME_TOKEN_SECRET: 'test-secret',
        });

        expect(config.profile).toBe('hwe');
        expect(config.scenario).toBe('1010');
        expect(config.profileName).toBe('hwe:2');
    });

    it('falls back to the legacy profile and scenario pair', () => {
        const config = resolveGameApiConfigFromEnv({
            PROFILE: 'hwe',
            SCENARIO: '2',
            GAME_TOKEN_SECRET: 'test-secret',
        });

        expect(config.profileName).toBe('hwe:2');
    });

    it.each(['0', '-1', '1.5', '999', 'Infinity'])('rejects unsafe account icon reconcile interval %s', (value) => {
        expect(() =>
            resolveGameApiConfigFromEnv({
                GAME_TOKEN_SECRET: 'test-secret',
                ACCOUNT_ICON_RESET_RECONCILE_INTERVAL_MS: value,
            })
        ).toThrow('ACCOUNT_ICON_RESET_RECONCILE_INTERVAL_MS');
    });

    it('accepts a bounded integer account icon reconcile interval', () => {
        expect(
            resolveGameApiConfigFromEnv({
                GAME_TOKEN_SECRET: 'test-secret',
                ACCOUNT_ICON_RESET_RECONCILE_INTERVAL_MS: '1000',
            }).accountIconResetReconcileIntervalMs
        ).toBe(1000);
    });
});
