@simplens/onboard 1.0.8 → 1.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,188 +0,0 @@
1
- import { describe, it, expect } from 'vitest';
2
- import {
3
- OnboardingError,
4
- DockerNotInstalledError,
5
- DockerNotRunningError,
6
- DockerPermissionError,
7
- DockerComposeError,
8
- FileSystemError,
9
- DirectoryNotWritableError,
10
- isOnboardingError,
11
- formatErrorForUser,
12
- } from '../types/errors.js';
13
-
14
- describe('error types', () => {
15
- describe('OnboardingError', () => {
16
- it('should create error with code and message', () => {
17
- const error = new OnboardingError('TEST_CODE', 'Test message');
18
-
19
- expect(error.code).toBe('TEST_CODE');
20
- expect(error.message).toBe('Test message');
21
- expect(error.name).toBe('OnboardingError');
22
- });
23
-
24
- it('should include troubleshooting when provided', () => {
25
- const error = new OnboardingError(
26
- 'TEST_CODE',
27
- 'Test message',
28
- 'Try this fix'
29
- );
30
-
31
- expect(error.troubleshooting).toBe('Try this fix');
32
- });
33
-
34
- it('should have proper stack trace', () => {
35
- const error = new OnboardingError('TEST', 'message');
36
-
37
- expect(error.stack).toBeDefined();
38
- expect(error.stack).toContain('OnboardingError');
39
- });
40
- });
41
-
42
- describe('DockerNotInstalledError', () => {
43
- it('should have correct message and troubleshooting', () => {
44
- const error = new DockerNotInstalledError();
45
-
46
- expect(error.message).toContain('not installed');
47
- expect(error.troubleshooting).toContain('https://docs.docker.com/get-docker/');
48
- expect(error.name).toBe('DockerNotInstalledError');
49
- });
50
- });
51
-
52
- describe('DockerNotRunningError', () => {
53
- it('should have correct message and troubleshooting', () => {
54
- const error = new DockerNotRunningError();
55
-
56
- expect(error.message).toContain('not running');
57
- expect(error.troubleshooting).toContain('Docker daemon');
58
- expect(error.name).toBe('DockerNotRunningError');
59
- });
60
- });
61
-
62
- describe('DockerPermissionError', () => {
63
- it('should have correct message and troubleshooting', () => {
64
- const error = new DockerPermissionError();
65
-
66
- expect(error.message).toContain('Permission denied');
67
- expect(error.troubleshooting).toContain('usermod');
68
- expect(error.name).toBe('DockerPermissionError');
69
- });
70
- });
71
-
72
- describe('DockerComposeError', () => {
73
- it('should include operation in message', () => {
74
- const error = new DockerComposeError('start services');
75
-
76
- expect(error.message).toContain('start services');
77
- expect(error.name).toBe('DockerComposeError');
78
- });
79
-
80
- it('should use custom troubleshooting when provided', () => {
81
- const error = new DockerComposeError('pull images', 'Check network connection');
82
-
83
- expect(error.troubleshooting).toBe('Check network connection');
84
- });
85
-
86
- it('should have default troubleshooting', () => {
87
- const error = new DockerComposeError('operation');
88
-
89
- expect(error.troubleshooting).toContain('docker compose logs');
90
- expect(error.troubleshooting).toContain('docker-compose logs');
91
- });
92
- });
93
-
94
- describe('FileSystemError', () => {
95
- it('should include path in error', () => {
96
- const error = new FileSystemError(
97
- 'Cannot write',
98
- '/path/to/file',
99
- 'Check permissions'
100
- );
101
-
102
- expect(error.path).toBe('/path/to/file');
103
- expect(error.troubleshooting).toBe('Check permissions');
104
- });
105
- });
106
-
107
- describe('DirectoryNotWritableError', () => {
108
- it('should include path in message and error', () => {
109
- const error = new DirectoryNotWritableError('/opt/app');
110
-
111
- expect(error.message).toContain('/opt/app');
112
- expect(error.path).toBe('/opt/app');
113
- expect(error.troubleshooting).toContain('permissions');
114
- });
115
- });
116
-
117
- describe('isOnboardingError', () => {
118
- it('should return true for OnboardingError instances', () => {
119
- const error = new OnboardingError('CODE', 'message');
120
-
121
- expect(isOnboardingError(error)).toBe(true);
122
- });
123
-
124
- it('should return true for subclasses of OnboardingError', () => {
125
- const error = new DockerNotInstalledError();
126
-
127
- expect(isOnboardingError(error)).toBe(true);
128
- });
129
-
130
- it('should return false for regular Error', () => {
131
- const error = new Error('regular error');
132
-
133
- expect(isOnboardingError(error)).toBe(false);
134
- });
135
-
136
- it('should return false for non-error values', () => {
137
- expect(isOnboardingError('string')).toBe(false);
138
- expect(isOnboardingError(null)).toBe(false);
139
- expect(isOnboardingError(undefined)).toBe(false);
140
- expect(isOnboardingError({})).toBe(false);
141
- });
142
- });
143
-
144
- describe('formatErrorForUser', () => {
145
- it('should format OnboardingError with troubleshooting', () => {
146
- const error = new OnboardingError(
147
- 'TEST',
148
- 'Something went wrong',
149
- 'Try this fix'
150
- );
151
-
152
- const formatted = formatErrorForUser(error);
153
-
154
- expect(formatted).toContain('❌ Something went wrong');
155
- expect(formatted).toContain('💡 Troubleshooting:');
156
- expect(formatted).toContain('Try this fix');
157
- });
158
-
159
- it('should format OnboardingError without troubleshooting', () => {
160
- const error = new OnboardingError('TEST', 'Something went wrong');
161
-
162
- const formatted = formatErrorForUser(error);
163
-
164
- expect(formatted).toBe('❌ Something went wrong');
165
- expect(formatted).not.toContain('Troubleshooting');
166
- });
167
-
168
- it('should format regular Error', () => {
169
- const error = new Error('Regular error message');
170
-
171
- const formatted = formatErrorForUser(error);
172
-
173
- expect(formatted).toContain('Unexpected error');
174
- expect(formatted).toContain('Regular error message');
175
- });
176
-
177
- it('should handle non-Error values', () => {
178
- const formatted = formatErrorForUser('some string');
179
-
180
- expect(formatted).toBe('❌ An unknown error occurred');
181
- });
182
-
183
- it('should handle null/undefined', () => {
184
- expect(formatErrorForUser(null)).toBe('❌ An unknown error occurred');
185
- expect(formatErrorForUser(undefined)).toBe('❌ An unknown error occurred');
186
- });
187
- });
188
- });
@@ -1,56 +0,0 @@
1
- import { describe, it, expect, vi, beforeEach } from 'vitest';
2
-
3
- // Mock @clack/prompts
4
- vi.mock('@clack/prompts', () => ({
5
- multiselect: vi.fn(),
6
- spinner: vi.fn(() => ({
7
- start: vi.fn(),
8
- stop: vi.fn(),
9
- error: vi.fn(),
10
- message: vi.fn(),
11
- })),
12
- }));
13
-
14
- // Mock the ui.js module
15
- vi.mock('../ui.js', () => ({
16
- handleCancel: vi.fn(),
17
- }));
18
-
19
- import { multiselect } from '@clack/prompts';
20
- import { promptInfraServicesWithBasePath } from '../infra.js';
21
-
22
- describe('promptInfraServicesWithBasePath', () => {
23
- beforeEach(() => {
24
- vi.clearAllMocks();
25
- });
26
-
27
- it('includes nginx when allowNginx is true', async () => {
28
- const mockMultiselect = vi.mocked(multiselect);
29
- mockMultiselect.mockResolvedValue(['mongo', 'redis', 'nginx']);
30
-
31
- const result = await promptInfraServicesWithBasePath({ allowNginx: true, defaultNginx: true });
32
-
33
- // Should include nginx in options
34
- const callArgs = mockMultiselect.mock.calls[0][0] as any;
35
- const values = callArgs.options.map((o: any) => o.value);
36
- expect(values).toContain('nginx');
37
- expect(callArgs.initialValues).toContain('nginx');
38
- expect(callArgs.initialValues).not.toContain('kafka-ui');
39
-
40
- expect(result).toContain('nginx');
41
- });
42
-
43
- it('excludes nginx when allowNginx is false', async () => {
44
- const mockMultiselect = vi.mocked(multiselect);
45
- mockMultiselect.mockResolvedValue(['mongo', 'redis']);
46
-
47
- const result = await promptInfraServicesWithBasePath({ allowNginx: false });
48
-
49
- // Should NOT include nginx in options
50
- const callArgs = mockMultiselect.mock.calls[0][0] as any;
51
- const values = callArgs.options.map((o: any) => o.value);
52
- expect(values).not.toContain('nginx');
53
-
54
- expect(result).not.toContain('nginx');
55
- });
56
- });
@@ -1,30 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
- import { buildAppComposeContent } from '../infra.js';
3
-
4
- describe('infra app compose generation', () => {
5
- it('does not include nginx when disabled', () => {
6
- const compose = buildAppComposeContent(false);
7
- expect(compose).not.toContain(' nginx:');
8
- });
9
-
10
- it('includes nginx service when enabled', () => {
11
- const compose = buildAppComposeContent(true);
12
- expect(compose).toContain(' nginx:');
13
- expect(compose).toContain('./nginx.conf:/etc/nginx/conf.d/default.conf:ro');
14
- });
15
-
16
- it('does not include certbot services when ssl is disabled', () => {
17
- const compose = buildAppComposeContent(true, { includeSsl: false });
18
- expect(compose).not.toContain(' certbot:');
19
- expect(compose).not.toContain(' certbot-renew:');
20
- });
21
-
22
- it('includes certbot services and volumes when ssl is enabled', () => {
23
- const compose = buildAppComposeContent(false, { includeSsl: true });
24
- expect(compose).toContain(' nginx:');
25
- expect(compose).toContain(' certbot:');
26
- expect(compose).toContain(' certbot-renew:');
27
- expect(compose).toContain('certbot-etc:');
28
- expect(compose).toContain('certbot-www:');
29
- });
30
- });
@@ -1,142 +0,0 @@
1
- import { describe, it, expect, beforeEach, afterEach } from 'vitest';
2
- import { promises as fs } from 'fs';
3
- import os from 'os';
4
- import path from 'path';
5
- import {
6
- fileExists,
7
- writeFile,
8
- readFile,
9
- appendFile,
10
- } from '../utils.js';
11
-
12
- describe('utils - file operations', () => {
13
- let tempDir: string;
14
-
15
- beforeEach(async () => {
16
- // Create a temporary directory for tests
17
- tempDir = path.join(os.tmpdir(), `onboard-test-${Date.now()}`);
18
- await fs.mkdir(tempDir, { recursive: true });
19
- });
20
-
21
- afterEach(async () => {
22
- // Clean up temporary directory
23
- try {
24
- await fs.rm(tempDir, { recursive: true, force: true });
25
- } catch (error) {
26
- // Ignore cleanup errors
27
- }
28
- });
29
-
30
- describe('fileExists', () => {
31
- it('should return true for existing file', async () => {
32
- const filePath = path.join(tempDir, 'test.txt');
33
- await fs.writeFile(filePath, 'content');
34
-
35
- expect(await fileExists(filePath)).toBe(true);
36
- });
37
-
38
- it('should return false for non-existing file', async () => {
39
- const filePath = path.join(tempDir, 'nonexistent.txt');
40
-
41
- expect(await fileExists(filePath)).toBe(false);
42
- });
43
-
44
- it('should return true for existing directory', async () => {
45
- expect(await fileExists(tempDir)).toBe(true);
46
- });
47
- });
48
-
49
- describe('writeFile', () => {
50
- it('should write content to file', async () => {
51
- const filePath = path.join(tempDir, 'write-test.txt');
52
- const content = 'Hello, World!';
53
-
54
- await writeFile(filePath, content);
55
-
56
- const written = await fs.readFile(filePath, 'utf-8');
57
- expect(written).toBe(content);
58
- });
59
-
60
- it('should create parent directories if they do not exist', async () => {
61
- const filePath = path.join(tempDir, 'nested', 'dir', 'file.txt');
62
- const content = 'Nested content';
63
-
64
- await writeFile(filePath, content);
65
-
66
- expect(await fileExists(filePath)).toBe(true);
67
- const written = await fs.readFile(filePath, 'utf-8');
68
- expect(written).toBe(content);
69
- });
70
-
71
- it('should overwrite existing file', async () => {
72
- const filePath = path.join(tempDir, 'overwrite.txt');
73
-
74
- await fs.writeFile(filePath, 'original');
75
- await writeFile(filePath, 'new content');
76
-
77
- const written = await fs.readFile(filePath, 'utf-8');
78
- expect(written).toBe('new content');
79
- });
80
- });
81
-
82
- describe('readFile', () => {
83
- it('should read file content', async () => {
84
- const filePath = path.join(tempDir, 'read-test.txt');
85
- const content = 'File content to read';
86
- await fs.writeFile(filePath, content);
87
-
88
- const read = await readFile(filePath);
89
-
90
- expect(read).toBe(content);
91
- });
92
-
93
- it('should throw error for non-existing file', async () => {
94
- const filePath = path.join(tempDir, 'nonexistent.txt');
95
-
96
- await expect(readFile(filePath)).rejects.toThrow();
97
- });
98
-
99
- it('should handle UTF-8 content', async () => {
100
- const filePath = path.join(tempDir, 'utf8.txt');
101
- const content = 'Hello 世界 🌍';
102
- await fs.writeFile(filePath, content);
103
-
104
- const read = await readFile(filePath);
105
-
106
- expect(read).toBe(content);
107
- });
108
- });
109
-
110
- describe('appendFile', () => {
111
- it('should append content to existing file', async () => {
112
- const filePath = path.join(tempDir, 'append-test.txt');
113
- await fs.writeFile(filePath, 'Line 1\n');
114
-
115
- await appendFile(filePath, 'Line 2\n');
116
-
117
- const content = await fs.readFile(filePath, 'utf-8');
118
- expect(content).toBe('Line 1\nLine 2\n');
119
- });
120
-
121
- it('should create file if it does not exist', async () => {
122
- const filePath = path.join(tempDir, 'new-append.txt');
123
-
124
- await appendFile(filePath, 'First line\n');
125
-
126
- expect(await fileExists(filePath)).toBe(true);
127
- const content = await fs.readFile(filePath, 'utf-8');
128
- expect(content).toBe('First line\n');
129
- });
130
-
131
- it('should handle multiple appends', async () => {
132
- const filePath = path.join(tempDir, 'multi-append.txt');
133
-
134
- await appendFile(filePath, 'Line 1\n');
135
- await appendFile(filePath, 'Line 2\n');
136
- await appendFile(filePath, 'Line 3\n');
137
-
138
- const content = await fs.readFile(filePath, 'utf-8');
139
- expect(content).toBe('Line 1\nLine 2\nLine 3\n');
140
- });
141
- });
142
- });
@@ -1,221 +0,0 @@
1
- import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2
- import {
3
- checkDockerInstalled,
4
- checkDockerRunning,
5
- detectOS,
6
- validatePrerequisites,
7
- validateEnvValue,
8
- validatePublicDomain,
9
- validateEmailAddress
10
- } from '../validators.js';
11
- import {
12
- DockerNotInstalledError,
13
- DockerNotRunningError,
14
- DockerPermissionError
15
- } from '../types/errors.js';
16
-
17
- // Mock execa
18
- vi.mock('execa', () => ({
19
- execa: vi.fn(),
20
- }));
21
-
22
- // Mock utils
23
- vi.mock('../utils.js', () => ({
24
- logError: vi.fn(),
25
- logSuccess: vi.fn(),
26
- logWarning: vi.fn(),
27
- }));
28
-
29
- import { execa } from 'execa';
30
-
31
- describe('validators', () => {
32
- beforeEach(() => {
33
- vi.clearAllMocks();
34
- });
35
-
36
- describe('checkDockerInstalled', () => {
37
- it('should not throw when docker is installed', async () => {
38
- vi.mocked(execa).mockResolvedValueOnce({
39
- stdout: 'Docker version 24.0.0',
40
- stderr: '',
41
- } as any);
42
-
43
- await expect(checkDockerInstalled()).resolves.not.toThrow();
44
- });
45
-
46
- it('should throw DockerNotInstalledError when docker is not installed', async () => {
47
- vi.mocked(execa).mockRejectedValueOnce(new Error('Command not found'));
48
-
49
- await expect(checkDockerInstalled()).rejects.toThrow(DockerNotInstalledError);
50
- });
51
- });
52
-
53
- describe('checkDockerRunning', () => {
54
- it('should not throw when docker daemon is running', async () => {
55
- vi.mocked(execa).mockResolvedValueOnce({
56
- stdout: 'CONTAINER ID IMAGE',
57
- stderr: '',
58
- } as any);
59
-
60
- await expect(checkDockerRunning()).resolves.not.toThrow();
61
- });
62
-
63
- it('should throw DockerPermissionError on permission denied', async () => {
64
- const error = new Error('permission denied while trying to connect');
65
- vi.mocked(execa).mockRejectedValueOnce(error);
66
-
67
- await expect(checkDockerRunning()).rejects.toThrow(DockerPermissionError);
68
- });
69
-
70
- it('should throw DockerNotRunningError when daemon is not running', async () => {
71
- const error = new Error('Cannot connect to the Docker daemon');
72
- vi.mocked(execa).mockRejectedValueOnce(error);
73
-
74
- await expect(checkDockerRunning()).rejects.toThrow(DockerNotRunningError);
75
- });
76
-
77
- it('should throw DockerNotRunningError on generic docker error', async () => {
78
- const error = new Error('Some other docker error');
79
- vi.mocked(execa).mockRejectedValueOnce(error);
80
-
81
- await expect(checkDockerRunning()).rejects.toThrow(DockerNotRunningError);
82
- });
83
- });
84
-
85
- describe('detectOS', () => {
86
- const originalPlatform = process.platform;
87
-
88
- afterEach(() => {
89
- Object.defineProperty(process, 'platform', {
90
- value: originalPlatform
91
- });
92
- });
93
-
94
- it('should detect Windows', () => {
95
- Object.defineProperty(process, 'platform', {
96
- value: 'win32'
97
- });
98
- expect(detectOS()).toBe('windows');
99
- });
100
-
101
- it('should detect macOS', () => {
102
- Object.defineProperty(process, 'platform', {
103
- value: 'darwin'
104
- });
105
- expect(detectOS()).toBe('darwin');
106
- });
107
-
108
- it('should detect Linux', () => {
109
- Object.defineProperty(process, 'platform', {
110
- value: 'linux'
111
- });
112
- expect(detectOS()).toBe('linux');
113
- });
114
-
115
- it('should default to Linux for unknown platforms', () => {
116
- Object.defineProperty(process, 'platform', {
117
- value: 'freebsd'
118
- });
119
- expect(detectOS()).toBe('linux');
120
- });
121
- });
122
-
123
- describe('validateEnvValue', () => {
124
- describe('URL validation', () => {
125
- it('should reject empty MongoDB URI', () => {
126
- expect(validateEnvValue('MONGO_URI', '')).toBe(false);
127
- });
128
-
129
- it('should reject MongoDB URI without proper format', () => {
130
- expect(validateEnvValue('MONGO_URI', 'localhost:27017')).toBe(false);
131
- });
132
-
133
- it('should accept valid MongoDB URI', () => {
134
- expect(validateEnvValue('MONGO_URI', 'mongodb://localhost:27017')).toBe(true);
135
- });
136
-
137
- it('should reject Redis URL without proper format', () => {
138
- expect(validateEnvValue('REDIS_URL', 'localhost:6379')).toBe(false);
139
- });
140
-
141
- it('should accept valid Redis URL', () => {
142
- expect(validateEnvValue('REDIS_URL', 'redis://localhost:6379')).toBe(true);
143
- });
144
- });
145
-
146
- describe('Port validation', () => {
147
- it('should reject negative ports', () => {
148
- expect(validateEnvValue('PORT', '-1')).toBe(false);
149
- });
150
-
151
- it('should reject zero port', () => {
152
- expect(validateEnvValue('PORT', '0')).toBe(false);
153
- });
154
-
155
- it('should reject ports > 65535', () => {
156
- expect(validateEnvValue('PORT', '65536')).toBe(false);
157
- });
158
-
159
- it('should accept valid ports', () => {
160
- expect(validateEnvValue('PORT', '3000')).toBe(true);
161
- expect(validateEnvValue('API_PORT', '8080')).toBe(true);
162
- });
163
-
164
- it('should reject non-numeric ports', () => {
165
- expect(validateEnvValue('PORT', 'abc')).toBe(false);
166
- });
167
- });
168
-
169
- describe('Security fields validation', () => {
170
- it('should reject short API keys', () => {
171
- expect(validateEnvValue('API_KEY', 'short')).toBe(false);
172
- });
173
-
174
- it('should reject short passwords', () => {
175
- expect(validateEnvValue('PASSWORD', '1234567')).toBe(false);
176
- });
177
-
178
- it('should reject short secrets', () => {
179
- expect(validateEnvValue('AUTH_SECRET', 'abc')).toBe(false);
180
- });
181
-
182
- it('should accept API keys with 8+ characters', () => {
183
- expect(validateEnvValue('API_KEY', 'verylongapikey123')).toBe(true);
184
- });
185
-
186
- it('should accept passwords with 8+ characters', () => {
187
- expect(validateEnvValue('PASSWORD', 'password123')).toBe(true);
188
- });
189
- });
190
-
191
- describe('General validation', () => {
192
- it('should accept valid non-special values', () => {
193
- expect(validateEnvValue('SOME_VAR', 'some-value')).toBe(true);
194
- });
195
- });
196
- });
197
-
198
- describe('validatePublicDomain', () => {
199
- it('accepts valid public domains', () => {
200
- expect(validatePublicDomain('example.com')).toBe(true);
201
- expect(validatePublicDomain('app.example.com')).toBe(true);
202
- });
203
-
204
- it('rejects urls or malformed values', () => {
205
- expect(validatePublicDomain('https://example.com')).not.toBe(true);
206
- expect(validatePublicDomain('example')).not.toBe(true);
207
- expect(validatePublicDomain('example.com/path')).not.toBe(true);
208
- });
209
- });
210
-
211
- describe('validateEmailAddress', () => {
212
- it('accepts valid email addresses', () => {
213
- expect(validateEmailAddress('admin@example.com')).toBe(true);
214
- });
215
-
216
- it('rejects invalid email values', () => {
217
- expect(validateEmailAddress('admin@')).not.toBe(true);
218
- expect(validateEmailAddress('not-an-email')).not.toBe(true);
219
- });
220
- });
221
- });