Skip to content

Commit 695ebf7

Browse files
CopilotApollon77
andcommitted
Finalize CLI testing implementation with linting fixes
Co-authored-by: Apollon77 <11976694+Apollon77@users.noreply.github.com>
1 parent e8ffa61 commit 695ebf7

File tree

3 files changed

+74
-62
lines changed

3 files changed

+74
-62
lines changed

packages/cli/test/adapter-lifecycle.test.ts

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,73 +18,79 @@ const testConfig = {
1818
system: {
1919
memoryLimitMB: 0,
2020
hostname: '',
21-
instanceStartInterval: 2000
21+
instanceStartInterval: 2000,
2222
},
2323
objects: {
2424
type: 'file',
2525
host: '127.0.0.1',
2626
port: 19011,
27-
dataDir: path.join(testDir, 'objects')
27+
dataDir: path.join(testDir, 'objects'),
2828
},
2929
states: {
30-
type: 'file',
30+
type: 'file',
3131
host: '127.0.0.1',
3232
port: 19010,
33-
dataDir: path.join(testDir, 'states')
33+
dataDir: path.join(testDir, 'states'),
3434
},
3535
log: {
3636
level: 'warn',
3737
noStdout: true,
3838
transport: {
3939
file1: {
4040
type: 'file',
41-
enabled: false
42-
}
43-
}
41+
enabled: false,
42+
},
43+
},
4444
},
4545
dataDir: testDir,
46-
plugins: {}
46+
plugins: {},
4747
};
4848

