1
+ const { spawn } = require ( 'child_process' ) ;
2
+ const path = require ( 'path' ) ;
3
+ const cli = require ( './cli' ) ;
4
+
5
+ describe ( 'CLI Commands' , ( ) => {
6
+ const indexPath = path . join ( __dirname , 'index.js' ) ;
7
+
8
+ const runCommand = ( args ) => {
9
+ return new Promise ( ( resolve , reject ) => {
10
+ const process = spawn ( 'node' , [ indexPath , ...args ] ) ;
11
+ let stdout = '' ;
12
+ let stderr = '' ;
13
+
14
+ process . stdout . on ( 'data' , ( data ) => {
15
+ stdout += data . toString ( ) ;
16
+ } ) ;
17
+
18
+ process . stderr . on ( 'data' , ( data ) => {
19
+ stderr += data . toString ( ) ;
20
+ } ) ;
21
+
22
+ process . on ( 'close' , ( code ) => {
23
+ resolve ( { code, stdout, stderr } ) ;
24
+ } ) ;
25
+
26
+ process . on ( 'error' , ( error ) => {
27
+ reject ( error ) ;
28
+ } ) ;
29
+ } ) ;
30
+ } ;
31
+
32
+ describe ( 'update-cloud-profile command' , ( ) => {
33
+ it ( 'should show error when required options are missing' , async ( ) => {
34
+ const result = await runCommand ( [ 'update-cloud-profile' ] ) ;
35
+ expect ( result . code ) . not . toBe ( 0 ) ;
36
+ expect ( result . stderr ) . toContain ( 'required option' ) ;
37
+ } ) ;
38
+
39
+ it ( 'should show help with --help flag' , async ( ) => {
40
+ const result = await runCommand ( [ 'update-cloud-profile' , '--help' ] ) ;
41
+ expect ( result . code ) . toBe ( 0 ) ;
42
+ expect ( result . stdout ) . toContain ( '--token' ) ;
43
+ expect ( result . stdout ) . toContain ( '--server' ) ;
44
+ expect ( result . stdout ) . toContain ( '--image' ) ;
45
+ expect ( result . stdout ) . toContain ( '--cloudprofile' ) ;
46
+ expect ( result . stdout ) . toContain ( '--agentprefix' ) ;
47
+ expect ( result . stdout ) . toContain ( '--dryrun' ) ;
48
+ } ) ;
49
+
50
+ it ( 'should validate required parameters' , async ( ) => {
51
+ const result = await runCommand ( [
52
+ 'update-cloud-profile' ,
53
+ '--token' , 'test-token'
54
+ ] ) ;
55
+ expect ( result . code ) . not . toBe ( 0 ) ;
56
+ expect ( result . stderr ) . toContain ( 'required option' ) ;
57
+ } ) ;
58
+ } ) ;
59
+
60
+ describe ( 'remove-disabled-agents command' , ( ) => {
61
+ it ( 'should show error when required options are missing' , async ( ) => {
62
+ const result = await runCommand ( [ 'remove-disabled-agents' ] ) ;
63
+ expect ( result . code ) . not . toBe ( 0 ) ;
64
+ expect ( result . stderr ) . toContain ( 'required option' ) ;
65
+ } ) ;
66
+
67
+ it ( 'should show help with --help flag' , async ( ) => {
68
+ const result = await runCommand ( [ 'remove-disabled-agents' , '--help' ] ) ;
69
+ expect ( result . code ) . toBe ( 0 ) ;
70
+ expect ( result . stdout ) . toContain ( '--token' ) ;
71
+ expect ( result . stdout ) . toContain ( '--server' ) ;
72
+ expect ( result . stdout ) . toContain ( '--dryrun' ) ;
73
+ } ) ;
74
+
75
+ it ( 'should validate required parameters' , async ( ) => {
76
+ const result = await runCommand ( [
77
+ 'remove-disabled-agents' ,
78
+ '--token' , 'test-token'
79
+ ] ) ;
80
+ expect ( result . code ) . not . toBe ( 0 ) ;
81
+ expect ( result . stderr ) . toContain ( 'required option' ) ;
82
+ } ) ;
83
+ } ) ;
84
+
85
+ describe ( 'General CLI' , ( ) => {
86
+ it ( 'should show version with --version flag' , async ( ) => {
87
+ const result = await runCommand ( [ '--version' ] ) ;
88
+ expect ( result . code ) . toBe ( 0 ) ;
89
+ expect ( result . stdout ) . toContain ( '1.0.0' ) ;
90
+ } ) ;
91
+
92
+ it ( 'should show help with --help flag' , async ( ) => {
93
+ const result = await runCommand ( [ '--help' ] ) ;
94
+ expect ( result . code ) . toBe ( 0 ) ;
95
+ expect ( result . stdout ) . toContain ( 'TeamCity Cloud Agent Updater' ) ;
96
+ expect ( result . stdout ) . toContain ( 'update-cloud-profile' ) ;
97
+ expect ( result . stdout ) . toContain ( 'remove-disabled-agents' ) ;
98
+ } ) ;
99
+
100
+ it ( 'should use update-cloud-profile as default command' , async ( ) => {
101
+ const result = await runCommand ( [ ] ) ;
102
+ expect ( result . code ) . not . toBe ( 0 ) ;
103
+ // Should show error for missing required options from update-cloud-profile
104
+ expect ( result . stderr ) . toContain ( 'required option' ) ;
105
+ } ) ;
106
+ } ) ;
107
+ } ) ;
0 commit comments