mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-10-09 15:58:34 +00:00
d92406a9fd
针对 PR #3238 的 2 条 Codex review 意见修复: - application/state/settingsIpcSync.ts / useSettingsState.ts:新增 STORAGE_KEY_AUTO_LAUNCH_ENABLED 的跨窗口广播处理。AppLockGate 会 在每个窗口(主窗口、托盘面板、终端弹窗等)都挂载一份 useSettingsState,即使开机自启动开关只在设置窗口的 UI 里展示和 交互。此前该键缺失在 settingsIpcSync.ts 的广播处理里,其他窗口 收到设置窗口推送的 notifySettingsChanged 广播后不会更新自己 内存里的 autoLaunchEnabled,与项目里几乎所有其它布尔类设置的 跨窗口同步约定不一致。新增结构断言测试验证键处理与端到端接线 都已补齐。 - application/state/systemSettingsEffects.ts:新增 autoLaunchWriteGenerationRef,推送 effect 的写入响应只在"没有 更新的写入请求已经发起"时才被采纳。此前连续快速切换开关会启动 多个并发的 bridge.setAutoLaunch 调用,每个回调只跟自己请求时的 闭包值比较,不知道是否已经有更晚的请求覆盖了它——一个陈旧请求 的延迟响应(比如报告了与自己请求不符的状态)可能在更新的请求 已经成功落地之后才到达,用过时的"纠正"把已经确认生效的最新 状态覆盖掉。新增单测覆盖乱序到达、连续三次切换等场景。 新增/更新单测覆盖上述两个场景,199 项相关测试通过。 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
59 lines
2.6 KiB
TypeScript
59 lines
2.6 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import fs from 'node:fs';
|
|
import test from 'node:test';
|
|
|
|
/**
|
|
* useSettingsIpcSync is a React hook (useEffect-driven); without
|
|
* react-test-renderer available in this environment we can't mount it and
|
|
* assert on live cross-window broadcast behavior. These structural checks
|
|
* instead verify the source wiring end-to-end: every other window's
|
|
* useSettingsState instance (mounted via AppLockGate, even though the
|
|
* auto-launch toggle only ever renders in the Settings window) must consume
|
|
* the STORAGE_KEY_AUTO_LAUNCH_ENABLED broadcast, matching the established
|
|
* pattern used by every other synchronized boolean setting in this file.
|
|
*/
|
|
|
|
test('settingsIpcSync handles the auto-launch broadcast key like other synced booleans', () => {
|
|
const source = fs.readFileSync(new URL('./settingsIpcSync.ts', import.meta.url), 'utf8');
|
|
|
|
assert.match(
|
|
source,
|
|
/STORAGE_KEY_AUTO_LAUNCH_ENABLED/,
|
|
'must import the storage key',
|
|
);
|
|
assert.match(
|
|
source,
|
|
/if \(key === STORAGE_KEY_AUTO_LAUNCH_ENABLED && typeof value === 'boolean'\) \{\s*[\s\S]*?setAutoLaunchEnabled\(/,
|
|
'must apply the broadcast value to local state, same as e.g. STORAGE_KEY_GLOBAL_HOTKEY_ENABLED',
|
|
);
|
|
assert.match(
|
|
source,
|
|
/setAutoLaunchEnabled: Dispatch<SetStateAction<boolean>>/,
|
|
'the setter prop must be declared on the params interface',
|
|
);
|
|
|
|
// The dependency array of the effect must list the setter, or the
|
|
// listener could close over a stale setter reference after a re-render.
|
|
const effectStart = source.indexOf('useEffect(() => {');
|
|
const depsStart = source.indexOf('}, [', effectStart);
|
|
assert.ok(effectStart > -1 && depsStart > effectStart, 'expected to find the sync effect and its deps array');
|
|
const depsSlice = source.slice(depsStart, source.indexOf(']);', depsStart));
|
|
assert.match(depsSlice, /setAutoLaunchEnabled/, 'setAutoLaunchEnabled must be listed in the effect deps array');
|
|
});
|
|
|
|
test('useSettingsState wires the real autoLaunchEnabled setter into useSettingsIpcSync (not a no-op)', () => {
|
|
const source = fs.readFileSync(new URL('./useSettingsState.ts', import.meta.url), 'utf8');
|
|
|
|
const hookCallStart = source.indexOf('useSettingsIpcSync({');
|
|
assert.notEqual(hookCallStart, -1, 'expected to find the useSettingsIpcSync(...) call');
|
|
const hookCallEnd = source.indexOf('});', hookCallStart);
|
|
const callSlice = source.slice(hookCallStart, hookCallEnd);
|
|
|
|
assert.match(
|
|
callSlice,
|
|
/setAutoLaunchEnabled,/,
|
|
'useSettingsIpcSync must receive the real setAutoLaunchEnabled state setter, ' +
|
|
'or a broadcast from another window silently does nothing in this one',
|
|
);
|
|
});
|