dsh-plugin-ui-actions 0.2.9 → 0.2.10

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 +122 -5
  2. package/README.zh-CN.md +130 -0
  3. package/package.json +3 -2
package/README.md CHANGED
@@ -1,13 +1,130 @@
1
1
  # dsh-plugin-ui-actions
2
2
 
3
- DSH UI 自动化动作库(Playwright):插件无关的通用动作,供 DSH 插件 E2E/spec 复用。
3
+ English | [简体中文](./README.zh-CN.md)
4
+
5
+ Plugin-agnostic Playwright actions for driving the DeepSeek Harness (DSH) web UI in tests: launch a browser, get past the onboarding dialog, flip localStorage settings, mock a POST, type a draft, click/wait, measure element geometry, and compare pixel and ARIA goldens.
6
+
7
+ This is a **pure library** — no `bin`, no CLI. Every action is `async` and takes a Playwright `page` as its **first argument**, except `launchBrowser`, which creates one.
8
+
9
+ ## Install
10
+
11
+ ```bash
12
+ npm i -D dsh-plugin-ui-actions
13
+ ```
14
+
15
+ - Node >= 22.15.0.
16
+ - Dependencies: `playwright-core`, `pngjs`, `pixelmatch`.
17
+ - No browsers are bundled. Pass `channel: 'msedge'` to drive system Edge, or install Playwright's bundled Chromium with `npx dsh-plugin-e2e install-browser`.
18
+
19
+ ## Quick start
4
20
 
