11import test from 'ava' ;
22import path from 'node:path' ;
3- import fs from 'node:fs/promises' ;
3+ import fs , { rm } from 'node:fs/promises' ;
44import run from '../src/run' ;
55import createLightningServer from '@openfn/lightning-mock' ;
66import { extractLogs , assertLog } from '../src/util' ;
@@ -12,6 +12,50 @@ const port = 8967;
1212const endpoint = `http://localhost:${ port } ` ;
1313let tmpDir = path . resolve ( 'tmp/deploy' ) ;
1414
15+ const testProjectV2 = `
16+ id: my-project
17+ name: My Project
18+ schema_version: '4.0'
19+ workflows:
20+ - id: my-workflow
21+ name: My Workflow
22+ start: webhook
23+ steps:
24+ - id: webhook
25+ type: webhook
26+ enabled: true
27+ next:
28+ transform-data: {}
29+ - id: transform-data
30+ name: Transform data
31+ expression: 'fn(s => s)'
32+ adaptor: '@openfn/language-common@latest'
33+ ` . trim ( ) ;
34+
35+ const testProjectV2WithCredential = `
36+ id: my-project
37+ name: My Project
38+ schema_version: '4.0'
39+ credentials:
40+ - name: http1
41+ owner: super@openfn.org
42+ workflows:
43+ - id: my-workflow
44+ name: My Workflow
45+ start: webhook
46+ steps:
47+ - id: webhook
48+ type: webhook
49+ enabled: true
50+ next:
51+ transform-data: {}
52+ - id: transform-data
53+ name: Transform data
54+ expression: 'fn(s => s)'
55+ adaptor: '@openfn/language-common@latest'
56+ configuration: super@openfn.org|http1
57+ ` . trim ( ) ;
58+
1559const testProject = `
1660name: test-project
1761workflows:
@@ -97,7 +141,6 @@ test.serial('deploy a local project', async (t) => {
97141 --log-json \
98142 -l debug`
99143 ) ;
100-
101144 t . falsy ( stderr ) ;
102145
103146 const logs = extractLogs ( stdout ) ;
@@ -269,6 +312,51 @@ test.serial('redirect to v2 protocol if openfn.yaml is present', async (t) => {
269312 ) ;
270313} ) ;
271314
315+ test . serial ( 'deploy a v2 spec file' , async ( t ) => {
316+ const testProjectV2 = `
317+ name: test-project
318+ schema_version: '4.0'
319+ workflows:
320+ - id: my-workflow
321+ name: My Workflow
322+ start: webhook
323+ steps:
324+ - id: webhook
325+ type: webhook
326+ enabled: true
327+ next:
328+ my-job: {}
329+ - id: my-job
330+ name: My Job
331+ expression: 'fn(s => s)'
332+ adaptor: '@openfn/language-common@latest'
333+ ` . trim ( ) ;
334+
335+ await fs . writeFile ( path . join ( tmpDir , 'project.yaml' ) , testProjectV2 ) ;
336+
337+ t . is ( Object . keys ( server . state . projects ) . length , 0 ) ;
338+
339+ const { stdout, stderr } = await run (
340+ `openfn deploy \
341+ --project-path ${ tmpDir } /project.yaml \
342+ --state-path ${ tmpDir } /.state.json \
343+ --no-confirm \
344+ --log-json \
345+ -l debug`
346+ ) ;
347+ t . falsy ( stderr ) ;
348+
349+ const logs = extractLogs ( stdout ) ;
350+ assertLog ( t , logs , / v 2 s p e c / i) ;
351+ assertLog ( t , logs , / D e p l o y e d / ) ;
352+
353+ t . is ( Object . keys ( server . state . projects ) . length , 1 ) ;
354+ const [ project ] = Object . values ( server . state . projects ) as any [ ] ;
355+ t . is ( project . name , 'test-project' ) ;
356+ const [ workflow ] = Object . values ( project . workflows ) as any [ ] ;
357+ t . is ( workflow . name , 'My Workflow' ) ;
358+ } ) ;
359+
272360test . serial ( 'deploy then pull, changes one workflow, deploy' , async ( t ) => {
273361 t . is ( Object . keys ( server . state . projects ) . length , 0 ) ;
274362
@@ -318,7 +406,6 @@ test.serial('deploy then pull, changes one workflow, deploy', async (t) => {
318406
319407 // And deploy those changes
320408 const { stdout, stderr } = await run ( deployCmd ) ;
321-
322409 t . falsy ( stderr ) ;
323410
324411 const logs = extractLogs ( stdout ) ;
@@ -332,3 +419,63 @@ test.serial('deploy then pull, changes one workflow, deploy', async (t) => {
332419 t . is ( Object . keys ( server . state . projects ) . length , 1 ) ;
333420 t . truthy ( server . state . projects [ projectId ] ) ;
334421} ) ;
422+
423+ test . serial ( 'deploy a v2 project.yaml' , async ( t ) => {
424+ await fs . writeFile ( path . join ( tmpDir , 'project.yaml' ) , testProjectV2 ) ;
425+
426+ const { stdout, stderr } = await run (
427+ `openfn deploy \
428+ --project-path ${ tmpDir } /project.yaml \
429+ --state-path ${ tmpDir } /.state.json \
430+ --no-confirm \
431+ --log-json \
432+ -l debug`
433+ ) ;
434+
435+ t . falsy ( stderr ) ;
436+
437+ const logs = extractLogs ( stdout ) ;
438+ assertLog ( t , logs , / D e p l o y e d / ) ;
439+
440+ t . is ( Object . keys ( server . state . projects ) . length , 1 ) ;
441+ const [ project ] = Object . values ( server . state . projects ) as any [ ] ;
442+ t . is ( project . name , 'My Project' ) ;
443+ } ) ;
444+
445+ test . serial ( 'deploy a new v2 project.yaml with credentials' , async ( t ) => {
446+ await fs . writeFile (
447+ path . join ( tmpDir , 'project.yaml' ) ,
448+ testProjectV2WithCredential
449+ ) ;
450+
451+ try {
452+ await rm ( `${ tmpDir } /.state.json` ) ;
453+ } catch ( e ) {
454+ // ignore
455+ }
456+
457+ const { stdout, stderr } = await run (
458+ `openfn deploy \
459+ --project-path ${ tmpDir } /project.yaml \
460+ --no-confirm \
461+ --log-json \
462+ -l debug`
463+ ) ;
464+
465+ t . falsy ( stderr ) ;
466+
467+ const logs = extractLogs ( stdout ) ;
468+ assertLog ( t , logs , / D e p l o y e d / ) ;
469+
470+ t . is ( Object . keys ( server . state . projects ) . length , 1 ) ;
471+ const [ project ] = Object . values ( server . state . projects ) as any [ ] ;
472+ t . is ( project . name , 'My Project' ) ;
473+
474+ t . is ( project . project_credentials [ 0 ] . name , 'http1' ) ;
475+ t . is ( project . project_credentials [ 0 ] . owner , 'super@openfn.org' ) ;
476+
477+ const uuid = project . project_credentials [ 0 ] . id ;
478+
479+ const workflow : any = Object . values ( project . workflows ) . pop ( ) ;
480+ t . is ( workflow . jobs [ 0 ] . project_credential_id , uuid ) ;
481+ } ) ;
0 commit comments