OpenClaw 插件的测试工具、模式和 lint 约束参考。Documentation Index
Fetch the complete documentation index at: https://openclaw.zhcndoc.com/llms.txt
Use this file to discover all available pages before exploring further.
测试工具
插件 API mock 导入:openclaw/plugin-sdk/plugin-test-api
Agent 运行时契约导入: openclaw/plugin-sdk/agent-runtime-test-contracts
Channel 契约导入: openclaw/plugin-sdk/channel-contract-testing
Channel 测试辅助导入: openclaw/plugin-sdk/channel-test-helpers
Channel 目标测试导入: openclaw/plugin-sdk/channel-target-testing
插件契约导入: openclaw/plugin-sdk/plugin-test-contracts
插件运行时测试导入: openclaw/plugin-sdk/plugin-test-runtime
Provider 契约导入: openclaw/plugin-sdk/provider-test-contracts
Provider HTTP mock 导入: openclaw/plugin-sdk/provider-http-test-mocks
环境/网络测试导入: openclaw/plugin-sdk/test-env
通用 fixture 导入: openclaw/plugin-sdk/test-fixtures
Node 内置 mock 导入: openclaw/plugin-sdk/test-node-mocks
新插件测试优先使用下面这些更聚焦的子路径。广义的
openclaw/plugin-sdk/testing 入口仅用于旧版兼容。
仓库守卫会拒绝来自 plugin-sdk/testing 和
plugin-sdk/test-utils 的新的真实导入;这些名称仅作为已弃用的兼容
表面保留给外部插件和兼容性记录测试。
可用导出
| 导出 | 用途 |
|---|---|
createTestPluginApi | 为直接注册的单元测试构建一个最小插件 API mock。导入自 plugin-sdk/plugin-test-api |
AUTH_PROFILE_RUNTIME_CONTRACT | 供原生 agent 运行时适配器使用的共享 auth-profile 契约 fixture。导入自 plugin-sdk/agent-runtime-test-contracts |
DELIVERY_NO_REPLY_RUNTIME_CONTRACT | 供原生 agent 运行时适配器使用的共享“抑制投递回复”契约 fixture。导入自 plugin-sdk/agent-runtime-test-contracts |
OUTCOME_FALLBACK_RUNTIME_CONTRACT | 供原生 agent 运行时适配器使用的共享回退分类契约 fixture。导入自 plugin-sdk/agent-runtime-test-contracts |
createParameterFreeTool | 为原生运行时契约测试构建动态工具 schema fixture。导入自 plugin-sdk/agent-runtime-test-contracts |
expectChannelInboundContextContract | 断言 channel 入站上下文形状。导入自 plugin-sdk/channel-contract-testing |
installChannelOutboundPayloadContractSuite | 安装 channel 出站 payload 契约用例。导入自 plugin-sdk/channel-contract-testing |
createStartAccountContext | 构建 channel 账户生命周期上下文。导入自 plugin-sdk/channel-test-helpers |
installChannelActionsContractSuite | 安装通用的 channel 消息动作契约用例。导入自 plugin-sdk/channel-test-helpers |
installChannelSetupContractSuite | 安装通用的 channel 设置契约用例。导入自 plugin-sdk/channel-test-helpers |
installChannelStatusContractSuite | 安装通用的 channel 状态契约用例。导入自 plugin-sdk/channel-test-helpers |
expectDirectoryIds | 断言来自目录列表函数的 channel 目录 id。导入自 plugin-sdk/channel-test-helpers |
assertBundledChannelEntries | 断言打包后的 channel 入口暴露出预期的公开契约。导入自 plugin-sdk/channel-test-helpers |
formatEnvelopeTimestamp | 格式化确定性的封装时间戳。导入自 plugin-sdk/channel-test-helpers |
expectPairingReplyText | 断言 channel 配对回复文本并提取其代码。导入自 plugin-sdk/channel-test-helpers |
describePluginRegistrationContract | 安装插件注册契约检查。导入自 plugin-sdk/plugin-test-contracts |
registerSingleProviderPlugin | 在加载器 smoke 测试中注册一个 provider 插件。导入自 plugin-sdk/plugin-test-runtime |
registerProviderPlugin | 从一个插件中捕获所有 provider 类型。导入自 plugin-sdk/plugin-test-runtime |
registerProviderPlugins | 跨多个插件捕获 provider 注册。导入自 plugin-sdk/plugin-test-runtime |
requireRegisteredProvider | 断言 provider 集合包含某个 id。导入自 plugin-sdk/plugin-test-runtime |
createRuntimeEnv | 构建一个 mock 的 CLI/plugin 运行时环境。导入自 plugin-sdk/plugin-test-runtime |
createPluginSetupWizardStatus | 为 channel 插件构建设置状态辅助工具。导入自 plugin-sdk/plugin-test-runtime |
describeOpenAIProviderRuntimeContract | 安装 provider 家族运行时契约检查。导入自 plugin-sdk/provider-test-contracts |
expectPassthroughReplayPolicy | 断言 provider 回放策略会透传 provider 拥有的工具和元数据。导入自 plugin-sdk/provider-test-contracts |
runRealtimeSttLiveTest | 使用共享音频 fixture 运行一个实时语音转文字(STT)provider 测试。导入自 plugin-sdk/provider-test-contracts |
normalizeTranscriptForMatch | 在进行模糊断言前规范化实时转写输出。导入自 plugin-sdk/provider-test-contracts |
expectExplicitVideoGenerationCapabilities | 断言视频 provider 声明了明确的生成模式能力。导入自 plugin-sdk/provider-test-contracts |
expectExplicitMusicGenerationCapabilities | 断言音乐 provider 声明了明确的生成/编辑能力。导入自 plugin-sdk/provider-test-contracts |
mockSuccessfulDashscopeVideoTask | 安装一个成功的 DashScope 兼容视频任务响应。导入自 plugin-sdk/provider-test-contracts |
getProviderHttpMocks | 访问可选启用的 provider HTTP/auth Vitest mocks。导入自 plugin-sdk/provider-http-test-mocks |
installProviderHttpMockCleanup | 在每个测试后重置 provider HTTP/auth mocks。导入自 plugin-sdk/provider-http-test-mocks |
installCommonResolveTargetErrorCases | 用于目标解析错误处理的共享测试用例。导入自 plugin-sdk/channel-target-testing |
shouldAckReaction | 检查 channel 是否应添加 ack reaction。导入自 plugin-sdk/channel-feedback |
removeAckReactionAfterReply | 在回复投递后移除 ack reaction。导入自 plugin-sdk/channel-feedback |
createTestRegistry | 构建 channel 插件 registry fixture。导入自 plugin-sdk/plugin-test-runtime 或 plugin-sdk/channel-test-helpers |
createEmptyPluginRegistry | 构建一个空的插件 registry fixture。导入自 plugin-sdk/plugin-test-runtime 或 plugin-sdk/channel-test-helpers |
setActivePluginRegistry | 为插件运行时测试安装一个 registry fixture。导入自 plugin-sdk/plugin-test-runtime 或 plugin-sdk/channel-test-helpers |
createRequestCaptureJsonFetch | 在媒体辅助测试中捕获 JSON fetch 请求。导入自 plugin-sdk/test-env |
withServer | 针对一个可销毁的本地 HTTP 服务器运行测试。导入自 plugin-sdk/test-env |
createMockIncomingRequest | 构建一个最小的传入 HTTP 请求对象。导入自 plugin-sdk/test-env |
withFetchPreconnect | 在安装了 preconnect 钩子的情况下运行 fetch 测试。导入自 plugin-sdk/test-env |
withEnv / withEnvAsync | 临时修改环境变量。导入自 plugin-sdk/test-env |
createTempHomeEnv / withTempHome / withTempDir | 创建隔离的文件系统测试 fixture。导入自 plugin-sdk/test-env |
createMockServerResponse | 创建一个最小的 HTTP server response mock。导入自 plugin-sdk/test-env |
createCliRuntimeCapture | 在测试中捕获 CLI 运行时输出。导入自 plugin-sdk/test-fixtures |
importFreshModule | 使用新的查询 token 导入一个 ESM 模块,以绕过模块缓存。导入自 plugin-sdk/test-fixtures |
bundledPluginRoot / bundledPluginFile | 解析内置插件源码或 dist fixture 路径。导入自 plugin-sdk/test-fixtures |
mockNodeBuiltinModule | 安装窄范围的 Node 内置 Vitest mocks。导入自 plugin-sdk/test-node-mocks |
createSandboxTestContext | 构建沙箱测试上下文。导入自 plugin-sdk/test-fixtures |
writeSkill | 写入 skill fixture。导入自 plugin-sdk/test-fixtures |
makeAgentAssistantMessage | 构建 agent 转写消息 fixture。导入自 plugin-sdk/test-fixtures |
peekSystemEvents / resetSystemEventsForTest | 检查并重置系统事件 fixture。导入自 plugin-sdk/test-fixtures |
sanitizeTerminalText | 为断言净化终端输出。导入自 plugin-sdk/test-fixtures |
countLines / hasBalancedFences | 断言分块输出形状。导入自 plugin-sdk/test-fixtures |
runProviderCatalog | 使用测试依赖执行 provider catalog hook |
resolveProviderWizardOptions | 在契约测试中解析 provider 设置向导选项 |
resolveProviderModelPickerEntries | 在契约测试中解析 provider 模型选择器条目 |
buildProviderPluginMethodChoice | 为断言构建 provider 向导 choice id |
setProviderWizardProvidersResolverForTest | 为隔离测试注入 provider 向导 providers |
createProviderUsageFetch | 构建 provider 使用情况 fetch fixture |
useFrozenTime / useRealTime | 为对时间敏感的测试冻结和恢复计时器。导入自 plugin-sdk/test-env |
createTestWizardPrompter | 构建一个 mock 的设置向导提示器 |
createRuntimeTaskFlow | 创建隔离的运行时任务流状态 |
typedCases | 为表驱动测试保留字面量类型。导入自 plugin-sdk/test-fixtures |
src/plugins/contracts 下。新的扩展测试请保持在已文档化的、聚焦的 SDK 子路径上,例如
plugin-sdk/plugin-test-api、plugin-sdk/channel-contract-testing、
plugin-sdk/agent-runtime-test-contracts、plugin-sdk/channel-test-helpers、
plugin-sdk/plugin-test-contracts、plugin-sdk/plugin-test-runtime、
plugin-sdk/provider-test-contracts、plugin-sdk/provider-http-test-mocks、
plugin-sdk/test-env 或 plugin-sdk/test-fixtures,而不要直接导入广义的
plugin-sdk/testing 兼容入口、仓库 src/** 文件或仓库 test/helpers/* 桥接。
类型
聚焦的测试子路径也会重新导出在测试文件中很有用的类型:测试目标解析
使用installCommonResolveTargetErrorCases 为频道目标解析添加标准错误案例:
测试模式
测试注册契约
将手写的api mock 传给 register(api) 的单元测试并不会覆盖 OpenClaw 加载器的接受门控。请为插件依赖的每个注册入口至少添加一个由 loader 驱动的冒烟测试,尤其是 hooks 和诸如 memory 这类独占能力。
真实加载器会在缺少必需元数据,或者插件调用了它不拥有的能力 API 时失败。例如,api.registerHook(...) 需要提供 hook 名称,而 api.registerMemoryCapability(...) 需要插件清单或导出的入口声明 kind: "memory"。
测试运行时配置访问
在测试打包后的频道插件时,优先使用来自openclaw/plugin-sdk/channel-test-helpers 的共享插件运行时 mock。其已弃用的 runtime.config.loadConfig() 和 runtime.config.writeConfigFile(...) mock 默认会抛出错误,因此测试可以捕获对兼容性 API 的新用法。只有当测试明确覆盖旧版兼容行为时,才应覆盖这些 mock。
频道插件的单元测试
提供者插件的单元测试
模拟插件运行时
对于使用createPluginRuntimeStore 的代码,请在测试中 mock 运行时:
使用按实例 stub 进行测试
优先使用按实例 stub,而不是修改原型:契约测试(仓库内插件)
打包内置插件具有契约测试,用于验证注册所有权:- 哪些插件注册了哪些 providers
- 哪些插件注册了哪些 speech providers
- 注册形状是否正确
- 运行时契约是否符合要求
运行指定范围的测试
对于特定插件:Lint 强制规则(仓库内插件)
pnpm check 会对仓库内插件强制执行三条规则:
- 禁止单体根导入 — 拒绝使用
openclaw/plugin-sdk根入口导出 - 禁止直接导入
src/— 插件不能直接导入../../src/ - 禁止自我导入 — 插件不能导入它自己的
plugin-sdk/<name>子路径