Files
openchamber/packages/ui/src/lib/terminalPreview.test.ts
T
Bohdan Triapitsyn a5aa32446d feat(browser): replace the preview proxy with a real browser panel and an agent web tool (#2883)
The preview panel worked by proxying a dev server through OpenChamber's own
origin and rewriting the HTML that came back. Anything the rewriter did not
anticipate broke, and pages that refuse to be embedded never loaded at all.
This deletes the proxy (-1604 lines and its tests) and merges the preview and
browser panels into one surface backed by a real Chromium view.

What the panel is now

- A `<webview>` in its own session partition: logins and cookies persist, hot
  reload works because nothing is rewritten, DevTools are one click away.
- Annotation: pick one element, drag a region, or draw freehand, write a note,
  and it reaches chat with a screenshot of the visible page with the marks on it.
- Toolbar: hard reload, page zoom, device sizes, a light/dark switch that
  applies to the page rather than the app, and cookie/cache clearing scoped to
  the panel alone.
- Several pages at once, each tab showing the page's own favicon, and an address
  bar that suggests pages already visited in this project.
- Dev servers are listed from what is actually listening on the machine, checked
  against what a project announced, so a server is offered no matter how it was
  started. One that is still starting is waited for instead of failing.

Remote dev servers

The desktop app binds a local port and pipes raw bytes to the OpenChamber host
over the existing authenticated connection, so the page keeps its own origin at
the root of its own host. The reachable set is exactly what discovery reports
and is re-checked per connection, so an authenticated client cannot dial
arbitrary local services on the host. Links and redirects to another loopback
port stay on the machine that served the page. A tunnel that cannot be opened is
reported; it is never replaced by the plain loopback URL, which would answer
from the user's own machine under a remote address.

Agent control

Browser actions are a separate `openchamber_web` tool: open, snapshot, click,
type, scroll, inspect computed styles, resize between mobile/tablet/desktop, and
capture a screenshot into `.openchamber/screenshots/` in the project. The
existing `openchamber` tool keeps sessions, worktrees and scheduled tasks. Each
has its own setting in the new Settings -> General -> OpenChamber Tools section,
and the plugin is not injected at all when both are off.

Capability belongs to the connected client, not to configuration: a client
declares on its event stream that it can drive a page, which only a Chromium
host does. Exactly one client performs each request — it claims the request
before acting, and the first claim wins — because deciding by whose result
arrives first would be too late for a click that already happened. No client
listening is answered immediately with an explanation rather than a timeout.

Runtime boundaries

Web tabs get a plain iframe that can display a page but not inspect one. The
VS Code extension no longer offers the surface at all, since nothing that makes
the panel worth having works there. Mobile is unaffected.

Native boundary

Camera, microphone, location and device-picker requests from panel pages are
denied — Electron grants them by default when no handler is set, and the panel
loads whatever address the user types. Page capture, appearance emulation and
storage clearing verify that their target belongs to the panel's own session
instead of trusting a web-contents id from the renderer.

Persisted state

Stored `preview` tabs migrate to `browser` (v13 -> v14). Context panel tab
limits are now per surface, so filling one surface no longer evicts another's
tabs. Address history is stored per project and per runtime.

Documentation

`preview.mdx` and `desktop-browser.mdx` rewritten across all locales, the agent
tool settings path corrected, new `DOCUMENTATION.md` for the browser-control
broker and the dev tunnel, and the `ui-api-decoupling` skill updated where it
still described the deleted proxy.
2026-08-13 22:44:13 +03:00

132 lines
5.2 KiB
TypeScript

import { describe, expect, test } from 'bun:test';
import { extractAnnouncedUrls, extractProjectActionUrl, extractTerminalPreviewUrl } from './terminalPreview';
/**
* Real output from a project running four Astro apps behind a dev gateway. The
* gateway logs its own routing table, so the output is full of URLs that look
* perfectly openable but are backends the user should never be sent to — and
* two of the apps are served under a base path.
*/
const GATEWAY_LOG = [
'[gateway] OpenChamber website dev gateway ready on http://localhost:3000',
'[gateway] - site -> http://127.0.0.1:4321',
'[gateway] - docs -> http://127.0.0.1:4322',
'[gateway] - analytics -> http://127.0.0.1:4323',
'[gateway] - api -> http://127.0.0.1:8787',
].join('\n');
const ANALYTICS_LOG = [
'[analytics] astro v5.18.1 ready in 1531 ms',
'[analytics]',
'[analytics] ┃ Local http://localhost:4323/__analytics',
'[analytics] ┃ Network http://192.168.1.115:4323/__analytics',
].join('\n');
describe('announced preview url', () => {
test('takes the gateway front door over the backends it lists', () => {
expect(extractTerminalPreviewUrl(GATEWAY_LOG)).toBe('http://localhost:3000');
});
test('keeps the base path an app is served under', () => {
expect(extractTerminalPreviewUrl(ANALYTICS_LOG)).toBe('http://localhost:4323/__analytics');
});
test('prefers the LAN-independent local address over the network one', () => {
expect(extractTerminalPreviewUrl(ANALYTICS_LOG)).not.toContain('192.168');
});
test('ignores a routing-table line, which announces nothing', () => {
expect(extractTerminalPreviewUrl('[gateway] - analytics -> http://127.0.0.1:4323')).toBeNull();
});
});
describe('project action url', () => {
test('opens the gateway, not a backend from its routing table', () => {
expect(extractProjectActionUrl(GATEWAY_LOG + '\n' + ANALYTICS_LOG)).toBe('http://localhost:3000');
});
test('keeps the base path instead of stripping it to the origin', () => {
// Dropping the path is what lands the user on the app's own 404.
expect(extractProjectActionUrl(ANALYTICS_LOG)).toBe('http://localhost:4323/__analytics');
});
test('falls back to scoring when nothing announces itself', () => {
const output = 'starting\nhttp://127.0.0.1:4323/__analytics\n';
expect(extractProjectActionUrl(output)).toBe('http://127.0.0.1:4323/__analytics');
});
test('prefers a loopback candidate over a public one', () => {
const output = 'see https://example.com:8443/app and http://127.0.0.1:5173/ui';
expect(extractProjectActionUrl(output)).toBe('http://127.0.0.1:5173/ui');
});
test('returns nothing when the output has no url with a port', () => {
expect(extractProjectActionUrl('building...\ndone\n')).toBeNull();
expect(extractProjectActionUrl('')).toBeNull();
});
test('normalizes a wildcard bind to an address the browser can reach', () => {
expect(extractProjectActionUrl('server listening on http://0.0.0.0:4000/app'))
.toBe('http://127.0.0.1:4000/app');
});
});
describe('auto-discovery url', () => {
const ROUTING_TABLE_CHUNK = [
'[gateway] - site -> http://127.0.0.1:4321',
'[gateway] - docs -> http://127.0.0.1:4322',
'[gateway] - analytics -> http://127.0.0.1:4323',
].join('\n');
test('waits rather than opening a backend from a routing table', () => {
// Whether this chunk or the announcement arrives first depends on where the
// terminal split its output, so guessing here is guessing differently each run.
expect(extractProjectActionUrl(ROUTING_TABLE_CHUNK, { requireAnnounced: true })).toBeNull();
});
test('opens the gateway once it announces itself', () => {
expect(extractProjectActionUrl(GATEWAY_LOG, { requireAnnounced: true })).toBe('http://localhost:3000');
});
test('a configured action may still open a bare url it printed, taking the first', () => {
expect(extractProjectActionUrl(ROUTING_TABLE_CHUNK)).toBe('http://127.0.0.1:4321/');
});
});
describe('every announced url', () => {
test('returns each server that announced itself, in order', () => {
const output = [
'[gateway] OpenChamber website dev gateway ready on http://localhost:3000',
'[gateway] - site -> http://127.0.0.1:4321',
'[api] ⚡ API dev server running on http://localhost:8787',
'[analytics] ┃ Local http://localhost:4323/__analytics',
'[docs] ┃ Local http://localhost:4322/docs',
].join('\n');
expect(extractAnnouncedUrls(output)).toEqual([
'http://localhost:3000',
'http://localhost:8787',
'http://localhost:4323/__analytics',
'http://localhost:4322/docs',
]);
});
test('leaves out routing-table entries, which announce nothing', () => {
expect(extractAnnouncedUrls('[gateway] - site -> http://127.0.0.1:4321')).toEqual([]);
});
test('does not repeat an address announced twice', () => {
const output = [
'[site] ┃ Local http://localhost:4321/',
'[site] server running on http://localhost:4321/',
].join('\n');
expect(extractAnnouncedUrls(output)).toEqual(['http://localhost:4321/']);
});
test('is empty for output with no announcement', () => {
expect(extractAnnouncedUrls('building...\ndone')).toEqual([]);
expect(extractAnnouncedUrls('')).toEqual([]);
});
});