Skip to content

fix: keep per-provider bridge methods live after the api is handed out - #5

Closed
z91300 wants to merge 1 commit into
koi128bit:mainfrom
z91300:fix/provider-bridge-live-methods
Closed

z91300 wants to merge 1 commit into
koi128bit:mainfrom
z91300:fix/provider-bridge-live-methods

Conversation

@z91300

@z91300 z91300 commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

背景

#3 修掉了「点添加账号报 proxy 不变量错误」,但把 apiFor() 从「Proxy 实时读取」改成了「一次性拷贝方法值」,引入了回归:main 上的 npm run test:ui 现在是失败的(macOS 作业,run 40266561479)。本 PR 修复这个回归。

失败的 3 个用例:

  • overview handles absent usage, quotas and API state without manufactured chart data
  • extended task confirmation separates compatibility records from paid actions and shows each manual reason
  • native popup layout shows the original icon, credits and reward actions

原因

这几个用例会在页面挂载后替换 bridge 上的方法(tests/ui.spec.ts 的 window.workbuddy.snapshot = …、Object.assign(window.workbuddy, { … }) 等)。原来的 Proxy 通过 get trap 在调用时读取,所以能看到替换后的函数;而 #3 的实现把这些函数值拷贝进了普通对象,apiFor() 第一次调用时就把旧函数固定下来,App 之后一直用旧函数 —— 于是「暂无用量记录」的空状态不出现、任务上限从 1 变成 2,断言自然对不上。

修复

拷贝改成 getter 实时读取,其余结构不变:

function copyMethods(target: Bridge): Methods {
  const methods: Methods = {};
  for (const key of Object.getOwnPropertyNames(target))
    Object.defineProperty(methods, key, {
      get: () => target[key],
      enumerable: true,
      configurable: true,
    });
  return methods;
}
  • 按地区改写的方法(登录四个方法、workbuddy-global 的 scope 转发与「仅国内版」拦截)用 override() 定义成数据属性;登录包装也改成调用时再读 target[key],替换后依然生效。
  • 依旧不读也不改写 target 上那些不可写属性,所以 fix: keep contextBridge login methods callable instead of proxying them #3 修掉的代理不变量错误不会复发。

验证

  • 新增回归用例:a bridge method replaced after the api was handed out is still used —— 先取 api,再替换 bridge 上的方法,断言 api 用的是新函数。修复前该用例失败。
  • npx playwright test(完整 36 项 UI 用例,Windows + 本地 Chromium):36 通过,含上面失败的 3 项。
  • npm test:205 通过,0 失败,2 跳过。
  • npm run typecheck、npm run build 通过。

说明

#4(下拉框弹层与聚焦环)当前 CI 会是红的,因为它基于这之前的 main;本 PR 合并后我会把 #4 rebase 到最新 main 再确认。

@z91300

z91300 commented Oct 26, 2026

Copy link
Copy Markdown
Contributor Author

我先看到了 main 上的 039d46b / 60221c1 / acc5a0f:你选择了保留 #3 的生产代码,把 UI fixture 改成不可变的原生风格桥,并用 window.workbuddyFixture 让测试重新配置 handler,同时把我的测试文件换成 tests/provider-bridge.test.mjs。这个方向更贴合 contextBridge 的真实语义,我认同。

我起初是按「恢复 Proxy 的实时读取」来修这 3 个 UI 用例如的,但既然 fixture 现在不可变、生产代码也不该依赖可变桥,那我这份改动就是多余的了,而且会在 main 上重新造一个 tests/providers.test.mjs。所以我把它关掉,不给仓库添噪音。

本机验证结果留档(万一以后需要):getter 方案下 npx playwright test 36 项全绿,npm test 205 通过。分支我随后删除。

另外 #4(下拉框弹层 / 聚焦环)我会 rebase 到最新 main 上重跑,稍后更新。

@z91300 z91300 closed this Oct 26, 2026
@z91300
z91300 deleted the fix/provider-bridge-live-methods branch October 26, 2026 04:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant