browserCloseTabNotification.test.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { describe, expect, it } from "vitest";
  2. import { buildBrowserViewCloseTabNotification } from "../src/main/browserView/browserCloseTabNotification.js";
  3. /**
  4. * 这条链路此前完全没有测试防护:字段拼装原先内联在 `index.ts` 的 manager 构造回调里(Electron 入口不进单测),
  5. * 而唯一的 E2E(BTC01)走 `closeTabFromRenderer` → `notifyRenderer=false`,结构上就不经过这段代码。
  6. * 拼错字段名或漏掉 scope 会让"用户切走 workspace 后 Agent 关 tab"重新退化成关不掉的幽灵 tab,
  7. * 但 main 单测、renderer 单测、E2E 会全部保持绿色 —— 所以这里必须逐字段精确断言。
  8. */
  9. describe("buildBrowserViewCloseTabNotification", () => {
  10. it("remote workspace 的 owner 逐字段落到 renderer 能路由的 scope 上", () => {
  11. // 三个值彼此明显不同,字段串位(例如把 sessionId 写进 workspaceKey)会立刻被 toEqual 抓住。
  12. expect(
  13. buildBrowserViewCloseTabNotification("browser:tab-a", {
  14. workspaceKey: "ssh://host/repo",
  15. sessionId: "session-a",
  16. remoteSessionId: "remote-a",
  17. }),
  18. ).toEqual({
  19. tabId: "browser:tab-a",
  20. workspaceKey: "ssh://host/repo",
  21. sessionId: "session-a",
  22. remoteSessionId: "remote-a",
  23. });
  24. });
  25. it("本地 workspace 不产出 remoteSessionId 这个 key,而不是给它一个 undefined", () => {
  26. const notification = buildBrowserViewCloseTabNotification("browser:tab-b", {
  27. workspaceKey: "/repo",
  28. sessionId: "session-b",
  29. });
  30. expect(notification).toEqual({
  31. tabId: "browser:tab-b",
  32. workspaceKey: "/repo",
  33. sessionId: "session-b",
  34. });
  35. // renderer 按“字段是否存在”判定作用域,显式 undefined 会经 IPC 结构化克隆后表现不同。
  36. expect(Object.hasOwn(notification, "remoteSessionId")).toBe(false);
  37. });
  38. it("空字符串 remoteSessionId 视为缺席,避免 renderer 拿到空 scope 误判 remote", () => {
  39. const notification = buildBrowserViewCloseTabNotification("browser:tab-c", {
  40. workspaceKey: "/repo",
  41. sessionId: "session-c",
  42. remoteSessionId: "",
  43. });
  44. expect(Object.hasOwn(notification, "remoteSessionId")).toBe(false);
  45. });
  46. it("owner 缺省时只带 tabId,保留 recovery-orphan 路径的“仅当前 workspace”语义", () => {
  47. const notification = buildBrowserViewCloseTabNotification("browser:tab-d");
  48. expect(notification).toEqual({ tabId: "browser:tab-d" });
  49. expect(Object.hasOwn(notification, "workspaceKey")).toBe(false);
  50. expect(Object.hasOwn(notification, "sessionId")).toBe(false);
  51. });
  52. });