import { isEnvironment } from '../cli'; /** * Replicates the outputHeaderToPrefix logic from generate/client.ts for testing. */ function outputHeaderToPrefix(header: unknown): string { if (header == null || typeof header === 'function') return ''; const lines = typeof header === 'string' ? header.split(/\r?\n/) : (header as string[]).flatMap((line) => line.split(/\r?\n/)); const content = lines.join('\n'); return content ? `${content}\n\n` : ''; } describe('outputHeaderToPrefix logic', () => { it('returns default comment for string header', () => { const result = outputHeaderToPrefix('// This file is auto-generated by @hey-api/openapi-ts'); expect(result).toBe('// This file is auto-generated by @hey-api/openapi-ts\n\n'); }); it('returns joined lines for array header', () => { const result = outputHeaderToPrefix([ '// This file is auto-generated by @hey-api/openapi-ts', '// @ts-nocheck', ]); expect(result).toBe( '// This file is auto-generated by @hey-api/openapi-ts\n// @ts-nocheck\n\n', ); }); it('returns empty string for null header', () => { expect(outputHeaderToPrefix(null)).toBe(''); }); it('returns empty string for undefined header', () => { expect(outputHeaderToPrefix(undefined)).toBe(''); }); it('returns empty string for function header', () => { expect(outputHeaderToPrefix(() => '// dynamic')).toBe(''); }); it('handles string with embedded newlines', () => { const result = outputHeaderToPrefix('// line1\n// line2'); expect(result).toBe('// line1\n// line2\n\n'); }); }); describe('isEnvironment', () => { const originalEnv = process.env; beforeEach(() => { process.env = { ...originalEnv }; delete process.env.HEYAPI_CODEGEN_ENV; }); afterAll(() => { process.env = originalEnv; }); it('returns false when env var is not set', () => { expect(isEnvironment('development')).toBe(false); }); it('returns true when env var is set to "development"', () => { process.env.HEYAPI_CODEGEN_ENV = 'development'; expect(isEnvironment('development')).toBe(true); }); it('returns false when env var is set to other values', () => { process.env.HEYAPI_CODEGEN_ENV = '0'; expect(isEnvironment('development')).toBe(false); process.env.HEYAPI_CODEGEN_ENV = 'false'; expect(isEnvironment('development')).toBe(false); process.env.HEYAPI_CODEGEN_ENV = 'anything'; expect(isEnvironment('development')).toBe(false); }); });