dsh-lean-router 1.0.4 → 1.0.6

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.
@@ -0,0 +1,61 @@
1
+ // dsh-lean-router 1.0.6 —— 包入口(lib/index.js)的 cordis 契约回归。
2
+ // 背景(0.2.3 真机):inject 写成 { required: [], optional: ["skills"] } 会被 cordis 当成
3
+ // 两个叫 required / optional 的服务,宿主层那行永远 PENDING,apply 不跑,客户端 bundle
4
+ // 不被扫描,顶栏按钮不出现。cordis 的 inject 只有两种形状:
5
+ // Inject<M> = (keyof M)[] | { [K in keyof M]?: M[K] } (registry.ts L19)
6
+ import test from "node:test";
7
+ import assert from "node:assert/strict";
8
+ import { tmpdir } from "node:os";
9
+ import { join } from "node:path";
10
+
11
+ import * as entry from "../lib/index.js";
12
+
13
+ /** 把 homedir() 指向临时目录,避免自检日志写进真实用户目录。 */
14
+ function withTempHome(fn) {
15
+ const saved = process.env.USERPROFILE;
16
+ process.env.USERPROFILE = join(tmpdir(), "dsh-lean-router-test-home");
17
+ try {
18
+ return fn();
19
+ } finally {
20
+ if (saved === undefined) delete process.env.USERPROFILE;
21
+ else process.env.USERPROFILE = saved;
22
+ }
23
+ }
24
+
25
+ test("inject 是 cordis 认的形状,不能出现 required/optional 键", () => {
26
+ if (Array.isArray(entry.inject)) return;
27
+ assert.equal(typeof entry.inject, "object", "inject 必须是数组或服务名映射");
28
+ const keys = Object.keys(entry.inject);
29
+ assert.ok(
30
+ !keys.includes("required") && !keys.includes("optional"),
31
+ "inject 被当成服务名映射后出现了 required/optional 键:" + keys.join(","),
32
+ );
33
+ });
34
+
35
+ test("宿主作用域:provider:false 时不碰 skills,apply 返回 undefined", () => {
36
+ withTempHome(() => {
37
+ const ctx = { inject() { throw new Error("provider:false 不该调用 ctx.inject"); } };
38
+ assert.equal(entry.apply(ctx, { provider: false }), undefined);
39
+ });
40
+ });
41
+
42
+ test("预设作用域:用 ctx.inject 等 skills 就绪,而不是把它写进必需依赖", () => {
43
+ withTempHome(() => {
44
+ const calls = [];
45
+ const ctx = { inject(deps, cb) { calls.push({ deps, cb }); } };
46
+ assert.equal(entry.apply(ctx, {}), undefined, "apply 不能把 ctx.inject 的 fiber 当 effect 交回去");
47
+ assert.equal(calls.length, 1);
48
+ assert.deepEqual(calls[0].deps, ["skills"]);
49
+ let registered = null;
50
+ calls[0].cb({ skills: { registerProvider(factory) { registered = factory; } } });
51
+ assert.equal(typeof registered, "function", "skills 就绪后必须注册 provider");
52
+ });
53
+ });
54
+
55
+ test("没有 ctx.inject 时退化为直接注册(老宿主兼容)", () => {
56
+ withTempHome(() => {
57
+ let registered = null;
58
+ entry.apply({ skills: { registerProvider(factory) { registered = factory; } } }, {});
59
+ assert.equal(typeof registered, "function");
60
+ });
61
+ });
@@ -79,6 +79,18 @@ test("apply 默认注册 hooks 与 commands", async () => {
79
79
  assert.ok(commands.has("skill"));
80
80
  });
81
81
 
