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 ( componentStore , "writeComponentToFileListFromText" ) ;
3935 } ) ;
4036
4137 afterEach ( ( ) => {
@@ -48,17 +44,8 @@ implementation:
4844
4945 const result = await importPipelineFromYaml ( validYamlContent ) ;
5046
51- // Expect the function to call componentSpecToYaml
52- expect ( componentStore . componentSpecToYaml ) . toHaveBeenCalled ( ) ;
53-
5447 // 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- ) ;
48+ expect ( componentStore . writeComponentToFileListFromText ) . toHaveBeenCalled ( ) ;
6249
6350 // Expect successful result
6451 expect ( result ) . toEqual ( {
@@ -79,9 +66,6 @@ implementation:
7966 } ,
8067 ) ;
8168
82- // Mock the console.error to prevent test output pollution
83- vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ;
84-
8569 const result = await importPipelineFromYaml ( validYamlContent , false ) ;
8670
8771 // Since we're now renaming rather than erroring, expect a successful result
@@ -95,7 +79,7 @@ implementation:
9579 ) . toHaveBeenCalledWith (
9680 USER_PIPELINES_LIST_NAME ,
9781 "Test Pipeline (1)" ,
98- "mocked-yaml" ,
82+ expect . stringContaining ( "name: Test Pipeline (1)" ) ,
9983 ) ;
10084 } ) ;
10185
@@ -114,9 +98,6 @@ implementation:
11498 } ,
11599 ) ;
116100
117- // Mock the console.error
118- vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ;
119-
120101 const result = await importPipelineFromYaml ( validYamlContent , false ) ;
121102
122103 // Expect a successful result with the name incremented to (3)
@@ -129,14 +110,14 @@ implementation:
129110 ) . toHaveBeenCalledWith (
130111 USER_PIPELINES_LIST_NAME ,
131112 "Test Pipeline (3)" ,
132- "mocked-yaml" ,
113+ yaml . dump ( {
114+ ...validYamlObject ,
115+ name : "Test Pipeline (3)" ,
116+ } ) ,
133117 ) ;
134118 } ) ;
135119
136120 it ( "should handle invalid YAML content" , async ( ) => {
137- // Mock the console.error to prevent test output pollution
138- vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ;
139-
140121 const result = await importPipelineFromYaml ( "invalid: yaml: content: -" ) ;
141122
142123 // Expect unsuccessful result
@@ -146,19 +127,16 @@ implementation:
146127 } ) ;
147128
148129 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- ` ;
130+ const containerPipelineObj = {
131+ name : "Container Pipeline" ,
132+ implementation : {
133+ container : {
134+ image : "test-image" ,
135+ command : [ "echo" , "hello" ] ,
136+ } ,
137+ } ,
138+ } ;
139+ const containerPipeline = yaml . dump ( containerPipelineObj ) ;
162140
163141 const result = await importPipelineFromYaml ( containerPipeline ) ;
164142
@@ -173,18 +151,21 @@ implementation:
173151 } ) ;
174152
175153 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- ` ;
154+ const unnamedPipelineSpec = {
155+ metadata : {
156+ annotations : {
157+ sdk : "https://cloud-pipelines.net/pipeline-editor/" ,
158+ } ,
159+ } ,
160+ implementation : {
161+ graph : {
162+ tasks : { } ,
163+ outputValues : { } ,
164+ } ,
165+ } ,
166+ } ;
167+
168+ const unnamedYaml = yaml . dump ( unnamedPipelineSpec ) ;
188169
189170 vi . mocked ( componentStore . getComponentFileFromList ) . mockResolvedValue ( null ) ;
190171
@@ -196,7 +177,7 @@ implementation:
196177 ) . toHaveBeenCalledWith (
197178 USER_PIPELINES_LIST_NAME ,
198179 "Imported Pipeline" ,
199- "mocked-yaml" ,
180+ unnamedYaml ,
200181 ) ;
201182
202183 expect ( result . name ) . toBe ( "Imported Pipeline" ) ;
0 commit comments