
























@@ -4,16 +4,18 @@ import path from "node:path";
44import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
5566const closeTrackedBrowserTabsForSessionsImpl = vi.hoisted(() => vi.fn());
7-const loadBundledPluginPublicSurfaceModuleSync = vi.hoisted(() => vi.fn());
7+const canLoadActivatedBundledPluginPublicSurface = vi.hoisted(() => vi.fn());
8+const tryLoadActivatedBundledPluginPublicSurfaceModuleSync = vi.hoisted(() => vi.fn());
89const runExec = vi.hoisted(() => vi.fn());
910const realMkdirSync = fs.mkdirSync.bind(fs);
1011const realMkdtempSync = fs.mkdtempSync.bind(fs);
1112const realRmSync = fs.rmSync.bind(fs);
1213const realWriteFileSync = fs.writeFileSync.bind(fs);
1314const realRealpathSyncNative = fs.realpathSync.native.bind(fs.realpathSync);
141515-vi.mock("./facade-loader.js", () => ({
16- loadBundledPluginPublicSurfaceModuleSync,
16+vi.mock("./facade-runtime.js", () => ({
17+ canLoadActivatedBundledPluginPublicSurface,
18+ tryLoadActivatedBundledPluginPublicSurfaceModuleSync,
1719}));
18201921vi.mock("../process/exec.js", () => ({
@@ -38,6 +40,7 @@ describe("browser maintenance", () => {
38403941beforeEach(() => {
4042vi.restoreAllMocks();
43+vi.resetModules();
4144testRoot = realRealpathSyncNative(
4245realMkdtempSync(path.join(os.tmpdir(), "openclaw-browser-maintenance-")),
4346);
@@ -46,15 +49,17 @@ describe("browser maintenance", () => {
4649realMkdirSync(path.join(homeDir, ".Trash"), { recursive: true, mode: 0o700 });
4750realMkdirSync(tmpDir, { recursive: true, mode: 0o700 });
4851closeTrackedBrowserTabsForSessionsImpl.mockReset();
49-loadBundledPluginPublicSurfaceModuleSync.mockReset();
52+canLoadActivatedBundledPluginPublicSurface.mockReset();
53+tryLoadActivatedBundledPluginPublicSurfaceModuleSync.mockReset();
5054runExec.mockReset();
5155vi.spyOn(Date, "now").mockReturnValue(123);
5256vi.spyOn(os, "homedir").mockReturnValue(homeDir);
5357vi.spyOn(os, "tmpdir").mockReturnValue(tmpDir);
5458vi.spyOn(fs.realpathSync, "native").mockImplementation((candidate) =>
5559realRealpathSyncNative(candidate),
5660);
57-loadBundledPluginPublicSurfaceModuleSync.mockReturnValue({
61+canLoadActivatedBundledPluginPublicSurface.mockReturnValue(true);
62+tryLoadActivatedBundledPluginPublicSurfaceModuleSync.mockReturnValue({
5863closeTrackedBrowserTabsForSessions: closeTrackedBrowserTabsForSessionsImpl,
5964});
6065});
@@ -76,7 +81,39 @@ describe("browser maintenance", () => {
7681const { closeTrackedBrowserTabsForSessions } = await import("./browser-maintenance.js");
77827883await expect(closeTrackedBrowserTabsForSessions({ sessionKeys: [] })).resolves.toBe(0);
79-expect(loadBundledPluginPublicSurfaceModuleSync).not.toHaveBeenCalled();
84+expect(tryLoadActivatedBundledPluginPublicSurfaceModuleSync).not.toHaveBeenCalled();
85+});
86+87+it("skips browser cleanup when the browser plugin is disabled", async () => {
88+canLoadActivatedBundledPluginPublicSurface.mockReturnValue(false);
89+90+const { closeTrackedBrowserTabsForSessions } = await import("./browser-maintenance.js");
91+92+await expect(
93+closeTrackedBrowserTabsForSessions({ sessionKeys: ["agent:main:test"] }),
94+).resolves.toBe(0);
95+expect(canLoadActivatedBundledPluginPublicSurface).toHaveBeenCalledWith({
96+dirName: "browser",
97+artifactBasename: "browser-maintenance.js",
98+});
99+expect(tryLoadActivatedBundledPluginPublicSurfaceModuleSync).not.toHaveBeenCalled();
100+expect(closeTrackedBrowserTabsForSessionsImpl).not.toHaveBeenCalled();
101+});
102+103+it("rechecks plugin activation before using a cached browser cleanup surface", async () => {
104+closeTrackedBrowserTabsForSessionsImpl.mockResolvedValue(2);
105+106+const { closeTrackedBrowserTabsForSessions } = await import("./browser-maintenance.js");
107+108+await expect(
109+closeTrackedBrowserTabsForSessions({ sessionKeys: ["agent:main:test"] }),
110+).resolves.toBe(2);
111+canLoadActivatedBundledPluginPublicSurface.mockReturnValue(false);
112+await expect(
113+closeTrackedBrowserTabsForSessions({ sessionKeys: ["agent:main:test"] }),
114+).resolves.toBe(0);
115+116+expect(closeTrackedBrowserTabsForSessionsImpl).toHaveBeenCalledTimes(1);
80117});
8111882119it("delegates cleanup through the browser maintenance surface", async () => {
@@ -87,7 +124,7 @@ describe("browser maintenance", () => {
87124await expect(
88125closeTrackedBrowserTabsForSessions({ sessionKeys: ["agent:main:test"] }),
89126).resolves.toBe(2);
90-expect(loadBundledPluginPublicSurfaceModuleSync).toHaveBeenCalledWith({
127+expect(tryLoadActivatedBundledPluginPublicSurfaceModuleSync).toHaveBeenCalledWith({
91128dirName: "browser",
92129artifactBasename: "browser-maintenance.js",
93130});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。