Files
gridpilot.gg/tests/unit/website/getAppMode.test.ts
2025-12-12 23:49:56 +01:00

63 lines
1.8 KiB
TypeScript

import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { getAppMode, AppMode } from '../../../apps/website/lib/mode';
const ORIGINAL_NODE_ENV = process.env.NODE_ENV;
describe('getAppMode', () => {
const originalEnv = process.env;
beforeEach(() => {
process.env = { ...originalEnv };
(process.env as any).NODE_ENV = 'production';
});
afterEach(() => {
process.env = originalEnv;
(process.env as any).NODE_ENV = ORIGINAL_NODE_ENV;
});
it('returns "pre-launch" when NEXT_PUBLIC_GRIDPILOT_MODE is undefined', () => {
delete process.env.NEXT_PUBLIC_GRIDPILOT_MODE;
const mode = getAppMode();
expect(mode).toBe<AppMode>('pre-launch');
});
it('returns "pre-launch" when NEXT_PUBLIC_GRIDPILOT_MODE is explicitly set to "pre-launch"', () => {
process.env.NEXT_PUBLIC_GRIDPILOT_MODE = 'pre-launch';
const mode = getAppMode();
expect(mode).toBe<AppMode>('pre-launch');
});
it('returns "alpha" when NEXT_PUBLIC_GRIDPILOT_MODE is set to "alpha"', () => {
process.env.NEXT_PUBLIC_GRIDPILOT_MODE = 'alpha';
const mode = getAppMode();
expect(mode).toBe<AppMode>('alpha');
});
it('falls back to "pre-launch" and logs when NEXT_PUBLIC_GRIDPILOT_MODE is invalid in production', () => {
const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {});
process.env.NEXT_PUBLIC_GRIDPILOT_MODE = 'invalid-mode' as any;
const mode = getAppMode();
expect(mode).toBe<AppMode>('pre-launch');
expect(consoleError).toHaveBeenCalled();
consoleError.mockRestore();
});
it('throws in development when NEXT_PUBLIC_GRIDPILOT_MODE is invalid', () => {
(process.env as any).NODE_ENV = 'development';
process.env.NEXT_PUBLIC_GRIDPILOT_MODE = 'invalid-mode' as any;
expect(() => getAppMode()).toThrowError(/Invalid NEXT_PUBLIC_GRIDPILOT_MODE/);
});
});