dsh-session-webhook 0.0.0-stage → 0.2.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.
- package/LICENSE +21 -0
- package/README.md +412 -2
- package/cordis.patch.yml +8 -0
- package/docs/ACCEPTANCE.md +158 -0
- package/docs/DESIGN.md +136 -0
- package/docs/TOOL-CONTRACT.md +315 -0
- package/icon.svg +13 -0
- package/lib/bases.js +527 -0
- package/lib/client.js +1755 -0
- package/lib/control.js +321 -0
- package/lib/deliver.js +272 -0
- package/lib/errors.js +62 -0
- package/lib/http.js +258 -0
- package/lib/index.js +220 -0
- package/lib/store.js +381 -0
- package/lib/tools.js +433 -0
- package/lib/types.js +1 -0
- package/lib/urlfile.js +290 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +72 -4
- package/scripts/cold-resume-health.mjs +268 -0
- package/scripts/verify-client.mjs +1199 -0
- package/scripts/verify-health-contract.mjs +173 -0
- package/scripts/verify-host.mjs +1322 -0
|
@@ -0,0 +1,1199 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Offline verification for the Client half.
|
|
3
|
+
*
|
|
4
|
+
* Loads the real `client.js` into a fake browser module table, runs `apply()`
|
|
5
|
+
* against a stub Client context, and renders the registered components with a
|
|
6
|
+
* tiny React shim. It proves the bundle registers both seats, that the menu row
|
|
7
|
+
* opens the dialog, and that a row renders without throwing — not how it looks.
|
|
8
|
+
*
|
|
9
|
+
* node scripts/verify-client.mjs
|
|
10
|
+
*/
|
|
11
|
+
import assert from 'node:assert/strict';
|
|
12
|
+
import { readFile } from 'node:fs/promises';
|
|
13
|
+
import { fileURLToPath } from 'node:url';
|
|
14
|
+
import { dirname, join } from 'node:path';
|
|
15
|
+
import vm from 'node:vm';
|
|
16
|
+
|
|
17
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
18
|
+
const source = await readFile(join(here, '..', 'lib', 'client.js'), 'utf8');
|
|
19
|
+
|
|
20
|
+
let passed = 0;
|
|
21
|
+
const failures = [];
|
|
22
|
+
async function test(name, fn) {
|
|
23
|
+
try {
|
|
24
|
+
await fn();
|
|
25
|
+
passed += 1;
|
|
26
|
+
console.log(` ok ${name}`);
|
|
27
|
+
} catch (error) {
|
|
28
|
+
failures.push({ name, error });
|
|
29
|
+
console.log(`FAIL ${name}\n ${error?.stack ?? error}`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Minimal React: enough for a pure render, with hooks evaluated eagerly. */
|
|
34
|
+
const React = {
|
|
35
|
+
Fragment: Symbol('Fragment'),
|
|
36
|
+
createElement(type, props, ...children) {
|
|
37
|
+
const flat = children.length === 1 && Array.isArray(children[0]) ? children[0] : children;
|
|
38
|
+
return { type, props: props ?? {}, children: flat };
|
|
39
|
+
},
|
|
40
|
+
useState: (initial) => [typeof initial === 'function' ? initial() : initial, () => {}],
|
|
41
|
+
useEffect: (fn) => { try { const cleanup = fn(); if (typeof cleanup === 'function') { /* no re-run */ } } catch { /* render-time effects are best effort */ } },
|
|
42
|
+
// 纯渲染树测试没有 hook 运行时:useRef 返回一个可变盒子,useEffect 直接跑一次。
|
|
43
|
+
// 这足以让「新增后聚焦」这类接线被测到(focus 调用记录在盒子/桩上),
|
|
44
|
+
// 但**不**模拟重渲染语义——真正的聚焦行为由浏览器验收覆盖。
|
|
45
|
+
useRef: (initial) => ({ current: initial ?? null }),
|
|
46
|
+
useCallback: (fn) => fn,
|
|
47
|
+
useMemo: (fn) => fn(),
|
|
48
|
+
useSyncExternalStore: (_subscribe, getSnapshot) => getSnapshot(),
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/** 把 JSX automatic runtime 的 {type, props.children, key} 还原成 createElement 形态的节点树。 */
|
|
52
|
+
function fromJsx(type, props, key) {
|
|
53
|
+
const { children, ...rest } = props ?? {};
|
|
54
|
+
const withKey = key === undefined ? rest : { ...rest, key };
|
|
55
|
+
const kids = children === undefined ? [] : (Array.isArray(children) ? children : [children]);
|
|
56
|
+
return React.createElement(type, withKey, ...kids);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** react/jsx-runtime 的最小桩:宿主在 static module table 里提供它,测试里也必须有一份。 */
|
|
60
|
+
const jsxRuntime = {
|
|
61
|
+
Fragment: React.Fragment,
|
|
62
|
+
jsx: (type, props, key) => fromJsx(type, props, key),
|
|
63
|
+
jsxs: (type, props, key) => fromJsx(type, props, key),
|
|
64
|
+
jsxDEV: (type, props, key) => fromJsx(type, props, key),
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const modules = { react: React, 'react/jsx-runtime': jsxRuntime };
|
|
68
|
+
let loaded;
|
|
69
|
+
const styles = [];
|
|
70
|
+
const head = {
|
|
71
|
+
querySelector: () => null,
|
|
72
|
+
appendChild: (tag) => { styles.push(tag); return tag; },
|
|
73
|
+
};
|
|
74
|
+
const element = () => ({
|
|
75
|
+
style: {}, dataset: {}, textContent: '',
|
|
76
|
+
select() {}, remove() { const index = styles.indexOf(this); if (index >= 0) styles.splice(index, 1); },
|
|
77
|
+
});
|
|
78
|
+
const context = {
|
|
79
|
+
window: { __ModuleLoader__: { load: (entry) => { loaded = entry; } } },
|
|
80
|
+
document: { createElement: element, head, body: { appendChild() {}, removeChild() {} } },
|
|
81
|
+
navigator: {},
|
|
82
|
+
console,
|
|
83
|
+
setTimeout,
|
|
84
|
+
clearTimeout,
|
|
85
|
+
// 设置页用 `new URL()` 归一化入口地址;浏览器里有这个全局,桩上下文也得给。
|
|
86
|
+
URL,
|
|
87
|
+
};
|
|
88
|
+
context.globalThis = context;
|
|
89
|
+
vm.createContext(context);
|
|
90
|
+
vm.runInContext(source, context, { filename: 'client.js' });
|
|
91
|
+
|
|
92
|
+
await test('the bundle registers one lazy factory under its package id', async () => {
|
|
93
|
+
assert.ok(loaded, 'window.__ModuleLoader__.load was called');
|
|
94
|
+
assert.equal(loaded.id, 'dsh-session-webhook');
|
|
95
|
+
assert.equal(typeof loaded.factory, 'function');
|
|
96
|
+
// TSX automatic runtime 必须走宿主的 static module table(external),不能打进第二份 runtime
|
|
97
|
+
assert.match(source, /require\("react\/jsx-runtime"\)/, 'react/jsx-runtime is required from the host module table');
|
|
98
|
+
assert.doesNotMatch(source, /React\.createElement/, 'no manual createElement left over');
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
const plugin = loaded.factory((name) => {
|
|
102
|
+
if (Object.hasOwn(modules, name)) return modules[name];
|
|
103
|
+
throw new Error(`unexpected module require(${JSON.stringify(name)})`);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
/** 元素是否带某个 class(class 属性可能包含多个 token)。 */
|
|
107
|
+
function hasClass(node, className) {
|
|
108
|
+
const value = node?.props?.className;
|
|
109
|
+
return typeof value === 'string' && value.split(/\s+/).includes(className);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Find the first element carrying a class in a rendered tree(穿透 JSX 的嵌套 children 数组)。 */
|
|
113
|
+
function findByClass(node, className) {
|
|
114
|
+
if (node === null || node === undefined || typeof node !== 'object') return null;
|
|
115
|
+
if (Array.isArray(node)) {
|
|
116
|
+
for (const child of node) {
|
|
117
|
+
const found = findByClass(child, className);
|
|
118
|
+
if (found !== null) return found;
|
|
119
|
+
}
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
if (hasClass(node, className)) return node;
|
|
123
|
+
for (const child of node.children ?? []) {
|
|
124
|
+
const found = findByClass(child, className);
|
|
125
|
+
if (found !== null) return found;
|
|
126
|
+
}
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Find every element carrying a class in a rendered tree(同样穿透嵌套数组)。 */
|
|
131
|
+
function findAllByClass(node, className, out = []) {
|
|
132
|
+
if (node === null || node === undefined || typeof node !== 'object') return out;
|
|
133
|
+
if (Array.isArray(node)) {
|
|
134
|
+
for (const child of node) findAllByClass(child, className, out);
|
|
135
|
+
return out;
|
|
136
|
+
}
|
|
137
|
+
if (hasClass(node, className)) out.push(node);
|
|
138
|
+
for (const child of node.children ?? []) findAllByClass(child, className, out);
|
|
139
|
+
return out;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** 返回子树里第一个匹配 className 的类名(找不到返回 null)。 */
|
|
143
|
+
function findClassDeep(node) {
|
|
144
|
+
if (node === null || node === undefined || typeof node !== 'object') return null;
|
|
145
|
+
if (Array.isArray(node)) {
|
|
146
|
+
for (const child of node) {
|
|
147
|
+
const found = findClassDeep(child);
|
|
148
|
+
if (found !== null) return found;
|
|
149
|
+
}
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
if (typeof node.props?.className === 'string' && node.props.className.includes('si-num')) return 'si-num';
|
|
153
|
+
for (const child of node.children ?? []) {
|
|
154
|
+
const found = findClassDeep(child);
|
|
155
|
+
if (found !== null) return found;
|
|
156
|
+
}
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Flatten an element tree into visible text. */
|
|
161
|
+
function textOf(node) {
|
|
162
|
+
if (node === null || node === undefined || node === false) return '';
|
|
163
|
+
if (typeof node === 'string' || typeof node === 'number') return String(node);
|
|
164
|
+
if (Array.isArray(node)) return node.map(textOf).join('');
|
|
165
|
+
return textOf(node.children);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const rpcCalls = [];
|
|
169
|
+
let connectionStub;
|
|
170
|
+
const connectionStubHolder = { value: undefined };
|
|
171
|
+
|
|
172
|
+
await test('apply registers the menu row and the overlay dialog', async () => {
|
|
173
|
+
const registrations = [];
|
|
174
|
+
const injections = [];
|
|
175
|
+
const ctx = {
|
|
176
|
+
locale: {
|
|
177
|
+
bind: () => (key, params) => (params === undefined ? key : `${key} ${JSON.stringify(params)}`),
|
|
178
|
+
register: (namespace, dicts) => {
|
|
179
|
+
assert.equal(namespace, 'session-inject');
|
|
180
|
+
assert.ok(dicts.en && dicts.zh);
|
|
181
|
+
// 键集必须完全一致:任一语言缺键都会在界面上露成 key 名。
|
|
182
|
+
assert.deepEqual(Object.keys(dicts.en).sort(), Object.keys(dicts.zh).sort());
|
|
183
|
+
// 设置页与弹层新增文案确实合并进了同一个命名空间。
|
|
184
|
+
assert.ok(Object.hasOwn(dicts.zh, 'settings.nav'));
|
|
185
|
+
assert.ok(Object.hasOwn(dicts.zh, 'settings.savedEffective'));
|
|
186
|
+
assert.ok(Object.hasOwn(dicts.zh, 'dialog.createdBy.agent'));
|
|
187
|
+
return () => {};
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
effect: (fn) => { fn(); },
|
|
191
|
+
slots: {
|
|
192
|
+
inject: (slot, callback) => { injections.push(slot); const eff = callback(); registrations.push(eff); },
|
|
193
|
+
register: (spec, component) => ({ spec, component }),
|
|
194
|
+
},
|
|
195
|
+
connection: {
|
|
196
|
+
rpc: {
|
|
197
|
+
call: async (channel, endpoint, payload) => {
|
|
198
|
+
rpcCalls.push({ channel, endpoint, payload });
|
|
199
|
+
return { ok: true, value: { hooks: [] } };
|
|
200
|
+
},
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
get: (name) => (name === 'connection' ? connectionStubHolder.value : undefined),
|
|
204
|
+
};
|
|
205
|
+
plugin.apply(ctx);
|
|
206
|
+
connectionStubHolder.value = ctx.connection;
|
|
207
|
+
assert.deepEqual(injections, [
|
|
208
|
+
'sidebar.workspaces.session.menu.item',
|
|
209
|
+
'shell.overlay',
|
|
210
|
+
'settings.section',
|
|
211
|
+
]);
|
|
212
|
+
assert.equal(registrations.length, 3);
|
|
213
|
+
const [menu, overlay, settings] = registrations;
|
|
214
|
+
assert.equal(menu.spec.name, 'sidebar.workspaces.session.menu.item');
|
|
215
|
+
assert.equal(menu.spec.id, 'session-inject.webhook');
|
|
216
|
+
assert.equal(menu.spec.order, 460);
|
|
217
|
+
assert.equal(menu.spec.locale, 'session-inject');
|
|
218
|
+
assert.equal(overlay.spec.name, 'shell.overlay');
|
|
219
|
+
assert.equal(overlay.spec.id, 'session-inject.dialog');
|
|
220
|
+
|
|
221
|
+
// 整页设置:id 稳定、导航文案走 locale 命名空间、RPC 由注册时注入。
|
|
222
|
+
assert.equal(settings.spec.name, 'settings.section');
|
|
223
|
+
assert.equal(settings.spec.id, 'session-inject.settings');
|
|
224
|
+
assert.equal(settings.spec.order, 30);
|
|
225
|
+
assert.equal(settings.spec.locale, 'session-inject');
|
|
226
|
+
assert.equal(settings.spec.label(), 'settings.nav');
|
|
227
|
+
assert.equal(typeof settings.spec.inject().rpc, 'function');
|
|
228
|
+
assert.equal(settings.component, plugin.__test__.SettingsSection);
|
|
229
|
+
|
|
230
|
+
// The in-page regression this test exists for: reading ctx.connection as a
|
|
231
|
+
// property throws on a fiber that does not inject it. The dialog must reach
|
|
232
|
+
// the service through ctx.get() instead.
|
|
233
|
+
const injected = overlay.spec.inject();
|
|
234
|
+
const value = await injected.rpc('hooks/list', { sessionId: 'session-1' });
|
|
235
|
+
assert.equal(value.hooks.length, 0);
|
|
236
|
+
assert.equal(rpcCalls.length, 1);
|
|
237
|
+
assert.equal(rpcCalls[0].channel, '/api');
|
|
238
|
+
assert.equal(rpcCalls[0].endpoint, 'session-inject-rpc');
|
|
239
|
+
assert.equal(rpcCalls[0].payload.method, 'hooks/list');
|
|
240
|
+
assert.equal(rpcCalls[0].payload.payload.sessionId, 'session-1');
|
|
241
|
+
|
|
242
|
+
connectionStubHolder.value = undefined;
|
|
243
|
+
await assert.rejects(
|
|
244
|
+
() => injected.rpc('hooks/list', {}),
|
|
245
|
+
/browser Connection service is unavailable/,
|
|
246
|
+
);
|
|
247
|
+
connectionStubHolder.value = ctx.connection;
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
await test('apply installs the stylesheet once, outside the dialog', async () => {
|
|
251
|
+
assert.equal(styles.length, 1, 'exactly one stylesheet element');
|
|
252
|
+
const tag = styles[0];
|
|
253
|
+
assert.equal(tag.dataset.pluginCss, 'dsh-session-webhook/styles.css');
|
|
254
|
+
assert.match(tag.textContent, /\.si-menu-row\{/);
|
|
255
|
+
// The menu styling must not depend on the dialog being mounted.
|
|
256
|
+
assert.match(tag.textContent, /\.si-menu-row:focus-visible:not\(:disabled\)\{[^}]*outline:none/);
|
|
257
|
+
assert.match(tag.textContent, /gap:6px/);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
await test('the stylesheet is theme-token driven, with no literal colours', async () => {
|
|
261
|
+
const css = styles[0].textContent;
|
|
262
|
+
assert.doesNotMatch(css, /#[0-9a-fA-F]{3,8}\b/, 'no hex colours: they would ignore light/dark');
|
|
263
|
+
assert.doesNotMatch(css, /rgba?\(/, 'no rgb() literals');
|
|
264
|
+
// the modal surface and mask come from the host theme, not from this plugin
|
|
265
|
+
assert.match(css, /--dsw-radius-panel/);
|
|
266
|
+
assert.match(css, /--dsw-alias-bg-mask-1/);
|
|
267
|
+
assert.match(css, /--dsw-alias-bg-layer-2/);
|
|
268
|
+
assert.match(css, /--dsw-alias-button-primary-fill/);
|
|
269
|
+
// one control height keeps the create row aligned
|
|
270
|
+
const heights = [...css.matchAll(/height:(\d+)px/g)].map((m) => m[1]);
|
|
271
|
+
assert.ok(heights.includes('36'), 'controls share the 36px height');
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
await test('the menu row is a menuitem that closes the menu and opens the dialog', async () => {
|
|
275
|
+
let closed = false;
|
|
276
|
+
let opened = null;
|
|
277
|
+
const element = plugin.__test__.WebhookMenuItem({
|
|
278
|
+
sessionId: 'session-1',
|
|
279
|
+
displayTitle: '排查会话',
|
|
280
|
+
t: (key) => key,
|
|
281
|
+
useMenuOpenState: () => [true, (value) => { closed = value === false; }],
|
|
282
|
+
openWebhooks: (sessionId, title) => { opened = { sessionId, title }; },
|
|
283
|
+
});
|
|
284
|
+
assert.equal(element.type, 'button');
|
|
285
|
+
assert.equal(element.props.role, 'menuitem');
|
|
286
|
+
assert.equal(element.children.length, 2);
|
|
287
|
+
assert.equal(element.children[0].props.className, 'si-menu-icon');
|
|
288
|
+
assert.equal(element.children[1].props.className, 'si-menu-label');
|
|
289
|
+
element.props.onClick();
|
|
290
|
+
assert.equal(closed, true);
|
|
291
|
+
assert.deepEqual(opened, { sessionId: 'session-1', title: '排查会话' });
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
await test('the dialog renders nothing while closed', async () => {
|
|
295
|
+
const element = plugin.__test__.WebhookDialog({ t: (key) => key, rpc: async () => ({ hooks: [] }) });
|
|
296
|
+
assert.equal(element, null);
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
await test('one endpoint row renders its name, tags, mode, and masked URL', async () => {
|
|
300
|
+
const element = plugin.__test__.Row({
|
|
301
|
+
t: (key) => key,
|
|
302
|
+
hook: {
|
|
303
|
+
id: 'wh_1', name: 'github-notify', tags: ['github', 'notify'], defaultMode: 'steer',
|
|
304
|
+
enabled: true, maskedUrl: 'si_abcd…wxyz', createdAt: '2026-10-27T03:00:00.000Z',
|
|
305
|
+
lastUsedAt: null, lastStatus: null, deliveryCount: 0,
|
|
306
|
+
},
|
|
307
|
+
busy: false, copied: null, revealed: {}, confirming: null, renaming: null,
|
|
308
|
+
renameValue: '', setRenameValue: () => {},
|
|
309
|
+
onReveal: () => {}, onCopy: () => {}, onToggle: () => {}, onAskRevoke: () => {},
|
|
310
|
+
onCancelRevoke: () => {}, onRevoke: () => {}, onStartRename: () => {},
|
|
311
|
+
onCancelRename: () => {}, onRename: () => {},
|
|
312
|
+
});
|
|
313
|
+
// Two columns: the content grows, the actions sit at the right edge.
|
|
314
|
+
assert.equal(element.props.className, 'si-row');
|
|
315
|
+
const main = findByClass(element, 'si-row-main');
|
|
316
|
+
const actions = findByClass(element, 'si-row-actions');
|
|
317
|
+
assert.ok(main, 'content column rendered');
|
|
318
|
+
assert.ok(actions, 'actions column rendered');
|
|
319
|
+
assert.equal(textOf(main).includes('改名'), false, 'buttons left the content column');
|
|
320
|
+
const labels = actions.children.map((button) => button.children[0]);
|
|
321
|
+
assert.equal(labels.length, 4);
|
|
322
|
+
assert.equal(labels[0], 'dialog.rename');
|
|
323
|
+
assert.equal(labels[1], 'dialog.reveal');
|
|
324
|
+
assert.equal(labels[2], 'dialog.disable');
|
|
325
|
+
assert.equal(labels[3], 'dialog.revoke');
|
|
326
|
+
const text = textOf(element);
|
|
327
|
+
assert.match(text, /github-notify/);
|
|
328
|
+
assert.match(text, /github/);
|
|
329
|
+
assert.match(text, /notify/);
|
|
330
|
+
assert.match(text, /mode\.steer\.label/);
|
|
331
|
+
assert.match(text, /mode\.steer\.hint/, 'the badge carries its explanation as a tooltip');
|
|
332
|
+
assert.match(text, /si_abcd…wxyz/);
|
|
333
|
+
assert.match(text, /dialog\.neverUsed/);
|
|
334
|
+
assert.doesNotMatch(text, /undefined/);
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
await test('a revealed row shows the full URL and the copy action', async () => {
|
|
338
|
+
const element = plugin.__test__.Row({
|
|
339
|
+
t: (key) => key,
|
|
340
|
+
hook: {
|
|
341
|
+
id: 'wh_1', name: 'ci', tags: [], defaultMode: 'queue',
|
|
342
|
+
enabled: false, maskedUrl: 'si_1111…2222', lastUsedAt: '2026-10-27T03:00:00.000Z',
|
|
343
|
+
lastStatus: 'delivered', deliveryCount: 3,
|
|
344
|
+
},
|
|
345
|
+
busy: false, copied: 'wh_1', revealed: { wh_1: 'http://host/api/session-inject/hooks/si_secret' },
|
|
346
|
+
confirming: null, renaming: null, renameValue: '', setRenameValue: () => {},
|
|
347
|
+
onReveal: () => {}, onCopy: () => {}, onToggle: () => {}, onAskRevoke: () => {},
|
|
348
|
+
onCancelRevoke: () => {}, onRevoke: () => {}, onStartRename: () => {},
|
|
349
|
+
onCancelRename: () => {}, onRename: () => {},
|
|
350
|
+
});
|
|
351
|
+
const text = textOf(element);
|
|
352
|
+
assert.match(text, /http:\/\/host\/api\/session-inject\/hooks\/si_secret/);
|
|
353
|
+
assert.match(text, /dialog\.copied/);
|
|
354
|
+
assert.match(text, /dialog\.disabled/);
|
|
355
|
+
assert.match(text, /dialog\.statusDelivered/);
|
|
356
|
+
assert.match(text, /dialog\.deliveryCount/);
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
await test('the panel renders the empty state and the security note', async () => {
|
|
360
|
+
let requested = null;
|
|
361
|
+
const rpc = async (method, payload) => {
|
|
362
|
+
requested = { method, payload };
|
|
363
|
+
return { hooks: [] };
|
|
364
|
+
};
|
|
365
|
+
const element = plugin.__test__.WebhookPanel({
|
|
366
|
+
t: (key) => key, rpc, sessionId: 'session-1', displayTitle: '排查会话', onClose: () => {},
|
|
367
|
+
});
|
|
368
|
+
const text = textOf(element);
|
|
369
|
+
assert.match(text, /dialog\.title/);
|
|
370
|
+
assert.match(text, /dialog\.securityNote/);
|
|
371
|
+
// the three-way picker: one button per mode, the current one pressed, each with a tooltip
|
|
372
|
+
const modes = findByClass(element, 'si-modes');
|
|
373
|
+
assert.ok(modes, 'mode group rendered');
|
|
374
|
+
const chips = modes.children;
|
|
375
|
+
assert.equal(chips.length, 3);
|
|
376
|
+
// Cross-realm arrays never satisfy deepEqual; compare element by element.
|
|
377
|
+
assert.equal(chips[0].children[0].props['aria-pressed'], true);
|
|
378
|
+
assert.equal(chips[1].children[0].props['aria-pressed'], false);
|
|
379
|
+
assert.equal(chips[2].children[0].props['aria-pressed'], false);
|
|
380
|
+
for (const chip of chips) {
|
|
381
|
+
const tip = chip.children[1];
|
|
382
|
+
assert.equal(tip.props.className, 'si-tip');
|
|
383
|
+
assert.equal(tip.props.role, 'tooltip');
|
|
384
|
+
assert.match(tip.children[0], /^mode\.(steer|queue|passive)\.hint$/);
|
|
385
|
+
}
|
|
386
|
+
// localized labels, not the wire values
|
|
387
|
+
assert.equal(chips[0].children[0].children[0], 'mode.steer.label');
|
|
388
|
+
assert.equal(chips[1].children[0].children[0], 'mode.queue.label');
|
|
389
|
+
assert.equal(chips[2].children[0].children[0], 'mode.passive.label');
|
|
390
|
+
assert.equal(requested.method, 'hooks/list');
|
|
391
|
+
// Cross-realm objects never satisfy deepEqual; compare the wire fields.
|
|
392
|
+
assert.equal(requested.payload.sessionId, 'session-1');
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
// ── 设置页 / 新增弹层字段 / RPC 边界(T4) ───────────────────────────────────
|
|
397
|
+
|
|
398
|
+
await test('save is disabled until the draft differs from the effective config', async () => {
|
|
399
|
+
const snapshot = {
|
|
400
|
+
yaml: { publicBaseUrls: {}, limits: {} },
|
|
401
|
+
settings: {},
|
|
402
|
+
effective: {
|
|
403
|
+
publicBaseUrls: { direct: 'http://127.0.0.1:3080' },
|
|
404
|
+
defaultBase: 'direct',
|
|
405
|
+
limits: { maxBodyBytes: 1048576, maxTextBytes: 65536, ratePerMinute: 60 },
|
|
406
|
+
},
|
|
407
|
+
directOrigin: 'http://127.0.0.1:3080',
|
|
408
|
+
basePath: '/api/session-inject',
|
|
409
|
+
};
|
|
410
|
+
const pristine = plugin.__test__.draftFromConfig(snapshot);
|
|
411
|
+
// 原样草稿 = 不脏
|
|
412
|
+
assert.equal(plugin.__test__.isDraftDirty(pristine, snapshot), false);
|
|
413
|
+
// 改任意一项 = 脏
|
|
414
|
+
const cases = [
|
|
415
|
+
['限额', (d) => ({ ...d, ratePerMinute: '61' })],
|
|
416
|
+
['默认入口', (d) => ({ ...d, defaultBase: 'direct' })],
|
|
417
|
+
];
|
|
418
|
+
assert.equal(plugin.__test__.isDraftDirty(cases[0][1](pristine), snapshot), true);
|
|
419
|
+
// 加一行 = 脏
|
|
420
|
+
assert.equal(plugin.__test__.isDraftDirty(plugin.__test__.addBaseRow(pristine), snapshot), true);
|
|
421
|
+
// 去掉 direct 的 URL(direct 的生效值被清空)= 脏
|
|
422
|
+
const blankDirect = { ...pristine, rows: pristine.rows.map((row) => ({ ...row, url: '' })) };
|
|
423
|
+
assert.equal(plugin.__test__.isDraftDirty(blankDirect, snapshot), true);
|
|
424
|
+
// 改名又改回 = 不脏(用 edited 标记就会误判成脏)
|
|
425
|
+
let back = plugin.__test__.updateBaseRow(pristine, pristine.rows[0].id, { url: 'http://x' });
|
|
426
|
+
back = plugin.__test__.updateBaseRow(back, pristine.rows[0].id, { url: pristine.rows[0].url });
|
|
427
|
+
assert.equal(plugin.__test__.isDraftDirty(back, snapshot), false);
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
await test('the settings page matches the host control recipes (QA P2 batch)', async () => {
|
|
431
|
+
const css = styles[0].textContent;
|
|
432
|
+
|
|
433
|
+
// P2-5 主按钮 hover 是变亮,不是降透明度变暗
|
|
434
|
+
assert.match(css, /\.si-btn-primary:hover:not\(:disabled\)\{[^}]*--dsw-alias-button-primary-hover/);
|
|
435
|
+
// hover 时把 opacity 复位成 1 是可以的;禁止的是「靠降透明度变暗」那种写法
|
|
436
|
+
assert.doesNotMatch(css, /\.si-btn-primary:hover:not\(:disabled\)\{[^}]*opacity:\.\d/);
|
|
437
|
+
|
|
438
|
+
// P2-6 按钮焦点环走 box-shadow,不用 UA outline
|
|
439
|
+
assert.match(css, /\.si-btn:focus-visible\{[^}]*outline:none/);
|
|
440
|
+
assert.match(css, /\.si-btn:focus-visible\{[^}]*box-shadow:0 0 0 2px/);
|
|
441
|
+
|
|
442
|
+
// P2-7 行内危险按钮无描边
|
|
443
|
+
assert.match(css, /\.si-btn-danger\{[^}]*border:none/);
|
|
444
|
+
|
|
445
|
+
// P2-3 组标题:12/600/tertiary + letter-spacing(对齐宿主 Agent 预设的分组标题)
|
|
446
|
+
const groupHead = css.match(/\.si-group-head\{([^}]*)\}/)[1];
|
|
447
|
+
assert.match(groupHead, /font-size:12px/);
|
|
448
|
+
assert.match(groupHead, /color:var\(--dsw-alias-label-tertiary\)/);
|
|
449
|
+
assert.match(groupHead, /letter-spacing:\.06em/);
|
|
450
|
+
|
|
451
|
+
// P2-8 只读输入框外观要和可编辑区分开
|
|
452
|
+
assert.match(css, /\.si-input\[readonly\]\{[^}]*cursor:default/);
|
|
453
|
+
|
|
454
|
+
// P2-11 保存条吸底
|
|
455
|
+
assert.match(css, /\.si-page-actions\{[^}]*position:sticky/);
|
|
456
|
+
assert.match(css, /\.si-page-actions\{[^}]*bottom:0/);
|
|
457
|
+
|
|
458
|
+
// P2-12 入口名 160px
|
|
459
|
+
assert.match(css, /\.si-base-name\{[^}]*flex:0 0 160px/);
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
await test('limit rows join range and source into one sentence without a stray full stop', async () => {
|
|
463
|
+
// P2-4:`范围 1024–67108864 · 继承自 YAML: 1048576`
|
|
464
|
+
// 中文 help 不能自带句号(否则拼出来是「。·」),source 用半角冒号。
|
|
465
|
+
const zh = plugin.__test__.SETTINGS_LOCALE.zh ?? plugin.__test__.SETTINGS_LOCALE['zh-CN'];
|
|
466
|
+
const help = zh['settings.limits.help'];
|
|
467
|
+
assert.doesNotMatch(help, /。/, 'help 不该带句号:它会和来源提示拼成一句');
|
|
468
|
+
assert.match(help, /范围 \{min\}–\{max\}/);
|
|
469
|
+
assert.equal(zh['settings.source.yaml'], '继承自 YAML: {yaml}');
|
|
470
|
+
assert.match(zh['settings.source.settingsOverYaml'], /界面覆盖 · YAML: \{yaml\}/);
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
await test('adding a row focuses the new row instead of leaving focus on the button', async () => {
|
|
474
|
+
// P2-9:点「添加入口」后焦点要落到新行的名称输入框。
|
|
475
|
+
const snapshot = {
|
|
476
|
+
yaml: { publicBaseUrls: {}, limits: {} },
|
|
477
|
+
settings: {},
|
|
478
|
+
effective: {
|
|
479
|
+
publicBaseUrls: { direct: 'http://127.0.0.1:3080' },
|
|
480
|
+
defaultBase: 'direct',
|
|
481
|
+
limits: { maxBodyBytes: 1048576, maxTextBytes: 65536, ratePerMinute: 60 },
|
|
482
|
+
},
|
|
483
|
+
directOrigin: 'http://127.0.0.1:3080',
|
|
484
|
+
basePath: '/api/session-inject',
|
|
485
|
+
};
|
|
486
|
+
const before = plugin.__test__.draftFromConfig(snapshot);
|
|
487
|
+
const after = plugin.__test__.addBaseRow(before);
|
|
488
|
+
// 新行 id 必须能被取到(面板据此挂 ref)
|
|
489
|
+
const id = plugin.__test__.lastRowId(after);
|
|
490
|
+
assert.ok(typeof id === 'string' && id.length > 0);
|
|
491
|
+
assert.notEqual(id, plugin.__test__.lastRowId(before));
|
|
492
|
+
assert.equal(after.rows[after.rows.length - 1].id, id);
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
await test('the add-entry button carries a plus icon', async () => {
|
|
496
|
+
// P2-2:纯文字的全宽虚线按钮会被读成输入框;宿主同类按钮都带图标。
|
|
497
|
+
const snapshot = {
|
|
498
|
+
yaml: { publicBaseUrls: {}, limits: {} },
|
|
499
|
+
settings: {},
|
|
500
|
+
effective: {
|
|
501
|
+
publicBaseUrls: { direct: 'http://127.0.0.1:3080' },
|
|
502
|
+
defaultBase: 'direct',
|
|
503
|
+
limits: { maxBodyBytes: 1048576, maxTextBytes: 65536, ratePerMinute: 60 },
|
|
504
|
+
},
|
|
505
|
+
directOrigin: 'http://127.0.0.1:3080',
|
|
506
|
+
basePath: '/api/session-inject',
|
|
507
|
+
};
|
|
508
|
+
const element = plugin.__test__.SettingsPanel({
|
|
509
|
+
t: (key, params) => (params === undefined ? key : `${key} ${JSON.stringify(params)}`),
|
|
510
|
+
rpc: async () => ({}),
|
|
511
|
+
snapshot,
|
|
512
|
+
draft: plugin.__test__.draftFromConfig(snapshot),
|
|
513
|
+
errors: [],
|
|
514
|
+
notice: null,
|
|
515
|
+
busy: false,
|
|
516
|
+
onChangeDraft: () => {},
|
|
517
|
+
onSave: () => {},
|
|
518
|
+
onReset: async () => {},
|
|
519
|
+
});
|
|
520
|
+
// 找到「添加入口」按钮,确认它带有 aria-hidden 的 + 图标
|
|
521
|
+
const findAdd = (node) => {
|
|
522
|
+
if (node === null || typeof node !== 'object' || Array.isArray(node)) return null;
|
|
523
|
+
if (node.props?.className === 'si-add') return node;
|
|
524
|
+
for (const child of node.children ?? []) {
|
|
525
|
+
const found = findAdd(child);
|
|
526
|
+
if (found !== null) return found;
|
|
527
|
+
}
|
|
528
|
+
return null;
|
|
529
|
+
};
|
|
530
|
+
const addButton = findAdd(element);
|
|
531
|
+
assert.ok(addButton, '存在「添加入口」按钮');
|
|
532
|
+
const icon = (addButton.children ?? []).find(
|
|
533
|
+
(child) => typeof child === 'object' && child !== null && child.props?.['aria-hidden'] === 'true');
|
|
534
|
+
assert.ok(icon, '按钮里应有 aria-hidden 的 + 图标');
|
|
535
|
+
assert.equal(textOf(icon), '+');
|
|
536
|
+
});
|
|
537
|
+
|
|
538
|
+
await test('no two rule blocks share a selector (class-name collision guard)', async () => {
|
|
539
|
+
// 这条守的是 QA 实测到的 P0 回归:.si-row 同时被「弹层列表行」和「设置页行」定义,
|
|
540
|
+
// 后定义的 padding 把先定义的覆盖掉,弹层行内边距塌成 0。
|
|
541
|
+
// 渲染树层面的断言看不见 CSS 撞车,所以必须直接扫样式表。
|
|
542
|
+
const css = styles[0].textContent.replace(/\/\*[\s\S]*?\*\//g, '');
|
|
543
|
+
// 允许同一个选择器分块追加(例如 .si-input 先落基线、再补 padding)——那是正常的分层写法。
|
|
544
|
+
// 真正危险的是**布局属性被另一条同名规则改写**(padding/margin/display/height),
|
|
545
|
+
// 因为它会被静默覆盖且很难从渲染结果反推。这里只对这几个属性做撞车检查。
|
|
546
|
+
const LAYOUT = /(^|;)\s*(padding|margin|display|height|flex)(-[a-z]+)?\s*:/;
|
|
547
|
+
const seen = new Map();
|
|
548
|
+
for (const rule of css.matchAll(/([^{}]+)\{([^{}]*)\}/g)) {
|
|
549
|
+
const body = rule[2];
|
|
550
|
+
for (const raw of rule[1].split(',')) {
|
|
551
|
+
const sel = raw.trim();
|
|
552
|
+
if (sel === '' || sel.includes('@') || sel.includes(':') || sel.includes(' ')) continue;
|
|
553
|
+
if (!/^\.-?[_a-zA-Z][\w-]*(\.[_a-zA-Z][\w-]*)*$/.test(sel)) continue;
|
|
554
|
+
const props = [...body.matchAll(/(^|;)\s*([a-z-]+)\s*:/g)].map((m) => m[2]);
|
|
555
|
+
for (const prop of props) {
|
|
556
|
+
if (!LAYOUT.test(';' + prop + ':')) continue;
|
|
557
|
+
const key = `${sel}{${prop}}`;
|
|
558
|
+
if (seen.has(key)) {
|
|
559
|
+
throw new Error(`选择器 ${sel} 的 ${prop} 被定义了两次——`
|
|
560
|
+
+ '后定义会静默覆盖前定义,这正是 .si-row 撞车的成因');
|
|
561
|
+
}
|
|
562
|
+
seen.set(key, true);
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
// 反向确认:弹层行与设置页行必须是两个不同的类名
|
|
567
|
+
assert.ok(css.includes('.si-row{'), '弹层列表行仍用 .si-row');
|
|
568
|
+
assert.ok(css.includes('.si-setting-row{'), '设置页行改用 .si-setting-row');
|
|
569
|
+
// 两者的 padding 必须各自成立,互不覆盖
|
|
570
|
+
assert.match(css, /\.si-row\{[^}]*padding:12px/);
|
|
571
|
+
assert.match(css, /\.si-setting-row\{[^}]*padding:16px 0/);
|
|
572
|
+
});
|
|
573
|
+
|
|
574
|
+
await test('the stylesheet also carries the settings page, still token-only', async () => {
|
|
575
|
+
const css = styles[0].textContent;
|
|
576
|
+
// 设置页照宿主 Settings 页的卡片/行布局取值
|
|
577
|
+
assert.match(css, /\.si-page\{[^}]*max-width:720px/);
|
|
578
|
+
assert.match(css, /\.si-card\{[^}]*var\(--dsw-alias-settings-card-fill\)/);
|
|
579
|
+
assert.match(css, /\.si-card\{[^}]*var\(--dsw-alias-settings-card-stroke\)/);
|
|
580
|
+
// 注意:assert.match 失败会抛异常,不能用 `||` 做「或」判断;用 test 先收集布尔。
|
|
581
|
+
const controlBaseRe = /\.si-input,\.si-select,\.si-num\{([^}]*)\}/;
|
|
582
|
+
assert.ok(controlBaseRe.test(css), '输入/下拉/数字框共用一条基线规则');
|
|
583
|
+
// select / 数字框只在基线之外补差异(内边距、定宽、右对齐),高度统一由基线给。
|
|
584
|
+
assert.ok(/\.si-select\{[^}]*padding/.test(css), 'select 只补内边距');
|
|
585
|
+
assert.ok(/\.si-num\{[^}]*width:132px/.test(css), '数字框定宽 132px');
|
|
586
|
+
assert.ok(/\.si-num\{[^}]*text-align:left/.test(css), '数字框左对齐(与宿主输入框一致)');
|
|
587
|
+
// 原生上下箭头必须去掉:宿主输入框没有 spinner,留着会让聚焦态看起来不像同一个控件。
|
|
588
|
+
assert.match(css, /\.si-num::-webkit-inner-spin-button\{[^}]*appearance:none/);
|
|
589
|
+
assert.match(css, /\.si-num\{[^}]*appearance:textfield/);
|
|
590
|
+
// 控件外观必须逐属性对齐宿主设置页的输入框(内置插件页的搜索框):
|
|
591
|
+
// .5px 描边 + bg-layer-1 + 13px 字号 + radius-md,三者共用同一条基线规则。
|
|
592
|
+
const base = css.match(controlBaseRe)[1];
|
|
593
|
+
assert.match(base, /border:\.5px solid var\(--dsw-alias-border-l4\)/);
|
|
594
|
+
assert.match(base, /background:var\(--dsw-alias-bg-layer-1\)/);
|
|
595
|
+
assert.match(base, /font-size:13px/);
|
|
596
|
+
assert.match(base, /border-radius:var\(--dsw-radius-md\)/);
|
|
597
|
+
assert.match(base, /height:36px/);
|
|
598
|
+
// 焦点环必须跟宿主一样用「边框变色 + 2px 柔光」,不能用浏览器默认 outline
|
|
599
|
+
const focusRule = css.match(/\{([^}]*--dsw-alias-state-business-primary[^}]*)\}/);
|
|
600
|
+
assert.ok(focusRule, '焦点环规则存在');
|
|
601
|
+
assert.match(focusRule[1], /outline:none/);
|
|
602
|
+
assert.match(focusRule[1], /border-color:var\(--dsw-alias-state-business-primary\)/);
|
|
603
|
+
assert.match(focusRule[1], /box-shadow:0 0 0 2px color-mix/);
|
|
604
|
+
// 回归保护:不要再包 var(--dsw-focus-ring-color, …),该变量在宿主此层是空值,
|
|
605
|
+
// CSS 里空值不触发 fallback,会让边框变透明(实测踩过)。
|
|
606
|
+
// (只匹配「当作 CSS 值用」的写法,注释里提到变量名不算)
|
|
607
|
+
assert.doesNotMatch(css, /:[^;{}]*var\(--dsw-focus-ring-color/);
|
|
608
|
+
// agent 建的端点徽标用状态色,不用字面色
|
|
609
|
+
assert.match(css, /\.si-badge-agent\{[^}]*var\(--dsw-alias-state-warn-label\)/);
|
|
610
|
+
// 硬约束:设置页新增规则也不许出现字面颜色
|
|
611
|
+
assert.doesNotMatch(css, /#[0-9a-fA-F]{3,8}\b/);
|
|
612
|
+
assert.doesNotMatch(css, /rgba?\(/);
|
|
613
|
+
});
|
|
614
|
+
|
|
615
|
+
await test('createRpc turns a failure envelope into RpcError with the host code', async () => {
|
|
616
|
+
const rpc = plugin.__test__.createRpc({
|
|
617
|
+
get: () => ({ rpc: { call: async () => ({ ok: false, error: { code: 'invalid-name', message: '入口名称不合法' } }) } }),
|
|
618
|
+
});
|
|
619
|
+
await assert.rejects(() => rpc('config/set', {}), (failure) => {
|
|
620
|
+
assert.equal(failure.name, 'RpcError');
|
|
621
|
+
assert.equal(failure.code, 'invalid-name');
|
|
622
|
+
assert.equal(failure.message, '入口名称不合法');
|
|
623
|
+
return true;
|
|
624
|
+
});
|
|
625
|
+
});
|
|
626
|
+
|
|
627
|
+
await test('the overlay passes the session to the dialog and renders nothing while closed', async () => {
|
|
628
|
+
plugin.__test__.closeDialog();
|
|
629
|
+
assert.equal(plugin.__test__.WebhookDialog({ t: (key) => key, rpc: async () => ({ hooks: [] }) }), null);
|
|
630
|
+
plugin.__test__.openDialog('session-9', '排查会话');
|
|
631
|
+
try {
|
|
632
|
+
const rpc = async () => ({ hooks: [] });
|
|
633
|
+
const element = plugin.__test__.WebhookDialog({ t: (key) => key, rpc });
|
|
634
|
+
assert.equal(element.type, plugin.__test__.WebhookPanel);
|
|
635
|
+
assert.equal(element.props.sessionId, 'session-9');
|
|
636
|
+
assert.equal(element.props.displayTitle, '排查会话');
|
|
637
|
+
assert.equal(element.props.rpc, rpc);
|
|
638
|
+
} finally {
|
|
639
|
+
plugin.__test__.closeDialog();
|
|
640
|
+
}
|
|
641
|
+
});
|
|
642
|
+
|
|
643
|
+
/** 一条端点行的测试 props(默认字段可按需覆盖)。 */
|
|
644
|
+
function rowProps(overrides) {
|
|
645
|
+
return {
|
|
646
|
+
t: (key) => key,
|
|
647
|
+
hook: {
|
|
648
|
+
id: 'wh_1', name: 'github-notify', tags: ['github'], defaultMode: 'steer', enabled: true,
|
|
649
|
+
maskedUrl: 'si_abcd…wxyz', lastUsedAt: null, lastStatus: null, deliveryCount: 0,
|
|
650
|
+
createdBy: 'ui', archived: false, ...overrides,
|
|
651
|
+
},
|
|
652
|
+
busy: false, copied: null, revealed: {}, confirming: null, renaming: null,
|
|
653
|
+
renameValue: '', setRenameValue: () => {},
|
|
654
|
+
onReveal: () => {}, onCopy: () => {}, onToggle: () => {}, onAskRevoke: () => {},
|
|
655
|
+
onCancelRevoke: () => {}, onRevoke: () => {}, onStartRename: () => {},
|
|
656
|
+
onCancelRename: () => {}, onRename: () => {},
|
|
657
|
+
};
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
await test('only agent-created endpoints carry the creator badge', async () => {
|
|
661
|
+
// UI 建的是默认情况,不标注;旧记录缺 createdBy 也按 UI 处理。
|
|
662
|
+
const uiRow = plugin.__test__.Row(rowProps({ createdBy: 'ui' }));
|
|
663
|
+
assert.doesNotMatch(textOf(uiRow), /dialog\.createdBy/);
|
|
664
|
+
assert.equal(findAllByClass(uiRow, 'si-badge-agent').length, 0);
|
|
665
|
+
const legacyRow = plugin.__test__.Row(rowProps({ createdBy: undefined }));
|
|
666
|
+
assert.doesNotMatch(textOf(legacyRow), /dialog\.createdBy/);
|
|
667
|
+
|
|
668
|
+
const agentRow = plugin.__test__.Row(rowProps({ createdBy: 'agent' }));
|
|
669
|
+
assert.match(textOf(agentRow), /dialog\.createdBy\.agent/);
|
|
670
|
+
const badges = findAllByClass(agentRow, 'si-badge-agent');
|
|
671
|
+
assert.equal(badges.length, 1);
|
|
672
|
+
assert.equal(textOf(badges[0]), 'dialog.createdBy.agent');
|
|
673
|
+
});
|
|
674
|
+
|
|
675
|
+
await test('a created endpoint lists url files by base only when more than one base exists', async () => {
|
|
676
|
+
const single = plugin.__test__.CreatedBlock({
|
|
677
|
+
t: (key) => key,
|
|
678
|
+
copied: false,
|
|
679
|
+
onCopy: () => {},
|
|
680
|
+
onDismiss: () => {},
|
|
681
|
+
created: {
|
|
682
|
+
hookId: 'wh_1',
|
|
683
|
+
url: 'https://hooks.example.com/api/session-inject/hooks/si_tok',
|
|
684
|
+
defaultBase: 'direct',
|
|
685
|
+
urlFiles: [{ name: 'direct', path: '~/.dsh/session-inject/urls/wh_1.url' }],
|
|
686
|
+
},
|
|
687
|
+
});
|
|
688
|
+
assert.match(textOf(single), /dialog\.createdBase/);
|
|
689
|
+
assert.equal(findByClass(single, 'si-created-files'), null, 'a single base needs no file list');
|
|
690
|
+
assert.match(textOf(single), /curl -s -X POST/);
|
|
691
|
+
|
|
692
|
+
const multiple = plugin.__test__.CreatedBlock({
|
|
693
|
+
t: (key) => key,
|
|
694
|
+
copied: false,
|
|
695
|
+
onCopy: () => {},
|
|
696
|
+
onDismiss: () => {},
|
|
697
|
+
created: {
|
|
698
|
+
hookId: 'wh_1',
|
|
699
|
+
url: 'https://hooks.example.com/api/session-inject/hooks/si_tok',
|
|
700
|
+
defaultBase: 'proxy',
|
|
701
|
+
urlFiles: [
|
|
702
|
+
{ name: 'direct', path: '~/.dsh/session-inject/urls/wh_1.url' },
|
|
703
|
+
{ name: 'proxy', path: '~/.dsh/session-inject/urls/wh_1.proxy.url' },
|
|
704
|
+
],
|
|
705
|
+
},
|
|
706
|
+
});
|
|
707
|
+
const files = findByClass(multiple, 'si-created-files');
|
|
708
|
+
assert.ok(files, 'file list rendered');
|
|
709
|
+
assert.match(textOf(files), /dialog\.urlFilesTitle/);
|
|
710
|
+
assert.match(textOf(files), /wh_1\.proxy\.url/);
|
|
711
|
+
const paths = findAllByClass(files, 'si-created-file-path');
|
|
712
|
+
assert.equal(paths.length, 2);
|
|
713
|
+
// 路径不是 URL:区块里唯一的一条完整 URL 仍然是默认入口那条
|
|
714
|
+
assert.equal(paths.some((node) => textOf(node).includes('https://')), false);
|
|
715
|
+
});
|
|
716
|
+
|
|
717
|
+
/** 三层配置快照:legacy 只存在于 YAML,proxy 被界面覆盖,newbase 只在界面里。 */
|
|
718
|
+
function snapshotFixture(overrides) {
|
|
719
|
+
return {
|
|
720
|
+
yaml: {
|
|
721
|
+
publicBaseUrls: { legacy: 'https://legacy.example.com', proxy: 'https://yaml.example.com' },
|
|
722
|
+
defaultBase: 'direct',
|
|
723
|
+
limits: { maxBodyBytes: 2097152, maxTextBytes: 65536, ratePerMinute: 120 },
|
|
724
|
+
},
|
|
725
|
+
settings: {
|
|
726
|
+
publicBaseUrls: { proxy: 'https://hook.example.com', newbase: 'https://new.example.com' },
|
|
727
|
+
defaultBase: 'proxy',
|
|
728
|
+
limits: { maxBodyBytes: 1048576, maxTextBytes: 65536, ratePerMinute: 60 },
|
|
729
|
+
},
|
|
730
|
+
effective: {
|
|
731
|
+
publicBaseUrls: {
|
|
732
|
+
direct: 'http://127.0.0.1:3080',
|
|
733
|
+
legacy: 'https://legacy.example.com',
|
|
734
|
+
proxy: 'https://hook.example.com',
|
|
735
|
+
newbase: 'https://new.example.com',
|
|
736
|
+
},
|
|
737
|
+
defaultBase: 'proxy',
|
|
738
|
+
limits: { maxBodyBytes: 1048576, maxTextBytes: 65536, ratePerMinute: 60 },
|
|
739
|
+
},
|
|
740
|
+
directOrigin: 'http://127.0.0.1:3080',
|
|
741
|
+
directError: null,
|
|
742
|
+
basePath: '/api/session-inject',
|
|
743
|
+
...overrides,
|
|
744
|
+
};
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
await test('readConfig accepts the three-layer view and the legacy flat shape', async () => {
|
|
748
|
+
const layered = plugin.__test__.readConfig(snapshotFixture({}));
|
|
749
|
+
assert.equal(layered.effective.publicBaseUrls.direct, 'http://127.0.0.1:3080');
|
|
750
|
+
assert.equal(layered.yaml.publicBaseUrls.legacy, 'https://legacy.example.com');
|
|
751
|
+
assert.equal(layered.settings.publicBaseUrls.proxy, 'https://hook.example.com');
|
|
752
|
+
assert.equal(layered.directOrigin, 'http://127.0.0.1:3080');
|
|
753
|
+
assert.equal(layered.directError, null);
|
|
754
|
+
assert.equal(layered.basePath, '/api/session-inject');
|
|
755
|
+
|
|
756
|
+
// origin 取不到时不再抛错,而是 null + directError
|
|
757
|
+
const noOrigin = plugin.__test__.readConfig(snapshotFixture({ directOrigin: null, directError: '未绑定监听端口' }));
|
|
758
|
+
assert.equal(noOrigin.directOrigin, null);
|
|
759
|
+
assert.equal(noOrigin.directError, '未绑定监听端口');
|
|
760
|
+
|
|
761
|
+
// 旧扁平形状:整份返回当成生效值,没有分层信息
|
|
762
|
+
const flat = plugin.__test__.readConfig({
|
|
763
|
+
publicBaseUrls: { direct: 'http://h:1' },
|
|
764
|
+
defaultBase: 'direct',
|
|
765
|
+
directOrigin: 'http://h:1',
|
|
766
|
+
basePath: '/api/session-inject',
|
|
767
|
+
limits: { maxBodyBytes: 1024, maxTextBytes: 1, ratePerMinute: 1 },
|
|
768
|
+
});
|
|
769
|
+
assert.deepEqual({ ...flat.yaml }, {});
|
|
770
|
+
assert.deepEqual({ ...flat.settings }, {});
|
|
771
|
+
assert.equal(flat.effective.publicBaseUrls.direct, 'http://h:1');
|
|
772
|
+
});
|
|
773
|
+
|
|
774
|
+
await test('draftFromConfig marks each row origin (direct implicit, yaml, settings)', async () => {
|
|
775
|
+
const snapshot = plugin.__test__.readConfig(snapshotFixture({}));
|
|
776
|
+
const draft = plugin.__test__.draftFromConfig(snapshot);
|
|
777
|
+
assert.deepEqual([...draft.rows].map((row) => [row.name, row.origin]), [
|
|
778
|
+
['direct', 'implicit'],
|
|
779
|
+
['legacy', 'yaml'],
|
|
780
|
+
['proxy', 'settings'],
|
|
781
|
+
['newbase', 'settings'],
|
|
782
|
+
]);
|
|
783
|
+
assert.equal(draft.defaultBase, 'proxy');
|
|
784
|
+
assert.equal(draft.maxBodyBytes, '1048576');
|
|
785
|
+
});
|
|
786
|
+
|
|
787
|
+
await test('validateDraft sends the whole visible base table (the overlay takes the table over)', async () => {
|
|
788
|
+
const snapshot = plugin.__test__.readConfig(snapshotFixture({}));
|
|
789
|
+
const draft = plugin.__test__.draftFromConfig(snapshot);
|
|
790
|
+
const edited = plugin.__test__.updateBaseRow(draft, 'proxy', { url: 'https://edited.example.com//' });
|
|
791
|
+
const outcome = plugin.__test__.validateDraft(edited, (key) => key, snapshot);
|
|
792
|
+
assert.equal(outcome.errors.length, 0);
|
|
793
|
+
// JSON 式的整表接管:界面上看得见的显式入口全部写进覆盖层,
|
|
794
|
+
// 否则 settings 文件一存在就会把它们从生效集合里抹掉。
|
|
795
|
+
assert.equal(outcome.payload.publicBaseUrls.proxy, 'https://edited.example.com');
|
|
796
|
+
assert.equal(outcome.payload.publicBaseUrls.legacy, 'https://legacy.example.com');
|
|
797
|
+
assert.equal(outcome.payload.publicBaseUrls.newbase, 'https://new.example.com');
|
|
798
|
+
// direct 是隐式项且没被动过 → 不写,继续跟随监听地址
|
|
799
|
+
assert.equal('direct' in outcome.payload.publicBaseUrls, false);
|
|
800
|
+
assert.equal(outcome.payload.defaultBase, 'proxy');
|
|
801
|
+
assert.equal(outcome.payload.limits.maxBodyBytes, 1048576);
|
|
802
|
+
});
|
|
803
|
+
|
|
804
|
+
await test('validateDraft can pin direct explicitly when the user edits it', async () => {
|
|
805
|
+
const snapshot = plugin.__test__.readConfig(snapshotFixture({}));
|
|
806
|
+
const draft = plugin.__test__.draftFromConfig(snapshot);
|
|
807
|
+
const edited = plugin.__test__.updateBaseRow(draft, 'direct', { url: 'https://edge.example.com' });
|
|
808
|
+
const outcome = plugin.__test__.validateDraft(edited, (key) => key, snapshot);
|
|
809
|
+
assert.equal(outcome.errors.length, 0);
|
|
810
|
+
assert.equal(outcome.payload.publicBaseUrls.direct, 'https://edge.example.com');
|
|
811
|
+
});
|
|
812
|
+
|
|
813
|
+
await test('validateDraft rejects invalid input locally (name, url, default, limits)', async () => {
|
|
814
|
+
const snapshot = plugin.__test__.readConfig(snapshotFixture({}));
|
|
815
|
+
const base = plugin.__test__.EMPTY_DRAFT;
|
|
816
|
+
const withRow = (patch) => ({
|
|
817
|
+
...base,
|
|
818
|
+
rows: [...base.rows, { id: 'row-x', name: '', url: '', origin: 'settings', edited: true, ...patch }],
|
|
819
|
+
});
|
|
820
|
+
const errorsOf = (draft) => [...plugin.__test__.validateDraft(draft, (key) => key, snapshot).errors];
|
|
821
|
+
|
|
822
|
+
assert.deepEqual(errorsOf(withRow({ name: 'Bad Name', url: 'https://a.example' })), ['settings.error.name']);
|
|
823
|
+
assert.deepEqual(errorsOf(withRow({ name: '', url: 'https://a.example' })), ['settings.error.nameEmpty']);
|
|
824
|
+
assert.deepEqual(errorsOf(withRow({ name: 'direct', url: 'https://a.example' })), ['settings.error.nameDuplicate']);
|
|
825
|
+
assert.deepEqual(errorsOf(withRow({ name: 'x', url: '' })), ['settings.error.urlRequired']);
|
|
826
|
+
assert.deepEqual(errorsOf(withRow({ name: 'x', url: 'ftp://a.example' })), ['settings.error.url']);
|
|
827
|
+
assert.deepEqual(errorsOf(withRow({ name: 'x', url: 'https://a.example/?q=1' })), ['settings.error.url']);
|
|
828
|
+
assert.deepEqual(errorsOf({ ...base, defaultBase: 'nope' }), ['settings.error.defaultMissing']);
|
|
829
|
+
assert.deepEqual(errorsOf({ ...base, ratePerMinute: '1.5' }), ['settings.error.limitInteger']);
|
|
830
|
+
assert.deepEqual(errorsOf({ ...base, ratePerMinute: '0' }), ['settings.error.limitRange']);
|
|
831
|
+
// 限额跨字段:单条文本不能大于请求体
|
|
832
|
+
assert.deepEqual(errorsOf({ ...base, maxBodyBytes: '1024', maxTextBytes: '2048' }), ['settings.error.textOverBody']);
|
|
833
|
+
// 非法输入一个请求都不发
|
|
834
|
+
assert.equal(plugin.__test__.validateDraft({ ...base, defaultBase: 'nope' }, (key) => key, snapshot).payload, null);
|
|
835
|
+
});
|
|
836
|
+
|
|
837
|
+
/** 设置页纯展示树的测试 props。 */
|
|
838
|
+
function panelProps(overrides) {
|
|
839
|
+
const snapshot = plugin.__test__.readConfig(snapshotFixture({}));
|
|
840
|
+
return {
|
|
841
|
+
t: (key, params) => (params === undefined ? key : `${key} ${JSON.stringify(params)}`),
|
|
842
|
+
status: 'ready',
|
|
843
|
+
loadError: null,
|
|
844
|
+
snapshot,
|
|
845
|
+
draft: plugin.__test__.draftFromConfig(snapshot),
|
|
846
|
+
errors: [],
|
|
847
|
+
notice: null,
|
|
848
|
+
busy: false,
|
|
849
|
+
onChangeDraft: () => {},
|
|
850
|
+
onSave: () => {},
|
|
851
|
+
onReset: () => {},
|
|
852
|
+
onReload: () => {},
|
|
853
|
+
...overrides,
|
|
854
|
+
};
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
await test('the settings page renders every base row with its origin hint', async () => {
|
|
858
|
+
const element = plugin.__test__.SettingsPanel(panelProps({}));
|
|
859
|
+
const text = textOf(element);
|
|
860
|
+
assert.match(text, /settings\.title/);
|
|
861
|
+
assert.match(text, /settings\.layerNote/);
|
|
862
|
+
assert.match(text, /settings\.limits\.title/);
|
|
863
|
+
|
|
864
|
+
const nameInputs = findAllByClass(element, 'si-base-name');
|
|
865
|
+
const urlInputs = findAllByClass(element, 'si-base-url');
|
|
866
|
+
assert.equal(nameInputs.length, 4, 'direct + legacy + proxy + newbase');
|
|
867
|
+
assert.equal(urlInputs.length, 4);
|
|
868
|
+
assert.equal(nameInputs[0].props.value, 'direct');
|
|
869
|
+
assert.equal(nameInputs[0].props.readOnly, true, 'direct cannot be renamed');
|
|
870
|
+
// direct 显示的是生效值(含隐式监听地址)
|
|
871
|
+
assert.equal(urlInputs[0].props.value, 'http://127.0.0.1:3080');
|
|
872
|
+
// 只有 direct 的名称是固定的;其它行都能改名(保存时整表接管)
|
|
873
|
+
assert.equal(nameInputs[1].props.readOnly, false);
|
|
874
|
+
assert.equal(nameInputs[2].props.readOnly, false);
|
|
875
|
+
assert.equal(nameInputs[3].props.readOnly, false);
|
|
876
|
+
|
|
877
|
+
// 来源小字:继承 YAML / 界面覆盖(YAML 有值) / 界面覆盖(YAML 无此项) / direct 隐式
|
|
878
|
+
assert.match(text, /settings\.source\.yaml/);
|
|
879
|
+
assert.match(text, /settings\.source\.settingsOverYaml/);
|
|
880
|
+
assert.match(text, /settings\.source\.settingsOnly/);
|
|
881
|
+
assert.match(text, /settings\.bases\.directHint/);
|
|
882
|
+
|
|
883
|
+
const select = findByClass(element, 'si-select');
|
|
884
|
+
assert.equal(select.props.value, 'proxy');
|
|
885
|
+
assert.equal(select.children.length, 4);
|
|
886
|
+
|
|
887
|
+
const numbers = findAllByClass(element, 'si-num');
|
|
888
|
+
assert.equal(numbers.length, 3);
|
|
889
|
+
assert.equal(numbers[0].props.type, 'number');
|
|
890
|
+
assert.equal(numbers[0].props.value, '1048576');
|
|
891
|
+
assert.equal(numbers[0].props.min, plugin.__test__.LIMIT_BOUNDS.maxBodyBytes.min);
|
|
892
|
+
assert.equal(numbers[0].props.max, plugin.__test__.LIMIT_BOUNDS.maxBodyBytes.max);
|
|
893
|
+
|
|
894
|
+
const save = findAllByClass(element, 'si-btn').find((button) => textOf(button) === 'settings.save');
|
|
895
|
+
assert.ok(save, 'save button rendered');
|
|
896
|
+
assert.ok(hasClass(save, 'si-btn-primary'));
|
|
897
|
+
});
|
|
898
|
+
|
|
899
|
+
await test('every base except direct can be removed, whatever layer it came from', async () => {
|
|
900
|
+
const element = plugin.__test__.SettingsPanel(panelProps({}));
|
|
901
|
+
const rows = findAllByClass(element, 'si-base-item');
|
|
902
|
+
assert.equal(rows.length, 4);
|
|
903
|
+
const removeState = (rowIndex) => {
|
|
904
|
+
const remove = findAllByClass(rows[rowIndex], 'si-btn').find((button) => textOf(button) === 'settings.bases.remove');
|
|
905
|
+
return remove === undefined ? null : remove.props.disabled;
|
|
906
|
+
};
|
|
907
|
+
assert.equal(removeState(0), null, 'direct has no remove button');
|
|
908
|
+
assert.notEqual(removeState(1), true, 'YAML base can be removed (the overlay takes the table over)');
|
|
909
|
+
assert.notEqual(removeState(2), true);
|
|
910
|
+
assert.notEqual(removeState(3), true);
|
|
911
|
+
});
|
|
912
|
+
|
|
913
|
+
await test('the settings page shows the direct error and keeps working without an origin', async () => {
|
|
914
|
+
const snapshot = plugin.__test__.readConfig(snapshotFixture({
|
|
915
|
+
directOrigin: null,
|
|
916
|
+
directError: '未绑定监听端口',
|
|
917
|
+
effective: {
|
|
918
|
+
publicBaseUrls: { legacy: 'https://legacy.example.com' },
|
|
919
|
+
defaultBase: 'legacy',
|
|
920
|
+
limits: { maxBodyBytes: 1048576, maxTextBytes: 65536, ratePerMinute: 60 },
|
|
921
|
+
},
|
|
922
|
+
}));
|
|
923
|
+
const element = plugin.__test__.SettingsPanel(panelProps({ snapshot, draft: plugin.__test__.draftFromConfig(snapshot) }));
|
|
924
|
+
const text = textOf(element);
|
|
925
|
+
assert.match(text, /settings\.bases\.directError/);
|
|
926
|
+
assert.match(text, /未绑定监听端口/);
|
|
927
|
+
// direct 行仍然在(占位文案走「取不到监听地址」)
|
|
928
|
+
const urlInputs = findAllByClass(element, 'si-base-url');
|
|
929
|
+
assert.equal(urlInputs[0].props.value, '');
|
|
930
|
+
assert.match(String(urlInputs[0].props.placeholder), /originUnavailable/);
|
|
931
|
+
});
|
|
932
|
+
|
|
933
|
+
await test('the settings page keeps an invalid default visible and shows local errors', async () => {
|
|
934
|
+
const element = plugin.__test__.SettingsPanel(panelProps({
|
|
935
|
+
draft: { ...plugin.__test__.draftFromConfig(plugin.__test__.readConfig(snapshotFixture({}))), defaultBase: 'gone' },
|
|
936
|
+
errors: ['settings.error.defaultMissing'],
|
|
937
|
+
}));
|
|
938
|
+
const select = findByClass(element, 'si-select');
|
|
939
|
+
const labels = select.children.map((option) => textOf(option));
|
|
940
|
+
assert.ok(labels.some((label) => label.includes('invalidOption')), 'invalid default stays selectable');
|
|
941
|
+
const errors = findByClass(element, 'si-page-errors');
|
|
942
|
+
assert.ok(errors, 'validation errors rendered');
|
|
943
|
+
assert.match(textOf(errors), /settings\.error\.defaultMissing/);
|
|
944
|
+
});
|
|
945
|
+
|
|
946
|
+
await test('the settings page has explicit loading and failed states', async () => {
|
|
947
|
+
const loading = plugin.__test__.SettingsPanel(panelProps({ status: 'loading' }));
|
|
948
|
+
assert.match(textOf(loading), /settings\.loading/);
|
|
949
|
+
|
|
950
|
+
let reloads = 0;
|
|
951
|
+
const failed = plugin.__test__.SettingsPanel(panelProps({
|
|
952
|
+
status: 'failed',
|
|
953
|
+
loadError: '入口 origin 取不到',
|
|
954
|
+
onReload: () => { reloads += 1; },
|
|
955
|
+
}));
|
|
956
|
+
assert.match(textOf(failed), /settings\.error\.load/);
|
|
957
|
+
assert.match(textOf(failed), /入口 origin 取不到/);
|
|
958
|
+
const retry = findAllByClass(failed, 'si-btn').find((button) => textOf(button).includes('settings.retry'));
|
|
959
|
+
assert.ok(retry, 'retry button rendered');
|
|
960
|
+
retry.props.onClick();
|
|
961
|
+
assert.equal(reloads, 1);
|
|
962
|
+
});
|
|
963
|
+
|
|
964
|
+
/** 无任何界面覆盖时的快照:来源应全部回落到 YAML / direct 隐式。 */
|
|
965
|
+
function noOverrideSnapshot() {
|
|
966
|
+
const snapshot = plugin.__test__.readConfig(snapshotFixture({}));
|
|
967
|
+
return plugin.__test__.readConfig({
|
|
968
|
+
...snapshot,
|
|
969
|
+
settings: {},
|
|
970
|
+
effective: snapshot.effective,
|
|
971
|
+
yaml: snapshot.yaml,
|
|
972
|
+
});
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
await test('limit rows follow the host row layout, not a stacked field', async () => {
|
|
976
|
+
const element = plugin.__test__.SettingsPanel({
|
|
977
|
+
t: (key, params) => (params === undefined ? key : `${key} ${JSON.stringify(params)}`),
|
|
978
|
+
rpc: async () => ({}),
|
|
979
|
+
snapshot: {
|
|
980
|
+
yaml: { publicBaseUrls: {}, limits: {} },
|
|
981
|
+
settings: {},
|
|
982
|
+
effective: {
|
|
983
|
+
publicBaseUrls: { direct: 'http://127.0.0.1:3080' },
|
|
984
|
+
defaultBase: 'direct',
|
|
985
|
+
limits: { maxBodyBytes: 1048576, maxTextBytes: 65536, ratePerMinute: 60 },
|
|
986
|
+
},
|
|
987
|
+
directOrigin: 'http://127.0.0.1:3080',
|
|
988
|
+
basePath: '/api/session-inject',
|
|
989
|
+
},
|
|
990
|
+
draft: {
|
|
991
|
+
rows: [],
|
|
992
|
+
defaultBase: 'direct',
|
|
993
|
+
maxBodyBytes: '1048576',
|
|
994
|
+
maxTextBytes: '65536',
|
|
995
|
+
ratePerMinute: '60',
|
|
996
|
+
},
|
|
997
|
+
errors: [],
|
|
998
|
+
notice: null,
|
|
999
|
+
busy: false,
|
|
1000
|
+
onChangeDraft: () => {},
|
|
1001
|
+
onSave: () => {},
|
|
1002
|
+
onReset: async () => {},
|
|
1003
|
+
});
|
|
1004
|
+
const rows = [];
|
|
1005
|
+
const collect = (node) => {
|
|
1006
|
+
if (node === null || node === undefined || typeof node !== 'object') return;
|
|
1007
|
+
if (Array.isArray(node)) {
|
|
1008
|
+
for (const child of node) collect(child);
|
|
1009
|
+
return;
|
|
1010
|
+
}
|
|
1011
|
+
if (node.props?.className === 'si-setting-row') rows.push(node);
|
|
1012
|
+
for (const child of node.children ?? []) collect(child);
|
|
1013
|
+
};
|
|
1014
|
+
collect(element);
|
|
1015
|
+
// 三个限额 + 默认入口各一行
|
|
1016
|
+
assert.equal(rows.length, 4);
|
|
1017
|
+
for (const row of rows) {
|
|
1018
|
+
assert.ok(findByClass(row, 'si-setting-row-text'), 'row carries a text column');
|
|
1019
|
+
assert.ok(findByClass(row, 'si-setting-row-control'), 'row carries a control column');
|
|
1020
|
+
}
|
|
1021
|
+
// 数字输入必须落在 control 列里(而不是独占一整行),否则会退回 160px 高的巨框
|
|
1022
|
+
const numbers = rows.filter((row) => findByClass(row, 'si-num') !== null);
|
|
1023
|
+
assert.equal(numbers.length, 3);
|
|
1024
|
+
for (const row of numbers) {
|
|
1025
|
+
assert.equal(findByClass(row, 'si-setting-row-control').children.some(
|
|
1026
|
+
(child) => findClassDeep(child) === 'si-num'), true);
|
|
1027
|
+
}
|
|
1028
|
+
});
|
|
1029
|
+
|
|
1030
|
+
await test('the reset-to-YAML button only appears when that group has an override', async () => {
|
|
1031
|
+
// 有覆盖(fixture 的 settings 里三键齐全):两张卡片各一个按钮
|
|
1032
|
+
const withOverride = plugin.__test__.SettingsPanel(panelProps({}));
|
|
1033
|
+
const resets = findAllByClass(withOverride, 'si-btn').filter((button) => textOf(button) === 'settings.reset');
|
|
1034
|
+
assert.equal(resets.length, 2, 'one per group');
|
|
1035
|
+
assert.equal(plugin.__test__.hasBasesOverride(panelProps({}).snapshot), true);
|
|
1036
|
+
assert.equal(plugin.__test__.hasLimitsOverride(panelProps({}).snapshot), true);
|
|
1037
|
+
|
|
1038
|
+
// 无覆盖:按钮消失(没有任何覆盖可清)
|
|
1039
|
+
const snapshot = noOverrideSnapshot();
|
|
1040
|
+
assert.equal(plugin.__test__.hasBasesOverride(snapshot), false);
|
|
1041
|
+
assert.equal(plugin.__test__.hasLimitsOverride(snapshot), false);
|
|
1042
|
+
const withoutOverride = plugin.__test__.SettingsPanel(panelProps({
|
|
1043
|
+
snapshot,
|
|
1044
|
+
draft: plugin.__test__.draftFromConfig(snapshot),
|
|
1045
|
+
}));
|
|
1046
|
+
assert.equal(findAllByClass(withoutOverride, 'si-btn').filter((button) => textOf(button) === 'settings.reset').length, 0);
|
|
1047
|
+
});
|
|
1048
|
+
|
|
1049
|
+
await test('each reset button asks for its own group keys and is disabled while busy', async () => {
|
|
1050
|
+
const requested = [];
|
|
1051
|
+
const element = plugin.__test__.SettingsPanel(panelProps({ onReset: (keys) => requested.push(keys) }));
|
|
1052
|
+
const resets = findAllByClass(element, 'si-btn').filter((button) => textOf(button) === 'settings.reset');
|
|
1053
|
+
assert.equal(resets.length, 2);
|
|
1054
|
+
// 顺序即组顺序:入口组(publicBaseUrls + defaultBase)在前,限额组在后
|
|
1055
|
+
assert.equal(typeof resets[0].props.onClick, 'function');
|
|
1056
|
+
resets[0].props.onClick();
|
|
1057
|
+
resets[1].props.onClick();
|
|
1058
|
+
assert.deepEqual([...requested[0]], ['publicBaseUrls', 'defaultBase']);
|
|
1059
|
+
assert.deepEqual([...requested[1]], ['limits']);
|
|
1060
|
+
|
|
1061
|
+
const busy = plugin.__test__.SettingsPanel(panelProps({ busy: true }));
|
|
1062
|
+
for (const button of findAllByClass(busy, 'si-btn').filter((node) => textOf(node) === 'settings.reset')) {
|
|
1063
|
+
assert.equal(button.props.disabled, true, 'reset disabled while a request is in flight');
|
|
1064
|
+
}
|
|
1065
|
+
});
|
|
1066
|
+
|
|
1067
|
+
await test('after a reset the source hints fall back to YAML and the implicit direct', async () => {
|
|
1068
|
+
const snapshot = noOverrideSnapshot();
|
|
1069
|
+
const t = (key, params) => (params === undefined ? key : `${key} ${JSON.stringify(params)}`);
|
|
1070
|
+
// 入口:YAML 里有的就是「继承自 YAML」,direct 没有显式配置 → 隐式跟随监听地址
|
|
1071
|
+
assert.match(plugin.__test__.baseSourceHint(t, snapshot, 'legacy'), /^settings\.source\.yaml/);
|
|
1072
|
+
assert.match(plugin.__test__.baseSourceHint(t, snapshot, 'proxy'), /^settings\.source\.yaml/);
|
|
1073
|
+
assert.equal(plugin.__test__.baseSourceHint(t, snapshot, 'direct'), '');
|
|
1074
|
+
assert.match(plugin.__test__.defaultBaseSourceHint(t, snapshot), /^settings\.source\.yaml/);
|
|
1075
|
+
assert.match(plugin.__test__.limitSourceHint(t, snapshot, 'maxBodyBytes'), /^settings\.source\.yaml/);
|
|
1076
|
+
});
|
|
1077
|
+
|
|
1078
|
+
await test('the settings container reads config/get, then saves through config/set and reloads', async () => {
|
|
1079
|
+
const calls = [];
|
|
1080
|
+
const snapshot = snapshotFixture({});
|
|
1081
|
+
const rpc = async (method, payload) => {
|
|
1082
|
+
calls.push({ method, payload });
|
|
1083
|
+
if (method === 'config/get') return snapshot;
|
|
1084
|
+
return { saved: true, ...snapshot };
|
|
1085
|
+
};
|
|
1086
|
+
const element = plugin.__test__.SettingsSection({ t: (key) => key, rpc });
|
|
1087
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
1088
|
+
assert.equal(calls[0].method, 'config/get');
|
|
1089
|
+
// 首屏是加载态的面板;它的 onSave 就是容器的保存闭包。
|
|
1090
|
+
assert.equal(typeof element.props.onSave, 'function');
|
|
1091
|
+
element.props.onSave();
|
|
1092
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
1093
|
+
assert.equal(calls[1].method, 'config/set');
|
|
1094
|
+
assert.equal(calls[1].payload.defaultBase, 'direct', 'EMPTY_DRAFT saves the implicit direct base');
|
|
1095
|
+
assert.equal(calls[1].payload.limits.maxBodyBytes, 1048576);
|
|
1096
|
+
// 保存成功后重新拉一次三层视图
|
|
1097
|
+
assert.equal(calls[2].method, 'config/get');
|
|
1098
|
+
});
|
|
1099
|
+
|
|
1100
|
+
await test('the settings container resets a group through config/reset then reloads', async () => {
|
|
1101
|
+
const calls = [];
|
|
1102
|
+
const snapshot = snapshotFixture({});
|
|
1103
|
+
const rpc = async (method, payload) => {
|
|
1104
|
+
calls.push({ method, payload });
|
|
1105
|
+
if (method === 'config/get') return snapshot;
|
|
1106
|
+
return snapshot; // config/reset 返回与 config/get 同形;UI 不依赖它
|
|
1107
|
+
};
|
|
1108
|
+
const element = plugin.__test__.SettingsSection({ t: (key) => key, rpc });
|
|
1109
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
1110
|
+
assert.equal(calls[0].method, 'config/get');
|
|
1111
|
+
assert.equal(typeof element.props.onReset, 'function');
|
|
1112
|
+
element.props.onReset(['publicBaseUrls', 'defaultBase']);
|
|
1113
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
1114
|
+
assert.equal(calls[1].method, 'config/reset');
|
|
1115
|
+
assert.deepEqual([...calls[1].payload.keys], ['publicBaseUrls', 'defaultBase']);
|
|
1116
|
+
// 重置后同样重拉三层视图,让来源小字跟着变
|
|
1117
|
+
assert.equal(calls[2].method, 'config/get');
|
|
1118
|
+
});
|
|
1119
|
+
|
|
1120
|
+
await test('the settings container swallows a rejected reset (no unhandled rejection)', async () => {
|
|
1121
|
+
const rpc = async (method) => {
|
|
1122
|
+
if (method === 'config/get') return snapshotFixture({});
|
|
1123
|
+
throw new Error('reset rejected');
|
|
1124
|
+
};
|
|
1125
|
+
const element = plugin.__test__.SettingsSection({ t: (key) => key, rpc });
|
|
1126
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
1127
|
+
element.props.onReset(['limits']);
|
|
1128
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
1129
|
+
});
|
|
1130
|
+
|
|
1131
|
+
await test('the settings container swallows a rejected save (no unhandled rejection)', async () => {
|
|
1132
|
+
const rpc = async (method) => {
|
|
1133
|
+
if (method === 'config/get') return snapshotFixture({});
|
|
1134
|
+
throw new Error('write rejected');
|
|
1135
|
+
};
|
|
1136
|
+
const element = plugin.__test__.SettingsSection({ t: (key) => key, rpc });
|
|
1137
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
1138
|
+
element.props.onSave();
|
|
1139
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
1140
|
+
});
|
|
1141
|
+
|
|
1142
|
+
await test('locale fragments carry identical en/zh key sets', async () => {
|
|
1143
|
+
for (const [label, fragment] of [
|
|
1144
|
+
['dialog', plugin.__test__.DIALOG_LOCALE],
|
|
1145
|
+
['settings', plugin.__test__.SETTINGS_LOCALE],
|
|
1146
|
+
]) {
|
|
1147
|
+
assert.deepEqual(Object.keys(fragment.en).sort(), Object.keys(fragment.zh).sort(), `${label} parity`);
|
|
1148
|
+
for (const [key, value] of Object.entries(fragment.zh)) assert.notEqual(value, '', `${label}.zh.${key}`);
|
|
1149
|
+
for (const [key, value] of Object.entries(fragment.en)) assert.notEqual(value, '', `${label}.en.${key}`);
|
|
1150
|
+
}
|
|
1151
|
+
});
|
|
1152
|
+
|
|
1153
|
+
await test('a urlfile-failed save is a warning, not a failure: view refreshes and notice is warn', async () => {
|
|
1154
|
+
const calls = [];
|
|
1155
|
+
const snapshot = snapshotFixture({});
|
|
1156
|
+
const rpc = async (method) => {
|
|
1157
|
+
calls.push(method);
|
|
1158
|
+
if (method === 'config/get') return snapshot;
|
|
1159
|
+
// Host 在「配置已保存并生效,只是 urlFile 重写失败」时抛 urlfile-failed。
|
|
1160
|
+
throw new plugin.__test__.RpcError('urlfile-failed', '配置已保存并已生效,但 URL 文件写入失败:EACCES');
|
|
1161
|
+
};
|
|
1162
|
+
const element = plugin.__test__.SettingsSection({ t: (key, params) => (params === undefined ? key : `${key} ${JSON.stringify(params)}`), rpc });
|
|
1163
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
1164
|
+
element.props.onSave();
|
|
1165
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
1166
|
+
// 保存失败后仍会重拉视图:配置其实已经生效,界面不能停在旧状态。
|
|
1167
|
+
assert.deepEqual([...calls], ['config/get', 'config/set', 'config/get']);
|
|
1168
|
+
});
|
|
1169
|
+
|
|
1170
|
+
await test('a urlfile-failed reset is surfaced as a warning and refreshes the view', async () => {
|
|
1171
|
+
const calls = [];
|
|
1172
|
+
const snapshot = snapshotFixture({});
|
|
1173
|
+
const rpc = async (method) => {
|
|
1174
|
+
calls.push(method);
|
|
1175
|
+
if (method === 'config/get') return snapshot;
|
|
1176
|
+
throw new plugin.__test__.RpcError('urlfile-failed', '配置覆盖已回退并生效,但 URL 文件写入失败:EACCES');
|
|
1177
|
+
};
|
|
1178
|
+
const element = plugin.__test__.SettingsSection({ t: (key) => key, rpc });
|
|
1179
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
1180
|
+
element.props.onReset(['limits']);
|
|
1181
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
1182
|
+
assert.deepEqual([...calls], ['config/get', 'config/reset', 'config/get']);
|
|
1183
|
+
});
|
|
1184
|
+
|
|
1185
|
+
await test('a warn notice renders in the warning colour with role=status', async () => {
|
|
1186
|
+
const element = plugin.__test__.SettingsPanel(panelProps({
|
|
1187
|
+
notice: { kind: 'warn', text: '警告:配置已保存,但 URL 文件写入失败' },
|
|
1188
|
+
}));
|
|
1189
|
+
const notice = findAllByClass(element, 'si-page-note').find((node) => textOf(node).includes('警告'));
|
|
1190
|
+
assert.ok(notice, 'warning notice rendered');
|
|
1191
|
+
assert.equal(notice.props.role, 'status');
|
|
1192
|
+
// 保存成功的 notice 仍是绿色 role=status
|
|
1193
|
+
const saved = plugin.__test__.SettingsPanel(panelProps({ notice: { kind: 'saved', text: '已保存并已生效' } }));
|
|
1194
|
+
const savedNotice = findAllByClass(saved, 'si-saved').find((node) => textOf(node).includes('已保存'));
|
|
1195
|
+
assert.ok(savedNotice, 'saved notice rendered');
|
|
1196
|
+
});
|
|
1197
|
+
|
|
1198
|
+
console.log(`\n${passed} passed, ${failures.length} failed`);
|
|
1199
|
+
if (failures.length > 0) process.exitCode = 1;
|