hs2-mock 0.1.0 → 0.2.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/README.md CHANGED
@@ -23,22 +23,31 @@ Reading a member the mock lacks throws `hs2-mock: hs.x.y is not mocked`. Tests c
23
23
 
24
24
  ## Mocked
25
25
 
26
- | Module | Members |
27
- | ---------------- | ----------------------------------------- |
28
- | `hs.application` | `matchingBundleID`, `on`, `off` |
29
- | `hs.permissions` | `checkAccessibility` |
30
- | `hs.timer` | `doAfter`, `doEvery`, `secondsSinceEpoch` |
26
+ | Module | Members |
27
+ | ---------------- | -------------------------------------------- |
28
+ | `hs.application` | `frontmost`, `matchingBundleID`, `on`, `off` |
29
+ | `hs.ax` | `applicationElement`, `windowElement` |
30
+ | `hs.hotkey` | `bind`, `create` |
31
+ | `hs.permissions` | `checkAccessibility` |
32
+ | `hs.timer` | `doAfter`, `doEvery`, `secondsSinceEpoch` |
33
+ | `hs.ui` | `alert` (`show`) |
34
+ | `hs.urlevent` | `openURL` |
31
35
 
32
36
  ## Controls (`ctl`)
33
37
 
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 |
38
+ | Control | Does |
39
+ | ----------------------------------------- | -------------------------------------------------------------------------------------- |
40
+ | `advance(seconds)` | Moves the clock, firing timers and settling promises between them |
41
+ | `flush()` | Settles pending promises |
42
+ | `addApp({ bundleID, name, allWindows })` | Adds a running app and returns it |
43
+ | `activate(bundleID)` | Brings an app to the front and emits `didActivate`; `null` for one without a bundle ID |
44
+ | `hotkeys` | Created hotkeys that have not been destroyed |
45
+ | `element({ role, children, attributes })` | An accessibility element |
46
+ | `window(element)` | A window whose element it is, for `addApp`'s `focusedWindow` or `mainWindow` |
47
+ | `url(text)` | A URL attribute value, as `hs.ax` returns it |
48
+ | `alerts`, `openedURLs` | What was shown and opened |
49
+ | `killed` | Bundle IDs of killed apps |
50
+ | `accessibility` | Whether Accessibility permission is granted |
42
51
 
43
52
  ## Types
44
53
 
package/index.js CHANGED
@@ -1,7 +1,10 @@
1
1
  const { createApplication } = require("./lib/application.js");
2
+ const { createAx } = require("./lib/ax.js");
3
+ const { createHotkey } = require("./lib/hotkey.js");
2
4
  const { loadSpoon } = require("./lib/load.js");
3
5
  const { strict } = require("./lib/strict.js");
4
6
  const { createTimer } = require("./lib/timer.js");
7
+ const { createUi } = require("./lib/ui.js");
5
8
 
6
9
  /**
7
10
  * A fresh strict mock of the Hammerspoon 2 `hs` global, and the test controls.
@@ -9,15 +12,25 @@ const { createTimer } = require("./lib/timer.js");
9
12
  function createHs() {
10
13
  const timer = createTimer();
11
14
  const application = createApplication();
15
+ const hotkey = createHotkey();
16
+ const ax = createAx();
17
+ const ui = createUi();
12
18
  const ctl = {
13
19
  ...timer.ctl,
14
20
  ...application.ctl,
21
+ ...hotkey.ctl,
22
+ ...ax.ctl,
23
+ ...ui.ctl,
15
24
  accessibility: true,
16
25
  };
17
26
  const modules = {
18
27
  application: application.module,
28
+ ax: ax.module,
29
+ hotkey: hotkey.module,
19
30
  permissions: { checkAccessibility: () => ctl.accessibility },
20
31
  timer: timer.module,
32
+ ui: ui.ui,
33
+ urlevent: ui.urlevent,
21
34
  };
22
35
  for (const [name, module] of Object.entries(modules))
23
36
  modules[name] = strict(`hs.${name}`, module);
@@ -1,3 +1,4 @@
1
+ const { element } = require("./ax.js");
1
2
  const { createEmitter } = require("./emitter.js");
2
3
  const { strict } = require("./strict.js");
3
4
 
@@ -9,15 +10,26 @@ function createApplication() {
9
10
 
10
11
  const module = {
11
12
  matchingBundleID: (bundleID) => apps.get(bundleID) ?? null,
13
+ frontmost: () => frontmost,
12
14
  on: events.on,
13
15
  off: events.off,
14
16
  };
15
17
 
16
- function addApp({ bundleID, name = bundleID, allWindows = [] }) {
18
+ function addApp({
19
+ bundleID,
20
+ name = bundleID,
21
+ allWindows = [],
22
+ focusedWindow = null,
23
+ mainWindow = null,
24
+ }) {
25
+ const axElement = element({ role: "AXApplication" });
17
26
  const app = strict("HSApplication", {
18
27
  bundleID,
19
28
  name,
20
29
  allWindows,
30
+ focusedWindow,
31
+ mainWindow,
32
+ axElement: () => axElement,
21
33
  get isActive() {
22
34
  return frontmost === app;
23
35
  },
@@ -32,7 +44,8 @@ function createApplication() {
32
44
  }
33
45
 
34
46
  function activate(bundleID) {
35
- frontmost = apps.get(bundleID) ?? addApp({ bundleID });
47
+ frontmost =
48
+ apps.get(bundleID) ?? addApp({ bundleID, name: bundleID ?? "unbundled" });
36
49
  events.emit("didActivate", frontmost);
37
50
  }
38
51
 
package/lib/ax.js ADDED
@@ -0,0 +1,34 @@
1
+ const { inspect } = require("node:util");
2
+ const { strict } = require("./strict.js");
3
+
4
+ function element({ role, children = [], attributes = {} }) {
5
+ const values = { ...attributes };
6
+ return strict("HSAXElement", {
7
+ role,
8
+ children: () => children,
9
+ attributeValue: (name) => values[name] ?? null,
10
+ setAttributeValue(name, value) {
11
+ values[name] = value;
12
+ return true;
13
+ },
14
+ });
15
+ }
16
+
17
+ const window = (axElement) =>
18
+ strict("HSWindow", { axElement: () => axElement });
19
+
20
+ /** hs.ax returns URL attributes as an NSURL that only util.inspect() reads. */
21
+ const url = (text) => ({
22
+ toString: () => "[object NSURL]",
23
+ [inspect.custom]: () => text,
24
+ });
25
+
26
+ function createAx() {
27
+ const module = {
28
+ applicationElement: (app) => app.axElement(),
29
+ windowElement: (w) => w.axElement(),
30
+ };
31
+ return { module, ctl: { element, window, url } };
32
+ }
33
+
34
+ module.exports = { createAx, element };
package/lib/hotkey.js ADDED
@@ -0,0 +1,41 @@
1
+ const { strict } = require("./strict.js");
2
+
3
+ function createHotkey() {
4
+ const hotkeys = [];
5
+
6
+ const module = {
7
+ create(mods, key, onPressed) {
8
+ let enabled = false;
9
+ const hotkey = strict("HSHotkey", {
10
+ mods,
11
+ key,
12
+ onPressed,
13
+ enable() {
14
+ enabled = true;
15
+ return true;
16
+ },
17
+ disable() {
18
+ enabled = false;
19
+ },
20
+ isEnabled: () => enabled,
21
+ destroy() {
22
+ enabled = false;
23
+ const index = hotkeys.indexOf(hotkey);
24
+ if (index >= 0) hotkeys.splice(index, 1);
25
+ },
26
+ });
27
+ hotkeys.push(hotkey);
28
+ return hotkey;
29
+ },
30
+ };
31
+
32
+ module.bind = (mods, key, onPressed) => {
33
+ const hotkey = module.create(mods, key, onPressed);
34
+ hotkey.enable();
35
+ return hotkey;
36
+ };
37
+
38
+ return { module, ctl: { hotkeys } };
39
+ }
40
+
41
+ module.exports = { createHotkey };
package/lib/load.js CHANGED
@@ -1,9 +1,40 @@
1
1
  const fs = require("node:fs");