5
21
  ```js
6
- import { launchBrowser, dismissOnboarding, enableSetting, mockPost, typeDraft, click, waitFor, rects, golden, ariaSnapshot, smoke } from 'dsh-plugin-ui-actions'
22
+ import {
23
+ launchBrowser,
24
+ dismissOnboarding,
25
+ enableSetting,
26
+ typeDraft,
27
+ clickAttr,
28
+ smoke,
29
+ shot,
30
+ } from 'dsh-plugin-ui-actions'
31
+
7
32
  const { browser, context, page } = await launchBrowser({ channel: 'msedge' })
33
+ await page.goto('http://127.0.0.1:5173/')
34
+
35
+ await dismissOnboarding(page) // first-run "preview build" notice, if present
36
+ await enableSetting(page, 'my-plugin.enabled', '1') // plugins often store flags in localStorage
37
+ await page.reload()
38
+
39
+ if (!(await smoke('data-my-plugin'))) throw new Error('plugin did not mount')
40
+
41
+ await typeDraft(page, 'hello from a test')
42
+ await clickAttr(page, 'data-my-plugin')
43
+ await shot(page, 'artifacts/after-click.png')
44
+
45
+ await browser.close()
46
+ ```
47
+
48
+ ## API
49
+
50
+ All 14 exports. `page` is a Playwright `Page` unless stated otherwise.
51
+
52
+ | Export | Signature | Returns | Behavior |
53
+ |---|---|---|---|
54
+ | `launchBrowser` | `launchBrowser(opts = {})` | `Promise<{ browser, context, page }>` | Launches Chromium (or `opts.channel`), opens a context and one page. |
55
+ | `dismissOnboarding` | `dismissOnboarding(page)` | `Promise<void>` | Clicks the first button matching `继续` / `Continue` / `Got it` / `知道了` (4 s timeout), then waits 800 ms. No-op when absent. |
56
+ | `enableSetting` | `enableSetting(page, key, value)` | `Promise<void>` | `localStorage.setItem(key, String(value))`. |
57
+ | `mockPost` | `mockPost(page, pattern, body, status = 200)` | `Promise<void>` | `page.route(pattern)` fulfilling with `JSON.stringify(body)` as `application/json`. |
58
+ | `typeDraft` | `typeDraft(page, text)` | `Promise<void>` | Clicks the first `[contenteditable="true"]`, types `text` via the keyboard, waits 600 ms. |
59
+ | `clickAttr` | `clickAttr(page, attr)` | `Promise<void>` | Force-clicks the first `[attr]` element, waits 1200 ms. |
60
+ | `click` | `click(page, selector, opts = {})` | `Promise<void>` | Clicks the first match, then waits `opts.settleMs` (default 300 ms). |
61
+ | `waitFor` | `waitFor(page, selector, timeout = 15000)` | `Promise<void>` | Waits for the first match to reach `state: 'visible'`. |
62
+ | `smoke` | `smoke(page, attr)` | `Promise<boolean>` | `true` if `[attr]` matches at least one element — pass the full attribute name (e.g. `'data-my-plugin'`), i.e. proof the plugin actually mounted. |
63
+ | `rects` | `rects(page, prevSel, cardSel)` | `Promise<object>` | Geometry of two elements plus their left/width delta and vertical gap. |
64
+ | `shot` | `shot(page, filePath)` | `Promise<string>` | Screenshots to `filePath` and returns it. |
65
+ | `golden` | `golden(page, name, opts)` | `Promise<object>` | Pixel golden compare with tolerance; `opts.goldenDir` is required. |
66
+ | `ariaSnapshot` | `ariaSnapshot(page, sel = 'body')` | `Promise<string>` | `Locator.ariaSnapshot()` for `sel`. |
67
+ | `goldenAria` | `goldenAria(page, name, opts)` | `Promise<object>` | Normalized ARIA golden compare with line tolerance; `opts.dir` is required. |
68
+
69
+ ### `launchBrowser` options
70
+
71
+ | Option | Default | Meaning |
72
+ |---|---|---|
73
+ | `headless` | `true` | Anything other than `false` keeps it headless. |
74
+ | `channel` | none | `'msedge'` (system Edge) or any Playwright channel. `'chromium'` / `'bundled'` are treated as "no channel". |
75
+ | `viewport` | `{ width: 1440, height: 900 }` | Context viewport. |
76
+
77
+ `chromiumSandbox` is always `false`.
78
+
79
+ ### `golden(page, name, opts)`
80
+
81
+ | Option | Default | Meaning |
82
+ |---|---|---|
83
+ | `goldenDir` | — | **Required.** Directory for `<name>.png`; created if missing. |
84
+ | `sel` | full page | Screenshot only this element. |
85
+ | `stabilize` | `true` | Inject `animation/transition: none` and await `document.fonts.ready`. |
86
+ | `pixelThreshold` | `0.1` | `pixelmatch` per-pixel color threshold. |
87
+ | `maxDiffRatio` | `0.002` | Allowed differing-pixel **ratio** — 0.2% of the image. |
88
+
89
+ Returns `{ status: 'created', file }` on first run, `{ status: 'match', file, diffPixels, diffRatio }` within tolerance, or `{ status: 'diff', file, diffFile?, diffPixels?, diffRatio?, reason? }` — a `.diff.png` is written next to the golden, and a size mismatch short-circuits to `status: 'diff'` with a `reason`. The tolerance is **inclusive**: at 100×100 (10,000 px) with the default `0.002`, 20 differing pixels is `match` (ratio `0.002`) and 21 is `diff` (ratio `0.0021`) — both verified.
90
+
91
+ ### `goldenAria(page, name, opts)`
92
+
93
+ | Option | Default | Meaning |
94
+ |---|---|---|
95
+ | `dir` | — | **Required.** Directory for `<name>.aria.txt`. |
96
+ | `sel` | `'body'` | Element to snapshot. |
97
+ | `maxDiffLines` | `max(3, ceil(2% of the larger snapshot's line count))` | Allowed differing lines. |
98
+
99
+ Snapshots are normalized first (trim each line, collapse whitespace, drop empty lines). Returns `{ status: 'created' | 'updated' | 'match' | 'diff', file, diffLines? }`; on `diff` the new snapshot is written to `<name>.aria.diff.txt`. Set `GOLDEN_UPDATE=1` to rewrite the baseline (status `updated`).
100
+
101
+ `diffLines` is a **multiset difference**, so one changed line counts as **2** (one line missing from the baseline, one extra in the new snapshot). With the default floor of 3, that single edit still passes as `match`; verified alongside a 200-line rewrite, which reported `diffLines: 202` and `status: 'diff'`.
102
+
103
+ ### `rects` return value
104
+
105
+ `{ found: false, hasPreview, hasCard }` when either selector is missing; otherwise:
106
+
107
+ ```js
108
+ {
109
+ found: true,
110
+ preview: { left, top, bottom, width }, // rounded px
111
+ card: { left, top, width },
112
+ leftDiff, // preview.left - card.left
113
+ widthDiff, // preview.width - card.width
114
+ gapAboveCard, // card.top - preview.bottom
115
+ }
8
116
  ```
9
117
 
