1+ import yaml from "js-yaml" ;
12import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
23
3- import * as componentSpecModule from "@/utils/componentSpec" ;
44import * as componentStore from "@/utils/componentStore" ;
55import { USER_PIPELINES_LIST_NAME } from "@/utils/constants" ;
66
77import { importPipelineFromYaml } from "./pipelineService" ;
88
9- vi . mock ( "@/utils/componentStore" , ( ) => ( {
10- componentSpecToYaml : vi . fn ( ) ,
11- getComponentFileFromList : vi . fn ( ) ,
12- writeComponentToFileListFromText : vi . fn ( ) . mockResolvedValue ( { } ) ,
13- } ) ) ;
14-
15- vi . mock ( "@/utils/componentSpec" , ( ) => ( {
16- isGraphImplementation : vi . fn ( ) ,
17- } ) ) ;
18-
199describe ( "importPipelineFromYaml" , ( ) => {
20- const validYamlContent = `
21- name: Test Pipeline
22- metadata:
23- annotations:
24- sdk: https://cloud-pipelines.net/pipeline-editor/
25- implementation:
26- graph:
27- tasks: {}
28- outputValues: {}
29- ` ;
10+ const validYamlObject = {
11+ name : "Test Pipeline" ,
12+ metadata : {
13+ annotations : {
14+ sdk : "https://cloud-pipelines.net/pipeline-editor/" ,
15+ } ,
16+ } ,
17+ implementation : {
18+ graph : {
19+ tasks : [ ] ,
20+ outputValues : [ ] ,
21+ } ,
22+ } ,
23+ } ;
24+
25+ const validYamlContent = yaml . dump ( validYamlObject ) ;
3026
3127 beforeEach ( ( ) => {
3228 vi . clearAllMocks ( ) ;
33- // Default to returning true for valid tests
34- vi . mocked ( componentSpecModule . isGraphImplementation ) . mockReturnValue ( true ) ;
35- // Default for componentSpecToYaml
36- vi . mocked ( componentStore . componentSpecToYaml ) . mockReturnValue (
37- "mocked-yaml" ,
29+ vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ;
30+
31+ vi . spyOn ( componentStore , "getComponentFileFromList" ) . mockResolvedValue (
32+ null ,
3833 ) ;
34+ vi . spyOn (
35+ componentStore ,
36+ "writeComponentToFileListFromText" ,
37+ ) . mockResolvedValue ( { } as any ) ;
3938 } ) ;
4039
4140 afterEach ( ( ) => {
@@ -48,17 +47,8 @@ implementation:
4847
4948 const result = await importPipelineFromYaml ( validYamlContent ) ;
5049
51- // Expect the function to call componentSpecToYaml
52- expect ( componentStore . componentSpecToYaml ) . toHaveBeenCalled ( ) ;
53-
5450 // Expect writeComponentToFileListFromText to be called with correct parameters
55- expect (
56- componentStore . writeComponentToFileListFromText ,
57- ) . toHaveBeenCalledWith (
58- USER_PIPELINES_LIST_NAME ,
59- "Test Pipeline" ,
60- "mocked-yaml" ,
61- ) ;
51+ expect ( componentStore . writeComponentToFileListFromText ) . toHaveBeenCalled ( ) ;
6252
6353 // Expect successful result
6454 expect ( result ) . toEqual ( {
@@ -79,9 +69,6 @@ implementation:
7969 } ,
8070 ) ;
8171
82- // Mock the console.error to prevent test output pollution
83- vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ;
84-
8572 const result = await importPipelineFromYaml ( validYamlContent , false ) ;
8673
8774 // Since we're now renaming rather than erroring, expect a successful result
@@ -95,7 +82,7 @@ implementation:
9582 ) . toHaveBeenCalledWith (
9683 USER_PIPELINES_LIST_NAME ,
9784 "Test Pipeline (1)" ,
98- "mocked-yaml" ,
85+ expect . stringContaining ( "name: Test Pipeline (1)" ) ,
9986 ) ;
10087 } ) ;
10188
@@ -114,9 +101,6 @@ implementation:
114101 } ,
115102 ) ;
116103
117- // Mock the console.error
118- vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ;
119-
120104 const result = await importPipelineFromYaml ( validYamlContent , false ) ;
121105
122106 // Expect a successful result with the name incremented to (3)
@@ -129,14 +113,14 @@ implementation:
129113 ) . toHaveBeenCalledWith (
130114 USER_PIPELINES_LIST_NAME ,
131115 "Test Pipeline (3)" ,
132- "mocked-yaml" ,
116+ yaml . dump ( {
117+ ...validYamlObject ,
118+ name : "Test Pipeline (3)" ,
119+ } ) ,
133120 ) ;
134121 } ) ;
135122
136123 it ( "should handle invalid YAML content" , async ( ) => {
137- // Mock the console.error to prevent test output pollution
138- vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ;
139-
140124 const result = await importPipelineFromYaml ( "invalid: yaml: content: -" ) ;
141125
142126 // Expect unsuccessful result
@@ -146,19 +130,16 @@ implementation:
146130 } ) ;
147131
148132 it ( "should handle non-graph pipelines" , async ( ) => {
149- // Mock isGraphImplementation to return false
150- vi . mocked ( componentSpecModule . isGraphImplementation ) . mockReturnValue ( false ) ;
151-
152- // Mock the console.error
153- vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ;
154-
155- const containerPipeline = `
156- name: Container Pipeline
157- implementation:
158- container:
159- image: test-image
160- command: ["echo", "hello"]
161- ` ;
133+ const containerPipelineObj = {
134+ name : "Container Pipeline" ,
135+ implementation : {
136+ container : {
137+ image : "test-image" ,
138+ command : [ "echo" , "hello" ] ,
139+ } ,
140+ } ,
141+ } ;
142+ const containerPipeline = yaml . dump ( containerPipelineObj ) ;
162143
163144 const result = await importPipelineFromYaml ( containerPipeline ) ;
164145
@@ -173,18 +154,21 @@ implementation:
173154 } ) ;
174155
175156 it ( "should use default name for unnamed pipelines" , async ( ) => {
176- // Mock isGraphImplementation to return true
177- vi . mocked ( componentSpecModule . isGraphImplementation ) . mockReturnValue ( true ) ;
178-
179- const unnamedYaml = `
180- metadata:
181- annotations:
182- sdk: https://cloud-pipelines.net/pipeline-editor/
183- implementation:
184- graph:
185- tasks: {}
186- outputValues: {}
187- ` ;
157+ const unnamedPipelineSpec = {
158+ metadata : {
159+ annotations : {
160+ sdk : "https://cloud-pipelines.net/pipeline-editor/" ,
161+ } ,
162+ } ,
163+ implementation : {
164+ graph : {
165+ tasks : { } ,
166+ outputValues : { } ,
167+ } ,
168+ } ,
169+ } ;
170+
171+ const unnamedYaml = yaml . dump ( unnamedPipelineSpec ) ;
188172
189173 vi . mocked ( componentStore . getComponentFileFromList ) . mockResolvedValue ( null ) ;
190174
@@ -196,7 +180,7 @@ implementation:
196180 ) . toHaveBeenCalledWith (
197181 USER_PIPELINES_LIST_NAME ,
198182 "Imported Pipeline" ,
199- "mocked-yaml" ,
183+ unnamedYaml ,
200184 ) ;
201185
202186 expect ( result . name ) . toBe ( "Imported Pipeline" ) ;
0 commit comments