@simplens/onboard 1.0.9 → 1.0.11

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,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
- });
@@ -1,87 +0,0 @@
1
- /**
2
- * Application configuration constants
3
- */
4
-
5
- /**
6
- * Service port mappings
7
- */
8
- export const SERVICE_PORTS = {
9
- API: 3000,
10
- DASHBOARD: 3002,
11
- GRAFANA: 3001,
12
- KAFKA_UI: 8080,
13
- MONGO: 27017,
14
- KAFKA: 9092,
15
- REDIS: 6379,
16
- LOKI: 3100,
17
- } as const;
18
-
19
- /**
20
- * Health check configuration
21
- */
22
- export const HEALTH_CHECK = {
23
- MAX_RETRIES: 30,
24
- RETRY_DELAY_MS: 2000,
25
- TIMEOUT_MS: 60000,
26
- } as const;
27
-
28
- /**
29
- * Docker command timeouts
30
- */
31
- export const DOCKER_TIMEOUTS = {
32
- START_MS: 30000,
33
- STOP_MS: 15000,
34
- BUILD_MS: 300000,
35
- } as const;
36
-
37
- /**
38
- * Critical environment variables that always need user input
39
- */
40
- export const CRITICAL_ENV_KEYS = [
41
- 'NS_API_KEY',
42
- 'MONGO_URI',
43
- 'BROKERS',
44
- 'REDIS_URL',
45
- 'AUTH_SECRET',
46
- 'ADMIN_PASSWORD',
47
- 'CORE_VERSION',
48
- 'DASHBOARD_VERSION',
49
- ];
50
-
51
- /**
52
- * Validation constraints
53
- */
54
- export const VALIDATION = {
55
- MIN_PASSWORD_LENGTH: 8,
56
- MIN_API_KEY_LENGTH: 8,
57
- PORT_MIN: 1,
58
- PORT_MAX: 65535,
59
- } as const;
60
-
61
- /**
62
- * File paths
63
- */
64
- export const FILES = {
65
- DOCKER_COMPOSE_INFRA: 'docker-compose.infra.yaml',
66
- DOCKER_COMPOSE_APP: 'docker-compose.yaml',
67
- ENV_FILE: '.env',
68
- CONFIG_FILE: 'simplens.config.yaml',
69
- ERROR_LOG: 'onboard-error.log',
70
- } as const;
71
-
72
- /**
73
- * URL templates for service access
74
- */
75
- export function getServiceURL(service: keyof typeof SERVICE_PORTS, host: string = 'localhost'): string {
76
- return `http://${host}:${SERVICE_PORTS[service]}`;
77
- }
78
-
79
- /**
80
- * Docker compose file paths
81
- */
82
- export const DOCKER_COMPOSE_COMMANDS = {
83
- UP: ['up', '-d'],
84
- DOWN: ['down'],
85
- LOGS: ['logs', '-f'],
86
- PS: ['ps'],
87
- } as const;
@@ -1 +0,0 @@
1
- export * from './constants.js';