Skip to content

Commit 2436967

Browse files
committed
add tests
1 parent 4cc7471 commit 2436967

2 files changed

Lines changed: 101 additions & 8 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: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import test from 'ava';
44
import mockfs from 'mock-fs';
55
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';
710

811
import { DeployError, type DeployConfig } from '@openfn/deploy';
912
import { DeployOptions } from '../../src/deploy/command';
@@ -88,6 +91,91 @@ test.serial('sets the exit code to 1', async (t) => {
8891
process.exitCode = origExitCode;
8992
});
9093

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+
91179
test.serial('catches DeployErrors', async (t) => {
92180
const origExitCode = process.exitCode;
93181

0 commit comments

Comments
 (0)