-
-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathCaptureChoiceEngine.notice.test.ts
More file actions
308 lines (268 loc) · 7.7 KB
/
CaptureChoiceEngine.notice.test.ts
File metadata and controls
308 lines (268 loc) · 7.7 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
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/captureChoiceFormatter", () => {
class CaptureChoiceFormatterMock {
constructor() {}
setLinkToCurrentFileBehavior() {}
setTitle() {}
setDestinationFile() {}
setDestinationSourcePath() {}
setUseSelectionAsCaptureValue() {}
async formatContentOnly(content: string) {
return content;
}
async formatContentWithFile(_content: string) {
return "";
}
async formatFileName(name: string) {
return name;
}
getAndClearTemplatePropertyVars() {
return new Map();
}
}
return {
CaptureChoiceFormatter: CaptureChoiceFormatterMock,
};
});
vi.mock("../utilityObsidian", () => ({
appendToCurrentLine: vi.fn(),
getMarkdownFilesInFolder: vi.fn(async () => []),
getMarkdownFilesWithTag: vi.fn(async () => []),
insertFileLinkToActiveView: vi.fn(),
insertOnNewLineAbove: vi.fn(),
insertOnNewLineBelow: vi.fn(),
isFolder: vi.fn(() => false),
openExistingFileTab: vi.fn(() => null),
openFile: vi.fn(),
overwriteTemplaterOnce: vi.fn(),
resolveClipboardForNoteContent: vi.fn(async () => ""),
templaterParseTemplate: vi.fn(async (_app, content) => content),
getTemplater: vi.fn(() => ({})),
}));
vi.mock("three-way-merge", () => ({
default: vi.fn(() => ({})),
__esModule: true,
}));
vi.mock("src/gui/InputSuggester/inputSuggester", () => ({
default: class InputSuggesterMock {},
}));
vi.mock("../main", () => ({
default: class QuickAddMock {},
}));
vi.mock("obsidian-dataview", () => ({
getAPI: vi.fn(),
}));
import type { App } from "obsidian";
import { Notice } from "obsidian";
import { CaptureChoiceEngine } from "./CaptureChoiceEngine";
import type { IChoiceExecutor } from "../IChoiceExecutor";
import type ICaptureChoice from "../types/choices/ICaptureChoice";
import { MacroAbortError } from "../errors/MacroAbortError";
import { ChoiceAbortError } from "../errors/ChoiceAbortError";
import { settingsStore } from "../settingsStore";
const defaultSettingsState = structuredClone(settingsStore.getState());
type NoticeTestClass = typeof Notice & {
instances: Array<{ message: string; timeout?: number }>;
};
const noticeClass = Notice as unknown as NoticeTestClass;
const createCaptureChoice = (): ICaptureChoice => ({
name: "Test Capture Choice",
id: "capture-choice-id",
type: "Capture",
command: false,
captureTo: "Daily/Test.md",
captureToActiveFile: false,
createFileIfItDoesntExist: {
enabled: false,
createWithTemplate: false,
template: "",
},
format: { enabled: false, format: "{{VALUE}}" },
prepend: false,
appendLink: false,
task: false,
insertAfter: {
enabled: false,
after: "",
insertAtEnd: false,
considerSubsections: false,
createIfNotFound: false,
createIfNotFoundLocation: "",
},
newLineCapture: {
enabled: false,
direction: "below",
},
openFile: false,
fileOpening: {
location: "tab",
direction: "vertical",
mode: "source",
focus: false,
},
});
const createEngine = (abortError: Error) => {
const app = {
vault: {
adapter: {
exists: vi.fn(async () => false),
},
getAbstractFileByPath: vi.fn(),
modify: vi.fn(),
create: vi.fn(),
},
workspace: {
getActiveFile: vi.fn(() => null),
},
fileManager: {
getNewFileParent: vi.fn(() => ({ path: "" })),
},
} as unknown as App;
const plugin = { settings: settingsStore.getState() } as any;
const choiceExecutor: IChoiceExecutor = {
execute: vi.fn(),
variables: new Map<string, unknown>(),
};
const engine = new CaptureChoiceEngine(
app,
plugin,
createCaptureChoice(),
choiceExecutor,
);
(engine as any).getFormattedPathToCaptureTo = vi.fn(async () => {
throw abortError;
});
return engine;
};
describe("CaptureChoiceEngine 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 = createEngine(new MacroAbortError("Input cancelled by user"));
await engine.run();
expect(noticeClass.instances).toHaveLength(1);
expect(noticeClass.instances[0]?.message).toContain(
"Capture execution aborted: Input cancelled by user",
);
});
it("suppresses cancellation notices when the setting is disabled", async () => {
settingsStore.setState({
...settingsStore.getState(),
showInputCancellationNotification: false,
});
const engine = createEngine(new MacroAbortError("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 = createEngine(new MacroAbortError("Target file missing"));
await engine.run();
expect(noticeClass.instances).toHaveLength(1);
expect(noticeClass.instances[0]?.message).toContain(
"Capture execution aborted: Target file missing",
);
});
it("shows notices for choice abort errors even when input cancellation notifications are disabled", async () => {
settingsStore.setState({
...settingsStore.getState(),
showInputCancellationNotification: false,
});
const engine = createEngine(
new ChoiceAbortError("Insert-after target not found: '# Missing'."),
);
await engine.run();
expect(noticeClass.instances).toHaveLength(1);
expect(noticeClass.instances[0]?.message).toContain(
"Capture execution aborted: Insert-after target not found: '# Missing'.",
);
});
it("shows a notice when the target file is missing and create is disabled", async () => {
settingsStore.setState({
...settingsStore.getState(),
showInputCancellationNotification: false,
});
const app = {
vault: {
adapter: {
exists: vi.fn(async () => false),
},
getAbstractFileByPath: vi.fn(),
modify: vi.fn(),
create: vi.fn(),
},
workspace: {
getActiveFile: vi.fn(() => null),
},
fileManager: {
getNewFileParent: vi.fn(() => ({ path: "" })),
},
} as unknown as App;
const plugin = { settings: settingsStore.getState() } as any;
const choiceExecutor: IChoiceExecutor = {
execute: vi.fn(),
variables: new Map<string, unknown>(),
};
const engine = new CaptureChoiceEngine(
app,
plugin,
createCaptureChoice(),
choiceExecutor,
);
await engine.run();
expect(noticeClass.instances).toHaveLength(1);
expect(noticeClass.instances[0]?.message).toContain(
"Capture execution aborted: Target file missing",
);
});
});