Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Launch: a server swap with no window leaves the opening that started it alone

Liame8638a639d6a
diff --git a/electron/main.mjs b/electron/main.mjs
index 74d3a376a4..feb7af9b2e 100644
--- a/electron/main.mjs
+++ b/electron/main.mjs
@@ -1622,7 +1622,7 @@ async function switchLocalEnvironmentTo(targetDir, targetId, quiet = false) {
return failedSwitch();
}
switchSucceeded = true;
- if (windowFollows()) navigateMainWindow(rendererOrigin());
+ if (windowFollows() && mainWindow && !mainWindow.isDestroyed()) navigateMainWindow(rendererOrigin());
return { ok: true };
} catch (error) {
slog(`environment switch to ${targetDir} failed (${error?.message ?? error})`);
diff --git a/electron/server-supervisor.node-test.mjs b/electron/server-supervisor.node-test.mjs
index d118fc32a0..90431a5347 100644
--- a/electron/server-supervisor.node-test.mjs
+++ b/electron/server-supervisor.node-test.mjs
@@ -423,10 +423,11 @@ test("a purge of a missing folder still removes the entry (rm runs with force)",
assert.deepEqual(plain(rmOptions), [{ recursive: true, force: true }]);
});
-function switchFixture(t, { startResults = [], releaseLeaseThrows = false, stopUtilityServer = async () => true, acquireThrowsOn = [] } = {}) {
+function switchFixture(t, { startResults = [], releaseLeaseThrows = false, stopUtilityServer = async () => true, acquireThrowsOn = [], mainWindow = null } = {}) {
const targetDir = mkdtempSync(join(tmpdir(), "omb-switch-fixture-"));
t.after(() => rmSync(targetDir, { recursive: true, force: true }));
const forks = [];
+ const navigated = [];
let forkIndex = 0;
let acquires = 0;
const context = vm.createContext({
@@ -462,7 +463,8 @@ function switchFixture(t, { startResults = [], releaseLeaseThrows = false, stopU
startupEnvironmentDir: env.startupEnvironmentDir,
withActive: env.withActive,
persistEnvironments: (next) => { context.environmentsState = next; },
- navigateMainWindow: () => {},
+ mainWindow,
+ navigateMainWindow: (url) => navigated.push(url),
placeSwitch: { turn: () => 0 },
rendererOrigin: () => "http://127.0.0.1:8799",
dialog: { showErrorBox: () => {} },
@@ -473,7 +475,7 @@ function switchFixture(t, { startResults = [], releaseLeaseThrows = false, stopU
const body = MAIN_SOURCE.slice(start, MAIN_SOURCE.indexOf("\nasync function switchEnvironment", start));
assert.ok(start > 0 && body.length > 0, "the switch helper exists");
vm.runInContext(`${body}; this.switchLocalEnvironmentTo = switchLocalEnvironmentTo;`, context);
- return { context, forks, targetDir };
+ return { context, forks, navigated, targetDir };
}
test("a failed switch with no live child reforks the active environment once and adopts it", async (t) => {
@@ -512,6 +514,20 @@ test("a successful switch never runs the safety net", async (t) => {
assert.equal(forks.length, 1, "only the switch's own startChild fork");
});
+test("a successful switch before the main window exists navigates nothing, so the opening that asked for it is not cancelled", async (t) => {
+ const { context, navigated, targetDir } = switchFixture(t);
+ const result = await context.switchLocalEnvironmentTo(targetDir, "l1");
+ assert.deepEqual(plain(result), { ok: true });
+ assert.deepEqual(navigated, []);
+});
+
+test("a successful switch moves a live main window to the new server", async (t) => {
+ const { context, navigated, targetDir } = switchFixture(t, { mainWindow: { isDestroyed: () => false } });
+ const result = await context.switchLocalEnvironmentTo(targetDir, "l1");
+ assert.deepEqual(plain(result), { ok: true });
+ assert.deepEqual(navigated, ["http://127.0.0.1:8799"]);
+});
+
test("a child that will not stop aborts the switch before its lease is handed away", async (t) => {
const { context, forks, targetDir } = switchFixture(t, { stopUtilityServer: async () => false });
context.serverProc = { pid: 1 };