-
-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathMacroChoiceEngine.conditional.test.ts
More file actions
225 lines (194 loc) · 5.27 KB
/
MacroChoiceEngine.conditional.test.ts
File metadata and controls
225 lines (194 loc) · 5.27 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
import { describe, expect, it, vi, beforeEach, afterEach, afterAll } from "vitest";
vi.mock("../quickAddApi", () => ({
QuickAddApi: {
GetApi: vi.fn(),
},
}));
vi.mock("../gui/GenericSuggester/genericSuggester", () => ({
default: class GenericSuggesterMock {
static Suggest() {
return Promise.resolve(undefined);
}
},
}));
vi.mock("../main", () => ({
default: class QuickAddMock {},
}));
vi.mock("../gui/choiceList/ChoiceView.svelte", () => ({}));
vi.mock("../quickAddSettingsTab", () => ({
DEFAULT_SETTINGS: {},
QuickAddSettingsTab: class {},
}));
vi.mock("../settingsStore", () => ({
settingsStore: {
getState: () => ({ ai: {}, disableOnlineFeatures: false }),
},
}));
vi.mock("../formatters/completeFormatter", () => ({
CompleteFormatter: class CompleteFormatterMock {
constructor() {}
setDestinationFile() {}
setDestinationSourcePath() {}
},
}));
vi.mock("../ai/AIAssistant", () => ({
runAIAssistant: vi.fn(),
}));
vi.mock("../ai/aiHelpers", () => ({
getModelByName: vi.fn(),
getModelNames: vi.fn().mockReturnValue([]),
getModelProvider: vi.fn().mockReturnValue({ apiKey: "" }),
}));
import type { App } from "obsidian";
import { MacroChoiceEngine } from "./MacroChoiceEngine";
import { ConditionalCommand } from "../types/macros/Conditional/ConditionalCommand";
import { ObsidianCommand } from "../types/macros/ObsidianCommand";
import type { IMacro } from "../types/macros/IMacro";
import type IMacroChoice from "../types/choices/IMacroChoice";
import type { IChoiceExecutor } from "../IChoiceExecutor";
import { QuickAddApi } from "../quickAddApi";
const createConditionalCommand = (
condition: ConditionalCommand["condition"],
thenId: string,
elseId: string
) => {
const thenCommand = new ObsidianCommand("Then command", thenId);
thenCommand.generateId();
const elseCommand = new ObsidianCommand("Else command", elseId);
elseCommand.generateId();
return new ConditionalCommand({
condition,
thenCommands: [thenCommand],
elseCommands: [elseCommand],
});
};
const createEngine = (
command: ConditionalCommand,
variables: Record<string, unknown>
) => {
const executeCommandById = vi.fn();
const app = {
commands: {
executeCommandById,
},
} as unknown as App;
const plugin = {
getChoiceById: vi.fn(),
getChoiceByName: vi.fn(),
} as unknown as any;
const macro: IMacro = {
name: "Test macro",
id: "macro-id",
commands: [command],
};
const choice: IMacroChoice = {
name: "Test choice",
id: "choice-id",
type: "Macro",
command: false,
macro,
runOnStartup: false,
};
const choiceExecutor: IChoiceExecutor = {
execute: vi.fn(),
variables: new Map<string, unknown>(),
};
const variablesMap = new Map<string, unknown>(Object.entries(variables));
const engine = new MacroChoiceEngine(
app,
plugin,
choice,
choiceExecutor,
variablesMap
);
return { engine, executeCommandById, choiceExecutor };
};
describe("MacroChoiceEngine conditional commands", () => {
const getApiMock = QuickAddApi.GetApi as unknown as ReturnType<typeof vi.fn>;
getApiMock.mockReturnValue({});
beforeEach(() => {
vi.clearAllMocks();
});
afterEach(() => {
getApiMock.mockClear();
});
afterAll(() => {
getApiMock.mockReset();
});
it("runs then-branch commands when condition is true", async () => {
const conditional = createConditionalCommand(
{
mode: "variable",
variableName: "status",
operator: "equals",
valueType: "string",
expectedValue: "ready",
},
"then-id",
"else-id"
);
const { engine, executeCommandById } = createEngine(conditional, {
status: "ready",
});
await engine.run();
expect(executeCommandById).toHaveBeenCalledWith("then-id");
expect(executeCommandById).not.toHaveBeenCalledWith("else-id");
});
it("runs else-branch commands when condition is false", async () => {
const conditional = createConditionalCommand(
{
mode: "variable",
variableName: "status",
operator: "equals",
valueType: "string",
expectedValue: "ready",
},
"then-id",
"else-id"
);
const { engine, executeCommandById } = createEngine(conditional, {
status: "pending",
});
await engine.run();
expect(executeCommandById).toHaveBeenCalledWith("else-id");
expect(executeCommandById).not.toHaveBeenCalledWith("then-id");
});
it("skips missing else branch without errors", async () => {
const conditional = new ConditionalCommand({
condition: {
mode: "variable",
variableName: "flag",
operator: "isFalsy",
valueType: "boolean",
},
thenCommands: [new ObsidianCommand("Then", "then-id")],
elseCommands: [],
});
const { engine, executeCommandById } = createEngine(conditional, {
flag: true,
});
await engine.run();
expect(executeCommandById).not.toHaveBeenCalled();
});
it("pulls variables written through QuickAdd API helpers", async () => {
const conditional = createConditionalCommand(
{
mode: "variable",
variableName: "status",
operator: "equals",
valueType: "string",
expectedValue: "ready",
},
"then-id",
"else-id"
);
const { engine, executeCommandById, choiceExecutor } = createEngine(
conditional,
{}
);
choiceExecutor.variables.set("status", "ready");
await engine.run();
expect(executeCommandById).toHaveBeenCalledWith("then-id");
expect(executeCommandById).not.toHaveBeenCalledWith("else-id");
});
});