Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
import type { DesktopLogAnnotations } from "../app/DesktopObservability.ts";
import { assert, describe, it } from "@effect/vitest";
import * as Deferred from "effect/Deferred";
import * as Effect from "effect/Effect";
import * as Fiber from "effect/Fiber";
import * as Layer from "effect/Layer";
import * as Logger from "effect/Logger";
import * as Option from "effect/Option";
import * as Ref from "effect/Ref";
import * as References from "effect/References";
import * as TestClock from "effect/testing/TestClock";
import type * as Electron from "electron";
import { vi } from "vite-plus/test";
import * as DesktopAppSettings from "../settings/DesktopAppSettings.ts";
import * as DesktopWindow from "./DesktopWindow.ts";
import {
failDisplayLookupOnce,
makeFakeBrowserWindow,
makeTestLayer,
} from "./test-support/DesktopWindowHarness.ts";
describe("DesktopWindow", () => {
it("restores bounds only when the window fits within a connected display", () => {
const persistedBounds = { x: 2040, y: 80, width: 1320, height: 880 };
const displays = [
{ x: 0, y: 0, width: 1920, height: 1080 },
{ x: 1920, y: 0, width: 2560, height: 1440 },
];
assert.deepEqual(
DesktopWindow.resolveInitialMainWindowBounds(persistedBounds, displays),
persistedBounds,
);
assert.deepEqual(
DesktopWindow.resolveInitialMainWindowBounds(persistedBounds, [displays[0]!]),
DesktopAppSettings.DEFAULT_MAIN_WINDOW_SIZE,
);
});
it.effect("does not open a development window until the backend is ready", () =>
Effect.gen(function* () {
const fakeWindow = makeFakeBrowserWindow();
const createCount = yield* Ref.make(0);
const mainWindow = yield* Ref.make<Option.Option<Electron.BrowserWindow>>(Option.none());
const createdWindowOptions: Electron.BrowserWindowConstructorOptions[] = [];
const layer = makeTestLayer({
window: fakeWindow.window,
createCount,
mainWindow,
createdWindowOptions,
});
yield* Effect.gen(function* () {
const desktopWindow = yield* DesktopWindow.DesktopWindow;
yield* desktopWindow.activate;
assert.equal(yield* Ref.get(createCount), 0);
yield* desktopWindow.handleBackendReady(new URL("http://127.0.0.1:3773"));
assert.equal(yield* Ref.get(createCount), 1);
assert.equal(createdWindowOptions[0]?.width, 1100);
assert.equal(createdWindowOptions[0]?.height, 780);
assert.isUndefined(createdWindowOptions[0]?.x);
assert.isUndefined(createdWindowOptions[0]?.y);
assert.isTrue(createdWindowOptions[0]?.disableAutoHideCursor);
assert.isFalse(createdWindowOptions[0]?.webPreferences?.backgroundThrottling);
assert.deepEqual(fakeWindow.setAutoHideCursor.mock.calls, [[false]]);
assert.deepEqual(fakeWindow.loadURL.mock.calls[0], ["akeru-dev://app/"]);
assert.equal(fakeWindow.openDevTools.mock.calls.length, 0);
}).pipe(Effect.provide(layer));
}),
);
it.effect("uses the persisted main window bounds when opening the window", () =>
Effect.gen(function* () {
const fakeWindow = makeFakeBrowserWindow();
const createCount = yield* Ref.make(0);
const mainWindow = yield* Ref.make<Option.Option<Electron.BrowserWindow>>(Option.none());
const createdWindowOptions: Electron.BrowserWindowConstructorOptions[] = [];
const layer = makeTestLayer({
window: fakeWindow.window,
createCount,
mainWindow,
createdWindowOptions,
desktopSettings: {
...DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS,
mainWindowBounds: { x: 120, y: 80, width: 1320, height: 880 },
},
});
yield* Effect.gen(function* () {
const desktopWindow = yield* DesktopWindow.DesktopWindow;
yield* desktopWindow.handleBackendReady(new URL("http://127.0.0.1:3773"));
assert.equal(createdWindowOptions[0]?.width, 1320);
assert.equal(createdWindowOptions[0]?.height, 880);
assert.equal(createdWindowOptions[0]?.x, 120);
assert.equal(createdWindowOptions[0]?.y, 80);
}).pipe(Effect.provide(layer));
}),
);
it.effect("restores the persisted maximized state", () =>
Effect.gen(function* () {
const fakeWindow = makeFakeBrowserWindow();
const createCount = yield* Ref.make(0);
const mainWindow = yield* Ref.make<Option.Option<Electron.BrowserWindow>>(Option.none());
const layer = makeTestLayer({
window: fakeWindow.window,
createCount,
mainWindow,
desktopSettings: {
...DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS,
mainWindowBounds: { x: 120, y: 80, width: 1320, height: 880 },
mainWindowMaximized: true,
},
});
yield* Effect.gen(function* () {
const desktopWindow = yield* DesktopWindow.DesktopWindow;
yield* desktopWindow.handleBackendReady(new URL("http://127.0.0.1:3773"));
assert.equal(fakeWindow.maximize.mock.calls.length, 0);
const readyToShow = fakeWindow.windowListeners.get("ready-to-show");
if (!readyToShow) {
return yield* Effect.die("window ready-to-show listener was not registered");
}
readyToShow();
assert.equal(fakeWindow.maximize.mock.calls.length, 1);
}).pipe(Effect.provide(layer));
}),
);
// The window boots hidden with throttling disabled so first paint runs at
// full speed; the first reveal must hand it back to normal hidden-window
// throttling or a minimized window stays expensive forever.
it.effect("re-enables background throttling on first reveal", () =>
Effect.gen(function* () {
const fakeWindow = makeFakeBrowserWindow();
const createCount = yield* Ref.make(0);
const mainWindow = yield* Ref.make<Option.Option<Electron.BrowserWindow>>(Option.none());
const layer = makeTestLayer({
window: fakeWindow.window,
createCount,
mainWindow,
});
yield* Effect.gen(function* () {
const desktopWindow = yield* DesktopWindow.DesktopWindow;
yield* desktopWindow.handleBackendReady(new URL("http://127.0.0.1:3773"));
assert.equal(fakeWindow.setBackgroundThrottling.mock.calls.length, 0);
const readyToShow = fakeWindow.windowListeners.get("ready-to-show");
if (!readyToShow) {
return yield* Effect.die("window ready-to-show listener was not registered");
}
readyToShow();
assert.deepEqual(fakeWindow.setBackgroundThrottling.mock.calls, [[true]]);
}).pipe(Effect.provide(layer));
}),
);
it.effect("debounces move and resize bounds updates", () =>
Effect.gen(function* () {
const fakeWindow = makeFakeBrowserWindow();
const createCount = yield* Ref.make(0);
const mainWindow = yield* Ref.make<Option.Option<Electron.BrowserWindow>>(Option.none());
const mainWindowBoundsUpdates: DesktopAppSettings.DesktopWindowBounds[] = [];
const layer = makeTestLayer({
window: fakeWindow.window,
createCount,
mainWindow,
mainWindowBoundsUpdates,
});
yield* Effect.gen(function* () {
const desktopWindow = yield* DesktopWindow.DesktopWindow;
yield* desktopWindow.handleBackendReady(new URL("http://127.0.0.1:3773"));
const move = fakeWindow.windowListeners.get("move");
const resize = fakeWindow.windowListeners.get("resize");
if (!move || !resize) {
return yield* Effect.die("window bounds listeners were not registered");
}
fakeWindow.getBounds.mockReturnValue({ x: 120, y: 80, width: 1280, height: 840 });
move();
yield* TestClock.adjust(250);
fakeWindow.getBounds.mockReturnValue({ x: 160, y: 100, width: 1360, height: 900 });
resize();
yield* TestClock.adjust(499);
assert.deepEqual(mainWindowBoundsUpdates, []);
yield* TestClock.adjust(1);
yield* Effect.promise(() => Promise.resolve());
assert.deepEqual(mainWindowBoundsUpdates, [{ x: 160, y: 100, width: 1360, height: 900 }]);
}).pipe(Effect.provide(layer));
}),
);
it.effect("persists normal bounds and state for a maximized window", () =>
Effect.gen(function* () {
const fakeWindow = makeFakeBrowserWindow();
fakeWindow.isMaximized.mockReturnValue(true);
fakeWindow.getBounds.mockReturnValue({ x: 0, y: 0, width: 1920, height: 1080 });
fakeWindow.getNormalBounds.mockReturnValue({ x: 220, y: 140, width: 1380, height: 920 });
const createCount = yield* Ref.make(0);
const mainWindow = yield* Ref.make<Option.Option<Electron.BrowserWindow>>(Option.none());
const mainWindowBoundsUpdates: DesktopAppSettings.DesktopWindowBounds[] = [];
const mainWindowMaximizedUpdates: boolean[] = [];
const layer = makeTestLayer({
window: fakeWindow.window,
createCount,
mainWindow,
mainWindowBoundsUpdates,
mainWindowMaximizedUpdates,
});
yield* Effect.gen(function* () {
const desktopWindow = yield* DesktopWindow.DesktopWindow;
yield* desktopWindow.handleBackendReady(new URL("http://127.0.0.1:3773"));
const close = fakeWindow.windowListeners.get("close");
if (!close) {
return yield* Effect.die("window close listener was not registered");
}
close();
yield* Effect.promise(() => Promise.resolve());
assert.deepEqual(mainWindowBoundsUpdates, [{ x: 220, y: 140, width: 1380, height: 920 }]);
assert.deepEqual(mainWindowMaximizedUpdates, [true]);
assert.equal(fakeWindow.getNormalBounds.mock.calls.length, 1);
assert.equal(fakeWindow.getBounds.mock.calls.length, 0);
}).pipe(Effect.provide(layer));
}),
);
it.effect("persists normal bounds and state from the native maximize event", () =>
Effect.gen(function* () {
const fakeWindow = makeFakeBrowserWindow();
const createCount = yield* Ref.make(0);
const mainWindow = yield* Ref.make<Option.Option<Electron.BrowserWindow>>(Option.none());
const mainWindowBoundsUpdates: DesktopAppSettings.DesktopWindowBounds[] = [];
const mainWindowMaximizedUpdates: boolean[] = [];
const layer = makeTestLayer({
window: fakeWindow.window,
createCount,
mainWindow,
mainWindowBoundsUpdates,
mainWindowMaximizedUpdates,
});
yield* Effect.gen(function* () {
const desktopWindow = yield* DesktopWindow.DesktopWindow;
yield* desktopWindow.handleBackendReady(new URL("http://127.0.0.1:3773"));
const maximize = fakeWindow.windowListeners.get("maximize");
if (!maximize) {
return yield* Effect.die("window maximize listener was not registered");
}
fakeWindow.isMaximized.mockReturnValue(true);
fakeWindow.getBounds.mockReturnValue({ x: 0, y: 0, width: 1920, height: 1080 });
fakeWindow.getNormalBounds.mockReturnValue({ x: 220, y: 140, width: 1380, height: 920 });
maximize();
yield* TestClock.adjust(500);
yield* Effect.promise(() => Promise.resolve());
assert.deepEqual(mainWindowBoundsUpdates, [{ x: 220, y: 140, width: 1380, height: 920 }]);
assert.deepEqual(mainWindowMaximizedUpdates, [true]);
}).pipe(Effect.provide(layer));
}),
);
it.effect("does not persist bounds that fail the domain schema", () =>
Effect.gen(function* () {
const fakeWindow = makeFakeBrowserWindow();
fakeWindow.getBounds.mockReturnValue({ x: 100.4, y: 80.2, width: 839.4, height: 619.4 });
const createCount = yield* Ref.make(0);
const mainWindow = yield* Ref.make<Option.Option<Electron.BrowserWindow>>(Option.none());
const mainWindowBoundsUpdates: DesktopAppSettings.DesktopWindowBounds[] = [];
const layer = makeTestLayer({
window: fakeWindow.window,
createCount,
mainWindow,
mainWindowBoundsUpdates,
});
yield* Effect.gen(function* () {
const desktopWindow = yield* DesktopWindow.DesktopWindow;
yield* desktopWindow.handleBackendReady(new URL("http://127.0.0.1:3773"));
const resize = fakeWindow.windowListeners.get("resize");
if (!resize) {
return yield* Effect.die("window resize listener was not registered");
}
resize();
yield* TestClock.adjust(500);
yield* Effect.promise(() => Promise.resolve());
assert.deepEqual(mainWindowBoundsUpdates, []);
}).pipe(Effect.provide(layer));
}),
);
it.effect("preserves unrestorable bounds until the user changes the window", () =>
Effect.gen(function* () {
const fakeWindow = makeFakeBrowserWindow();
const createCount = yield* Ref.make(0);
const mainWindow = yield* Ref.make<Option.Option<Electron.BrowserWindow>>(Option.none());
const mainWindowBoundsUpdates: DesktopAppSettings.DesktopWindowBounds[] = [];
const layer = makeTestLayer({
window: fakeWindow.window,
createCount,
mainWindow,
mainWindowBoundsUpdates,
desktopSettings: {
...DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS,
mainWindowBounds: { x: 2040, y: 80, width: 1320, height: 880 },
},
});
yield* Effect.gen(function* () {
const desktopWindow = yield* DesktopWindow.DesktopWindow;
yield* desktopWindow.handleBackendReady(new URL("http://127.0.0.1:3773"));
const close = fakeWindow.windowListeners.get("close");
const move = fakeWindow.windowListeners.get("move");
if (!close || !move) {
return yield* Effect.die("window lifecycle listeners were not registered");
}
close();
yield* Effect.promise(() => Promise.resolve());
assert.deepEqual(mainWindowBoundsUpdates, []);
fakeWindow.getBounds.mockReturnValue({ x: 80, y: 60, width: 1280, height: 840 });
move();
yield* TestClock.adjust(500);
yield* Effect.promise(() => Promise.resolve());
assert.deepEqual(mainWindowBoundsUpdates, [{ x: 80, y: 60, width: 1280, height: 840 }]);
}).pipe(Effect.provide(layer));
}),
);
it.effect("flushes normal bounds when fullscreen before the debounce completes", () =>
Effect.gen(function* () {
const fakeWindow = makeFakeBrowserWindow();
fakeWindow.getBounds.mockReturnValue({ x: 0, y: 0, width: 1920, height: 1080 });
fakeWindow.getNormalBounds.mockReturnValue({ x: 200, y: 130, width: 1400, height: 940 });
const createCount = yield* Ref.make(0);
const mainWindow = yield* Ref.make<Option.Option<Electron.BrowserWindow>>(Option.none());
const mainWindowBoundsUpdates: DesktopAppSettings.DesktopWindowBounds[] = [];
const layer = makeTestLayer({
window: fakeWindow.window,
createCount,
mainWindow,
mainWindowBoundsUpdates,
});
yield* Effect.gen(function* () {
const desktopWindow = yield* DesktopWindow.DesktopWindow;
yield* desktopWindow.handleBackendReady(new URL("http://127.0.0.1:3773"));
const resize = fakeWindow.windowListeners.get("resize");
if (!resize) {
return yield* Effect.die("window resize listener was not registered");
}
resize();
yield* TestClock.adjust(250);
fakeWindow.isFullScreen.mockReturnValue(true);
yield* desktopWindow.flushMainWindowBounds;
assert.deepEqual(mainWindowBoundsUpdates, [{ x: 200, y: 130, width: 1400, height: 940 }]);
assert.equal(fakeWindow.getBounds.mock.calls.length, 0);
assert.equal(fakeWindow.getNormalBounds.mock.calls.length, 1);
}).pipe(Effect.provide(layer));
}),
);
it.effect("flushes normal bounds when minimized before the debounce completes", () =>
Effect.gen(function* () {
const fakeWindow = makeFakeBrowserWindow();
fakeWindow.getBounds.mockReturnValue({ x: -32_000, y: -32_000, width: 160, height: 28 });
fakeWindow.getNormalBounds.mockReturnValue({ x: 180, y: 120, width: 1440, height: 960 });
const createCount = yield* Ref.make(0);
const mainWindow = yield* Ref.make<Option.Option<Electron.BrowserWindow>>(Option.none());
const mainWindowBoundsUpdates: DesktopAppSettings.DesktopWindowBounds[] = [];
const layer = makeTestLayer({
window: fakeWindow.window,
createCount,
mainWindow,
mainWindowBoundsUpdates,
});
yield* Effect.gen(function* () {
const desktopWindow = yield* DesktopWindow.DesktopWindow;
yield* desktopWindow.handleBackendReady(new URL("http://127.0.0.1:3773"));
const resize = fakeWindow.windowListeners.get("resize");
if (!resize) {
return yield* Effect.die("window resize listener was not registered");
}
resize();
yield* TestClock.adjust(250);
fakeWindow.isMinimized.mockReturnValue(true);
yield* desktopWindow.flushMainWindowBounds;
assert.deepEqual(mainWindowBoundsUpdates, [{ x: 180, y: 120, width: 1440, height: 960 }]);
assert.equal(fakeWindow.getBounds.mock.calls.length, 0);
assert.equal(fakeWindow.getNormalBounds.mock.calls.length, 1);
}).pipe(Effect.provide(layer));
}),
);
it.effect("logs display lookup failures before falling back to the default size", () =>
Effect.gen(function* () {
const displayLookupFailure = new Error("screen API unavailable");
failDisplayLookupOnce(displayLookupFailure);
const logRecords: Array<{
readonly message: unknown;
readonly annotations: Readonly<DesktopLogAnnotations>;
}> = [];
const logger = Logger.make(({ fiber, message }) => {
logRecords.push({
message,
annotations: { ...fiber.getRef(References.CurrentLogAnnotations) },
});
});
const fakeWindow = makeFakeBrowserWindow();
const createCount = yield* Ref.make(0);
const mainWindow = yield* Ref.make<Option.Option<Electron.BrowserWindow>>(Option.none());
const createdWindowOptions: Electron.BrowserWindowConstructorOptions[] = [];
const layer = makeTestLayer({
window: fakeWindow.window,
createCount,
mainWindow,
createdWindowOptions,
});
yield* Effect.gen(function* () {
const desktopWindow = yield* DesktopWindow.DesktopWindow;
yield* desktopWindow.handleBackendReady(new URL("http://127.0.0.1:3773"));
}).pipe(
Effect.provide(Layer.mergeAll(layer, Logger.layer([logger], { mergeWithExisting: false }))),
);
const warning = logRecords.find(
(record) =>
Array.isArray(record.message) &&
record.message[0] === "failed to read connected displays; using defaults",
);
assert.isDefined(warning);
assert.strictEqual(warning.annotations.cause, displayLookupFailure);
assert.equal(createdWindowOptions[0]?.width, 1100);
assert.equal(createdWindowOptions[0]?.height, 780);
assert.isUndefined(createdWindowOptions[0]?.x);
assert.isUndefined(createdWindowOptions[0]?.y);
}),
);
it.effect("persists the current main window bounds before the window closes", () =>
Effect.gen(function* () {
const fakeWindow = makeFakeBrowserWindow();
fakeWindow.getBounds.mockReturnValue({ x: 240, y: 160, width: 1410, height: 930 });
const createCount = yield* Ref.make(0);
const mainWindow = yield* Ref.make<Option.Option<Electron.BrowserWindow>>(Option.none());
const mainWindowBoundsUpdates: DesktopAppSettings.DesktopWindowBounds[] = [];
const writeStarted = yield* Deferred.make<void>();
const allowWrite = yield* Deferred.make<void>();
const flushCompleted = yield* Deferred.make<void>();
const layer = makeTestLayer({
window: fakeWindow.window,
createCount,
mainWindow,
mainWindowBoundsUpdates,
beforeMainWindowBoundsUpdate: () =>
Deferred.succeed(writeStarted, undefined).pipe(
Effect.andThen(Deferred.await(allowWrite)),
Effect.asVoid,
),
});
yield* Effect.gen(function* () {
const desktopWindow = yield* DesktopWindow.DesktopWindow;
yield* desktopWindow.handleBackendReady(new URL("http://127.0.0.1:3773"));
const close = fakeWindow.windowListeners.get("close");
if (!close) {
return yield* Effect.die("window close listener was not registered");
}
close();
yield* Deferred.await(writeStarted);
fakeWindow.isDestroyed.mockReturnValue(true);
const flushFiber = yield* desktopWindow.flushMainWindowBounds.pipe(
Effect.andThen(Deferred.succeed(flushCompleted, undefined)),
Effect.forkChild({ startImmediately: true }),
);
yield* Effect.yieldNow;
assert.isFalse(yield* Deferred.isDone(flushCompleted));
yield* Deferred.succeed(allowWrite, undefined);
yield* Fiber.join(flushFiber);
assert.isTrue(yield* Deferred.isDone(flushCompleted));
assert.deepEqual(mainWindowBoundsUpdates, [
{
x: 240,
y: 160,
width: 1410,
height: 930,
},
]);
}).pipe(Effect.provide(layer));
}),
);
});
vi.mock("electron", async (importOriginal) => ({
...(await importOriginal<typeof import("electron")>()),
session: {
fromPartition: vi.fn(() => ({
getUserAgent: vi.fn(() => "Mozilla/5.0 Electron/41.5.0 t3code/1.2.3"),
setPermissionRequestHandler: vi.fn(),
setUserAgent: vi.fn(),
})),
},
screen: {
getAllDisplays: vi.fn(() => [
{
bounds: { x: 0, y: 0, width: 1920, height: 1080 },
},
]),
},
}));