4949
/**
5050
* Helper function to run CLI commands for lifecycle tests
51+
*
52+
* @param args - CLI command arguments to pass
53+
* @param timeout - Command timeout in milliseconds
5154
*/
52-
function runCliCommand(args: string[], timeout = 45000): Promise<{ exitCode: number | null; stdout: string; stderr: string }> {
53-
return new Promise((resolve) => {
55+
function runCliCommand(
56+
args: string[],
57+
timeout = 45000,
58+
): Promise<{ exitCode: number | null; stdout: string; stderr: string }> {
59+
return new Promise(resolve => {
5460
const nodeExecutable = process.execPath;
5561
const cliScript = path.join(thisDir, '../../controller/iobroker.js');
56-
62+
5763
// Set environment variable for test config
5864
const env = { ...process.env, IOB_CONF_FILE: testConfigPath };
59-
65+
6066
const child = spawn(nodeExecutable, [cliScript, ...args], {
6167
env,
6268
cwd: path.join(thisDir, '../..'),
63-
stdio: ['pipe', 'pipe', 'pipe']
69+
stdio: ['pipe', 'pipe', 'pipe'],
6470
});
6571

6672
let stdout = '';
6773
let stderr = '';
6874

69-
child.stdout?.on('data', (data) => {
75+
child.stdout?.on('data', data => {
7076
stdout += data.toString();
7177
});
7278

73-
child.stderr?.on('data', (data) => {
79+
child.stderr?.on('data', data => {
7480
stderr += data.toString();
7581
});
7682

7783
const timeoutId = setTimeout(() => {
7884
child.kill('SIGTERM');
79-
resolve({ exitCode: -1, stdout, stderr: stderr + '\nTIMEOUT' });
85+
resolve({ exitCode: -1, stdout, stderr: `${stderr}\nTIMEOUT` });
8086
}, timeout);
8187

82-
child.on('close', (exitCode) => {
88+
child.on('close', exitCode => {
8389
clearTimeout(timeoutId);
8490
resolve({ exitCode, stdout, stderr });
8591
});
8692

87-
child.on('error', (error) => {
93+
child.on('error', error => {
8894
clearTimeout(timeoutId);
8995
resolve({ exitCode: -1, stdout, stderr: stderr + error.message });
9096
});
@@ -133,7 +139,7 @@ describe('Adapter Lifecycle Tests', function () {
133139

134140
it('should list adapters and show installed adapters', async function () {
135141
const result = await runCliCommand(['list', 'adapters']);
136-
142+
137143
expect(result.exitCode).to.equal(0);
138144
// Should not timeout or crash
139145
expect(result.stderr).to.not.include('TIMEOUT');
@@ -142,14 +148,14 @@ describe('Adapter Lifecycle Tests', function () {
142148
it('should create an adapter instance (if adapter is available)', async function () {
143149
// First check if adapter exists
144150
const listResult = await runCliCommand(['list', 'adapters']);
145-
151+
146152
if (listResult.stdout.includes(testAdapter)) {
147153
// Try to create an instance
148154
const result = await runCliCommand(['add', testAdapter, '--enabled', 'false']);
149-
155+
150156
// The command should complete without crashing
151157
expect(result.stderr).to.not.include('TIMEOUT');
152-
158+
153159
// If successful, should show in instance list
154160
if (result.exitCode === 0) {
155161
const instancesResult = await runCliCommand(['list', 'instances']);
@@ -165,26 +171,26 @@ describe('Adapter Lifecycle Tests', function () {
165171

166172
it('should list instances and show created instances', async function () {
167173
const result = await runCliCommand(['list', 'instances']);
168-
174+
169175
expect(result.exitCode).to.equal(0);
170176
expect(result.stderr).to.not.include('TIMEOUT');
171177
});
172178

173179
it('should delete adapter instance (if one exists)', async function () {
174180
// List existing instances first
175181
const listResult = await runCliCommand(['list', 'instances']);
176-
182+
177183
if (listResult.stdout.includes(`${testAdapter}.`)) {
178184
// Extract instance number from output (assuming format adapter.X)
179185
const instanceMatch = listResult.stdout.match(new RegExp(`${testAdapter}\\.(\\d+)`));
180-
186+
181187
if (instanceMatch) {
182188
const instanceNum = instanceMatch[1];
183189
const result = await runCliCommand(['del', `${testAdapter}.${instanceNum}`]);
184-
190+
185191
// Should complete without crashing
186192
expect(result.stderr).to.not.include('TIMEOUT');
187-
193+
188194
// Verify deletion by listing instances again
189195
const afterDeleteResult = await runCliCommand(['list', 'instances']);
190196
expect(afterDeleteResult.exitCode).to.equal(0);
@@ -197,7 +203,7 @@ describe('Adapter Lifecycle Tests', function () {
197203

198204
it('should handle attempt to delete non-existent instance gracefully', async function () {
199205
const result = await runCliCommand(['del', 'non-existent-adapter.99']);
200-
206+
201207
// Should not crash or timeout
202208
expect(result.stderr).to.not.include('TIMEOUT');
203209
// Should return with some error indication but not crash
@@ -206,7 +212,7 @@ describe('Adapter Lifecycle Tests', function () {
206212

207213
it('should handle attempt to add non-existent adapter gracefully', async function () {
208214
const result = await runCliCommand(['add', 'definitely-non-existent-adapter-xyz123']);
209-
215+
210216
// Should not crash or timeout
211217
expect(result.stderr).to.not.include('TIMEOUT');
212218
// Should return error code for non-existent adapter
@@ -232,4 +238,4 @@ describe('Adapter Lifecycle Tests', function () {
232238
console.log(`States directory contains ${stateFiles.length} files`);
233239
});
234240
});
235-
});
241+
});

packages/cli/test/cli-commands.test.ts

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,73 +18,79 @@ const testConfig = {
1818
system: {
1919
memoryLimitMB: 0,
2020
hostname: '',
21-
instanceStartInterval: 2000
21+
instanceStartInterval: 2000,
2222
},
2323
objects: {
2424
type: 'file',
2525
host: '127.0.0.1',
2626
port: 19001,
27-
dataDir: path.join(testDir, 'objects')
27+
dataDir: path.join(testDir, 'objects'),
2828
},
2929
states: {
30-
type: 'file',
30+
type: 'file',
3131
host: '127.0.0.1',
3232
port: 19000,
33-
dataDir: path.join(testDir, 'states')
33+
dataDir: path.join(testDir, 'states'),
3434
},
3535
log: {
3636
level: 'warn',
3737
noStdout: true,
3838
transport: {
3939
file1: {
4040
type: 'file',
41-
enabled: false
42-
}
43-
}
41+
enabled: false,
42+
},
43+
},
4444
},
4545
dataDir: testDir,
46-
plugins: {}
46+
plugins: {},
4747
};
4848

4949
/**
5050
* Helper function to run CLI commands
51+
*
52+
* @param args - CLI command arguments to pass
53+
* @param timeout - Command timeout in milliseconds
5154
*/
52-
function runCliCommand(args: string[], timeout = 30000): Promise<{ exitCode: number | null; stdout: string; stderr: string }> {
53-
return new Promise((resolve) => {
55+
function runCliCommand(
56+
args: string[],
57+
timeout = 30000,
58+
): Promise<{ exitCode: number | null; stdout: string; stderr: string }> {
59+
return new Promise(resolve => {
5460
const nodeExecutable = process.execPath;
5561
const cliScript = path.join(thisDir, '../../controller/iobroker.js');
56-
62+
5763
// Set environment variable for test config
5864
const env = { ...process.env, IOB_CONF_FILE: testConfigPath };
59-
65+
6066
const child = spawn(nodeExecutable, [cliScript, ...args], {
6167
env,
6268
cwd: path.join(thisDir, '../..'),
63-
stdio: ['pipe', 'pipe', 'pipe']
69+
stdio: ['pipe', 'pipe', 'pipe'],
6470
});
6571

6672
let stdout = '';
6773
let stderr = '';
6874

69-
child.stdout?.on('data', (data) => {
75+
child.stdout?.on('data', data => {
7076
stdout += data.toString();
7177
});
7278

73-
child.stderr?.on('data', (data) => {
79+
child.stderr?.on('data', data => {
7480
stderr += data.toString();
7581
});
7682

7783
const timeoutId = setTimeout(() => {
7884
child.kill('SIGTERM');
79-
resolve({ exitCode: -1, stdout, stderr: stderr + '\nTIMEOUT' });
85+
resolve({ exitCode: -1, stdout, stderr: `${stderr}\nTIMEOUT` });
8086
}, timeout);
8187

82-
child.on('close', (exitCode) => {
88+
child.on('close', exitCode => {
8389
clearTimeout(timeoutId);
8490
resolve({ exitCode, stdout, stderr });
8591
});
8692

87-
child.on('error', (error) => {
93+
child.on('error', error => {
8894
clearTimeout(timeoutId);
8995
resolve({ exitCode: -1, stdout, stderr: stderr + error.message });
9096
});
@@ -117,35 +123,35 @@ describe('CLI Commands Integration Tests', function () {
117123
describe('Basic CLI Command Functionality', function () {
118124
it('should handle version command', async function () {
119125
const result = await runCliCommand(['version']);
120-
126+
121127
expect(result.exitCode).to.equal(0);
122128
expect(result.stdout).to.match(/\d+\.\d+\.\d+/); // Should contain version number
123129
});
124130

125131
it('should handle help command', async function () {
126132
const result = await runCliCommand(['--help']);
127-
133+
128134
expect(result.exitCode).to.equal(0);
129135
expect(result.stdout).to.include('Commands:'); // Adjusted to match actual output
130136
});
131137

132138
it('should handle list adapters command', async function () {
133139
const result = await runCliCommand(['list', 'adapters']);
134-
140+
135141
// Should succeed even with empty adapter list
136142
expect(result.exitCode).to.equal(0);
137143
});
138144

139145
it('should handle list instances command', async function () {
140146
const result = await runCliCommand(['list', 'instances']);
141-
142-
// Should succeed even with empty instance list
147+
148+
// Should succeed even with empty instance list
143149
expect(result.exitCode).to.equal(0);
144150
});
145151

146152
it('should handle invalid command gracefully', async function () {
147153
const result = await runCliCommand(['invalid-command-xyz']);
148-
154+
149155
// Should return non-zero exit code for invalid command
150156
expect(result.exitCode).to.not.equal(0);
151157
});
@@ -175,17 +181,17 @@ describe('CLI Commands Integration Tests', function () {
175181
describe('Adapter Management Commands', function () {
176182
it('should handle list repositories command', async function () {
177183
const result = await runCliCommand(['repo', 'list']);
178-
184+
179185
expect(result.exitCode).to.equal(0);
180186
});
181187

182188
it('should handle info command for non-existent adapter gracefully', async function () {
183189
const result = await runCliCommand(['info', 'non-existent-adapter-xyz123']);
184-
190+
185191
// The command may succeed but should handle gracefully (not crash)
186192
// We don't require a specific exit code, just that it doesn't timeout or crash
187193
expect(result.stderr).to.not.include('TIMEOUT');
188194
expect(result.stderr).to.not.include('ENOENT');
189195
});
190196
});
191-
});
197+
});

packages/cli/test/testData/iobroker.json renamed to packages/cli/test/testDataLifecycle/iobroker.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
"objects": {
88
"type": "file",
99
"host": "127.0.0.1",
10-
"port": 19001,
11-
"dataDir": "/home/runner/work/ioBroker.js-controller/ioBroker.js-controller/packages/cli/test/testData/objects"
10+
"port": 19011,
11+
"dataDir": "/home/runner/work/ioBroker.js-controller/ioBroker.js-controller/packages/cli/test/testDataLifecycle/objects"
1212
},
1313
"states": {
1414
"type": "file",
1515
"host": "127.0.0.1",
16-
"port": 19000,
17-
"dataDir": "/home/runner/work/ioBroker.js-controller/ioBroker.js-controller/packages/cli/test/testData/states"
16+
"port": 19010,
17+
"dataDir": "/home/runner/work/ioBroker.js-controller/ioBroker.js-controller/packages/cli/test/testDataLifecycle/states"
1818
},
1919
"log": {
2020
"level": "warn",
@@ -26,6 +26,6 @@
2626
}
2727
}
2828
},
29-
"dataDir": "/home/runner/work/ioBroker.js-controller/ioBroker.js-controller/packages/cli/test/testData",
29+
"dataDir": "/home/runner/work/ioBroker.js-controller/ioBroker.js-controller/packages/cli/test/testDataLifecycle",
3030
"plugins": {}
3131
}

0 commit comments

Comments
 (0)