dsh-plugin-testkit 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 +81 -13
- package/README.zh-CN.md +91 -0
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -1,23 +1,91 @@
|
|
|
1
1
|
# dsh-plugin-testkit
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
English | [简体中文](./README.zh-CN.md)
|
|
4
|
+
|
|
5
|
+
Unit-test the **client half** of a DSH plugin without booting DSH: evaluate the plugin's `client.js` in a `node:vm` sandbox with a fake `window.__ModuleLoader__` and a React stub, then assert on the slots it registered.
|
|
6
|
+
|
|
7
|
+
This is a **pure library** — no `bin`, no CLI. It covers client-side *logic* (does `apply` run, which slots get registered, what does it log). It does **not** replace end-to-end tests.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm i -D dsh-plugin-testkit
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
- Node >= 22.15.0.
|
|
16
|
+
- No runtime dependencies.
|
|
17
|
+
|
|
18
|
+
## Quick start
|
|
4
19
|
|
|
5
20
|
```js
|
|
6
21
|
import { loadClientModule, applyAndListSlots, hasSlot } from 'dsh-plugin-testkit'
|
|
7
22
|
import { readFileSync } from 'node:fs'
|
|
8
23
|
|
|
9
24
|
const mod = loadClientModule(readFileSync('client.js', 'utf8'), { id: 'my-plugin' })
|
|
10
|
-
|
|
25
|
+
|
|
26
|
+
const slots = applyAndListSlots(mod)
|
|
27
|
+
// [{ name: 'conversation.composer.dock', id: 'my-plugin', order: 5 }]
|
|
28
|
+
|
|
29
|
+
if (!hasSlot(mod, 'conversation.composer.dock')) throw new Error('dock slot missing')
|
|
11
30
|
```
|
|
12
31
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
32
|
+
## API
|
|
33
|
+
|
|
34
|
+
All 3 exports.
|
|
35
|
+
|
|
36
|
+
| Export | Signature | Returns | Behavior |
|
|
37
|
+
|---|---|---|---|
|
|
38
|
+
| `loadClientModule` | `loadClientModule(source, { id = 'plugin', extraRequire = {} } = {})` | sandbox handle | Runs `source` in a fresh `node:vm` context and returns the captured module plus helpers. |
|
|
39
|
+
| `applyAndListSlots` | `applyAndListSlots(clientModule)` | `Array<{ name, id, order }>` | Runs one idempotent `apply()` and maps the registrations to plain descriptors. |
|
|
40
|
+
| `hasSlot` | `hasSlot(clientModule, name, slotId?)` | `boolean` | Whether the **latest** `apply()` registered `name`; when `slotId` is given, its `id` must match too. |
|
|
41
|
+
|
|
42
|
+
### The handle returned by `loadClientModule`
|
|
43
|
+
|
|
44
|
+
| Field | Type | Meaning |
|
|
45
|
+
|---|---|---|
|
|
46
|
+
| `id` | `string` | The `id` you passed in. Used for the object and the vm filename only — it does **not** rewrite any slot or bundle id inside the source. |
|
|
47
|
+
| `module` | `object` | What the plugin's factory returned (must be an object with `apply(ctx)`). |
|
|
48
|
+
| `registrations` | `Array<{ def, comp }>` | Live list of `slots.register(def, comp)` calls. |
|
|
49
|
+
| `ctx` | `object` | The fake plugin context: `{ slots, logger, config: {}, on() {}, emit() {} }`. |
|
|
50
|
+
| `logs` | `Array` | Captured console calls as `['log' \| 'info' \| 'warn' \| 'error' \| 'debug', ...args]`. |
|
|
51
|
+
| `apply()` | `() => Array<{def, comp}>` | **Idempotent**: clears `registrations` first, then calls `module.apply(ctx)`. Repeated calls never accumulate duplicates. |
|
|
52
|
+
|
|
53
|
+
### `extraRequire`
|
|
54
|
+
|
|
55
|
+
`require(name)` resolves in this order: `extraRequire[name]` → the built-in `react` stub → throw.
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
const mod = loadClientModule(src, {
|
|
59
|
+
id: 'my-plugin',
|
|
60
|
+
extraRequire: { '@dsh/ui': { Button: () => null } },
|
|
61
|
+
})
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
An unresolved module throws `dsh-plugin-testkit: 未提供 require("<name>");用 extraRequire 传入`.
|
|
65
|
+
|
|
66
|
+
### `loadClientModule` failures
|
|
67
|
+
|
|
68
|
+
It throws, rather than returning a half-built handle, when the source is not shaped like a DSH client module. All four are verified:
|
|
69
|
+
|
|
70
|
+
| Condition | Error |
|
|
71
|
+
|---|---|
|
|
72
|
+
| Never called `window.__ModuleLoader__.load(...)` | `dsh-plugin-testkit: 客户端未调用 window.__ModuleLoader__.load(...)` |
|
|
73
|
+
| The factory returned a non-object (e.g. `42`, `null`) | `dsh-plugin-testkit: factory 未返回模块对象` |
|
|
74
|
+
| The returned object has no `apply` function | `dsh-plugin-testkit: 模块对象缺少 apply(ctx) 方法` |
|
|
75
|
+
| `slots.register` called without a definition object | `dsh-plugin-testkit: slots.register 需要定义对象,收到 undefined` |
|
|
76
|
+
|
|
77
|
+
The object check is `typeof captured === 'object'`, so an array passes it and then fails on the missing `apply`.
|
|
78
|
+
|
|
79
|
+
## Notes
|
|
80
|
+
|
|
81
|
+
- **Not a security boundary.** `node:vm` is a logic sandbox that can be escaped — run only **trusted test sources** in it, never third-party or user-supplied plugin code.
|
|
82
|
+
- **`window === globalThis`** (matching browser semantics), so code that writes `window.foo` or bare `foo` hits the same object.
|
|
83
|
+
- **`console` and timers are isolated stubs.** Logs land in `mod.logs` instead of the terminal; `setTimeout` / `setInterval` return `0` and never fire, so nothing can keep the host process alive.
|
|
84
|
+
- **`register` and `inject` both return disposers**, matching the real DSH contract. `register`'s disposer removes the record from `registrations`; `inject`'s is a no-op `() => {}` because `inject(name, fn)` simply invokes `fn()` when it is a function.
|
|
85
|
+
- **React hooks are simplified stubs.** `useState` / `useReducer` accept functional updates but never trigger a re-render; `useEffect` and `useLayoutEffect` are no-ops; `useMemo` runs the factory on every call; `useCallback` returns the function as-is; `memo` is identity. Components are never rendered — `createElement` just builds `{ type, props, children }`.
|
|
86
|
+
- **This tests the client half only.** Anything that depends on real rendering, effects, layout or DSH host wiring still needs an end-to-end test (`dsh-plugin-e2e`).
|
|
87
|
+
- The `document` stub is minimal (`createElement`, `addEventListener`, `querySelector` returning `null`); DOM-heavy code will need real browser coverage.
|
|
88
|
+
|
|
89
|
+
## License
|
|
90
|
+
|
|
91
|
+
MIT
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# dsh-plugin-testkit
|
|
2
|
+
|
|
3
|
+
[English](./README.md) | 简体中文
|
|
4
|
+
|
|
5
|
+
不开 DSH 也能单测插件的**客户端半侧**:把插件的 `client.js` 放进 `node:vm` 沙箱,配一个假的 `window.__ModuleLoader__` 和 React 桩执行,然后对它注册的 slot 做断言。
|
|
6
|
+
|
|
7
|
+
这是**纯库**——没有 `bin`,没有 CLI。它覆盖的是客户端**逻辑**(`apply` 跑没跑、注册了哪些 slot、打了什么日志),**不替代**端到端测试。
|
|
8
|
+
|
|
9
|
+
## 安装
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm i -D dsh-plugin-testkit
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
- Node >= 22.15.0。
|
|
16
|
+
- 无运行时依赖。
|
|
17
|
+
|
|
18
|
+
## 快速开始
|
|
19
|
+
|
|
20
|
+
```js
|
|
21
|
+
import { loadClientModule, applyAndListSlots, hasSlot } from 'dsh-plugin-testkit'
|
|
22
|
+
import { readFileSync } from 'node:fs'
|
|
23
|
+
|
|
24
|
+
const mod = loadClientModule(readFileSync('client.js', 'utf8'), { id: 'my-plugin' })
|
|
25
|
+
|
|
26
|
+
const slots = applyAndListSlots(mod)
|
|
27
|
+
// [{ name: 'conversation.composer.dock', id: 'my-plugin', order: 5 }]
|
|
28
|
+
|
|
29
|
+
if (!hasSlot(mod, 'conversation.composer.dock')) throw new Error('dock slot 没注册')
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## API
|
|
33
|
+
|
|
34
|
+
共 3 个导出。
|
|
35
|
+
|
|
36
|
+
| 导出 | 签名 | 返回 | 行为 |
|
|
37
|
+
|---|---|---|---|
|
|
38
|
+
| `loadClientModule` | `loadClientModule(source, { id = 'plugin', extraRequire = {} } = {})` | 沙箱句柄 | 在全新的 `node:vm` 上下文里执行 `source`,返回抓到的模块与辅助方法。 |
|
|
39
|
+
| `applyAndListSlots` | `applyAndListSlots(clientModule)` | `Array<{ name, id, order }>` | 跑一次幂等的 `apply()`,把注册记录映射成纯描述对象。 |
|
|
40
|
+
| `hasSlot` | `hasSlot(clientModule, name, slotId?)` | `boolean` | **最近一次** `apply()` 是否注册了 `name`;给了 `slotId` 还要求 `id` 也匹配。 |
|
|
41
|
+
|
|
42
|
+
### `loadClientModule` 返回的句柄
|
|
43
|
+
|
|
44
|
+
| 字段 | 类型 | 含义 |
|
|
45
|
+
|---|---|---|
|
|
46
|
+
| `id` | `string` | 你传进来的 `id`。只用于对象标识与 vm 文件名——**不会**改写源码里的任何 slot / bundle id。 |
|
|
47
|
+
| `module` | `object` | 插件 factory 返回的模块对象(必须是带 `apply(ctx)` 的对象)。 |
|
|
48
|
+
| `registrations` | `Array<{ def, comp }>` | `slots.register(def, comp)` 调用的实时列表。 |
|
|
49
|
+
| `ctx` | `object` | 假的插件上下文:`{ slots, logger, config: {}, on() {}, emit() {} }`。 |
|
|
50
|
+
| `logs` | `Array` | 捕获的 console 调用,形如 `['log' \| 'info' \| 'warn' \| 'error' \| 'debug', ...args]`。 |
|
|
51
|
+
| `apply()` | `() => Array<{def, comp}>` | **幂等**:先清空 `registrations`,再调 `module.apply(ctx)`。重复调用不会累加出重复注册。 |
|
|
52
|
+
|
|
53
|
+
### `extraRequire`
|
|
54
|
+
|
|
55
|
+
`require(name)` 的解析顺序:`extraRequire[name]` → 内置 `react` 桩 → 抛错。
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
const mod = loadClientModule(src, {
|
|
59
|
+
id: 'my-plugin',
|
|
60
|
+
extraRequire: { '@dsh/ui': { Button: () => null } },
|
|
61
|
+
})
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
模块没提供会抛 `dsh-plugin-testkit: 未提供 require("<name>");用 extraRequire 传入`。
|
|
65
|
+
|
|
66
|
+
### `loadClientModule` 的失败情形
|
|
67
|
+
|
|
68
|
+
源码不像 DSH 客户端模块时它**直接抛错**,不会返回半成品句柄。四种情形均已实测:
|
|
69
|
+
|
|
70
|
+
| 情形 | 报错 |
|
|
71
|
+
|---|---|
|
|
72
|
+
| 始终没调 `window.__ModuleLoader__.load(...)` | `dsh-plugin-testkit: 客户端未调用 window.__ModuleLoader__.load(...)` |
|
|
73
|
+
| factory 返回的不是对象(如 `42`、`null`) | `dsh-plugin-testkit: factory 未返回模块对象` |
|
|
74
|
+
| 返回的对象没有 `apply` 函数 | `dsh-plugin-testkit: 模块对象缺少 apply(ctx) 方法` |
|
|
75
|
+
| `slots.register` 调用时没传定义对象 | `dsh-plugin-testkit: slots.register 需要定义对象,收到 undefined` |
|
|
76
|
+
|
|
77
|
+
对象判断用的是 `typeof captured === 'object'`,所以数组能过这一关,随后卡在缺少 `apply` 上。
|
|
78
|
+
|
|
79
|
+
## 注意事项
|
|
80
|
+
|
|
81
|
+
- **不是安全边界。** `node:vm` 只是逻辑沙箱,可以被逃逸——只能拿它跑**可信的测试源码**,绝不要用它执行第三方或用户提供的插件代码。
|
|
82
|
+
- **`window === globalThis`**(同浏览器语义),所以写 `window.foo` 和写裸 `foo` 落到同一个对象。
|
|
83
|
+
- **`console` 与定时器是隔离桩。** 日志进 `mod.logs` 而不打到终端;`setTimeout` / `setInterval` 返回 `0` 且永不触发,不会挂住宿主进程。
|
|
84
|
+
- **`register` 与 `inject` 都返回 disposer**,与真实 DSH 契约一致。`register` 的 disposer 把记录从 `registrations` 里摘掉;`inject` 的是个空操作 `() => {}`,因为 `inject(name, fn)` 只是在 `fn` 是函数时直接调 `fn()`。
|
|
85
|
+
- **React hook 是简化桩。** `useState` / `useReducer` 支持函数式更新但不触发重渲染;`useEffect` 与 `useLayoutEffect` 是空操作;`useMemo` 每次调用都跑工厂;`useCallback` 原样返回函数;`memo` 是恒等。组件从不真正渲染——`createElement` 只拼出 `{ type, props, children }`。
|
|
86
|
+
- **它只测客户端半侧。** 依赖真实渲染、副作用、布局或 DSH 宿主接线的部分,仍需端到端测试(`dsh-plugin-e2e`)。
|
|
87
|
+
- `document` 桩极其精简(`createElement`、`addEventListener`、`querySelector` 返回 `null`);重度操作 DOM 的代码需要真实浏览器覆盖。
|
|
88
|
+
|
|
89
|
+
## 许可证
|
|
90
|
+
|
|
91
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-plugin-testkit",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
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
|
"repository": {
|
|
16
17
|
"type": "git",
|