Skip to content

Commit 891adb3

Browse files
sandbox testing
1 parent 10e4742 commit 891adb3

7 files changed

Lines changed: 659 additions & 505 deletions

File tree

.yarnrc.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1+
compressionLevel: mixed
2+
3+
enableGlobalCache: false
4+
15
nodeLinker: pnp

esbuild.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
import dotenv from "dotenv"
2-
import esbuild from "esbuild"
1+
import dotenv from "dotenv";
2+
import esbuild from "esbuild";
33

44
dotenv.config();
55

6+
const outputDir = process.argv[2] === "test" ? "sandbox" : "build";
7+
const outfile = `${outputDir}/compiled.js`;
8+
69
const result = await esbuild.build({
710
entryPoints: [`lib/plugin.js`],
811
bundle: true,
912
format: "iife",
10-
outfile: "build/compiled.js",
13+
outfile: outfile,
1114
packages: "external",
1215
platform: "node",
1316
write: true,
1417
});
15-
console.log("Result was", result)
18+
console.log("Result was", result);

lib/plugin.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
const plugin = {
22
constants: {},
3-
// https://www.amplenote.com/help/developing_amplenote_plugins#noteOption
43
taskOption: {
5-
"Bulk Actions - Clear Overdue Tasks": {
4+
"Clear Overdue Tasks": {
65
run: async function (app, noteUUID) {
76
try {
87
// Get current time minus 1 hour
@@ -107,7 +106,7 @@ const plugin = {
107106
successCount++;
108107
} else {
109108
console.log(
110-
` ⚠ Task ${task.uuid} has no calendar dates to remove`
109+
` ⚠ Task ${task.uuid} has no calendar dates to remove`,
111110
);
112111
successCount++;
113112
}
@@ -141,10 +140,34 @@ const plugin = {
141140
}
142141
},
143142
},
144-
},
143+
"Hide Lower Priority Tasks": {
144+
run: async function (app, noteUUID) {
145+
try {
146+
const taskDomains = await app.getTaskDomains();
145147

146-
insertText: {},
148+
if (!taskDomains || taskDomains.length === 0) {
149+
await app.alert("No task domains found.");
150+
return;
151+
}
152+
153+
const tasksByDomain = {}; // Track tasks per domain
147154

155+
for (const taskDomain of taskDomains) {
156+
const domainTasks = app.getTaskDomainTasks(taskDomain.uuid);
157+
if (domainTasks.length > 0) {
158+
tasksByDomain[taskDomain.name] = domainTasks;
159+
}
160+
}
161+
162+
await app.alert(JSON.stringify(tasksByDomain));
163+
} catch (error) {
164+
console.error("Error in Bulk Reset Overdue Tasks:", error);
165+
await app.alert(`Error: ${error.message}`);
166+
}
167+
},
168+
},
169+
},
170+
insertText: {},
148171
replaceText: {},
149172
};
150173
export default plugin;

lib/plugin.test.js

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { jest } from "@jest/globals";
22
import { mockPlugin, mockApp } from "./test-helpers.js";
33

4-
// --------------------------------------------------------------------------------------
54
describe("Bulk Task Reset Plugin", () => {
65
const plugin = mockPlugin();
76
plugin.constants.isTestEnvironment = true;
@@ -11,10 +10,7 @@ describe("Bulk Task Reset Plugin", () => {
1110
app.getTaskDomains = jest.fn().mockResolvedValue([]);
1211
app.alert = jest.fn();
1312

14-
await plugin.taskOption["Bulk Actions - Clear Overdue Tasks"].run(
15-
app,
16-
"test-note-uuid"
17-
);
13+
await plugin.taskOption["Clear Overdue Tasks"].run(app, "test-note-uuid");
1814

1915
expect(app.alert).toHaveBeenCalledWith("No task domains found.");
2016
});
@@ -31,15 +27,12 @@ describe("Bulk Task Reset Plugin", () => {
3127
app.getTaskDomainTasks = jest.fn().mockReturnValue(
3228
(async function* () {
3329
yield { uuid: "task1", startAt: thirtyMinutesAgo };
34-
})()
30+
})(),
3531
);
3632

3733
app.alert = jest.fn();
3834

39-
await plugin.taskOption["Bulk Actions - Clear Overdue Tasks"].run(
40-
app,
41-
"test-note-uuid"
42-
);
35+
await plugin.taskOption["Clear Overdue Tasks"].run(app, "test-note-uuid");
4336

4437
expect(app.alert).toHaveBeenCalledWith("No overdue tasks found.");
4538
});
@@ -57,7 +50,7 @@ describe("Bulk Task Reset Plugin", () => {
5750
(async function* () {
5851
yield { uuid: "task1", startAt: yesterday, content: "Buy groceries" };
5952
yield { uuid: "task2", endAt: yesterday, content: "Call dentist" };
60-
})()
53+
})(),
6154
);
6255

