hs2-mock 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +46 -2
- package/index.js +27 -0
- package/lib/application.js +42 -0
- package/lib/dts.js +37 -0
- package/lib/emitter.js +19 -0
- package/lib/load.js +26 -0
- package/lib/strict.js +15 -0
- package/lib/timer.js +58 -0
- package/package.json +20 -4
- package/types/VERSION +1 -0
- package/types/commonjs.d.ts +3 -0
- package/types/hammerspoon.d.ts +14951 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Hugo Haas
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,47 @@
|
|
|
1
|
-
#
|
|
1
|
+
# hs2-mock
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A strict mock of the [Hammerspoon 2](https://github.com/Hammerspoon/Hammerspoon2) `hs` global, for testing
|
|
4
|
+
Spoons with `node:test`. It also ships the matching `hammerspoon.d.ts` for type checking.
|
|
5
|
+
|
|
6
|
+
```sh
|
|
7
|
+
npm install --save-dev hs2-mock
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
```js
|
|
11
|
+
const { createHs, loadSpoon } = require("hs2-mock");
|
|
12
|
+
|
|
13
|
+
const { hs, ctl } = createHs();
|
|
14
|
+
const spoon = loadSpoon(`${__dirname}/..`, hs);
|
|
15
|
+
|
|
16
|
+
ctl.addApp({ bundleID: "com.apple.Preview" });
|
|
17
|
+
spoon.monitor([{ bundleID: "com.apple.Preview", idleTime: 60 }]).start();
|
|
18
|
+
await ctl.advance(600);
|
|
19
|
+
assert.deepEqual(ctl.killed, ["com.apple.Preview"]);
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Reading a member the mock lacks throws `hs2-mock: hs.x.y is not mocked`. Tests can add or replace anything.
|
|
23
|
+
|
|
24
|
+
## Mocked
|
|
25
|
+
|
|
26
|
+
| Module | Members |
|
|
27
|
+
| ---------------- | ----------------------------------------- |
|
|
28
|
+
| `hs.application` | `matchingBundleID`, `on`, `off` |
|
|
29
|
+
| `hs.permissions` | `checkAccessibility` |
|
|
30
|
+
| `hs.timer` | `doAfter`, `doEvery`, `secondsSinceEpoch` |
|
|
31
|
+
|
|
32
|
+
## Controls (`ctl`)
|
|
33
|
+
|
|
34
|
+
| Control | Does |
|
|
35
|
+
| ---------------------------------------- | ----------------------------------------------------------------- |
|
|
36
|
+
| `advance(seconds)` | Moves the clock, firing timers and settling promises between them |
|
|
37
|
+
| `flush()` | Settles pending promises |
|
|
38
|
+
| `addApp({ bundleID, name, allWindows })` | Adds a running app and returns it |
|
|
39
|
+
| `activate(bundleID)` | Brings an app to the front and emits `didActivate` |
|
|
40
|
+
| `killed` | Bundle IDs of killed apps |
|
|
41
|
+
| `accessibility` | Whether Accessibility permission is granted |
|
|
42
|
+
|
|
43
|
+
## Types
|
|
44
|
+
|
|
45
|
+
Add `node_modules/hs2-mock/types/*.d.ts` to your `tsconfig.json`, with `"types": []`. The version they come
|
|
46
|
+
from is in `types/VERSION`; refresh with `mise run vendor <tag>`. `hammerspoon.d.ts` is MIT-licensed,
|
|
47
|
+
copyright the Hammerspoon contributors.
|
package/index.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const { createApplication } = require("./lib/application.js");
|
|
2
|
+
const { loadSpoon } = require("./lib/load.js");
|
|
3
|
+
const { strict } = require("./lib/strict.js");
|
|
4
|
+
const { createTimer } = require("./lib/timer.js");
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A fresh strict mock of the Hammerspoon 2 `hs` global, and the test controls.
|
|
8
|
+
*/
|
|
9
|
+
function createHs() {
|
|
10
|
+
const timer = createTimer();
|
|
11
|
+
const application = createApplication();
|
|
12
|
+
const ctl = {
|
|
13
|
+
...timer.ctl,
|
|
14
|
+
...application.ctl,
|
|
15
|
+
accessibility: true,
|
|
16
|
+
};
|
|
17
|
+
const modules = {
|
|
18
|
+
application: application.module,
|
|
19
|
+
permissions: { checkAccessibility: () => ctl.accessibility },
|
|
20
|
+
timer: timer.module,
|
|
21
|
+
};
|
|
22
|
+
for (const [name, module] of Object.entries(modules))
|
|
23
|
+
modules[name] = strict(`hs.${name}`, module);
|
|
24
|
+
return { hs: strict("hs", modules), ctl };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
module.exports = { createHs, loadSpoon };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
const { createEmitter } = require("./emitter.js");
|
|
2
|
+
const { strict } = require("./strict.js");
|
|
3
|
+
|
|
4
|
+
function createApplication() {
|
|
5
|
+
const apps = new Map();
|
|
6
|
+
const killed = [];
|
|
7
|
+
const events = createEmitter();
|
|
8
|
+
let frontmost = null;
|
|
9
|
+
|
|
10
|
+
const module = {
|
|
11
|
+
matchingBundleID: (bundleID) => apps.get(bundleID) ?? null,
|
|
12
|
+
on: events.on,
|
|
13
|
+
off: events.off,
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
function addApp({ bundleID, name = bundleID, allWindows = [] }) {
|
|
17
|
+
const app = strict("HSApplication", {
|
|
18
|
+
bundleID,
|
|
19
|
+
name,
|
|
20
|
+
allWindows,
|
|
21
|
+
get isActive() {
|
|
22
|
+
return frontmost === app;
|
|
23
|
+
},
|
|
24
|
+
kill() {
|
|
25
|
+
killed.push(bundleID);
|
|
26
|
+
apps.delete(bundleID);
|
|
27
|
+
return true;
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
apps.set(bundleID, app);
|
|
31
|
+
return app;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function activate(bundleID) {
|
|
35
|
+
frontmost = apps.get(bundleID) ?? addApp({ bundleID });
|
|
36
|
+
events.emit("didActivate", frontmost);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return { module, ctl: { addApp, activate, killed } };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
module.exports = { createApplication };
|
package/lib/dts.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// Reads member names from the vendored, generated hammerspoon.d.ts. A line
|
|
2
|
+
// parser is enough for its regular layout, and TypeScript 7 has no compiler API.
|
|
3
|
+
const fs = require("node:fs");
|
|
4
|
+
const path = require("node:path");
|
|
5
|
+
|
|
6
|
+
const OPEN = /^declare (?:namespace|class|interface) ([\w.]+)/;
|
|
7
|
+
const MEMBER = {
|
|
8
|
+
namespace: /^ {4}(?:function|const|let|var) (\w+)/,
|
|
9
|
+
type: /^ {4}(?:static )?(?:readonly )?(\w+)\??[(:<]/,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/** @returns {Map<string, Set<string>>} member names by namespace or class */
|
|
13
|
+
function declarations(
|
|
14
|
+
file = path.join(__dirname, "..", "types", "hammerspoon.d.ts"),
|
|
15
|
+
) {
|
|
16
|
+
const found = new Map();
|
|
17
|
+
let members;
|
|
18
|
+
let pattern;
|
|
19
|
+
for (const line of fs.readFileSync(file, "utf8").split("\n")) {
|
|
20
|
+
const open = OPEN.exec(line);
|
|
21
|
+
if (open) {
|
|
22
|
+
members = found.get(open[1]) ?? new Set();
|
|
23
|
+
found.set(open[1], members);
|
|
24
|
+
pattern = line.startsWith("declare namespace")
|
|
25
|
+
? MEMBER.namespace
|
|
26
|
+
: MEMBER.type;
|
|
27
|
+
} else if (line.startsWith("}")) {
|
|
28
|
+
members = undefined;
|
|
29
|
+
} else if (members) {
|
|
30
|
+
const member = pattern.exec(line);
|
|
31
|
+
if (member) members.add(member[1]);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return found;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
module.exports = { declarations };
|
package/lib/emitter.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
function createEmitter() {
|
|
2
|
+
const listeners = new Map();
|
|
3
|
+
return {
|
|
4
|
+
on(event, listener) {
|
|
5
|
+
listeners.set(event, [...(listeners.get(event) ?? []), listener]);
|
|
6
|
+
},
|
|
7
|
+
off(event, listener) {
|
|
8
|
+
listeners.set(
|
|
9
|
+
event,
|
|
10
|
+
(listeners.get(event) ?? []).filter((l) => l !== listener),
|
|
11
|
+
);
|
|
12
|
+
},
|
|
13
|
+
emit(event, ...args) {
|
|
14
|
+
for (const listener of listeners.get(event) ?? []) listener(...args);
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
module.exports = { createEmitter };
|
package/lib/load.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
const fs = require("node:fs");
|
|
2
|
+
const path = require("node:path");
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Loads a spoon the way hs.spoons.load() does: its modules run afresh, spoon.json
|
|
6
|
+
* metadata is set on the export, then init() is called.
|
|
7
|
+
* @param {string} dir the spoon's directory
|
|
8
|
+
* @param {object} hs the mock to install as the `hs` global
|
|
9
|
+
*/
|
|
10
|
+
function loadSpoon(dir, hs) {
|
|
11
|
+
const root = fs.realpathSync(dir);
|
|
12
|
+
for (const file of Object.keys(require.cache)) {
|
|
13
|
+
if (file.startsWith(root + path.sep)) delete require.cache[file];
|
|
14
|
+
}
|
|
15
|
+
Object.assign(globalThis, { hs });
|
|
16
|
+
const manifest = JSON.parse(
|
|
17
|
+
fs.readFileSync(path.join(root, "spoon.json"), "utf8"),
|
|
18
|
+
);
|
|
19
|
+
const spoon = require(path.join(root, "init.js"));
|
|
20
|
+
const { name, author, version, description } = manifest;
|
|
21
|
+
Object.assign(spoon, { name, author, version, description });
|
|
22
|
+
spoon.init?.();
|
|
23
|
+
return spoon;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
module.exports = { loadSpoon };
|
package/lib/strict.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Keys that Node and promise resolution probe on any object.
|
|
2
|
+
const PROBED = new Set(["then", "toJSON", "inspect", "constructor"]);
|
|
3
|
+
|
|
4
|
+
/** Makes reading a member the mock lacks an error instead of undefined. */
|
|
5
|
+
function strict(name, object) {
|
|
6
|
+
return new Proxy(object, {
|
|
7
|
+
get(target, key, receiver) {
|
|
8
|
+
if (typeof key === "symbol" || key in target || PROBED.has(key))
|
|
9
|
+
return Reflect.get(target, key, receiver);
|
|
10
|
+
throw new Error(`hs2-mock: ${name}.${key} is not mocked`);
|
|
11
|
+
},
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
module.exports = { strict };
|
package/lib/timer.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
const { strict } = require("./strict.js");
|
|
2
|
+
|
|
3
|
+
const EPOCH = 1_800_000_000;
|
|
4
|
+
const flush = () => new Promise((resolve) => setImmediate(resolve));
|
|
5
|
+
|
|
6
|
+
function createTimer() {
|
|
7
|
+
let now = EPOCH;
|
|
8
|
+
const active = new Set();
|
|
9
|
+
|
|
10
|
+
function schedule(interval, callback, repeats) {
|
|
11
|
+
const state = { due: now + interval };
|
|
12
|
+
const timer = {
|
|
13
|
+
interval,
|
|
14
|
+
repeats,
|
|
15
|
+
start() {
|
|
16
|
+
state.due = now + interval;
|
|
17
|
+
active.add(state);
|
|
18
|
+
return timer;
|
|
19
|
+
},
|
|
20
|
+
stop() {
|
|
21
|
+
active.delete(state);
|
|
22
|
+
return timer;
|
|
23
|
+
},
|
|
24
|
+
running: () => active.has(state),
|
|
25
|
+
};
|
|
26
|
+
state.fire = () => {
|
|
27
|
+
if (repeats) state.due += interval;
|
|
28
|
+
else active.delete(state);
|
|
29
|
+
callback();
|
|
30
|
+
};
|
|
31
|
+
return strict("HSTimer", timer).start();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const module = {
|
|
35
|
+
secondsSinceEpoch: () => now,
|
|
36
|
+
doAfter: (seconds, callback) => schedule(seconds, callback, false),
|
|
37
|
+
doEvery: (interval, callback) => schedule(interval, callback, true),
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
async function advance(seconds) {
|
|
41
|
+
const end = now + seconds;
|
|
42
|
+
await flush();
|
|
43
|
+
for (;;) {
|
|
44
|
+
const next = [...active]
|
|
45
|
+
.filter((t) => t.due <= end)
|
|
46
|
+
.sort((a, b) => a.due - b.due)[0];
|
|
47
|
+
if (!next) break;
|
|
48
|
+
now = next.due;
|
|
49
|
+
next.fire();
|
|
50
|
+
await flush();
|
|
51
|
+
}
|
|
52
|
+
now = end;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return { module, ctl: { advance, flush } };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
module.exports = { createTimer };
|
package/package.json
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hs2-mock",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Strict mock of the Hammerspoon 2 hs global for testing Spoons under node:test",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/hugoh/hs2-mock.git"
|
|
9
|
+
},
|
|
10
|
+
"main": "index.js",
|
|
11
|
+
"files": [
|
|
12
|
+
"index.js",
|
|
13
|
+
"lib",
|
|
14
|
+
"types"
|
|
15
|
+
],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"test": "node --test"
|
|
18
|
+
},
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=22"
|
|
21
|
+
}
|
|
22
|
+
}
|
package/types/VERSION
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
1.0.0-beta.4
|