2
2
  const path = require("node:path");
3
3
 
4
+ /** Stands in for the bindHotkeys() that hs.spoons.load() makes from `bindings`. */
5
+ function installBindHotkeys(spoon, name) {
6
+ const bound = new Map();
7
+ const release = (action) => {
8
+ bound.get(action)?.destroy();
9
+ bound.delete(action);
10
+ };
11
+ spoon.bindHotkeys = (mapping) => {
12
+ for (const [action, spec] of Object.entries(mapping)) {
13
+ if (!Object.hasOwn(spoon.bindings, action)) {
14
+ throw new Error(`${name}.bindHotkeys(): no action '${action}'`);
15
+ }
16
+ release(action);
17
+ if (!spec) continue;
18
+ const [mods, key] = Array.isArray(spec) ? spec : [spec.mods, spec.key];
19
+ const binding = spoon.bindings[action];
20
+ const pressed = typeof binding === "function" ? binding : binding.pressed;
21
+ bound.set(
22
+ action,
23
+ globalThis.hs.hotkey.bind(mods, key, pressed, null, null),
24
+ );
25
+ }
26
+ return spoon;
27
+ };
28
+ spoon.unbindHotkeys = (actions = [...bound.keys()]) => {
29
+ for (const action of actions) release(action);
30
+ return spoon;
31
+ };
32
+ }
33
+
4
34
  /**
5
35
  * 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.
36
+ * metadata is set on the export, bindHotkeys() is made from `bindings`, then
37
+ * init() is called.
7
38
  * @param {string} dir the spoon's directory
8
39
  * @param {object} hs the mock to install as the `hs` global
9
40
  */
@@ -19,6 +50,7 @@ function loadSpoon(dir, hs) {
19
50
  const spoon = require(path.join(root, "init.js"));
20
51
  const { name, author, version, description } = manifest;
21
52
  Object.assign(spoon, { name, author, version, description });
53
+ if (spoon.bindings) installBindHotkeys(spoon, name);
22
54
  spoon.init?.();
23
55
  return spoon;
24
56
  }
package/lib/ui.js ADDED
@@ -0,0 +1,26 @@
1
+ const { strict } = require("./strict.js");
2
+
3
+ function createUi() {
4
+ const alerts = [];
5
+ const openedURLs = [];
6
+ const ui = {
7
+ alert(message) {
8
+ const alert = strict("HSUIAlert", {
9
+ show() {
10
+ alerts.push(message);
11
+ return alert;
12
+ },
13
+ });
14
+ return alert;
15
+ },
16
+ };
17
+ const urlevent = {
18
+ openURL(url) {
19
+ openedURLs.push(url);
20
+ return true;
21
+ },
22
+ };
23
+ return { ui, urlevent, ctl: { alerts, openedURLs } };
24
+ }
25
+
26
+ module.exports = { createUi };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hs2-mock",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Strict mock of the Hammerspoon 2 hs global for testing Spoons under node:test",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -1,3 +1,8 @@
1
1
  declare const module: { exports: any };
2
2
  declare function require(id: string): any;
3
3
  declare const __dirname: string;
4
+
5
+ // The part of Hammerspoon 2's built-in `util` module that spoons use.
6
+ declare module "util" {
7
+ export function inspect(value: unknown): string;
8
+ }