@@ -7,6 +7,7 @@ describe("getInputs()", () => {
77 if ( prop . startsWith ( "INPUT_" ) )
88 delete process . env [ prop ]
99 }
10+ delete process . env [ 'DISCORD_WEBHOOK' ]
1011
1112 // see action.yml for default values
1213 process . env [ 'INPUT_STATUS' ] = 'success'
@@ -16,135 +17,172 @@ describe("getInputs()", () => {
1617 process . env [ 'INPUT_NOPREFIX' ] = 'false'
1718 process . env [ 'INPUT_NODETAIL' ] = 'false'
1819 process . env [ 'INPUT_NOTIMESTAMP' ] = 'false'
19- process . env [ 'INPUT_ACKNOWEBHOOK' ] = 'false'
20+ process . env [ 'INPUT_ACK_NO_WEBHOOK' ] = 'false'
21+ } )
22+
23+ test ( "each field is mapped correctly" , ( ) => {
24+ // list all fields in Inputs type
25+ process . env [ 'INPUT_WEBHOOK' ] = 'https://env.webhook.invalid'
26+ process . env [ 'INPUT_STATUS' ] = 'failure'
27+ process . env [ 'INPUT_CONTENT' ] = 'content text'
28+ process . env [ 'INPUT_TITLE' ] = 'title text'
29+ process . env [ 'INPUT_DESCRIPTION' ] = 'description text'
30+ process . env [ 'INPUT_IMAGE' ] = 'https://example.com/image.png'
31+ process . env [ 'INPUT_COLOR' ] = '0xabcdef'
32+ process . env [ 'INPUT_URL' ] = 'https://example.com'
33+ process . env [ 'INPUT_USERNAME' ] = 'jest test'
34+ process . env [ 'INPUT_AVATAR_URL' ] = 'https://avatar.webhook.invalid'
35+ process . env [ 'INPUT_NODETAIL' ] = 'false'
36+ process . env [ 'INPUT_NOCONTEXT' ] = 'true'
37+ process . env [ 'INPUT_NOPREFIX' ] = 'true'
38+ process . env [ 'INPUT_NOTIMESTAMP' ] = 'true'
39+ process . env [ 'INPUT_ACK_NO_WEBHOOK' ] = 'true'
2040
21- // no defaults in action.yml, but need for passing validation
22- process . env [ 'DISCORD_WEBHOOK' ] = "https://env.webhook.invalid"
41+ const got = getInputs ( )
42+ expect ( got . webhooks ) . toStrictEqual ( [ 'https://env.webhook.invalid' ] )
43+ expect ( got . status ) . toBe ( 'failure' )
44+ expect ( got . content ) . toBe ( 'content text' )
45+ expect ( got . description ) . toBe ( 'description text' )
46+ expect ( got . title ) . toBe ( 'title text' )
47+ expect ( got . image ) . toBe ( 'https://example.com/image.png' )
48+ expect ( got . color ) . toBe ( 0xabcdef )
49+ expect ( got . url ) . toBe ( 'https://example.com' )
50+ expect ( got . username ) . toBe ( 'jest test' )
51+ expect ( got . avatar_url ) . toBe ( 'https://avatar.webhook.invalid' )
52+ expect ( got . nocontext ) . toBe ( true )
53+ expect ( got . noprefix ) . toBe ( true )
54+ expect ( got . notimestamp ) . toBe ( true )
55+ expect ( got . ack_no_webhook ) . toBe ( true )
2356 } )
2457
25- test ( "default" , ( ) => {
58+ test ( "multiple webhooks" , ( ) => {
59+ process . env [ 'INPUT_WEBHOOK' ] = '\nhttps://input.webhook.invalid\n\n\nhttps://input2.webhook.invalid\n\n\n'
2660 const got = getInputs ( )
27- expect ( got . noprefix ) . toBe ( false )
28- expect ( got . nocontext ) . toBe ( false )
29- expect ( got . webhooks ) . toStrictEqual ( [ "https://env.webhook.invalid" ] )
30- expect ( got . status ) . toBe ( 'success' )
31- expect ( got . content ) . toBe ( '' )
32- expect ( got . description ) . toBe ( '' )
33- expect ( got . title ) . toBe ( 'github actions' )
34- expect ( got . image ) . toBe ( '' )
35- expect ( got . color ) . toBe ( undefined )
36- expect ( got . username ) . toBe ( '' )
37- expect ( got . avatar_url ) . toBe ( '' )
38- expect ( got . ack_no_webhook ) . toBe ( false )
61+ expect ( got . webhooks ) . toStrictEqual ( [
62+ 'https://input.webhook.invalid' ,
63+ 'https://input2.webhook.invalid'
64+ ] )
3965 } )
4066
41- test ( "invalid status" , ( ) => {
42- process . env [ 'INPUT_STATUS' ] = 'invalid'
43- expect ( getInputs ) . toThrow ( 'invalid status value: invalid' )
67+ test ( "DISCORD_WEBHOOK works as webhook" , ( ) => {
68+ process . env [ 'DISCORD_WEBHOOK' ] = '\nhttps://input.webhook.invalid\n\n\nhttps://input2.webhook.invalid\n\n\n'
69+ const got = getInputs ( )
70+ expect ( got . webhooks ) . toStrictEqual ( [
71+ 'https://input.webhook.invalid' ,
72+ 'https://input2.webhook.invalid'
73+ ] )
74+ } )
75+
76+ test ( "webhook overrides DISCORD_WEBHOOK" , ( ) => {
77+ process . env [ 'DISCORD_WEBHOOK' ] = 'https://env.webhook.invalid'
78+ process . env [ 'INPUT_WEBHOOK' ] = '\nhttps://input.webhook.invalid\n\n\nhttps://input2.webhook.invalid\n\n\n'
79+ const got = getInputs ( )
80+ expect ( got . webhooks ) . toStrictEqual ( [
81+ 'https://input.webhook.invalid' ,
82+ 'https://input2.webhook.invalid'
83+ ] )
4484 } )
4585
46- test ( "override job by title" , ( ) => {
86+ test ( "job works as title" , ( ) => {
87+ process . env [ 'INPUT_WEBHOOK' ] = '\nhttps://input.webhook.invalid\n\n\nhttps://input2.webhook.invalid\n\n\n'
4788 process . env [ 'INPUT_JOB' ] = 'job'
89+
90+ const got = getInputs ( )
91+ expect ( got . title ) . toBe ( 'job' )
92+ } )
93+
94+ test ( "job overrides title" , ( ) => {
95+ process . env [ 'INPUT_WEBHOOK' ] = '\nhttps://input.webhook.invalid\n\n\nhttps://input2.webhook.invalid\n\n\n'
4896 process . env [ 'INPUT_TITLE' ] = 'title'
97+ process . env [ 'INPUT_JOB' ] = 'job'
4998
5099 const got = getInputs ( )
51- expect ( got . title ) . toBe ( 'title ' )
100+ expect ( got . title ) . toBe ( 'job ' )
52101 } )
53102
54- test ( "nocontext" , ( ) => {
55- process . env [ 'INPUT_NOCONTEXT' ] = 'true'
103+ test . each ( [
104+ '0xabcdef' ,
105+ '0xABCDEF' ,
106+ '11259375' , // 0xabcdef
107+ '11259375.123' , // 0xabcdef
108+ ] ) ( 'color parses int-like (%s)' , ( color ) => {
109+ process . env [ 'INPUT_WEBHOOK' ] = '\nhttps://input.webhook.invalid\n\n\nhttps://input2.webhook.invalid\n\n\n'
110+ process . env [ 'INPUT_COLOR' ] = color
111+
56112 const got = getInputs ( )
57- expect ( got . nocontext ) . toBe ( true )
58- expect ( got . noprefix ) . toBe ( false )
113+ expect ( got . color ) . toBe ( 0xabcdef )
59114 } )
60115
61- test ( "noprefix" , ( ) => {
116+ test . each ( [
117+ '0x' ,
118+ 'qwertyuiop' ,
119+ '0xqwerty' ,
120+ 'abcdef' ,
121+ ] ) ( "color is undefined if not int-like (%s)" , ( color ) => {
122+ process . env [ 'INPUT_WEBHOOK' ] = '\nhttps://input.webhook.invalid\n\n\nhttps://input2.webhook.invalid\n\n\n'
123+ process . env [ 'INPUT_COLOR' ] = color
124+
125+ const got = getInputs ( )
126+ expect ( got . color ) . toBe ( undefined )
127+ } )
128+
129+ test ( "nocontext and noprefix are respected by default" , ( ) => {
130+ process . env [ 'INPUT_WEBHOOK' ] = '\nhttps://input.webhook.invalid\n\n\nhttps://input2.webhook.invalid\n\n\n'
131+ process . env [ 'INPUT_NOCONTEXT' ] = 'true'
62132 process . env [ 'INPUT_NOPREFIX' ] = 'true'
133+
63134 const got = getInputs ( )
64- expect ( got . nocontext ) . toBe ( false )
135+ expect ( got . nocontext ) . toBe ( true )
65136 expect ( got . noprefix ) . toBe ( true )
66137 } )
67138
68- test ( "nodetail" , ( ) => {
139+ test ( "if nodetail is set, nocontext and noprefix are forced to true" , ( ) => {
140+ process . env [ 'INPUT_WEBHOOK' ] = '\nhttps://input.webhook.invalid\n\n\nhttps://input2.webhook.invalid\n\n\n'
69141 process . env [ 'INPUT_NODETAIL' ] = 'true'
142+ process . env [ 'INPUT_NOCONTEXT' ] = 'false'
143+ process . env [ 'INPUT_NOPREFIX' ] = 'false'
144+
70145 const got = getInputs ( )
71146 expect ( got . nocontext ) . toBe ( true )
72147 expect ( got . noprefix ) . toBe ( true )
73148 } )
74149
75- test ( "color 0 is accepted" , ( ) => {
76- process . env [ 'INPUT_COLOR' ] = '0'
77- const got = getInputs ( )
78- expect ( got . color ) . toBe ( 0 )
79- } )
80-
81- test ( "invalid color is defaulted to undefined" , ( ) => {
82- process . env [ 'INPUT_COLOR' ] = 'qwertyuiop'
150+ test ( "if nodetail is not set, nocontext and noprefix are respected to user choice" , ( ) => {
151+ process . env [ 'INPUT_WEBHOOK' ] = '\nhttps://input.webhook.invalid\n\n\nhttps://input2.webhook.invalid\n\n\n'
152+ process . env [ 'INPUT_NODETAIL' ] = 'false'
153+ process . env [ 'INPUT_NOCONTEXT' ] = 'false'
154+ process . env [ 'INPUT_NOPREFIX' ] = 'false'
155+
83156 const got = getInputs ( )
84- expect ( got . color ) . toBe ( undefined )
157+ expect ( got . nocontext ) . toBe ( false )
158+ expect ( got . noprefix ) . toBe ( false )
85159 } )
86160
87- test ( "all (job)" , ( ) => {
88- // this pattern is rare because we have default value
89- // for INPUT_TITLE, defined in action.yml, so this pattern
90- // happens only the user make the option blank manually
91- process . env [ 'INPUT_TITLE' ] = ''
92-
93- process . env [ 'INPUT_NODETAIL' ] = 'true'
161+ test ( "empty webhook raises by default" , ( ) => {
94162 process . env [ 'INPUT_WEBHOOK' ] = '\nhttps://input.webhook.invalid\n\n\nhttps://input2.webhook.invalid\n\n\n'
95- process . env [ 'INPUT_STATUS' ] = 'Cancelled'
96- process . env [ 'INPUT_CONTENT' ] = "\n\nhey i'm mentioning \n<@316911818725392384>\n"
97- process . env [ 'INPUT_DESCRIPTION' ] = 'description text'
98- process . env [ 'INPUT_JOB' ] = 'job text\n\n\n\n\n'
99- process . env [ 'INPUT_COLOR' ] = '0xffffff'
100- process . env [ 'INPUT_USERNAME' ] = 'jest test'
101- process . env [ 'INPUT_AVATAR_URL' ] = '\n\n\nhttps://avatar.webhook.invalid\n'
163+ process . env [ 'INPUT_WEBHOOK' ] = '\n\n\n'
164+ expect ( getInputs ) . toThrow ( 'No webhook is given. If this is intended, you can suppress this error by setting `ack_no_webhook` to `true`.' )
165+ } )
102166
103- const got = getInputs ( )
104- expect ( got . noprefix ) . toBe ( true )
105- expect ( got . nocontext ) . toBe ( true )
106- expect ( got . webhooks ) . toStrictEqual ( [
107- 'https://input.webhook.invalid' ,
108- 'https://input2.webhook.invalid'
109- ] )
110- expect ( got . status ) . toBe ( 'cancelled' )
111- expect ( got . content ) . toBe ( "hey i'm mentioning \n<@316911818725392384>" )
112- expect ( got . description ) . toBe ( 'description text' )
113- expect ( got . title ) . toBe ( 'job text' )
114- expect ( got . color ) . toBe ( 0xffffff )
115- expect ( got . username ) . toBe ( 'jest test' )
116- expect ( got . avatar_url ) . toBe ( 'https://avatar.webhook.invalid' )
167+ test ( "empty webhook raises when ack_no_webhook is not set" , ( ) => {
168+ process . env [ 'INPUT_WEBHOOK' ] = '\nhttps://input.webhook.invalid\n\n\nhttps://input2.webhook.invalid\n\n\n'
169+ process . env [ 'INPUT_WEBHOOK' ] = '\n\n\n'
170+ process . env [ 'INPUT_ACK_NO_WEBHOOK' ] = 'false'
171+ expect ( getInputs ) . toThrow ( 'No webhook is given. If this is intended, you can suppress this error by setting `ack_no_webhook` to `true`.' )
117172 } )
118173
119- test ( "all (title)" , ( ) => {
120- process . env [ 'INPUT_NODETAIL' ] = 'true'
174+ test ( "empty webhook did not raise when ack_no_webhook is set" , ( ) => {
121175 process . env [ 'INPUT_WEBHOOK' ] = '\nhttps://input.webhook.invalid\n\n\nhttps://input2.webhook.invalid\n\n\n'
122- process . env [ 'INPUT_STATUS' ] = 'Cancelled'
123- process . env [ 'INPUT_CONTENT' ] = "\n\nhey i'm mentioning \n<@316911818725392384>\n"
124- process . env [ 'INPUT_DESCRIPTION' ] = 'description text'
125- process . env [ 'INPUT_TITLE' ] = 'job text\n\n\n\n\n'
126- process . env [ 'INPUT_IMAGE' ] = '\n\nhttps://example.com/inputimage.png'
127- process . env [ 'INPUT_COLOR' ] = '0xffffff'
128- process . env [ 'INPUT_USERNAME' ] = 'jest test'
129- process . env [ 'INPUT_AVATAR_URL' ] = '\n\n\nhttps://avatar.webhook.invalid\n'
176+ process . env [ 'INPUT_WEBHOOK' ] = '\n\n\n'
130177 process . env [ 'INPUT_ACK_NO_WEBHOOK' ] = 'true'
131-
132178 const got = getInputs ( )
133- expect ( got . noprefix ) . toBe ( true )
134- expect ( got . nocontext ) . toBe ( true )
135- expect ( got . webhooks ) . toStrictEqual ( [
136- 'https://input.webhook.invalid' ,
137- 'https://input2.webhook.invalid'
138- ] )
139- expect ( got . status ) . toBe ( 'cancelled' )
140- expect ( got . content ) . toBe ( "hey i'm mentioning \n<@316911818725392384>" )
141- expect ( got . description ) . toBe ( 'description text' )
142- expect ( got . title ) . toBe ( 'job text' )
143- expect ( got . image ) . toBe ( 'https://example.com/inputimage.png' )
144- expect ( got . color ) . toBe ( 0xffffff )
145- expect ( got . username ) . toBe ( 'jest test' )
146- expect ( got . avatar_url ) . toBe ( 'https://avatar.webhook.invalid' )
147- expect ( got . noprefix ) . toBe ( true )
148- expect ( got . ack_no_webhook ) . toBe ( true )
179+
180+ expect ( got . webhooks ) . toStrictEqual ( [ ] )
181+ } )
182+
183+ test ( "invalid status raises" , ( ) => {
184+ process . env [ 'INPUT_WEBHOOK' ] = '\nhttps://input.webhook.invalid\n\n\nhttps://input2.webhook.invalid\n\n\n'
185+ process . env [ 'INPUT_STATUS' ] = 'invalid'
186+ expect ( getInputs ) . toThrow ( 'invalid status value: invalid' )
149187 } )
150188} )
0 commit comments