dsh-postapi 0.0.0-stage → 0.0.1

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 (75) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +139 -2
  3. package/cordis.patch.yml +3 -0
  4. package/dist/client.js +5651 -0
  5. package/docs/DETAIL.md +27 -0
  6. package/docs/apifox-design-spec.md +101 -0
  7. package/docs/apifox-research/analysis.md +254 -0
  8. package/docs/apifox-ui-delivery.md +150 -0
  9. package/docs/apifox-visual-parity-spec.md +353 -0
  10. package/docs/feature-matrix.md +112 -0
  11. package/docs/flow-reference/sequence-reference.html +163 -0
  12. package/docs/interface-contract-v2.md +175 -0
  13. package/docs/interface-contract.md +105 -0
  14. package/docs/nav-layout-v2-spec.md +107 -0
  15. package/docs/preview.svg +9 -0
  16. package/docs/probe-client.legacy.js +31 -0
  17. package/docs/rootcause-sidebar-vanish.md +95 -0
  18. package/docs/rootcause.md +86 -0
  19. package/docs/sidebar-notes.md +153 -0
  20. package/docs/ui-audit.md +197 -0
  21. package/docs/ui-notes.md +98 -0
  22. package/docs/v2-delivery.md +99 -0
  23. package/icon.svg +4 -0
  24. package/index.js +5 -0
  25. package/locale/en.json +7 -0
  26. package/locale/zh.json +7 -0
  27. package/package.json +73 -4
  28. package/src/client/index.js +132 -0
  29. package/src/client/jsx-shim.js +9 -0
  30. package/src/client/pages/__tests__/heights.test.js +135 -0
  31. package/src/client/pages/components/AssertTable.jsx +122 -0
  32. package/src/client/pages/components/BodyEditor.jsx +91 -0
  33. package/src/client/pages/components/EmptyState.jsx +52 -0
  34. package/src/client/pages/components/FolderTree.jsx +109 -0
  35. package/src/client/pages/components/JsonEditor.jsx +45 -0
  36. package/src/client/pages/components/ParamTable.jsx +226 -0
  37. package/src/client/pages/components/ResponsePanel.jsx +278 -0
  38. package/src/client/pages/components/ScriptPanel.jsx +99 -0
  39. package/src/client/pages/components/StatusPill.jsx +35 -0
  40. package/src/client/pages/components/__tests__/apifox-ui.test.js +291 -0
  41. package/src/client/pages/components/__tests__/pages-a.test.js +392 -0
  42. package/src/client/pages/components/__tests__/tree.test.js +379 -0
  43. package/src/client/pages/components/headerPresets.js +63 -0
  44. package/src/client/pages/debug.jsx +576 -0
  45. package/src/client/pages/design-tokens.css +2604 -0
  46. package/src/client/pages/doc.jsx +552 -0
  47. package/src/client/pages/env.jsx +378 -0
  48. package/src/client/pages/flow.jsx +349 -0
  49. package/src/client/pages/history.jsx +238 -0
  50. package/src/client/pages/import.jsx +396 -0
  51. package/src/client/pages/mock.jsx +316 -0
  52. package/src/client/pages/overview.jsx +348 -0
  53. package/src/client/pages/schema.jsx +317 -0
  54. package/src/client/pages/search.jsx +216 -0
  55. package/src/client/pages/sequence.jsx +344 -0
  56. package/src/client/pages/settings.jsx +205 -0
  57. package/src/client/pages/test.jsx +470 -0
  58. package/src/client/pages/theme.js +27 -0
  59. package/src/client/pages-entry.js +20 -0
  60. package/src/client/shell/ApiTitle.js +19 -0
  61. package/src/client/shell/Body.js +492 -0
  62. package/src/client/shell/definition.js +57 -0
  63. package/src/client/shell/locales.js +185 -0
  64. package/src/client/shell/menu.js +174 -0
  65. package/src/client/shell/registry.js +530 -0
  66. package/src/host/bridge.js +407 -0
  67. package/src/host/codegen.js +356 -0
  68. package/src/host/import-export.js +1044 -0
  69. package/src/host/index.js +102 -0
  70. package/src/host/mock-server.js +303 -0
  71. package/src/host/request.js +336 -0
  72. package/src/host/script-sandbox.js +272 -0
  73. package/src/host/storage.js +803 -0
  74. package/src/host/test-runner.js +126 -0
  75. package/src/host/variables.js +108 -0
