Skip to content

Commit d453906

Browse files
committed
add tests
1 parent 6a7e3ad commit d453906

2 files changed

Lines changed: 97 additions & 7 deletions

File tree

packages/cli/src/deploy/handler.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,27 @@ import { yamlToJson } from '@openfn/project';
1414
import fs from 'node:fs/promises';
1515

1616
export type DeployFn = typeof deploy;
17+
export type BetaHandlerFn = typeof beta.handler;
1718

1819
const actualDeploy: DeployFn = deploy;
20+
const actualBetaHandler: BetaHandlerFn = beta.handler;
1921

20-
// Flexible `deployFn` interface for testing.
22+
// Flexible `deployFn` / `betaHandler` interfaces for testing.
2123
async function deployHandler<F extends (...args: any) => any>(
2224
options: DeployOptions,
2325
logger: Logger,
24-
deployFn: F
26+
deployFn: F,
27+
betaHandler?: BetaHandlerFn
2528
): Promise<ReturnType<typeof deployFn>>;
2629

2730
async function deployHandler(
2831
options: DeployOptions,
2932
logger: Logger,
30-
deployFn = actualDeploy
33+
deployFn = actualDeploy,
34+
betaHandler: BetaHandlerFn = actualBetaHandler
3135
) {
3236
if (options.beta) {
33-
return beta.handler(options as any, logger);
37+
return betaHandler(options as any, logger);
3438
}
3539

3640
try {
@@ -41,7 +45,7 @@ async function deployHandler(
4145
'openfn.yaml'
4246
);
4347
if (!process.env.PREFER_LEGACY_SYNC && (await fileExists(v2ConfigPath))) {
44-
return redirectTov2(v2ConfigPath, options, config, logger);
48+
return redirectTov2(v2ConfigPath, options, config, logger, betaHandler);
4549
}
4650

4751
if (options.confirm === false) {
@@ -110,7 +114,8 @@ const redirectTov2 = async (
110114
v2ConfigPath: string,
111115
options: DeployOptions,
112116
config: DeployConfig,
113-
logger: Logger
117+
logger: Logger,
118+
betaHandler: BetaHandlerFn = actualBetaHandler
114119
) => {
115120
logger.always(
116121
'Detected openfn.yaml file - switching to v2 deploy (openfn project deploy). Set PREFER_LEGACY_SYNC to disable this.'
@@ -121,7 +126,7 @@ const redirectTov2 = async (
121126
const endpoint =
122127
options.endpoint ?? v2config?.project?.endpoint ?? config.endpoint;
123128

124-
return beta.handler(
129+
return betaHandler(
125130
{
126131
...options,
127132
force: true,

packages/cli/test/deploy/deploy.test.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,91 @@ test.serial('sets the exit code to 1', async (t) => {
8888
process.exitCode = origExitCode;
8989
});
9090

91+
test.serial(
92+
'redirects to beta handler when openfn.yaml exists in cwd',
93+
async (t) => {
94+
t.plan(3);
95+
mockfs({
96+
['./config.json']: `{"apiKey": "123"}`,
97+
['./project.yaml']: `{"apiKey": "123"}`,
98+
['./openfn.yaml']: 'project:\n endpoint: https://from-yaml.org',
99+
});
100+
101+
await deployHandler(options, logger, mockDeploy, async (args: any) => {
102+
t.is(args.force, true);
103+
t.is(args.endpoint, 'https://from-yaml.org');
104+
t.truthy(logger._find('always', /Detected openfn.yaml file/i));
105+
});
106+
}
107+
);
108+
109+
test.serial('does not redirect when PREFER_LEGACY_SYNC is set', async (t) => {
110+
t.plan(1);
111+
mockfs({
112+
['./config.json']: `{"apiKey": "123", "endpoint": "https://api.example.com"}`,
113+
['./project.yaml']: `{"apiKey": "123"}`,
114+
['./openfn.yaml']: 'project:\n endpoint: https://from-yaml.org',
115+
});
116+
process.env.PREFER_LEGACY_SYNC = '1';
117+
118+
await deployHandler(options, logger, mockDeploy, async (args: any) => {
119+
t.fail('called beta handler');
120+
});
121+
122+
delete process.env.PREFER_LEGACY_SYNC;
123+
t.pass();
124+
});
125+
126+
test.serial('CLI endpoint preferred over openfn.yaml endpoint', async (t) => {
127+
t.plan(1);
128+
mockfs({
129+
['./config.json']: `{"apiKey": "123"}`,
130+
['./project.yaml']: `{"apiKey": "123"}`,
131+
['./openfn.yaml']: 'project:\n endpoint: https://from-yaml.org',
132+
});
133+
134+
await deployHandler(
135+
{ ...options, endpoint: 'https://from-cli.org' } as any,
136+
logger,
137+
mockDeploy,
138+
async (args: any) => {
139+
t.is(args.endpoint, 'https://from-cli.org');
140+
}
141+
);
142+
});
143+
144+
test.serial(
145+
'openfn.yaml endpoint preferred over config.json endpoint',
146+
async (t) => {
147+
mockfs({
148+
['./config.json']: `{"apiKey": "123", "endpoint": "https://from-config.org"}`,
149+
['./project.yaml']: `{"apiKey": "123"}`,
150+
['./openfn.yaml']: 'project:\n endpoint: https://from-yaml.org',
151+
});
152+
153+
await deployHandler(options, logger, mockDeploy, async (args: any) => {
154+
t.is(args.endpoint, 'https://from-yaml.org');
155+
});
156+
}
157+
);
158+
159+
test.serial('CLI apiKey preferred over config.json apiKey', async (t) => {
160+
mockfs({
161+
['./config.json']: `{"apiKey": "from-config"}`,
162+
['./project.yaml']: `{"apiKey": "from-config"}`,
163+
['./openfn.yaml']: 'project:\n endpoint: https://from-yaml.org',
164+
});
165+
166+
await deployHandler(
167+
{ ...options, apiKey: 'from-cli' } as any,
168+
logger,
169+
mockDeploy,
170+
async (args: any) => {
171+
t.is(args.apiKey, 'from-cli');
172+
}
173+
);
174+
});
175+
91176
test.serial('catches DeployErrors', async (t) => {
92177
const origExitCode = process.exitCode;
93178

0 commit comments

Comments
 (0)