dsh-browser-verify 0.1.2 → 0.1.3

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
@@ -18,7 +18,7 @@ verification is just tool calls.
18
18
  ## Quick start
19
19
 
20
20
  ```sh
21
- dsh plugin --profile web add dsh-browser-verify@0.1.2
21
+ dsh plugin --profile web add dsh-browser-verify@0.1.3
22
22
  ```
23
23
 
24
24
  1. **Install** with the command above (or see [Install](#install)).
@@ -44,7 +44,7 @@ state?"; add `browser_screenshot` when you need to see the layout, or
44
44
 
45
45
  | Tool | Purpose |
46
46
  |---|---|
47
- | `browser_open` | Open a URL in a fresh scenario (headless Chromium, default viewport 390×844 @2x) and report title / HTTP status / visible-text summary / console errors. Optional `waitSelector` waits for a key element before returning, and optional inline `mocks` intercept APIs **before** the first navigation — for pages that boot against mocked data. |
47
+ | `browser_open` | Open a URL in a fresh scenario (headless Chromium, default viewport 390×844 @2x) and report title / HTTP status / visible-text summary / console errors. Without `waitSelector` it waits for the page to render-settle (two identical consecutive visible-text samples, capped at ~3s) before snapshotting, so it never returns the boot/skeleton frame; loading-state noise (`加载中...` etc.) is filtered out of the summary. Optional `waitSelector` waits for a key element before returning, and optional inline `mocks` intercept APIs **before** the first navigation — for pages that boot against mocked data. |
48
48
  | `browser_mock` | Register a playwright-glob route (`**/api/*.do*`) returning your JSON, then auto-reload the page to show the mocked state — the quickest way to verify empty / error / abnormal states without touching the backend. Duplicate patterns are rejected with a hint. |
49
49
  | `browser_assert` | The cheapest and most precise check: wait for a CSS selector, verify its count and contained text, return `{pass, count, actualText, elapsedMs}`. A mismatch is `pass:false` (with the diff), never a throw — so failure is a first-class result, not an error you debug. |
50
50
  | `browser_screenshot` | Capture the current page (viewport or full page) and **auto-project the image block into the model context** — the model sees the layout without any file handling. Reports dimensions, sha256, and `identicalToPrevious:true` when the shot is byte-identical to the previous one (page probably not refreshed). |
@@ -69,7 +69,7 @@ browser_screenshot
69
69
  ## Install
70
70
 
71
71
  ```sh
72
- dsh plugin --profile web add dsh-browser-verify@0.1.2
72
+ dsh plugin --profile web add dsh-browser-verify@0.1.3
73
73
  ```
74
74
 
75
75
  The version is pinned on purpose: pnpm 11 holds back packages published in the
@@ -120,7 +120,7 @@ first `browser_open` fails with an actionable install hint.
120
120
 
121
121
  ## Testing status
122
122
 
123
- 39 unit tests (fully offline — no browser needed), strict typecheck, and a
123
+ 41 unit tests (fully offline — no browser needed), strict typecheck, and a
124
124
  per-file ≥90% statement coverage gate. Verified **end-to-end in the real DSH
125
125
  web GUI** on dsh 0.1.2-alpha.4: a two-state loop (empty + normal) against a
126
126
  live uni-app H5 (hhhweb) in 6 tool calls, with screenshots auto-projected and
package/README.zh.md CHANGED
@@ -12,7 +12,7 @@
12
12
  ## 快速上手
13
13
 
14
14
  ```sh
15
- dsh plugin --profile web add dsh-browser-verify@0.1.2
15
+ dsh plugin --profile web add dsh-browser-verify@0.1.3
16
16
  ```
17
17
 
18
18
  1. **安装**(更多方式见 [安装](#安装))。
@@ -36,7 +36,7 @@ browser_assert selector=".empty-wrap" text="暂无可用缴费服务"
36
36
 
37
37
  | 工具 | 用途 |
38
38
  | ---------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
39
- | `browser_open` | 在全新场景中打开 URL(无头 Chromium,默认视口 390×844 @2x),返回 title / HTTP 状态 / 可见文本摘要 / console 错误。可选`waitSelector` 等待关键元素出现后再返回;可选内联 `mocks` 在**首次导航前**拦截接口——用于一启动就依赖 mock 数据的页面。 |
39
+ | `browser_open` | 在全新场景中打开 URL(无头 Chromium,默认视口 390×844 @2x),返回 title / HTTP 状态 / 可见文本摘要 / console 错误。不传 `waitSelector` 时默认等待页面**渲染稳定**后再采样(连续两次相同可见文本即稳定,上限约 3s),不会采到启动骨架帧;`加载中...` 等加载态文案自动过滤。可选 `waitSelector` 等待关键元素出现后再返回;可选内联 `mocks` 在**首次导航前**拦截接口——用于一启动就依赖 mock 数据的页面。 |
40
40
  | `browser_mock` | 注册 playwright glob 路由(如`**/api/*.do*`)返回指定 JSON,并自动 reload 页面展示 mock 状态——不碰后端最快验证空态/异常态。重复 pattern 会提示报错。 |
41
41
  | `browser_assert` | 最省 token 也最精确的验证:等待 CSS 选择器出现,校验数量与包含文本,返回`{pass, count, actualText, elapsedMs}`。不满足时返回 `pass:false`(附差异),**绝不抛错**——失败是一等公民的结果,而不是要调试的异常。 |
42
42
  | `browser_screenshot` | 截取当前页面(视口或整页),**自动以 image block 投影到模型上下文**——模型直接"看见"版式,无需任何文件处理。返回尺寸、sha256;与上一张完全一致时 `identicalToPrevious:true`(疑似页面未刷新)。 |
@@ -61,7 +61,7 @@ browser_screenshot
61
61
  ## 安装
62
62
 
63
63
  ```sh
64
- dsh plugin --profile web add dsh-browser-verify@0.1.2
64
+ dsh plugin --profile web add dsh-browser-verify@0.1.3
65
65
  ```
66
66
 
67
67
  版本故意钉死:pnpm 11 会暂缓 24 小时内新发布的包,裸写 `add dsh-browser-verify`(latest)会在发布当天装到上一个版本。`--profile web`
@@ -101,7 +101,7 @@ npx playwright install chromium # 需要 playwright-core@1.62.0
101
101
 
102
102
  ## 测试状态
103
103
 
104
- 39 个单测(完全离线,无需浏览器)、严格 typecheck、每文件 ≥90% 语句覆盖率闸门。已在 **dsh 0.1.2-alpha.4 真实 GUI 端到端验证**:对一个真实 uni-app H5(hhhweb),空态+正常态两态闭环共 6 次调用,截图自动投影,无临时目录残留、无僵尸进程。
104
+ 41 个单测(完全离线,无需浏览器)、严格 typecheck、每文件 ≥90% 语句覆盖率闸门。已在 **dsh 0.1.2-alpha.4 真实 GUI 端到端验证**:对一个真实 uni-app H5(hhhweb),空态+正常态两态闭环共 6 次调用,截图自动投影,无临时目录残留、无僵尸进程。
105
105
 
106
106
  ## 已知限制
107
107
 
package/lib/cli.js CHANGED
@@ -1,4 +1,4 @@
1
- import { t as BrowserDriver } from "./driver-DxMldTYf.js";
1
+ import { t as BrowserDriver } from "./driver-Dwuu4MgP.js";
2
2
  import { readFile } from "node:fs/promises";
3
3
  import { tmpdir } from "node:os";
4
4
  import { join, resolve } from "node:path";
@@ -91,6 +91,19 @@ const MAX_VISIBLE = 8;
91
91
  const MAX_VISIBLE_LEN = 40;
92
92
  const MAX_ERRORS = 5;
93
93
  const MAX_ERROR_LEN = 120;
94
+ /** Poll interval and cap for the default render-settled wait (no waitSelector). */
95
+ const SETTLE_INTERVAL_MS = 250;
96
+ const SETTLE_CAP_MS = 3e3;
97
+ /**
98
+ * Loading-state noise (uni-app showLoading / boot toasts) filtered out of the
99
+ * visible summary. Anchored full-string patterns only, so business states like
100
+ * 加载失败 / 加载更多 are never affected.
101
+ */
102
+ const NOISE_TEXT_PATTERN = /^(加载中|正在加载|请稍候|loading)[.…]{0,3}$/i;
103
+ /** True for transient loading-state text that should not pollute the summary. */
104
+ function isNoiseText(text) {
105
+ return NOISE_TEXT_PATTERN.test(text);
106
+ }
94
107
  function assertNoMockConflict(patterns, next) {
95
108
  if (patterns.includes(next)) throw new Error(`browser-verify: 拦截 pattern 已存在: ${next}(已有: ${patterns.join(", ")})。请先 browser_open 重开场景或用不同的 urlPattern。`);
96
109
  }
@@ -110,7 +123,7 @@ function summarizeVisibleText(texts) {
110
123
  const out = [];
111
124
  for (const raw of texts) {
112
125
  const trimmed = raw.trim();
113
- if (trimmed === "") continue;
126
+ if (trimmed === "" || isNoiseText(trimmed)) continue;
114
127
  const reduced = trimmed.length > MAX_VISIBLE_LEN ? trimmed.slice(0, MAX_VISIBLE_LEN) : trimmed;
115
128
  if (seen.has(reduced)) continue;
116
129
  seen.add(reduced);
@@ -159,6 +172,7 @@ var Scenario = class {
159
172
  timeout
160
173
  });
161
174
  if (opts.waitSelector !== void 0) await this.page.waitForSelector(opts.waitSelector, { timeout });
175
+ else await this.waitUntilRendered(timeout);
162
176
  const texts = await this.page.evaluate(VISIBLE_TEXT_SCRIPT);
163
177
  return {
164
178
  title: await this.page.title(),
@@ -169,6 +183,26 @@ var Scenario = class {
169
183
  elapsedMs: Date.now() - started
170
184
  };
171
185
  }
186
+ /**
187
+ * Default settle wait: two consecutive identical non-empty visible-text
188
+ * samples (250ms apart) mean the render stopped changing. Bounded by
189
+ * min(timeoutMs, 3s) — pages that never settle (polling/animations) fall
190
+ * through to the current snapshot instead of burning the whole budget.
191
+ */
192
+ async waitUntilRendered(timeoutMs) {
193
+ const cap = Math.min(timeoutMs, SETTLE_CAP_MS);
194
+ const deadline = Date.now() + cap;
195
+ let prev = null;
196
+ for (;;) {
197
+ const sig = (await this.page.evaluate(VISIBLE_TEXT_SCRIPT)).join("\0");
198
+ if (sig !== "") {
199
+ if (prev !== null && sig === prev) return;
200
+ prev = sig;
201
+ }
202
+ if (Date.now() >= deadline) return;
203
+ await new Promise((resolve) => setTimeout(resolve, SETTLE_INTERVAL_MS));
204
+ }
205
+ }
172
206
  async addMock(rule) {
173
207
  assertNoMockConflict([...this.mocks.keys()], rule.urlPattern);
174
208
  const status = rule.status ?? 200;
package/lib/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { t as BrowserDriver } from "./driver-DxMldTYf.js";
1
+ import { t as BrowserDriver } from "./driver-Dwuu4MgP.js";
2
2
  import { exec } from "node:child_process";
3
3
  import { readdir, rm, stat } from "node:fs/promises";
4
4
  import { tmpdir } from "node:os";
@@ -111,7 +111,7 @@ function registerBrowserTools(ctx) {
111
111
  });
112
112
  ctx.tools.register(defineTool({
113
113
  name: "browser_open",
114
- description: "在无头浏览器中打开一个页面并返回页面状态(标题/状态码/可见文本摘要/console 错误)用于验证前端页面;可选 waitSelector 等待关键元素出现,默认视口 390×844 @2x(移动端形态)。可传 mocks 在打开时拦截接口(用于启动即依赖接口数据的页面)。验证顺序:先 browser_assert 做 DOM 断言,确需看版式再 browser_screenshot。",
114
+ description: "在无头浏览器中打开一个页面并返回页面状态(标题/状态码/可见文本摘要/console 错误)用于验证前端页面;可选 waitSelector 等待关键元素出现(不传时自动等待页面渲染稳定后返回,加载态文案如「加载中...」自动过滤),默认视口 390×844 @2x(移动端形态)。可传 mocks 在打开时拦截接口(用于启动即依赖接口数据的页面)。验证顺序:先 browser_assert 做 DOM 断言,确需看版式再 browser_screenshot。",
115
115
  parameters: {
116
116
  url: {
117
117
  type: "string",
@@ -165,7 +165,7 @@ function registerBrowserTools(ctx) {
165
165
  },
166
166
  waitSelector: {
167
167
  type: "string",
168
- description: "可选:等待该选择器出现后再返回(优先于固定等待)"
168
+ description: "可选:等待该选择器出现后再返回;不传则自动等待页面渲染稳定后返回(最多数秒)"
169
169
  },
170
170
  timeoutMs: {
171
171
  type: "number",
@@ -23,6 +23,8 @@ export interface AssertResult {
23
23
  actualText: string | null;
24
24
  elapsedMs: number;
25
25
  }
26
+ /** True for transient loading-state text that should not pollute the summary. */
27
+ export declare function isNoiseText(text: string): boolean;
26
28
  export declare function assertNoMockConflict(patterns: string[], next: string): void;
27
29
  export declare function normalizeCountSpec(count: number | {
28
30
  min: number;
@@ -48,6 +50,13 @@ export declare class Scenario {
48
50
  waitSelector?: string;
49
51
  timeoutMs?: number;
50
52
  }): Promise<OpenResult>;
53
+ /**
54
+ * Default settle wait: two consecutive identical non-empty visible-text
55
+ * samples (250ms apart) mean the render stopped changing. Bounded by
56
+ * min(timeoutMs, 3s) — pages that never settle (polling/animations) fall
57
+ * through to the current snapshot instead of burning the whole budget.
58
+ */
59
+ private waitUntilRendered;
51
60
  addMock(rule: {
52
61
  urlPattern: string;
53
62
  json: unknown;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dsh-browser-verify",
3
3
  "description": "Read-only browser verification tools for the DeepSeek Harness web GUI: browser_open / browser_mock / browser_assert / browser_screenshot — verify a page (H5/desktop) in ≤4 tool calls with mock interception, DOM assertions, and screenshots that auto-project into the model context.",
4
- "version": "0.1.2",
4
+ "version": "0.1.3",
5
5
  "type": "module",
6
6
  "packageManager": "pnpm@11.7.0",
7
7
  "engines": {
@@ -34,6 +34,20 @@ const MAX_VISIBLE_LEN = 40
34
34
  const MAX_ERRORS = 5
35
35
  const MAX_ERROR_LEN = 120
36
36
  const MAX_DIFF_LEN = 120
37
+ /** Poll interval and cap for the default render-settled wait (no waitSelector). */
38
+ const SETTLE_INTERVAL_MS = 250
39
+ const SETTLE_CAP_MS = 3000
40
+ /**
41
+ * Loading-state noise (uni-app showLoading / boot toasts) filtered out of the
42
+ * visible summary. Anchored full-string patterns only, so business states like
43
+ * 加载失败 / 加载更多 are never affected.
44
+ */
45
+ const NOISE_TEXT_PATTERN = /^(加载中|正在加载|请稍候|loading)[.…]{0,3}$/i
46
+
47
+ /** True for transient loading-state text that should not pollute the summary. */
48
+ export function isNoiseText(text: string): boolean {
49
+ return NOISE_TEXT_PATTERN.test(text)
50
+ }
37
51
 
38
52
  export function assertNoMockConflict(patterns: string[], next: string): void {
39
53
  if (patterns.includes(next)) {
@@ -52,7 +66,7 @@ export function summarizeVisibleText(texts: string[]): string[] {
52
66
  const out: string[] = []
53
67
  for (const raw of texts) {
54
68
  const trimmed = raw.trim()
55
- if (trimmed === '') continue
69
+ if (trimmed === '' || isNoiseText(trimmed)) continue
56
70
  const reduced = trimmed.length > MAX_VISIBLE_LEN ? trimmed.slice(0, MAX_VISIBLE_LEN) : trimmed
57
71
  if (seen.has(reduced)) continue
58
72
  seen.add(reduced)
@@ -106,6 +120,11 @@ export class Scenario {
106
120
  const response = await this.page.goto(opts.url, { waitUntil: 'domcontentloaded', timeout })
107
121
  if (opts.waitSelector !== undefined) {
108
122
  await this.page.waitForSelector(opts.waitSelector, { timeout })
123
+ } else {
124
+ // No explicit selector: wait for the page to render-settle instead of
125
+ // snapshotting the boot frame (SPAs like uni-app show skeleton/loading
126
+ // right after domcontentloaded — a 500ms snapshot reads empty).
127
+ await this.waitUntilRendered(timeout)
109
128
  }
110
129
  const texts = await this.page.evaluate(VISIBLE_TEXT_SCRIPT) as string[]
111
130
  return {
@@ -118,6 +137,27 @@ export class Scenario {
118
137
  }
119
138
  }
120
139
 
140
+ /**
141
+ * Default settle wait: two consecutive identical non-empty visible-text
142
+ * samples (250ms apart) mean the render stopped changing. Bounded by
143
+ * min(timeoutMs, 3s) — pages that never settle (polling/animations) fall
144
+ * through to the current snapshot instead of burning the whole budget.
145
+ */
146
+ private async waitUntilRendered(timeoutMs: number): Promise<void> {
147
+ const cap = Math.min(timeoutMs, SETTLE_CAP_MS)
148
+ const deadline = Date.now() + cap
149
+ let prev: string | null = null
150
+ for (;;) {
151
+ const sig = (await this.page.evaluate(VISIBLE_TEXT_SCRIPT) as string[]).join('\u0000')
152
+ if (sig !== '') {
153
+ if (prev !== null && sig === prev) return
154
+ prev = sig
155
+ }
156
+ if (Date.now() >= deadline) return
157
+ await new Promise(resolve => setTimeout(resolve, SETTLE_INTERVAL_MS))
158
+ }
159
+ }
160
+
121
161
  async addMock(rule: { urlPattern: string; json: unknown; status?: number; reload?: boolean; timeoutMs?: number }): Promise<string[]> {
122
162
  assertNoMockConflict([...this.mocks.keys()], rule.urlPattern)
123
163
  const status = rule.status ?? 200
@@ -24,7 +24,7 @@ export function registerBrowserTools(ctx: Context): void {
24
24
 
25
25
  ctx.tools.register(defineTool({
26
26
  name: 'browser_open',
27
- description: '在无头浏览器中打开一个页面并返回页面状态(标题/状态码/可见文本摘要/console 错误)用于验证前端页面;可选 waitSelector 等待关键元素出现,默认视口 390×844 @2x(移动端形态)。可传 mocks 在打开时拦截接口(用于启动即依赖接口数据的页面)。验证顺序:先 browser_assert 做 DOM 断言,确需看版式再 browser_screenshot。',
27
+ description: '在无头浏览器中打开一个页面并返回页面状态(标题/状态码/可见文本摘要/console 错误)用于验证前端页面;可选 waitSelector 等待关键元素出现(不传时自动等待页面渲染稳定后返回,加载态文案如「加载中...」自动过滤),默认视口 390×844 @2x(移动端形态)。可传 mocks 在打开时拦截接口(用于启动即依赖接口数据的页面)。验证顺序:先 browser_assert 做 DOM 断言,确需看版式再 browser_screenshot。',
28
28
  parameters: {
29
29
  url: { type: 'string', required: true, description: '页面地址,如 http://localhost:5173/hweb/pages/...' },
30
30
  viewport: {
@@ -50,7 +50,7 @@ export function registerBrowserTools(ctx: Context): void {
50
50
  },
51
51
  description: '可选:页面启动前注册的接口拦截(glob urlPattern + json,如 [{urlPattern: "**/api/*.do*", json: {...}}])',
52
52
  },
53
- waitSelector: { type: 'string', description: '可选:等待该选择器出现后再返回(优先于固定等待)' },
53
+ waitSelector: { type: 'string', description: '可选:等待该选择器出现后再返回;不传则自动等待页面渲染稳定后返回(最多数秒)' },
54
54
  timeoutMs: { type: 'number', description: `加载超时,默认 ${envTimeoutMs()}ms` },
55
55
  },
56
56
  output: {