zcodeAgentBrowserAmbientContext.test.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { describe, expect, it, vi } from "vitest";
  2. import {
  3. collectBrowserAmbientContext,
  4. type BrowserAmbientContextExecutor,
  5. } from "../src/zcode-agent/zcodeAgentBrowserAmbientContext.js";
  6. const descriptor = {
  7. id: "iab-runtime",
  8. generation: 7,
  9. type: "iab" as const,
  10. name: "ZCode In-app Browser",
  11. capabilities: {},
  12. };
  13. describe("collectBrowserAmbientContext", () => {
  14. it("merges controlled and user tabs and preserves remote routing context", async () => {
  15. const list = vi.fn(async () => [descriptor]);
  16. const execute = vi.fn(async (input: { command: { method: string } }) =>
  17. input.command.method === "list"
  18. ? {
  19. ok: true,
  20. tabs: [
  21. {
  22. tabId: "controlled-1",
  23. url: "https://example.com/background",
  24. title: "Background",
  25. viewport: { width: 1280, height: 720 },
  26. },
  27. ],
  28. elapsedMs: 0,
  29. }
  30. : {
  31. ok: true,
  32. userTabs: [
  33. {
  34. id: "user-1",
  35. url: "https://user:password@example.com/current?q=1",
  36. title: "Current",
  37. },
  38. ],
  39. elapsedMs: 0,
  40. },
  41. );
  42. const executor = {
  43. list,
  44. execute,
  45. } as unknown as BrowserAmbientContextExecutor;
  46. await expect(
  47. collectBrowserAmbientContext(executor, {
  48. sessionId: "sess-1",
  49. workspacePath: "/workspace",
  50. workspaceIdentity: "remote:ssh:dev:/workspace",
  51. remoteSessionId: "remote-1",
  52. clientMode: "web-remote-replayable",
  53. }),
  54. ).resolves.toEqual({
  55. tabCount: 2,
  56. currentUrl: "https://example.com/current?q=1",
  57. });
  58. expect(list).toHaveBeenCalledWith(
  59. expect.objectContaining({
  60. workspaceKey: "remote:ssh:dev:/workspace",
  61. remoteSessionId: "remote-1",
  62. clientMode: "web-remote-replayable",
  63. }),
  64. );
  65. expect(execute).toHaveBeenCalledTimes(2);
  66. expect(execute).toHaveBeenCalledWith(
  67. expect.objectContaining({
  68. browserId: "iab-runtime",
  69. browserGeneration: 7,
  70. workspaceIdentity: "remote:ssh:dev:/workspace",
  71. remoteSessionId: "remote-1",
  72. clientMode: "web-remote-replayable",
  73. }),
  74. );
  75. });
  76. it("prefers an active controlled tab and degrades when the bridge fails", async () => {
  77. const executor: BrowserAmbientContextExecutor = {
  78. list: vi.fn(async () => [descriptor]),
  79. execute: vi.fn(async (input) =>
  80. input.command.method === "list"
  81. ? {
  82. ok: true,
  83. tabs: [
  84. {
  85. tabId: "controlled-1",
  86. url: "https://example.com/active",
  87. title: "Active",
  88. active: true,
  89. viewport: { width: 1280, height: 720 },
  90. },
  91. ],
  92. elapsedMs: 0,
  93. }
  94. : { ok: true, userTabs: [], elapsedMs: 0 },
  95. ),
  96. };
  97. await expect(
  98. collectBrowserAmbientContext(executor, {
  99. sessionId: "sess-1",
  100. workspacePath: "/workspace",
  101. }),
  102. ).resolves.toEqual({
  103. tabCount: 1,
  104. currentUrl: "https://example.com/active",
  105. });
  106. await expect(
  107. collectBrowserAmbientContext(
  108. {
  109. list: vi.fn(async () => {
  110. throw new Error("bridge unavailable");
  111. }),
  112. execute: vi.fn(),
  113. },
  114. { sessionId: "sess-1", workspacePath: "/workspace" },
  115. ),
  116. ).resolves.toBeUndefined();
  117. });
  118. });