|
| 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: sandboxing-simple |
| 8 | +name: Sandboxing Simple |
| 9 | +version: 2 |
| 10 | +collections: [] |
| 11 | +credentials: |
| 12 | + - id: 10a50683-78b0-4ddf-9c14-23a1fb21074a |
| 13 | + name: name |
| 14 | + owner: editor@openfn.org |
| 15 | +openfn: |
| 16 | + uuid: a272a529-716a-4de7-a01c-a082916c6d23 |
| 17 | + endpoint: http://localhost:4000 |
| 18 | + env: project |
| 19 | + fetched_at: 2025-11-26T17:55:09.716Z |
| 20 | + inserted_at: 2025-10-17T10:30:44Z |
| 21 | + updated_at: 2025-10-24T14:52:13Z |
| 22 | +options: |
| 23 | + env: main |
| 24 | + allow_support_access: false |
| 25 | + requires_mfa: false |
| 26 | + retention_policy: retain_all |
| 27 | + version_history: [] |
| 28 | +workflows: |
| 29 | + - name: Hello Workflow |
| 30 | + steps: |
| 31 | + - id: trigger |
| 32 | + type: webhook |
| 33 | + openfn: |
| 34 | + enabled: true |
| 35 | + uuid: 9c0a4e8a-b82f-4fa5-8d12-419da143cd04 |
| 36 | + next: |
| 37 | + transform-data: |
| 38 | + disabled: false |
| 39 | + condition: true |
| 40 | + openfn: |
| 41 | + uuid: add150e9-8616-48ca-844e-8aaa489c7a10 |
| 42 | + - id: transform-data |
| 43 | + name: Transform data |
| 44 | + expression: |- |
| 45 | + // TODO |
| 46 | + adaptor: "@openfn/language-dhis2@8.0.4" |
| 47 | + openfn: |
| 48 | + uuid: a9f64216-7974-469d-8415-d6d9baf2f92e |
| 49 | + project_credential_id: null |
| 50 | + openfn: |
| 51 | + uuid: af697653-98d2-46e4-912f-e1cb6bf8f4b4 |
| 52 | + concurrency: 5 |
| 53 | + inserted_at: 2025-10-17T10:30:51Z |
| 54 | + updated_at: 2025-10-24T14:52:13Z |
| 55 | + deleted_at: null |
| 56 | + lock_version: 16 |
| 57 | + id: hello-workflow |
| 58 | + history: [] |
| 59 | +`; |
| 60 | + |
| 61 | +const stagingYaml = `id: staging |
| 62 | +name: staging |
| 63 | +version: 2 |
| 64 | +collections: [] |
| 65 | +credentials: |
| 66 | + - id: 07c0baa7-c1d7-44b2-abb6-3446defbe3e3 |
| 67 | + name: name |
| 68 | + owner: editor@openfn.org |
| 69 | +openfn: |
| 70 | + uuid: bc6629fb-7dc8-4b28-93af-901e2bd58dc4 |
| 71 | + endpoint: http://localhost:4000 |
| 72 | + env: staging |
| 73 | + fetched_at: 2025-11-27T11:40:37.670Z |
| 74 | + inserted_at: 2025-11-27T11:39:33Z |
| 75 | + updated_at: 2025-11-27T11:39:33Z |
| 76 | +options: |
| 77 | + env: main |
| 78 | + color: "#F39B33" |
| 79 | + parent_id: a272a529-716a-4de7-a01c-a082916c6d23 |
| 80 | + allow_support_access: false |
| 81 | + requires_mfa: false |
| 82 | + retention_policy: retain_all |
| 83 | + version_history: |
| 84 | + - 7b0f5af558f5 |
| 85 | +workflows: |
| 86 | + - name: Hello Workflow |
| 87 | + steps: |
| 88 | + - id: trigger |
| 89 | + type: webhook |
| 90 | + openfn: |
| 91 | + enabled: false |
| 92 | + uuid: 9ef55dca-16a3-480c-a807-2d37744e6e53 |
| 93 | + next: |
| 94 | + transform-data: |
| 95 | + disabled: false |
| 96 | + condition: true |
| 97 | + openfn: |
| 98 | + uuid: f34146b5-de43-4b05-ac00-3b4f327e62ec |
| 99 | + - id: transform-data |
| 100 | + name: Transform data |
| 101 | + expression: |- |
| 102 | + fn() |
| 103 | + adaptor: "@openfn/language-dhis2@8.0.4" |
| 104 | + openfn: |
| 105 | + uuid: 5b4c74f9-76ac-4715-bd45-04b130ca549c |
| 106 | + project_credential_id: null |
| 107 | + |
| 108 | + openfn: |
| 109 | + uuid: 10ce2914-16aa-4e00-b746-47678b1c60d4 |
| 110 | + concurrency: 5 |
| 111 | + inserted_at: 2025-11-27T11:39:33Z |
| 112 | + updated_at: 2025-11-27T11:39:47Z |
| 113 | + deleted_at: null |
| 114 | + lock_version: 1 |
| 115 | + id: hello-workflow |
| 116 | + history: [] |
| 117 | +`; |
| 118 | +const projectsPath = path.resolve('tmp/project'); |
| 119 | + |
| 120 | +test.before(async () => { |
| 121 | + await rm('tmp/project', { recursive: true }); |
| 122 | + await mkdir('tmp/project/.projects', { recursive: true }); |
| 123 | + |
| 124 | + await writeFile('tmp/project/openfn.yaml', ''); |
| 125 | + await writeFile('tmp/project/.projects/main@app.openfn.org.yaml', mainYaml); |
| 126 | + await writeFile( |
| 127 | + 'tmp/project/.projects/staging@app.openfn.org.yaml', |
| 128 | + stagingYaml |
| 129 | + ); |
| 130 | +}); |
| 131 | + |
| 132 | +test.serial('list available projects', async (t) => { |
| 133 | + const { stdout } = await run(`openfn projects -p ${projectsPath}`); |
| 134 | + t.regex(stdout, /sandboxing-simple/); |
| 135 | + t.regex(stdout, /a272a529-716a-4de7-a01c-a082916c6d23/); |
| 136 | + t.regex(stdout, /staging/); |
| 137 | + t.regex(stdout, /bc6629fb-7dc8-4b28-93af-901e2bd58dc4/); |
| 138 | +}); |
| 139 | + |
| 140 | +test.serial('Checkout a project', async (t) => { |
| 141 | + await run(`openfn checkout staging -p ${projectsPath}`); |
| 142 | + |
| 143 | + // check workflow.yaml |
| 144 | + const workflowYaml = await readFile( |
| 145 | + path.resolve(projectsPath, 'workflows/hello-workflow/hello-workflow.yaml'), |
| 146 | + 'utf8' |
| 147 | + ); |
| 148 | + t.is( |
| 149 | + workflowYaml, |
| 150 | + `id: hello-workflow |
| 151 | +name: Hello Workflow |
| 152 | +options: |
| 153 | + history: [] |
| 154 | +steps: |
| 155 | + - id: trigger |
| 156 | + type: webhook |
| 157 | + next: |
| 158 | + transform-data: |
| 159 | + disabled: false |
| 160 | + condition: true |
| 161 | + - id: transform-data |
| 162 | + name: Transform data |
| 163 | + adaptor: "@openfn/language-dhis2@8.0.4" |
| 164 | + expression: ./transform-data.js |
| 165 | +` |
| 166 | + ); |
| 167 | + |
| 168 | + const expr = await readFile( |
| 169 | + path.resolve(projectsPath, 'workflows/hello-workflow/transform-data.js'), |
| 170 | + 'utf8' |
| 171 | + ); |
| 172 | + t.is(expr.trim(), 'fn()'); |
| 173 | +}); |
| 174 | + |
| 175 | +// requires the prior test to run |
| 176 | +test.serial('merge a project', async (t) => { |
| 177 | + const readStep = () => |
| 178 | + readFile( |
| 179 | + path.resolve(projectsPath, 'workflows/hello-workflow/transform-data.js'), |
| 180 | + 'utf8' |
| 181 | + ).then((str) => str.trim()); |
| 182 | + |
| 183 | + await run(`openfn checkout sandboxing-simple -p ${projectsPath}`); |
| 184 | + |
| 185 | + // assert the initial step code |
| 186 | + const initial = await readStep(); |
| 187 | + t.is(initial, '// TODO'); |
| 188 | + |
| 189 | + // Run the merge |
| 190 | + const { stdout } = await run( |
| 191 | + `openfn merge staging -p ${projectsPath} --force` |
| 192 | + ); |
| 193 | + |
| 194 | + // Check the step is updated |
| 195 | + const merged = await readStep(); |
| 196 | + t.is(merged, 'fn()'); |
| 197 | +}); |
0 commit comments