6356
app.updateTask = jest.fn().mockResolvedValue(true);
@@ -66,31 +59,28 @@ describe("Bulk Task Reset Plugin", () => {
6659
.mockResolvedValueOnce(1) // User clicks "Reset All"
6760
.mockResolvedValueOnce(undefined); // Success message
6861

69-
await plugin.taskOption["Bulk Actions - Clear Overdue Tasks"].run(
70-
app,
71-
"test-note-uuid"
72-
);
62+
await plugin.taskOption["Clear Overdue Tasks"].run(app, "test-note-uuid");
7363

7464
// Check that confirmation message includes domain name, count, and task names
7565
expect(app.alert).toHaveBeenNthCalledWith(
7666
1,
7767
expect.stringContaining("Found 2 overdue tasks"),
78-
expect.any(Object)
68+
expect.any(Object),
7969
);
8070
expect(app.alert).toHaveBeenNthCalledWith(
8171
1,
8272
expect.stringContaining("My Tasks (2)"),
83-
expect.any(Object)
73+
expect.any(Object),
8474
);
8575
expect(app.alert).toHaveBeenNthCalledWith(
8676
1,
8777
expect.stringContaining("Buy groceries"),
88-
expect.any(Object)
78+
expect.any(Object),
8979
);
9080
expect(app.alert).toHaveBeenNthCalledWith(
9181
1,
9282
expect.stringContaining("Call dentist"),
93-
expect.any(Object)
83+
expect.any(Object),
9484
);
9585

9686
expect(app.updateTask).toHaveBeenCalledTimes(1);
@@ -110,16 +100,13 @@ describe("Bulk Task Reset Plugin", () => {
110100
app.getTaskDomainTasks = jest.fn().mockReturnValue(
111101
(async function* () {
112102
yield { uuid: "task1", startAt: yesterday };
113-
})()
103+
})(),
114104
);
115105

116106
app.updateTask = jest.fn();
117107
app.alert = jest.fn().mockResolvedValue(0); // User clicks "Cancel"
118108

119-
await plugin.taskOption["Bulk Actions - Clear Overdue Tasks"].run(
120-
app,
121-
"test-note-uuid"
122-
);
109+
await plugin.taskOption["Clear Overdue Tasks"].run(app, "test-note-uuid");
123110

124111
expect(app.updateTask).not.toHaveBeenCalled();
125112
});

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
"name": "plugin-template",
1717
"scripts": {
1818
"build": "node esbuild.js",
19+
"build-test": "node esbuild.js test",
1920
"test": "yarn node --experimental-vm-modules $(yarn bin jest)"
2021
},
2122
"testEnvironment": "jsdom",
2223
"type": "module",
23-
"version": "1.0.5"
24+
"version": "1.0.6"
2425
}

