hs2-mock 0.4.0 → 0.5.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.
Files changed (3) hide show
  1. package/README.md +30 -49
  2. package/index.d.ts +309 -0
  3. package/package.json +7 -2
package/README.md CHANGED
@@ -50,52 +50,33 @@ Reading a member the mock lacks throws `hs2-mock: hs.x.y is not mocked`. Tests c
50
50
  | `hs.wifi` | `currentNetwork`, `on`, `off` |
51
51
  | `HSImage`, `HSSize` globals | `fromAppBundle`, `fromName`, `fromSymbol` |
52
52
 
53
- ## Controls (`ctl`)
54
-
55
- | Control | Does |
56
- | ------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- |
57
- | `advance(seconds)` | Moves the clock, firing timers and settling promises between them |
58
- | `flush()` | Settles pending promises |
59
- | `addApp({ bundleID, name, allWindows, focusedWindow, mainWindow })` | Adds a running app and returns it |
60
- | `activate(bundleID)` | Brings an app to the front and emits `didActivate`; `null` for one without a bundle ID |
61
- | `launch(options)`, `quit(bundleID)` | Starts or ends an app, emitting `didLaunch` or `didTerminate` |
62
- | `keystrokes`, `clicks`, `mouse` | What `hs.eventtap` sent, and where the mouse is |
63
- | `respondToKeystrokes(handler)`, `respondToClicks(handler)` | Runs as each is sent |
64
- | `websockets` | `url`, `sent`, `readyState`, `destroyed`, `open()`, `message(text)`, `remoteClose()`, `error(text)` |
65
- | `defaults` | What `hs.userdefaults` holds |
66
- | `shownAlerts` | Each alert's `message`, `duration` and whether it was `closed` |
67
- | `scans`, `discover({ identifier, name, rssi })`, `setBluetooth(on)` | BLE scans, advertisements and Bluetooth power |
68
- | `bluetoothPermission` | Whether Bluetooth access is granted |
69
- | `choosers` | `choices`, `visible`, `choose(text)`, `dismiss()` |
70
- | `locks` | Times the screen was locked |
71
- | `missingIcons` | Add the apps `HSImage.fromAppBundle` cannot load |
72
- | `killed`, `launched`, `activations`, `raised` | Apps killed, launched and activated, and windows raised |
73
- | `frontmostRequests` | Times Hammerspoon asked to be frontmost |
74
- | `element({ role, children, attributes })` | An accessibility element |
75
- | `window(element)` | A window whose element it is |
76
- | `url(text)` | A URL attribute value, as `hs.ax` returns it |
77
- | `hotkeys` | Created hotkeys that have not been destroyed |
78
- | `tasks` | Started tasks: `launchPath`, `args`, `environment`, `running`, `terminated`, `inputClosed`, `finish({ stdout, exitCode, reason, error })` |
79
- | `respondToTasks(handler)` | Answers each task as it starts with what `finish()` takes, or `undefined` to leave it running |
80
- | `menubarItems` | Menu bar items: `item`, `icon`, `tooltip`, `menu`, `click()`, `destroyed` |
81
- | `notifications` | `title`, `body`; `click()` for `show()` ones, `options` for `create()` ones |
82
- | `dialogs` | `message`, `informativeText`, `buttons`, `open`, `press(label)` |
83
- | `windows` | `rect`, `title`, `view.html`, `view.post(name, body)`, `shown`, `userClose()` |
84
- | `alerts`, `openedURLs` | What was shown and opened |
85
- | `files`, `dirs`, `home`, `writeFails` | The in-memory file system |
86
- | `power(event)`, `notify(name)` | Emits an `hs.power` event or a distributed notification |
87
- | `ssid`, `wifi(event)` | The current Wi-Fi network, and its events |
88
- | `addAudioDevice({ name, uid, input })` | Adds an audio device (emits `dev+`) and returns it |
89
- | `removeAudioDevice(device)` | Removes it (emits `dev-`) |
90
- | `setDefaultAudioDevice(direction, device)` | Sets the default and emits `dOut` or `dIn` |
91
- | `updateAudioDevice(device, changes)` | Sets `volume` or `muted` and emits the device event |
92
- | `audioListeners()` | Number of audio listeners registered |
93
- | `images` | Add the names `HSImage.fromName` can load |
94
- | `accessibility`, `location`, `notificationPermission` | Whether each permission is granted |
95
- | `locationRequests` | How often location permission was requested |
96
-
97
- ## Types
98
-
99
- Add `node_modules/hs2-mock/types/*.d.ts` to your `tsconfig.json`, with `"types": []`. The version they come
100
- from is in `types/VERSION`; refresh with `mise run vendor <tag>`. `hammerspoon.d.ts` is MIT-licensed,
101
- copyright the Hammerspoon contributors.
53
+ ## Controls
54
+
55
+ `ctl` drives the mock and records what the spoon did: the clock (`ctl.advance()`), apps, windows and the
56
+ accessibility tree, the menu bar, alerts, choosers and dialogs, hotkeys, keystrokes and clicks, tasks,
57
+ websockets, Bluetooth, files, settings and permissions.
58
+
59
+ [`index.d.ts`](index.d.ts) documents every control. Editors show it as you type `ctl.`, with no setup.
60
+
61
+ ## What belongs here, and what stays in a spoon's tests
62
+
63
+ Generic fakes belong here: ones that record calls or hold simple state, with no opinion about a particular
64
+ spoon. They follow what Hammerspoon 2 does, including the awkward parts, and a test fails if the mock has a
65
+ member the real API lacks.
66
+
67
+ A spoon's tests keep their own fake, with a comment saying why, when it is:
68
+
69
+ - **a test assertion** rather than a mock, such as counting how often an accessibility tree is walked;
70
+ - **deliberately unrealistic**, such as a call that throws, to test error handling;
71
+ - **particular to one spoon**, such as the Dock or Teams window trees built on `ctl.element()`.
72
+
73
+ ## Types for spoons
74
+
75
+ To type-check a spoon against Hammerspoon 2, add `node_modules/hs2-mock/types/*.d.ts` to its `tsconfig.json`,
76
+ with `"types": []`. The version those come from is in `types/VERSION`; refresh with `mise run vendor <tag>`.
77
+ `hammerspoon.d.ts` is MIT-licensed, copyright the Hammerspoon contributors.
78
+
79
+ ## Development
80
+
81
+ `mise run test` type-checks the declarations and runs the tests. A new member goes in the table above, and a
82
+ new control in `index.d.ts`; tests fail otherwise.
package/index.d.ts ADDED
@@ -0,0 +1,309 @@
1
+ // Types of the mock and its test controls. The `hs` it mocks is typed by
2
+ // types/hammerspoon.d.ts.
3
+
4
+ // Hammerspoon objects and the mock's stand-ins for them. Left open, so tests can
5
+ // replace anything, and so these types need no other declarations.
6
+ // biome-ignore lint/suspicious/noExplicitAny: see above
7
+ type Mocked = any;
8
+
9
+ export interface AppOptions {
10
+ bundleID: string | null;
11
+ /** Defaults to the bundle ID. */
12
+ name?: string;
13
+ allWindows?: Mocked[];
14
+ focusedWindow?: Mocked;
15
+ mainWindow?: Mocked;
16
+ /** The app's accessibility tree, from `ctl.element()`. */
17
+ axElement?: Mocked;
18
+ }
19
+
20
+ export interface ElementOptions {
21
+ role: string;
22
+ title?: string | null;
23
+ elementDescription?: string | null;
24
+ position?: { x: number; y: number } | null;
25
+ size?: { w: number; h: number } | null;
26
+ children?: Mocked[];
27
+ /** What `attributeValue()` returns, by attribute name. */
28
+ attributes?: Record<string, unknown>;
29
+ }
30
+
31
+ export interface AudioDeviceOptions {
32
+ name: string;
33
+ /** Defaults to the name. */
34
+ uid?: string;
35
+ input?: boolean;
36
+ /** Defaults to the opposite of `input`. */
37
+ output?: boolean;
38
+ volume?: number;
39
+ muted?: boolean;
40
+ isInUse?: boolean;
41
+ }
42
+
43
+ export interface TaskResult {
44
+ /** One chunk, or several, passed to the output callback in turn. */
45
+ stdout?: string | string[];
46
+ exitCode?: number;
47
+ reason?: string;
48
+ error?: string;
49
+ }
50
+
51
+ export interface Task {
52
+ launchPath: string;
53
+ args: string[];
54
+ environment: Record<string, string> | null;
55
+ inputClosed: boolean;
56
+ terminated: boolean;
57
+ readonly running: boolean;
58
+ /** The HSTask, when made with `hs.task.create()`. */
59
+ task?: Mocked;
60
+ /** Ends the task, calling its output and termination callbacks. */
61
+ finish(result?: TaskResult): void;
62
+ }
63
+
64
+ export interface Hotkey {
65
+ mods: string[];
66
+ key: string;
67
+ onPressed: () => void;
68
+ isEnabled(): boolean;
69
+ enable(): boolean;
70
+ disable(): void;
71
+ destroy(): void;
72
+ }
73
+
74
+ export interface MenubarItem {
75
+ /** The HSMenuBarItem, for its `title`. */
76
+ item: Mocked;
77
+ icon: Mocked;
78
+ tooltip: string | null;
79
+ /** What `setMenu()` was given: an array, or a function returning one. */
80
+ menu: Mocked;
81
+ destroyed: boolean;
82
+ /** Calls the click callback; throws if none is set. */
83
+ click(button?: string): void;
84
+ }
85
+
86
+ export interface Notification {
87
+ title: string;
88
+ body: string;
89
+ /** Clicks a notification made with `show()`. */
90
+ click?(): void;
91
+ /** What a notification made with `create()` was given. */
92
+ options?: Mocked;
93
+ }
94
+
95
+ export interface Dialog {
96
+ message: string;
97
+ informativeText: string | null;
98
+ buttons: string[];
99
+ style: string;
100
+ open: boolean;
101
+ /** Presses a button of an open dialog. */
102
+ press(label: string): void;
103
+ }
104
+
105
+ export interface Window {
106
+ rect: Mocked;
107
+ title: string;
108
+ /** The web view's HTML, and `post()` to send what its page would. */
109
+ view: { html: string | null; post(name: string, body: unknown): void } | null;
110
+ shown: boolean;
111
+ shows: number;
112
+ destroyed: boolean;
113
+ /** Closes the window as the user would. */
114
+ userClose(): void;
115
+ }
116
+
117
+ export interface Alert {
118
+ message: string;
119
+ duration: number;
120
+ closed: boolean;
121
+ }
122
+
123
+ export interface Chooser {
124
+ /** The HSChooser. */
125
+ chooser: Mocked;
126
+ choices: Mocked[];
127
+ readonly visible: boolean;
128
+ /** Selects the choice with this text: the chooser hides, then `onSelect`. */
129
+ choose(text: string): void;
130
+ /** Dismisses it as Escape does: `onSelect(null)`. */
131
+ dismiss(): void;
132
+ }
133
+
134
+ export interface Scan {
135
+ seconds: number;
136
+ allowDuplicates: boolean;
137
+ done: boolean;
138
+ }
139
+
140
+ export interface Advertisement {
141
+ identifier: string;
142
+ name?: string | null;
143
+ /** Defaults to the name. */
144
+ localName?: string | null;
145
+ /** 127 means no reading, which keeps the last one. */
146
+ rssi: number;
147
+ }
148
+
149
+ export interface Keystroke {
150
+ mods: string[];
151
+ key: string | number;
152
+ app: Mocked;
153
+ }
154
+
155
+ export interface WebSocket {
156
+ url: string;
157
+ /** Messages sent while open. */
158
+ sent: string[];
159
+ destroyed: boolean;
160
+ readonly readyState: number;
161
+ /** The connection succeeds. */
162
+ open(): void;
163
+ /** The server sends a message. */
164
+ message(text: string): void;
165
+ /** The server closes an open connection. */
166
+ remoteClose(code?: number, reason?: string): void;
167
+ /** The connection fails, as when it is refused. */
168
+ error(text: string): void;
169
+ }
170
+
171
+ export interface Controls {
172
+ /** Moves the clock, firing timers and settling promises between them. */
173
+ advance(seconds: number): Promise<void>;
174
+ /** Settles pending promises. */
175
+ flush(): Promise<void>;
176
+
177
+ /** Adds a running app, without an event, and returns it. */
178
+ addApp(options: AppOptions): Mocked;
179
+ /** Adds an app and emits `didLaunch`. */
180
+ launch(options: AppOptions): Mocked;
181
+ /** Ends a running app and emits `didTerminate`. */
182
+ quit(bundleID: string): void;
183
+ /** Brings an app to the front, adding it if needed, and emits `didActivate`. */
184
+ activate(bundleID: string | null): void;
185
+ /** Each time an app was activated. */
186
+ activations: { bundleID: string | null; allWindows: boolean }[];
187
+ /** Bundle IDs of the apps killed. */
188
+ killed: (string | null)[];
189
+ /** Bundle IDs passed to `hs.application.launchOrFocus()`. */
190
+ launched: string[];
191
+ /** Times Hammerspoon asked to be frontmost. */
192
+ readonly frontmostRequests: number;
193
+
194
+ /** An accessibility element. */
195
+ element(options: ElementOptions): Mocked;
196
+ /** A window whose accessibility element this is. */
197
+ window(
198
+ element: Mocked,
199
+ options?: { id?: number; isStandard?: boolean },
200
+ ): Mocked;
201
+ /** A URL attribute value, as `hs.ax` returns it. */
202
+ url(text: string): Mocked;
203
+ /** Windows raised. */
204
+ raised: Mocked[];
205
+
206
+ /** Adds an audio device, emitting `dev+`, and returns it. */
207
+ addAudioDevice(options: AudioDeviceOptions): Mocked;
208
+ /** Removes a device, emitting `dev-`. */
209
+ removeAudioDevice(device: Mocked): void;
210
+ /** Sets the default device and emits `dOut` or `dIn`. */
211
+ setDefaultAudioDevice(direction: "output" | "input", device: Mocked): void;
212
+ /** Changes a device, emitting its event for `volume` or `muted`. */
213
+ updateAudioDevice(device: Mocked, changes: Record<string, unknown>): void;
214
+ /** Number of audio listeners registered. */
215
+ audioListeners(): number;
216
+
217
+ /** BLE scans started. */
218
+ scans: Scan[];
219
+ /** An advertisement. Returns false when no scan was there to hear it. */
220
+ discover(advertisement: Advertisement): boolean;
221
+ /** Turns Bluetooth on or off; off fails the running scan and later ones. */
222
+ setBluetooth(on: boolean): void;
223
+
224
+ /** Choosers created. */
225
+ choosers: Chooser[];
226
+ /** Dialogs created. */
227
+ dialogs: Dialog[];
228
+ /** Windows created with `hs.ui.window()`. */
229
+ windows: Window[];
230
+ /** Messages of the alerts shown. */
231
+ alerts: string[];
232
+ /** The alerts shown, with their duration and whether they were closed. */
233
+ shownAlerts: Alert[];
234
+ /** Menu bar items created, destroyed ones included. */
235
+ menubarItems: MenubarItem[];
236
+ /** Notifications shown. */
237
+ notifications: Notification[];
238
+ /** Add the names `HSImage.fromName()` can load. */
239
+ images: Set<string>;
240
+ /** Add the apps `HSImage.fromAppBundle()` cannot load. */
241
+ missingIcons: Set<string>;
242
+
243
+ /** Created hotkeys that have not been destroyed. */
244
+ hotkeys: Hotkey[];
245
+ /** Keystrokes `hs.eventtap` sent. */
246
+ keystrokes: Keystroke[];
247
+ /** Clicks `hs.eventtap` made. */
248
+ clicks: { x: number; y: number }[];
249
+ /** Where the mouse is. */
250
+ mouse: { x: number; y: number };
251
+ /** Runs as each keystroke is sent. */
252
+ respondToKeystrokes(handler: (keystroke: Keystroke) => void): void;
253
+ /** Runs as each click is made. */
254
+ respondToClicks(handler: (point: { x: number; y: number }) => void): void;
255
+
256
+ /** Tasks started. Nothing runs until the test ends one. */
257
+ tasks: Task[];
258
+ /** Answers each task as it starts; return `undefined` to leave it running. */
259
+ respondToTasks(handler: (task: Task) => TaskResult | undefined): void;
260
+ /** Websockets opened. */
261
+ websockets: WebSocket[];
262
+ /** URLs passed to `hs.urlevent.openURL()`. */
263
+ openedURLs: string[];
264
+
265
+ /** The in-memory file system: contents by path. */
266
+ files: Map<string, string>;
267
+ /** Directories made. */
268
+ dirs: string[];
269
+ /** What `hs.fs.homeDirectory()` returns. */
270
+ home: string;
271
+ /** Makes `hs.fs.write()` fail. */
272
+ writeFails: boolean;
273
+ /** What `hs.userdefaults` holds. */
274
+ defaults: Map<string, unknown>;
275
+
276
+ /** Emits an `hs.power` event. */
277
+ power(event: string): void;
278
+ /** Times the screen was locked. */
279
+ readonly locks: number;
280
+ /** Emits a distributed notification. */
281
+ notify(name: string, object?: unknown, userInfo?: object): void;
282
+ /** The current Wi-Fi network. */
283
+ ssid: string | null;
284
+ /** Emits an `hs.wifi` event. */
285
+ wifi(event: string): void;
286
+
287
+ /** Whether Accessibility is granted. */
288
+ accessibility: boolean;
289
+ /** Whether Bluetooth access is granted. */
290
+ bluetoothPermission: boolean;
291
+ /** Whether Location is granted. */
292
+ location: boolean;
293
+ /** How often Location permission was requested. */
294
+ locationRequests: number;
295
+ /** Whether notifications are allowed. */
296
+ notificationPermission: boolean;
297
+ }
298
+
299
+ /**
300
+ * A fresh strict mock of the Hammerspoon 2 `hs` global, and the test controls.
301
+ * Also sets the HSImage and HSSize globals.
302
+ */
303
+ export function createHs(): { hs: Mocked; ctl: Controls };
304
+
305
+ /**
306
+ * Loads a spoon the way `hs.spoons.load()` does, installing `hs` as the global.
307
+ * @param dir the spoon's directory
308
+ */
309
+ export function loadSpoon(dir: string, hs: Mocked): Mocked;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hs2-mock",
3
- "version": "0.4.0",
3
+ "version": "0.5.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": {
@@ -8,8 +8,10 @@
8
8
  "url": "git+https://github.com/hugoh/hs2-mock.git"
9
9
  },
10
10
  "main": "index.js",
11
+ "types": "index.d.ts",
11
12
  "files": [
12
13
  "index.js",
14
+ "index.d.ts",
13
15
  "lib",
14
16
  "types"
15
17
  ],
@@ -27,5 +29,8 @@
27
29
  "mock",
28
30
  "testing",
29
31
  "node-test"
30
- ]
32
+ ],
33
+ "devDependencies": {
34
+ "typescript": "7.0.2"
35
+ }
31
36
  }