Repository navigation
Expand file tree
/
Copy pathmain.test.js
More file actions
207 lines (207 loc) · 6.39 KB
/
Copy pathmain.test.js
File metadata and controls
207 lines (207 loc) · 6.39 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
const { test } = require("node:test");
const assert = require("node:assert/strict");
const { EventEmitter } = require("node:events");
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
const vm = require("node:vm");
const root = path.resolve(__dirname, "..");
async function boot(
t,
config = {},
failAttach = false,
lock = true,
failLoadLayer = false,
) {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "todotree-main-"));
t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
fs.mkdirSync(path.join(dir, "desktop"));
let userDataPath = path.join(dir, "todotree");
fs.mkdirSync(userDataPath);
const configPath = path.join(userDataPath, "config.json");
fs.writeFileSync(configPath, JSON.stringify(config));
const handlers = new Map(),
timers = [],
windows = [],
messages = [];
class Window extends EventEmitter {
constructor(options) {
super();
this.options = options;
this.bounds = { x: 10, y: 10, ...options };
windows.push(this);
this.webContents = { send: (...args) => messages.push(args) };
}
loadFile() {}
setMenu() {}
show() {}
focus() {}
isDestroyed() {
return false;
}
setBounds(b) {
this.bounds = { ...b };
}
getBounds() {
return this.bounds;
}
setAlwaysOnTop() {}
setResizable() {}
setMovable(v) {
this.movable = v;
}
setFocusable(v) {
this.focusable = v;
}
setSkipTaskbar(v) {
this.skipTaskbar = v;
}
static getAllWindows() {
return windows;
}
}
const app = new EventEmitter();
Object.assign(app, {
getPath: (key) =>
key === "desktop"
? path.join(dir, "desktop")
: key === "userData"
? userDataPath
: dir,
setPath: (key, value) => {
if (key === "userData") userDataPath = value;
},
requestSingleInstanceLock: () => lock,
whenReady: () => Promise.resolve(),
quit: () => {
app.quitCalled = true;
},
getLoginItemSettings: () => ({ openAtLogin: false }),
setLoginItemSettings: () => {},
});
const dialog = {
showOpenDialog: async () => ({ canceled: false, filePaths: [dir] }),
};
const electron = {
app,
BrowserWindow: Window,
ipcMain: { handle: (name, fn) => handlers.set(name, fn) },
dialog,
Menu: { setApplicationMenu: () => {} },
screen: {
getPrimaryDisplay: () => ({
workArea: { x: 0, y: 0, width: 1920, height: 1080 },
}),
getDisplayMatching: () => ({
workArea: { x: 0, y: 0, width: 1920, height: 1080 },
}),
},
};
vm.runInNewContext(fs.readFileSync(path.join(root, "main.js"), "utf8"), {
require: (name) =>
name === "electron"
? electron
: name === "./desktop-layer"
? (() => {
if (failLoadLayer) throw Error("Cannot find module 'koffi'");
return {
attach: () => {
if (failAttach) throw Error("unsupported");
},
detach: () => {},
ensureAttached: () => {},
};
})()
: name === "./lib/file-store"
? require("../lib/file-store")
: require(name),
__dirname: root,
process: { platform: "win32", argv: [] },
console: { error: () => {} },
setInterval: () => 0,
setTimeout: (fn) => {
timers.push(fn);
return timers.length;
},
clearTimeout: () => {},
});
await Promise.resolve();
return {
configPath,
dir,
handlers,
window: windows[0],
timers,
messages,
app,
};
}
test("saving window bounds preserves previously saved section heights and other preferences", async (t) => {
const fixture = await boot(t, {
zoneHeights: { todo: 123 },
otherPreference: "keep",
});
assert.equal(
fixture.app.getPath("userData"),
path.join(fixture.dir, "todotree"),
);
await fixture.handlers.get("save-zone-heights")(null, {
todo: 210,
done: 70,
});
fixture.window.emit("move");
fixture.timers.at(-1)();
const config = JSON.parse(fs.readFileSync(fixture.configPath, "utf8"));
assert.deepEqual(config.zoneHeights, { todo: 210, done: 70 });
assert.equal(config.otherPreference, "keep");
});
test("unsupported desktop anchoring falls back to an accessible floating window", async (t) => {
const f = await boot(t, {}, true);
f.window.emit("ready-to-show");
assert.equal(f.window.focusable, true);
assert.equal(f.window.movable, true);
assert.equal(f.window.skipTaskbar, false);
assert.equal(f.handlers.get("load-todo")().pinned, false);
});
test("a read failure never returns replacement task content; failed file switch preserves current path", async (t) => {
const f = await boot(t);
const before = f.handlers.get("get-file-path")();
const switched = await f.handlers.get("change-file")();
assert.equal(switched.success, false);
assert.equal(switched.content, undefined);
assert.equal(f.handlers.get("get-file-path")(), before);
});
test("native close requests let the renderer flush saves before quitting", async (t) => {
const f = await boot(t);
let prevented = false;
f.window.emit("close", {
preventDefault: () => {
prevented = true;
},
});
assert.equal(prevented, true);
assert.ok(f.messages.some((message) => message[0] === "close-requested"));
f.handlers.get("close-app")();
assert.equal(f.app.quitCalled, true);
});
test("a second process never creates another window", async (t) => {
const f = await boot(t, {}, false, false);
assert.equal(f.app.quitCalled, true);
assert.equal(f.window, undefined);
});
test("missing desktop-layer module degrades gracefully and second-instance never hits TDZ", async (t) => {
const f = await boot(t, {}, false, true, true);
assert.doesNotThrow(() => f.app.emit("second-instance"));
f.window.emit("ready-to-show");
assert.equal(f.window.focusable, true);
assert.equal(f.window.movable, true);
assert.equal(f.handlers.get("load-todo")().pinned, false);
});
test("floating window snaps within 15px of screen workArea edges on move", async (t) => {
const f = await boot(t, { pinned: false, bounds: { x: 100, y: 100, width: 400, height: 600 } });
f.window.bounds = { x: 9, y: 472, width: 400, height: 600 }; // y + h = 1072, within 15px of 1080
f.window.emit("move");
f.timers.at(-1)();
assert.equal(f.window.getBounds().x, 0);
assert.equal(f.window.getBounds().y, 480);
});