browser-selection.test.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. selectBrowserForUrl,
  4. selectDefaultBrowser,
  5. type BrowserInfo,
  6. } from "../src/browser-client/index.js";
  7. function info(
  8. id: string,
  9. type: BrowserInfo["type"],
  10. metadata?: Record<string, string>,
  11. ): BrowserInfo {
  12. return {
  13. id,
  14. generation: 1,
  15. type,
  16. name: id,
  17. capabilities: {},
  18. ...(metadata ? { metadata } : {}),
  19. };
  20. }
  21. describe("browser backend selection", () => {
  22. const iab = info("iab-1", "iab");
  23. const extension = info("ext-1", "extension");
  24. const preferredExtension = info("ext-2", "extension", { preferred: "true" });
  25. const cdp = info("cdp-1", "cdp");
  26. it("selects the default browser by backend priority", () => {
  27. expect(selectDefaultBrowser([cdp, extension, preferredExtension, iab])).toBe(iab);
  28. expect(selectDefaultBrowser([cdp, extension, preferredExtension])).toBe(preferredExtension);
  29. expect(selectDefaultBrowser([cdp, extension])).toBe(extension);
  30. expect(selectDefaultBrowser([cdp])).toBe(cdp);
  31. expect(selectDefaultBrowser([])).toBeUndefined();
  32. expect(
  33. selectDefaultBrowser([
  34. extension,
  35. info("ext-live", "extension", { profileIsLastUsed: "true" }),
  36. ])?.id,
  37. ).toBe("ext-live");
  38. expect(
  39. selectDefaultBrowser([extension, info("ext-first", "extension", { profileOrdering: "0" })])
  40. ?.id,
  41. ).toBe("ext-first");
  42. });
  43. it("routes localhost and file targets to IAB", () => {
  44. const tabs = new Map<string, string[]>();
  45. expect(selectBrowserForUrl([extension, iab], "http://localhost:3000", tabs)).toBe(iab);
  46. expect(selectBrowserForUrl([extension, iab], "file:///tmp/index.html", tabs)).toBe(iab);
  47. });
  48. it("ranks existing tabs by exact, path, host, then host hierarchy", () => {
  49. const infos = [iab, extension, preferredExtension, cdp];
  50. const exact = new Map<string, string[]>([
  51. [iab.id, ["https://example.com/other"]],
  52. [extension.id, ["https://example.com/app?q=1#old"]],
  53. [preferredExtension.id, ["https://child.example.com/app"]],
  54. ]);
  55. expect(selectBrowserForUrl(infos, "https://example.com/app?q=1#new", exact)).toBe(extension);
  56. const samePath = new Map<string, string[]>([
  57. [iab.id, ["https://example.com/app?other=1"]],
  58. [extension.id, ["https://example.com/other"]],
  59. ]);
  60. expect(selectBrowserForUrl(infos, "https://example.com/app?q=1", samePath)).toBe(iab);
  61. const hierarchy = new Map<string, string[]>([
  62. [extension.id, ["https://www.example.com/home"]],
  63. [preferredExtension.id, ["https://unrelated.test/"]],
  64. ]);
  65. expect(selectBrowserForUrl(infos, "https://example.com/path", hierarchy)).toBe(extension);
  66. });
  67. it("uses backend priority to break equal matches and rejects bad multi-backend URLs", () => {
  68. const equal = new Map<string, string[]>([
  69. [iab.id, ["https://example.com/"]],
  70. [preferredExtension.id, ["https://example.com/"]],
  71. ]);
  72. expect(selectBrowserForUrl([preferredExtension, iab], "https://example.com/", equal)).toBe(iab);
  73. expect(() => selectBrowserForUrl([iab, extension], "not a url", new Map())).toThrow(
  74. /Invalid browser target URL/,
  75. );
  76. });
  77. it("does not treat single-label hosts as parent/child hostnames", () => {
  78. expect(
  79. selectBrowserForUrl(
  80. [cdp, extension],
  81. "https://x.localhost/",
  82. new Map([[cdp.id, ["https://localhost/"]]]),
  83. ),
  84. ).toBe(extension);
  85. });
  86. });