hass-taste-test 0.2.6 → 0.3.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 +30 -23
- package/lib/bin.js +2 -2
- package/lib/index.d.ts +42 -15
- package/lib/index.js +238 -108
- package/lib/integrations/get-diffable-html.d.ts +2 -27
- package/lib/integrations/get-diffable-html.js +30 -27
- package/lib/integrations/playwright.d.ts +6 -7
- package/lib/integrations/playwright.js +5 -5
- package/lib/socket.d.ts +1 -1
- package/lib/socket.js +6 -4
- package/lib/types.d.ts +1 -3
- package/package.json +38 -27
- package/lib/hass-test.d.ts +0 -103
- package/lib/hass-test.js +0 -411
- package/lib/hasstest-test.d.ts +0 -1
- package/lib/hasstest-test.js +0 -48
- package/lib/hasstest.d.ts +0 -62
- package/lib/hasstest.js +0 -232
- package/lib/homeassistant-types.d.ts +0 -112
- package/lib/homeassistant-types.js +0 -2
package/README.md
CHANGED
|
@@ -30,19 +30,27 @@ This guide will walk you through setting up tests for visual regression testing.
|
|
|
30
30
|
|
|
31
31
|
I recommend using either [Jest](https://jestjs.io) or [AVA](https://avajs.dev) for writing tests and [Playwright](https://playwright.dev) for browser automation. Both Jest and AVA run test files in parallel, and Playwright allows you to test on Chromium, Firefox, and WebKit. Since Jest has great support for [image snapshots](https://github.com/americanexpress/jest-image-snapshot), this guide will use Jest.
|
|
32
32
|
|
|
33
|
-
1. Install
|
|
33
|
+
1. Optional: Install uv (helps speed up the HA download)
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
curl -LsSf https://astral.sh/uv/install.sh | sh
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
2. Install dependencies
|
|
34
40
|
|
|
35
41
|
```bash
|
|
36
42
|
npm install --save-dev hass-taste-test jest jest-image-snapshot playwright
|
|
37
43
|
```
|
|
38
44
|
|
|
39
|
-
|
|
45
|
+
3. Verify that Hass Taste Test can download and run Home Assistant:
|
|
40
46
|
|
|
41
47
|
```bash
|
|
42
48
|
npx hass-taste-test
|
|
43
49
|
```
|
|
44
50
|
|
|
45
|
-
|
|
51
|
+
Installing [uv](https://docs.astral.sh/uv/) is strongly recommended: Hass Taste Test uses it when it is on your `PATH`, and it installs Home Assistant much faster and downloads a Python version that Home Assistant supports. Without uv, Hass Taste Test falls back to pip, which uses your `python3`; this must meet Home Assistant's minimum Python version.
|
|
52
|
+
|
|
53
|
+
4. Create a tests directory `__tests__` and create a file within it: `__tests__/card.test.js`. Add this code:
|
|
46
54
|
|
|
47
55
|
```javascript
|
|
48
56
|
const { HomeAssistant, PlaywrightBrowser } = require('hass-taste-test')
|
|
@@ -85,7 +93,7 @@ You may now consider customizing your Jest configuration using (`npx jest --init
|
|
|
85
93
|
|
|
86
94
|
## Developing
|
|
87
95
|
|
|
88
|
-
If you wish to make changes to this repository, follow these instructions.
|
|
96
|
+
If you wish to make changes to this repository, follow these instructions. The source is written so Node (and Jest, via its built-in type stripping) can run it directly (Node 22.18+), so no recompilation of typescript is necessary.
|
|
89
97
|
|
|
90
98
|
1. Install dependencies
|
|
91
99
|
|
|
@@ -104,26 +112,26 @@ To update snapshots, use `npx jest -u` and `npx ava -u`.
|
|
|
104
112
|
|
|
105
113
|
### But I want to write my tests in TypeScript
|
|
106
114
|
|
|
107
|
-
Install [`ts-jest`](https://kulshekhar.github.io/ts-jest/docs/getting-started/installation/), `@types/jest`, and `@types/jest-image-snapshot` along with the other dependencies, then run `npx ts-jest config:init` to configure Jest for Typescript. Browse the [examples](https://github.com/rianadon/hass-taste-test/tree/main/test) for example tests; they are all written in TypeScript.
|
|
115
|
+
Install [`ts-jest`](https://kulshekhar.github.io/ts-jest/docs/getting-started/installation/), `@types/jest`, and `@types/jest-image-snapshot` along with the other dependencies, then run `npx ts-jest config:init` to configure Jest for Typescript. The bundled type declarations require TypeScript 5.0 or later. Browse the [examples](https://github.com/rianadon/hass-taste-test/tree/main/test) for example tests; they are all written in TypeScript.
|
|
108
116
|
|
|
109
117
|
## Why visual regression testing?
|
|
110
118
|
|
|
111
119
|
I could've shown off how you can check that the text in the selectors you care about has the right substrings, or how to check that the correct number of `div`s got added to the special container. Here's why I didn't:
|
|
112
120
|
|
|
113
|
-
-
|
|
114
|
-
-
|
|
115
|
-
-
|
|
121
|
+
- **These tests are a pain to write**: Home Assistant is very complex, so your card may run into many edge cases that require tests. I believe test quantity is better than test quality for this domain.
|
|
122
|
+
- **Styles don't change much**: Most of the time, you are likely adding additonal features to the card rather than giving it a new paint job; so you won't be needing to update snapshots often. Testing visual regressions ensures that the card will remain stable for the features you've already added.
|
|
123
|
+
- **Cards are small and simple**: Small cards make for small visual diffs, which makes it easy to spot what regressed.
|
|
116
124
|
|
|
117
125
|
What if visual regression testing is not for me?
|
|
118
126
|
|
|
119
|
-
-
|
|
127
|
+
- Grab the card's HTML and [snapshot](https://jestjs.io/docs/snapshot-testing) [it](https://github.com/avajs/ava/blob/main/docs/04-snapshot-testing.md). This works similar to visual regression tests but does not catch CSS changes.
|
|
120
128
|
|
|
121
129
|
```javascript
|
|
122
130
|
expect(await dashboard.cards[0].hmtl()).toMatchSnapshot() // Jest
|
|
123
131
|
t.snapshot(await dashboard.cards[0].hmtl()) // AVA
|
|
124
132
|
```
|
|
125
133
|
|
|
126
|
-
-
|
|
134
|
+
- Narrow the card to an important selector and check the text.
|
|
127
135
|
|
|
128
136
|
```javascript
|
|
129
137
|
expect(await dashboard.cards[0].narrow('.text-content').text()).to… // Jest
|
|
@@ -132,15 +140,15 @@ What if visual regression testing is not for me?
|
|
|
132
140
|
|
|
133
141
|
## Important concepts
|
|
134
142
|
|
|
135
|
-
-
|
|
143
|
+
- **Home Assistant objects**: The `HomeAssistant` class creates a new Home Assistant Core instance. Each of these is fully isolated, so you can write multiple tests files each doing wildly different things without worrying about conflicts. They start up in parallel, except for about <100ms when multiple instances coordinate amongst themselves sequentially to choose unique ports and ensure Home Assistant is installed and upgraded.
|
|
136
144
|
|
|
137
145
|
Each instance creates a configuration directory in your [temporary directory](https://en.wikipedia.org/wiki/Temporary_folder), and these are deleted when `hass.close()` is called. The Home Assistant Core virtual environment is also stored in the temp directory, but is kept around for reuse.
|
|
138
146
|
|
|
139
|
-
-
|
|
147
|
+
- **Dashboards**: Individual tests are isolated by using unique Lovelace Dashboards. This isolation is good for tracking down which test broke and for, if you are using AVA, running tests within a file in parallel. The name and id for dashboards are automatically generated.
|
|
140
148
|
|
|
141
149
|
You can grab references to cards and elements using `dashboard.cards[0]` or `dashboards.cards[1].narrow('.text-content')`. These are merely references; the test will not look for these elements inside the page until you call `.screenshot()`, `.html()`, or `.text()`, which all return [Promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise).
|
|
142
150
|
|
|
143
|
-
-
|
|
151
|
+
- **Browser Integrations**: Hass Taste Test only requires a browser for working with cards in the dashboard; Home Assistant is configured entirely through the REST API. If you'd like to use the Dashboard card methods, you'll need to pass a browser integration instance to the `browser` option when configuring a `HomeAssistant` object.
|
|
144
152
|
|
|
145
153
|
Currently only [Playwright](https://playwright.dev) is supported, but you can write your own for other tools.
|
|
146
154
|
|
|
@@ -205,7 +213,6 @@ Configures, starts, and connects to a new Home Assistant instance. Appends the s
|
|
|
205
213
|
|
|
206
214
|
| Option | Description | Default |
|
|
207
215
|
| ------------------ | ---------------------------------------------------------------------------------------- | ----------- |
|
|
208
|
-
| `python` | Python executable used to create virtual environment | `python3` |
|
|
209
216
|
| `hassArgs` | Arguments to pass to the `hass` binary | `[]` |
|
|
210
217
|
| `host` | Host to which the Home Assistant frontend and api will be bound | `127.0.0.1` |
|
|
211
218
|
| `port` | Port used to host the Home Assistant instance. If `null`, an unused port will be chosen. | `null` |
|
|
@@ -262,8 +269,8 @@ Creates and configures a dashboard using `createDashboard` and `setDashboardView
|
|
|
262
269
|
|
|
263
270
|
Cards should be listed in `config`, and options allows you to set:
|
|
264
271
|
|
|
265
|
-
-
|
|
266
|
-
-
|
|
272
|
+
- `colorScheme`: Can be `light` or `dark` to use the page's light or dark theme
|
|
273
|
+
- That's all!
|
|
267
274
|
|
|
268
275
|
#### `async close()`
|
|
269
276
|
|
|
@@ -295,10 +302,10 @@ Returns a Buffer containing image data for the card's screenshot.
|
|
|
295
302
|
|
|
296
303
|
Returns normalized HTML for of the card. This differs from `outerHTML` in that:
|
|
297
304
|
|
|
298
|
-
-
|
|
299
|
-
-
|
|
300
|
-
-
|
|
301
|
-
-
|
|
302
|
-
-
|
|
303
|
-
-
|
|
304
|
-
-
|
|
305
|
+
- Traversal crosses Shadow DOM boundaries
|
|
306
|
+
- Polymer and Lit properties are included as attributes
|
|
307
|
+
- Indentation is normalized
|
|
308
|
+
- Attributes are sorted by name, and you can ignore attributes (`options.ignoreAttributes`)
|
|
309
|
+
- Script and style tags are not included (`options.ignoreTags`)
|
|
310
|
+
- The children of Common Home Assistant elements are not included (`options.ignoreChildren` for descendants, `options.ignoreShadowChildren` for Shadow DOM children)
|
|
311
|
+
- Empty and undefined attributes are not included
|
package/lib/bin.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
"use strict";
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
const
|
|
4
|
+
const index_ts_1 = require("./index.js");
|
|
5
5
|
(async () => {
|
|
6
6
|
console.log('Ensuring Home Assistant packages are installed...');
|
|
7
|
-
const hass = await
|
|
7
|
+
const hass = await index_ts_1.HomeAssistant.create('recorder:', {});
|
|
8
8
|
await hass.close();
|
|
9
9
|
console.log('All set!');
|
|
10
10
|
})();
|
package/lib/index.d.ts
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
1
|
import * as hass from 'home-assistant-js-websocket';
|
|
3
|
-
import { BrowserPage, DashboardOptions, DiffOptions, LovelaceDashboardCreateParams, LovelaceResourceType } from './types';
|
|
4
|
-
|
|
5
|
-
export { PlaywrightBrowser, PlaywrightElement } from './integrations/playwright';
|
|
2
|
+
import type { BrowserIntegration, BrowserPage, DashboardOptions, DiffOptions, LovelaceDashboardCreateParams, LovelaceResourceType } from './types.ts';
|
|
3
|
+
export { PlaywrightBrowser, PlaywrightPage, type PlaywrightElement, } from './integrations/playwright.ts';
|
|
6
4
|
interface HassOptions<E> {
|
|
7
|
-
python: string;
|
|
8
5
|
hassArgs: string[];
|
|
9
6
|
host: string;
|
|
10
7
|
port: number | null;
|
|
@@ -26,14 +23,14 @@ export declare class HomeAssistant<E> {
|
|
|
26
23
|
private options;
|
|
27
24
|
private dashboards;
|
|
28
25
|
private entities;
|
|
29
|
-
private started;
|
|
30
26
|
ws: hass.Connection;
|
|
31
27
|
private constructor();
|
|
32
28
|
private path_confFile;
|
|
33
29
|
private path_componentsDir;
|
|
34
30
|
private path_wwwDir;
|
|
31
|
+
private path_storageDir;
|
|
35
32
|
private path_hass;
|
|
36
|
-
private
|
|
33
|
+
private path_python;
|
|
37
34
|
private path_cache;
|
|
38
35
|
/** Create and connect to an isolated Home Assistant instance */
|
|
39
36
|
static create<E>(config: string, options?: Partial<HassOptions<E>>): Promise<HomeAssistant<E>>;
|
|
@@ -41,23 +38,53 @@ export declare class HomeAssistant<E> {
|
|
|
41
38
|
static connect<E>(options?: Partial<HassOptions<E>>): Promise<HomeAssistant<E>>;
|
|
42
39
|
/** Start the Home Assistant server and connect to its websocket */
|
|
43
40
|
private start;
|
|
44
|
-
/** Write configuration.yaml */
|
|
45
|
-
private
|
|
41
|
+
/** Write configuration.yaml, plus HTTP settings in whichever form this Home Assistant expects */
|
|
42
|
+
private writeConfiguration;
|
|
43
|
+
/**
|
|
44
|
+
* Newer Home Assistant versions keep HTTP settings in `.storage/http` and only import the
|
|
45
|
+
* `http:` YAML block once. That import is applied as an unconfirmed trial configuration,
|
|
46
|
+
* which pops up a confirmation dialog in the frontend and reverts (restarting Home
|
|
47
|
+
* Assistant) after 5 minutes. Detect these versions so we can skip the YAML entirely.
|
|
48
|
+
*/
|
|
49
|
+
private usesHttpStorage;
|
|
50
|
+
/**
|
|
51
|
+
* Seed the HTTP config store so Home Assistant boots straight into our host and port.
|
|
52
|
+
* This uses the original (version 1) storage format, which Home Assistant migrates on load
|
|
53
|
+
* into a confirmed ("stable") config with all defaults filled in.
|
|
54
|
+
*/
|
|
55
|
+
private writeHttpStorage;
|
|
56
|
+
/**
|
|
57
|
+
* Seed the onboarding store so the core config and analytics steps start out done. Finishing
|
|
58
|
+
* the core config step kicks off config flows for shopping_list, met, google_translate, and
|
|
59
|
+
* radio_browser, which are never needed for tests and log errors when Home Assistant is closed
|
|
60
|
+
* before they finish setting up. Storage version 2 predates both steps, so every Home Assistant
|
|
61
|
+
* release since then migrates this file by marking them done, leaving only the user and
|
|
62
|
+
* integration steps (which hand us auth codes) for onboard() to complete.
|
|
63
|
+
*/
|
|
64
|
+
private writeOnboardingStorage;
|
|
46
65
|
/** Symlink custom component directories to the configuration directory */
|
|
47
66
|
private linkComponents;
|
|
48
67
|
/** Wait for lockfile to become released. Forces HassTest instances to start up one at a time */
|
|
49
68
|
private acquireLock;
|
|
50
69
|
/** Find an open port to launch Home Assistant */
|
|
51
70
|
private findPort;
|
|
52
|
-
/** Creates a python virtual environment and installs Home Assistant */
|
|
71
|
+
/** Creates a python virtual environment and installs Home Assistant (using uv if available) */
|
|
72
|
+
private chooseInstaller;
|
|
53
73
|
private setupVenv;
|
|
54
74
|
private readCache;
|
|
55
|
-
/** Fetch the latest hass version from pypi */
|
|
75
|
+
/** Fetch the latest hass version, and the Python versions it supports, from pypi */
|
|
56
76
|
private latestHAVersion;
|
|
77
|
+
/** Path to the venv's site-packages directory */
|
|
78
|
+
private sitePackages;
|
|
57
79
|
/** Finds the version of Home Assistant installed, if any */
|
|
58
80
|
private hassVersion;
|
|
59
|
-
/** Checks if Home Assistant is
|
|
81
|
+
/** Checks if Home Assistant is serving the onboarding API, which onboard() starts with */
|
|
60
82
|
private isUp;
|
|
83
|
+
/**
|
|
84
|
+
* Wait for Home Assistant to finish starting. Until then it has not installed its signal
|
|
85
|
+
* handlers, so close() would interrupt startup and log a KeyboardInterrupt traceback.
|
|
86
|
+
*/
|
|
87
|
+
private waitUntilRunning;
|
|
61
88
|
/** Complete onboarding and fetch short-lived access token */
|
|
62
89
|
private onboard;
|
|
63
90
|
private get url();
|
|
@@ -83,7 +110,7 @@ export declare class HomeAssistant<E> {
|
|
|
83
110
|
link(): Promise<string>;
|
|
84
111
|
openInBrowser(): Promise<void>;
|
|
85
112
|
}>;
|
|
86
|
-
states(): Promise<hass.
|
|
113
|
+
states(): Promise<hass.HassEntityStates>;
|
|
87
114
|
/** Clean up connections and stop the HomeAssistant server */
|
|
88
115
|
close(): Promise<void>;
|
|
89
116
|
HassDashboard: {
|
|
@@ -98,14 +125,14 @@ export declare class HomeAssistant<E> {
|
|
|
98
125
|
};
|
|
99
126
|
}
|
|
100
127
|
export declare class HassCard<E> {
|
|
128
|
+
private selectors;
|
|
101
129
|
private n;
|
|
102
130
|
private page;
|
|
103
|
-
private selectors;
|
|
104
131
|
constructor(n: number, page: BrowserPage<E>, selectors?: string[]);
|
|
105
132
|
element(): Promise<E>;
|
|
106
133
|
narrow(selector: string): HassCard<E>;
|
|
107
134
|
html(options?: Partial<DiffOptions>): Promise<string>;
|
|
108
135
|
text(): Promise<string>;
|
|
109
|
-
screenshot(): Promise<Buffer
|
|
136
|
+
screenshot(): Promise<Buffer<ArrayBufferLike>>;
|
|
110
137
|
}
|
|
111
138
|
export declare function multiply(n: number, configFn: (i: number) => string): string;
|