@@ -0,0 +1,379 @@
1
+ // tree.test.js — FolderTree(多级分组树)静态纪律 + 行为断言
2
+ // 结构基线:docs/apifox-visual-parity-spec.md §4(左侧目录树实测)+ §12.3(类名契约)。
3
+ // 目录行 = switcher(.pp-tree-switcher) → 图标(.pp-tree-icon) → 名称(.pp-ellipsis) → 计数(.pp-tree-row-count);
4
+ // 接口行 = 方法徽标(.pp-method) → 名称,无 switcher/icon,靠 paddingLeft 负补偿与同层目录名左对齐。
5
+ // 形态照抄 apifox-ui.test.js:读源文本断言 + esbuild 打成 cjs 后用假 react 直接调组件函数,
6
+ // 断言返回的 element 树(不引 react-dom、不渲染)。
7
+ import { readFileSync, existsSync } from 'node:fs'
8
+ import { fileURLToPath } from 'node:url'
9
+ import { dirname, join, resolve } from 'node:path'
10
+ import { createRequire } from 'node:module'
11
+ import vm from 'node:vm'
12
+
13
+ const __dirname = dirname(fileURLToPath(import.meta.url))
14
+ const COMPONENTS_DIR = resolve(__dirname, '..')
15
+ const PAGES_DIR = resolve(COMPONENTS_DIR, '..')
16
+ const ROOT = resolve(PAGES_DIR, '..', '..', '..')
17
+
18
+ const FILE = {
19
+ tree: join(COMPONENTS_DIR, 'FolderTree.jsx'),
20
+ }
21
+
22
+ const read = (file) => (existsSync(file) ? readFileSync(file, 'utf8') : '')
23
+ const assert = (cond, message) => { if (!cond) throw new Error(message) }
24
+
25
+ /** 允许出现的类名:只限 design-tokens.css 已定义的 .pp-* 集合(本次不改样式表)。 */
26
+ const ALLOWED_CLASSES = new Set([
27
+ 'pp-tree', 'pp-tree-row', 'pp-tree-group', 'pp-tree-row-count',
28
+ 'pp-tree-switcher', 'pp-tree-icon',
29
+ 'pp-method', 'pp-ellipsis', 'pp-empty', 'pp-meta',
30
+ ])
31
+
32
+ const BANNED_APIS = [/\balert\s*\(/, /\bconfirm\s*\(/, /\bprompt\s*\(/]
33
+ const HARDCODED_COLOR = /#[0-9a-fA-F]{3}(?:[0-9a-fA-F]{3})?\b|\brgba?\s*\(|\bhsla?\s*\(/
34
+ const HARDCODED_FONT = /fontSize\s*:\s*['"?]?\d/
35
+
36
+ function loadEsbuild() {
37
+ for (const dir of [ROOT, join(ROOT, '.research', 'sidebar')]) {
38
+ if (existsSync(join(dir, 'node_modules', 'esbuild'))) {
39
+ return createRequire(join(dir, 'noop.js'))('esbuild')
40
+ }
41
+ }
42
+ throw new Error('esbuild 不可用(工作区与 .research/sidebar 均未安装)')
43
+ }
44
+
45
+ const fakeReact = {
46
+ createElement: (type, props, ...children) => ({ type, props, children }),
47
+ Fragment: 'Fragment',
48
+ useState: (v) => [typeof v === 'function' ? v() : v, () => {}],
49
+ useEffect: () => {},
50
+ useRef: (v) => ({ current: v }),
51
+ useMemo: (fn) => fn(),
52
+ useCallback: (fn) => fn(),
53
+ useLayoutEffect: () => {},
54
+ }
55
+
56
+ async function loadModule(file) {
57
+ const esbuild = loadEsbuild()
58
+ const built = await esbuild.build({
59
+ entryPoints: [file],
60
+ bundle: true,
61
+ write: false,
62
+ format: 'cjs',
63
+ platform: 'browser',
64
+ target: ['es2022'],
65
+ jsx: 'transform',
66
+ // 与 build.mjs 一致:classic runtime + 显式工厂(宿主只给 createElement)
67
+ jsxFactory: 'createElement',
68
+ jsxFragment: 'Fragment',
69
+ inject: [join(ROOT, 'src', 'client', 'jsx-shim.js')],
70
+ loader: { '.js': 'jsx' },
71
+ external: ['react'],
72
+ logLevel: 'silent',
73
+ })
74
+ const module = { exports: {} }
75
+ const sandbox = {
76
+ window: { __postapiQueue: [] },
77
+ module,
78
+ exports: module.exports,
79
+ console,
80
+ require: (name) => {
81
+ if (name === 'react') return fakeReact
82
+ throw new Error('意外的宿主 require: ' + name)
83
+ },
84
+ }
85
+ vm.createContext(sandbox)
86
+ vm.runInContext(built.outputFiles[0].text, sandbox, { filename: file })
87
+ return module.exports
88
+ }
89
+
90
+ /* ---------------- element 树工具 ---------------- */
91
+
92
+ /** JSX children 里 map 会产生嵌套数组,先摊平。 */
93
+ function flatten(children, out = []) {
94
+ for (const c of children) {
95
+ if (c == null || c === false || c === true) continue
96
+ if (Array.isArray(c)) flatten(c, out)
97
+ else out.push(c)
98
+ }
99
+ return out
100
+ }
101
+
102
+ /** 深度优先收集所有 element 节点(不含字符串)。 */
103
+ function collect(el, out = []) {
104
+ if (!el || typeof el !== 'object') return out
105
+ out.push(el)
106
+ for (const c of flatten(el.children || [])) {
107
+ if (c && typeof c === 'object') collect(c, out)
108
+ }
109
+ return out
110
+ }
111
+
112
+ /** 收集节点子树下所有文本片段。 */
113
+ function texts(el, out = []) {
114
+ if (el == null || el === false) return out
115
+ if (typeof el === 'string' || typeof el === 'number') { out.push(String(el)); return out }
116
+ if (Array.isArray(el)) { for (const c of el) texts(c, out); return out }
117
+ if (typeof el === 'object') texts(el.children || [], out)
118
+ return out
119
+ }
120
+
121
+ const cls = (el) => String((el && el.props && el.props.className) || '')
122
+
123
+ /** 组件节点(type 为函数,如 EmptyState):其 className 要调用后才可见。 */
124
+ const componentNodes = (root) => collect(root).filter((el) => typeof el.type === 'function')
125
+ const byClass = (root, name) => collect(root).filter((el) => cls(el).split(/\s+/).includes(name))
126
+ const allText = (root) => texts(root).join('|')
127
+
128
+ /** 目录行 = .pp-tree-row 且首个子节点是 .pp-tree-switcher 箭头 span(接口行没有 switcher)。 */
129
+ function folderRows(root) {
130
+ return byClass(root, 'pp-tree-row').filter((row) => {
131
+ const kids = flatten(row.children || [])
132
+ return kids.some((k) => k && typeof k === 'object' && cls(k) === 'pp-tree-switcher')
133
+ })
134
+ }
135
+ /** 接口行 = .pp-tree-row 且含 .pp-method 徽标。 */
136
+ function endpointRows(root) {
137
+ return byClass(root, 'pp-tree-row').filter((row) => {
138
+ const kids = flatten(row.children || [])
139
+ return kids.some((k) => k && typeof k === 'object' && cls(k) === 'pp-method')
140
+ })
141
+ }
142
+
143
+ /* ---------------- 固定夹具 ---------------- */
144
+
145
+ /** 三层目录:一级 → 二级 → 三级,接口挂最深一层 + 一个游离接口。 */
146
+ function deepProject() {
147
+ return {
148
+ id: 'proj_tree',
149
+ name: '订单中心',
150
+ folders: [
151
+ { id: 'f1', name: '一级', parentId: null, order: 0 },
152
+ { id: 'f2', name: '二级', parentId: 'f1', order: 0 },
153
+ { id: 'f3', name: '三级', parentId: 'f2', order: 0 },
154
+ ],
155
+ endpoints: [
156
+ { id: 'e_deep', name: '深层接口', method: 'GET', folderId: 'f3', order: 0 },
157
+ ],
158
+ }
159
+ }
160
+
161
+ export const tests = [
162
+ {
163
+ name: 'folder-tree: 只用既有 .pp-* 类名(不改 design-tokens.css)',
164
+ fn() {
165
+ const src = read(FILE.tree)
166
+ assert(src !== '', 'FolderTree.jsx 不存在')
167
+ const re = /className="([^"]*)"/g
168
+ let m
169
+ while ((m = re.exec(src))) {
170
+ for (const name of m[1].split(/\s+/).filter(Boolean)) {
171
+ assert(ALLOWED_CLASSES.has(name), 'FolderTree.jsx 使用了未定义的类名:' + name)
172
+ }
173
+ }
174
+ for (const need of ['pp-tree', 'pp-tree-row', 'pp-tree-group', 'pp-tree-row-count', 'pp-method', 'pp-ellipsis']) {
175
+ assert(src.includes(need), 'FolderTree.jsx 缺类名 ' + need)
176
+ }
177
+ // 展开箭头必须是文本 span,不能引图标库
178
+ assert(src.includes('▸') && src.includes('▾'), 'FolderTree.jsx 缺 ▸ / ▾ 展开箭头')
179
+ assert(!/from\s+['"](?!react|\.)[^'"]+['"]/.test(src), 'FolderTree.jsx 引入了非白名单模块(禁止新依赖)')
180
+ },
181
+ },
182
+ {
183
+ name: 'folder-tree: 无颜色/字号字面量,缩进走 var(--sp-*) 内联 layout',
184
+ fn() {
185
+ const src = read(FILE.tree)
186
+ assert(!HARDCODED_COLOR.test(src), 'FolderTree.jsx 出现颜色字面量(应用 var(--*) token)')
187
+ assert(!HARDCODED_FONT.test(src), 'FolderTree.jsx 出现字号字面量(应用 var(--fs-*) token)')
188
+ assert(/paddingLeft/.test(src), 'FolderTree.jsx 缩进应使用内联 paddingLeft')
189
+ assert(/var\(--sp-1\)/.test(src), 'FolderTree.jsx 缩进步长应为 var(--sp-1)')
190
+ for (const re of BANNED_APIS) assert(!re.test(src), 'FolderTree.jsx 使用了禁用 API(alert/confirm/prompt)')
191
+ },
192
+ },
193
+ {
194
+ name: 'folder-tree: 递归三层目录与接口全部渲染,缩进按深度递增',
195
+ async fn() {
196
+ const { FolderTree } = await loadModule(FILE.tree)
197
+ assert(typeof FolderTree === 'function', 'FolderTree.jsx 未导出 FolderTree')
198
+ const root = FolderTree({ project: deepProject(), activeEndpointId: '', onPick() {}, onToggle() {}, collapsed: {} })
199
+ const text = allText(root)
200
+ for (const name of ['一级', '二级', '三级', '深层接口']) {
201
+ assert(text.includes(name), '三层结构缺节点:' + name)
202
+ }
203
+ const rows = folderRows(root)
204
+ assert(rows.length === 3, '目录行应为 3 个,实际 ' + rows.length)
205
+ const pad = (row) => String((row.props.style && row.props.style.paddingLeft) || '')
206
+ assert(pad(rows[0]) === '', '顶层目录不应额外缩进,实际 ' + pad(rows[0]))
207
+ assert(pad(rows[1]).includes('* 1'), '二级目录缩进应为 1 个步长,实际 ' + pad(rows[1]))
208
+ assert(pad(rows[2]).includes('* 2'), '三级目录缩进应为 2 个步长,实际 ' + pad(rows[2]))
209
+ const eps = endpointRows(root)
210
+ assert(eps.length === 1, '接口行应为 1 个,实际 ' + eps.length)
211
+ // 接口行没有 switcher/icon 两格,按规格用负补偿与同层目录名左对齐(见用例「接口名与目录名左对齐」)
212
+ assert(String(eps[0].props.style.paddingLeft).includes('* 3'), '深层接口缩进应含 3 个步长基数')
213
+ assert(String(eps[0].props.style.paddingLeft).includes('- var(--sp-1)'), '接口行缩进应含 - var(--sp-1) 负补偿')
214
+ },
215
+ },
216
+ {
217
+ name: 'folder-tree: 接口行无 switcher/icon 占位,靠负补偿与同层目录名左对齐',
218
+ async fn() {
219
+ const { FolderTree } = await loadModule(FILE.tree)
220
+ const project = deepProject()
221
+ const root = FolderTree({ project, activeEndpointId: '', onPick() {}, onToggle() {}, collapsed: {} })
222
+ const ep = endpointRows(root)[0]
223
+ const kids = flatten(ep.children || []).filter((k) => k && typeof k === 'object')
224
+ assert(!kids.some((k) => cls(k) === 'pp-tree-switcher'), '接口行不应有 pp-tree-switcher')
225
+ assert(!kids.some((k) => cls(k) === 'pp-tree-icon'), '接口行不应留 pp-tree-icon 空白占位')
226
+ assert(kids.length === 2, '接口行应只有方法徽标 + 名称两格,实际 ' + kids.length)
227
+ // 深层接口挂在 f3 下:f3 目录行 depth=2(缩进 * 2),接口行 depth=3 且减一个步长 → 同为 * 2。
228
+ // 两边 paddingLeft 相等时,目录的 switcher(12+4+4) + icon(16) + 2 个 gap(4) = 44px 与
229
+ // 接口的 .pp-method min-width 44px 等宽,故名称左边缘严格对齐。
230
+ const folderPad = String(folderRows(root)[2].props.style.paddingLeft)
231
+ const epPad = String(ep.props.style.paddingLeft)
232
+ assert(folderPad === 'calc(var(--sp-1) * 2)', '三级目录缩进不符:' + folderPad)
233
+ assert(epPad === 'calc(var(--sp-1) * 3 - var(--sp-1))', '接口行缩进不符:' + epPad)
234
+ assert(epPad.replace(' * 3 - var(--sp-1)', ' * 2') === folderPad, '接口行与同层目录行缩进未对齐')
235
+ },
236
+ },
237
+ {
238
+ name: 'folder-tree: order 升序生效(目录与接口各自排序)',
239
+ async fn() {
240
+ const { FolderTree } = await loadModule(FILE.tree)
241
+ const project = {
242
+ name: '排序',
243
+ folders: [
244
+ { id: 'fb', name: '目录B', parentId: null, order: 2 },
245
+ { id: 'fa', name: '目录A', parentId: null, order: 0 },
246
+ { id: 'fc', name: '目录C', parentId: null, order: 1 },
247
+ ],
248
+ endpoints: [
249
+ { id: 'e3', name: '接口C', method: 'PUT', folderId: 'fa', order: 2 },
250
+ { id: 'e1', name: '接口A', method: 'GET', folderId: 'fa', order: 0 },
251
+ { id: 'e2', name: '接口B', method: 'POST', folderId: 'fa', order: 1 },
252
+ ],
253
+ }
254
+ const root = FolderTree({ project, activeEndpointId: '', onPick() {}, onToggle() {}, collapsed: {} })
255
+ const folderNames = folderRows(root).map((row) => texts(row).join(''))
256
+ const order = folderNames.map((t) => ['目录A', '目录B', '目录C'].find((n) => t.includes(n)))
257
+ assert(order.join(',') === '目录A,目录C,目录B', '目录未按 order 升序:' + order.join(','))
258
+ const epNames = endpointRows(root).map((row) => texts(row).join(''))
259
+ const epOrder = epNames.map((t) => ['接口A', '接口B', '接口C'].find((n) => t.includes(n)))
260
+ assert(epOrder.join(',') === '接口A,接口B,接口C', '接口未按 order 升序:' + epOrder.join(','))
261
+ },
262
+ },
263
+ {
264
+ name: 'folder-tree: 目录行显示接口数;折叠后子项不渲染',
265
+ async fn() {
266
+ const { FolderTree } = await loadModule(FILE.tree)
267
+ const project = deepProject()
268
+ const expanded = FolderTree({ project, activeEndpointId: '', onPick() {}, onToggle() {}, collapsed: {} })
269
+ const counts = byClass(expanded, 'pp-tree-row-count').map((el) => texts(el).join(''))
270
+ // 一级/二级自身无接口 → 0;三级挂 1 个
271
+ assert(counts.join(',') === '0,0,1', '目录接口数不正确:' + counts.join(','))
272
+ assert(allText(expanded).includes('深层接口'), '展开态应渲染深层接口')
273
+
274
+ const folded = FolderTree({ project, activeEndpointId: '', onPick() {}, onToggle() {}, collapsed: { f1: true } })
275
+ assert(!allText(folded).includes('二级'), '折叠一级后不应渲染二级目录')
276
+ assert(!allText(folded).includes('深层接口'), '折叠一级后不应渲染深层接口')
277
+ assert(allText(folded).includes('▸'), '折叠态箭头应为 ▸')
278
+ },
279
+ },
280
+ {
281
+ name: 'folder-tree: 未归类分组仅在存在游离接口时渲染(含悬挂 folderId)',
282
+ async fn() {
283
+ const { FolderTree } = await loadModule(FILE.tree)
284
+ const withLoose = deepProject()
285
+ withLoose.endpoints.push({ id: 'e_free', name: '游离接口', method: 'DELETE', folderId: null, order: 0 })
286
+ withLoose.endpoints.push({ id: 'e_orphan', name: '悬挂接口', method: 'PATCH', folderId: 'fld_gone', order: 1 })
287
+ const root = FolderTree({ project: withLoose, activeEndpointId: '', onPick() {}, onToggle() {}, collapsed: {} })
288
+ const groups = byClass(root, 'pp-tree-group').map((el) => texts(el).join(''))
289
+ assert(groups.includes('未归类'), '缺「未归类」分组:' + groups.join(','))
290
+ const looseText = allText(root)
291
+ assert(looseText.includes('游离接口'), '未归类分组缺 folderId=null 的接口')
292
+ assert(looseText.includes('悬挂接口'), '未归类分组应收容悬挂 folderId 的接口')
293
+
294
+ const clean = FolderTree({ project: deepProject(), activeEndpointId: '', onPick() {}, onToggle() {}, collapsed: {} })
295
+ const cleanGroups = byClass(clean, 'pp-tree-group').map((el) => texts(el).join(''))
296
+ assert(!cleanGroups.includes('未归类'), '无游离接口时不应渲染「未归类」分组')
297
+ },
298
+ },
299
+ {
300
+ name: 'folder-tree: 选中态 data-active 只落在目标接口行,onPick 回调收到 id',
301
+ async fn() {
302
+ const { FolderTree } = await loadModule(FILE.tree)
303
+ const picked = []
304
+ const project = deepProject()
305
+ project.endpoints.push({ id: 'e_other', name: '另一个接口', method: 'POST', folderId: 'f1', order: 1 })
306
+ const root = FolderTree({
307
+ project,
308
+ activeEndpointId: 'e_deep',
309
+ onPick: (id) => picked.push(id),
310
+ onToggle() {},
311
+ collapsed: {},
312
+ })
313
+ const active = endpointRows(root).filter((row) => row.props['data-active'] === true)
314
+ assert(active.length === 1, '选中行应恰好 1 个,实际 ' + active.length)
315
+ assert(texts(active[0]).join('').includes('深层接口'), '选中的不是目标接口')
316
+ const inactive = endpointRows(root).filter((row) => row.props['data-active'] === false)
317
+ assert(inactive.length === 1, '未选中行应显式 data-active=false,实际 ' + inactive.length)
318
+
319
+ active[0].props.onClick()
320
+ assert(picked.length === 1 && picked[0] === 'e_deep', 'onPick 未收到被点接口 id:' + JSON.stringify(picked))
321
+
322
+ // readOnly 下点击不触发 onPick
323
+ const ro = FolderTree({ project, activeEndpointId: '', onPick: (id) => picked.push(id), onToggle() {}, collapsed: {}, readOnly: true })
324
+ endpointRows(ro)[0].props.onClick()
325
+ assert(picked.length === 1, 'readOnly 下不应触发 onPick')
326
+ },
327
+ },
328
+ {
329
+ name: 'folder-tree: 空项目渲染空态;onToggle 收到目录 id',
330
+ async fn() {
331
+ const { FolderTree } = await loadModule(FILE.tree)
332
+ const empty = FolderTree({ project: { name: '空项目', folders: [], endpoints: [] }, activeEndpointId: '', onPick() {}, onToggle() {}, collapsed: {} })
333
+ const emptyNodes = componentNodes(empty)
334
+ assert(emptyNodes.length === 1, '空项目应渲染 1 个空态组件,实际 ' + emptyNodes.length)
335
+ assert(String(emptyNodes[0].props.text).includes('暂无目录与接口'), '空态文案缺失:' + JSON.stringify(emptyNodes[0].props))
336
+ const emptyDom = emptyNodes[0].type(emptyNodes[0].props)
337
+ assert(emptyDom.props.className === 'pp-empty', '空态根类不是 .pp-empty')
338
+ assert(folderRows(empty).length === 0, '空项目不应有目录行')
339
+
340
+ const toggled = []
341
+ const root = FolderTree({ project: deepProject(), activeEndpointId: '', onPick() {}, onToggle: (id) => toggled.push(id), collapsed: {} })
342
+ folderRows(root)[0].props.onClick()
343
+ assert(toggled.length === 1 && toggled[0] === 'f1', 'onToggle 未收到目录 id:' + JSON.stringify(toggled))
344
+
345
+ // project 为 null 时也不能炸(页面首帧 project 尚未加载)
346
+ const nullTree = FolderTree({ project: null, activeEndpointId: '', onPick() {}, onToggle() {}, collapsed: {} })
347
+ assert(componentNodes(nullTree).length === 1, 'project=null 应退化为空态')
348
+ },
349
+ },
350
+ ]
351
+
352
+ /* ---------------- mini runner(node 直跑本文件时生效) ---------------- */
353
+
354
+ const isMain = (() => {
355
+ try {
356
+ const argv1 = process.argv[1]
357
+ return Boolean(argv1) && resolve(argv1) === fileURLToPath(import.meta.url)
358
+ } catch {
359
+ return false
360
+ }
361
+ })()
362
+
363
+ if (isMain) {
364
+ let passed = 0
365
+ let failed = 0
366
+ for (const t of tests) {
367
+ try {
368
+ await t.fn()
369
+ passed += 1
370
+ console.log('PASS ' + t.name)
371
+ } catch (err) {
372
+ failed += 1
373
+ console.log('FAIL ' + t.name)
374
+ console.log(' ' + (err && err.message ? err.message : err))
375
+ }
376
+ }
377
+ console.log('\ntree.test:通过 ' + passed + ',失败 ' + failed + ',共 ' + (passed + failed))
378
+ process.exit(failed === 0 ? 0 : 1)
379
+ }
@@ -0,0 +1,63 @@
1
+ // headerPresets — 常用请求头清单与过滤函数
2
+ // debug 页 Headers 组的两处消费:ParamTable 的 suggest(下拉建议)与「+ 常用请求头」(按顺序补 3 条)。
3
+ // 纯数据 + 纯函数,不引 React、不碰样式,组件与测试都能直接 import。
4
+
5
+ /** 常用请求头清单:数组顺序即「+ 常用请求头」的追加顺序,filterHeaders 空查询取前 8 条。 */
6
+ export const COMMON_HEADERS = [
7
+ { key: 'Accept', value: 'application/json', description: '期望的响应内容类型' },
8
+ { key: 'Accept-Charset', value: 'utf-8', description: '可接受的字符集' },
9
+ { key: 'Accept-Encoding', value: 'gzip, deflate, br', description: '可接受的压缩编码' },
10
+ { key: 'Accept-Language', value: 'zh-CN,zh;q=0.9', description: '期望的响应语言' },
11
+ { key: 'Authorization', value: 'Bearer {{token}}', description: '认证凭证' },
12
+ { key: 'Cache-Control', value: 'no-cache', description: '缓存控制' },
13
+ { key: 'Content-Type', value: 'application/json', description: '请求体的媒体类型' },
14
+ { key: 'Cookie', value: '', description: 'Cookie 字符串' },
15
+ { key: 'If-None-Match', value: '', description: '条件请求(ETag)' },
16
+ { key: 'Origin', value: '', description: '请求来源域' },
17
+ { key: 'Referer', value: '', description: '来源页面地址' },
18
+ { key: 'User-Agent', value: 'PostAPI/0.1.0', description: '客户端标识' },
19
+ { key: 'X-Api-Key', value: '{{apiKey}}', description: 'API Key 认证' },
20
+ { key: 'X-Request-Id', value: '{{$uuid}}', description: '请求追踪 ID' },
21
+ { key: 'X-Forwarded-For', value: '', description: '代理链客户端 IP' },
22
+ ]
23
+
24
+ /** 建议下拉最多返回条数(与 Apifox 一致)。 */
25
+ const SUGGEST_LIMIT = 8
26
+
27
+ /** 统一归一:null/undefined 转空串、去首尾空白、转小写(大小写不敏感的比较基准)。 */
28
+ function norm(value) {
29
+ return String(value == null ? '' : value).trim().toLowerCase()
30
+ }
31
+
32
+ /** 复制一项:调用方拿到的永远是新对象,改写它不会污染 COMMON_HEADERS。 */
33
+ function copy(item) {
34
+ return { ...item }
35
+ }
36
+
37
+ /**
38
+ * 按关键字过滤常用请求头。
39
+ * - 大小写不敏感,关键字先去首尾空白;
40
+ * - 关键字为空时返回清单前 8 条;
41
+ * - 否则 key 前缀命中的排在包含命中之前,同组内保持清单原顺序,最多 8 条;
42
+ * - 始终返回新数组(元素也是副本),不会把 COMMON_HEADERS 本身交出去。
43
+ */
44
+ export function filterHeaders(query) {
45
+ const q = norm(query)
46
+ if (!q) return COMMON_HEADERS.slice(0, SUGGEST_LIMIT).map(copy)
47
+ const prefix = []
48
+ const contains = []
49
+ for (const item of COMMON_HEADERS) {
50
+ const key = norm(item.key)
51
+ const at = key.indexOf(q)
52
+ if (at === 0) prefix.push(item)
53
+ else if (at > 0) contains.push(item)
54
+ }
55
+ return prefix.concat(contains).slice(0, SUGGEST_LIMIT).map(copy)
56
+ }
57
+
58
+ /** 是否为常用请求头:大小写不敏感;空值/非字符串一律 false。 */
59
+ export function isCommonHeader(key) {
60
+ const k = norm(key)
61
+ if (!k) return false
62
+ return COMMON_HEADERS.some((item) => norm(item.key) === k)
63
+ }