10
- 导出:`launchBrowser` / `dismissOnboarding` / `enableSetting` / `mockPost` / `typeDraft` / `clickAttr` / `click` / `waitFor` / `smoke` / `rects` / `shot` / `golden`(像素容差金标准)/ `ariaSnapshot` / `goldenAria`(aria 容差金标准)。
118
+ ## Notes
119
+
120
+ - **`page` is always the first argument** — including `click`, `waitFor` and `smoke`. `dsh-plugin-e2e` spec helpers look inconsistent only because they pre-bind `page`: there `click(sel)` / `waitFor(sel)` / `smoke(attr)` / `dismissOnboarding()` are page-bound wrappers, while `enableSetting` / `mockPost` / `typeDraft` / `clickAttr` / `rects` still take `page` from the spec. This package itself never hides `page`.
121
+ - **Waits are baked in**: `typeDraft` 600 ms, `clickAttr` 1200 ms, `click` 300 ms (`settleMs`), `dismissOnboarding` 800 ms. Do not assert on DOM state immediately after a click; raise `settleMs` if your plugin debounces.
122
+ - **`goldenDir` / `dir` are effectively required**: `mkdir(undefined)` throws `TypeError: The "path" argument must be of type string...` before any comparison happens, so you get a confusing error rather than a golden file. `dsh-plugin-e2e` always supplies them.
123
+ - `golden` compares pixels, not bytes — a 0.2% tolerance absorbs cross-machine font and anti-aliasing jitter. `goldenAria` compares text lines, so it survives layout shifts but not content changes.
124
+ - `ariaSnapshot` throws `ariaSnapshot 不可用(需较新 Playwright)` when the installed Playwright lacks `Locator.ariaSnapshot`.
125
+ - `enableSetting` writes a string; call it before the reload/`goto` that your plugin reads the flag on.
126
+ - `dsh-plugin-e2e/browser` re-exports this package verbatim, so existing `dsh-plugin-e2e/browser` imports keep working.
127
+
128
+ ## License
11
129
 
