dsh-lean-router 1.0.8 → 1.1.0

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.
@@ -1,6 +1,19 @@
1
1
  # Changelog
2
2
 
3
- > English version of [changelog.md](./changelog.md). Versions follow package.json (currently 1.0.8).
3
+ > English version of [changelog.md](./changelog.md). Versions follow package.json (currently 1.1.0).
4
+
5
+ ## 1.1.0 (2026-11-25)
6
+
7
+ - Changed (surface, decided by the user): the skills panel moves from "header button + body overlay" to a **right sidebar tab**. The overlay had to be opened, covered the conversation and could not sit side by side, so it was never a usable form; the 1.0.9 `#lean-router-panel` overlay is dropped entirely.
8
+ - New: the right-sidebar contract follows the real examples `@modusensus/dsh-mneme` and `dsh-cost-meter` - the tab type and its guide entry go to `sidebarRightTabs.register({ id, kind, title, guide })` (checked with `get(id)` first, because the host throws `tab type id … is already registered`), and the body and title go to `sidebar.right.pane.tab` / `sidebar.right.pane.tab.title` with a `{ name, key }` spec.
9
+ - New: the header "Skills" button stays as a shortcut and opens the tab through `ctx.get("sidebarRight").openTab(kind)`.
10
+ - Fixed: the opener now resolves `sidebarRight` from the scoped ctx delivered by `ctx.inject(["slots"])`, so it cannot hit the "service not registered yet" timing trap again.
11
+ - Constraint: nothing is appended to `document.body` any more; this version is the shell only (a title and two lines), and every style inherits host theme tokens instead of hard-coded colours. 224 tests green.
12
+
13
+ ## 1.0.9 (2026-11-24)
14
+
15
+ - Fixed (real install, rendering): the header button appeared, but clicking it drew only a thin black bar across that row. The panel used to render as a React component inside the header slot, so `position: fixed` was confined to that row by an ancestor's `transform` / `overflow`. It is now a plain DOM overlay appended to `document.body` (`#lean-router-panel`), with only the button left in the slot; the overlay sits at `z-index: 2147483000`, closes on backdrop or ✕ click, and the button toggles it.
16
+ - Tests: new assertion that clicking the button mounts the panel on `body` and clicking again removes it; 222 tests green.
4
17
 
5
18
  ## 1.0.8 (2026-11-24)
6
19
 
package/docs/changelog.md CHANGED
@@ -1,6 +1,19 @@
1
1
  # 变更记录(changelog)
2
2
 
3
- > 中文为主,英文版:[changelog.en.md](./changelog.en.md)。版本号遵循 package.json(当前 1.0.8)。
3
+ > 中文为主,英文版:[changelog.en.md](./changelog.en.md)。版本号遵循 package.json(当前 1.1.0)。
4
+
5
+ ## 1.1.0 (2026-11-25)
6
+
7
+ - 变更(形态,用户拍板):技能面板从「顶栏按钮 + body 浮层」改为**右侧栏标签页**。浮层要点开、会遮挡主对话、不能并排,本身就不是可用形态,1.0.9 的 `#lean-router-panel` 浮层实现整体废弃。
8
+ - 新增:按真机范例 `@modusensus/dsh-mneme` 与 `dsh-cost-meter` 的契约接入右栏 —— 类型与引导挂 `sidebarRightTabs.register({ id, kind, title, guide })`(注册前先 `get(id)` 查重,宿主对重复 id 会抛 `tab type id … is already registered`);正文与标题挂 `sidebar.right.pane.tab` / `sidebar.right.pane.tab.title`,spec 用 `{ name, key }` 关联类型。
9
+ - 新增:顶栏「技能」按钮保留为快捷入口,点击走 `ctx.get("sidebarRight").openTab(kind)` 直接打开右栏标签。
10
+ - 修复:opener 改用 `ctx.inject(["slots"])` 就绪后的 scoped ctx 取 `sidebarRight`,避免又踩「服务还没注册」的时机坑。
11
+ - 约束:不再往 `document.body` 追加任何元素;当前版本只做壳(标题 + 两行说明),样式全部继承宿主主题 token,不写死颜色。224 测试全绿。
12
+
13
+ ## 1.0.9 (2026-11-24)
14
+
15
+ - 修复(真机,渲染):顶栏按钮能出现,但点击后面板只在那一行显示成「一条黑条」。原因是面板原先作为 React 组件渲染在顶栏 slot 内部,`position: fixed` 被祖先元素的 `transform` / `overflow` 限制在那一行里。改为挂到 `document.body` 的纯 DOM 浮层(`#lean-router-panel`),slot 里只留按钮;浮层层级提到 `2147483000`,点遮罩或 ✕ 关闭,再点按钮收起。
16
+ - 测试:新增「点按钮把面板挂到 body、再点一次移除」断言;222 测试全绿。
4
17
 
