forked from super-productivity/super-productivity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.js
More file actions
289 lines (241 loc) · 8.13 KB
/
plugin.js
File metadata and controls
289 lines (241 loc) · 8.13 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
// API Test Plugin - Comprehensive test of all plugin API methods
console.log('API Test Plugin initializing...', PluginAPI);
// Plugin configuration
let pluginConfig = null;
// Load plugin configuration on startup
async function loadPluginConfig() {
try {
pluginConfig = await PluginAPI.getConfig();
console.log('Plugin configuration loaded:', pluginConfig);
if (pluginConfig) {
// Apply configuration settings
if (pluginConfig.logLevel) {
console.log(`Log level set to: ${pluginConfig.logLevel}`);
}
if (pluginConfig.enabled === false) {
console.log('Plugin is disabled by configuration');
PluginAPI.showSnack({
msg: 'API Test Plugin is disabled',
type: 'WARNING',
});
}
} else {
console.log('No configuration found, using defaults');
}
} catch (error) {
console.error('Failed to load plugin configuration:', error);
}
}
// Initialize configuration
loadPluginConfig();
// Helper function to log results
function logResult(method, result, error = null) {
// Check log level from config
const logLevel = pluginConfig?.logLevel || 'info';
if (error) {
console.error(`API Test - ${method} failed:`, error);
PluginAPI.showSnack({
msg: `${method} failed: ${error.message}`,
type: 'ERROR',
});
} else {
if (logLevel === 'debug' || logLevel === 'info') {
console.log(`API Test - ${method} success:`, result);
}
}
}
// Test data persistence
async function testDataPersistence() {
console.log('=== Testing Data Persistence ===');
try {
// Save some test data
const testData = {
timestamp: new Date().toISOString(),
counter: Math.floor(Math.random() * 100),
message: 'Hello from API Test Plugin!',
};
await PluginAPI.persistDataSynced(JSON.stringify(testData));
logResult('persistDataSynced', 'Data saved successfully');
// Load it back
const loadedData = await PluginAPI.loadSyncedData();
const parsed = loadedData ? JSON.parse(loadedData) : null;
logResult('loadSyncedData', parsed);
PluginAPI.showSnack({
msg: 'Data persistence test passed',
type: 'SUCCESS',
});
} catch (error) {
logResult('Data Persistence', null, error);
}
}
// Test task operations
async function testTaskOperations() {
console.log('=== Testing Task Operations ===');
try {
// Get all tasks
const allTasks = await PluginAPI.getTasks();
logResult('getTasks', `Found ${allTasks.length} tasks`);
// Get archived tasks
const archivedTasks = await PluginAPI.getArchivedTasks();
logResult('getArchivedTasks', `Found ${archivedTasks.length} archived tasks`);
// Get current context tasks
const contextTasks = await PluginAPI.getCurrentContextTasks();
logResult('getCurrentContextTasks', `Found ${contextTasks.length} context tasks`);
// Create a test task
const newTaskId = await PluginAPI.addTask({
title: 'API Test Task - ' + new Date().toLocaleTimeString(),
notes: 'This task was created by the API Test Plugin to test the addTask method',
timeEstimate: 1800000, // 30 minutes in milliseconds
});
logResult('addTask', `Created task with ID: ${newTaskId}`);
// Update the task
if (newTaskId) {
await PluginAPI.updateTask(newTaskId, {
notes: 'Updated: This task was modified by the API Test Plugin',
timeEstimate: 3600000, // 1 hour
});
logResult('updateTask', 'Task updated successfully');
}
PluginAPI.showSnack({
msg: 'Task operations test completed',
type: 'SUCCESS',
});
} catch (error) {
logResult('Task Operations', null, error);
}
}
// Test project operations
async function testProjectOperations() {
console.log('=== Testing Project Operations ===');
try {
// Get all projects
const projects = await PluginAPI.getAllProjects();
logResult('getAllProjects', `Found ${projects.length} projects`);
// Create a test project
const newProject = await PluginAPI.addProject({
title: 'API Test Project - ' + new Date().toLocaleDateString(),
themeColor: '#' + Math.floor(Math.random() * 16777215).toString(16), // Random color
});
logResult('addProject', `Created project: ${newProject.title}`);
// Update the project
if (newProject && newProject.id) {
await PluginAPI.updateProject(newProject.id, {
title: newProject.title + ' (Updated)',
});
logResult('updateProject', 'Project updated successfully');
}
PluginAPI.showSnack({
msg: 'Project operations test completed',
type: 'SUCCESS',
});
} catch (error) {
logResult('Project Operations', null, error);
}
}
// Test tag operations
async function testTagOperations() {
console.log('=== Testing Tag Operations ===');
try {
// Get all tags
const tags = await PluginAPI.getAllTags();
logResult('getAllTags', `Found ${tags.length} tags`);
// Create a test tag
const newTag = await PluginAPI.addTag({
title: 'API Test Tag - ' + Date.now(),
color: '#' + Math.floor(Math.random() * 16777215).toString(16), // Random color
});
logResult('addTag', `Created tag: ${newTag.title}`);
// Update the tag
if (newTag && newTag.id) {
await PluginAPI.updateTag(newTag.id, {
title: newTag.title + ' (Updated)',
});
logResult('updateTag', 'Tag updated successfully');
}
PluginAPI.showSnack({
msg: 'Tag operations test completed',
type: 'SUCCESS',
});
} catch (error) {
logResult('Tag Operations', null, error);
}
}
// Test UI methods
function testUIOperations() {
console.log('=== Testing UI Operations ===');
// Test different snack types
const snackTypes = ['SUCCESS', 'ERROR', 'INFO'];
let snackIndex = 0;
const showNextSnack = () => {
if (snackIndex < snackTypes.length) {
const type = snackTypes[snackIndex];
PluginAPI.showSnack({
msg: `Test ${type} snack message`,
type: type,
ico: type === 'SUCCESS' ? 'check' : type === 'ERROR' ? 'error' : 'info',
});
snackIndex++;
setTimeout(showNextSnack, 1500);
}
};
showNextSnack();
// Test notification
setTimeout(() => {
PluginAPI.notify({
title: 'API Test Plugin',
body: 'This is a test notification from the API Test Plugin',
});
logResult('notify', 'Notification sent');
}, 5000);
}
// Register hooks to test them
PluginAPI.registerHook(PluginAPI.Hooks.TASK_COMPLETE, (taskData) => {
console.log('API Test - TASK_COMPLETE hook fired:', taskData);
});
PluginAPI.registerHook(PluginAPI.Hooks.TASK_UPDATE, (taskData) => {
console.log('API Test - TASK_UPDATE hook fired:', taskData);
});
PluginAPI.registerHook(PluginAPI.Hooks.TASK_DELETE, (taskData) => {
console.log('API Test - TASK_DELETE hook fired:', taskData);
});
PluginAPI.registerHook(PluginAPI.Hooks.CURRENT_TASK_CHANGE, (taskData) => {
console.log('API Test - CURRENT_TASK_CHANGE hook fired:', taskData);
});
// Register UI elements
PluginAPI.registerShortcut({
id: 'run_api_tests',
label: 'Run All API Tests',
onExec: runAllTests,
});
// Main test runner
async function runAllTests() {
PluginAPI.showSnack({
msg: 'Running all API tests...',
type: 'INFO',
ico: 'play_arrow',
});
console.log('========================================');
console.log('Starting API Test Plugin Test Suite');
console.log('========================================');
// Run tests sequentially
await testDataPersistence();
await new Promise((resolve) => setTimeout(resolve, 1000));
await testTaskOperations();
await new Promise((resolve) => setTimeout(resolve, 1000));
await testProjectOperations();
await new Promise((resolve) => setTimeout(resolve, 1000));
await testTagOperations();
await new Promise((resolve) => setTimeout(resolve, 1000));
testUIOperations();
console.log('========================================');
console.log('API Test Suite Completed');
console.log('========================================');
}
// Show initialization message
setTimeout(() => {
PluginAPI.showSnack({
msg: 'API Test Plugin ready! Use header button to run tests.',
type: 'SUCCESS',
ico: 'check',
});
}, 500);