fork of hey-api/openapi-ts because I need some additional things
1import { isEnvironment } from '../cli';
2
3/**
4 * Replicates the outputHeaderToPrefix logic from generate/client.ts for testing.
5 */
6function outputHeaderToPrefix(header: unknown): string {
7 if (header == null || typeof header === 'function') return '';
8 const lines =
9 typeof header === 'string'
10 ? header.split(/\r?\n/)
11 : (header as string[]).flatMap((line) => line.split(/\r?\n/));
12 const content = lines.join('\n');
13 return content ? `${content}\n\n` : '';
14}
15
16describe('outputHeaderToPrefix logic', () => {
17 it('returns default comment for string header', () => {
18 const result = outputHeaderToPrefix('// This file is auto-generated by @hey-api/openapi-ts');
19 expect(result).toBe('// This file is auto-generated by @hey-api/openapi-ts\n\n');
20 });
21
22 it('returns joined lines for array header', () => {
23 const result = outputHeaderToPrefix([
24 '// This file is auto-generated by @hey-api/openapi-ts',
25 '// @ts-nocheck',
26 ]);
27 expect(result).toBe(
28 '// This file is auto-generated by @hey-api/openapi-ts\n// @ts-nocheck\n\n',
29 );
30 });
31
32 it('returns empty string for null header', () => {
33 expect(outputHeaderToPrefix(null)).toBe('');
34 });
35
36 it('returns empty string for undefined header', () => {
37 expect(outputHeaderToPrefix(undefined)).toBe('');
38 });
39
40 it('returns empty string for function header', () => {
41 expect(outputHeaderToPrefix(() => '// dynamic')).toBe('');
42 });
43
44 it('handles string with embedded newlines', () => {
45 const result = outputHeaderToPrefix('// line1\n// line2');
46 expect(result).toBe('// line1\n// line2\n\n');
47 });
48});
49
50describe('isEnvironment', () => {
51 const originalEnv = process.env;
52
53 beforeEach(() => {
54 process.env = { ...originalEnv };
55 delete process.env.HEYAPI_CODEGEN_ENV;
56 });
57
58 afterAll(() => {
59 process.env = originalEnv;
60 });
61
62 it('returns false when env var is not set', () => {
63 expect(isEnvironment('development')).toBe(false);
64 });
65
66 it('returns true when env var is set to "development"', () => {
67 process.env.HEYAPI_CODEGEN_ENV = 'development';
68 expect(isEnvironment('development')).toBe(true);
69 });
70
71 it('returns false when env var is set to other values', () => {
72 process.env.HEYAPI_CODEGEN_ENV = '0';
73 expect(isEnvironment('development')).toBe(false);
74
75 process.env.HEYAPI_CODEGEN_ENV = 'false';
76 expect(isEnvironment('development')).toBe(false);
77
78 process.env.HEYAPI_CODEGEN_ENV = 'anything';
79 expect(isEnvironment('development')).toBe(false);
80 });
81});