|
3 | 3 | import test from 'ava'; |
4 | 4 | import mockfs from 'mock-fs'; |
5 | 5 | import { Logger, createMockLogger } from '@openfn/logger'; |
6 | | -import deployHandler, { DeployFn } from '../../src/deploy/handler'; |
| 6 | +import deployHandler, { |
| 7 | + BetaHandlerFn, |
| 8 | + DeployFn, |
| 9 | +} from '../../src/deploy/handler'; |
7 | 10 |
|
8 | 11 | import { DeployError, type DeployConfig } from '@openfn/deploy'; |
9 | 12 | import { DeployOptions } from '../../src/deploy/command'; |
@@ -88,6 +91,91 @@ test.serial('sets the exit code to 1', async (t) => { |
88 | 91 | process.exitCode = origExitCode; |
89 | 92 | }); |
90 | 93 |
|
| 94 | +test.serial( |
| 95 | + 'redirects to beta handler when openfn.yaml exists in cwd', |
| 96 | + async (t) => { |
| 97 | + t.plan(3); |
| 98 | + mockfs({ |
| 99 | + ['./config.json']: `{"apiKey": "123"}`, |
| 100 | + ['./project.yaml']: `{"apiKey": "123"}`, |
| 101 | + ['./openfn.yaml']: 'project:\n endpoint: https://from-yaml.org', |
| 102 | + }); |
| 103 | + |
| 104 | + await deployHandler(options, logger, mockDeploy, async (args: any) => { |
| 105 | + t.is(args.force, true); |
| 106 | + t.is(args.endpoint, 'https://from-yaml.org'); |
| 107 | + t.truthy(logger._find('always', /Detected openfn.yaml file/i)); |
| 108 | + }); |
| 109 | + } |
| 110 | +); |
| 111 | + |
| 112 | +test.serial('does not redirect when PREFER_LEGACY_SYNC is set', async (t) => { |
| 113 | + t.plan(1); |
| 114 | + mockfs({ |
| 115 | + ['./config.json']: `{"apiKey": "123", "endpoint": "https://api.example.com"}`, |
| 116 | + ['./project.yaml']: `{"apiKey": "123"}`, |
| 117 | + ['./openfn.yaml']: 'project:\n endpoint: https://from-yaml.org', |
| 118 | + }); |
| 119 | + process.env.PREFER_LEGACY_SYNC = '1'; |
| 120 | + |
| 121 | + await deployHandler(options, logger, mockDeploy, async (args: any) => { |
| 122 | + t.fail('called beta handler'); |
| 123 | + }); |
| 124 | + |
| 125 | + delete process.env.PREFER_LEGACY_SYNC; |
| 126 | + t.pass(); |
| 127 | +}); |
| 128 | + |
| 129 | +test.serial('CLI endpoint preferred over openfn.yaml endpoint', async (t) => { |
| 130 | + t.plan(1); |
| 131 | + mockfs({ |
| 132 | + ['./config.json']: `{"apiKey": "123"}`, |
| 133 | + ['./project.yaml']: `{"apiKey": "123"}`, |
| 134 | + ['./openfn.yaml']: 'project:\n endpoint: https://from-yaml.org', |
| 135 | + }); |
| 136 | + |
| 137 | + await deployHandler( |
| 138 | + { ...options, endpoint: 'https://from-cli.org' } as any, |
| 139 | + logger, |
| 140 | + mockDeploy, |
| 141 | + async (args: any) => { |
| 142 | + t.is(args.endpoint, 'https://from-cli.org'); |
| 143 | + } |
| 144 | + ); |
| 145 | +}); |
| 146 | + |
| 147 | +test.serial( |
| 148 | + 'openfn.yaml endpoint preferred over config.json endpoint', |
| 149 | + async (t) => { |
| 150 | + mockfs({ |
| 151 | + ['./config.json']: `{"apiKey": "123", "endpoint": "https://from-config.org"}`, |
| 152 | + ['./project.yaml']: `{"apiKey": "123"}`, |
| 153 | + ['./openfn.yaml']: 'project:\n endpoint: https://from-yaml.org', |
| 154 | + }); |
| 155 | + |
| 156 | + await deployHandler(options, logger, mockDeploy, async (args: any) => { |
| 157 | + t.is(args.endpoint, 'https://from-yaml.org'); |
| 158 | + }); |
| 159 | + } |
| 160 | +); |
| 161 | + |
| 162 | +test.serial('CLI apiKey preferred over config.json apiKey', async (t) => { |
| 163 | + mockfs({ |
| 164 | + ['./config.json']: `{"apiKey": "from-config"}`, |
| 165 | + ['./project.yaml']: `{"apiKey": "from-config"}`, |
| 166 | + ['./openfn.yaml']: 'project:\n endpoint: https://from-yaml.org', |
| 167 | + }); |
| 168 | + |
| 169 | + await deployHandler( |
| 170 | + { ...options, apiKey: 'from-cli' } as any, |
| 171 | + logger, |
| 172 | + mockDeploy, |
| 173 | + async (args: any) => { |
| 174 | + t.is(args.apiKey, 'from-cli'); |
| 175 | + } |
| 176 | + ); |
| 177 | +}); |
| 178 | + |
91 | 179 | test.serial('catches DeployErrors', async (t) => { |
92 | 180 | const origExitCode = process.exitCode; |
93 | 181 |
|
|
0 commit comments