From 381751a28dc4db0461cbe8fdd65da01c87c0738d Mon Sep 17 00:00:00 2001 From: zhanjunhao <642307404@qq.com> Date: Wed, 18 Nov 2026 17:40:53 +0800 Subject: [PATCH] =?UTF-8?q?fix(channel):=20=E4=BF=AE=E5=A4=8D=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E5=A1=AB=E5=85=A5=E4=B8=8E=E6=A3=80=E6=9F=A5=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/CHANGELOG.md | 16 + .../channels/modals/EditChannelModal.jsx | 1 + src/hooks/common/usePartnerWorkspaceEntry.js | 9 +- src/pages/Organization/index.jsx | 3 +- test/unit/clipboard.test.js | 331 ++++++++++++++++++ 5 files changed, 356 insertions(+), 4 deletions(-) create mode 100644 test/unit/clipboard.test.js diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 5bcb47ea..69124ca1 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -26,6 +26,22 @@ - **回滚**:按本条涉及的下载页、`UiThemeProvider.jsx` 与顶栏菜单组件差异还原,保留其他工作区修改。 - **关联**:`docs/ui/GLOBAL_UI_BASELINE.md` 第 1、4 节;`docs/standards/UI_COMPONENT_STANDARDS.md`。 +### 修复 · 复制回归测试与 Hook 依赖检查 + +- **范围**:企业密钥复制回归用例、伙伴工作台入口与企业中心请求清理。 +- **变更**:复制测试按当前公共复制函数及密钥、模型回调验证成功和失败反馈,同时校验成功标记与定时器;用例纳入 `test/unit/`;伙伴作用域按用户与企业身份保持稳定,企业请求清理捕获本次使用的引用。 +- **影响**:修复过期测试及两处 Hook 检查警告,保留现有复制、权限探测与过期请求失效逻辑。 +- **回滚**:仅还原本条相关 Hook 差异、复制测试与本记录,保留其他工作区修改。 +- **关联**:`test/unit/clipboard.test.js`、`src/hooks/common/usePartnerWorkspaceEntry.js`、`src/pages/Organization/index.jsx`。 + +### 修复 · 渠道自定义模型填入后清空 + +- **范围**:`/console/channel` 创建渠道与更新渠道信息表单。 +- **变更**:点击自定义模型名称的“填入”后,同步清空组件状态与表单字段值,避免输入框保留已处理的名称。 +- **影响**:保留模型合并、逗号分隔、去重、提示与原有渠道保存逻辑。 +- **回滚**:仅还原 `EditChannelModal.jsx` 中本条新增的字段清空调用与本记录。 +- **关联**:`src/components/table/channels/modals/EditChannelModal.jsx`。 + ### 修复 · 平台 Skill 分类标签与搜索栏左对齐 - **范围**:`/console/skill-platform` 分类速览标签区域。 diff --git a/src/components/table/channels/modals/EditChannelModal.jsx b/src/components/table/channels/modals/EditChannelModal.jsx index 037e8a03..7fcf6d3d 100644 --- a/src/components/table/channels/modals/EditChannelModal.jsx +++ b/src/components/table/channels/modals/EditChannelModal.jsx @@ -1708,6 +1708,7 @@ const EditChannelModal = (props) => { setModelOptions(localModelOptions); setCustomModel(''); + formApiRef.current?.setValue('custom_model', ''); handleInputChange('models', localModels); if (addedModels.length > 0) { diff --git a/src/hooks/common/usePartnerWorkspaceEntry.js b/src/hooks/common/usePartnerWorkspaceEntry.js index b0505752..451c0ba2 100644 --- a/src/hooks/common/usePartnerWorkspaceEntry.js +++ b/src/hooks/common/usePartnerWorkspaceEntry.js @@ -17,7 +17,7 @@ along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ -import { useContext, useEffect, useState } from 'react'; +import { useContext, useEffect, useMemo, useState } from 'react'; import { API } from '../../helpers/api'; import { UserContext } from '../../context/User'; import { StatusContext } from '../../context/Status'; @@ -29,7 +29,10 @@ export function usePartnerWorkspaceAccess(organizationId = null) { const [userState] = useContext(UserContext); const [statusState] = useContext(StatusContext); const user = userState?.user; - const scope = partnerScope(user?.id, organizationId); + const scope = useMemo( + () => partnerScope(user?.id, organizationId), + [user?.id, organizationId], + ); const portalEnabled = statusState?.status?.commission_partner_portal_enabled; // 分佣总开关关闭时不发起伙伴身份探测;旧后端不返回该字段,视为开启。 const commissionEnabled = statusState?.status?.commission_enabled !== false; @@ -58,7 +61,7 @@ export function usePartnerWorkspaceAccess(organizationId = null) { return () => { cancelled = true; }; - }, [enabled, user, attempt, scope.key]); + }, [enabled, user, attempt, scope]); // 绑定本次登录的用户快照,切换账号时在 effect 执行前就隐藏旧入口。 let status = 'loading'; diff --git a/src/pages/Organization/index.jsx b/src/pages/Organization/index.jsx index cf6dd1ca..481a8cbb 100644 --- a/src/pages/Organization/index.jsx +++ b/src/pages/Organization/index.jsx @@ -456,9 +456,10 @@ const Organization = () => { }, [orgId, user]); useEffect(() => { + const requestState = orgRequest; fetchOrgInfo(); return () => { - orgRequest.current++; + requestState.current++; }; }, [fetchOrgInfo]); diff --git a/test/unit/clipboard.test.js b/test/unit/clipboard.test.js new file mode 100644 index 00000000..cebf9e1b --- /dev/null +++ b/test/unit/clipboard.test.js @@ -0,0 +1,331 @@ +/* +Copyright (C) 2025 QuantumNous + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as +published by the Free Software Foundation, either version 3 of the +License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . + +For commercial licensing, please contact support@quantumnous.com +*/ + +import assert from 'node:assert/strict'; +import test from 'node:test'; +import { readFile } from 'node:fs/promises'; +import { + copyText, + writeClipboardText, + writeClipboardImage, +} from '../../src/helpers/clipboard.js'; + +function globals(t, values) { + for (const [key, value] of Object.entries(values)) { + const descriptor = Object.getOwnPropertyDescriptor(globalThis, key); + Object.defineProperty(globalThis, key, { value, configurable: true }); + t.after(() => { + if (descriptor) Object.defineProperty(globalThis, key, descriptor); + else delete globalThis[key]; + }); + } +} + +function fallback({ result = true, throws = false, modal = false } = {}) { + const calls = []; + let textarea; + const container = { appendChild: () => calls.push(modal ? 'modal' : 'body') }; + const active = { + selectionStart: 2, + selectionEnd: 4, + selectionDirection: 'backward', + closest: () => (modal ? container : null), + focus: () => calls.push('restore-focus'), + setSelectionRange: (...range) => calls.push(['restore-input', ...range]), + }; + const doc = { + body: modal + ? { appendChild: () => assert.fail('must stay in modal') } + : container, + activeElement: active, + createElement: (tag) => { + assert.equal(tag, 'textarea'); + textarea = { + style: {}, + focus: () => calls.push('focus'), + select: () => calls.push('select'), + setSelectionRange: (...range) => calls.push(['select-range', ...range]), + remove: () => calls.push('remove'), + }; + return textarea; + }, + execCommand: (command) => { + assert.equal(command, 'copy'); + calls.push(['copy', textarea.value]); + if (throws) throw new Error('Copy blocked'); + return result; + }, + }; + return { doc, calls }; +} + +test('modern clipboard preserves full multiline and Unicode text', async (t) => { + const values = []; + globals(t, { + navigator: { + clipboard: { writeText: async (value) => values.push(value) }, + }, + document: undefined, + }); + const text = 'fixture-key-unmasked\n\u4e2d\u6587\nlast line'; + assert.equal(await copyText(text), true); + assert.deepEqual(values, [text]); +}); + +for (const mode of ['missing', 'partial', 'reject', 'sync-throw']) { + test(`text fallback works when Clipboard API is ${mode}`, async (t) => { + const { doc, calls } = fallback(); + const clipboard = + mode === 'partial' + ? {} + : { + writeText: + mode === 'reject' + ? async () => { + throw new Error('Permission denied'); + } + : () => { + throw new Error('Unavailable'); + }, + }; + globals(t, { + navigator: mode === 'missing' ? {} : { clipboard }, + document: doc, + }); + assert.equal(await copyText('complete-fixture-key'), true); + assert.ok( + calls.some( + (call) => + Array.isArray(call) && + call[0] === 'copy' && + call[1] === 'complete-fixture-key', + ), + ); + assert.ok(calls.includes('remove')); + assert.deepEqual(calls.at(-1), ['restore-input', 2, 4, 'backward']); + }); +} + +test('legacy selection stays within the active modal and restores DOM selection', async (t) => { + const { doc, calls } = fallback({ modal: true }); + const range = {}; + doc.getSelection = () => ({ + rangeCount: 1, + getRangeAt: () => ({ cloneRange: () => range }), + removeAllRanges: () => calls.push('clear-ranges'), + addRange: (value) => { + assert.equal(value, range); + calls.push('restore-range'); + }, + }); + globals(t, { navigator: {}, document: doc }); + assert.equal(await copyText('modal-fixture'), true); + assert.ok(calls.includes('modal')); + assert.equal(calls.at(-1), 'restore-range'); +}); + +for (const failure of [{ result: false }, { throws: true }]) { + test(`failed legacy copy reports failure and cleans up (${JSON.stringify(failure)})`, async (t) => { + const { doc, calls } = fallback(failure); + globals(t, { navigator: {}, document: doc }); + assert.equal(await copyText('fixture'), false); + await assert.rejects( + writeClipboardText('fixture'), + /Clipboard copy failed/, + ); + assert.equal(calls.filter((call) => call === 'remove').length, 2); + assert.ok(calls.includes('restore-focus')); + }); +} + +test('unsupported environments and absent values fail without throwing', async (t) => { + globals(t, { navigator: undefined, document: undefined }); + assert.equal(await copyText('fixture'), false); + assert.equal(await copyText(null), false); + assert.equal(await copyText(undefined), false); +}); + +test('zero and empty text are copied without converting them to undefined', async (t) => { + const values = []; + globals(t, { + navigator: { + clipboard: { writeText: async (value) => values.push(value) }, + }, + }); + assert.equal(await copyText(0), true); + assert.equal(await copyText(''), true); + assert.deepEqual(values, ['0', '']); +}); + +test('PNG image copy keeps the original blob', async (t) => { + const png = new Blob(['fixture'], { type: 'image/png' }); + const items = []; + globals(t, { + navigator: { clipboard: { write: async (value) => items.push(value) } }, + ClipboardItem: class { + constructor(data) { + this.data = data; + } + }, + }); + await writeClipboardImage(png); + assert.equal(items[0][0].data['image/png'], png); +}); + +test('JPEG image bytes are converted to PNG before writing and bitmap is released', async (t) => { + const png = new Blob(['converted'], { type: 'image/png' }); + let closed = false; + let written; + globals(t, { + navigator: { + clipboard: { + write: async (value) => { + written = value; + }, + }, + }, + ClipboardItem: class { + constructor(data) { + this.data = data; + } + }, + createImageBitmap: async () => ({ + width: 10, + height: 20, + close: () => { + closed = true; + }, + }), + document: { + createElement: () => ({ + getContext: () => ({ drawImage() {} }), + toBlob: (callback, type) => { + assert.equal(type, 'image/png'); + callback(png); + }, + }), + }, + }); + await writeClipboardImage(new Blob(['jpeg'], { type: 'image/jpeg' })); + assert.equal(written[0].data['image/png'], png); + assert.equal(closed, true); +}); + +test('unsupported image clipboard rejects instead of pretending success', async (t) => { + globals(t, { navigator: {}, ClipboardItem: undefined }); + await assert.rejects( + writeClipboardImage(new Blob()), + /Image clipboard unavailable/, + ); +}); + +const orgSource = ( + await readFile( + new URL('../../src/pages/Organization/OrgTokens.jsx', import.meta.url), + 'utf8', + ) +).replaceAll('\r\n', '\n'); +function orgHandler(name, bindings) { + const match = orgSource.match( + new RegExp(`(?:^|\n)([ \\t]*)const ${name} = ([\\s\\S]*?)\n\\1};`), + ); + assert.ok(match, `Missing current handler: ${name}`); + return new Function(...Object.keys(bindings), `return ${match[2]}\n};`)( + ...Object.values(bindings), + ); +} +for (const name of ['copyText', 'handleCopyKey', 'handleCopyModel']) { + for (const succeeds of [true, false]) { + test(`org tokens ${name} reports the actual copy result (${succeeds})`, async () => { + const feedback = []; + const copied = []; + const state = []; + const cleared = []; + const timers = []; + const t = (text) => text; + const copyText = orgHandler('copyText', { + copy: async (text) => { + copied.push(text); + return succeeds; + }, + Toast: { + success: (text) => feedback.push(['success', text]), + error: (text) => feedback.push(['error', text]), + }, + }); + const handler = + name === 'copyText' + ? (text) => copyText(text, t) + : orgHandler(name, { + copyText, + t, + loadFullKey: async (id) => { + assert.equal(id, 42); + return 'full-fixture-key'; + }, + setCopiedId: (value) => state.push(value), + setCopiedModel: (value) => state.push(value), + copiedTimer: { current: 'previous-key-timer' }, + copiedModelTimer: { current: 'previous-model-timer' }, + clearTimeout: (value) => cleared.push(value), + setTimeout: (callback, delay) => { + assert.equal(delay, 1500); + timers.push(callback); + return 'next-timer'; + }, + }); + const result = await handler( + name === 'handleCopyKey' ? 42 : 'full-fixture-key', + ); + assert.deepEqual(copied, ['full-fixture-key']); + assert.deepEqual(feedback, [ + [ + succeeds ? 'success' : 'error', + succeeds + ? name === 'handleCopyModel' + ? '已复制模型名称' + : '已复制到剪贴板' + : '复制失败,请手动复制', + ], + ]); + if (name === 'copyText') assert.equal(result, succeeds); + else { + assert.deepEqual( + state, + succeeds ? [name === 'handleCopyKey' ? 42 : 'full-fixture-key'] : [], + ); + assert.deepEqual( + cleared, + succeeds + ? [ + name === 'handleCopyKey' + ? 'previous-key-timer' + : 'previous-model-timer', + ] + : [], + ); + assert.equal(timers.length, succeeds ? 1 : 0); + if (succeeds) { + timers[0](); + assert.equal(state.at(-1), null); + } + } + }); + } +}