@dsh-workbench/shared 0.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.
Files changed (206) hide show
  1. package/dist/api/blocker-table.d.ts +9 -0
  2. package/dist/api/blocker-table.js +130 -0
  3. package/dist/api/blocker.d.ts +30 -0
  4. package/dist/api/blocker.js +1 -0
  5. package/dist/api/context-ref.d.ts +10 -0
  6. package/dist/api/context-ref.js +40 -0
  7. package/dist/api/dsh-client.d.ts +170 -0
  8. package/dist/api/dsh-client.js +1 -0
  9. package/dist/api/dsh.d.ts +153 -0
  10. package/dist/api/dsh.js +1 -0
  11. package/dist/api/facts.d.ts +27 -0
  12. package/dist/api/facts.js +146 -0
  13. package/dist/api/host-write.d.ts +12 -0
  14. package/dist/api/host-write.js +17 -0
  15. package/dist/api/long-task.d.ts +18 -0
  16. package/dist/api/long-task.js +22 -0
  17. package/dist/api/unit-board.d.ts +14 -0
  18. package/dist/api/unit-board.js +29 -0
  19. package/dist/api/unit.d.ts +54 -0
  20. package/dist/api/unit.js +1 -0
  21. package/dist/api/workbench-client.d.ts +19 -0
  22. package/dist/api/workbench-client.js +33 -0
  23. package/dist/api/workbench-contract.d.ts +32 -0
  24. package/dist/api/workbench-contract.js +1 -0
  25. package/dist/components/ApprovalDetail.d.ts +27 -0
  26. package/dist/components/ApprovalDetail.js +33 -0
  27. package/dist/components/CardHead.d.ts +16 -0
  28. package/dist/components/CardHead.js +14 -0
  29. package/dist/components/ChoiceGroup.d.ts +14 -0
  30. package/dist/components/ChoiceGroup.js +7 -0
  31. package/dist/components/LongTaskRow.d.ts +16 -0
  32. package/dist/components/LongTaskRow.js +28 -0
  33. package/dist/components/MockStrip.d.ts +14 -0
  34. package/dist/components/MockStrip.js +7 -0
  35. package/dist/components/ResourceStates.d.ts +18 -0
  36. package/dist/components/ResourceStates.js +17 -0
  37. package/dist/components/SaveBar.d.ts +12 -0
  38. package/dist/components/SaveBar.js +6 -0
  39. package/dist/components/StateDot.d.ts +5 -0
  40. package/dist/components/StateDot.js +10 -0
  41. package/dist/components/board-module.d.ts +67 -0
  42. package/dist/components/board-module.js +1 -0
  43. package/dist/components/icons.d.ts +18 -0
  44. package/dist/components/icons.js +81 -0
  45. package/dist/config/constants.d.ts +27 -0
  46. package/dist/config/constants.js +27 -0
  47. package/dist/config/storage.d.ts +4 -0
  48. package/dist/config/storage.js +27 -0
  49. package/dist/host/__tests__/fakes.d.ts +41 -0
  50. package/dist/host/__tests__/fakes.js +76 -0
  51. package/dist/host/__tests__/preflight-fixtures.d.ts +19 -0
  52. package/dist/host/__tests__/preflight-fixtures.js +47 -0
  53. package/dist/host/cli-error.d.ts +8 -0
  54. package/dist/host/cli-error.js +70 -0
  55. package/dist/host/context-inject.d.ts +24 -0
  56. package/dist/host/context-inject.js +74 -0
  57. package/dist/host/data-root.d.ts +3 -0
  58. package/dist/host/data-root.js +12 -0
  59. package/dist/host/events.d.ts +9 -0
  60. package/dist/host/events.js +70 -0
  61. package/dist/host/facts.d.ts +24 -0
  62. package/dist/host/facts.js +44 -0
  63. package/dist/host/git.d.ts +55 -0
  64. package/dist/host/git.js +64 -0
  65. package/dist/host/http.d.ts +36 -0
  66. package/dist/host/http.js +242 -0
  67. package/dist/host/long-task.d.ts +58 -0
  68. package/dist/host/long-task.js +148 -0
  69. package/dist/host/mount-units.d.ts +52 -0
  70. package/dist/host/mount-units.js +186 -0
  71. package/dist/host/opencli-runtime.d.ts +22 -0
  72. package/dist/host/opencli-runtime.js +46 -0
  73. package/dist/host/opencli.d.ts +37 -0
  74. package/dist/host/opencli.js +152 -0
  75. package/dist/host/plugin-source.d.ts +31 -0
  76. package/dist/host/plugin-source.js +81 -0
  77. package/dist/host/preflight.d.ts +58 -0
  78. package/dist/host/preflight.js +233 -0
  79. package/dist/host/probe-cache.d.ts +6 -0
  80. package/dist/host/probe-cache.js +19 -0
  81. package/dist/host/probe-libs.d.ts +45 -0
  82. package/dist/host/probe-libs.js +61 -0
  83. package/dist/host/probe-units.d.ts +17 -0
  84. package/dist/host/probe-units.js +102 -0
  85. package/dist/host/probe-verdict.d.ts +7 -0
  86. package/dist/host/probe-verdict.js +143 -0
  87. package/dist/host/provision.d.ts +14 -0
  88. package/dist/host/provision.js +12 -0
  89. package/dist/host/shell.d.ts +14 -0
  90. package/dist/host/shell.js +23 -0
  91. package/dist/host/tool-approval.d.ts +29 -0
  92. package/dist/host/tool-approval.js +190 -0
  93. package/dist/host/tool-kit.d.ts +19 -0
  94. package/dist/host/tool-kit.js +37 -0
  95. package/dist/host/tool-scope.d.ts +32 -0
  96. package/dist/host/tool-scope.js +84 -0
  97. package/dist/host/unit-face.d.ts +66 -0
  98. package/dist/host/unit-face.js +1 -0
  99. package/dist/host/unit-libs.d.ts +37 -0
  100. package/dist/host/unit-libs.js +1 -0
  101. package/dist/host/unit-readiness.d.ts +7 -0
  102. package/dist/host/unit-readiness.js +5 -0
  103. package/dist/host/unit-registry.d.ts +9 -0
  104. package/dist/host/unit-registry.js +33 -0
  105. package/dist/host/value-handoff.d.ts +16 -0
  106. package/dist/host/value-handoff.js +21 -0
  107. package/dist/host/version-range.d.ts +2 -0
  108. package/dist/host/version-range.js +73 -0
  109. package/dist/host/workspace-store.d.ts +19 -0
  110. package/dist/host/workspace-store.js +133 -0
  111. package/dist/host/write-result.d.ts +1 -0
  112. package/dist/host/write-result.js +18 -0
  113. package/dist/host.d.ts +19 -0
  114. package/dist/host.js +19 -0
  115. package/dist/i18n/en.d.ts +2 -0
  116. package/dist/i18n/en.js +162 -0
  117. package/dist/i18n/index.d.ts +172 -0
  118. package/dist/i18n/index.js +3 -0
  119. package/dist/i18n/merge.d.ts +6 -0
  120. package/dist/i18n/merge.js +40 -0
  121. package/dist/i18n/zh.d.ts +163 -0
  122. package/dist/i18n/zh.js +162 -0
  123. package/dist/index.d.ts +33 -0
  124. package/dist/index.js +33 -0
  125. package/dist/lib/blocker-action.d.ts +13 -0
  126. package/dist/lib/blocker-action.js +12 -0
  127. package/dist/lib/busy.d.ts +5 -0
  128. package/dist/lib/busy.js +7 -0
  129. package/dist/lib/react.d.ts +10 -0
  130. package/dist/lib/react.js +9 -0
  131. package/dist/lib/text.d.ts +4 -0
  132. package/dist/lib/text.js +48 -0
  133. package/dist/lib/ui.d.ts +11 -0
  134. package/dist/lib/ui.js +37 -0
  135. package/package.json +44 -0
  136. package/src/api/__tests__/blocker-table.test.ts +253 -0
  137. package/src/api/__tests__/facts.test.ts +274 -0
  138. package/src/api/__tests__/host-write.test.ts +55 -0
  139. package/src/api/__tests__/long-task.test.ts +52 -0
  140. package/src/api/__tests__/unit-board.test.ts +76 -0
  141. package/src/api/blocker-table.ts +155 -0
  142. package/src/api/blocker.ts +25 -0
  143. package/src/api/context-ref.ts +47 -0
  144. package/src/api/dsh-client.ts +176 -0
  145. package/src/api/dsh.ts +156 -0
  146. package/src/api/facts.ts +166 -0
  147. package/src/api/host-write.ts +27 -0
  148. package/src/api/long-task.ts +38 -0
  149. package/src/api/unit-board.ts +44 -0
  150. package/src/api/unit.ts +59 -0
  151. package/src/api/workbench-client.test.ts +92 -0
  152. package/src/api/workbench-client.ts +59 -0
  153. package/src/api/workbench-contract.ts +40 -0
  154. package/src/components/ApprovalDetail.tsx +57 -0
  155. package/src/components/CardHead.tsx +74 -0
  156. package/src/components/ChoiceGroup.tsx +38 -0
  157. package/src/components/LongTaskRow.tsx +65 -0
  158. package/src/components/MockStrip.tsx +40 -0
  159. package/src/components/ResourceStates.tsx +44 -0
  160. package/src/components/SaveBar.tsx +31 -0
  161. package/src/components/StateDot.tsx +31 -0
  162. package/src/components/board-module.ts +73 -0
  163. package/src/components/icons.tsx +232 -0
  164. package/src/config/constants.test.ts +41 -0
  165. package/src/config/constants.ts +52 -0
  166. package/src/config/storage.test.ts +82 -0
  167. package/src/config/storage.ts +25 -0
  168. package/src/host/__tests__/fakes.ts +116 -0
  169. package/src/host/__tests__/opencli-runtime.test.ts +46 -0
  170. package/src/host/cli-error.ts +77 -0
  171. package/src/host/data-root.ts +13 -0
  172. package/src/host/facts.ts +69 -0
  173. package/src/host/git.ts +99 -0
  174. package/src/host/http.ts +311 -0
  175. package/src/host/long-task.ts +235 -0
  176. package/src/host/opencli-runtime.ts +66 -0
  177. package/src/host/opencli.ts +210 -0
  178. package/src/host/plugin-source.ts +145 -0
  179. package/src/host/provision.ts +34 -0
  180. package/src/host/shell.ts +42 -0
  181. package/src/host/tool-approval.ts +243 -0
  182. package/src/host/tool-kit.ts +56 -0
  183. package/src/host/unit-face.ts +65 -0
  184. package/src/host/unit-libs.ts +41 -0
  185. package/src/host/value-handoff.ts +38 -0
  186. package/src/host/workspace-store.ts +161 -0
  187. package/src/host/write-result.ts +16 -0
  188. package/src/host.ts +19 -0
  189. package/src/i18n/en.ts +170 -0
  190. package/src/i18n/index.test.ts +15 -0
  191. package/src/i18n/index.ts +12 -0
  192. package/src/i18n/merge.test.ts +60 -0
  193. package/src/i18n/merge.ts +54 -0
  194. package/src/i18n/zh.test.ts +40 -0
  195. package/src/i18n/zh.ts +170 -0
  196. package/src/index.ts +33 -0
  197. package/src/lib/blocker-action.test.ts +40 -0
  198. package/src/lib/blocker-action.ts +28 -0
  199. package/src/lib/busy.test.ts +27 -0
  200. package/src/lib/busy.ts +11 -0
  201. package/src/lib/react.test.ts +26 -0
  202. package/src/lib/react.ts +16 -0
  203. package/src/lib/text.test.ts +92 -0
  204. package/src/lib/text.ts +46 -0
  205. package/src/lib/ui.test.ts +93 -0
  206. package/src/lib/ui.ts +38 -0
