-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanalysis-set-mode.test.ts
More file actions
107 lines (88 loc) · 4.02 KB
/
Copy pathanalysis-set-mode.test.ts
File metadata and controls
107 lines (88 loc) · 4.02 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
import prompts from "prompts";
import { beforeEach, describe, expect, test, vi } from "vitest";
import { makeEnvironmentConfig } from "../../test-utils/mock-config.js";
import { makeAccount } from "../../test-utils/mock-sdk.js";
import { resetInjectedPrompts } from "../../test-utils/reset-prompts.js";
const getEnvironmentConfigMock = vi.fn();
const errorHandlerMock = vi.fn((str: unknown) => {
throw new Error(String(str));
});
const successMSGMock = vi.fn();
let accountInstance: ReturnType<typeof makeAccount>;
vi.mock("@tago-io/sdk", () => ({
Account: function Account() {
return accountInstance;
},
}));
vi.mock("../../lib/config-file.js", () => ({
getEnvironmentConfig: getEnvironmentConfigMock,
}));
vi.mock("../../lib/messages.js", () => ({
errorHandler: errorHandlerMock,
infoMSG: vi.fn(),
successMSG: successMSGMock,
highlightMSG: (s: string) => s,
}));
vi.mock("../../lib/resolve-scope.js", () => ({
requireLocalScope: () => ({
scope: "local" as const,
root: "/repo",
configPath: "/repo/tagoconfig.json",
envFilePath: "/repo/.tagoio/personal.env",
configExists: true,
}),
}));
describe("analysisSetMode", () => {
// Factory — the command sorts these in place, so each test needs a fresh copy.
const makeAnalyses = () => [
{ id: "an-1", name: "Script A", run_on: "tago" },
{ id: "an-2", name: "Script B", run_on: "external" },
];
beforeEach(() => {
accountInstance = makeAccount();
getEnvironmentConfigMock.mockReset();
errorHandlerMock.mockClear();
successMSGMock.mockClear();
resetInjectedPrompts();
});
test("updates run_on for each selected analysis and emits a totals line", async () => {
const analyses = makeAnalyses();
getEnvironmentConfigMock.mockReturnValue(makeEnvironmentConfig());
accountInstance.analysis.list.mockResolvedValue(analyses);
accountInstance.analysis.edit.mockResolvedValue(undefined);
// 1st inject → chooseFromList selection, 2nd inject → pickFromList mode
prompts.inject([[analyses[0]], "external"]);
const { analysisSetMode } = await import("./analysis-set-mode.js");
await analysisSetMode(undefined as never, { environment: "prod", mode: "", filterMode: "" });
expect(accountInstance.analysis.edit).toHaveBeenCalledWith("an-1", { run_on: "external" });
const totals = successMSGMock.mock.calls.find((c) => String(c[0]).includes("Total analyses updated"));
expect(totals?.[0]).toContain("count=1");
expect(totals?.[0]).toContain("run_on=");
});
test("calls errorHandler when the account returns no analyses", async () => {
getEnvironmentConfigMock.mockReturnValue(makeEnvironmentConfig());
accountInstance.analysis.list.mockResolvedValue([]);
const { analysisSetMode } = await import("./analysis-set-mode.js");
await expect(
analysisSetMode(undefined as never, { environment: "prod", mode: "", filterMode: "" }),
).rejects.toThrow(/No analysis found/);
});
test("calls errorHandler when the environment is missing", async () => {
getEnvironmentConfigMock.mockReturnValue(makeEnvironmentConfig({ profileToken: "" }));
const { analysisSetMode } = await import("./analysis-set-mode.js");
await expect(
analysisSetMode(undefined as never, { environment: "prod", mode: "", filterMode: "" }),
).rejects.toThrow(/Environment not found/);
});
test("skips mode prompt and uses options.mode directly when provided", async () => {
const analyses = makeAnalyses();
getEnvironmentConfigMock.mockReturnValue(makeEnvironmentConfig());
accountInstance.analysis.list.mockResolvedValue([analyses[1]]);
accountInstance.analysis.edit.mockResolvedValue(undefined);
// Only chooseFromList inject — no pickFromList since mode is set
prompts.inject([[analyses[1]]]);
const { analysisSetMode } = await import("./analysis-set-mode.js");
await analysisSetMode(undefined as never, { environment: "prod", mode: "external", filterMode: "" });
expect(accountInstance.analysis.edit).toHaveBeenCalledWith("an-2", { run_on: "external" });
});
});