@opentiny/next-sdk 0.4.2 → 0.4.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/dist/{SimulatorMask-BHVXyogh-CARX3Rff.js → SimulatorMask-BHVXyogh-CKmEDsYG.js} +1 -1
- package/dist/{index-R_HIbfUX.js → index-CHMa2zG4.js} +1065 -1042
- package/dist/index.js +1 -1
- package/dist/page-tools/a11y/config.d.ts +6 -3
- package/dist/page-tools/a11y/constants.d.ts +20 -0
- package/dist/page-tools/a11y/utils.d.ts +14 -0
- package/dist/page-tools/constants.d.ts +4 -2
- package/dist/page-tools/context.d.ts +2 -0
- package/dist/page-tools/handlers/hover.d.ts +9 -0
- package/dist/page-tools/schema.d.ts +3 -3
- package/dist/page-tools/utils/dom.d.ts +8 -2
- package/dist/runtime.js +19 -172
- package/package.json +6 -1
- package/WebMcpClient.ts +0 -505
- package/WebMcpServer.ts +0 -447
- package/agent/AgentModelProvider.ts +0 -932
- package/agent/type.ts +0 -91
- package/agent/utils/generateReActPrompt.ts +0 -71
- package/agent/utils/getAISDKTools.ts +0 -34
- package/agent/utils/getBuiltinMcpTools.ts +0 -86
- package/agent/utils/parseReActAction.ts +0 -34
- package/core.ts +0 -26
- package/index.ts +0 -91
- package/page-tools/a11y/build.ts +0 -74
- package/page-tools/a11y/config.ts +0 -465
- package/page-tools/a11y/constants.ts +0 -131
- package/page-tools/a11y/search.ts +0 -127
- package/page-tools/a11y/types.ts +0 -105
- package/page-tools/a11y/utils.ts +0 -239
- package/page-tools/a11y/vnode.ts +0 -439
- package/page-tools/a11y-tree.ts +0 -14
- package/page-tools/bridge.ts +0 -722
- package/page-tools/configs/console-cloud.ts +0 -172
- package/page-tools/constants.ts +0 -12
- package/page-tools/context.ts +0 -50
- package/page-tools/effects.ts +0 -345
- package/page-tools/handlers/browserState.ts +0 -12
- package/page-tools/handlers/click.ts +0 -30
- package/page-tools/handlers/executeJavascript.ts +0 -22
- package/page-tools/handlers/fill.ts +0 -65
- package/page-tools/handlers/scroll.ts +0 -66
- package/page-tools/handlers/searchTree.ts +0 -27
- package/page-tools/handlers/select.ts +0 -39
- package/page-tools/initialize-builtin-WebMCP.ts +0 -20
- package/page-tools/page-agent-highlight/index.ts +0 -245
- package/page-tools/page-agent-mask/SimulatorMask.module.css +0 -14
- package/page-tools/page-agent-mask/SimulatorMask.ts +0 -299
- package/page-tools/page-agent-mask/checkDarkMode.ts +0 -181
- package/page-tools/page-agent-mask/cursor-border.svg +0 -3
- package/page-tools/page-agent-mask/cursor-fill.svg +0 -5
- package/page-tools/page-agent-mask/cursor.module.css +0 -70
- package/page-tools/page-agent-mask/hauwei.svg +0 -25
- package/page-tools/page-agent-prompt.md +0 -139
- package/page-tools/page-agent-tool-event.ts +0 -113
- package/page-tools/page-agent-tool.ts +0 -207
- package/page-tools/page-state-cache.ts +0 -78
- package/page-tools/schema.ts +0 -52
- package/page-tools/tool-config.ts +0 -100
- package/page-tools/utils/dom.ts +0 -158
- package/page-tools/utils/scroll.ts +0 -58
- package/remoter/QrCode.ts +0 -54
- package/remoter/createRemoter.ts +0 -957
- package/remoter/svgs/chat.svg +0 -43
- package/remoter/svgs/icon-copy.svg +0 -25
- package/remoter/svgs/link.svg +0 -28
- package/remoter/svgs/logo.svg +0 -45
- package/remoter/svgs/qrcode.svg +0 -35
- package/remoter/svgs/scan.svg +0 -21
- package/remoter/tooltips.ts +0 -260
- package/runtime.html +0 -39
- package/runtime.ts +0 -44
- package/script/utils.ts +0 -26
- package/skills/index.ts +0 -287
- package/test/page-tools/a11y/build.test.ts +0 -638
- package/test/page-tools/a11y/config.test.ts +0 -370
- package/test/page-tools/configs/console-cloud.test.ts +0 -168
- package/test/page-tools/page-agent-highlight.test.ts +0 -110
- package/test/page-tools/page-agent-tool-dispatch.test.ts +0 -208
- package/test/page-tools/page-agent-tool.test.ts +0 -102
- package/test/page-tools/tool-config.test.ts +0 -112
- package/test/page-tools/utils/dom.test.ts +0 -122
- package/tsconfig.json +0 -16
- package/utils/builtinProxy.ts +0 -144
- package/utils/env.ts +0 -13
- package/utils/uuid.ts +0 -10
- package/vite.config.runtime.ts +0 -22
- package/vite.config.ts +0 -63
- package/vitest.config.ts +0 -10
|
@@ -1,208 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, beforeAll, beforeEach, afterAll, vi } from 'vitest'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 保障 page-agent-tool 的 switch(action) 不会 fall-through:
|
|
5
|
-
* 每个 action 只能调用对应 handler 一次,且不得落入后续分支 / default。
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
const { handlerMocks } = vi.hoisted(() => {
|
|
9
|
-
const marker = (name: string) => ({
|
|
10
|
-
content: [{ type: 'text' as const, text: `ok:${name}` }],
|
|
11
|
-
})
|
|
12
|
-
return {
|
|
13
|
-
handlerMocks: {
|
|
14
|
-
handleBrowserState: vi.fn(async () => marker('browserState')),
|
|
15
|
-
handleClick: vi.fn(async () => marker('click')),
|
|
16
|
-
handleFill: vi.fn(async () => marker('fill')),
|
|
17
|
-
handleSelect: vi.fn(async () => marker('select')),
|
|
18
|
-
handleScroll: vi.fn(async () => marker('scroll')),
|
|
19
|
-
handleExecuteJavascript: vi.fn(async () => marker('executeJavascript')),
|
|
20
|
-
handleSearchTree: vi.fn(async () => marker('searchTree')),
|
|
21
|
-
},
|
|
22
|
-
}
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
vi.mock('../../page-tools/page-agent-mask/SimulatorMask', () => ({
|
|
26
|
-
SimulatorMask: class SimulatorMask {
|
|
27
|
-
show() {}
|
|
28
|
-
hide() {}
|
|
29
|
-
dispose() {}
|
|
30
|
-
borderElement() {}
|
|
31
|
-
removeBorderElement() {}
|
|
32
|
-
},
|
|
33
|
-
}))
|
|
34
|
-
|
|
35
|
-
vi.mock('@page-agent/page-controller', () => ({
|
|
36
|
-
PageController: class PageController {
|
|
37
|
-
mask = {
|
|
38
|
-
borderElement() {},
|
|
39
|
-
removeBorderElement() {},
|
|
40
|
-
}
|
|
41
|
-
maskReady = Promise.resolve()
|
|
42
|
-
async showMask() {}
|
|
43
|
-
async hideMask() {}
|
|
44
|
-
},
|
|
45
|
-
}))
|
|
46
|
-
|
|
47
|
-
vi.mock('../../page-tools/handlers/browserState', () => ({
|
|
48
|
-
handleBrowserState: handlerMocks.handleBrowserState,
|
|
49
|
-
}))
|
|
50
|
-
vi.mock('../../page-tools/handlers/click', () => ({
|
|
51
|
-
handleClick: handlerMocks.handleClick,
|
|
52
|
-
}))
|
|
53
|
-
vi.mock('../../page-tools/handlers/fill', () => ({
|
|
54
|
-
handleFill: handlerMocks.handleFill,
|
|
55
|
-
}))
|
|
56
|
-
vi.mock('../../page-tools/handlers/select', () => ({
|
|
57
|
-
handleSelect: handlerMocks.handleSelect,
|
|
58
|
-
}))
|
|
59
|
-
vi.mock('../../page-tools/handlers/scroll', () => ({
|
|
60
|
-
handleScroll: handlerMocks.handleScroll,
|
|
61
|
-
}))
|
|
62
|
-
vi.mock('../../page-tools/handlers/executeJavascript', () => ({
|
|
63
|
-
handleExecuteJavascript: handlerMocks.handleExecuteJavascript,
|
|
64
|
-
}))
|
|
65
|
-
vi.mock('../../page-tools/handlers/searchTree', () => ({
|
|
66
|
-
handleSearchTree: handlerMocks.handleSearchTree,
|
|
67
|
-
}))
|
|
68
|
-
|
|
69
|
-
import { registerPageAgentTool } from '../../page-tools/page-agent-tool'
|
|
70
|
-
import type { PageAgentToolInput } from '../../page-tools/schema'
|
|
71
|
-
|
|
72
|
-
interface ToolTextContent {
|
|
73
|
-
type: string
|
|
74
|
-
text: string
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
interface ToolExecuteResult {
|
|
78
|
-
content: ToolTextContent[]
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
type PageAgentExecute = (args: PageAgentToolInput) => Promise<ToolExecuteResult>
|
|
82
|
-
|
|
83
|
-
interface RegisterablePageAgentTool {
|
|
84
|
-
name?: string
|
|
85
|
-
execute?: PageAgentExecute
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
interface ModelContextWithRegisterTool {
|
|
89
|
-
registerTool: (
|
|
90
|
-
tool: RegisterablePageAgentTool,
|
|
91
|
-
options?: unknown
|
|
92
|
-
) => unknown
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
interface DocumentWithModelContext {
|
|
96
|
-
modelContext?: ModelContextWithRegisterTool
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
const ACTIONS = [
|
|
100
|
-
'browserState',
|
|
101
|
-
'click',
|
|
102
|
-
'fill',
|
|
103
|
-
'select',
|
|
104
|
-
'scroll',
|
|
105
|
-
'executeJavascript',
|
|
106
|
-
'searchTree',
|
|
107
|
-
] as const
|
|
108
|
-
|
|
109
|
-
type ActionName = (typeof ACTIONS)[number]
|
|
110
|
-
|
|
111
|
-
const handlerByAction: Record<ActionName, keyof typeof handlerMocks> = {
|
|
112
|
-
browserState: 'handleBrowserState',
|
|
113
|
-
click: 'handleClick',
|
|
114
|
-
fill: 'handleFill',
|
|
115
|
-
select: 'handleSelect',
|
|
116
|
-
scroll: 'handleScroll',
|
|
117
|
-
executeJavascript: 'handleExecuteJavascript',
|
|
118
|
-
searchTree: 'handleSearchTree',
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
function resetWindowGlobals() {
|
|
122
|
-
delete window.__webmcpcli_toolConfig
|
|
123
|
-
delete window.__webmcpcli_beforeGetBrowserState
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
function captureExecute(): PageAgentExecute {
|
|
127
|
-
const mcp = (document as DocumentWithModelContext).modelContext
|
|
128
|
-
if (!mcp?.registerTool) {
|
|
129
|
-
throw new Error('document.modelContext.registerTool unavailable')
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
let execute: PageAgentExecute | null = null
|
|
133
|
-
const original = mcp.registerTool.bind(mcp)
|
|
134
|
-
mcp.registerTool = (tool: RegisterablePageAgentTool, options?: unknown) => {
|
|
135
|
-
if (tool?.name === 'page-agent-tool' && typeof tool.execute === 'function') {
|
|
136
|
-
execute = tool.execute.bind(tool)
|
|
137
|
-
}
|
|
138
|
-
return original(tool, options)
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
try {
|
|
142
|
-
registerPageAgentTool()
|
|
143
|
-
} finally {
|
|
144
|
-
mcp.registerTool = original
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
if (!execute) {
|
|
148
|
-
throw new Error('failed to capture page-agent-tool execute')
|
|
149
|
-
}
|
|
150
|
-
return execute
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
function argsFor(action: ActionName): PageAgentToolInput {
|
|
154
|
-
switch (action) {
|
|
155
|
-
case 'browserState':
|
|
156
|
-
return { action, responseMode: 'full', contextLines: 2, maxMatches: 20 }
|
|
157
|
-
case 'click':
|
|
158
|
-
return { action, index: 0, contextLines: 2, maxMatches: 20 }
|
|
159
|
-
case 'fill':
|
|
160
|
-
return { action, index: 0, text: 'hello', contextLines: 2, maxMatches: 20 }
|
|
161
|
-
case 'select':
|
|
162
|
-
return { action, index: 0, text: 'opt', contextLines: 2, maxMatches: 20 }
|
|
163
|
-
case 'scroll':
|
|
164
|
-
return { action, down: true, numPages: 1, contextLines: 2, maxMatches: 20 }
|
|
165
|
-
case 'executeJavascript':
|
|
166
|
-
return { action, script: '1+1', contextLines: 2, maxMatches: 20 }
|
|
167
|
-
case 'searchTree':
|
|
168
|
-
return { action, query: 'button', contextLines: 2, maxMatches: 20 }
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
describe('page-agent-tool action dispatch(防 switch fall-through)', () => {
|
|
173
|
-
let execute: PageAgentExecute
|
|
174
|
-
|
|
175
|
-
beforeAll(async () => {
|
|
176
|
-
resetWindowGlobals()
|
|
177
|
-
execute = captureExecute()
|
|
178
|
-
// 等待 pageController.maskReady 把 SimulatorMask 挂上
|
|
179
|
-
await Promise.resolve()
|
|
180
|
-
await Promise.resolve()
|
|
181
|
-
})
|
|
182
|
-
|
|
183
|
-
beforeEach(() => {
|
|
184
|
-
for (const mock of Object.values(handlerMocks)) {
|
|
185
|
-
mock.mockClear()
|
|
186
|
-
}
|
|
187
|
-
})
|
|
188
|
-
|
|
189
|
-
afterAll(() => {
|
|
190
|
-
resetWindowGlobals()
|
|
191
|
-
})
|
|
192
|
-
|
|
193
|
-
it.each(ACTIONS)('action=%s 只调用对应 handler,且不落入 default', async (action) => {
|
|
194
|
-
const result = await execute(argsFor(action))
|
|
195
|
-
|
|
196
|
-
expect(result.content[0]?.text).toBe(`ok:${action}`)
|
|
197
|
-
expect(result.content[0]?.text).not.toMatch(/^未知操作:/)
|
|
198
|
-
|
|
199
|
-
const expectedKey = handlerByAction[action]
|
|
200
|
-
for (const [key, mock] of Object.entries(handlerMocks)) {
|
|
201
|
-
if (key === expectedKey) {
|
|
202
|
-
expect(mock, `${key} should be called once`).toHaveBeenCalledTimes(1)
|
|
203
|
-
} else {
|
|
204
|
-
expect(mock, `${key} must not be called for action=${action}`).not.toHaveBeenCalled()
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
})
|
|
208
|
-
})
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
|
2
|
-
|
|
3
|
-
// jsdom 无 WebGL2,避免 SimulatorMask → ai-motion 初始化刷 stderr
|
|
4
|
-
vi.mock('../../page-tools/page-agent-mask/SimulatorMask', () => ({
|
|
5
|
-
SimulatorMask: class SimulatorMask {
|
|
6
|
-
show() {}
|
|
7
|
-
hide() {}
|
|
8
|
-
dispose() {}
|
|
9
|
-
},
|
|
10
|
-
}))
|
|
11
|
-
|
|
12
|
-
import { registerPageAgentTool } from '../../page-tools/page-agent-tool'
|
|
13
|
-
import { getPageAgentToolConfig, setPageAgentToolConfig } from '../../page-tools/tool-config'
|
|
14
|
-
|
|
15
|
-
function resetWindowGlobals() {
|
|
16
|
-
delete window.__webmcpcli_toolConfig
|
|
17
|
-
delete window.__webmcpcli_beforeGetBrowserState
|
|
18
|
-
// 注意:initializeBuiltinWebMCP() 内部用模块级单例 flag 控制只初始化一次 document.modelContext,
|
|
19
|
-
// 且没有对外暴露重置能力,因此这里不删除 document.modelContext,避免二次调用时因单例 flag
|
|
20
|
-
// 仍为 true 而不会重新创建 modelContext,导致 registerTool 拿到 undefined
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
beforeEach(() => {
|
|
24
|
-
resetWindowGlobals()
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
afterEach(() => {
|
|
28
|
-
resetWindowGlobals()
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
describe('registerPageAgentTool - 统一无障碍配置接入', () => {
|
|
32
|
-
it('仅传 options.a11yConfig 时,与默认配置合并生效', () => {
|
|
33
|
-
registerPageAgentTool({
|
|
34
|
-
a11yConfig: { roles: [{ role: 'tab', selector: '.tab-item' }] },
|
|
35
|
-
})
|
|
36
|
-
const config = getPageAgentToolConfig().a11yConfig
|
|
37
|
-
expect(config.roles).toEqual([{ role: 'tab', selector: '.tab-item' }])
|
|
38
|
-
// 默认内置规则仍然存在(不会被覆盖丢失)
|
|
39
|
-
expect(config.states.error?.length).toBeGreaterThan(0)
|
|
40
|
-
expect(window.__webmcpcli_toolConfig?.a11yConfig).toBe(config)
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
it('不传 a11yConfig 时,使用默认配置初始化', () => {
|
|
44
|
-
registerPageAgentTool()
|
|
45
|
-
const config = getPageAgentToolConfig().a11yConfig
|
|
46
|
-
expect(config.roles).toEqual([])
|
|
47
|
-
expect(config.states.error?.length).toBeGreaterThan(0)
|
|
48
|
-
})
|
|
49
|
-
|
|
50
|
-
it('a11yConfig 中的 whitelist/exposedAttributes 与默认配置合并,互不覆盖', () => {
|
|
51
|
-
registerPageAgentTool({
|
|
52
|
-
a11yConfig: {
|
|
53
|
-
whitelist: ['.new-whitelist'],
|
|
54
|
-
exposedAttributes: ['data-config-attr'],
|
|
55
|
-
},
|
|
56
|
-
})
|
|
57
|
-
const config = getPageAgentToolConfig().a11yConfig
|
|
58
|
-
expect(config.whitelist).toContain('.new-whitelist')
|
|
59
|
-
expect(config.exposedAttributes).toContain('data-config-attr')
|
|
60
|
-
})
|
|
61
|
-
|
|
62
|
-
it('重复调用 registerPageAgentTool 以 replace 模式重新初始化,不会无限累加旧配置', () => {
|
|
63
|
-
registerPageAgentTool({ a11yConfig: { roles: [{ role: 'tab', selector: '.v1' }] } })
|
|
64
|
-
registerPageAgentTool({ a11yConfig: { roles: [{ role: 'tab', selector: '.v2' }] } })
|
|
65
|
-
const config = getPageAgentToolConfig().a11yConfig
|
|
66
|
-
expect(config.roles).toEqual([{ role: 'tab', selector: '.v2' }])
|
|
67
|
-
})
|
|
68
|
-
})
|
|
69
|
-
|
|
70
|
-
describe('registerPageAgentTool - 顶层工具配置(PageAgentToolConfig)接入', () => {
|
|
71
|
-
it('不传 enableHighlight 时默认关闭元素高亮', () => {
|
|
72
|
-
registerPageAgentTool()
|
|
73
|
-
expect(getPageAgentToolConfig().enableHighlight).toBe(false)
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
it('options.enableHighlight = true 时运行期配置同步生效', () => {
|
|
77
|
-
registerPageAgentTool({ enableHighlight: true })
|
|
78
|
-
expect(getPageAgentToolConfig().enableHighlight).toBe(true)
|
|
79
|
-
expect(window.__webmcpcli_toolConfig?.enableHighlight).toBe(true)
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
it('注册后可通过 setPageAgentToolConfig 在运行期动态修改,无需重新 registerPageAgentTool', () => {
|
|
83
|
-
registerPageAgentTool({ enableHighlight: true })
|
|
84
|
-
setPageAgentToolConfig({ enableHighlight: false })
|
|
85
|
-
expect(getPageAgentToolConfig().enableHighlight).toBe(false)
|
|
86
|
-
})
|
|
87
|
-
|
|
88
|
-
it('重复调用 registerPageAgentTool 以 replace 模式重新初始化顶层配置', () => {
|
|
89
|
-
registerPageAgentTool({ enableHighlight: false })
|
|
90
|
-
registerPageAgentTool({ enableHighlight: true })
|
|
91
|
-
expect(getPageAgentToolConfig().enableHighlight).toBe(true)
|
|
92
|
-
})
|
|
93
|
-
|
|
94
|
-
it('a11yConfig 与顶层配置项互不影响,分别合并生效', () => {
|
|
95
|
-
registerPageAgentTool({
|
|
96
|
-
enableHighlight: false,
|
|
97
|
-
a11yConfig: { roles: [{ role: 'tab', selector: '.tab-item' }] },
|
|
98
|
-
})
|
|
99
|
-
expect(getPageAgentToolConfig().enableHighlight).toBe(false)
|
|
100
|
-
expect(getPageAgentToolConfig().a11yConfig.roles).toEqual([{ role: 'tab', selector: '.tab-item' }])
|
|
101
|
-
})
|
|
102
|
-
})
|
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
|
|
2
|
-
import {
|
|
3
|
-
getPageAgentToolConfig,
|
|
4
|
-
setPageAgentToolConfig,
|
|
5
|
-
DEFAULT_PAGE_AGENT_TOOL_CONFIG
|
|
6
|
-
} from '../../page-tools/tool-config'
|
|
7
|
-
|
|
8
|
-
function resetWindowGlobals() {
|
|
9
|
-
delete window.__webmcpcli_toolConfig
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
beforeEach(() => {
|
|
13
|
-
resetWindowGlobals()
|
|
14
|
-
})
|
|
15
|
-
|
|
16
|
-
afterEach(() => {
|
|
17
|
-
resetWindowGlobals()
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
describe('getPageAgentToolConfig / setPageAgentToolConfig', () => {
|
|
21
|
-
it('未初始化时返回默认配置', () => {
|
|
22
|
-
expect(getPageAgentToolConfig()).toEqual(DEFAULT_PAGE_AGENT_TOOL_CONFIG)
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
it('merge 模式下只覆盖传入的字段,未传入字段保持原值', () => {
|
|
26
|
-
setPageAgentToolConfig({ enableHighlight: false })
|
|
27
|
-
const current = getPageAgentToolConfig()
|
|
28
|
-
expect(current.enableHighlight).toBe(false)
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
it('merge 模式下只覆盖传入的字段,removeMaskAfterToolCall字段保持原值', () => {
|
|
32
|
-
setPageAgentToolConfig({ removeMaskAfterToolCall: false })
|
|
33
|
-
const current = getPageAgentToolConfig()
|
|
34
|
-
expect(current.removeMaskAfterToolCall).toBe(false)
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
it('merge 模式下只覆盖传入的字段,removeMaskAfterToolCall字段保持原值', () => {
|
|
38
|
-
setPageAgentToolConfig({ removeMaskAfterToolCall: true })
|
|
39
|
-
const current = getPageAgentToolConfig()
|
|
40
|
-
expect(current.removeMaskAfterToolCall).toBe(true)
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
it('函数式 patch:入参为当前生效配置,返回值与默认配置合并', () => {
|
|
44
|
-
setPageAgentToolConfig({ enableHighlight: false })
|
|
45
|
-
const next = setPageAgentToolConfig((current) => ({ enableHighlight: !current.enableHighlight }))
|
|
46
|
-
expect(next.enableHighlight).toBe(true)
|
|
47
|
-
expect(getPageAgentToolConfig().enableHighlight).toBe(true)
|
|
48
|
-
})
|
|
49
|
-
|
|
50
|
-
it('replace 模式丢弃之前的运行期状态,只与默认值重新合并', () => {
|
|
51
|
-
setPageAgentToolConfig({ enableHighlight: true })
|
|
52
|
-
setPageAgentToolConfig({}, { mode: 'replace' })
|
|
53
|
-
expect(getPageAgentToolConfig().enableHighlight).toBe(false)
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
it('setPageAgentToolConfig 后 getPageAgentToolConfig 读到最新值', () => {
|
|
57
|
-
const next = setPageAgentToolConfig({ enableHighlight: false })
|
|
58
|
-
expect(getPageAgentToolConfig()).toEqual(next)
|
|
59
|
-
})
|
|
60
|
-
})
|
|
61
|
-
|
|
62
|
-
describe('setPageAgentToolConfig - a11yConfig 子字段', () => {
|
|
63
|
-
it('未初始化时 a11yConfig 与默认无障碍配置等价', () => {
|
|
64
|
-
const config = getPageAgentToolConfig().a11yConfig
|
|
65
|
-
expect(config.roles).toEqual([])
|
|
66
|
-
expect(config.whitelist).toEqual([])
|
|
67
|
-
expect(config.blacklist).toEqual([])
|
|
68
|
-
expect(config.exposedAttributes).toEqual([])
|
|
69
|
-
expect(config.dialogSelectors).toEqual(DEFAULT_PAGE_AGENT_TOOL_CONFIG.a11yConfig.dialogSelectors)
|
|
70
|
-
})
|
|
71
|
-
|
|
72
|
-
it('merge 模式下多次调用累加合并(拼接而非覆盖)', () => {
|
|
73
|
-
setPageAgentToolConfig({ a11yConfig: { roles: [{ role: 'tab', selector: '.tab-1' }] } })
|
|
74
|
-
setPageAgentToolConfig({ a11yConfig: { roles: [{ role: 'tabpanel', selector: '.panel-1' }] } })
|
|
75
|
-
const current = getPageAgentToolConfig().a11yConfig
|
|
76
|
-
expect(current.roles).toEqual([
|
|
77
|
-
{ role: 'tab', selector: '.tab-1' },
|
|
78
|
-
{ role: 'tabpanel', selector: '.panel-1' }
|
|
79
|
-
])
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
it('函数式 patch 可用于过滤/移除旧规则(真正移除,不会因再次合并而复活)', () => {
|
|
83
|
-
setPageAgentToolConfig({
|
|
84
|
-
a11yConfig: {
|
|
85
|
-
roles: [
|
|
86
|
-
{ role: 'tab', selector: '.tab-1' },
|
|
87
|
-
{ role: 'keep-me', selector: '.k' }
|
|
88
|
-
]
|
|
89
|
-
}
|
|
90
|
-
})
|
|
91
|
-
setPageAgentToolConfig((current) => ({
|
|
92
|
-
a11yConfig: { roles: current.a11yConfig.roles.filter((r) => r.role !== 'tab') }
|
|
93
|
-
}))
|
|
94
|
-
const result = getPageAgentToolConfig().a11yConfig
|
|
95
|
-
expect(result.roles.some((r) => r.role === 'tab')).toBe(false)
|
|
96
|
-
expect(result.roles.some((r) => r.role === 'keep-me')).toBe(true)
|
|
97
|
-
})
|
|
98
|
-
|
|
99
|
-
it('replace 模式丢弃之前的运行期状态,只与默认值重新合并', () => {
|
|
100
|
-
setPageAgentToolConfig({ a11yConfig: { roles: [{ role: 'tab', selector: '.old' }] } })
|
|
101
|
-
setPageAgentToolConfig({ a11yConfig: { roles: [{ role: 'v2', selector: '.new' }] } }, { mode: 'replace' })
|
|
102
|
-
const result = getPageAgentToolConfig().a11yConfig
|
|
103
|
-
expect(result.roles).toEqual([{ role: 'v2', selector: '.new' }])
|
|
104
|
-
})
|
|
105
|
-
|
|
106
|
-
it('a11yConfig 与 enableHighlight 可在同一次 patch 中一起设置,互不影响', () => {
|
|
107
|
-
setPageAgentToolConfig({ enableHighlight: false, a11yConfig: { dialogSelectors: ['.my-modal'] } })
|
|
108
|
-
const current = getPageAgentToolConfig()
|
|
109
|
-
expect(current.enableHighlight).toBe(false)
|
|
110
|
-
expect(current.a11yConfig.dialogSelectors).toContain('.my-modal')
|
|
111
|
-
})
|
|
112
|
-
})
|
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
|
|
2
|
-
import { detectPageDialog, detectValidationErrors, deepQuerySelectorAll } from '../../../page-tools/utils/dom'
|
|
3
|
-
import { setPageAgentToolConfig } from '../../../page-tools/tool-config'
|
|
4
|
-
|
|
5
|
-
function mockRect(el: Element, rect: Partial<DOMRect>) {
|
|
6
|
-
const full: DOMRect = {
|
|
7
|
-
x: 0,
|
|
8
|
-
y: 0,
|
|
9
|
-
width: 0,
|
|
10
|
-
height: 0,
|
|
11
|
-
top: 0,
|
|
12
|
-
left: 0,
|
|
13
|
-
right: 0,
|
|
14
|
-
bottom: 0,
|
|
15
|
-
toJSON: () => ({}),
|
|
16
|
-
...rect,
|
|
17
|
-
}
|
|
18
|
-
Object.defineProperty(el, 'getBoundingClientRect', {
|
|
19
|
-
configurable: true,
|
|
20
|
-
value: () => full,
|
|
21
|
-
})
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function resetWindowGlobals() {
|
|
25
|
-
delete window.__webmcpcli_toolConfig
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
beforeEach(() => {
|
|
29
|
-
resetWindowGlobals()
|
|
30
|
-
document.body.innerHTML = ''
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
afterEach(() => {
|
|
34
|
-
resetWindowGlobals()
|
|
35
|
-
document.body.innerHTML = ''
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
describe('detectValidationErrors', () => {
|
|
39
|
-
it('通过默认 states.error 选择器(ARIA 标准 + 主流框架)检出校验错误', () => {
|
|
40
|
-
const err = document.createElement('div')
|
|
41
|
-
err.className = 'el-form-item__error'
|
|
42
|
-
err.textContent = '用户名不能为空'
|
|
43
|
-
document.body.appendChild(err)
|
|
44
|
-
mockRect(err, { width: 100, height: 20 })
|
|
45
|
-
|
|
46
|
-
const result = detectValidationErrors()
|
|
47
|
-
expect(result).toContain('用户名不能为空')
|
|
48
|
-
expect(result).toContain('[校验提示]')
|
|
49
|
-
})
|
|
50
|
-
|
|
51
|
-
it('通过 setPageAgentToolConfig 追加的自定义 states.error 选择器检出', () => {
|
|
52
|
-
setPageAgentToolConfig({ a11yConfig: { states: { error: { selector: '.my-custom-error' } } } })
|
|
53
|
-
const err = document.createElement('div')
|
|
54
|
-
err.className = 'my-custom-error'
|
|
55
|
-
err.textContent = '自定义错误文案'
|
|
56
|
-
document.body.appendChild(err)
|
|
57
|
-
mockRect(err, { width: 100, height: 20 })
|
|
58
|
-
|
|
59
|
-
expect(detectValidationErrors()).toContain('自定义错误文案')
|
|
60
|
-
})
|
|
61
|
-
|
|
62
|
-
it('无校验错误时返回空字符串', () => {
|
|
63
|
-
expect(detectValidationErrors()).toBe('')
|
|
64
|
-
})
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
describe('detectPageDialog', () => {
|
|
68
|
-
it('通过默认 dialogSelectors(role=dialog)检出真正阻塞交互的模态弹窗', () => {
|
|
69
|
-
const dialog = document.createElement('div')
|
|
70
|
-
dialog.setAttribute('role', 'dialog')
|
|
71
|
-
dialog.textContent = '确认删除该记录吗?此操作不可撤销。'
|
|
72
|
-
dialog.style.position = 'fixed'
|
|
73
|
-
dialog.style.zIndex = '1000'
|
|
74
|
-
document.body.appendChild(dialog)
|
|
75
|
-
mockRect(dialog, { width: 400, height: 200, left: 300, right: 700, top: 250, bottom: 450 })
|
|
76
|
-
|
|
77
|
-
const result = detectPageDialog()
|
|
78
|
-
expect(result).toContain('[页面弹窗检测]')
|
|
79
|
-
expect(result).toContain('确认删除该记录吗')
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
it('通过 setPageAgentToolConfig 追加的自定义 dialogSelectors 检出', () => {
|
|
83
|
-
setPageAgentToolConfig({ a11yConfig: { dialogSelectors: ['.my-modal'] } })
|
|
84
|
-
const dialog = document.createElement('div')
|
|
85
|
-
dialog.className = 'my-modal'
|
|
86
|
-
dialog.textContent = '自定义配置检测到的模态弹窗内容'
|
|
87
|
-
dialog.style.position = 'fixed'
|
|
88
|
-
dialog.style.zIndex = '1000'
|
|
89
|
-
document.body.appendChild(dialog)
|
|
90
|
-
mockRect(dialog, { width: 400, height: 200, left: 300, right: 700, top: 250, bottom: 450 })
|
|
91
|
-
|
|
92
|
-
expect(detectPageDialog()).toContain('自定义配置检测到的模态弹窗内容')
|
|
93
|
-
})
|
|
94
|
-
|
|
95
|
-
it('非模态(非 fixed/absolute 或 z-index 过低)的弹窗不会被误报', () => {
|
|
96
|
-
const notModal = document.createElement('div')
|
|
97
|
-
notModal.setAttribute('role', 'dialog')
|
|
98
|
-
notModal.textContent = '这是一个静态定位的普通提示框内容'
|
|
99
|
-
document.body.appendChild(notModal)
|
|
100
|
-
mockRect(notModal, { width: 400, height: 200, left: 300, right: 700, top: 250, bottom: 450 })
|
|
101
|
-
|
|
102
|
-
expect(detectPageDialog()).toBe('')
|
|
103
|
-
})
|
|
104
|
-
|
|
105
|
-
it('无弹窗时返回空字符串', () => {
|
|
106
|
-
expect(detectPageDialog()).toBe('')
|
|
107
|
-
})
|
|
108
|
-
})
|
|
109
|
-
|
|
110
|
-
describe('deepQuerySelectorAll', () => {
|
|
111
|
-
it('可以穿透 Shadow DOM 查询元素', () => {
|
|
112
|
-
const host = document.createElement('div')
|
|
113
|
-
document.body.appendChild(host)
|
|
114
|
-
const shadow = host.attachShadow({ mode: 'open' })
|
|
115
|
-
const btn = document.createElement('button')
|
|
116
|
-
btn.textContent = 'Shadow 内部按钮'
|
|
117
|
-
shadow.appendChild(btn)
|
|
118
|
-
|
|
119
|
-
const found = deepQuerySelectorAll('button', document.body)
|
|
120
|
-
expect(found).toContain(btn)
|
|
121
|
-
})
|
|
122
|
-
})
|
package/tsconfig.json
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"declaration": true,
|
|
4
|
-
"outDir": "dist",
|
|
5
|
-
"target": "ES2015",
|
|
6
|
-
"lib": ["ES2015", "DOM"],
|
|
7
|
-
"moduleResolution": "node",
|
|
8
|
-
"module": "esnext",
|
|
9
|
-
"strict": true,
|
|
10
|
-
"esModuleInterop": true,
|
|
11
|
-
"skipLibCheck": true,
|
|
12
|
-
"types": ["vite/client"]
|
|
13
|
-
},
|
|
14
|
-
"include": ["**/*.ts"],
|
|
15
|
-
"exclude": ["node_modules", "dist", "**/*.test.ts", "**/*.spec.ts"]
|
|
16
|
-
}
|