-
-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathMacroChoiceEngine.notice.test.ts
More file actions
261 lines (230 loc) · 6.68 KB
/
MacroChoiceEngine.notice.test.ts
File metadata and controls
261 lines (230 loc) · 6.68 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
249
250
251
252
253
254
255
256
257
258
259
260
261
import { beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("../quickAddSettingsTab", () => {
const defaultSettings = {
choices: [],
inputPrompt: "single-line",
devMode: false,
templateFolderPath: "",
useSelectionAsCaptureValue: true,
announceUpdates: "major",
version: "0.0.0",
globalVariables: {},
onePageInputEnabled: false,
disableOnlineFeatures: true,
enableRibbonIcon: false,
showCaptureNotification: true,
showInputCancellationNotification: true,
enableTemplatePropertyTypes: false,
ai: {
defaultModel: "Ask me",
defaultSystemPrompt: "",
promptTemplatesFolderPath: "",
showAssistant: true,
providers: [],
},
migrations: {
migrateToMacroIDFromEmbeddedMacro: true,
useQuickAddTemplateFolder: false,
incrementFileNameSettingMoveToDefaultBehavior: false,
consolidateFileExistsBehavior: false,
mutualExclusionInsertAfterAndWriteToBottomOfFile: false,
setVersionAfterUpdateModalRelease: false,
addDefaultAIProviders: false,
removeMacroIndirection: false,
migrateFileOpeningSettings: false,
backfillFileOpeningDefaults: false,
},
};
return {
DEFAULT_SETTINGS: defaultSettings,
QuickAddSettingsTab: class {},
};
});
vi.mock("../formatters/completeFormatter", () => ({
CompleteFormatter: class CompleteFormatterMock {
setDestinationFile() {}
setDestinationSourcePath() {}
},
}));
vi.mock("obsidian-dataview", () => ({
getAPI: vi.fn(),
}));
vi.mock("../main", () => ({
default: class QuickAddMock {},
}));
import type { App } from "obsidian";
import { Notice } from "obsidian";
import type IMacroChoice from "../types/choices/IMacroChoice";
import type { IChoiceExecutor } from "../IChoiceExecutor";
import type { IMacro } from "../types/macros/IMacro";
import { CommandType } from "../types/macros/CommandType";
import { MacroChoiceEngine } from "./MacroChoiceEngine";
import { MacroAbortError } from "../errors/MacroAbortError";
import { settingsStore } from "../settingsStore";
import type IChoice from "../types/choices/IChoice";
const defaultSettingsState = structuredClone(settingsStore.getState());
type NoticeTestClass = typeof Notice & {
instances: Array<{ message: string; timeout?: number }>;
};
const noticeClass = Notice as unknown as NoticeTestClass;
class CancellationTestMacroChoiceEngine extends MacroChoiceEngine {
private abortMessage: string;
constructor(
app: App,
plugin: any,
choice: IMacroChoice,
choiceExecutor: IChoiceExecutor,
variables: Map<string, unknown>,
abortMessage: string
) {
super(app, plugin, choice, choiceExecutor, variables);
this.abortMessage = abortMessage;
}
protected override executeObsidianCommand(): void {
throw new MacroAbortError(this.abortMessage);
}
}
const createTestEngine = (abortMessage: string) => {
const app = {} as App;
const plugin = { settings: settingsStore.getState() } as any;
const macro: IMacro = {
id: "macro-id",
name: "Test macro",
commands: [
{
type: CommandType.Obsidian,
} as any,
],
};
const choice: IMacroChoice = {
id: "choice-id",
name: "Test choice",
type: "Macro",
command: false,
macro,
runOnStartup: false,
};
const choiceExecutor: IChoiceExecutor = {
execute: vi.fn(),
variables: new Map<string, unknown>(),
};
const variables = new Map<string, unknown>();
return new CancellationTestMacroChoiceEngine(
app,
plugin,
choice,
choiceExecutor,
variables,
abortMessage
);
};
describe("MacroChoiceEngine cancellation notices", () => {
beforeEach(() => {
settingsStore.setState(structuredClone(defaultSettingsState));
noticeClass.instances.length = 0;
});
it("shows a cancellation notice when the setting is enabled", async () => {
settingsStore.setState({
...settingsStore.getState(),
showInputCancellationNotification: true,
});
const engine = createTestEngine("Input cancelled by user");
await engine.run();
expect(noticeClass.instances).toHaveLength(1);
expect(noticeClass.instances[0]?.message).toContain("Input cancelled by user");
});
it("suppresses cancellation notices when the setting is disabled", async () => {
settingsStore.setState({
...settingsStore.getState(),
showInputCancellationNotification: false,
});
const engine = createTestEngine("Input cancelled by user");
await engine.run();
expect(noticeClass.instances).toHaveLength(0);
});
it("still shows notices for other abort reasons", async () => {
settingsStore.setState({
...settingsStore.getState(),
showInputCancellationNotification: false,
});
const engine = createTestEngine("Invalid project name");
await engine.run();
expect(noticeClass.instances).toHaveLength(1);
expect(noticeClass.instances[0]?.message).toContain("Invalid project name");
});
describe("MacroChoiceEngine nested choice propagation", () => {
it("halts subsequent commands when a nested choice cancels", async () => {
const app = {} as App;
const plugin = { settings: settingsStore.getState() } as any;
const nestedChoice: IChoice = {
id: "nested-template",
name: "Nested Template",
type: "Template",
command: false,
};
const macro: IMacro = {
id: "macro-id",
name: "Macro with nested choice",
commands: [
{
id: "nested-command",
name: "Nested choice",
type: CommandType.NestedChoice,
choice: nestedChoice,
},
{
id: "obsidian",
name: "Should not run",
type: CommandType.Obsidian,
} as any,
],
};
const choice: IMacroChoice = {
id: "choice-id",
name: "Macro",
type: "Macro",
command: false,
macro,
runOnStartup: false,
};
let pendingAbort: MacroAbortError | null = null;
const signalAbort = vi.fn((error: MacroAbortError) => {
pendingAbort = error;
});
const consumeAbortSignal = vi.fn(() => {
const error = pendingAbort;
pendingAbort = null;
return error;
});
const choiceExecutor: IChoiceExecutor = {
variables: new Map<string, unknown>(),
execute: vi.fn(async (choiceToRun) => {
if (choiceToRun.id === nestedChoice.id) {
signalAbort(new MacroAbortError("Input cancelled by user"));
}
}),
signalAbort,
consumeAbortSignal,
};
class ObservationMacroChoiceEngine extends MacroChoiceEngine {
public obsidianExecutions = 0;
protected override executeObsidianCommand(): void {
this.obsidianExecutions += 1;
}
}
const engine = new ObservationMacroChoiceEngine(
app,
plugin,
choice,
choiceExecutor,
new Map<string, unknown>(),
);
await engine.run();
expect(choiceExecutor.execute).toHaveBeenCalledTimes(1);
expect(signalAbort).toHaveBeenCalled();
expect(signalAbort.mock.calls.at(-1)?.[0]).toBeInstanceOf(MacroAbortError);
expect(consumeAbortSignal).toHaveBeenCalledTimes(1);
expect(engine.obsidianExecutions).toBe(0);
});
});
});