@@ -0,0 +1,253 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import type { Translate } from '../dsh-client';
3
+ import type { CapabilityPayload } from '../workbench-contract';
4
+ import { blockerOfCapability, blockerOfError } from '../blocker-table';
5
+
6
+ const t: Translate = (key, params) => (params === undefined ? key : `${key}(${JSON.stringify(params)})`);
7
+
8
+ describe('blockerOfError', () => {
9
+ it('未登录 + 有登录页地址 → 行内「去登录 ↗」+ 一颗手动「重试」(不安排自动重试)', () => {
10
+ const blocker = blockerOfError(
11
+ { code: 'auth-required', message: '未登录 CODING', helpUrl: 'https://x.coding.net/login' },
12
+ t,
13
+ );
14
+
15
+ expect(blocker.id).toBe('auth-required');
16
+ expect(blocker.severity).toBe('block');
17
+ expect(blocker.actions).toEqual([
18
+ {
19
+ kind: 'open-url',
20
+ label: 'blocker.login.go',
21
+ url: 'https://x.coding.net/login',
22
+ inline: true,
23
+ },
24
+ { kind: 'retry', label: 'blocker.retry' },
25
+ ]);
26
+ expect(blocker.hint).toBe('blocker.login.hint');
27
+ });
28
+
29
+ it('平台名由**调用方**给(同一个"未登录"能服务不同平台)', () => {
30
+ const coding = blockerOfError(
31
+ { code: 'auth-required', message: '未登录', helpUrl: 'https://x.coding.net/login' },
32
+ t,
33
+ { platform: 'Coding' },
34
+ );
35
+ const cmdb = blockerOfError(
36
+ { code: 'auth-required', message: '未登录', helpUrl: 'https://cmdb.example.cn/login' },
37
+ t,
38
+ { platform: 'CMDB' },
39
+ );
40
+
41
+ expect(coding.title).toContain('"platform":"Coding"');
42
+ expect(cmdb.title).toContain('"platform":"CMDB"');
43
+ expect(cmdb.actions[0]).toMatchObject({ kind: 'open-url', url: 'https://cmdb.example.cn/login' });
44
+ });
45
+
46
+ it('没给平台名 → 用不带名字的文案(不猜平台)', () => {
47
+ const blocker = blockerOfError({ code: 'auth-required', message: '未登录' }, t);
48
+
49
+ expect(blocker.title).toBe('blocker.login.titleGeneric');
50
+ });
51
+
52
+ it('未登录但没有地址 → 不给点不动的按钮,退回终端指引', () => {
53
+ const blocker = blockerOfError({ code: 'auth-required', message: '未登录' }, t);
54
+
55
+ expect(blocker.actions.map((action) => action.kind)).toEqual(['retry']);
56
+ expect(blocker.hint).toBe('blocker.login.manual');
57
+ });
58
+
59
+ it('权限不足 → block,主动作「打开权限页」(地址同样来自信封)', () => {
60
+ const blocker = blockerOfError(
61
+ { code: 'permission-denied', message: '没有权限', helpUrl: 'https://x.coding.net/p/x/settings' },
62
+ t,
63
+ { platform: 'Coding' },
64
+ );
65
+
66
+ expect(blocker.id).toBe('permission-denied');
67
+ expect(blocker.severity).toBe('block');
68
+ expect(blocker.title).toContain('"platform":"Coding"');
69
+ expect(blocker.actions[0]).toMatchObject({ kind: 'open-url', url: 'https://x.coding.net/p/x/settings' });
70
+ });
71
+
72
+ it('权限不足但没有地址 → 只给重试(不编一个页面出来)', () => {
73
+ const blocker = blockerOfError({ code: 'permission-denied', message: '没有权限' }, t);
74
+
75
+ expect(blocker.actions.map((action) => action.kind)).toEqual(['retry']);
76
+ expect(blocker.title).toBe('blocker.forbidden.titleGeneric');
77
+ });
78
+
79
+ it('平台专属的("不是 X 仓库")**不归本表管** → 走兜底,由平台层覆盖', () => {
80
+ const blocker = blockerOfError({ code: 'project-unsupported', message: '这个目录没有可关联的远程仓库' }, t);
81
+
82
+ expect(blocker.title).toBe('这个目录没有可关联的远程仓库');
83
+ expect(blocker.actions.map((action) => action.kind)).toEqual(['retry']);
84
+ });
85
+
86
+ it('超时 → notice + 重试(不是"坏了")', () => {
87
+ const blocker = blockerOfError({ code: 'cli-timeout', message: '超时了' }, t);
88
+
89
+ expect(blocker.severity).toBe('notice');
90
+ expect(blocker.actions.map((action) => action.kind)).toEqual(['retry']);
91
+ });
92
+
93
+ it('未知错误 → 照原样说,并给重试', () => {
94
+ const blocker = blockerOfError({ code: 'weird', message: '怪错误' }, t);
95
+
96
+ expect(blocker.title).toBe('怪错误');
97
+ expect(blocker.actions.map((action) => action.kind)).toEqual(['retry']);
98
+ });
99
+
100
+ it('没有错误对象 → 兜底文案 + 重试(不炸)', () => {
101
+ expect(blockerOfError(undefined, t).title).toBe('blocker.unknown.title');
102
+ });
103
+ });
104
+
105
+ function capability(patch: Partial<CapabilityPayload>): CapabilityPayload {
106
+ return {
107
+ unit: 'issues',
108
+ status: 'ok',
109
+ required: [],
110
+ missing: [],
111
+ detail: '',
112
+ ...patch,
113
+ };
114
+ }
115
+
116
+ describe('blockerOfCapability', () => {
117
+ it('就绪 → 没有阻断', () => {
118
+ expect(blockerOfCapability(capability({}), t)).toBeUndefined();
119
+ });
120
+
121
+ it('就绪 → 没有阻断(**没有**"能凑合用但提示一下"的中间态了,见 ADR-0016)', () => {
122
+ expect(blockerOfCapability(capability({ status: 'ok', plugin: 'coding' }), t)).toBeUndefined();
123
+ });
124
+
125
+ it('★ 缺哪些命令只出现在 detail(排障区),标题里没有命令名', () => {
126
+ const blocker = blockerOfCapability(
127
+ capability({
128
+ status: 'missing-commands',
129
+ plugin: 'coding',
130
+ missing: ['issue-detail'],
131
+ detail: '1 份安装都在(缺必需:issue-detail)',
132
+ }),
133
+ t,
134
+ );
135
+
136
+ expect(blocker?.title).not.toContain('issue-detail');
137
+ expect(blocker?.detail).toContain('issue-detail');
138
+ });
139
+
140
+ it('降级那一档已经不存在(`ok` 就是没阻断)', () => {
141
+ expect(blockerOfCapability(capability({ status: 'ok', plugin: 'coding' }), t)).toBeUndefined();
142
+ });
143
+
144
+ it('★ 没装好 → 标题只说"这个模块还没装好",**不点名插件**(用户不认识插件)', () => {
145
+ const blocker = blockerOfCapability(
146
+ capability({
147
+ status: 'missing-plugin',
148
+ plugin: 'coding',
149
+ missing: ['issues'],
150
+ command: 'opencli plugin install <coding 插件源>',
151
+ }),
152
+ t,
153
+ );
154
+
155
+ expect(blocker?.id).toBe('unit-missing-plugin');
156
+ expect(blocker?.title).toBe('env.unit.missingPlugin');
157
+ expect(blocker?.title).not.toContain('coding');
158
+ expect(blocker?.hint).not.toContain('插件');
159
+ expect(blocker?.actions.map((action) => action.kind)).toEqual(['copy', 'retry']);
160
+ });
161
+
162
+ it('能一键修时,主动作是「帮我升级」并带上 { action, unit }(命令由宿主侧决定)', () => {
163
+ const blocker = blockerOfCapability(
164
+ capability({ status: 'missing-commands', plugin: 'coding', missing: ['issue-detail'], fix: 'update' }),
165
+ t,
166
+ );
167
+
168
+ expect(blocker?.actions[0]).toEqual({
169
+ kind: 'run-fix',
170
+ label: 'blocker.fix.update',
171
+ unit: 'issues',
172
+ fix: 'update',
173
+ });
174
+ });
175
+
176
+ it('★ 来源解析不出(宿主给了 install 但不是 ready)→ 给「去模块市场」,不给注定失败的按钮', () => {
177
+ for (const install of ['env-missing', 'not-declared', 'malformed'] as const) {
178
+ const blocker = blockerOfCapability(
179
+ capability({
180
+ status: 'missing-plugin',
181
+ plugin: 'coding',
182
+ command: 'opencli plugin install <coding 插件源>',
183
+ install,
184
+ }),
185
+ t,
186
+ );
187
+
188
+ expect(blocker?.actions.map((action) => action.kind)).toEqual(['open-market', 'retry']);
189
+ expect(blocker?.actions[0]).toMatchObject({ unit: 'issues' });
190
+ }
191
+ });
192
+
193
+ it('来源解析得出时仍然给「帮我安装」+ 复制命令(open-market 不该抢戏)', () => {
194
+ const blocker = blockerOfCapability(
195
+ capability({
196
+ status: 'missing-plugin',
197
+ plugin: 'coding',
198
+ command: 'opencli plugin install /some/path',
199
+ install: 'ready',
200
+ fix: 'install',
201
+ }),
202
+ t,
203
+ );
204
+
205
+ expect(blocker?.actions.map((action) => action.kind)).toEqual(['run-fix', 'copy', 'retry']);
206
+ });
207
+
208
+ it('缺插件且有来源 → 主动作是「帮我安装」', () => {
209
+ const blocker = blockerOfCapability(
210
+ capability({ status: 'missing-plugin', plugin: 'coding', fix: 'install' }),
211
+ t,
212
+ );
213
+
214
+ expect(blocker?.actions[0]?.kind).toBe('run-fix');
215
+ expect(blocker?.actions[0]?.label).toBe('blocker.fix.install');
216
+ expect(blocker?.actions[0]).toMatchObject({ fix: 'install', unit: 'issues' });
217
+ });
218
+
219
+ it('缺命令(版本旧)→ 也给「帮我升级」', () => {
220
+ const blocker = blockerOfCapability(
221
+ capability({ status: 'missing-commands', plugin: 'coding', missing: ['issue-detail'], fix: 'update' }),
222
+ t,
223
+ );
224
+
225
+ expect(blocker?.actions[0]?.kind).toBe('run-fix');
226
+ expect(blocker?.actions[0]?.label).toBe('blocker.fix.update');
227
+ });
228
+
229
+ it('缺命令 → 列出缺哪几个,并提示多半是版本旧', () => {
230
+ const blocker = blockerOfCapability(
231
+ capability({ status: 'missing-commands', plugin: 'coding', missing: ['issue-detail'] }),
232
+ t,
233
+ );
234
+
235
+ expect(blocker?.id).toBe('unit-missing-commands');
236
+ expect(blocker?.title).toBe('env.unit.missingCommands');
237
+ expect(blocker?.title).not.toContain('issue-detail');
238
+ expect(blocker?.hint).toBe('env.unit.missingCommandsHint');
239
+ });
240
+
241
+ it('探不动 → notice,**绝不**说成缺命令/缺插件,也不给"复制安装命令"', () => {
242
+ const blocker = blockerOfCapability(
243
+ capability({ status: 'unchecked', plugin: 'coding', missing: ['issues', 'issue-detail'], detail: '拿不到执行通道' }),
244
+ t,
245
+ );
246
+
247
+ expect(blocker?.id).toBe('unit-unchecked');
248
+ expect(blocker?.severity).toBe('notice');
249
+ expect(blocker?.title).toBe('env.unit.unchecked');
250
+ expect(blocker?.actions.map((action) => action.kind)).toEqual(['retry']);
251
+ expect(blocker?.title).not.toContain('issue-detail');
252
+ });
253
+ });
@@ -0,0 +1,274 @@
1
+ import { describe, expect, it, vi } from 'vitest';
2
+ import {
3
+ createFactChannel,
4
+ factOfRoute,
5
+ isFactScope,
6
+ parseFactFrame,
7
+ scopePrefixOf,
8
+ unitFactScope,
9
+ type FactFrame,
10
+ type FactTransport,
11
+ } from '../facts';
12
+
13
+ function fakeTransport(): {
14
+ transport: FactTransport;
15
+ push(frame: Partial<FactFrame> & { readonly facts: readonly string[] }): void;
16
+ starts: () => number;
17
+ stops: () => number;
18
+ } {
19
+ let onFrame: ((frame: FactFrame) => void) | null = null;
20
+ let starts = 0;
21
+ let stops = 0;
22
+ return {
23
+ starts: () => starts,
24
+ stops: () => stops,
25
+ push(frame) {
26
+ onFrame?.({ epoch: 'e1', rev: 1, ...frame });
27
+ },
28
+ transport: {
29
+ start(handlers) {
30
+ starts += 1;
31
+ onFrame = handlers.onFrame;
32
+ return () => {
33
+ stops += 1;
34
+ onFrame = null;
35
+ };
36
+ },
37
+ },
38
+ };
39
+ }
40
+
41
+ describe('createFactChannel', () => {
42
+ it('★ 建立起的第一帧只设基线,不惊动订阅者(挂载时那次取数已经包含它)', () => {
43
+ const wire = fakeTransport();
44
+ const channel = createFactChannel({ transport: wire.transport });
45
+ const listener = vi.fn();
46
+ channel.subscribe('capability', listener);
47
+
48
+ channel.applyFrame({ epoch: 'e1', rev: 7, facts: ['capability', 'panels'] });
49
+
50
+ expect(listener).not.toHaveBeenCalled();
51
+ expect(channel.revisionOf('capability')).toBeGreaterThan(0);
52
+ expect(channel.revisionOf('panels')).toBeGreaterThan(0);
53
+ });
54
+
55
+ it('★ 之后的帧只惊动它点名的事实(别的读者不该白跑一次)', () => {
56
+ const wire = fakeTransport();
57
+ const channel = createFactChannel({ transport: wire.transport });
58
+ const cmdb = vi.fn();
59
+ const issues = vi.fn();
60
+ channel.subscribe('cmdb/summary', cmdb);
61
+ channel.subscribe('issues', issues);
62
+ channel.applyFrame({ epoch: 'e1', rev: 1, facts: [] });
63
+
64
+ channel.applyFrame({ epoch: 'e1', rev: 2, facts: ['cmdb/summary'] });
65
+
66
+ expect(cmdb).toHaveBeenCalledTimes(1);
67
+ expect(issues).not.toHaveBeenCalled();
68
+ expect(channel.revisionOf('cmdb/summary')).toBe(2);
69
+ expect(channel.revisionOf('issues')).toBe(0);
70
+ });
71
+
72
+ it('★ 纪元变了(宿主热重载)→ 全部重读,哪怕修订号看着倒退了', () => {
73
+ const wire = fakeTransport();
74
+ const channel = createFactChannel({ transport: wire.transport });
75
+ const listener = vi.fn();
76
+ channel.subscribe('panels', listener);
77
+ channel.applyFrame({ epoch: 'e1', rev: 9, facts: [] });
78
+ const before = channel.revisionOf('panels');
79
+
80
+ channel.applyFrame({ epoch: 'e2', rev: 1, facts: [] });
81
+
82
+ expect(listener).toHaveBeenCalledTimes(1);
83
+ expect(channel.revisionOf('panels')).not.toBe(before);
84
+ });
85
+
86
+ it('★ refresh(fact) 与收到推送同一后果(用户按 ⟳)', () => {
87
+ const wire = fakeTransport();
88
+ const channel = createFactChannel({ transport: wire.transport });
89
+ const listener = vi.fn();
90
+ channel.subscribe('capability', listener);
91
+ channel.applyFrame({ epoch: 'e1', rev: 1, facts: [] });
92
+
93
+ channel.refresh('capability');
94
+
95
+ expect(listener).toHaveBeenCalledTimes(1);
96
+ expect(channel.revisionOf('capability')).toBeGreaterThan(1);
97
+ });
98
+
99
+ it('★ 最后一个订阅者走了就关传输;再订回来重新起(没人听时不占连接)', () => {
100
+ const wire = fakeTransport();
101
+ const channel = createFactChannel({ transport: wire.transport });
102
+ const off = channel.subscribe('panels', vi.fn());
103
+ const offOther = channel.subscribe('issues', vi.fn());
104
+
105
+ expect(wire.starts()).toBe(1);
106
+ off();
107
+ expect(wire.stops()).toBe(0);
108
+ offOther();
109
+ expect(wire.stops()).toBe(1);
110
+
111
+ const offAgain = channel.subscribe('panels', vi.fn());
112
+ expect(wire.starts()).toBe(2);
113
+ offAgain();
114
+ });
115
+
116
+ it('★ 重连后的首帧**必须惊动**(它装着断线期间的差量)—— 只有这一页的第一帧才按基线', () => {
117
+ const wire = fakeTransport();
118
+ const channel = createFactChannel({ transport: wire.transport });
119
+ const idle = vi.fn();
120
+ const first = channel.subscribe('panels', idle);
121
+ channel.applyFrame({ epoch: 'e1', rev: 3, facts: ['panels'] });
122
+ expect(idle).not.toHaveBeenCalled();
123
+
124
+ first();
125
+ const listener = vi.fn();
126
+ channel.subscribe('panels', listener);
127
+ channel.applyFrame({ epoch: 'e1', rev: 5, facts: ['panels'] });
128
+
129
+ expect(listener).toHaveBeenCalledTimes(1);
130
+ });
131
+
132
+ it('★ 点名一个命名空间 = 惊动它下面每一条已订阅的事实(装/更/卸之后,用它的模块一起重读)', () => {
133
+ const wire = fakeTransport();
134
+ const channel = createFactChannel({ transport: wire.transport });
135
+ const summary = vi.fn();
136
+ const builds = vi.fn();
137
+ const issues = vi.fn();
138
+ channel.subscribe('cmdb/summary', summary);
139
+ channel.subscribe('cmdb/builds?env=innettest', builds);
140
+ channel.subscribe('coding-issues/list', issues);
141
+ channel.applyFrame({ epoch: 'e1', rev: 1, facts: [] });
142
+
143
+ channel.applyFrame({ epoch: 'e1', rev: 2, facts: ['capability', 'panels', 'cmdb*'] });
144
+
145
+ expect(summary).toHaveBeenCalledTimes(1);
146
+ expect(builds).toHaveBeenCalledTimes(1);
147
+ expect(issues).not.toHaveBeenCalled();
148
+ expect(channel.revisionOf('cmdb/summary')).toBeGreaterThan(1);
149
+ });
150
+
151
+ it('命名空间不许凭空造出订阅(订阅表只由读者写),也不许越界到别的模块', () => {
152
+ const wire = fakeTransport();
153
+ const channel = createFactChannel({ transport: wire.transport });
154
+ const codingAnalysis = vi.fn();
155
+ const lookalike = vi.fn();
156
+ channel.subscribe('coding-analysis/summary', codingAnalysis);
157
+ channel.subscribe('cmdb-extra/summary', lookalike);
158
+ channel.applyFrame({ epoch: 'e1', rev: 1, facts: [] });
159
+
160
+ channel.applyFrame({ epoch: 'e1', rev: 2, facts: ['cmdb*'] });
161
+
162
+ expect(codingAnalysis).not.toHaveBeenCalled();
163
+ expect(lookalike).not.toHaveBeenCalled();
164
+ expect(channel.revisionOf('cmdb/never-subscribed')).toBe(0);
165
+ expect(channel.subscriberCount()).toBe(2);
166
+ });
167
+
168
+ it('★ 命名空间也走"第一帧只设基线"这条规则(重开页面不为历史那次动作白读一遍)', () => {
169
+ const wire = fakeTransport();
170
+ const channel = createFactChannel({ transport: wire.transport });
171
+ const listener = vi.fn();
172
+ channel.subscribe('cmdb/builds?env=innettest', listener);
173
+
174
+ channel.applyFrame({ epoch: 'e1', rev: 4, facts: ['cmdb*'] });
175
+
176
+ expect(listener).not.toHaveBeenCalled();
177
+ expect(channel.revisionOf('cmdb/builds?env=innettest')).toBeGreaterThan(0);
178
+ });
179
+
180
+ it('refresh(命名空间) 只惊动它下面的事实(本地失效也按模块点名,不是一律全量)', () => {
181
+ const wire = fakeTransport();
182
+ const channel = createFactChannel({ transport: wire.transport });
183
+ const cmdb = vi.fn();
184
+ const issues = vi.fn();
185
+ channel.subscribe('cmdb/summary', cmdb);
186
+ channel.subscribe('coding-issues/list', issues);
187
+ channel.applyFrame({ epoch: 'e1', rev: 1, facts: [] });
188
+
189
+ channel.refresh('cmdb*');
190
+
191
+ expect(cmdb).toHaveBeenCalledTimes(1);
192
+ expect(issues).not.toHaveBeenCalled();
193
+ });
194
+
195
+ it('传输把当前修订交回去(重连只要增量)', () => {
196
+ const seen: number[] = [];
197
+ const channel = createFactChannel({
198
+ transport: {
199
+ start(handlers) {
200
+ seen.push(handlers.cursor());
201
+ return () => {};
202
+ },
203
+ },
204
+ });
205
+ channel.applyFrame({ epoch: 'e1', rev: 4, facts: ['panels'] });
206
+ channel.subscribe('panels', vi.fn());
207
+
208
+ expect(seen).toEqual([4]);
209
+ });
210
+ });
211
+
212
+ describe('parseFactFrame', () => {
213
+ it('★ 推送帧没有纪元也认(宿主只在首帧带它)—— 认不出就是"拨了开关不亮"', () => {
214
+ expect(parseFactFrame('{"type":"bump","rev":12,"facts":["panels","capability"]}')).toEqual({
215
+ rev: 12,
216
+ facts: ['panels', 'capability'],
217
+ });
218
+ });
219
+
220
+ it('★ 缺纪元的帧照样惊动它点名的事实(推送这条路不能整条哑掉)', () => {
221
+ const wire = fakeTransport();
222
+ const channel = createFactChannel({ transport: wire.transport });
223
+ const listener = vi.fn();
224
+ channel.subscribe('panels', listener);
225
+ channel.applyFrame({ epoch: 'e1', rev: 10, facts: ['panels'] });
226
+ listener.mockClear();
227
+
228
+ channel.applyFrame({ rev: 11, facts: ['panels'] });
229
+
230
+ expect(listener).toHaveBeenCalledTimes(1);
231
+ expect(channel.revisionOf('panels')).toBeGreaterThan(0);
232
+ });
233
+
234
+ it('缺纪元 ≠ 换纪元(推送帧不该把"宿主重载过"误报成全量重读)', () => {
235
+ const wire = fakeTransport();
236
+ const channel = createFactChannel({ transport: wire.transport });
237
+ const listener = vi.fn();
238
+ channel.applyFrame({ epoch: 'e1', rev: 3, facts: [] });
239
+ channel.subscribe('panels', listener);
240
+
241
+ channel.applyFrame({ rev: 4, facts: [] });
242
+ channel.applyFrame({ epoch: 'e1', rev: 5, facts: [] });
243
+ expect(listener).not.toHaveBeenCalled();
244
+
245
+ channel.applyFrame({ epoch: 'e2', rev: 1, facts: [] });
246
+ expect(listener).toHaveBeenCalledTimes(1);
247
+ });
248
+
249
+ it('读不懂的帧丢掉(不猜):非字符串 / 坏 JSON / 没有修订号', () => {
250
+ expect(parseFactFrame(undefined)).toBeNull();
251
+ expect(parseFactFrame({ rev: 1, facts: [] })).toBeNull();
252
+ expect(parseFactFrame('{ 这不是 JSON')).toBeNull();
253
+ expect(parseFactFrame('{"type":"bump","facts":["panels"]}')).toBeNull();
254
+ expect(parseFactFrame('{"rev":2}')).toEqual({ rev: 2, facts: [] });
255
+ expect(parseFactFrame('{"rev":2,"facts":["panels",7]}')).toEqual({ rev: 2, facts: ['panels'] });
256
+ });
257
+ });
258
+
259
+ describe('factOfRoute', () => {
260
+ it('事实名就是路由名(不另起一套词汇,读与订阅不可能各说各话)', () => {
261
+ expect(factOfRoute('cmdb/summary')).toBe('cmdb/summary');
262
+ expect(factOfRoute('capability')).toBe('capability');
263
+ });
264
+ });
265
+
266
+ describe('unitFactScope', () => {
267
+ it('命名空间 = 模块 id + `*`("这个模块的数据全作废"只写一个字符串)', () => {
268
+ expect(unitFactScope('cmdb')).toBe('cmdb*');
269
+ expect(isFactScope('cmdb*')).toBe(true);
270
+ expect(isFactScope('*')).toBe(false);
271
+ expect(isFactScope('cmdb/summary')).toBe(false);
272
+ expect(scopePrefixOf('cmdb*')).toBe('cmdb');
273
+ });
274
+ });
@@ -0,0 +1,55 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { writeOutcomeOf, writeRoute } from '../host-write';
3
+
4
+ describe('writeRoute', () => {
5
+ it('三条通道各自拼得出路由(面板上的写只有这三条)', () => {
6
+ expect(writeRoute('prefs', 'cmdb')).toBe('prefs/cmdb');
7
+ expect(writeRoute('actions', 'cmdb-release')).toBe('actions/cmdb-release');
8
+ expect(writeRoute('provision', '')).toBe('provision');
9
+ });
10
+
11
+ it('provision 忽略资源名(它只发 {action, unit},没有资源段)', () => {
12
+ expect(writeRoute('provision', 'issues')).toBe('provision');
13
+ });
14
+ });
15
+
16
+ describe('writeOutcomeOf', () => {
17
+ it('成了 → 值原样交出', () => {
18
+ const outcome = writeOutcomeOf<{ text: string }>({ ok: true, text: '发出去了' }, '网络不通');
19
+
20
+ expect(outcome).toEqual({ ok: true, value: { ok: true, text: '发出去了' } });
21
+ });
22
+
23
+ it('★ 请求没到(null)→ 用调用方给的那句网络文案', () => {
24
+ expect(writeOutcomeOf(null, '网络不通')).toEqual({ ok: false, message: '网络不通' });
25
+ });
26
+
27
+ it('★ 宿主说失败 → 原样用宿主的话 + detail(不再包一层"操作失败")', () => {
28
+ const outcome = writeOutcomeOf(
29
+ { ok: false, error: { code: 'cmdb/release-blocked', message: '生产环境要先审批', detail: 'job 1234' } },
30
+ '网络不通',
31
+ );
32
+
33
+ expect(outcome).toEqual({ ok: false, message: '生产环境要先审批', detail: 'job 1234' });
34
+ });
35
+
36
+ it('没有 detail 就不给那个字段(别塞一个 undefined 进类型里)', () => {
37
+ const outcome = writeOutcomeOf({ ok: false, error: { code: 'x', message: '不行' } }, '网络不通');
38
+
39
+ expect(outcome).toEqual({ ok: false, message: '不行' });
40
+ expect(Object.keys(outcome)).not.toContain('detail');
41
+ });
42
+
43
+ it('★ 领域结果那一档(`{ok:false, text}`,没有 `error`)→ 用生产者自己那句话,**不抛异常**', () => {
44
+ const outcome = writeOutcomeOf(
45
+ { ok: false, text: 'innettest 没有发布配置(configs 查不到)' } as never,
46
+ '网络不通',
47
+ );
48
+
49
+ expect(outcome).toEqual({ ok: false, message: 'innettest 没有发布配置(configs 查不到)' });
50
+ });
51
+
52
+ it('★ 连话都没说(`{ok:false}`)→ 用调用方的兜底文案,还是不抛', () => {
53
+ expect(writeOutcomeOf({ ok: false } as never, '网络不通')).toEqual({ ok: false, message: '网络不通' });
54
+ });
55
+ });
@@ -0,0 +1,52 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { longTaskDot, longTaskPollMs, longTaskRunsOf, runsRouteOf } from '../long-task';
3
+
4
+ describe('runsRouteOf', () => {
5
+ it('★ 事实名 = 路由名(三处同一个来源,改一处不可能漏另一处)', () => {
6
+ expect(runsRouteOf('cmdb')).toBe('cmdb/runs');
7
+ expect(runsRouteOf('issues')).toBe('issues/runs');
8
+ });
9
+ });
10
+
11
+ describe('longTaskDot', () => {
12
+ it('★ 两个"还在等"的状态都转圈:等编排与等你登录是同一个信号', () => {
13
+ expect(longTaskDot('running')).toBe('ongoing');
14
+ expect(longTaskDot('needs-login')).toBe('ongoing');
15
+ expect(longTaskDot('ok')).toBe('done');
16
+ expect(longTaskDot('failed')).toBe('error');
17
+ });
18
+ });
19
+
20
+ describe('longTaskRunsOf(这一读交回的"当前那些趟")', () => {
21
+ const runs = [{ id: 'run-1', target: 'test', status: 'running' }];
22
+
23
+ it('读到了 → 原样交出(套件自己投影的形状,外壳不裁剪)', () => {
24
+ expect(longTaskRunsOf({ phase: 'ready', value: { runs } })).toEqual(runs);
25
+ });
26
+
27
+ it('★ 重取期间(`phase` 转 loading、但 `value` 还留着)→ **仍然交出那一份**', () => {
28
+ expect(longTaskRunsOf({ phase: 'loading', value: { runs } })).toEqual(runs);
29
+ expect(longTaskRunsOf({ phase: 'error', value: { runs } })).toEqual(runs);
30
+ });
31
+
32
+ it('还没读到过 / 读回来是空的 → 空数组(界面据此不画那一行)', () => {
33
+ expect(longTaskRunsOf({})).toEqual([]);
34
+ expect(longTaskRunsOf({ value: { runs: [] } })).toEqual([]);
35
+ });
36
+ });
37
+
38
+ describe('longTaskPollMs(轮询两档:还在跑要快,都收尾了也要问)', () => {
39
+ it('还在跑 → 快档(进度要跟手)', () => {
40
+ expect(longTaskPollMs(true)).toBe(1500);
41
+ });
42
+
43
+ it('★ 都收尾了 → **慢档但不停**(停了之后"别人起了一趟"就只剩推送那一条路)', () => {
44
+ expect(longTaskPollMs(false)).toBe(10_000);
45
+ expect(longTaskPollMs(false)).toBeLessThan(Number.POSITIVE_INFINITY);
46
+ });
47
+
48
+ it('调用方钉了值就用它(测试 / 特殊界面)', () => {
49
+ expect(longTaskPollMs(true, 50)).toBe(50);
50
+ expect(longTaskPollMs(false, 50)).toBe(50);
51
+ });
52
+ });