5
18
  ## 1.0.8 (2026-11-24)
6
19
 
@@ -20,7 +20,7 @@ After restarting DSH, pick the **Lean router** preset (lean tier) or **Lean rout
20
20
  ### Path B: npm install (recommended)
21
21
 
22
22
  ~~~powershell
23
- dsh plugin --profile desktop add npm:dsh-lean-router@1.0.8
23
+ dsh plugin --profile desktop add npm:dsh-lean-router@1.1.0
24
24
  ~~~
25
25
 
26
26
  (Both paths match the output of scripts/install.mjs; the real installer, claim boundaries and `--uninstall` rollback belong to ticket 09.)
@@ -20,7 +20,7 @@ dsh plugin --profile desktop add "link:D:\path\to\dsh-lean-router"
20
20
  ### 路径 B:npm 安装(推荐)
21
21
 
22
22
  ~~~powershell
23
- dsh plugin --profile desktop add npm:dsh-lean-router@1.0.8
23
+ dsh plugin --profile desktop add npm:dsh-lean-router@1.1.0
24
24
  ~~~
25
25
 
26
26
  (两条路径与 scripts/install.mjs 输出的说明一致;真安装器、认领边界与 `--uninstall` 回滚归票 09。)
package/lib/client.js CHANGED
@@ -1,13 +1,17 @@
1
- // dsh-lean-router - 客户端 UI 层(技能面板)
1
+ // dsh-lean-router - 客户端 UI 层(技能面板 · 右侧栏标签页)
2
2
  //
3
- // 1.0.5 修(真机两轮验证后):
4
- // 1. 原实现 require("react-dom") + ReactDOM.createPortal —— 参考实现 dsh-cost-meter 只
5
- // require("react"),从不 require react-dom。宿主若不提供 react-dom,factory 立刻抛错,
6
- // 整个客户端模块加载失败,按钮不出现且无任何可见症状。改为不依赖 react-dom,
7
- // 面板用 position:fixed 直接渲染在按钮旁边。
8
- // 2. 原实现用 ctx.slots.register(...);cost-meter 用的是
9
- // ctx.inject(slotName, () => ctx.register(spec, Component))。改为优先走后者,保留回退。
10
- // 3. apply 不再是 generator:1.0.2 曾因 apply 返回值不符合 cordis effect 契约而整条预设失效。
3
+ // 形态(用户拍板):右侧栏 tab,不做浮层、不往 document.body 追加第二份应用。
4
+ // 契约来源:已安装真机范例 @modusensus/dsh-mneme 的 lib/client.js 与 dsh-cost-meter。
5
+ // 1) 类型与引导挂服务 sidebarRightTabs: tabs.register({ id, kind, title, guide })
6
+ // 2) 正文与标题挂 slot: sidebar.right.pane.tab / sidebar.right.pane.tab.title,
7
+ // spec 用 { name, key: TAB_ID, inject }(注意是 key 不是 id)
8
+ // 3) 打开: ctx.get("sidebarRight").openTab(TAB_KIND)
9
+ //
10
+ // 踩过的坑(别再犯):
11
+ // - 直接读 ctx.register 会抛 cannot get property "register" without inject(1.0.6)
12
+ // - apply 早于 slots 服务就绪,ctx.get("slots") 是 undefined(1.0.7)
13
+ // 所以一律用 ctx.inject(["slots"], cb) 等服务就绪后再注册。
14
+ // - 面板不要渲染在顶栏 slot 内(会被祖先 transform/overflow 裁成黑条,1.0.9)
11
15
  //
