fork of hey-api/openapi-ts because I need some additional things
1import path from 'node:path';
2
3describe('isDevMode logic', () => {
4 const scenarios: ReadonlyArray<{
5 description: string;
6 expected: boolean;
7 mockPath: string;
8 }> = [
9 {
10 description: 'returns true in dev mode (src/generate)',
11 expected: true,
12 mockPath: ['', 'home', 'user', 'packages', 'openapi-python', 'src', 'generate'].join(
13 path.sep,
14 ),
15 },
16 {
17 description: 'returns false in prod mode (dist/generate)',
18 expected: false,
19 mockPath: ['', 'home', 'user', 'packages', 'openapi-python', 'dist', 'generate'].join(
20 path.sep,
21 ),
22 },
23 {
24 description: 'returns false when path contains /src/ but not in correct position',
25 expected: false,
26 mockPath: [
27 '',
28 'home',
29 'user',
30 'src',
31 'project',
32 'node_modules',
33 '@hey-api',
34 'openapi-python',
35 'dist',
36 'generate',
37 ].join(path.sep),
38 },
39 {
40 description: 'returns false when src is in project path (pnpm case from issue)',
41 expected: false,
42 mockPath: [
43 '',
44 'home',
45 'user',
46 'src',
47 'thcdb',
48 'worktree',
49 'schema-gen',
50 'web',
51 'node_modules',
52 '.pnpm',
53 '@hey-api+openapi-python@0.91.1',
54 'node_modules',
55 '@hey-api',
56 'openapi-python',
57 'dist',
58 'generate',
59 ].join(path.sep),
60 },
61 {
62 description:
63 'returns false when src is in project path with plugins path (exact error from issue)',
64 expected: false,
65 mockPath: [
66 '',
67 'home',
68 'user',
69 'src',
70 'thcdb',
71 'worktree',
72 'schema-gen',
73 'web',
74 'node_modules',
75 '.pnpm',
76 '@hey-api+openapi-python@0.91.1_magicast@0.5.1_typescript@5.9.3',
77 'node_modules',
78 '@hey-api',
79 'openapi-python',
80 'plugins',
81 '@hey-api',
82 'client-core',
83 'bundle',
84 ].join(path.sep),
85 },
86 {
87 description: 'returns true only when ending with src/generate',
88 expected: true,
89 mockPath: [
90 '',
91 'home',
92 'user',
93 'src',
94 'backup',
95 'packages',
96 'openapi-python',
97 'src',
98 'generate',
99 ].join(path.sep),
100 },
101 {
102 description: 'returns false when not ending with generate',
103 expected: false,
104 mockPath: ['', 'home', 'user', 'packages', 'openapi-python', 'src', 'plugins'].join(path.sep),
105 },
106 {
107 description: 'returns false when src exists but dist is later',
108 expected: false,
109 mockPath: ['', 'home', 'user', 'src', 'project', 'openapi-python', 'dist', 'generate'].join(
110 path.sep,
111 ),
112 },
113 ];
114
115 it.each(scenarios)('$description', ({ expected, mockPath }) => {
116 // Test the isDevMode logic
117 const normalized = mockPath.split(path.sep);
118 const srcIndex = normalized.lastIndexOf('src');
119 const distIndex = normalized.lastIndexOf('dist');
120
121 const result =
122 srcIndex !== -1 &&
123 srcIndex > distIndex &&
124 srcIndex === normalized.length - 2 &&
125 normalized[srcIndex + 1] === 'generate';
126
127 expect(result).toBe(expected);
128 });
129});