|
| 1 | +import test from 'ava'; |
| 2 | +import path from 'node:path'; |
| 3 | +import fs from 'node:fs'; |
| 4 | +import run from '../src/run'; |
| 5 | +import { generateProject } from '@openfn/project'; |
| 6 | +import createLightningServer from '@openfn/lightning-mock'; |
| 7 | +import { rimraf } from 'rimraf'; |
| 8 | + |
| 9 | +let PORT = 5353; |
| 10 | +let lightning; |
| 11 | +const endpoint = `http://localhost:${PORT}/api/provision`; |
| 12 | + |
| 13 | +test.before(async () => { |
| 14 | + await rimraf('tmp/sync'); |
| 15 | + |
| 16 | + lightning = createLightningServer({ |
| 17 | + port: PORT, |
| 18 | + }); |
| 19 | +}); |
| 20 | + |
| 21 | +const initWorkspace = (t: any) => { |
| 22 | + const id = t.title.replaceAll(' ', '_').toLowerCase(); |
| 23 | + const p = path.resolve('tmp/sync', id); |
| 24 | + |
| 25 | + return { |
| 26 | + workspace: p, |
| 27 | + read: (filePath: string) => { |
| 28 | + return fs.readFileSync(path.resolve(p, filePath), 'utf8'); |
| 29 | + }, |
| 30 | + }; |
| 31 | +}; |
| 32 | + |
| 33 | +const gen = (name = 'patients', workflows = ['trigger-job(body="fn()")']) => { |
| 34 | + // generate a project |
| 35 | + const project = generateProject(name, workflows, { |
| 36 | + openfnUuid: true, |
| 37 | + }); |
| 38 | + const state = project.serialize('state', { format: 'json' }); |
| 39 | + lightning.addProject(state); |
| 40 | + return project; |
| 41 | +}; |
| 42 | + |
| 43 | +test('fetch a new project', async (t) => { |
| 44 | + const { workspace, read } = initWorkspace(t); |
| 45 | + const project = gen(); |
| 46 | + |
| 47 | + await run( |
| 48 | + `openfn project fetch \ |
| 49 | + --workspace ${workspace} \ |
| 50 | + --endpoint ${endpoint} \ |
| 51 | + --api-key abc \ |
| 52 | + ${project.openfn.uuid}` |
| 53 | + ); |
| 54 | + |
| 55 | + // now check that the filesystem is roughly right |
| 56 | + const pyaml = read('.projects/main@localhost.yaml'); |
| 57 | + |
| 58 | + t.regex(pyaml, /id: patients/); |
| 59 | + t.regex(pyaml, new RegExp(`uuid: ${project.openfn.uuid}`)); |
| 60 | +}); |
| 61 | + |
| 62 | +test('fetch a new project with an alias', async (t) => { |
| 63 | + const { workspace, read } = initWorkspace(t); |
| 64 | + const project = gen(); |
| 65 | + |
| 66 | + await run( |
| 67 | + `openfn project fetch \ |
| 68 | + --workspace ${workspace} \ |
| 69 | + --endpoint ${endpoint} \ |
| 70 | + --api-key abc \ |
| 71 | + --alias staging\ |
| 72 | + ${project.openfn.uuid}` |
| 73 | + ); |
| 74 | + |
| 75 | + // now check that the filesystem is roughly right |
| 76 | + const pyaml = read('.projects/staging@localhost.yaml'); |
| 77 | + |
| 78 | + t.regex(pyaml, /id: patients/); |
| 79 | + t.regex(pyaml, new RegExp(`uuid: ${project.openfn.uuid}`)); |
| 80 | +}); |
| 81 | + |
| 82 | +test('fetch a new project to a path', async (t) => { |
| 83 | + const { workspace, read } = initWorkspace(t); |
| 84 | + const project = gen(); |
| 85 | + |
| 86 | + await run( |
| 87 | + `openfn project fetch \ |
| 88 | + --workspace ${workspace} \ |
| 89 | + --endpoint ${endpoint} \ |
| 90 | + --api-key abc \ |
| 91 | + --output ${workspace}/project.yaml\ |
| 92 | + ${project.openfn.uuid}` |
| 93 | + ); |
| 94 | + |
| 95 | + // now check that the filesystem is roughly right |
| 96 | + const pyaml = read('project.yaml'); |
| 97 | + |
| 98 | + t.regex(pyaml, /id: patients/); |
| 99 | + t.regex(pyaml, new RegExp(`uuid: ${project.openfn.uuid}`)); |
| 100 | +}); |
| 101 | + |
| 102 | +test.todo('fetch throws if writing a new project UUID to an existing file'); |
| 103 | + |
| 104 | +test('fetch an existing project with an alias', async (t) => { |
| 105 | + const { workspace, read } = initWorkspace(t); |
| 106 | + const project = gen(); |
| 107 | + |
| 108 | + // fetch the project locally |
| 109 | + await run( |
| 110 | + `openfn project fetch \ |
| 111 | + --workspace ${workspace} \ |
| 112 | + --endpoint ${endpoint} \ |
| 113 | + --api-key abc \ |
| 114 | + --alias staging \ |
| 115 | + ${project.openfn.uuid}` |
| 116 | + ); |
| 117 | + |
| 118 | + const before = read('.projects/staging@localhost.yaml'); |
| 119 | + t.regex(before, /fn\(\)/); |
| 120 | + |
| 121 | + // now update the remote project |
| 122 | + project.workflows[0].steps[0].expression = 'fn(x)'; |
| 123 | + const state = project.serialize('state', { format: 'json' }); |
| 124 | + lightning.addProject(state); |
| 125 | + |
| 126 | + // Now run another fetch but only use the alias - no uuid |
| 127 | + await run( |
| 128 | + `openfn project fetch \ |
| 129 | + --workspace ${workspace} \ |
| 130 | + --endpoint ${endpoint} \ |
| 131 | + --api-key abc \ |
| 132 | + staging` |
| 133 | + ); |
| 134 | + |
| 135 | + // now check that the filesystem is roughly right |
| 136 | + const after = read('.projects/staging@localhost.yaml'); |
| 137 | + |
| 138 | + t.regex(after, /fn\(x\)/); |
| 139 | +}); |
| 140 | + |
| 141 | +test('pull a new project', async (t) => { |
| 142 | + const { workspace, read } = initWorkspace(t); |
| 143 | + const project = gen(); |
| 144 | + |
| 145 | + await run( |
| 146 | + `openfn project pull \ |
| 147 | + --workspace ${workspace} \ |
| 148 | + --endpoint ${endpoint} \ |
| 149 | + --api-key abc \ |
| 150 | + --log debug \ |
| 151 | + ${project.openfn.uuid}` |
| 152 | + ); |
| 153 | + |
| 154 | + // now check that the filesystem is roughly right |
| 155 | + const proj_yaml = read('.projects/main@localhost.yaml'); |
| 156 | + |
| 157 | + t.regex(proj_yaml, /id: patients/); |
| 158 | + t.regex(proj_yaml, new RegExp(`uuid: ${project.openfn.uuid}`)); |
| 159 | + |
| 160 | + const openfn_yaml = read('openfn.yaml'); |
| 161 | + t.regex(openfn_yaml, new RegExp(`uuid: ${project.openfn.uuid}`)); |
| 162 | + t.regex(openfn_yaml, new RegExp(`endpoint: ${endpoint}`)); |
| 163 | + |
| 164 | + const job = read('workflows/workflow/job.js'); |
| 165 | + t.is(job, 'fn()'); |
| 166 | +}); |
| 167 | + |
| 168 | +test('pull a new project with an alias', async (t) => { |
| 169 | + const { workspace, read } = initWorkspace(t); |
| 170 | + const project = gen(); |
| 171 | + |
| 172 | + await run( |
| 173 | + `openfn project pull \ |
| 174 | + --workspace ${workspace} \ |
| 175 | + --endpoint ${endpoint} \ |
| 176 | + --api-key abc \ |
| 177 | + --log debug \ |
| 178 | + --alias staging \ |
| 179 | + ${project.openfn.uuid}` |
| 180 | + ); |
| 181 | + |
| 182 | + // now check that the filesystem is roughly right |
| 183 | + const proj_yaml = read('.projects/staging@localhost.yaml'); |
| 184 | + |
| 185 | + t.regex(proj_yaml, /id: patients/); |
| 186 | + t.regex(proj_yaml, new RegExp(`uuid: ${project.openfn.uuid}`)); |
| 187 | + |
| 188 | + const openfn_yaml = read('openfn.yaml'); |
| 189 | + t.regex(openfn_yaml, new RegExp(`uuid: ${project.openfn.uuid}`)); |
| 190 | + t.regex(openfn_yaml, new RegExp(`endpoint: ${endpoint}`)); |
| 191 | + |
| 192 | + const job = read('workflows/workflow/job.js'); |
| 193 | + t.is(job, 'fn()'); |
| 194 | +}); |
| 195 | + |
| 196 | +test('pull an update to project', async (t) => { |
| 197 | + const { workspace, read } = initWorkspace(t); |
| 198 | + const project = gen(); |
| 199 | + |
| 200 | + // fetch the project once to set up the repo |
| 201 | + await run( |
| 202 | + `openfn project pull \ |
| 203 | + --workspace ${workspace} \ |
| 204 | + --endpoint ${endpoint} \ |
| 205 | + --api-key abc \ |
| 206 | + ${project.openfn.uuid}` |
| 207 | + ); |
| 208 | + |
| 209 | + const job = read('workflows/workflow/job.js'); |
| 210 | + t.is(job, 'fn()'); |
| 211 | + |
| 212 | + // now update the remote project |
| 213 | + project.workflows[0].steps[0].expression = 'fn(x)'; |
| 214 | + const state = project.serialize('state', { format: 'json' }); |
| 215 | + lightning.addProject(state); |
| 216 | + // (note that the verison hash hasn't updated so not the best test) |
| 217 | + |
| 218 | + // and refetch |
| 219 | + await run( |
| 220 | + `openfn project pull \ |
| 221 | + --workspace ${workspace} \ |
| 222 | + --endpoint ${endpoint} \ |
| 223 | + --api-key abc \ |
| 224 | + ${project.openfn.uuid}` |
| 225 | + ); |
| 226 | + |
| 227 | + const proj_yaml = read('.projects/main@localhost.yaml'); |
| 228 | + t.regex(proj_yaml, /fn\(x\)/); |
| 229 | + t.regex(proj_yaml, new RegExp(`uuid: ${project.openfn.uuid}`)); |
| 230 | + |
| 231 | + const openfn_yaml = read('openfn.yaml'); |
| 232 | + t.regex(openfn_yaml, new RegExp(`uuid: ${project.openfn.uuid}`)); |
| 233 | + t.regex(openfn_yaml, new RegExp(`endpoint: ${endpoint}`)); |
| 234 | + |
| 235 | + const job_updated = read('workflows/workflow/job.js'); |
| 236 | + t.is(job_updated, 'fn()'); |
| 237 | +}); |
| 238 | + |
| 239 | +test('checkout by alias', async (t) => { |
| 240 | + const { workspace, read } = initWorkspace(t); |
| 241 | + const main = gen(); |
| 242 | + const staging = gen('patients-staging', ['trigger-job(body="fn(x)")']); |
| 243 | + |
| 244 | + await run( |
| 245 | + `openfn project fetch \ |
| 246 | + --workspace ${workspace} \ |
| 247 | + --endpoint ${endpoint} \ |
| 248 | + --api-key abc \ |
| 249 | + --alias main\ |
| 250 | + ${main.openfn.uuid}` |
| 251 | + ); |
| 252 | + await run( |
| 253 | + `openfn project fetch \ |
| 254 | + --workspace ${workspace} \ |
| 255 | + --endpoint ${endpoint} \ |
| 256 | + --api-key abc \ |
| 257 | + --alias staging\ |
| 258 | + ${staging.openfn.uuid}` |
| 259 | + ); |
| 260 | + |
| 261 | + // Ensure the repo is set up correctly |
| 262 | + const main_yaml = read('.projects/main@localhost.yaml'); |
| 263 | + t.regex(main_yaml, /fn\(\)/); |
| 264 | + const staging_yaml = read('.projects/staging@localhost.yaml'); |
| 265 | + t.regex(staging_yaml, /fn\(x\)/); |
| 266 | + |
| 267 | + await run( |
| 268 | + `openfn project checkout main \ |
| 269 | + --workspace ${workspace}` |
| 270 | + ); |
| 271 | + |
| 272 | + // only do a rough check of the file system |
| 273 | + // local tests can be more thorough - at this level |
| 274 | + // I just want to see that the command has basically worked |
| 275 | + let job = read('workflows/workflow/job.js'); |
| 276 | + t.is(job, 'fn()'); |
| 277 | + |
| 278 | + await run( |
| 279 | + `openfn project checkout staging \ |
| 280 | + --workspace ${workspace}` |
| 281 | + ); |
| 282 | + |
| 283 | + job = read('workflows/workflow/job.js'); |
| 284 | + t.is(job, 'fn(x)'); |
| 285 | +}); |
| 286 | + |
| 287 | +test.todo('merge by alias'); |
0 commit comments