12
16
  // 加载契约:package.json 的 dsh.client.platform="web" + exports["./client"],
13
17
  // 入口形如 window.__ModuleLoader__.load({ id, factory }),factory(require) 取宿主模块。
@@ -17,6 +21,10 @@ window.__ModuleLoader__.load({
17
21
  const React = require("react");
18
22
  const h = React.createElement;
19
23
 
24
+ const TAB_ID = "lean-router.skills";
25
+ const TAB_KIND = "lean-router-skills";
26
+ const TAB_TITLE = "技能";
27
+
20
28
  const btnStyle = {
21
29
  display: "inline-flex",
22
30
  alignItems: "center",
@@ -26,214 +34,161 @@ window.__ModuleLoader__.load({
26
34
  marginLeft: 6,
27
35
  borderRadius: 6,
28
36
  border: "1px solid var(--dsh-border, rgba(128,128,128,.35))",
29
- background: "var(--dsh-surface, transparent)",
37
+ background: "transparent",
30
38
  color: "inherit",
31
39
  fontSize: 12,
32
40
  lineHeight: "24px",
33
41
  cursor: "pointer",
34
42
  };
35
43
 
36
- const overlayStyle = {
37
- position: "fixed",
38
- inset: 0,
39
- zIndex: 9999,
40
- display: "flex",
41
- alignItems: "center",
42
- justifyContent: "center",
43
- background: "rgba(0,0,0,.45)",
44
- };
45
-
46
- const cardStyle = {
47
- position: "relative",
48
- width: "min(920px, 92vw)",
49
- maxHeight: "82vh",
50
- overflow: "auto",
51
- padding: "20px 22px",
52
- borderRadius: 12,
53
- background: "var(--dsh-surface, #1b1b1f)",
54
- color: "var(--dsh-text, inherit)",
55
- boxShadow: "0 18px 60px rgba(0,0,0,.45)",
56
- fontSize: 13,
57
- lineHeight: 1.6,
58
- };
59
-
60
- const closeStyle = {
61
- position: "absolute",
62
- top: 10,
63
- right: 12,
64
- border: "none",
65
- background: "transparent",
66
- color: "inherit",
67
- fontSize: 18,
68
- lineHeight: "20px",
69
- cursor: "pointer",
70
- opacity: 0.7,
71
- };
44
+ /** 右栏标签图标:三条横线,取 currentColor 跟随宿主主题。 */
45
+ function SkillsIcon(props) {
46
+ const size = props && props.size ? props.size : 16;
47
+ return h(
48
+ "svg",
49
+ { width: size, height: size, viewBox: "0 0 16 16", fill: "none", "aria-hidden": "true" },
50
+ h("path", {
51
+ d: "M3 4h10M3 8h10M3 12h6",
52
+ stroke: "currentColor",
53
+ strokeWidth: 1.5,
54
+ strokeLinecap: "round",
55
+ })
56
+ );
57
+ }
72
58
 
73
- /** 技能面板(骨架;阶段 2/3 接真实数据)。 */
74
- function SkillsPanel(props) {
59
+ /** 右栏正文(1.0.10 只做壳,验证形态)。样式全部继承宿主主题,不写死颜色。 */
60
+ function SkillsPane() {
75
61
  return h(
76
62
  "div",
77
- { style: overlayStyle, onClick: props.onClose },
63
+ { style: { padding: "12px 14px", fontSize: 13, lineHeight: 1.7 } },
64
+ h("div", { style: { fontWeight: 600, marginBottom: 6 } }, "技能面板 · 骨架"),
78
65
  h(
79
66
  "div",
80
- { style: cardStyle, onClick: (event) => event.stopPropagation() },
81
- h("button", { type: "button", style: closeStyle, onClick: props.onClose, title: "关闭" }, "✕"),
82
- h("div", { style: { fontSize: 15, fontWeight: 600, marginBottom: 8 } }, "技能面板 · 骨架"),
83
- h(
84
- "div",
85
- { style: { opacity: 0.75 } },
86
- "链路已通:这个面板由 dsh-lean-router 的客户端插件渲染。",
87
- h("br"),
88
- "阶段 2 接入 31 个技能与审计统计,阶段 3 加分组表格、开关与一键工作流。"
89
- )
67
+ { style: { opacity: 0.75 } },
68
+ "右栏形态已接好:这是 lean-router 的右侧栏标签页,可以和其他右栏面板并存。"
69
+ ),
70
+ h(
71
+ "div",
72
+ { style: { opacity: 0.75, marginTop: 6 } },
73
+ "下一步接入 31 个技能、开关与命中统计。"
90
74
  )
91
75
  );
92
76
  }
93
77
 
94
- /** 顶栏「技能」按钮:点开/关闭面板。 */
95
- function SkillsButton() {
96
- const [open, setOpen] = React.useState(false);
97
- const close = React.useCallback(() => setOpen(false), []);
78
+ /** 顶栏「技能」按钮:打开右栏标签(宿主无右栏服务时什么也不做,只告警)。 */
79
+ function SkillsButton(props) {
80
+ const open = props && typeof props.open === "function" ? props.open : () => {};
98
81
  return h(
99
- React.Fragment,
100
- null,
101
- h(
102
- "button",
103
- {
104
- type: "button",
105
- style: btnStyle,
106
- title: "lean-router 技能面板",
107
- onClick: () => setOpen((value) => !value),
108
- },
109
- "技能"
110
- ),
111
- open ? h(SkillsPanel, { onClose: close }) : null
82
+ "button",
83
+ { type: "button", style: btnStyle, title: "打开技能面板(右侧栏)", onClick: open },
84
+ "技能"
112
85
  );
113
86
  }
114
87
 
115
- /**
116
- * 纯 DOM 兜底(零 React 依赖):右上角一个固定定位的「技能」按钮。
117
- * 只在 slot 注册全部失败时使用,用来区分失败类型。
118
- */
119
- function domFallback() {
120
- if (typeof document === "undefined" || !document.body) return;
121
- if (document.getElementById("lean-router-dom-fallback")) return;
122
- const btn = document.createElement("button");
123
- btn.id = "lean-router-dom-fallback";
124
- btn.type = "button";
125
- btn.textContent = "技能";
126
- btn.title = "lean-router 兜底按钮(slot 注册失败)";
127
- btn.style.cssText =
128
- "position:fixed;top:8px;right:12px;z-index:2147483647;height:26px;padding:0 10px;" +
129
- "border-radius:6px;border:1px solid rgba(128,128,128,.45);background:rgba(30,30,36,.92);" +
130
- "color:#fff;font-size:12px;line-height:24px;cursor:pointer;box-shadow:0 4px 16px rgba(0,0,0,.35)";
131
- let panel = null;
132
- btn.addEventListener("click", () => {
133
- if (panel) {
134
- panel.remove();
135
- panel = null;
136
- return;
88
+ /** 打开右栏标签;返回是否打开成功。 */
89
+ function makeOpener(ctx) {
90
+ return () => {
91
+ try {
92
+ const sidebar = typeof ctx.get === "function" ? ctx.get("sidebarRight") : undefined;
93
+ if (sidebar && typeof sidebar.openTab === "function") {
94
+ sidebar.openTab(TAB_KIND);
95
+ return true;
96
+ }
97
+ console.warn("[lean-router] sidebarRight.openTab 不可用,右栏无法打开");
98
+ } catch (err) {
99
+ console.warn("[lean-router] sidebarRight.openTab failed", err);
137
100
  }
138
- panel = document.createElement("div");
139
- panel.style.cssText =
140
- "position:fixed;top:44px;right:12px;z-index:2147483647;width:min(420px,92vw);" +
141
- "padding:16px 18px;border-radius:10px;background:rgba(30,30,36,.97);color:#fff;" +
142
- "font:13px/1.6 system-ui,sans-serif;box-shadow:0 18px 60px rgba(0,0,0,.5)";
143
- panel.innerHTML =
144
- "<div style='font-weight:600;margin-bottom:6px'>技能面板 · 兜底渲染</div>" +
145
- "<div style='opacity:.75'>客户端 bundle 已加载,但 slot 注册没成功。" +
146
- "这说明问题在 slot API,不在加载链路。</div>";
147
- document.body.appendChild(panel);
148
- });
149
- document.body.appendChild(btn);
101
+ return false;
102
+ };
150
103
  }
151
104
 
152
- // 0.2.3 真机事实(对照 dsh-cost-meter 的 lib/client.js):
153
- // const slots = ctx.get("slots")
154
- // slots.inject(slotName, () => slots.register({ name, id, order }, Component))
155
- // `slots` / `register` 都是客户端 cordis 的服务;直接读 ctx.register / ctx.slots 会抛
156
- // Error: cannot get property "register" without inject。取服务的唯一安全姿势是 ctx.get(name)。
157
- //
158
- // 1.0.7 修(真机):旧代码把 `typeof ctx.register === "function"` 写在 try 之外,属性访问
159
- // 直接抛错 → 整个 apply 失败 → 连 DOM 兜底都跑不到(顶栏没按钮的真因,控制台原文:
160
- // dsh-lean-router: failed: Error: cannot get property "register" without inject)。
161
- // 1.0.8 修(真机):apply 执行得比 slots 服务注册还早。此时 ctx.get("slots") 是 undefined,
162
- // 旧代码直接走 DOM 兜底,而那时 document.body 还不存在 → domFallback 静默 return,
163
- // 于是「bundle 已加载、控制台无报错、界面上什么都没有」。
164
- // 正确姿势与 dsh-cost-meter 用 e.inject(["locale"], …) 一致:ctx.inject(["slots"], cb)
165
- // 等服务就绪后再注册,服务变化时还会重跑。
166
- const SLOTS = ["conversation.session.header.actions", "conversation.session.header.utilities"];
167
-
168
105
  function apply(ctx) {
169
106
  const disposers = [];
170
- let registered = false;
171
107
 
172
- const registerInto = (slots) => {
173
- for (const slot of SLOTS) {
174
- // inject 是 slot 的 props 工厂:cost-meter 每次 register 都带,这里给空对象即可。
175
- const spec = { name: slot, id: "lean-router.skills", order: 60, inject: () => ({}) };
176
- try {
177
- const dispose = slots.inject(slot, () => slots.register(spec, SkillsButton));
178
- if (typeof dispose === "function") disposers.push(dispose);
179
- registered = true;
180
- return true;
181
- } catch (err) {
182
- try {
183
- console.warn("[lean-router] register failed on", slot, err);
184
- } catch {}
185
- }
108
+ const registerSlots = (scope) => {
109
+ const slots = scope && typeof scope.get === "function" ? scope.get("slots") : undefined;
110
+ if (!slots || typeof slots.inject !== "function" || typeof slots.register !== "function") {
111
+ return false;
186
112
  }
187
- return false;
188
- };
189
113
 
190
- const attempt = (scope) => {
191
- let slots;
114
+ // 1) 右栏 tab 的类型与引导入口。类型挂在宿主服务上、插件重挂时仍留着,
115
+ // 所以必须先查重,否则宿主会抛 tab type id already registered。
192
116
  try {
193
- slots = scope && typeof scope.get === "function" ? scope.get("slots") : undefined;
117
+ const tabs = typeof scope.get === "function" ? scope.get("sidebarRightTabs") : undefined;
118
+ if (tabs && typeof tabs.register === "function") {
119
+ const already = typeof tabs.get === "function" ? tabs.get(TAB_ID) : null;
120
+ if (!already) {
121
+ tabs.register({
122
+ id: TAB_ID,
123
+ kind: TAB_KIND,
124
+ title: () => TAB_TITLE,
125
+ guide: [
126
+ {
127
+ id: "lean-router:skills:guide",
128
+ order: 60,
129
+ title: () => TAB_TITLE,
130
+ description: () => "技能清单、开关与命中统计",
131
+ icon: (size) => h(SkillsIcon, { size }),
132
+ },
133
+ ],
134
+ });
135
+ }
136
+ }
194
137
  } catch (err) {
195
- try {
196
- console.warn("[lean-router] ctx.get('slots') failed", err);
197
- } catch {}
138
+ console.warn("[lean-router] sidebarRightTabs.register failed", err);
198
139
  }
199
- if (slots === undefined || typeof slots.inject !== "function" || typeof slots.register !== "function") {
200
- return false;
140
+
141
+ // 2) 顶栏入口 + 右栏正文与标题。opener 必须基于已就绪的 scope,
142
+ // 用外层 ctx 取 sidebarRight 可能还拿不到(1.0.8 同款时机问题)。
143
+ const open = makeOpener(scope);
144
+ const registrations = [
145
+ [
146
+ "conversation.session.header.actions",
147
+ () => ({ name: "conversation.session.header.actions", id: "lean-router.skills", order: 60, inject: () => ({ open }) }),
148
+ SkillsButton,
149
+ ],
150
+ [
151
+ "sidebar.right.pane.tab",
152
+ () => ({
153
+ name: "sidebar.right.pane.tab",
154
+ key: TAB_ID,
155
+ inject: (sessionId) => ({ sessionId }),
156
+ }),
157
+ SkillsPane,
158
+ ],
159
+ [
160
+ "sidebar.right.pane.tab.title",
161
+ () => ({ name: "sidebar.right.pane.tab.title", key: TAB_ID }),
162
+ () => TAB_TITLE,
163
+ ],
164
+ ];
165
+
166
+ let registered = 0;
167
+ for (const [slot, spec, component] of registrations) {
168
+ try {
169
+ const dispose = slots.inject(slot, () => slots.register(spec(), component));
170
+ if (typeof dispose === "function") disposers.push(dispose);
171
+ registered += 1;
172
+ } catch (err) {
173
+ console.warn("[lean-router] register failed on", slot, err);
174
+ }
201
175
  }
202
- return registerInto(slots);
176
+ return registered > 0;
203
177
  };
204
178
 
205
179
  if (typeof ctx.inject === "function") {
206
180
  try {
207
181
  const dispose = ctx.inject(["slots"], (scoped) => {
208
- attempt(scoped);
182
+ registerSlots(scoped);
209
183
  });
210
184
  if (typeof dispose === "function") disposers.push(dispose);
211
185
  } catch (err) {
212
- try {
213
- console.warn("[lean-router] ctx.inject(['slots']) failed", err);
214
- } catch {}
186
+ console.warn("[lean-router] ctx.inject(['slots']) failed", err);
215
187
  }
216
188
  } else {
217
- attempt(ctx);
189
+ registerSlots(ctx);
218
190
  }
219
191
 
220
- // 兜底:2 秒后仍没注册上,就在右上角插一个纯 DOM 按钮。
221
- // 目的不是最终形态,而是让「没挂上」这件事可见,不要再静默消失。
222
- try {
223
- const timer = setTimeout(() => {
224
- if (registered) return;
225
- try {
226
- domFallback();
227
- } catch (err) {
228
- try {
229
- console.warn("[lean-router] dom fallback failed", err);
230
- } catch {}
231
- }
232
- }, 2000);
233
- if (timer && typeof timer.unref === "function") timer.unref();
234
- disposers.push(() => clearTimeout(timer));
235
- } catch {}
236
-
237
192
  return () => {
238
193
  for (const d of disposers) {
239
194
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-lean-router",
3
- "version": "1.0.8",
3
+ "version": "1.1.0",
4
4
  "description": "lean 省 token × 思考链路由 × code_run 工具遮蔽 × mattpocock 技能自动调用的 DSH cordis 插件(单 bundle:预设+配套技能)",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
@@ -1,9 +1,10 @@
1
- // dsh-lean-router 1.0.8 —— 客户端插件(lib/client.js)的服务获取与时机回归。
2
- // 两次真机事故:
3
- // 1.0.6: 直接读 ctx.register 抛 cannot get property "register" without inject,apply 整体失败。
4
- // 1.0.7: 改成 ctx.get("slots"),但 apply 比 slots 服务注册更早,拿到 undefined 后走 DOM 兜底,
5
- // 而那时 document.body 还没建好 → 静默无按钮、控制台无报错。
6
- // 现在的契约:用 ctx.inject(["slots"], cb) 等服务就绪再注册;2 秒仍未注册才插 DOM 兜底。
1
+ // dsh-lean-router —— 客户端插件(lib/client.js)契约回归。
2
+ // 形态演进:浮层 → 右栏标签页(用户 2026-11-25 拍板)。
3
+ // 契约来源:真机范例 @modusensus/dsh-mneme + dsh-cost-meter。
4
+ // 类型/引导: ctx.get("sidebarRightTabs").register({ id, kind, title, guide })
5
+ // 正文/标题: sidebar.right.pane.tab / .title,spec 用 { name, key, inject }
6
+ // 打开: ctx.get("sidebarRight").openTab(kind)
7
+ // 踩过的坑:直接读 ctx.register 会抛;apply 早于 slots 就绪时 ctx.get("slots") 为 undefined。
7
8
  import test from "node:test";
8
9
  import assert from "node:assert/strict";
9
10
 
@@ -14,8 +15,6 @@ await import("../lib/client.js");
14
15
  const React = {
15
16
  createElement: (type, props, ...children) => ({ type, props, children }),
16
17
  Fragment: "Fragment",
17
- useState: (value) => [value, () => {}],
18
- useCallback: (fn) => fn,
19
18
  };
20
19
 
21
20
  function makePlugin() {
@@ -25,14 +24,7 @@ function makePlugin() {
25
24
  });
26
25
  }
27
26
 
28
- function makeSlots(registered) {
29
- return {
30
- inject(name, cb) { cb(); return () => {}; },
31
- register(spec, component) { registered.push({ spec, component }); return () => {}; },
32
- };
33
- }
34
-
35
- /** 访问 slots / register 属性就会抛错的 ctx,复刻客户端 cordis 的服务保护。 */
27
+ /** 访问 register / slots 属性就会抛错的 ctx,复刻客户端 cordis 的服务保护。 */
36
28
  function throwingGetters(extra) {
37
29
  return Object.assign({
38
30
  get register() { throw new Error('cannot get property "register" without inject'); },
@@ -40,46 +32,85 @@ function throwingGetters(extra) {
40
32
  }, extra);
41
33
  }
42
34
 
43
- test("用 ctx.inject(['slots'], cb) 等服务就绪后注册到头栏 slot", () => {
35
+ /** 组一套假服务:slots / sidebarRightTabs / sidebarRight。 */
36
+ function makeServices(options = {}) {
44
37
  const registered = [];
45
- const slots = makeSlots(registered);
46
- let asked = null;
38
+ const tabTypes = [];
39
+ const opened = [];
40
+ const slots = {
41
+ inject(name, cb) { cb(); return () => {}; },
42
+ register(spec, component) { registered.push({ spec, component }); return () => {}; },
43
+ };
44
+ const tabs = {
45
+ register(def) { tabTypes.push(def); },
46
+ get() { return options.existingTabType === true ? { id: "lean-router.skills" } : null; },
47
+ };
48
+ const sidebar = { openTab(kind) { opened.push(kind); } };
49
+ const map = { slots, sidebarRightTabs: tabs, sidebarRight: sidebar };
50
+ return { registered, tabTypes, opened, map };
51
+ }
52
+
53
+ function applyWith(services) {
47
54
  const ctx = throwingGetters({
48
- inject(deps, cb) { asked = deps; cb(throwingGetters({ get: (name) => (name === "slots" ? slots : undefined) })); return () => {}; },
49
- get(name) { return name === "slots" ? undefined : undefined; },
55
+ inject(deps, cb) { cb(throwingGetters({ get: (name) => services.map[name] })); return () => {}; },
56
+ get() { return undefined; },
50
57
  });
51
58
  const dispose = makePlugin().apply(ctx);
52
- assert.equal(typeof dispose, "function");
53
- assert.deepEqual(asked, ["slots"]);
54
- assert.equal(registered.length, 1);
55
- assert.equal(registered[0].spec.name, "conversation.session.header.actions");
56
- assert.equal(registered[0].spec.id, "lean-router.skills");
59
+ assert.equal(typeof dispose, "function", "apply 必须返回 disposer");
60
+ return services;
61
+ }
62
+
63
+ test("注册右栏 tab 类型,含 id / kind / title / guide", () => {
64
+ const s = applyWith(makeServices());
65
+ assert.equal(s.tabTypes.length, 1);
66
+ assert.equal(s.tabTypes[0].id, "lean-router.skills");
67
+ assert.equal(s.tabTypes[0].kind, "lean-router-skills");
68
+ assert.equal(s.tabTypes[0].title(), "技能");
69
+ assert.ok(Array.isArray(s.tabTypes[0].guide) && s.tabTypes[0].guide.length === 1);
57
70
  });
58
71
 
59
- test("没有 ctx.inject 时退化为直接取 slots 注册", () => {
60
- const registered = [];
61
- const slots = makeSlots(registered);
62
- const ctx = throwingGetters({ get: (name) => (name === "slots" ? slots : undefined) });
72
+ test("tab 类型已存在时不重复注册(宿主对重复 id 会抛)", () => {
73
+ const s = applyWith(makeServices({ existingTabType: true }));
74
+ assert.equal(s.tabTypes.length, 0);
75
+ });
76
+
77
+ test("注册三个座位:顶栏入口 / 右栏正文 / 右栏标题,正文用 key 关联类型", () => {
78
+ const s = applyWith(makeServices());
79
+ const names = s.registered.map((r) => r.spec.name);
80
+ assert.deepEqual(names, [
81
+ "conversation.session.header.actions",
82
+ "sidebar.right.pane.tab",
83
+ "sidebar.right.pane.tab.title",
84
+ ]);
85
+ const pane = s.registered.find((r) => r.spec.name === "sidebar.right.pane.tab");
86
+ assert.equal(pane.spec.key, "lean-router.skills");
87
+ const title = s.registered.find((r) => r.spec.name === "sidebar.right.pane.tab.title");
88
+ assert.equal(title.spec.key, "lean-router.skills");
89
+ });
90
+
91
+ test("点顶栏按钮走 sidebarRight.openTab(kind)", () => {
92
+ const s = applyWith(makeServices());
93
+ const header = s.registered.find((r) => r.spec.name === "conversation.session.header.actions");
94
+ const element = header.component(header.spec.inject());
95
+ assert.equal(element.type, "button");
96
+ element.props.onClick();
97
+ assert.deepEqual(s.opened, ["lean-router-skills"]);
98
+ });
99
+
100
+ test("没有 ctx.inject 时退化为直接注册(老宿主兼容)", () => {
101
+ const services = makeServices();
102
+ const ctx = throwingGetters({ get: (name) => services.map[name] });
63
103
  const dispose = makePlugin().apply(ctx);
64
104
  assert.equal(typeof dispose, "function");
65
- assert.equal(registered.length, 1);
105
+ assert.equal(services.registered.length, 3);
66
106
  });
67
107
 
68
- test("slots 一直不出现时,2 秒后插入纯 DOM 兜底按钮(不再静默)", async () => {
69
- const appended = [];
70
- globalThis.document = {
71
- body: { appendChild(node) { appended.push(node); } },
72
- getElementById() { return undefined; },
73
- createElement(tag) { return { tag, style: {}, addEventListener() {}, remove() {} }; },
74
- };
108
+ test("不再往 document.body 追加任何东西(浮层形态已废弃)", () => {
109
+ const children = [];
110
+ globalThis.document = { body: { appendChild(node) { children.push(node); } }, getElementById() { return undefined; }, createElement() { return { style: {}, addEventListener() {} }; } };
75
111
  try {
76
- const ctx = throwingGetters({ inject() { return () => {}; }, get() { return undefined; } });
77
- const dispose = makePlugin().apply(ctx);
78
- assert.equal(typeof dispose, "function");
79
- assert.equal(appended.length, 0, "兜底要等 2 秒,不能立刻插");
80
- await new Promise((resolve) => setTimeout(resolve, 2200));
81
- assert.equal(appended.length, 1, "2 秒后必须插兜底按钮");
82
- assert.equal(appended[0].id, "lean-router-dom-fallback");
112
+ applyWith(makeServices());
113
+ assert.equal(children.length, 0);
83
114
  } finally {
84
115
  delete globalThis.document;
85
116
  }