12
- - 依赖 `playwright-core`(浏览器通道由调用方给:`msedge` 系统 Edge 或 `chromium` 自带)。
13
- - 原 `dsh-plugin-e2e/browser` 现转发到本包,向后兼容。
130
+ MIT
@@ -0,0 +1,130 @@
1
+ # dsh-plugin-ui-actions
2
+
3
+ [English](./README.md) | 简体中文
4
+
5
+ 驱动 DeepSeek Harness(DSH)Web UI 的**插件无关** Playwright 动作库:起浏览器、关首启引导弹窗、写 localStorage 开关、拦 POST 请求做 mock、键入草稿、点击/等待、量元素几何、像素金标准与 aria 金标准比对。
6
+
7
+ 这是**纯库**——没有 `bin`,没有 CLI。除 `launchBrowser` 自己造 `page` 外,其余每个动作都是 `async`,且**首参固定是 Playwright 的 `page`**。
8
+
9
+ ## 安装
10
+
11
+ ```bash
12
+ npm i -D dsh-plugin-ui-actions
13
+ ```
14
+
15
+ - Node >= 22.15.0。
16
+ - 依赖:`playwright-core`、`pngjs`、`pixelmatch`。
17
+ - 不带浏览器。传 `channel: 'msedge'` 用系统 Edge,或用 `npx dsh-plugin-e2e install-browser` 装 Playwright 自带的 Chromium。
18
+
19
+ ## 快速开始
20
+
21
+ ```js
22
+ import {
23
+ launchBrowser,
24
+ dismissOnboarding,
25
+ enableSetting,
26
+ typeDraft,
27
+ clickAttr,
28
+ smoke,
29
+ shot,
30
+ } from 'dsh-plugin-ui-actions'
31
+
32
+ const { browser, context, page } = await launchBrowser({ channel: 'msedge' })
33
+ await page.goto('http://127.0.0.1:5173/')
34
+
35
+ await dismissOnboarding(page) // 首启的「预览版说明」弹窗,没有就跳过
36
+ await enableSetting(page, 'my-plugin.enabled', '1') // 插件开关多以 localStorage 存
37
+ await page.reload()
38
+
39
+ if (!(await smoke('data-my-plugin'))) throw new Error('插件没挂上')
40
+
41
+ await typeDraft(page, 'hello from a test')
42
+ await clickAttr(page, 'data-my-plugin')
43
+ await shot(page, 'artifacts/after-click.png')
44
+
45
+ await browser.close()
46
+ ```
47
+
48
+ ## API
49
+
50
+ 共 14 个导出。除特别说明外,`page` 都是 Playwright 的 `Page`。
51
+
52
+ | 导出 | 签名 | 返回 | 行为 |
53
+ |---|---|---|---|
54
+ | `launchBrowser` | `launchBrowser(opts = {})` | `Promise<{ browser, context, page }>` | 启动 Chromium(或 `opts.channel`),建一个 context 和一个 page。 |
55
+ | `dismissOnboarding` | `dismissOnboarding(page)` | `Promise<void>` | 点第一个文案匹配 `继续` / `Continue` / `Got it` / `知道了` 的按钮(4 秒超时),再等 800 ms。没有就跳过。 |
56
+ | `enableSetting` | `enableSetting(page, key, value)` | `Promise<void>` | `localStorage.setItem(key, String(value))`。 |
57
+ | `mockPost` | `mockPost(page, pattern, body, status = 200)` | `Promise<void>` | 用 `page.route(pattern)` 拦截,返回 `JSON.stringify(body)`,`content-type` 为 `application/json`。 |
58
+ | `typeDraft` | `typeDraft(page, text)` | `Promise<void>` | 点第一个 `[contenteditable="true"]`,键盘输入 `text`,等 600 ms。 |
59
+ | `clickAttr` | `clickAttr(page, attr)` | `Promise<void>` | 强制点第一个带 `[attr]` 的元素,等 1200 ms。 |
60
+ | `click` | `click(page, selector, opts = {})` | `Promise<void>` | 点第一个匹配项,再等 `opts.settleMs`(默认 300 ms)。 |
61
+ | `waitFor` | `waitFor(page, selector, timeout = 15000)` | `Promise<void>` | 等第一个匹配项到 `state: 'visible'`。 |
62
+ | `smoke` | `smoke(page, attr)` | `Promise<boolean>` | `[attr]` 至少匹配到一个元素时为 `true`——`attr` 要传完整属性名(如 `'data-my-plugin'`),即插件真的挂上了的证据。 |
63
+ | `rects` | `rects(page, prevSel, cardSel)` | `Promise<object>` | 两个元素的几何,外加左右/宽度差与垂直间距。 |
64
+ | `shot` | `shot(page, filePath)` | `Promise<string>` | 截图到 `filePath` 并原样返回该路径。 |
65
+ | `golden` | `golden(page, name, opts)` | `Promise<object>` | 像素金标准比对(带容差);`opts.goldenDir` 必填。 |
66
+ | `ariaSnapshot` | `ariaSnapshot(page, sel = 'body')` | `Promise<string>` | 对 `sel` 调 `Locator.ariaSnapshot()`。 |
67
+ | `goldenAria` | `goldenAria(page, name, opts)` | `Promise<object>` | 归一化后的 aria 金标准比对(带行级容差);`opts.dir` 必填。 |
68
+
69
+ ### `launchBrowser` 选项
70
+
71
+ | 选项 | 默认 | 含义 |
72
+ |---|---|---|
73
+ | `headless` | `true` | 只有显式 `false` 才开有头。 |
74
+ | `channel` | 无 | `'msedge'`(系统 Edge)或任意 Playwright channel。`'chromium'` / `'bundled'` 视为「不指定 channel」。 |
75
+ | `viewport` | `{ width: 1440, height: 900 }` | context 视口。 |
76
+
77
+ `chromiumSandbox` 恒为 `false`。
78
+
79
+ ### `golden(page, name, opts)`
80
+
81
+ | 选项 | 默认 | 含义 |
82
+ |---|---|---|
83
+ | `goldenDir` | — | **必填。** `<name>.png` 所在目录,不存在会自动创建。 |
84
+ | `sel` | 整页 | 只截这个元素。 |
85
+ | `stabilize` | `true` | 注入 `animation/transition: none` 并等 `document.fonts.ready`。 |
86
+ | `pixelThreshold` | `0.1` | `pixelmatch` 的单像素色差阈值。 |
87
+ | `maxDiffRatio` | `0.002` | 允许的差异像素**占比**——即 0.2%。 |
88
+
89
+ 首次返回 `{ status: 'created', file }`;在容差内返回 `{ status: 'match', file, diffPixels, diffRatio }`;超出则返回 `{ status: 'diff', file, diffFile?, diffPixels?, diffRatio?, reason? }`——会在金标准旁写一个 `.diff.png`;尺寸不同则直接短路成 `status: 'diff'` 并带 `reason`。容差是**含等号**的:100×100(10000 px)配默认 `0.002` 时,20 个差异像素是 `match`(ratio `0.002`),21 个就是 `diff`(ratio `0.0021`)——两者均已实测。
90
+
91
+ ### `goldenAria(page, name, opts)`
92
+
93
+ | 选项 | 默认 | 含义 |
94
+ |---|---|---|
95
+ | `dir` | — | **必填。** `<name>.aria.txt` 所在目录。 |
96
+ | `sel` | `'body'` | 取快照的元素。 |
97
+ | `maxDiffLines` | `max(3, ceil(较大快照的行数 × 2%))` | 允许的差异行数。 |
98
+
99
+ 快照先归一化(每行 trim、压缩空白、丢空行)。返回 `{ status: 'created' | 'updated' | 'match' | 'diff', file, diffLines? }`;`diff` 时把新快照写到 `<name>.aria.diff.txt`。设 `GOLDEN_UPDATE=1` 重建基线(状态为 `updated`)。
100
+
101
+ `diffLines` 是**多重集差异**,所以改一行会算成 **2**(基线少一行、新快照多一行)。默认下限是 3,因此这种单行改动仍判 `match`;已与「重写 200 行」一起实测,后者报 `diffLines: 202`、`status: 'diff'`。
102
+
103
+ ### `rects` 返回值
104
+
105
+ 任一选择器没找到时返回 `{ found: false, hasPreview, hasCard }`;否则:
106
+
107
+ ```js
108
+ {
109
+ found: true,
110
+ preview: { left, top, bottom, width }, // 四舍五入到 px
111
+ card: { left, top, width },
112
+ leftDiff, // preview.left - card.left
113
+ widthDiff, // preview.width - card.width
114
+ gapAboveCard, // card.top - preview.bottom
115
+ }
116
+ ```
117
+
118
+ ## 注意事项
119
+
120
+ - **`page` 永远是首参**——`click`、`waitFor`、`smoke` 也不例外。`dsh-plugin-e2e` 的 spec helper 看着不一致,只是因为它们把 `page` 预绑了:那里 `click(sel)` / `waitFor(sel)` / `smoke(attr)` / `dismissOnboarding()` 是已绑 `page` 的包装,而 `enableSetting` / `mockPost` / `typeDraft` / `clickAttr` / `rects` 仍由 spec 自己传 `page`。本包自身从不隐藏 `page`。
121
+ - **等待是写死的**:`typeDraft` 600 ms、`clickAttr` 1200 ms、`click` 300 ms(`settleMs`)、`dismissOnboarding` 800 ms。点完别立刻断言 DOM;插件若有防抖,调大 `settleMs`。
122
+ - **`goldenDir` / `dir` 实际是必填**:`mkdir(undefined)` 会在任何比对发生之前抛 `TypeError: The "path" argument must be of type string...`,所以拿到的是一个莫名其妙的报错而不是金标准文件。`dsh-plugin-e2e` 总会传这两个值。
123
+ - `golden` 比的是像素不是字节——0.2% 容差用来吸收跨机器字体与抗锯齿抖动。`goldenAria` 比的是文本行,所以能容忍布局位移,但容忍不了内容变化。
124
+ - 装到的 Playwright 没有 `Locator.ariaSnapshot` 时,`ariaSnapshot` 抛 `ariaSnapshot 不可用(需较新 Playwright)`。
125
+ - `enableSetting` 写入的是字符串;要在插件读取该开关的那次 reload / `goto` **之前**调用。
126
+ - `dsh-plugin-e2e/browser` 原样转发本包,原有的 `dsh-plugin-e2e/browser` 导入继续可用。
127
+
128
+ ## 许可证
129
+
130
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-plugin-ui-actions",
3
- "version": "0.2.9",
3
+ "version": "0.2.10",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -10,7 +10,8 @@
10
10
  },
11
11
  "files": [
12
12
  "src/",
13
- "README.md"
13
+ "README.md",
14
+ "README.zh-CN.md"
14
15
  ],
15
16
  "dependencies": {
16
17
  "playwright-core": "^1.49.0",