sandbox/compiled.js

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
(() => {
2+
// lib/plugin.js
3+
var plugin = {
4+
constants: {},
5+
taskOption: {
6+
"Clear Overdue Tasks": {
7+
run: async function(app, noteUUID) {
8+
try {
9+
const oneHourAgo = Math.floor(Date.now() / 1e3) - 3600;
10+
const taskDomains = await app.getTaskDomains();
11+
if (!taskDomains || taskDomains.length === 0) {
12+
await app.alert("No task domains found.");
13+
return;
14+
}
15+
const overdueTasks = [];
16+
const tasksByDomain = {};
17+
for (const taskDomain of taskDomains) {
18+
const domainTasks = [];
19+
for await (const task of app.getTaskDomainTasks(taskDomain.uuid)) {
20+
const taskDate = task.startAt || task.endAt;
21+
if (taskDate && taskDate < oneHourAgo) {
22+
overdueTasks.push(task);
23+
domainTasks.push(task);
24+
}
25+
}
26+
if (domainTasks.length > 0) {
27+
tasksByDomain[taskDomain.name] = domainTasks;
28+
}
29+
}
30+
if (overdueTasks.length === 0) {
31+
await app.alert("No overdue tasks found.");
32+
return;
33+
}
34+
const domainBreakdown = Object.entries(tasksByDomain).map(([domainName, tasks]) => {
35+
const taskList = tasks.map((task) => {
36+
const content = task.content || "Untitled task";
37+
const truncated = content.length > 60 ? content.substring(0, 57) + "..." : content;
38+
return ` - ${truncated}`;
39+
}).join("\n");
40+
return ` ${domainName} (${tasks.length}):
41+
${taskList}`;
42+
}).join("\n\n");
43+
const confirmMessage = `Found ${overdueTasks.length} overdue task${overdueTasks.length > 1 ? "s" : ""}:
44+
45+
${domainBreakdown}
46+
47+
Remove from calendar?`;
48+
const confirmed = await app.alert(confirmMessage, {
49+
actions: [
50+
{ label: "Cancel", icon: "cancel" },
51+
{ label: "Remove from calendar", icon: "check" }
52+
]
53+
});
54+
if (confirmed === 1) {
55+
let successCount = 0;
56+
let failCount = 0;
57+
const errors = [];
58+
for (const task of overdueTasks) {
59+
try {
60+
const updates = {};
61+
console.log(`Processing task ${task.uuid}:`);
62+
console.log(` startAt: ${task.startAt}`);
63+
console.log(` endAt: ${task.endAt}`);
64+
console.log(` hideUntil: ${task.hideUntil}`);
65+
if (task.startAt !== null && task.startAt !== void 0) {
66+
updates.startAt = null;
67+
}
68+
if (task.endAt !== null && task.endAt !== void 0) {
69+
delete updates.endAt;
70+
}
71+
if (task.hideUntil !== null && task.hideUntil !== void 0) {
72+
delete updates.hideUntil;
73+
}
74+
console.log(` Updates to apply:`, updates);
75+
if (Object.keys(updates).length > 0) {
76+
await app.updateTask(task.uuid, updates);
77+
console.log(` \u2713 Successfully updated task ${task.uuid}`);
78+
successCount++;
79+
} else {
80+
console.log(
81+
` \u26A0 Task ${task.uuid} has no calendar dates to remove`
82+
);
83+
successCount++;
84+
}
85+
} catch (error) {
86+
const errorMsg = `Task "${task.content || task.uuid}": ${error.message}`;
87+
console.error(`Failed to reset task ${task.uuid}:`, error);
88+
console.error(`Task data:`, JSON.stringify(task));
89+
errors.push(errorMsg);
90+
failCount++;
91+
}
92+
}
93+
let resultMessage = `Successfully reset ${successCount} task${successCount !== 1 ? "s" : ""}.`;
94+
if (failCount > 0) {
95+
resultMessage += `
96+
97+
${failCount} task${failCount !== 1 ? "s" : ""} failed:
98+
`;
99+
resultMessage += errors.map((e) => `\u2022 ${e}`).join("\n");
100+
}
101+
await app.alert(resultMessage);
102+
}
103+
} catch (error) {
104+
console.error("Error in Bulk Reset Overdue Tasks:", error);
105+
await app.alert(`Error: ${error.message}`);
106+
}
107+
}
108+
},
109+
"Hide Lower Priority Tasks": {
110+
run: async function(app, noteUUID) {
111+
try {
112+
const taskDomains = await app.getTaskDomains();
113+
if (!taskDomains || taskDomains.length === 0) {
114+
await app.alert("No task domains found.");
115+
return;
116+
}
117+
const tasksByDomain = {};
118+
for (const taskDomain of taskDomains) {
119+
const domainTasks = app.getTaskDomainTasks(taskDomain.uuid);
120+
if (domainTasks.length > 0) {
121+
tasksByDomain[taskDomain.name] = domainTasks;
122+
}
123+
}
124+
await app.alert(JSON.stringify(tasksByDomain));
125+
} catch (error) {
126+
console.error("Error in Bulk Reset Overdue Tasks:", error);
127+
await app.alert(`Error: ${error.message}`);
128+
}
129+
}
130+
}
131+
},
132+
insertText: {},
133+
replaceText: {}
134+
};
135+
var plugin_default = plugin;
136+
})();

0 commit comments

Comments
 (0)