Skip to content

Commit 6b423da

Browse files
committed
fix: test error issues
1 parent 64ee9bf commit 6b423da

20 files changed

Lines changed: 1106 additions & 11619 deletions

src/__tests__/ArchiveActionExecutor.canvas.test.ts

Lines changed: 115 additions & 97 deletions
Large diffs are not rendered by default.

src/__tests__/ArchiveActionExecutor.markdown.test.ts

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
OnCompletionActionType,
1515
} from "../types/onCompletion";
1616
import { Task } from "../types/task";
17-
import { createMockPlugin } from "./mockUtils";
17+
import { createMockPlugin, createMockApp } from "./mockUtils";
1818
import TaskProgressBarPlugin from "../index";
1919

2020
// Mock vault
@@ -34,22 +34,39 @@ const mockApp = {
3434
describe("ArchiveActionExecutor - Markdown Tasks", () => {
3535
let executor: ArchiveActionExecutor;
3636
let mockContext: OnCompletionExecutionContext;
37-
let mockPlugin: TaskProgressBarPlugin;
37+
let mockPlugin: any;
38+
let mockApp: any;
3839

3940
beforeEach(() => {
4041
executor = new ArchiveActionExecutor();
42+
43+
// Create fresh mock instances for each test
4144
mockPlugin = createMockPlugin();
45+
mockApp = createMockApp();
4246

4347
// Reset mocks
4448
jest.clearAllMocks();
4549

4650
// Reset all vault method mocks to default behavior
47-
mockVault.getAbstractFileByPath.mockReset();
48-
mockVault.getFileByPath.mockReset();
49-
mockVault.read.mockReset();
50-
mockVault.modify.mockReset();
51-
mockVault.create.mockReset();
52-
mockVault.createFolder.mockReset();
51+
mockApp.vault.getAbstractFileByPath.mockReset();
52+
mockApp.vault.getFileByPath.mockReset();
53+
mockApp.vault.read.mockReset();
54+
mockApp.vault.modify.mockReset();
55+
mockApp.vault.create.mockReset();
56+
mockApp.vault.createFolder.mockReset();
57+
58+
// Mock the current date to ensure consistent test results
59+
jest.spyOn(Date.prototype, "toISOString").mockReturnValue(
60+
"2025-07-07T00:00:00.000Z"
61+
);
62+
jest.spyOn(Date.prototype, "getFullYear").mockReturnValue(2025);
63+
jest.spyOn(Date.prototype, "getMonth").mockReturnValue(6); // July (0-indexed)
64+
jest.spyOn(Date.prototype, "getDate").mockReturnValue(7);
65+
});
66+
67+
afterEach(() => {
68+
// Restore date mocks
69+
jest.restoreAllMocks();
5370
});
5471

5572
describe("Markdown Task Archiving", () => {
@@ -82,7 +99,7 @@ describe("ArchiveActionExecutor - Markdown Tasks", () => {
8299

83100
// Mock source file
84101
const mockSourceFile = { path: "source.md" };
85-
mockVault.getFileByPath
102+
mockApp.vault.getFileByPath
86103
.mockReturnValueOnce(mockSourceFile) // Source file
87104
.mockReturnValueOnce({ path: "Archive/Completed Tasks.md" }); // Archive file
88105

@@ -91,11 +108,11 @@ describe("ArchiveActionExecutor - Markdown Tasks", () => {
91108
"# Tasks\n\n- [ ] Other task\n- [x] Task with onCompletion 🏁 archive:done.md\n- [ ] Another task";
92109
const archiveContent = "# Archive\n\n## Completed Tasks\n\n";
93110

94-
mockVault.read
111+
mockApp.vault.read
95112
.mockResolvedValueOnce(sourceContent) // Read source
96113
.mockResolvedValueOnce(archiveContent); // Read archive
97114

98-
mockVault.modify.mockResolvedValue(undefined);
115+
mockApp.vault.modify.mockResolvedValue(undefined);
99116

100117
const result = await executor.execute(mockContext, archiveConfig);
101118

@@ -105,17 +122,17 @@ describe("ArchiveActionExecutor - Markdown Tasks", () => {
105122
);
106123

107124
// Verify source file was updated (task removed)
108-
const sourceModifyCall = mockVault.modify.mock.calls[0];
125+
const sourceModifyCall = mockApp.vault.modify.mock.calls[0];
109126
const updatedSourceContent = sourceModifyCall[1];
110127
expect(updatedSourceContent).toBe(
111128
"# Tasks\n\n- [ ] Other task\n- [ ] Another task"
112129
);
113130

114131
// Verify archive file was updated (task added without onCompletion metadata)
115-
const archiveModifyCall = mockVault.modify.mock.calls[1];
132+
const archiveModifyCall = mockApp.vault.modify.mock.calls[1];
116133
const updatedArchiveContent = archiveModifyCall[1];
117134
expect(updatedArchiveContent).toContain(
118-
"- [x] Task with onCompletion ✅ 2025-07-04 (from source.md)"
135+
"- [x] Task with onCompletion ✅ 2025-07-07 (from source.md)"
119136
);
120137
expect(updatedArchiveContent).not.toContain("🏁");
121138
expect(updatedArchiveContent).not.toContain("archive:done.md");
@@ -150,7 +167,7 @@ describe("ArchiveActionExecutor - Markdown Tasks", () => {
150167

151168
// Mock source file
152169
const mockSourceFile = { path: "source.md" };
153-
mockVault.getFileByPath
170+
mockApp.vault.getFileByPath
154171
.mockReturnValueOnce(mockSourceFile) // Source file
155172
.mockReturnValueOnce({ path: "Archive/Completed Tasks.md" }); // Archive file
156173

@@ -159,21 +176,21 @@ describe("ArchiveActionExecutor - Markdown Tasks", () => {
159176
"# Tasks\n- [ ] Incomplete task to archive 🏁 archive\n- [ ] Other task";
160177
const archiveContent = "# Archive\n\n## Completed Tasks\n\n";
161178

162-
mockVault.read
179+
mockApp.vault.read
163180
.mockResolvedValueOnce(sourceContent) // Read source
164181
.mockResolvedValueOnce(archiveContent); // Read archive
165182

166-
mockVault.modify.mockResolvedValue(undefined);
183+
mockApp.vault.modify.mockResolvedValue(undefined);
167184

168185
const result = await executor.execute(mockContext, archiveConfig);
169186

170187
expect(result.success).toBe(true);
171188

172189
// Verify archive file contains completed task without onCompletion metadata
173-
const archiveModifyCall = mockVault.modify.mock.calls[1];
190+
const archiveModifyCall = mockApp.vault.modify.mock.calls[1];
174191
const updatedArchiveContent = archiveModifyCall[1];
175192
expect(updatedArchiveContent).toContain(
176-
"- [x] Incomplete task to archive ✅ 2025-07-04 (from source.md)"
193+
"- [x] Incomplete task to archive ✅ 2025-07-07 (from source.md)"
177194
);
178195
expect(updatedArchiveContent).not.toContain("- [ ]"); // Should not contain incomplete checkbox
179196
expect(updatedArchiveContent).not.toContain("🏁");
@@ -208,7 +225,7 @@ describe("ArchiveActionExecutor - Markdown Tasks", () => {
208225

209226
// Mock source file
210227
const mockSourceFile = { path: "source.md" };
211-
mockVault.getFileByPath
228+
mockApp.vault.getFileByPath
212229
.mockReturnValueOnce(mockSourceFile) // Source file
213230
.mockReturnValueOnce({ path: "Archive/Completed Tasks.md" }); // Archive file
214231

@@ -217,21 +234,21 @@ describe("ArchiveActionExecutor - Markdown Tasks", () => {
217234
"- [x] Task with dataview onCompletion [onCompletion:: archive:done.md]";
218235
const archiveContent = "# Archive\n\n## Completed Tasks\n\n";
219236

220-
mockVault.read
237+
mockApp.vault.read
221238
.mockResolvedValueOnce(sourceContent) // Read source
222239
.mockResolvedValueOnce(archiveContent); // Read archive
223240

224-
mockVault.modify.mockResolvedValue(undefined);
241+
mockApp.vault.modify.mockResolvedValue(undefined);
225242

226243
const result = await executor.execute(mockContext, archiveConfig);
227244

228245
expect(result.success).toBe(true);
229246

230247
// Verify archive file contains task without dataview onCompletion metadata
231-
const archiveModifyCall = mockVault.modify.mock.calls[1];
248+
const archiveModifyCall = mockApp.vault.modify.mock.calls[1];
232249
const updatedArchiveContent = archiveModifyCall[1];
233250
expect(updatedArchiveContent).toContain(
234-
"- [x] Task with dataview onCompletion ✅ 2025-07-04 (from source.md)"
251+
"- [x] Task with dataview onCompletion ✅ 2025-07-07 (from source.md)"
235252
);
236253
expect(updatedArchiveContent).not.toContain("[onCompletion::");
237254
expect(updatedArchiveContent).not.toContain("archive:done.md");
@@ -267,7 +284,7 @@ describe("ArchiveActionExecutor - Markdown Tasks", () => {
267284

268285
// Mock source file
269286
const mockSourceFile = { path: "source.md" };
270-
mockVault.getFileByPath
287+
mockApp.vault.getFileByPath
271288
.mockReturnValueOnce(mockSourceFile) // Source file
272289
.mockReturnValueOnce({ path: "Archive/Completed Tasks.md" }); // Archive file
273290

@@ -276,21 +293,21 @@ describe("ArchiveActionExecutor - Markdown Tasks", () => {
276293
'- [x] Task with JSON onCompletion 🏁 {"type": "archive", "archiveFile": "custom.md"}';
277294
const archiveContent = "# Archive\n\n## Completed Tasks\n\n";
278295

279-
mockVault.read
296+
mockApp.vault.read
280297
.mockResolvedValueOnce(sourceContent) // Read source
281298
.mockResolvedValueOnce(archiveContent); // Read archive
282299

283-
mockVault.modify.mockResolvedValue(undefined);
300+
mockApp.vault.modify.mockResolvedValue(undefined);
284301

285302
const result = await executor.execute(mockContext, archiveConfig);
286303

287304
expect(result.success).toBe(true);
288305

289306
// Verify archive file contains task without JSON onCompletion metadata
290-
const archiveModifyCall = mockVault.modify.mock.calls[1];
307+
const archiveModifyCall = mockApp.vault.modify.mock.calls[1];
291308
const updatedArchiveContent = archiveModifyCall[1];
292309
expect(updatedArchiveContent).toContain(
293-
"- [x] Task with JSON onCompletion ✅ 2025-07-04 (from source.md)"
310+
"- [x] Task with JSON onCompletion ✅ 2025-07-07 (from source.md)"
294311
);
295312
expect(updatedArchiveContent).not.toContain("🏁");
296313
expect(updatedArchiveContent).not.toContain('{"type": "archive"');
@@ -324,7 +341,7 @@ describe("ArchiveActionExecutor - Markdown Tasks", () => {
324341
};
325342

326343
// Mock source file not found
327-
mockVault.getFileByPath.mockReturnValue(null);
344+
mockApp.vault.getFileByPath.mockReturnValue(null);
328345

329346
const result = await executor.execute(mockContext, archiveConfig);
330347

src/__tests__/CanvasTaskMatching.integration.test.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ describe("Canvas Task Matching Integration Tests", () => {
174174
// the core content "Task content only" is the same after metadata removal
175175
const canvasLine = "- [x] Task content only #new-tag";
176176

177-
const lineMatchesTask = (canvasUpdater as any).lineMatchesTask.bind(canvasUpdater);
177+
const lineMatchesTask = (canvasUpdater as any).lineMatchesTask.bind(
178+
canvasUpdater
179+
);
178180
const result = lineMatchesTask(canvasLine, task);
179181

180182
// This should now pass with the improved extractCoreTaskContent method
@@ -236,7 +238,7 @@ describe("Canvas Task Matching Integration Tests", () => {
236238
});
237239

238240
it("should handle tasks without originalMarkdown", () => {
239-
const task: Task<CanvasTaskMetadata> = {
241+
const task: any = {
240242
id: "test-task-7",
241243
content: "Task without originalMarkdown",
242244
filePath: "test.canvas",
@@ -282,9 +284,12 @@ describe("Canvas Task Matching Integration Tests", () => {
282284
};
283285

284286
// Canvas line has different metadata but same core content
285-
const canvasLine = "- [x] Important task #urgent 🏁 archive 📅 2024-12-25";
287+
const canvasLine =
288+
"- [x] Important task #urgent 🏁 archive 📅 2024-12-25";
286289

287-
const lineMatchesTask = (canvasUpdater as any).lineMatchesTask.bind(canvasUpdater);
290+
const lineMatchesTask = (canvasUpdater as any).lineMatchesTask.bind(
291+
canvasUpdater
292+
);
288293
const result = lineMatchesTask(canvasLine, task);
289294

290295
// Should match because core content "Important task" is the same

0 commit comments

Comments
 (0)