|
| 1 | +import test from 'ava'; |
| 2 | +import { rm, mkdir, writeFile, readFile } from 'node:fs/promises'; |
| 3 | +import path from 'node:path'; |
| 4 | +import run from '../src/run'; |
| 5 | + |
| 6 | +const mainYaml = ` |
| 7 | +id: 8dbc4349-52b4-4bf2-be10-fdf06da52c46 |
| 8 | +name: hello-world |
| 9 | +description: Simple test project to test sandboxing and merging |
| 10 | +project_credentials: [] |
| 11 | +collections: [] |
| 12 | +inserted_at: 2025-10-07T09:47:41Z |
| 13 | +updated_at: 2025-10-07T09:47:41Z |
| 14 | +env: null |
| 15 | +color: null |
| 16 | +concurrency: null |
| 17 | +scheduled_deletion: null |
| 18 | +parent_id: null |
| 19 | +history_retention_period: null |
| 20 | +allow_support_access: false |
| 21 | +dataclip_retention_period: null |
| 22 | +requires_mfa: false |
| 23 | +retention_policy: retain_all |
| 24 | +version_history: [] |
| 25 | +workflows: |
| 26 | + - name: my workflow |
| 27 | + id: 0afbefab-5824-4911-aaae-a19f20106dec |
| 28 | + concurrency: null |
| 29 | + inserted_at: 2025-10-07T10:00:23Z |
| 30 | + updated_at: 2025-10-07T10:00:29Z |
| 31 | + deleted_at: null |
| 32 | + lock_version: 2 |
| 33 | + jobs: |
| 34 | + - name: Transform data |
| 35 | + body: | |
| 36 | + // TODO |
| 37 | + adaptor: "@openfn/language-common@latest" |
| 38 | + id: b8b780f3-98dd-4244-880b-e534d8f24547 |
| 39 | + project_credential_id: null |
| 40 | + triggers: |
| 41 | + - type: webhook |
| 42 | + enabled: true |
| 43 | + id: 3b4a47c0-7242-4f0c-8886-838e34762654 |
| 44 | + edges: |
| 45 | + - id: 33dce70f-047f-4508-82fd-950eb508519b |
| 46 | + target_job_id: b8b780f3-98dd-4244-880b-e534d8f24547 |
| 47 | + enabled: true |
| 48 | + source_trigger_id: 3b4a47c0-7242-4f0c-8886-838e34762654 |
| 49 | + condition_type: always |
| 50 | +`; |
| 51 | + |
| 52 | +const stagingYaml = ` |
| 53 | +id: 5deddbfa-c63f-4dbc-98b5-a49d3395a488 |
| 54 | +name: hello-world-staging |
| 55 | +description: "simulate a staging project " |
| 56 | +project_credentials: [] |
| 57 | +collections: [] |
| 58 | +inserted_at: 2025-10-07T10:00:13Z |
| 59 | +updated_at: 2025-10-07T10:00:13Z |
| 60 | +env: null |
| 61 | +color: null |
| 62 | +concurrency: null |
| 63 | +scheduled_deletion: null |
| 64 | +parent_id: null |
| 65 | +history_retention_period: null |
| 66 | +allow_support_access: false |
| 67 | +dataclip_retention_period: null |
| 68 | +requires_mfa: false |
| 69 | +retention_policy: retain_all |
| 70 | +version_history: [] |
| 71 | +workflows: |
| 72 | + - name: my workflow |
| 73 | + id: 9e2cc86a-8896-4a5a-9467-9c4128207fd3 |
| 74 | + concurrency: null |
| 75 | + inserted_at: 2025-10-07T10:00:36Z |
| 76 | + updated_at: 2025-10-07T10:00:53Z |
| 77 | + deleted_at: null |
| 78 | + lock_version: 3 |
| 79 | + jobs: |
| 80 | + - name: Transform data |
| 81 | + body: log('hello world') |
| 82 | + adaptor: "@openfn/language-common@latest" |
| 83 | + id: 8d627978-ebb9-4fb2-8cda-9b31c10c963e |
| 84 | + project_credential_id: null |
| 85 | + triggers: |
| 86 | + - type: webhook |
| 87 | + enabled: true |
| 88 | + id: 7bb476cc-0292-4573-89d0-b13417bc648e |
| 89 | + edges: |
| 90 | + - id: 4c68d22a-4ba7-4d8f-8103-6f4f15c4e7d2 |
| 91 | + target_job_id: 8d627978-ebb9-4fb2-8cda-9b31c10c963e |
| 92 | + enabled: true |
| 93 | + source_trigger_id: 7bb476cc-0292-4573-89d0-b13417bc648e |
| 94 | + condition_type: always |
| 95 | +`; |
| 96 | +const projectsPath = path.resolve('tmp/project'); |
| 97 | + |
| 98 | +test.before(async () => { |
| 99 | + // await rm('tmp/project', { recursive: true }); |
| 100 | + await mkdir('tmp/project/.projects', { recursive: true }); |
| 101 | + |
| 102 | + await writeFile('tmp/project/openfn.yaml', ''); |
| 103 | + await writeFile('tmp/project/.projects/main@app.openfn.org.yaml', mainYaml); |
| 104 | + await writeFile( |
| 105 | + 'tmp/project/.projects/staging@app.openfn.org.yaml', |
| 106 | + stagingYaml |
| 107 | + ); |
| 108 | +}); |
| 109 | + |
| 110 | +// checkout a project from a yaml file |
| 111 | +test.serial('Checkout a project', async (t) => { |
| 112 | + await run(`openfn checkout hello-world -p ${projectsPath}`); |
| 113 | + |
| 114 | + // check workflow.yaml |
| 115 | + const workflowYaml = await readFile( |
| 116 | + path.resolve(projectsPath, 'workflows/my-workflow/my-workflow.yaml'), |
| 117 | + 'utf8' |
| 118 | + ); |
| 119 | + t.is( |
| 120 | + workflowYaml, |
| 121 | + `id: my-workflow |
| 122 | +name: my workflow |
| 123 | +options: {} |
| 124 | +steps: |
| 125 | + - id: trigger |
| 126 | + type: webhook |
| 127 | + next: |
| 128 | + transform-data: |
| 129 | + disabled: false |
| 130 | + condition: true |
| 131 | + openfn: |
| 132 | + uuid: 33dce70f-047f-4508-82fd-950eb508519b |
| 133 | + - id: transform-data |
| 134 | + name: Transform data |
| 135 | + adaptor: "@openfn/language-common@latest" |
| 136 | + expression: ./transform-data.js |
| 137 | +` |
| 138 | + ); |
| 139 | + |
| 140 | + const expr = await readFile( |
| 141 | + path.resolve(projectsPath, 'workflows/my-workflow/transform-data.js'), |
| 142 | + 'utf8' |
| 143 | + ); |
| 144 | + t.is(expr.trim(), '// TODO'); |
| 145 | +}); |
| 146 | + |
| 147 | +// requires the prior test to run |
| 148 | +test.serial('merge a project', async (t) => { |
| 149 | + const readStep = () => |
| 150 | + readFile( |
| 151 | + path.resolve(projectsPath, 'workflows/my-workflow/transform-data.js'), |
| 152 | + 'utf8' |
| 153 | + ).then((str) => str.trim()); |
| 154 | + |
| 155 | + // assert the intial step code |
| 156 | + const initial = await readStep(); |
| 157 | + t.is(initial, '// TODO'); |
| 158 | + |
| 159 | + // Run the merge |
| 160 | + await run(`openfn merge hello-world-staging -p ${projectsPath}`); |
| 161 | + |
| 162 | + // Check the step is updated |
| 163 | + const merged = await readStep(); |
| 164 | + t.is(merged, "log('hello world')"); |
| 165 | +}); |
0 commit comments