Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.
Python agentagent-osaiai-agentsai-partnerchatopsclaudeclaude-codecodexdevtoolsdiscord-botlark-botllmlocal-firstlong-term-memoryopencodeslack-botvibe-codingvibeywechat
/* @vitest-environment jsdom */
import { act, renderHook } from '@testing-library/react';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { ShowPageVoiceHost } from '../../lib/showPageVoiceBridge';
import { useShowPageAnnotation } from './useShowPageAnnotation';
let teardown: (() => void) | null = null;
afterEach(() => {
teardown?.();
teardown = null;
document.body.replaceChildren();
vi.restoreAllMocks();
});
const mountBridge = (initialSrc: string | null = '/show/session') => {
const iframe = document.createElement('iframe');
document.body.append(iframe);
const hook = renderHook(({ src }) => useShowPageAnnotation(src), {
initialProps: { src: initialSrc },
});
act(() => hook.result.current.setIframe(iframe));
teardown = () => {
act(() => hook.result.current.setIframe(null));
hook.unmount();
iframe.remove();
};
return { iframe, ...hook };
};
const reportState = (iframe: HTMLIFrameElement, enabled: boolean) => {
const source = iframe.contentWindow;
if (!source) throw new Error('Expected the attached iframe to have a contentWindow');
act(() => {
window.dispatchEvent(
new MessageEvent('message', {
origin: window.location.origin,
source,
data: {
type: 'avibe:annotation:state',
enabled,
mode: 'smart',
available: true,
},
}),
);
});
};
const listenForFrameKeydown = (iframe: HTMLIFrameElement) => {
const listener = vi.fn();
const frameDocument = iframe.contentDocument;
if (!frameDocument) throw new Error('Expected the attached iframe to have a contentDocument');
frameDocument.addEventListener('keydown', listener);
return listener;
};
const dispatchParentKeydown = (target: EventTarget, key = 'Escape', preventDefault = false) => {
const event = new KeyboardEvent('keydown', { key, bubbles: true, cancelable: true });
if (preventDefault) event.preventDefault();
act(() => target.dispatchEvent(event));
};
const dispatchAnnotationShortcut = (target: EventTarget) => {
const event = new KeyboardEvent('keydown', {
code: 'KeyX',
altKey: true,
bubbles: true,
cancelable: true,
});
act(() => target.dispatchEvent(event));
return event;
};
const dispatchFrameMessage = (
iframe: HTMLIFrameElement,
data: unknown,
origin = window.location.origin,
source: MessageEventSource | null = iframe.contentWindow,
) => {
act(() => {
window.dispatchEvent(new MessageEvent('message', { data, origin, source }));
});
};
describe('useShowPageAnnotation voice boundary', () => {
it('answers voice requests only from the attached same-origin iframe', async () => {
const fetchMock = vi.spyOn(globalThis, 'fetch').mockResolvedValue(new Response(
JSON.stringify({ available: false }),
{ status: 200, headers: { 'Content-Type': 'application/json' } },
));
const { iframe } = mountBridge();
const postMessage = vi.spyOn(iframe.contentWindow!, 'postMessage');
const query = {
type: 'avibe:annotation:voice:request',
action: 'query',
requestId: 'voice-probe',
};
dispatchFrameMessage(iframe, query, 'https://foreign.test');
dispatchFrameMessage(iframe, query, window.location.origin, null);
await Promise.resolve();
expect(fetchMock).not.toHaveBeenCalled();
dispatchFrameMessage(iframe, query);
await vi.waitFor(() => expect(fetchMock).toHaveBeenCalledWith(
'/api/asr/status',
expect.any(Object),
));
await vi.waitFor(() => expect(postMessage).toHaveBeenCalledWith({
type: 'avibe:annotation:voice:event',
kind: 'availability',
requestId: 'voice-probe',
available: false,
}, window.location.origin));
});
it('deduplicates only in-flight availability and refreshes after it settles', async () => {
const fetchMock = vi.spyOn(globalThis, 'fetch').mockResolvedValue(new Response(
JSON.stringify({ available: true, max_file_bytes: 1_000_000 }),
{ status: 200, headers: { 'Content-Type': 'application/json' } },
));
const { iframe } = mountBridge();
const postMessage = vi.spyOn(iframe.contentWindow!, 'postMessage');
const statusCalls = () => fetchMock.mock.calls.filter(([url]) => url === '/api/asr/status');
dispatchFrameMessage(iframe, {
type: 'avibe:annotation:voice:request',
action: 'query',
requestId: 'voice-probe-1',
});
await vi.waitFor(() => expect(statusCalls()).toHaveLength(1));
await vi.waitFor(() => expect(postMessage).toHaveBeenCalledWith(
expect.objectContaining({ kind: 'availability', requestId: 'voice-probe-1' }),
window.location.origin,
));
dispatchFrameMessage(iframe, {
type: 'avibe:annotation:voice:request',
action: 'query',
requestId: 'voice-probe-2',
});
await vi.waitFor(() => expect(statusCalls()).toHaveLength(2));
});
it('replaces the voice host and clears stale state on every iframe load', () => {
const dispose = vi.spyOn(ShowPageVoiceHost.prototype, 'dispose');
const { iframe, result } = mountBridge();
reportState(iframe, true);
dispose.mockClear();
act(() => result.current.handleIframeLoad());
expect(dispose).toHaveBeenCalledOnce();
expect(result.current.state).toBeNull();
});
});
describe('useShowPageAnnotation shortcut', () => {
it('leaves the shortcut to the parent surface outside the Show Page frame', () => {
const { iframe } = mountBridge();
const postMessage = vi.spyOn(iframe.contentWindow!, 'postMessage');
reportState(iframe, false);
postMessage.mockClear();
const event = dispatchAnnotationShortcut(document.body);
expect(event.defaultPrevented).toBe(false);
expect(postMessage).not.toHaveBeenCalled();
});
it('enters annotation from the owning parent surface', () => {
const { iframe, result } = mountBridge();
const postMessage = vi.spyOn(iframe.contentWindow!, 'postMessage');
reportState(iframe, false);
postMessage.mockClear();
const nativeEvent = new KeyboardEvent('keydown', {
code: 'KeyX',
altKey: true,
bubbles: true,
cancelable: true,
});
act(() => result.current.handleShortcutKeyDown({
defaultPrevented: false,
repeat: false,
target: document.body,
nativeEvent,
preventDefault: () => nativeEvent.preventDefault(),
} as React.KeyboardEvent<HTMLElement>));
expect(nativeEvent.defaultPrevented).toBe(true);
expect(postMessage).toHaveBeenLastCalledWith(
{ type: 'avibe:annotation:control', action: 'enable' },
window.location.origin,
);
});
it('binds the same shortcut inside the focused Show Page iframe', () => {
const { iframe } = mountBridge();
const postMessage = vi.spyOn(iframe.contentWindow!, 'postMessage');
reportState(iframe, false);
postMessage.mockClear();
dispatchAnnotationShortcut(iframe.contentWindow!);
expect(postMessage).toHaveBeenLastCalledWith(
{ type: 'avibe:annotation:control', action: 'enable' },
window.location.origin,
);
});
it('leaves the iframe shortcut with its active dialog or menu overlay', () => {
const { iframe } = mountBridge();
const postMessage = vi.spyOn(iframe.contentWindow!, 'postMessage');
const frameDocument = iframe.contentDocument!;
reportState(iframe, false);
postMessage.mockClear();
const dialog = frameDocument.createElement('div');
dialog.setAttribute('role', 'dialog');
const dialogButton = frameDocument.createElement('button');
dialog.append(dialogButton);
frameDocument.body.append(dialog);
dialogButton.focus();
expect(dispatchAnnotationShortcut(iframe.contentWindow!).defaultPrevented).toBe(false);
dialog.remove();
const menu = frameDocument.createElement('div');
menu.setAttribute('role', 'menu');
menu.dataset.state = 'open';
const menuItem = frameDocument.createElement('button');
menu.append(menuItem);
frameDocument.body.append(menu);
menuItem.focus();
expect(dispatchAnnotationShortcut(iframe.contentWindow!).defaultPrevented).toBe(false);
expect(postMessage).not.toHaveBeenCalled();
});
it('does not mistake persistent iframe navigation for an open overlay', () => {
const { iframe } = mountBridge();
const postMessage = vi.spyOn(iframe.contentWindow!, 'postMessage');
const frameDocument = iframe.contentDocument!;
reportState(iframe, false);
postMessage.mockClear();
const navigation = frameDocument.createElement('div');
navigation.setAttribute('role', 'menu');
const pageButton = frameDocument.createElement('button');
frameDocument.body.append(navigation, pageButton);
pageButton.focus();
expect(dispatchAnnotationShortcut(iframe.contentWindow!).defaultPrevented).toBe(true);
expect(postMessage).toHaveBeenLastCalledWith(
{ type: 'avibe:annotation:control', action: 'enable' },
window.location.origin,
);
});
});
describe('useShowPageAnnotation host Escape forwarding', () => {
it('forwards parent Escape to the iframe document while annotation is enabled', () => {
const { iframe } = mountBridge();
const frameKeydown = listenForFrameKeydown(iframe);
reportState(iframe, true);
dispatchParentKeydown(document.body, 'Enter');
expect(frameKeydown).not.toHaveBeenCalled();
dispatchParentKeydown(document.body);
expect(frameKeydown).toHaveBeenCalledTimes(1);
expect(frameKeydown.mock.calls[0]?.[0]).toMatchObject({ key: 'Escape', bubbles: true, cancelable: true });
});
it('leaves Escape with an editable target or editable ancestor', () => {
const { iframe } = mountBridge();
const frameKeydown = listenForFrameKeydown(iframe);
reportState(iframe, true);
const textarea = document.createElement('textarea');
document.body.append(textarea);
dispatchParentKeydown(textarea);
const input = document.createElement('input');
document.body.append(input);
dispatchParentKeydown(input);
const editor = document.createElement('div');
editor.setAttribute('contenteditable', 'true');
const child = document.createElement('span');
editor.append(child);
document.body.append(editor);
dispatchParentKeydown(child);
expect(frameKeydown).not.toHaveBeenCalled();
});
it('leaves Escape with targets inside an open overlay or dialog', () => {
const { iframe } = mountBridge();
const frameKeydown = listenForFrameKeydown(iframe);
reportState(iframe, true);
const openPopover = document.createElement('div');
openPopover.dataset.state = 'open';
const popoverTarget = document.createElement('button');
openPopover.append(popoverTarget);
document.body.append(openPopover);
dispatchParentKeydown(popoverTarget);
const roleDialog = document.createElement('div');
roleDialog.setAttribute('role', 'dialog');
const roleDialogTarget = document.createElement('button');
roleDialog.append(roleDialogTarget);
document.body.append(roleDialog);
dispatchParentKeydown(roleDialogTarget);
const nativeDialog = document.createElement('dialog');
nativeDialog.open = true;
const nativeDialogTarget = document.createElement('button');
nativeDialog.append(nativeDialogTarget);
document.body.append(nativeDialog);
dispatchParentKeydown(nativeDialogTarget);
expect(frameKeydown).not.toHaveBeenCalled();
});
it('leaves Escape with a target inside a custom menu', () => {
const { iframe } = mountBridge();
const frameKeydown = listenForFrameKeydown(iframe);
reportState(iframe, true);
const menu = document.createElement('div');
menu.setAttribute('role', 'menu');
const menuItem = document.createElement('button');
menu.append(menuItem);
document.body.append(menu);
dispatchParentKeydown(menuItem);
expect(frameKeydown).not.toHaveBeenCalled();
});
it('leaves Escape with an outside target while a custom menu is open', () => {
const { iframe } = mountBridge();
const frameKeydown = listenForFrameKeydown(iframe);
reportState(iframe, true);
const menu = document.createElement('div');
menu.setAttribute('role', 'menu');
document.body.append(menu);
const invokingButton = document.createElement('button');
document.body.append(invokingButton);
dispatchParentKeydown(invokingButton);
expect(frameKeydown).not.toHaveBeenCalled();
});
it('leaves Escape with an expanded popup trigger', () => {
const { iframe } = mountBridge();
const frameKeydown = listenForFrameKeydown(iframe);
reportState(iframe, true);
const trigger = document.createElement('button');
trigger.setAttribute('aria-expanded', 'true');
trigger.setAttribute('aria-haspopup', 'menu');
document.body.append(trigger);
dispatchParentKeydown(trigger);
expect(frameKeydown).not.toHaveBeenCalled();
});
it('forwards Escape from an expanded non-popup control', () => {
const { iframe } = mountBridge();
const frameKeydown = listenForFrameKeydown(iframe);
reportState(iframe, true);
const accordionTrigger = document.createElement('button');
accordionTrigger.setAttribute('aria-expanded', 'true');
document.body.append(accordionTrigger);
dispatchParentKeydown(accordionTrigger);
expect(frameKeydown).toHaveBeenCalledTimes(1);
});
it('forwards Escape from the non-modal pinned Show Page window container', () => {
const { iframe } = mountBridge();
const frameKeydown = listenForFrameKeydown(iframe);
reportState(iframe, true);
const appWindow = document.createElement('div');
appWindow.setAttribute('role', 'dialog');
appWindow.dataset.windowId = 'show-window';
const titleBarButton = document.createElement('button');
appWindow.append(titleBarButton);
document.body.append(appWindow);
dispatchParentKeydown(titleBarButton);
expect(frameKeydown).toHaveBeenCalledTimes(1);
});
it('does not forward an already-prevented parent Escape', () => {
const { iframe } = mountBridge();
const frameKeydown = listenForFrameKeydown(iframe);
reportState(iframe, true);
dispatchParentKeydown(document.body, 'Escape', true);
expect(frameKeydown).not.toHaveBeenCalled();
});
it('disarms forwarding when annotation becomes disabled', () => {
const { iframe } = mountBridge();
const frameKeydown = listenForFrameKeydown(iframe);
reportState(iframe, true);
dispatchParentKeydown(document.body);
expect(frameKeydown).toHaveBeenCalledTimes(1);
reportState(iframe, false);
dispatchParentKeydown(document.body);
expect(frameKeydown).toHaveBeenCalledTimes(1);
});
it('does not arm forwarding before the overlay reports state', () => {
const { iframe } = mountBridge();
const frameKeydown = listenForFrameKeydown(iframe);
dispatchParentKeydown(document.body);
expect(frameKeydown).not.toHaveBeenCalled();
});
it('disarms forwarding when the source resets state to unknown', () => {
const { iframe, result, rerender } = mountBridge();
const frameKeydown = listenForFrameKeydown(iframe);
reportState(iframe, true);
dispatchParentKeydown(document.body);
expect(frameKeydown).toHaveBeenCalledTimes(1);
rerender({ src: null });
expect(result.current.state).toBeNull();
dispatchParentKeydown(document.body);
expect(frameKeydown).toHaveBeenCalledTimes(1);
});
it('disarms forwarding when the iframe unmounts', () => {
const { iframe, result } = mountBridge();
const frameKeydown = listenForFrameKeydown(iframe);
reportState(iframe, true);
act(() => result.current.setIframe(null));
dispatchParentKeydown(document.body);
expect(frameKeydown).not.toHaveBeenCalled();
});
});