|
1 | | -import { main } from '../index'; |
| 1 | +import { main, start, type CliOptions } from '../index'; |
2 | 2 | import { parseCliOptions, freezeOptions, type GlobalOptions } from '../options'; |
3 | 3 | import { runServer } from '../server'; |
4 | 4 |
|
@@ -111,5 +111,95 @@ describe('main', () => { |
111 | 111 |
|
112 | 112 | expect(callOrder).toEqual(['parse', 'freeze', 'run']); |
113 | 113 | }); |
| 114 | + |
| 115 | + it('should merge programmatic options with CLI options', async () => { |
| 116 | + const cliOptions = { docsHost: false }; |
| 117 | + const programmaticOptions = { docsHost: true }; |
| 118 | + |
| 119 | + mockParseCliOptions.mockReturnValue(cliOptions); |
| 120 | + |
| 121 | + await main(programmaticOptions); |
| 122 | + |
| 123 | + // Should merge CLI options with programmatic options (programmatic takes precedence) |
| 124 | + expect(mockFreezeOptions).toHaveBeenCalledWith({ docsHost: true }); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should work with empty programmatic options', async () => { |
| 128 | + const cliOptions = { docsHost: true }; |
| 129 | + |
| 130 | + mockParseCliOptions.mockReturnValue(cliOptions); |
| 131 | + |
| 132 | + await main({}); |
| 133 | + |
| 134 | + expect(mockFreezeOptions).toHaveBeenCalledWith({ docsHost: true }); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should work with undefined programmatic options', async () => { |
| 138 | + const cliOptions = { docsHost: false }; |
| 139 | + |
| 140 | + mockParseCliOptions.mockReturnValue(cliOptions); |
| 141 | + |
| 142 | + await main(); |
| 143 | + |
| 144 | + expect(mockFreezeOptions).toHaveBeenCalledWith({ docsHost: false }); |
| 145 | + }); |
| 146 | +}); |
| 147 | + |
| 148 | +describe('start alias', () => { |
| 149 | + let consoleErrorSpy: jest.SpyInstance; |
| 150 | + let processExitSpy: jest.SpyInstance; |
| 151 | + |
| 152 | + beforeEach(() => { |
| 153 | + jest.clearAllMocks(); |
| 154 | + |
| 155 | + // Mock process.exit to prevent actual exit |
| 156 | + processExitSpy = jest.spyOn(process, 'exit').mockImplementation(() => undefined as never); |
| 157 | + |
| 158 | + // Mock console.error |
| 159 | + consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(); |
| 160 | + |
| 161 | + // Setup default mocks |
| 162 | + mockParseCliOptions.mockReturnValue({ docsHost: false }); |
| 163 | + mockFreezeOptions.mockReturnValue({} as GlobalOptions); |
| 164 | + mockRunServer.mockResolvedValue(undefined); |
| 165 | + }); |
| 166 | + |
| 167 | + afterEach(() => { |
| 168 | + consoleErrorSpy.mockRestore(); |
| 169 | + processExitSpy.mockRestore(); |
| 170 | + }); |
| 171 | + |
| 172 | + it('should be equivalent to main function', async () => { |
| 173 | + const cliOptions = { docsHost: true }; |
| 174 | + |
| 175 | + mockParseCliOptions.mockReturnValue(cliOptions); |
| 176 | + |
| 177 | + await start(); |
| 178 | + |
| 179 | + expect(mockParseCliOptions).toHaveBeenCalled(); |
| 180 | + expect(mockFreezeOptions).toHaveBeenCalledWith(cliOptions); |
| 181 | + expect(mockRunServer).toHaveBeenCalled(); |
| 182 | + }); |
| 183 | + |
| 184 | + it('should accept programmatic options like main', async () => { |
| 185 | + const cliOptions = { docsHost: false }; |
| 186 | + const programmaticOptions = { docsHost: true }; |
| 187 | + |
| 188 | + mockParseCliOptions.mockReturnValue(cliOptions); |
| 189 | + |
| 190 | + await start(programmaticOptions); |
| 191 | + |
| 192 | + expect(mockFreezeOptions).toHaveBeenCalledWith({ docsHost: true }); |
| 193 | + }); |
| 194 | +}); |
| 195 | + |
| 196 | +describe('type exports', () => { |
| 197 | + it('should export CliOptions type', () => { |
| 198 | + // This test ensures the type is properly exported |
| 199 | + // TypeScript compilation will fail if the type is not available |
| 200 | + const options: Partial<CliOptions> = { docsHost: true }; |
| 201 | + |
| 202 | + expect(options).toBeDefined(); |
| 203 | + }); |
114 | 204 | }); |
115 | 205 |
|
0 commit comments