-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathhybridRun.test.ts
More file actions
248 lines (221 loc) · 7.18 KB
/
Copy pathhybridRun.test.ts
File metadata and controls
248 lines (221 loc) · 7.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import { afterEach, beforeEach, describe, expect, it, mock } from "bun:test";
import type { AuthCommandContext } from "~/shell/commandContext.js";
import { makeFakeUI } from "~/shell/commandContext.testUtils.js";
import type { FlowsRunFlags } from "~/domains/runner/runInternals.js";
import type { FlowRuntimeDeps } from "~/domains/runner/flowRuntimeDeps.js";
import { makeNoopLogger } from "~/shell/logger.testUtils.js";
import { makeNoopSignals } from "~/shell/signals/createSignalRegistry.fixtures.js";
import {
handleHybridFlowsRun,
type HandleHybridFlowsRunDeps,
} from "./hybridRunDefaults.js";
afterEach(() => {
mock.restore();
});
const expandPatternsMock = mock<HandleHybridFlowsRunDeps["expandPatterns"]>();
const pullEnvMock = mock<HandleHybridFlowsRunDeps["pullEnv"]>();
const ensureFlowDepsMock = mock<(envDir: string) => Promise<void>>();
const configureTestkitMock = mock<(dir: string) => Promise<void>>();
const flowsRunMock = mock<HandleHybridFlowsRunDeps["flowsRun"]>();
const runWebFlowDepsMock = mock<() => Promise<unknown>>();
const createFlowRuntimeDepsMock = mock<(...args: unknown[]) => unknown>();
const sharedFlowRuntimeDeps: FlowRuntimeDeps = {
fetchLatestEnvironmentVariables: async () => {},
};
const trackedMocks = [
expandPatternsMock,
pullEnvMock,
ensureFlowDepsMock,
configureTestkitMock,
flowsRunMock,
runWebFlowDepsMock,
createFlowRuntimeDepsMock,
];
beforeEach(() => {
for (const m of trackedMocks) m.mockClear();
expandPatternsMock.mockResolvedValue([]);
pullEnvMock.mockResolvedValue(undefined);
ensureFlowDepsMock.mockResolvedValue(undefined);
configureTestkitMock.mockResolvedValue(undefined);
flowsRunMock.mockResolvedValue(undefined);
runWebFlowDepsMock.mockResolvedValue({} as unknown);
createFlowRuntimeDepsMock.mockReturnValue(sharedFlowRuntimeDeps);
});
function makeCtx(): AuthCommandContext {
return {
configDir: "/mock/config",
apiBaseUrl: "https://app.qawolf.com",
outputMode: "human",
isInteractive: false,
apiKeySource: "env",
platform: {} as unknown,
signals: makeNoopSignals(),
ui: makeFakeUI("human"),
log: () => makeNoopLogger(),
} as unknown as AuthCommandContext;
}
function defaultFlags(): FlowsRunFlags {
return {
retries: 0,
bail: false,
workers: 1,
timeout: 30_000,
video: "off",
trace: "off",
har: "off",
harContent: "omit",
outputDir: "/tmp",
headed: false,
};
}
function makeDeps(): HandleHybridFlowsRunDeps {
return {
expandPatterns: expandPatternsMock,
pullEnv: pullEnvMock,
ensureFlowDeps: ensureFlowDepsMock,
configureTestkit: configureTestkitMock,
flowsRun: flowsRunMock,
runWebFlowDeps:
runWebFlowDepsMock as unknown as HandleHybridFlowsRunDeps["runWebFlowDeps"],
createFlowRuntimeDeps:
createFlowRuntimeDepsMock as unknown as HandleHybridFlowsRunDeps["createFlowRuntimeDeps"],
};
}
describe("handleHybridFlowsRun", () => {
it("returns error with exitCode 2 when env is not a valid ID", async () => {
const result = await handleHybridFlowsRun(
makeCtx(),
undefined,
{ ...defaultFlags(), env: "INVALID ENV ID" },
makeDeps(),
);
expect(result).toEqual({
error: "--env must be a UUID or kebab-case slug (got: INVALID ENV ID)",
exitCode: 2,
});
expect(expandPatternsMock).not.toHaveBeenCalled();
});
it("runs files from envDir directly when already cached", async () => {
const ctx = makeCtx();
const deps = makeDeps();
expandPatternsMock.mockResolvedValue([
"/mock/.qawolf/my-env/login.flow.ts",
]);
await handleHybridFlowsRun(
ctx,
"**/login.flow.ts",
{ ...defaultFlags(), env: "my-env" },
deps,
);
expect(pullEnvMock).not.toHaveBeenCalled();
expect(expandPatternsMock).toHaveBeenCalledWith(
["**/login.flow.ts"],
expect.any(String),
expect.anything(),
);
expect(flowsRunMock).toHaveBeenCalledWith(
expect.anything(),
["/mock/.qawolf/my-env/login.flow.ts"],
expect.anything(),
expect.anything(),
);
expect(flowsRunMock.mock.calls[0]?.[3].logger).toBeDefined();
});
it("pulls env on cache miss and runs matched files", async () => {
const ctx = makeCtx();
const deps = makeDeps();
expandPatternsMock
.mockResolvedValueOnce([])
.mockResolvedValueOnce(["/mock/.qawolf/my-env/login.flow.ts"]);
pullEnvMock.mockResolvedValue(undefined);
await handleHybridFlowsRun(
ctx,
"**/login.flow.ts",
{ ...defaultFlags(), env: "my-env" },
deps,
);
expect(pullEnvMock).toHaveBeenCalledWith(ctx, "my-env");
expect(expandPatternsMock).toHaveBeenCalledTimes(2);
expect(flowsRunMock).toHaveBeenCalledWith(
expect.anything(),
["/mock/.qawolf/my-env/login.flow.ts"],
expect.anything(),
expect.anything(),
);
});
it("returns error when no flows match after pull", async () => {
const ctx = makeCtx();
const deps = makeDeps();
expandPatternsMock.mockResolvedValue([]);
pullEnvMock.mockResolvedValue(undefined);
const result = await handleHybridFlowsRun(
ctx,
"**/missing.flow.ts",
{ ...defaultFlags(), env: "my-env" },
deps,
);
expect(result).toEqual({
error: "No flows matched '**/missing.flow.ts' in env 'my-env'",
exitCode: 2,
});
expect(flowsRunMock).not.toHaveBeenCalled();
});
it("propagates pull error and skips re-glob and run", async () => {
const ctx = makeCtx();
const deps = makeDeps();
expandPatternsMock.mockResolvedValue([]);
pullEnvMock.mockResolvedValue({ error: "network error" });
const result = await handleHybridFlowsRun(
ctx,
"**/login.flow.ts",
{ ...defaultFlags(), env: "my-env" },
deps,
);
expect(result).toEqual({ error: "network error" });
expect(expandPatternsMock).toHaveBeenCalledTimes(1);
expect(flowsRunMock).not.toHaveBeenCalled();
});
it("runs all flows from envDir when pattern is undefined (cache hit)", async () => {
const ctx = makeCtx();
const deps = makeDeps();
expandPatternsMock.mockResolvedValue([
"/mock/.qawolf/my-env/a.flow.ts",
"/mock/.qawolf/my-env/b.flow.ts",
]);
await handleHybridFlowsRun(
ctx,
undefined,
{ ...defaultFlags(), env: "my-env" },
deps,
);
expect(pullEnvMock).not.toHaveBeenCalled();
expect(flowsRunMock).toHaveBeenCalledWith(
expect.anything(),
["/mock/.qawolf/my-env/a.flow.ts", "/mock/.qawolf/my-env/b.flow.ts"],
expect.anything(),
expect.anything(),
);
expect(expandPatternsMock).toHaveBeenCalledWith(
[],
expect.any(String),
expect.anything(),
);
});
it("returns error when no flows found in env after pull (pattern undefined)", async () => {
const ctx = makeCtx();
const deps = makeDeps();
expandPatternsMock.mockResolvedValue([]);
pullEnvMock.mockResolvedValue(undefined);
const result = await handleHybridFlowsRun(
ctx,
undefined,
{ ...defaultFlags(), env: "my-env" },
deps,
);
expect(result).toEqual({
error: "No flows found in env 'my-env'",
exitCode: 2,
});
expect(flowsRunMock).not.toHaveBeenCalled();
});
});