|
| 1 | +import * as fs from 'node:fs'; |
| 2 | + |
| 3 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 4 | + |
| 5 | +vi.mock('node:fs', () => ({ |
| 6 | + existsSync: vi.fn(() => true), |
| 7 | + statSync: vi.fn(() => ({ isDirectory: () => true })) |
| 8 | +})); |
| 9 | + |
| 10 | +describe('commandLine', () => { |
| 11 | + const originalArgv = process.argv; |
| 12 | + const originalExit = process.exit; |
| 13 | + const originalConsoleLog = console.log; |
| 14 | + const originalConsoleError = console.error; |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + vi.clearAllMocks(); |
| 18 | + process.exit = vi.fn() as never; |
| 19 | + console.log = vi.fn(); |
| 20 | + console.error = vi.fn(); |
| 21 | + }); |
| 22 | + |
| 23 | + afterEach(() => { |
| 24 | + process.argv = originalArgv; |
| 25 | + process.exit = originalExit; |
| 26 | + console.log = originalConsoleLog; |
| 27 | + console.error = originalConsoleError; |
| 28 | + vi.resetModules(); |
| 29 | + }); |
| 30 | + |
| 31 | + describe('argument parsing', () => { |
| 32 | + it('should parse arguments with --flag=value format', async () => { |
| 33 | + process.argv = [ |
| 34 | + 'node', |
| 35 | + 'script.js', |
| 36 | + '--engine=async', |
| 37 | + '--sourcepath=/test/dist', |
| 38 | + '--outputfile=/test/output.h', |
| 39 | + '--etag=true', |
| 40 | + '--gzip=false' |
| 41 | + ]; |
| 42 | + |
| 43 | + const { cmdLine } = await import('../../src/commandLine'); |
| 44 | + |
| 45 | + expect(cmdLine.engine).toBe('async'); |
| 46 | + expect(cmdLine.sourcepath).toBe('/test/dist'); |
| 47 | + expect(cmdLine.outputfile).toBe('/test/output.h'); |
| 48 | + expect(cmdLine.etag).toBe('true'); |
| 49 | + expect(cmdLine.gzip).toBe('false'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should parse arguments with -flag value format', async () => { |
| 53 | + process.argv = ['node', 'script.js', '-e', 'psychic2', '-s', '/test/dist', '-o', '/test/output.h']; |
| 54 | + |
| 55 | + const { cmdLine } = await import('../../src/commandLine'); |
| 56 | + |
| 57 | + expect(cmdLine.engine).toBe('psychic2'); |
| 58 | + expect(cmdLine.sourcepath).toBe('/test/dist'); |
| 59 | + expect(cmdLine.outputfile).toBe('/test/output.h'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should parse arguments with --flag value format', async () => { |
| 63 | + process.argv = [ |
| 64 | + 'node', |
| 65 | + 'script.js', |
| 66 | + '--engine', |
| 67 | + 'espidf', |
| 68 | + '--sourcepath', |
| 69 | + '/test/dist', |
| 70 | + '--outputfile', |
| 71 | + '/test/output.h' |
| 72 | + ]; |
| 73 | + |
| 74 | + const { cmdLine } = await import('../../src/commandLine'); |
| 75 | + |
| 76 | + expect(cmdLine.engine).toBe('espidf'); |
| 77 | + expect(cmdLine.sourcepath).toBe('/test/dist'); |
| 78 | + expect(cmdLine.outputfile).toBe('/test/output.h'); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should use default values when not specified', async () => { |
| 82 | + process.argv = ['node', 'script.js', '--sourcepath=/test/dist']; |
| 83 | + |
| 84 | + const { cmdLine } = await import('../../src/commandLine'); |
| 85 | + |
| 86 | + expect(cmdLine.engine).toBe('psychic'); |
| 87 | + expect(cmdLine.outputfile).toBe('svelteesp32.h'); |
| 88 | + expect(cmdLine.etag).toBe('false'); |
| 89 | + expect(cmdLine.gzip).toBe('true'); |
| 90 | + expect(cmdLine.created).toBe(false); |
| 91 | + expect(cmdLine.version).toBe(''); |
| 92 | + expect(cmdLine.espmethod).toBe('initSvelteStaticFiles'); |
| 93 | + expect(cmdLine.define).toBe('SVELTEESP32'); |
| 94 | + expect(cmdLine.cachetime).toBe(0); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should parse boolean flags', async () => { |
| 98 | + process.argv = ['node', 'script.js', '--sourcepath=/test/dist', '--created']; |
| 99 | + |
| 100 | + const { cmdLine } = await import('../../src/commandLine'); |
| 101 | + |
| 102 | + expect(cmdLine.created).toBe(true); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should parse version, espmethod, define, and cachetime', async () => { |
| 106 | + process.argv = [ |
| 107 | + 'node', |
| 108 | + 'script.js', |
| 109 | + '--sourcepath=/test/dist', |
| 110 | + '--version=v1.0.0', |
| 111 | + '--espmethod=myMethod', |
| 112 | + '--define=MYPREFIX', |
| 113 | + '--cachetime=86400' |
| 114 | + ]; |
| 115 | + |
| 116 | + const { cmdLine } = await import('../../src/commandLine'); |
| 117 | + |
| 118 | + expect(cmdLine.version).toBe('v1.0.0'); |
| 119 | + expect(cmdLine.espmethod).toBe('myMethod'); |
| 120 | + expect(cmdLine.define).toBe('MYPREFIX'); |
| 121 | + expect(cmdLine.cachetime).toBe(86_400); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should handle value with equals sign in --flag=value format', async () => { |
| 125 | + process.argv = ['node', 'script.js', '--sourcepath=/test/dist', '--version=v1.0.0=beta']; |
| 126 | + |
| 127 | + const { cmdLine } = await import('../../src/commandLine'); |
| 128 | + |
| 129 | + expect(cmdLine.version).toBe('v1.0.0=beta'); |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + describe('validation', () => { |
| 134 | + it('should validate engine values', async () => { |
| 135 | + process.argv = ['node', 'script.js', '--engine=invalid', '--sourcepath=/test/dist']; |
| 136 | + |
| 137 | + await expect(import('../../src/commandLine')).rejects.toThrow('Invalid engine: invalid'); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should accept all valid engine values', async () => { |
| 141 | + for (const engine of ['psychic', 'psychic2', 'async', 'espidf']) { |
| 142 | + vi.resetModules(); |
| 143 | + process.argv = ['node', 'script.js', `--engine=${engine}`, '--sourcepath=/test/dist']; |
| 144 | + |
| 145 | + const { cmdLine } = await import('../../src/commandLine'); |
| 146 | + expect(cmdLine.engine).toBe(engine); |
| 147 | + } |
| 148 | + }); |
| 149 | + |
| 150 | + it('should validate etag tri-state values', async () => { |
| 151 | + process.argv = ['node', 'script.js', '--sourcepath=/test/dist', '--etag=invalid']; |
| 152 | + |
| 153 | + await expect(import('../../src/commandLine')).rejects.toThrow('Invalid etag: invalid'); |
| 154 | + }); |
| 155 | + |
| 156 | + it('should accept all valid etag values', async () => { |
| 157 | + for (const etag of ['true', 'false', 'compiler']) { |
| 158 | + vi.resetModules(); |
| 159 | + process.argv = ['node', 'script.js', '--sourcepath=/test/dist', `--etag=${etag}`]; |
| 160 | + |
| 161 | + const { cmdLine } = await import('../../src/commandLine'); |
| 162 | + expect(cmdLine.etag).toBe(etag); |
| 163 | + } |
| 164 | + }); |
| 165 | + |
| 166 | + it('should validate gzip tri-state values', async () => { |
| 167 | + process.argv = ['node', 'script.js', '--sourcepath=/test/dist', '--gzip=invalid']; |
| 168 | + |
| 169 | + await expect(import('../../src/commandLine')).rejects.toThrow('Invalid gzip: invalid'); |
| 170 | + }); |
| 171 | + |
| 172 | + it('should validate cachetime is a number', async () => { |
| 173 | + process.argv = ['node', 'script.js', '--sourcepath=/test/dist', '--cachetime=notanumber']; |
| 174 | + |
| 175 | + await expect(import('../../src/commandLine')).rejects.toThrow('Invalid cachetime: notanumber'); |
| 176 | + }); |
| 177 | + |
| 178 | + it('should throw error for unknown flag', async () => { |
| 179 | + process.argv = ['node', 'script.js', '--sourcepath=/test/dist', '--unknown=value']; |
| 180 | + |
| 181 | + await expect(import('../../src/commandLine')).rejects.toThrow('Unknown flag: --unknown'); |
| 182 | + }); |
| 183 | + |
| 184 | + it('should throw error for missing value', async () => { |
| 185 | + process.argv = ['node', 'script.js', '--sourcepath=/test/dist', '--engine']; |
| 186 | + |
| 187 | + await expect(import('../../src/commandLine')).rejects.toThrow('Missing value for flag: --engine'); |
| 188 | + }); |
| 189 | + |
| 190 | + it('should throw error for invalid argument format', async () => { |
| 191 | + process.argv = ['node', 'script.js', '--sourcepath=/test/dist', '--=']; |
| 192 | + |
| 193 | + await expect(import('../../src/commandLine')).rejects.toThrow('Invalid argument format: --='); |
| 194 | + }); |
| 195 | + }); |
| 196 | + |
| 197 | + describe('required arguments', () => { |
| 198 | + it('should exit when sourcepath is missing', async () => { |
| 199 | + process.argv = ['node', 'script.js']; |
| 200 | + |
| 201 | + await import('../../src/commandLine').catch(() => {}); |
| 202 | + |
| 203 | + expect(console.error).toHaveBeenCalledWith('Error: --sourcepath is required'); |
| 204 | + expect(process.exit).toHaveBeenCalledWith(0); |
| 205 | + }); |
| 206 | + }); |
| 207 | + |
| 208 | + describe('directory validation', () => { |
| 209 | + it('should exit when source directory does not exist', async () => { |
| 210 | + process.argv = ['node', 'script.js', '--sourcepath=/nonexistent/path']; |
| 211 | + |
| 212 | + const fsModule = await import('node:fs'); |
| 213 | + vi.mocked(fsModule.existsSync).mockReturnValue(false); |
| 214 | + |
| 215 | + await import('../../src/commandLine').catch(() => {}); |
| 216 | + |
| 217 | + expect(console.error).toHaveBeenCalledWith('Directory /nonexistent/path not exists or not a directory'); |
| 218 | + expect(process.exit).toHaveBeenCalledWith(1); |
| 219 | + }); |
| 220 | + |
| 221 | + it('should exit when source path is not a directory', async () => { |
| 222 | + process.argv = ['node', 'script.js', '--sourcepath=/test/file.txt']; |
| 223 | + |
| 224 | + const fsModule = await import('node:fs'); |
| 225 | + vi.mocked(fsModule.existsSync).mockReturnValue(true); |
| 226 | + vi.mocked(fsModule.statSync).mockReturnValue({ isDirectory: () => false } as fs.Stats); |
| 227 | + |
| 228 | + await import('../../src/commandLine').catch(() => {}); |
| 229 | + |
| 230 | + expect(console.error).toHaveBeenCalledWith('Directory /test/file.txt not exists or not a directory'); |
| 231 | + expect(process.exit).toHaveBeenCalledWith(1); |
| 232 | + }); |
| 233 | + }); |
| 234 | +}); |
0 commit comments