forked from callumalpass/tasknotes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskCalendarSyncService.test.ts
More file actions
81 lines (66 loc) · 2.81 KB
/
Copy pathTaskCalendarSyncService.test.ts
File metadata and controls
81 lines (66 loc) · 2.81 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
import { TaskCalendarSyncService } from "../../src/services/TaskCalendarSyncService";
import { TaskInfo } from "../../src/types";
describe("TaskCalendarSyncService", () => {
let syncService: any;
let mockPlugin: any;
let mockGoogleCalendarService: any;
beforeEach(() => {
jest.useFakeTimers();
mockPlugin = {
settings: {
googleCalendarExport: {
syncOnTaskUpdate: true,
targetCalendarId: "test-calendar",
}
},
cacheManager: {
getTaskInfo: jest.fn()
},
statusManager: {
getStatusConfig: jest.fn().mockReturnValue({ label: "Todo" })
},
priorityManager: {
getPriorityConfig: jest.fn().mockReturnValue({ label: "High" })
},
i18n: {
translate: jest.fn().mockReturnValue("Untitled Task")
}
};
mockGoogleCalendarService = {
updateEvent: jest.fn().mockResolvedValue({}),
createEvent: jest.fn().mockResolvedValue({ id: "test-id" })
};
syncService = new TaskCalendarSyncService(mockPlugin, mockGoogleCalendarService);
// Mock internal methods to avoid testing downstream serialization logic which might be complex
syncService.executeTaskUpdate = jest.fn().mockResolvedValue(undefined);
});
afterEach(() => {
jest.useRealTimers();
});
it("should use the most recently passed task explicitly, avoiding stale cacheManager payloads during debounce", async () => {
const taskPath = "test/path.md";
const firstPayload: TaskInfo = {
path: taskPath,
title: "Task Title",
scheduled: "2026-04-04"
};
const secondPayload: TaskInfo = {
path: taskPath,
title: "Task Title",
scheduled: "2026-04-06" // Agent updated it to April 6
};
// Pretend the metadataCache hasn't caught up and still returns the stale task
mockPlugin.cacheManager.getTaskInfo.mockResolvedValue(firstPayload);
// Act: trigger sync twice rapidly to simulate MCP updates or user typing
syncService.updateTaskInCalendar(firstPayload);
syncService.updateTaskInCalendar(secondPayload);
// Fast-forward past the 500ms debounce
jest.advanceTimersByTime(500);
// Flush the microtask queue so the async debounce handler completes
await Promise.resolve();
await Promise.resolve();
// Assert: It should execute only once, and pass the explicit secondPayload, not the stale cache!
expect(syncService.executeTaskUpdate).toHaveBeenCalledTimes(1);
expect(syncService.executeTaskUpdate).toHaveBeenCalledWith(secondPayload);
});
});