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 * as Schema from "effect/Schema";
import { it as effectIt } from "@effect/vitest";
import * as Cause from "effect/Cause";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as Fiber from "effect/Fiber";
import * as Option from "effect/Option";
import { beforeEach, describe, expect, vi } from "vite-plus/test";
import { createPreviewManagerHarness } from "./test-support/PreviewManagerHarness.ts";
const {
browserWindowConstructor,
createFromBuffer,
createFromPath,
fromId,
getFocusedWebContents,
mkdir,
showItemInFolder,
webviewSend,
writeFile,
writeImage,
} = vi.hoisted(() => ({
browserWindowConstructor: vi.fn(),
createFromBuffer: vi.fn(),
createFromPath: vi.fn((): Pick<Electron.NativeImage, "isEmpty"> => ({ isEmpty: () => false })),
fromId: vi.fn((_id?: number): Electron.WebContents | null => null),
getFocusedWebContents: vi.fn((): Electron.WebContents | null => null),
mkdir: vi.fn((_path: string) => undefined),
showItemInFolder: vi.fn(),
webviewSend: vi.fn(),
writeFile: vi.fn((_path: string, _data: Uint8Array) => undefined),
writeImage: vi.fn(),
}));
vi.mock("electron", () => ({
BrowserWindow: browserWindowConstructor,
clipboard: {
writeImage,
},
nativeImage: {
createFromBuffer,
createFromPath,
},
shell: {
showItemInFolder,
},
session: {
fromPartition: vi.fn(),
},
webContents: {
fromId,
getFocusedWebContents,
},
}));
const { withManager, makeTestPreviewWebContents } = createPreviewManagerHarness({
browserWindowConstructor,
createFromBuffer,
createFromPath,
fromId,
getFocusedWebContents,
mkdir,
showItemInFolder,
webviewSend,
writeFile,
writeImage,
});
describe("PreviewManager", () => {
beforeEach(() => {
browserWindowConstructor.mockReset();
fromId.mockClear();
getFocusedWebContents.mockReset();
getFocusedWebContents.mockReturnValue(null);
mkdir.mockClear();
writeFile.mockClear();
showItemInFolder.mockClear();
writeImage.mockClear();
createFromPath.mockClear();
createFromBuffer.mockReset();
webviewSend.mockClear();
});
effectIt.effect("captures a PNG screenshot into browser artifacts", () =>
withManager((manager) =>
Effect.gen(function* () {
const png = Buffer.from("preview-png");
const capturePage = vi.fn(async () => ({ toPNG: () => png }));
const listeners = new Map<string, (...args: never[]) => void>();
fromId.mockReturnValue({
id: 42,
isDestroyed: () => false,
getType: () => "webview",
getURL: () => "https://example.com:8443/path?query=value",
getTitle: () => "Example",
isLoading: () => false,
getZoomFactor: () => 1,
setZoomFactor: vi.fn(),
setAudioMuted: vi.fn(),
isCurrentlyAudible: () => false,
on: vi.fn((event: string, listener: (...args: never[]) => void) => {
listeners.set(event, listener);
}),
off: vi.fn(),
ipc: { on: vi.fn(), off: vi.fn() },
send: webviewSend,
navigationHistory: { canGoBack: () => false, canGoForward: () => false },
setIgnoreMenuShortcuts: vi.fn(),
setWindowOpenHandler: vi.fn(),
debugger: {
isAttached: () => false,
attach: vi.fn(),
sendCommand: vi.fn(async () => undefined),
on: vi.fn(),
off: vi.fn(),
},
capturePage,
} as never);
yield* manager.createTab("tab_1");
yield* manager.registerWebview("tab_1", 42);
expect(webviewSend).toHaveBeenCalledWith(
"preview:annotation-theme",
expect.objectContaining({
colorScheme: "light",
primary: "oklch(0.488 0.217 264)",
}),
);
const artifact = yield* manager.captureScreenshot("tab_1");
expect(capturePage).toHaveBeenCalledOnce();
expect(mkdir).toHaveBeenCalledWith("/tmp/t3/dev/browser-artifacts");
expect(writeFile).toHaveBeenCalledWith(artifact.path, png);
expect(artifact).toMatchObject({
tabId: "tab_1",
mimeType: "image/png",
sizeBytes: png.byteLength,
});
expect(artifact.path).toMatch(
/\/browser-artifacts\/browser-screenshot-example-com-[^.]+\.png$/,
);
const captureCause = new Error("capture failed");
capturePage.mockRejectedValueOnce(captureCause);
const exit = yield* Effect.exit(manager.captureScreenshot("tab_1"));
expect(Exit.isFailure(exit)).toBe(true);
if (Exit.isSuccess(exit)) return;
const error = Option.getOrThrow(Cause.findErrorOption(exit.cause));
expect(error).toMatchObject({
_tag: "PreviewOperationError",
operation: "captureScreenshot.capturePage",
tabId: "tab_1",
webContentsId: 42,
cause: captureCause,
});
}),
),
);
effectIt.effect("keeps window unthrottled until the final frame capture stops", () =>
withManager((manager) =>
Effect.gen(function* () {
const setBackgroundThrottling = vi.fn();
const capturePage = vi.fn(async () => ({
toJPEG: () => Buffer.from("recording-frame"),
getSize: () => ({ width: 1280, height: 720 }),
}));
const webContentsById = new Map([
[41, makeTestPreviewWebContents(capturePage, 41)],
[42, makeTestPreviewWebContents(capturePage, 42)],
]);
fromId.mockImplementation((id) =>
id === undefined ? null : (webContentsById.get(id) ?? null),
);
yield* manager.createTab("tab_capture_throttling_1");
yield* manager.createTab("tab_capture_throttling_2");
yield* manager.registerWebview("tab_capture_throttling_1", 41);
yield* manager.registerWebview("tab_capture_throttling_2", 42);
yield* manager.setMainWindow({
isDestroyed: () => false,
once: vi.fn(),
webContents: { setBackgroundThrottling },
} as never);
yield* manager.startRecording("tab_capture_throttling_1");
yield* manager.startRecording("tab_capture_throttling_2");
expect(setBackgroundThrottling.mock.calls).toEqual([[false]]);
yield* manager.stopRecording("tab_capture_throttling_1");
expect(setBackgroundThrottling.mock.calls).toEqual([[false]]);
yield* manager.stopRecording("tab_capture_throttling_2");
expect(setBackgroundThrottling.mock.calls).toEqual([[false], [true]]);
}),
),
);
effectIt.effect("does not commit failed starts and retries throttle restoration", () =>
withManager((manager) =>
Effect.gen(function* () {
const setBackgroundThrottling = vi.fn<(enabled: boolean) => void>();
const capturePage = vi.fn(async () => ({
toJPEG: () => Buffer.from("recording-frame"),
getSize: () => ({ width: 1280, height: 720 }),
}));
fromId.mockReturnValue(makeTestPreviewWebContents(capturePage));
yield* manager.createTab("tab_capture_throttling_failure");
yield* manager.registerWebview("tab_capture_throttling_failure", 42);
yield* manager.setMainWindow({
isDestroyed: () => false,
once: vi.fn(),
webContents: { setBackgroundThrottling },
} as never);
setBackgroundThrottling.mockImplementationOnce(() => {
throw new Error("start throttling update failed");
});
const failedStart = yield* Effect.exit(
manager.startRecording("tab_capture_throttling_failure"),
);
expect(Exit.isFailure(failedStart)).toBe(true);
yield* manager.startRecording("tab_capture_throttling_failure");
expect(setBackgroundThrottling.mock.calls).toEqual([[false], [false]]);
setBackgroundThrottling.mockImplementationOnce(() => {
throw new Error("stop throttling update failed");
});
yield* manager.stopRecording("tab_capture_throttling_failure");
expect(setBackgroundThrottling.mock.calls).toEqual([[false], [false], [true], [true]]);
yield* manager.startRecording("tab_capture_throttling_failure");
yield* manager.stopRecording("tab_capture_throttling_failure");
expect(setBackgroundThrottling.mock.calls).toEqual([
[false],
[false],
[true],
[true],
[false],
[true],
]);
}),
),
);
effectIt.effect("does not publish a replacement window when capture reconciliation fails", () =>
withManager((manager) =>
Effect.gen(function* () {
const setBackgroundThrottling = vi.fn(() => {
throw new Error("replacement throttling update failed");
});
const capturePage = vi.fn(async () => ({
toJPEG: () => Buffer.from("recording-frame"),
getSize: () => ({ width: 1280, height: 720 }),
}));
fromId.mockReturnValue(makeTestPreviewWebContents(capturePage));
yield* manager.createTab("tab_capture_replacement_failure");
yield* manager.registerWebview("tab_capture_replacement_failure", 42);
yield* manager.startRecording("tab_capture_replacement_failure");
const failedReplacement = yield* Effect.exit(
manager.setMainWindow({
isDestroyed: () => false,
once: vi.fn(),
webContents: { setBackgroundThrottling },
} as never),
);
expect(Exit.isFailure(failedReplacement)).toBe(true);
yield* manager.stopRecording("tab_capture_replacement_failure");
expect(setBackgroundThrottling.mock.calls).toEqual([[false]]);
}),
),
);
effectIt.effect("ignores close events from replaced main windows", () =>
withManager((manager) =>
Effect.gen(function* () {
let closeFirstWindow: (() => void) | undefined;
const firstWindowThrottling = vi.fn();
const replacementWindowThrottling = vi.fn();
const capturePage = vi.fn(async () => ({
toJPEG: () => Buffer.from("recording-frame"),
getSize: () => ({ width: 1280, height: 720 }),
}));
fromId.mockReturnValue(makeTestPreviewWebContents(capturePage));
yield* manager.createTab("tab_replaced_window_close");
yield* manager.registerWebview("tab_replaced_window_close", 42);
yield* manager.setMainWindow({
isDestroyed: () => false,
once: vi.fn((event: string, listener: () => void) => {
if (event === "closed") closeFirstWindow = listener;
}),
webContents: { setBackgroundThrottling: firstWindowThrottling },
} as never);
yield* manager.setMainWindow({
isDestroyed: () => false,
once: vi.fn(),
webContents: { setBackgroundThrottling: replacementWindowThrottling },
} as never);
closeFirstWindow?.();
yield* manager.startRecording("tab_replaced_window_close");
expect(firstWindowThrottling).not.toHaveBeenCalled();
expect(replacementWindowThrottling.mock.calls).toEqual([[false]]);
yield* manager.stopRecording("tab_replaced_window_close");
expect(replacementWindowThrottling.mock.calls).toEqual([[false], [true]]);
}),
),
);
effectIt.effect("releases frame capture when the main window closes", () =>
withManager((manager) =>
Effect.gen(function* () {
let closeMainWindow: (() => void) | undefined;
const firstWindowThrottling = vi.fn();
const replacementWindowThrottling = vi.fn();
const capturePage = vi.fn(async () => ({
toJPEG: () => Buffer.from("recording-frame"),
getSize: () => ({ width: 1280, height: 720 }),
}));
const webContentsById = new Map([
[42, makeTestPreviewWebContents(capturePage, 42)],
[43, makeTestPreviewWebContents(capturePage, 43)],
]);
fromId.mockImplementation((id) =>
id === undefined ? null : (webContentsById.get(id) ?? null),
);
yield* manager.createTab("tab_window_close_recording");
yield* manager.createTab("tab_window_close_race");
yield* manager.registerWebview("tab_window_close_recording", 42);
yield* manager.registerWebview("tab_window_close_race", 43);
yield* manager.setMainWindow({
isDestroyed: () => false,
once: vi.fn((event: string, listener: () => void) => {
if (event === "closed") closeMainWindow = listener;
}),
webContents: { setBackgroundThrottling: firstWindowThrottling },
} as never);
yield* manager.startRecording("tab_window_close_recording");
expect(firstWindowThrottling.mock.calls).toEqual([[false]]);
closeMainWindow?.();
const racedStart = yield* Effect.exit(manager.startRecording("tab_window_close_race"));
expect(Exit.isFailure(racedStart)).toBe(true);
if (Exit.isFailure(racedStart)) {
expect(Option.getOrThrow(Cause.findErrorOption(racedStart.cause))).toMatchObject({
_tag: "PreviewMainWindowClosedError",
tabId: "tab_window_close_race",
});
}
yield* Effect.yieldNow;
yield* Effect.yieldNow;
yield* manager.setMainWindow({
isDestroyed: () => false,
once: vi.fn(),
webContents: { setBackgroundThrottling: replacementWindowThrottling },
} as never);
expect(replacementWindowThrottling).not.toHaveBeenCalled();
}),
),
);
effectIt.effect("keeps element picking active during subframe navigation", () =>
withManager((manager) =>
Effect.gen(function* () {
const listeners = new Map<string, (...args: unknown[]) => void>();
fromId.mockReturnValue({
id: 42,
isDestroyed: () => false,
getType: () => "webview",
getURL: () => "https://example.com",
getTitle: () => "Example",
isLoading: () => false,
isFocused: () => true,
getZoomFactor: () => 1,
setZoomFactor: vi.fn(),
setAudioMuted: vi.fn(),
isCurrentlyAudible: () => false,
on: vi.fn((event: string, listener: (...args: unknown[]) => void) => {
listeners.set(event, listener);
}),
once: vi.fn((event: string, listener: (...args: unknown[]) => void) => {
listeners.set(event, listener);
}),
off: vi.fn(),
ipc: { on: vi.fn(), off: vi.fn(), removeListener: vi.fn() },
send: webviewSend,
navigationHistory: { canGoBack: () => false, canGoForward: () => false },
setIgnoreMenuShortcuts: vi.fn(),
setWindowOpenHandler: vi.fn(),
debugger: {
isAttached: () => false,
attach: vi.fn(),
sendCommand: vi.fn(async () => undefined),
on: vi.fn(),
off: vi.fn(),
},
} as never);
yield* manager.createTab("tab_1");
yield* manager.registerWebview("tab_1", 42);
const pick = yield* manager.pickElement("tab_1").pipe(Effect.forkChild);
yield* Effect.yieldNow;
listeners.get("did-start-navigation")?.({}, "about:blank", false, false);
yield* Effect.yieldNow;
expect(pick.pollUnsafe()).toBeUndefined();
listeners.get("did-start-navigation")?.({}, "https://example.com/next", false, true);
expect(yield* Fiber.join(pick)).toBeNull();
}),
),
);
effectIt.effect("navigates the guest history when the thumb-button ipc fires", () =>
withManager((manager) =>
Effect.gen(function* () {
let mouseNavigate:
| ((
event: Record<string, never>,
payload: Record<string, Schema.Json | undefined>,
) => void)
| undefined;
const goBack = vi.fn();
const goForward = vi.fn();
let canGoBack = true;
fromId.mockReturnValue({
id: 42,
isDestroyed: () => false,
getType: () => "webview",
getURL: () => "https://example.com",
getTitle: () => "Example",
isLoading: () => false,
getZoomFactor: () => 1,
setZoomFactor: vi.fn(),
setAudioMuted: vi.fn(),
isCurrentlyAudible: () => false,
on: vi.fn(),
off: vi.fn(),
ipc: {
on: vi.fn((channel: string, listener: typeof mouseNavigate) => {
if (channel === "preview:mouse-navigate") mouseNavigate = listener;
}),
off: vi.fn(),
},
send: webviewSend,
navigationHistory: {
canGoBack: () => canGoBack,
canGoForward: () => true,
goBack,
goForward,
},
setIgnoreMenuShortcuts: vi.fn(),
setWindowOpenHandler: vi.fn(),
debugger: {
isAttached: () => false,
attach: vi.fn(),
sendCommand: vi.fn(async () => undefined),
on: vi.fn(),
off: vi.fn(),
},
} as never);
yield* manager.createTab("tab_nav");
yield* manager.registerWebview("tab_nav", 42);
expect(mouseNavigate).toBeDefined();
mouseNavigate?.({}, { direction: "back" });
yield* Effect.yieldNow;
expect(goBack).toHaveBeenCalledOnce();
mouseNavigate?.({}, { direction: "forward" });
yield* Effect.yieldNow;
expect(goForward).toHaveBeenCalledOnce();
// Ignores unknown payloads and never navigates when history is exhausted.
mouseNavigate?.({}, { direction: "sideways" });
canGoBack = false;
mouseNavigate?.({}, { direction: "back" });
yield* Effect.yieldNow;
expect(goBack).toHaveBeenCalledOnce();
}),
),
);
effectIt.effect("reveals only files inside the configured browser artifact directory", () =>
withManager((manager) =>
Effect.gen(function* () {
yield* manager.revealArtifact("/tmp/t3/dev/browser-artifacts/browser-screenshot-test.png");
expect(showItemInFolder).toHaveBeenCalledWith(
"/tmp/t3/dev/browser-artifacts/browser-screenshot-test.png",
);
const exit = yield* Effect.exit(manager.revealArtifact("/tmp/t3/dev/settings.json"));
expect(Exit.isFailure(exit)).toBe(true);
if (Exit.isSuccess(exit)) return;
const error = Option.getOrThrow(Cause.findErrorOption(exit.cause));
expect(error).toMatchObject({
_tag: "PreviewArtifactPathOutsideDirectoryError",
artifactPath: "/tmp/t3/dev/settings.json",
artifactDirectory: "/tmp/t3/dev/browser-artifacts",
});
expect("cause" in error).toBe(false);
}),
),
);
effectIt.effect("copies screenshot artifacts to the system clipboard", () =>
withManager((manager) =>
Effect.gen(function* () {
const artifactPath = "/tmp/t3/dev/browser-artifacts/browser-screenshot-test.png";
yield* manager.copyArtifactToClipboard(artifactPath);
expect(createFromPath).toHaveBeenCalledWith(artifactPath);
expect(writeImage).toHaveBeenCalledOnce();
const exit = yield* Effect.exit(
manager.copyArtifactToClipboard("/tmp/t3/dev/settings.json"),
);
expect(Exit.isFailure(exit)).toBe(true);
if (Exit.isSuccess(exit)) return;
const error = Option.getOrThrow(Cause.findErrorOption(exit.cause));
expect(error).toMatchObject({
_tag: "PreviewArtifactPathOutsideDirectoryError",
artifactPath: "/tmp/t3/dev/settings.json",
artifactDirectory: "/tmp/t3/dev/browser-artifacts",
});
expect("cause" in error).toBe(false);
createFromPath.mockReturnValueOnce({ isEmpty: () => true });
const invalidImageExit = yield* Effect.exit(manager.copyArtifactToClipboard(artifactPath));
expect(Exit.isFailure(invalidImageExit)).toBe(true);
if (Exit.isSuccess(invalidImageExit)) return;
expect(Option.getOrThrow(Cause.findErrorOption(invalidImageExit.cause))).toMatchObject({
_tag: "PreviewArtifactImageLoadError",
artifactPath,
});
}),
),
);
});