82
+ test("每个命令的 input hint 都非空(宿主 normalizeDefinition 会抛 TypeError)", async () => {
83
+ // 1.0.6 回归:/panel 曾用 input:{hint:""},0.2.3 真机报
84
+ // TypeError: command "panel" input hint must not be empty,整条 lean-router-commands 不激活。
85
+ const { commands } = await routedSetup({});
86
+ assert.ok(commands.size >= 4, "band / skill / panel / lean-audit 都应注册");
87
+ for (const [name, def] of commands) {
88
+ if (def.input === undefined) continue;
89
+ assert.equal(typeof def.input.hint, "string", name + " 的 input.hint 必须是字符串");
90
+ assert.ok(def.input.hint.trim().length > 0, "命令 " + name + " 的 input hint 不能为空");
91
+ }
92
+ });
93
+
82
94
  test("options 分流:hooks:false 只留命令;commands:false 只留钩子", async () => {
83
95
  const a = await routedSetup({ hooks: false });
84
96
  assert.ok(!a.handlers.has("agent/inbox/claimed"));
@@ -0,0 +1,114 @@
1
+ // 技能面板(命令输出形态)的单元测试:断言结构与关键事实,不锁死排版细节。
2
+ import { test } from "node:test";
3
+ import assert from "node:assert/strict";
4
+ import { mkdtempSync, writeFileSync } from "node:fs";
5
+ import { tmpdir } from "node:os";
6
+ import { join } from "node:path";
7
+ import {
8
+ renderSkillsPanel,
9
+ loadMatrix,
10
+ parsePanelArgs,
11
+ executeSkillsPanelCommand,
12
+ displayWidth,
13
+ } from "../lib/skills-panel.js";
14
+
15
+ function tmpAudit(lines) {
16
+ const dir = mkdtempSync(join(tmpdir(), "lean-router-panel-"));
17
+ const p = join(dir, "audit.jsonl");
18
+ writeFileSync(p, lines.map((l) => JSON.stringify(l)).join("\n") + "\n", "utf8");
19
+ return p;
20
+ }
21
+
22
+ const MISSING = () => join(tmpdir(), "missing-" + Date.now() + "-" + Math.random() + ".jsonl");
23
+
24
+ function roundEntry(over) {
25
+ return {
26
+ seq: 1,
27
+ v: 1,
28
+ kind: "skillgate-round",
29
+ turn: 1,
30
+ band: "react",
31
+ scores: [
32
+ { name: "code-review", score: 3, signals: { category: { scene: "engineering", points: 2 }, keywords: [] } },
33
+ { name: "tdd", score: 1, signals: { category: { scene: "engineering", points: 1 }, keywords: [] } },
34
+ ],
35
+ decision: { auto: ["code-review"], hint: [], degradations: [] },
36
+ config: { tokenBudget: 8000, charsPerToken: 4 },
37
+ ...over,
38
+ };
39
+ }
40
+
41
+ test("loadMatrix 读到 31 个技能", () => {
42
+ const m = loadMatrix();
43
+ assert.ok(m && Array.isArray(m.skills));
44
+ assert.equal(m.skills.length, 31);
45
+ });
46
+
47
+ test("总览包含四类信息", () => {
48
+ const text = renderSkillsPanel({ auditPath: MISSING() });
49
+ assert.match(text, /=== 技能面板/);
50
+ assert.match(text, /技能 31/);
51
+ assert.match(text, /--- 本轮 ---/);
52
+ assert.match(text, /--- 工程(/);
53
+ assert.match(text, /--- 从未命中/);
54
+ });
55
+
56
+ test("按场景分组:工程与流程两组都在", () => {
57
+ const text = renderSkillsPanel({ auditPath: MISSING(), view: "skills" });
58
+ assert.match(text, /--- 工程(/);
59
+ assert.match(text, /--- 流程(/);
60
+ assert.match(text, /--- 元交互(/);
61
+ });
62
+
63
+ test("技能名列不被最长名字撑破(表头与分隔线对齐)", () => {
64
+ const text = renderSkillsPanel({ auditPath: MISSING(), view: "skills" });
65
+ const lines = text.split("\n");
66
+ const i = lines.findIndex((l) => l.includes("自动") && l.includes("提示") && l.startsWith("技能"));
67
+ assert.ok(i >= 0, "应有表头行");
68
+ const rule = lines[i + 1];
69
+ assert.equal(displayWidth(lines[i]), displayWidth(rule), "表头与分隔线显示宽度应一致");
70
+ const row = lines.find((l) => l.startsWith("improve-codebase-architecture"));
71
+ assert.ok(row, "最长技能名应有行");
72
+ assert.ok(
73
+ displayWidth(row) <= displayWidth(rule),
74
+ "最长技能名那一行不应超出分隔线(名字列宽度够用)"
75
+ );
76
+ assert.ok(displayWidth(row) >= displayWidth(rule) - 4, "该行也不应明显短于分隔线");
77
+ });
78
+
79
+ test("审计缺失时降级而不抛错", () => {
80
+ const text = renderSkillsPanel({ auditPath: MISSING() });
81
+ assert.match(text, /未生成/);
82
+ });
83
+
84
+ test("历史统计按技能聚合", () => {
85
+ const text = renderSkillsPanel({ auditPath: tmpAudit([roundEntry({})]) });
86
+ assert.match(text, /1 轮/);
87
+ const row = text.split("\n").find((l) => l.startsWith("code-review"));
88
+ assert.ok(row);
89
+ });
90
+
91
+ test("stats 视图含轮次/自动/提示列", () => {
92
+ const text = renderSkillsPanel({ auditPath: tmpAudit([roundEntry({})]), view: "stats" });
93
+ assert.match(text, /--- 历史统计(1 轮)---/);
94
+ assert.match(text, /轮次/);
95
+ });
96
+
97
+ test("round 视图给得分构成", () => {
98
+ const text = renderSkillsPanel({ auditPath: MISSING(), view: "round" });
99
+ assert.match(text, /尚无门控记录/);
100
+ });
101
+
102
+ test("parsePanelArgs 解析视图与 all", () => {
103
+ assert.deepEqual(parsePanelArgs(""), { view: "summary", all: false });
104
+ assert.deepEqual(parsePanelArgs("skills"), { view: "skills", all: false });
105
+ assert.deepEqual(parsePanelArgs("all"), { view: "summary", all: true });
106
+ assert.deepEqual(parsePanelArgs(" stats ALL "), { view: "stats", all: true });
107
+ assert.deepEqual(parsePanelArgs("bogus"), { view: "summary", all: false });
108
+ });
109
+
110
+ test("命令入口返回 success", async () => {
111
+ const out = await executeSkillsPanelCommand({ rawInput: "" }, { auditPath: MISSING() });
112
+ assert.equal(out.kind, "success");
113
+ assert.match(out.text, /技能面板/);
114
+ });