From 5b179e7343741c03c48885d61c3ed4d2ee2164d0 Mon Sep 17 00:00:00 2001 From: DavertMik Date: Fri, 13 Nov 2026 04:10:16 +0200 Subject: [PATCH] prima browser opens a playwright-cli session when none is open Runs `playwright-cli open`, falling back to `npx -y @playwright/cli open` (the unscoped `playwright-cli` npm package is deprecated), at --url or the configured base URL, then attaches. A --pw-session name that is not open yet is opened under that name; several open sessions with none named still fail asking for --pw-session. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 5 ++++ boat/prima/src/cli.ts | 2 +- boat/prima/src/prima.ts | 28 +++++++++++++++++--- boat/prima/tests/prima.test.ts | 47 +++++++++++++++++++++++++++++++++- 4 files changed, 77 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6417be2b..63fd1fc4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ ### Changes - [Planner] Stress tests in the `psycho` style no longer expect the app to reject strange values such as SQL-like text. Saving the value and showing it as plain text passes; a crash, an error, lost data or executed markup fails. +- [Prima] `prima browser` opens a playwright-cli session when none is open, instead of only printing the command to start one. It runs `playwright-cli open`, or `npx @playwright/cli open` when playwright-cli is not installed, at `--url` or the configured base URL. A `--pw-session` name that is not open yet is opened under that name. When several sessions are open and none is named, it still asks you to pick one. + ```bash + prima browser --url https://app.example.com # browser: playwright-cli session "default" at ... + prima browser --pw-session staging # opens or reuses the "staging" session + ``` ## 2026-11-12 ### Changes diff --git a/boat/prima/src/cli.ts b/boat/prima/src/cli.ts index 5bd795ef..6c6a9073 100644 --- a/boat/prima/src/cli.ts +++ b/boat/prima/src/cli.ts @@ -174,7 +174,7 @@ export function createPrimaCommands(name = 'prima'): Command { process.exit(0); }); - addBrowserOptions(cmd.command('browser').description('Check that a playwright-cli session is open for prima to drive')).action(async (options) => { + addBrowserOptions(cmd.command('browser').description('Open a playwright-cli session for prima to drive, or report the one already open')).action(async (options) => { try { console.log(await primaFor(options).browserCheck()); } catch (error) { diff --git a/boat/prima/src/prima.ts b/boat/prima/src/prima.ts index 12490990..654e1b5c 100644 --- a/boat/prima/src/prima.ts +++ b/boat/prima/src/prima.ts @@ -1,3 +1,4 @@ +import { spawnSync } from 'node:child_process'; import { createHash } from 'node:crypto'; import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'; import { createRequire } from 'node:module'; @@ -581,7 +582,12 @@ export class Prima { async browserCheck(): Promise { await this.loadConfig(); - const { match, candidates, browser } = await this.discover(); + let discovery = await this.discover(); + if (!discovery.match && (this.pwSessionTitle() || !discovery.candidates.length)) { + this.openPlaywrightSession(); + discovery = await this.discover(); + } + const { match, candidates, browser } = discovery; await browser?.close().catch(() => {}); if (!match) throw this.missingSessionError(candidates); return `browser: ${this.attachmentLabel(match)} at ${match.endpoint}`; @@ -722,9 +728,25 @@ export class Prima { `); } + private openPlaywrightSession(): void { + const args = ['open']; + const title = this.pwSessionTitle(); + if (title) args.unshift(`-s=${title}`); + const url = this.options.url || this.configBaseUrl(); + if (url) args.push(url); + + const stdio: any = ['ignore', 'ignore', 'inherit']; + const local = spawnSync('playwright-cli', args, { stdio }); + if (!local.error) return; + spawnSync('npx', ['-y', '@playwright/cli', ...args], { stdio }); + } + + private pwSessionTitle(): string | undefined { + return this.options.pwSession ?? process.env.PLAYWRIGHT_CLI_SESSION; + } + private async discover(descriptors = readDescriptors()): Promise { - const title = this.options.pwSession ?? process.env.PLAYWRIGHT_CLI_SESSION; - const opts = { title }; + const opts = { title: this.pwSessionTitle() }; const alive = new Map(); for (const candidate of selectDescriptor(descriptors, opts).candidates) { diff --git a/boat/prima/tests/prima.test.ts b/boat/prima/tests/prima.test.ts index 0cdc8325..e319c17c 100644 --- a/boat/prima/tests/prima.test.ts +++ b/boat/prima/tests/prima.test.ts @@ -1744,9 +1744,54 @@ describe('Prima browser instances', () => { expect(await prima.browserCheck()).toBe('browser: playwright-cli session "auth" at ws://127.0.0.1:4321/auth'); }); - test('browserCheck fails proposing playwright-cli open when no session is open', async () => { + test('browserCheck opens a playwright-cli session when none is open', async () => { const { prima } = fakePrima({ url: 'https://app.example.com' }); (prima as any).loadConfig = async () => ({}); + let opened = false; + (prima as any).openPlaywrightSession = () => { + opened = true; + }; + (prima as any).discover = () => { + if (!opened) return { candidates: [] }; + return { match: { title: 'default', endpoint: 'ws://127.0.0.1:4321/default', workspaceDir: '' }, candidates: [] }; + }; + + expect(await prima.browserCheck()).toBe('browser: playwright-cli session "default" at ws://127.0.0.1:4321/default'); + }); + + test('browserCheck opens a named session that is not open yet beside other sessions', async () => { + const { prima } = fakePrima({ pwSession: 'mine' }); + (prima as any).loadConfig = async () => ({}); + let opened = false; + (prima as any).openPlaywrightSession = () => { + opened = true; + }; + (prima as any).discover = () => { + const other = { title: 'other', endpoint: 'ws://127.0.0.1:4321/other', workspaceDir: '' }; + if (!opened) return { candidates: [other] }; + return { match: { title: 'mine', endpoint: 'ws://127.0.0.1:4321/mine', workspaceDir: '' }, candidates: [other] }; + }; + + expect(await prima.browserCheck()).toBe('browser: playwright-cli session "mine" at ws://127.0.0.1:4321/mine'); + }); + + test('browserCheck asks to pick a session when several are open and none is named', async () => { + const { prima } = fakePrima(); + (prima as any).loadConfig = async () => ({}); + let opened = false; + (prima as any).openPlaywrightSession = () => { + opened = true; + }; + (prima as any).discover = () => ({ candidates: [{ title: 'a' }, { title: 'b' }] }); + + await expect(prima.browserCheck()).rejects.toThrow(/--pw-session/); + expect(opened).toBe(false); + }); + + test('browserCheck fails proposing playwright-cli open when the launch opened nothing', async () => { + const { prima } = fakePrima({ url: 'https://app.example.com' }); + (prima as any).loadConfig = async () => ({}); + (prima as any).openPlaywrightSession = () => {}; (prima as any).discover = () => ({ candidates: [] }); await expect(prima.browserCheck()).rejects.toThrow(/playwright-cli open https:\/\/app.example.com/);