diff --git a/beehive/package.json b/beehive/package.json index 9306ca9110..614c89bb6f 100644 --- a/beehive/package.json +++ b/beehive/package.json @@ -1 +1 @@ -{"name":"@buzz/beehive","version":"0.0.1","private":true,"type":"module","engines":{"node":">=22.18"},"scripts":{"check":"tsc --noEmit","test":"node --test test/*.test.ts","start":"node src/cli.ts"},"dependencies":{"@napi-rs/keyring":"2.0.0","@noble/curves":"1.2.0","nostr-tools":"2.23.1","ws":"8.21.0"},"devDependencies":{"@types/node":"22.18.0","@types/ws":"8.18.1","typescript":"5.9.3"}} +{"name":"@buzz/beehive","version":"0.0.1","private":true,"type":"module","engines":{"node":">=22.18"},"scripts":{"check":"tsc --noEmit","test":"node --test $(find test -name '*.test.ts' ! -name 'shell-opentui.test.ts')","test:shell":"bun test test/shell-*.test.ts","start":"node src/cli.ts","start:tui":"bun src/manager-view.ts"},"dependencies":{"@napi-rs/keyring":"2.0.0","@noble/curves":"1.2.0","nostr-tools":"2.23.1","ws":"8.21.0","@opentui/core":"0.5.11"},"devDependencies":{"@types/node":"22.18.0","@types/ws":"8.18.1","typescript":"5.9.3"}} diff --git a/beehive/pnpm-lock.yaml b/beehive/pnpm-lock.yaml index 2bd06d161a..74a44c5f70 100644 --- a/beehive/pnpm-lock.yaml +++ b/beehive/pnpm-lock.yaml @@ -14,6 +14,9 @@ importers: '@noble/curves': specifier: 1.2.0 version: 1.2.0 + '@opentui/core': + specifier: 0.5.11 + version: 0.5.11(typescript@5.9.3)(web-tree-sitter@0.25.10) nostr-tools: specifier: 2.23.1 version: 2.23.1(typescript@5.9.3) @@ -133,6 +136,54 @@ packages: resolution: {integrity: sha512-XlOlEbQcE9fmuXxrVTXCTlG2nlRXa9Rj3rr5Ue/+tX+nmkgbX720YHh0VR3hBF9xDvwnb8D2shVGOwNx+ulArw==} engines: {node: '>= 20.19.0'} + '@opentui/core-darwin-arm64@0.5.11': + resolution: {integrity: sha512-DRXY5ioq+n1ZNAMAcaFaBunr0cmi2gqucjbTW7lgFp8t9uN3fNZnLTDOqkCTQtT2XhrU4GXqySaPSQ4qFG/EAQ==} + cpu: [arm64] + os: [darwin] + + '@opentui/core-darwin-x64@0.5.11': + resolution: {integrity: sha512-yP/8GliJDiJNm8YYJKvgWuy6xyCEd8d4GwBVOIzCFOI7ZIbGP8GOTvmwIjRW4Paw20pTttWMWyRoQiCvOXvH2g==} + cpu: [x64] + os: [darwin] + + '@opentui/core-linux-arm64-musl@0.5.11': + resolution: {integrity: sha512-x+xeR2LYibvIi/qQetRjJR008sFRve60QuDcO8ItxUwzFeKTDzl5CEiZpBXfm5I4FhRNuyuj0TSPIFadMvrjFQ==} + cpu: [arm64] + os: [linux] + libc: [musl] + + '@opentui/core-linux-arm64@0.5.11': + resolution: {integrity: sha512-zBIsRFHlLUYFNhapRSNt9dz4mC8gZ4Wxcfy3A+2AwqsgCipcr2FkIuAXYqN08q+IvqFX7DfqgIFGWDNedHTPUg==} + cpu: [arm64] + os: [linux] + + '@opentui/core-linux-x64-musl@0.5.11': + resolution: {integrity: sha512-MyqOnSs8pTYG2xmFr1xt6xZIuHu2Xu4pkle9my9JdE+WClmusHf0YN9Eas6jQLAq5XUi34r22wMeK0juk93zyw==} + cpu: [x64] + os: [linux] + libc: [musl] + + '@opentui/core-linux-x64@0.5.11': + resolution: {integrity: sha512-pSOXqOADrv+zINOgR3FDFA9zVRaim3zl8/yhtO+X9rEJ6f34z3gDund0Gf88hNJSMpZK5xWtipEVm28RY5VF8w==} + cpu: [x64] + os: [linux] + + '@opentui/core-win32-arm64@0.5.11': + resolution: {integrity: sha512-MGGRXIDJ//HyaqC5ndSr7/CUl+ICdYEAMcjbA00UWsthh4ZO/rYhPkyqOaTn5ck6+G4ca3/+M76J+jImMYTjNg==} + cpu: [arm64] + os: [win32] + + '@opentui/core-win32-x64@0.5.11': + resolution: {integrity: sha512-sMEGX9rhiPd1gBa190jzj5uIdzKCMImxHmr22hJLxIRWqejxWvqGnRBkW68wT8NfMViDiMtZWavgWL7GgmYCAQ==} + cpu: [x64] + os: [win32] + + '@opentui/core@0.5.11': + resolution: {integrity: sha512-pImMfjCNx7JUp9Df1LRZBDLisWqgzOLXZKXO+hh3jA9ujBFPQnZOqbG+/N5uVAAX8IZGaqhYSQRwSZIFBmcfbQ==} + engines: {bun: '>=1.3.0', node: '>=26.4.0'} + peerDependencies: + web-tree-sitter: 0.25.10 + '@scure/base@2.0.0': resolution: {integrity: sha512-3E1kpuZginKkek01ovG8krQ0Z44E3DHPjc5S2rjJw9lZn3KSQOs8S7wqikF/AH7iRanHypj85uGyxk0XAyC37w==} @@ -148,6 +199,31 @@ packages: '@types/ws@8.18.1': resolution: {integrity: sha512-ThVF6DCVhA8kUGy+aazFQ4kXQ7E1Ty7A3ypFOe0IcJV8O/M511G99AW24irKrW56Wt44yG9+ij8FaqoBGkuBXg==} + ansi-regex@6.3.0: + resolution: {integrity: sha512-WpDfL7NO6j7tH88IDBNVdUJxDh9nmCteAVW9dsep846XdwF4naCBK+/tGLX3KJgcpgMRXCFlTM2hKGoK9FsdrQ==} + engines: {node: '>=12'} + + bun-ffi-structs@0.3.1: + resolution: {integrity: sha512-3gM7PpVWLyrwxWjcilSiGuhWanhZivvo6l0u573NziPH6f/gwk6McbaYgn7oJWov6pKGRTDbrg94W5DcJsKTtQ==} + peerDependencies: + typescript: ^5 + + diff@9.0.0: + resolution: {integrity: sha512-svtcdpS8CgJyqAjEQIXdb3OjhFVVYjzGAPO8WGCmRbrml64SPw/jJD4GoE98aR7r25A0XcgrK3F02yw9R/vhQw==} + engines: {node: '>=0.3.1'} + + emoji-regex@10.6.0: + resolution: {integrity: sha512-toUI84YS5YmxW219erniWD0CIVOo46xGKColeNQRgOzDorgBi1v4D71/OFzgD9GO2UGKIv1C3Sp8DAn0+j5w7A==} + + get-east-asian-width@1.6.0: + resolution: {integrity: sha512-QRbvDIbx6YklUe6RxeTeleMR0yv3cYH6PsPZHcnVn7xv7zO1BHN8r0XETu8n6Ye3Q+ahtSarc3WgtNWmehIBfA==} + engines: {node: '>=18'} + + marked@17.0.1: + resolution: {integrity: sha512-boeBdiS0ghpWcSwoNm/jJBwdpFaMnZWRzjA6SkUMYb40SVaN1x7mmfGKp0jvexGcx+7y2La5zRZsYFZI6Qpypg==} + engines: {node: '>= 20'} + hasBin: true + nostr-tools@2.23.1: resolution: {integrity: sha512-Q5SJ1omrseBFXtLwqDhufpFLA6vX3rS/IuBCc974qaYX6YKGwEPxa/ZsyxruUOr+b+5EpWL2hFmCB5AueYrfBw==} peerDependencies: @@ -159,6 +235,14 @@ packages: nostr-wasm@0.1.0: resolution: {integrity: sha512-78BTryCLcLYv96ONU8Ws3Q1JzjlAt+43pWQhIl86xZmWeegYCNLPml7yQ+gG3vR6V5h4XGj+TxO+SS5dsThQIA==} + string-width@7.2.0: + resolution: {integrity: sha512-tsaTIkKW9b4N+AEj+SVA+WhJzV7/zMhcSu78mLKWSk7cXMOSHsBKFWUs0fWwq8QyK3MgJBQRX6Gbi4kYbdvGkQ==} + engines: {node: '>=18'} + + strip-ansi@7.1.2: + resolution: {integrity: sha512-gmBGslpoQJtgnMAvOVqGZpEz9dyoKTCzy2nfz/n8aIFhN/jCE/rCmcxabB6jOOHV+0WNnylOxaxBQPSvcWklhA==} + engines: {node: '>=12'} + typescript@5.9.3: resolution: {integrity: sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==} engines: {node: '>=14.17'} @@ -167,6 +251,14 @@ packages: undici-types@6.21.0: resolution: {integrity: sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==} + web-tree-sitter@0.25.10: + resolution: {integrity: sha512-Y09sF44/13XvgVKgO2cNDw5rGk6s26MgoZPXLESvMXeefBf7i6/73eFurre0IsTW6E14Y0ArIzhUMmjoc7xyzA==} + peerDependencies: + '@types/emscripten': ^1.40.0 + peerDependenciesMeta: + '@types/emscripten': + optional: true + ws@8.21.0: resolution: {integrity: sha512-Vsp28b7DRcimFQvrqu2Wek3z1iYxDCWqHYB8Qsnk/S4RfaCQzPGPyBNuVjJV3cd6UiKtUtp6sNM77gWvzcCH+g==} engines: {node: '>=10.0.0'} @@ -246,6 +338,50 @@ snapshots: '@noble/hashes@2.0.1': {} + '@opentui/core-darwin-arm64@0.5.11': + optional: true + + '@opentui/core-darwin-x64@0.5.11': + optional: true + + '@opentui/core-linux-arm64-musl@0.5.11': + optional: true + + '@opentui/core-linux-arm64@0.5.11': + optional: true + + '@opentui/core-linux-x64-musl@0.5.11': + optional: true + + '@opentui/core-linux-x64@0.5.11': + optional: true + + '@opentui/core-win32-arm64@0.5.11': + optional: true + + '@opentui/core-win32-x64@0.5.11': + optional: true + + '@opentui/core@0.5.11(typescript@5.9.3)(web-tree-sitter@0.25.10)': + dependencies: + bun-ffi-structs: 0.3.1(typescript@5.9.3) + diff: 9.0.0 + marked: 17.0.1 + string-width: 7.2.0 + strip-ansi: 7.1.2 + web-tree-sitter: 0.25.10 + optionalDependencies: + '@opentui/core-darwin-arm64': 0.5.11 + '@opentui/core-darwin-x64': 0.5.11 + '@opentui/core-linux-arm64': 0.5.11 + '@opentui/core-linux-arm64-musl': 0.5.11 + '@opentui/core-linux-x64': 0.5.11 + '@opentui/core-linux-x64-musl': 0.5.11 + '@opentui/core-win32-arm64': 0.5.11 + '@opentui/core-win32-x64': 0.5.11 + transitivePeerDependencies: + - typescript + '@scure/base@2.0.0': {} '@scure/bip32@2.0.1': @@ -267,6 +403,20 @@ snapshots: dependencies: '@types/node': 22.18.0 + ansi-regex@6.3.0: {} + + bun-ffi-structs@0.3.1(typescript@5.9.3): + dependencies: + typescript: 5.9.3 + + diff@9.0.0: {} + + emoji-regex@10.6.0: {} + + get-east-asian-width@1.6.0: {} + + marked@17.0.1: {} + nostr-tools@2.23.1(typescript@5.9.3): dependencies: '@noble/ciphers': 2.1.1 @@ -281,8 +431,20 @@ snapshots: nostr-wasm@0.1.0: {} + string-width@7.2.0: + dependencies: + emoji-regex: 10.6.0 + get-east-asian-width: 1.6.0 + strip-ansi: 7.1.2 + + strip-ansi@7.1.2: + dependencies: + ansi-regex: 6.3.0 + typescript@5.9.3: {} undici-types@6.21.0: {} + web-tree-sitter@0.25.10: {} + ws@8.21.0: {} diff --git a/beehive/src/manager-entry.ts b/beehive/src/manager-entry.ts index d4a799a23a..59f354eacc 100644 --- a/beehive/src/manager-entry.ts +++ b/beehive/src/manager-entry.ts @@ -1,5 +1,25 @@ -import { ShellApp } from './shell-app.ts'; +import { existsSync } from 'node:fs'; +import { dirname, join } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { spawnSync } from 'node:child_process'; -const app = new ShellApp({ stdin: process.stdin, stdout: process.stdout }); -for (const signal of ['SIGINT', 'SIGTERM'] as const) process.once(signal, () => app.close()); -await app.start(); +const root = join(dirname(fileURLToPath(import.meta.url)), '..'); +const packagedBun = join(root, 'runtime', 'bun'); +const bun = existsSync(packagedBun) ? packagedBun : process.env.BEEHIVE_BUN || 'bun'; +const version = spawnSync(bun, ['--version'], { encoding: 'utf8' }); +if (version.error || version.status !== 0) { + console.error('beehive: Bun 1.4.2 is required to run the manager. Set BEEHIVE_BUN to the pinned executable or complete the packaged runtime installation.'); + process.exit(1); +} +if (version.stdout.trim() !== '1.4.2') { + console.error(`beehive: Bun 1.4.2 is required; ${bun} reported ${version.stdout.trim() || 'an unknown version'}.`); + process.exit(1); +} +const child = spawnSync(bun, [join(root, 'src', 'manager-view.ts')], { stdio: 'inherit' }); +if (child.error) { + console.error(`beehive: cannot start the OpenTUI manager: ${child.error.message}`); + process.exit(1); +} +if (child.status !== null) process.exit(child.status); +if (child.signal) process.kill(process.pid, child.signal); +process.exit(1); diff --git a/beehive/src/manager-view.ts b/beehive/src/manager-view.ts new file mode 100644 index 0000000000..9e39a07e89 --- /dev/null +++ b/beehive/src/manager-view.ts @@ -0,0 +1,20 @@ +import { createCliRenderer } from '@opentui/core'; +import { OpenTuiShell } from './opentui-shell.ts'; + +const renderer = await createCliRenderer({ + exitOnCtrlC: false, + screenMode: 'alternate-screen', + consoleMode: 'disabled', + openConsoleOnError: false, +}); +const shell = new OpenTuiShell(renderer); +const stop = () => shell.close(); +process.once('SIGINT', stop); +process.once('SIGTERM', stop); +try { + await shell.done; +} finally { + process.off('SIGINT', stop); + process.off('SIGTERM', stop); + shell.close(); +} diff --git a/beehive/src/opentui-shell.ts b/beehive/src/opentui-shell.ts new file mode 100644 index 0000000000..37b71a4730 --- /dev/null +++ b/beehive/src/opentui-shell.ts @@ -0,0 +1,150 @@ +import { BoxRenderable, TextRenderable, type CliRenderer, type KeyEvent } from '@opentui/core'; +import { brand, destinations, listWidth, ownerLabel, ShellState, type FocusRegion } from './shell-state.ts'; + +export const palette = { + surface: '#0d100e', text: '#dce1d8', muted: '#7d867c', divider: '#4a5149', + focus: '#ffd34e', selected: '#dce1d8', selectedText: '#10130f', dialog: '#9ca49b', scrim: '#070907', +} as const; + +const wideFooter = '←→ focus Enter open Tab panes Esc return ? help q quit'; +const compactFooter = '←→ nav ↵ open Tab panes Esc back ? help q quit'; +const minimumFooter = 'Ctrl-Q quit'; + +/** OpenTUI owns rendering, layout, input dispatch, pointer dispatch, and lifecycle. */ +export class OpenTuiShell { + readonly state = new ShellState(); + readonly done: Promise; + private finish!: () => void; + private closed = false; + private readonly root: BoxRenderable; + private readonly brandText: TextRenderable; + private readonly ownerText: TextRenderable; + private readonly nav: BoxRenderable; + private readonly navItems: TextRenderable[]; + private readonly headerRule: TextRenderable; + private readonly body: BoxRenderable; + private readonly listPane: BoxRenderable; + private readonly detailPane: BoxRenderable; + private readonly footerRule: TextRenderable; + private readonly footer: TextRenderable; + private help?: BoxRenderable; + + constructor(readonly renderer: CliRenderer) { + this.done = new Promise(resolve => { this.finish = resolve; }); + this.root = new BoxRenderable(renderer, { width: '100%', height: '100%', flexDirection: 'column', backgroundColor: palette.surface }); + renderer.root.add(this.root); + + const identity = new BoxRenderable(renderer, { width: '100%', height: 1, backgroundColor: palette.surface }); + this.root.add(identity); + this.brandText = new TextRenderable(renderer, { position: 'absolute', left: 1, top: 0, height: 1, fg: palette.focus, content: brand }); + identity.add(this.brandText); + this.ownerText = new TextRenderable(renderer, { position: 'absolute', top: 0, height: 1, fg: palette.focus, content: ownerLabel, + onMouseDown: () => this.activate(destinations.length) }); + identity.add(this.ownerText); + + this.nav = new BoxRenderable(renderer, { width: '100%', height: 1, flexDirection: 'row', justifyContent: 'center', gap: 1, backgroundColor: palette.surface }); + this.root.add(this.nav); + this.navItems = destinations.map((label, index) => { + const item = new TextRenderable(renderer, { width: label.length, height: 1, content: label, fg: palette.text, onMouseDown: () => this.activate(index) }); + this.nav.add(item); return item; + }); + this.headerRule = new TextRenderable(renderer, { height: 1, fg: palette.divider }); this.root.add(this.headerRule); + + this.body = new BoxRenderable(renderer, { flexGrow: 1, minHeight: 1, width: '100%', flexDirection: 'row', backgroundColor: palette.surface }); + this.root.add(this.body); + this.listPane = this.pane('list'); this.detailPane = this.pane('detail'); + this.body.add(this.listPane); this.body.add(this.detailPane); + + this.footerRule = new TextRenderable(renderer, { height: 1, fg: palette.divider }); this.root.add(this.footerRule); + this.footer = new TextRenderable(renderer, { height: 1, fg: palette.muted, content: wideFooter }); this.root.add(this.footer); + + renderer.keyInput.on('keypress', key => this.key(key)); + renderer.on('resize', () => this.resize()); + renderer.on('destroy', () => this.close(false)); + this.resize(); + } + + private pane(region: Exclude) { + return new BoxRenderable(this.renderer, { height: '100%', border: true, borderColor: palette.divider, backgroundColor: palette.surface, + onMouseDown: () => { if (!this.visible(region)) return; this.state.focus = region; this.paint(); } }); + } + + private visible(region: 'list' | 'detail') { + if (!this.state.collection) return region === 'detail'; + return this.state.splitPane || this.state.narrowPane === region; + } + + private key(key: KeyEvent) { + const effect = this.state.key(key.name, { ctrl: key.ctrl, shift: key.shift }); + if (effect !== 'none') key.preventDefault(); + if (effect === 'quit') this.close(); + else if (effect === 'render') this.paint(); + } + + private activate(index: number) { + if (this.state.belowMinimum || this.state.helpOpen) return; + this.state.activateHeader(index); this.paint(); + } + + private resize() { + this.state.resize(this.renderer.width, this.renderer.height); + this.paint(); + } + + private paint() { + if (this.closed) return; + const width = this.renderer.width; + this.ownerText.left = Math.max(0, width - ownerLabel.length - 1); + this.brandText.width = Math.max(0, this.ownerText.left - 1); + const rule = '─'.repeat(Math.max(1, width)); + this.headerRule.content = rule; this.footerRule.content = rule; + this.footer.content = this.state.belowMinimum ? minimumFooter : width >= 70 ? wideFooter : compactFooter; + + this.navItems.forEach((item, index) => { + const focused = this.state.focus === 'header' && this.state.headerIndex === index; + const active = this.state.mode === 'section' && this.state.activeSection === index; + item.bg = focused ? palette.selected : palette.surface; + item.fg = focused ? palette.selectedText : active ? palette.focus : index < 2 ? palette.muted : palette.text; + }); + const ownerFocused = this.state.focus === 'header' && this.state.headerIndex === destinations.length; + this.ownerText.bg = ownerFocused ? palette.selected : palette.surface; + this.ownerText.fg = ownerFocused ? palette.selectedText : palette.focus; + + this.listPane.visible = this.visible('list'); this.detailPane.visible = this.visible('detail'); + if (this.state.splitPane) { + this.listPane.width = listWidth(width); this.detailPane.width = Math.max(1, width - listWidth(width)); this.detailPane.flexGrow = 0; + } else { + this.listPane.width = '100%'; this.detailPane.width = '100%'; this.detailPane.flexGrow = 1; + } + this.listPane.borderColor = this.state.focus === 'list' ? palette.focus : palette.divider; + this.detailPane.borderColor = this.state.focus === 'detail' ? palette.focus : palette.divider; + this.syncHelp(); + } + + private syncHelp() { + if (!this.state.helpOpen) { + if (this.help) { this.help.destroyRecursively(); this.help = undefined; } + return; + } + const width = Math.max(4, Math.min(62, this.renderer.width - 4)); + const height = Math.max(4, Math.min(14, this.renderer.height - 4)); + if (!this.help) { + this.help = new BoxRenderable(this.renderer, { position: 'absolute', border: true, borderColor: palette.dialog, backgroundColor: palette.scrim, flexDirection: 'column', paddingLeft: 2, paddingRight: 2, paddingTop: 1 }); + this.renderer.root.add(this.help); + this.help.add(new TextRenderable(this.renderer, { fg: palette.focus, height: 2, content: 'HELP\n──────────────────────────────────────────────────────────' })); + this.help.add(new TextRenderable(this.renderer, { fg: palette.text, content: '← → focus a destination\nEnter activate the focused destination\nTab / Shift-Tab move between visible regions\nEsc return to the active header control\n? / Enter / Esc close help\nq quit Beehive' })); + } + this.help.width = width; this.help.height = height; + this.help.left = Math.floor((this.renderer.width - width) / 2); + this.help.top = Math.floor((this.renderer.height - height) / 2); + } + + close(destroy = true) { + if (this.closed) return; + this.closed = true; + if (destroy) this.renderer.destroy(); + this.finish(); + } +} + +export const footerGuides = { wide: wideFooter, compact: compactFooter, minimum: minimumFooter } as const; diff --git a/beehive/src/shell-app.ts b/beehive/src/shell-app.ts deleted file mode 100644 index c4851bc095..0000000000 --- a/beehive/src/shell-app.ts +++ /dev/null @@ -1,90 +0,0 @@ -import { renderFrame } from './shell-frame.ts'; -import { destinations, ownerLabel, ShellState } from './shell-state.ts'; - -export type TerminalIO = { - stdin: NodeJS.ReadStream; - stdout: NodeJS.WriteStream; -}; - -/** Owns only terminal presentation and input. It never opens product state or services. */ -export class ShellApp { - readonly state = new ShellState(); - private closed = false; - private buffer = ''; - private priorRaw = false; - private resolveDone!: () => void; - readonly done = new Promise(resolve => { this.resolveDone = resolve; }); - - private readonly io: TerminalIO; - constructor(io: TerminalIO) { this.io = io; } - - start() { - this.priorRaw = Boolean(this.io.stdin.isRaw); - if (this.io.stdin.isTTY) this.io.stdin.setRawMode(true); - this.io.stdin.setEncoding('utf8'); - this.io.stdin.resume(); - this.io.stdin.on('data', this.onData); - this.io.stdout.on('resize', this.onResize); - this.io.stdout.write('\x1b[?1049h\x1b[?25l\x1b[?1000h\x1b[?1006h'); - this.render(); - return this.done; - } - - private onResize = () => this.render(); - private onData = (chunk: string) => { - this.buffer += chunk; - while (this.buffer.length) { - const mouse = this.buffer.match(/^\x1b\[<([0-9]+);([0-9]+);([0-9]+)([Mm])/); - if (mouse) { - this.buffer = this.buffer.slice(mouse[0].length); - if (mouse[4] === 'M' && Number(mouse[1]) === 0) this.pointer(Number(mouse[2]) - 1, Number(mouse[3]) - 1); - continue; - } - const keys: [string, string, { ctrl?: boolean; shift?: boolean }?][] = [ - ['\x1b[A', 'up'], ['\x1b[B', 'down'], ['\x1b[C', 'right'], ['\x1b[D', 'left'], ['\x1b[Z', 'tab', { shift: true }], - ['\r', 'return'], ['\n', 'return'], ['\t', 'tab'], ['\x1b', 'escape'], ['\x11', 'q', { ctrl: true }], ['\x03', 'c', { ctrl: true }], - ]; - const found = keys.find(([sequence]) => this.buffer.startsWith(sequence)); - if (found) { this.buffer = this.buffer.slice(found[0].length); this.dispatch(found[1], found[2]); continue; } - if (this.buffer.startsWith('\x1b[') && this.buffer.length < 6) return; - const character = this.buffer[0]!; this.buffer = this.buffer.slice(1); - if (character === 'q' || character === '?') this.dispatch(character); - } - }; - - private dispatch(name: string, options: { ctrl?: boolean; shift?: boolean } = {}) { - const effect = this.state.key(name, { ...options, belowMinimum: this.width < 50 || this.height < 20 }); - if (effect === 'quit') this.close(); else if (effect === 'render') this.render(); - } - - private pointer(x: number, y: number) { - if (this.width < 50 || this.height < 20 || this.state.helpOpen) return; - if (y === 0 && x >= this.width - ownerLabel.length - 1) { this.state.openHeader(destinations.length); this.render(); return; } - if (y !== 1) return; - const total = destinations.reduce((sum, item) => sum + item.length, 0) + destinations.length - 1; - let cursor = Math.max(0, Math.floor((this.width - total) / 2)); - for (let index = 0; index < destinations.length; index++) { - const end = cursor + destinations[index]!.length; - if (x >= cursor && x < end) { this.state.openHeader(index); this.render(); return; } - cursor = end + 1; - } - } - - private get width() { return this.io.stdout.columns || 80; } - private get height() { return this.io.stdout.rows || 24; } - render() { - if (this.closed) return; - this.io.stdout.write(`\x1b[H${renderFrame(this.state, this.width, this.height)}`); - } - close() { - if (this.closed) return; - this.closed = true; - this.io.stdin.off('data', this.onData); - this.io.stdout.off('resize', this.onResize); - if (this.io.stdin.isTTY) this.io.stdin.setRawMode(this.priorRaw); - this.io.stdin.pause(); - this.io.stdin.unref(); - this.io.stdout.write('\x1b[?1006l\x1b[?1000l\x1b[?25h\x1b[?1049l'); - this.resolveDone(); - } -} diff --git a/beehive/src/shell-frame.ts b/beehive/src/shell-frame.ts deleted file mode 100644 index 451565f57f..0000000000 --- a/beehive/src/shell-frame.ts +++ /dev/null @@ -1,113 +0,0 @@ -import { compactShortcutGuide, destinations, ownerLabel, shortcutGuide, type FocusRegion, type ShellState } from './shell-state.ts'; - -const rgb = (hex: string, background = false) => { - const [r, g, b] = [hex.slice(1, 3), hex.slice(3, 5), hex.slice(5, 7)].map(value => Number.parseInt(value, 16)); - return `\x1b[${background ? 48 : 38};2;${r};${g};${b}m`; -}; -const reset = '\x1b[0m'; -const colors = { - surface: rgb('#0d100e', true), text: rgb('#dce1d8'), muted: rgb('#7d867c'), - divider: rgb('#4a5149'), focus: rgb('#ffd34e'), selected: rgb('#dce1d8', true), selectedText: rgb('#10130f'), - dialog: rgb('#9ca49b'), -}; -type Style = 'text' | 'muted' | 'divider' | 'focus' | 'selected' | 'dialog'; -const styleCode = (style: Style) => colors.surface + (style === 'selected' ? colors.selected + colors.selectedText : colors[style]); - -function put(chars: string[], styles: Style[], start: number, text: string, style: Style) { - for (let offset = 0; offset < text.length && start + offset < chars.length; offset++) { - if (start + offset < 0) continue; - chars[start + offset] = text[offset]!; - styles[start + offset] = style; - } -} -function line(chars: string[], styles: Style[]) { - let output = '', prior: Style | undefined; - for (let index = 0; index < chars.length; index++) { - const style = styles[index]!; - if (style !== prior) { output += styleCode(style); prior = style; } - output += chars[index]; - } - return output + reset; -} -function blank(width: number, style: Style = 'text') { return [Array(width).fill(' ') as string[], Array(width).fill(style) as Style[]] as const; } - -export function listWidth(width: number) { - if (width >= 90) return Math.min(35, Math.max(28, Math.floor((width - 1) * 0.32))); - if (width >= 60) return Math.min(28, Math.max(24, Math.floor((width - 1) * 0.36))); - return width; -} - -/** Deterministic cell renderer. Pane documents intentionally contain zero text. */ -export function renderFrame(state: ShellState, width: number, height: number) { - width = Math.max(1, Math.floor(width)); height = Math.max(1, Math.floor(height)); - const rows: string[] = []; - { - const [chars, styles] = blank(width); - put(chars, styles, 1, '⬢ BEEHIVE', 'focus'); - put(chars, styles, width - ownerLabel.length - 1, ownerLabel, state.headerIndex === destinations.length && state.focus === 'header' ? 'selected' : 'focus'); - rows.push(line(chars, styles)); - } - { - const [chars, styles] = blank(width); - const total = destinations.reduce((sum, item) => sum + item.length, 0) + destinations.length - 1; - let cursor = Math.max(0, Math.floor((width - total) / 2)); - destinations.forEach((item, index) => { - const focused = state.focus === 'header' && state.headerIndex === index; - const active = state.mode === 'section' && state.activeSection === index; - const gated = index < 2; - put(chars, styles, cursor, item, focused ? 'selected' : active ? 'focus' : gated ? 'muted' : 'text'); - cursor += item.length + 1; - }); - rows.push(line(chars, styles)); - } - { - const [chars, styles] = blank(width, 'divider'); chars.fill('─'); rows.push(line(chars, styles)); - } - const bodyHeight = Math.max(0, height - 5); - const split = state.splitPane && width >= 60; - const divider = split ? listWidth(width) : -1; - for (let row = 0; row < bodyHeight; row++) { - const [chars, styles] = blank(width); - if (split && divider < width) { chars[divider] = '│'; styles[divider] = 'divider'; } - outline(chars, styles, state.focus, split, divider, row, bodyHeight); - rows.push(line(chars, styles)); - } - if (height >= 2) { - const [chars, styles] = blank(width, 'divider'); chars.fill('─'); rows.push(line(chars, styles)); - const [footer, footerStyles] = blank(width, 'muted'); - put(footer, footerStyles, 1, width >= 70 ? shortcutGuide : compactShortcutGuide, 'muted'); - rows.push(line(footer, footerStyles)); - } - if (state.helpOpen) applyHelp(rows, width, height); - return rows.slice(0, height).join('\n'); -} - -function outline(chars: string[], styles: Style[], focus: FocusRegion, split: boolean, divider: number, row: number, bodyHeight: number) { - if (focus === 'header' || bodyHeight < 2) return; - const left = focus === 'list' && split ? 0 : split ? divider + 1 : 0; - const right = focus === 'list' && split ? divider - 1 : chars.length - 1; - if (right < left) return; - if (row === 0 || row === bodyHeight - 1) { - for (let column = left; column <= right; column++) { chars[column] = '─'; styles[column] = 'focus'; } - } else { - chars[left] = '│'; styles[left] = 'focus'; chars[right] = '│'; styles[right] = 'focus'; - } -} - -function applyHelp(rows: string[], width: number, height: number) { - const boxWidth = Math.max(1, Math.min(62, width - 4)); - const content = ['HELP', '', '← → move through the complete header', 'Enter open the focused destination', 'Tab move through available pane regions', 'Esc return to the initiating header control', '? close help', 'q quit Beehive']; - const boxHeight = Math.min(height - 4, content.length + 4); - if (boxWidth < 4 || boxHeight < 4) return; - const left = Math.floor((width - boxWidth) / 2), top = Math.floor((height - boxHeight) / 2); - for (let y = 0; y < boxHeight; y++) { - const [chars, styles] = blank(width); - for (let x = left; x < left + boxWidth; x++) styles[x] = 'text'; - chars[left] = y === 0 ? '┌' : y === boxHeight - 1 ? '└' : '│'; - chars[left + boxWidth - 1] = y === 0 ? '┐' : y === boxHeight - 1 ? '┘' : '│'; - styles[left] = styles[left + boxWidth - 1] = 'dialog'; - if (y === 0 || y === boxHeight - 1) for (let x = left + 1; x < left + boxWidth - 1; x++) { chars[x] = '─'; styles[x] = 'dialog'; } - const text = content[y - 2]; if (text) put(chars, styles, left + 2, text.slice(0, boxWidth - 4), y === 2 ? 'focus' : 'text'); - rows[top + y] = line(chars, styles); - } -} diff --git a/beehive/src/shell-state.ts b/beehive/src/shell-state.ts index b66f48ab1c..65a2d421fc 100644 --- a/beehive/src/shell-state.ts +++ b/beehive/src/shell-state.ts @@ -1,53 +1,85 @@ export const destinations = ['HOST', 'AGENTS', 'HARNESSES', 'PROVIDERS'] as const; +// The current prototype's signed-out owner control is a state label, not a sign-in action. export const ownerLabel = 'SIGNED OUT'; -export const shortcutGuide = '←→ header Enter open Tab panes Esc return ? help q quit'; -export const compactShortcutGuide = '←→ nav ↵ open Tab panes Esc back ? help q quit'; +export const brand = '⬢ BEEHIVE'; export type FocusRegion = 'header' | 'list' | 'detail'; export type ShellMode = 'section' | 'owner'; export type ShellEffect = 'render' | 'quit' | 'none'; -/** Pure navigation owner for the never-signed-in tracer bullet. */ +/** Framework-independent state for the never-signed-in shell tracer. */ export class ShellState { headerIndex = 0; activeSection = 0; focus: FocusRegion = 'header'; mode: ShellMode = 'section'; helpOpen = false; + width = 120; + height = 40; + narrowPane: 'list' | 'detail' = 'list'; + get belowMinimum() { return this.width < 50 || this.height < 20; } get protectedSection() { return this.activeSection < 2; } - get splitPane() { return this.mode === 'section' && !this.protectedSection; } - get activeOwner() { return this.mode === 'owner'; } + get collection() { return this.mode === 'section' && !this.protectedSection; } + get splitPane() { return this.collection && this.width >= 60; } + get ownerActive() { return this.mode === 'owner'; } - key(name: string, options: { ctrl?: boolean; shift?: boolean; belowMinimum?: boolean } = {}): ShellEffect { - if (options.ctrl && (name === 'q' || name === 'c')) return 'quit'; - if (options.belowMinimum) return 'none'; + resize(width: number, height: number) { + const wasSplit = this.splitPane; + this.width = Math.max(1, Math.floor(width)); + this.height = Math.max(1, Math.floor(height)); + if (wasSplit && !this.splitPane && this.collection) this.narrowPane = this.focus === 'detail' ? 'detail' : 'list'; + if (this.belowMinimum) this.helpOpen = false; + } + + key(name: string, options: { ctrl?: boolean; shift?: boolean } = {}): ShellEffect { + if (options.ctrl && name === 'q') return 'quit'; + if (this.belowMinimum) return 'none'; + if (options.ctrl && name === 'c') return 'quit'; if (this.helpOpen) { if (name === 'escape' || name === 'return' || name === '?') { this.helpOpen = false; return 'render'; } return 'none'; } - if (!options.ctrl && name === 'q') return 'quit'; + if (!options.ctrl && !options.shift && name === 'q') return 'quit'; if (!options.ctrl && name === '?') { this.helpOpen = true; return 'render'; } if (name === 'escape') { - if (this.focus !== 'header') { this.focus = 'header'; this.headerIndex = this.mode === 'owner' ? destinations.length : this.activeSection; return 'render'; } + if (this.collection && !this.splitPane && this.narrowPane === 'detail') { + this.narrowPane = 'list'; this.focus = 'list'; return 'render'; + } + if (this.focus !== 'header') { + this.focus = 'header'; + this.headerIndex = this.mode === 'owner' ? destinations.length : this.activeSection; + return 'render'; + } return 'none'; } if (this.focus === 'header') { - if (name === 'left') { const next = Math.max(0, this.headerIndex - 1); if (next !== this.headerIndex) { this.headerIndex = next; return 'render'; } return 'none'; } - if (name === 'right') { const next = Math.min(destinations.length, this.headerIndex + 1); if (next !== this.headerIndex) { this.headerIndex = next; return 'render'; } return 'none'; } - if (name === 'return' || name === 'down') { this.openHeader(); return 'render'; } + if (name === 'left' || name === 'right') { + const delta = name === 'left' ? -1 : 1; + const next = Math.max(0, Math.min(destinations.length, this.headerIndex + delta)); + if (next !== this.headerIndex) { this.headerIndex = next; return 'render'; } + return 'none'; + } + if (name === 'return' || name === 'down') { this.activateHeader(); return 'render'; } + } + if (this.collection && !this.splitPane && this.focus === 'list' && (name === 'right' || name === 'return')) { + this.narrowPane = 'detail'; this.focus = 'detail'; return 'render'; + } + if (this.collection && !this.splitPane && this.focus === 'detail' && name === 'left') { + this.narrowPane = 'list'; this.focus = 'list'; return 'render'; } if (name === 'tab') { - const regions: FocusRegion[] = this.splitPane ? ['header', 'list', 'detail'] : ['header', 'detail']; - const current = regions.indexOf(this.focus); - const delta = options.shift ? regions.length - 1 : 1; - this.focus = regions[(current + delta) % regions.length]!; + const regions: FocusRegion[] = this.collection + ? this.splitPane ? ['header', 'list', 'detail'] : ['header', this.narrowPane] + : ['header', 'detail']; + const current = Math.max(0, regions.indexOf(this.focus)); + this.focus = regions[(current + (options.shift ? regions.length - 1 : 1)) % regions.length]!; return 'render'; } return 'none'; } - openHeader(index = this.headerIndex) { + activateHeader(index = this.headerIndex) { this.headerIndex = Math.max(0, Math.min(destinations.length, index)); if (this.headerIndex === destinations.length) { this.mode = 'owner'; @@ -56,7 +88,13 @@ export class ShellState { } this.mode = 'section'; this.activeSection = this.headerIndex; - // Host and Agents remain gated: activation exposes only an empty safe pane. - this.focus = this.splitPane ? 'list' : 'detail'; + this.narrowPane = 'list'; + this.focus = this.collection ? 'list' : 'detail'; } } + +export function listWidth(width: number) { + if (width >= 90) return Math.min(35, Math.max(28, Math.floor((width - 1) * 0.32))); + if (width >= 60) return Math.min(28, Math.max(24, Math.floor((width - 1) * 0.36))); + return width; +} diff --git a/beehive/test/shell-frame.test.ts b/beehive/test/shell-frame.test.ts deleted file mode 100644 index 58fc021568..0000000000 --- a/beehive/test/shell-frame.test.ts +++ /dev/null @@ -1,43 +0,0 @@ -import test from 'node:test'; -import assert from 'node:assert/strict'; -import { listWidth, renderFrame } from '../src/shell-frame.ts'; -import { ShellState, shortcutGuide } from '../src/shell-state.ts'; - -const plain = (value: string) => value.replace(/\x1b\[[0-9;]*m/g, ''); - -test('wide shell follows frame geometry, label order, and literal empty panes', () => { - const state = new ShellState(); - state.headerIndex = 2; state.openHeader(); - const lines = plain(renderFrame(state, 120, 40)).split('\n'); - assert.equal(lines.length, 40); - assert.match(lines[0]!, /⬢ BEEHIVE.*SIGNED OUT/); - assert.ok(lines[1]!.indexOf('HOST') < lines[1]!.indexOf('AGENTS')); - assert.ok(lines[1]!.indexOf('AGENTS') < lines[1]!.indexOf('HARNESSES')); - assert.ok(lines[1]!.indexOf('HARNESSES') < lines[1]!.indexOf('PROVIDERS')); - assert.equal(lines[2], '─'.repeat(120)); - assert.equal(lines[38], '─'.repeat(120)); - assert.equal(lines[39]!.trim(), shortcutGuide); - assert.equal(listWidth(120), 35); - assert.equal(lines.slice(3, 38).some(line => /[A-Za-z0-9]/.test(line)), false, 'pane body gained placeholder or product copy'); - assert.equal(lines[4]![35], '│'); -}); - -test('minimum 50x20 shell keeps every header destination and an empty one-pane body', () => { - const state = new ShellState(); - state.headerIndex = 3; state.openHeader(); - const lines = plain(renderFrame(state, 50, 20)).split('\n'); - assert.equal(lines.length, 20); - for (const label of ['HOST', 'AGENTS', 'HARNESSES', 'PROVIDERS']) assert.ok(lines[1]!.includes(label)); - assert.ok(lines[0]!.includes('SIGNED OUT')); - assert.equal(lines.slice(3, 18).some(line => /[A-Za-z0-9]/.test(line)), false); - assert.equal(lines[19]!.trim(), '←→ nav ↵ open Tab panes Esc back ? help q quit'); - assert.equal(lines.some(line => line.length !== 50), false, 'frame clipped or overflowed'); -}); - -test('protected destinations and owner state never render section content', () => { - for (const index of [0, 1, 4]) { - const state = new ShellState(); state.headerIndex = index; state.openHeader(); - const body = plain(renderFrame(state, 90, 30)).split('\n').slice(3, 28).join('\n'); - assert.equal(/[A-Za-z0-9]/.test(body), false); - } -}); diff --git a/beehive/test/shell-opentui.test.ts b/beehive/test/shell-opentui.test.ts new file mode 100644 index 0000000000..bb3246ca96 --- /dev/null +++ b/beehive/test/shell-opentui.test.ts @@ -0,0 +1,47 @@ +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { createTestRenderer } from '@opentui/core/testing'; +import { OpenTuiShell, footerGuides, palette } from '../src/opentui-shell.ts'; + +const text = (frame: string) => frame.endsWith('\n') ? frame.slice(0, -1).split('\n') : frame.split('\n'); +const waitForEscape = () => new Promise(resolve => setTimeout(resolve, 50)); + +test('memory renderer draws the exact wide empty shell and focus perimeter', async () => { + const ui = await createTestRenderer({ width: 120, height: 40, exitOnCtrlC: false }); + const shell = new OpenTuiShell(ui.renderer); + try { + await ui.renderOnce(); let frame = ui.captureCharFrame(); + assert.ok(frame.includes('⬢ BEEHIVE')); assert.ok(frame.includes('HOST AGENTS HARNESSES PROVIDERS')); assert.ok(frame.includes('SIGNED OUT')); + assert.ok(frame.includes(footerGuides.wide)); assert.doesNotMatch(frame, /loading|sample|placeholder|sign in to/i); + assert.equal(text(frame).length, 40); + // Host is protected and therefore owns one literally empty full-width body. + assert.equal(text(frame).slice(3, 38).join('').replace(/[│─┌┐└┘ ]/g, ''), ''); + ui.mockInput.pressArrow('right'); await ui.renderOnce(); + assert.equal(shell.state.headerIndex, 1); assert.equal(shell.state.activeSection, 0); + ui.mockInput.pressArrow('right'); ui.mockInput.pressEnter(); await ui.renderOnce(); frame = ui.captureCharFrame(); + assert.equal(shell.state.activeSection, 2); assert.equal(shell.state.focus, 'list'); assert.ok(frame.includes('│')); + assert.equal(text(frame).slice(3, 38).join('').replace(/[│─┌┐└┘ ]/g, ''), ''); + const spans = JSON.stringify(ui.captureSpans()); + assert.ok(spans.includes('"0":255,"1":211,"2":78'), `focus color missing from ${spans.slice(0, 300)}`); + } finally { shell.close(); } +}); + +test('memory renderer preserves the 50x20 narrow shell, help, keyboard, and pointer activation', async () => { + const ui = await createTestRenderer({ width: 50, height: 20, exitOnCtrlC: false }); + const shell = new OpenTuiShell(ui.renderer); + try { + await ui.renderOnce(); let frame = ui.captureCharFrame(); + assert.equal(text(frame).length, 20); assert.ok(frame.includes(footerGuides.compact)); assert.ok(frame.includes('PROVIDERS')); + // Click Harnesses in the centered destination strip. + const navLine = text(frame)[1]!; const x = navLine.indexOf('HARNESSES'); + await ui.mockMouse.click(x, 1); await ui.renderOnce(); + assert.equal(shell.state.activeSection, 2); assert.equal(shell.state.narrowPane, 'list'); + ui.mockInput.pressEnter(); await ui.renderOnce(); assert.equal(shell.state.narrowPane, 'detail'); + ui.mockInput.pressEscape(); await waitForEscape(); await ui.renderOnce(); assert.equal(shell.state.narrowPane, 'list'); + ui.mockInput.pressKey('?'); await ui.renderOnce(); frame = ui.captureCharFrame(); assert.ok(frame.includes('HELP')); assert.ok(frame.includes('Shift-Tab')); + ui.mockInput.pressEscape(); await waitForEscape(); await ui.renderOnce(); assert.ok(!ui.captureCharFrame().includes('Shift-Tab')); + ui.resize(49, 19); await ui.renderOnce(); assert.ok(ui.captureCharFrame().includes(footerGuides.minimum)); + ui.mockInput.pressKey('q'); await ui.renderOnce(); assert.equal(shell.state.belowMinimum, true); + ui.mockInput.pressKey('q', { ctrl: true }); await shell.done; + } finally { shell.close(); } +}); diff --git a/beehive/test/shell-state.test.ts b/beehive/test/shell-state.test.ts index 7285e3336e..d99ae0d252 100644 --- a/beehive/test/shell-state.test.ts +++ b/beehive/test/shell-state.test.ts @@ -1,71 +1,37 @@ -import test from 'node:test'; +import { test } from 'node:test'; import assert from 'node:assert/strict'; -import { destinations, ownerLabel, ShellState, shortcutGuide } from '../src/shell-state.ts'; +import { brand, destinations, listWidth, ownerLabel, ShellState } from '../src/shell-state.ts'; -const press = (state: ShellState, name: string, options = {}) => state.key(name, options); - -test('never-signed-in shell starts on the complete gated header', () => { +// Keep product text and interaction truth at the state owner. +test('never-signed-in shell labels and initial state are exact', () => { const state = new ShellState(); assert.deepEqual(destinations, ['HOST', 'AGENTS', 'HARNESSES', 'PROVIDERS']); - assert.equal(ownerLabel, 'SIGNED OUT'); - assert.equal(state.headerIndex, 0); - assert.equal(state.activeSection, 0); - assert.equal(state.focus, 'header'); - assert.equal(state.protectedSection, true); + assert.equal(brand, '⬢ BEEHIVE'); assert.equal(ownerLabel, 'SIGNED OUT'); + assert.deepEqual({ active: state.activeSection, focus: state.focus, mode: state.mode, help: state.helpOpen }, { active: 0, focus: 'header', mode: 'section', help: false }); }); -test('header arrows clamp, preserve active selection, and Enter opens separately from focus', () => { +test('focus stays distinct from activation and protected/local geometry stays empty-capable', () => { const state = new ShellState(); - assert.equal(press(state, 'left'), 'none'); - press(state, 'right'); - assert.equal(state.headerIndex, 1); - assert.equal(state.activeSection, 0, 'focus must not activate a destination'); - press(state, 'return'); - assert.equal(state.activeSection, 1); - assert.equal(state.focus, 'detail'); - assert.equal(state.splitPane, false, 'owner-scoped destination must not pretend protected content loaded'); - press(state, 'escape'); - assert.equal(state.focus, 'header'); - assert.equal(state.headerIndex, 1); - for (let index = 0; index < 10; index++) press(state, 'right'); - assert.equal(state.headerIndex, destinations.length); - assert.equal(press(state, 'right'), 'none'); + assert.equal(state.key('right'), 'render'); assert.equal(state.headerIndex, 1); assert.equal(state.activeSection, 0); + state.key('right'); state.key('return'); + assert.equal(state.activeSection, 2); assert.equal(state.focus, 'list'); assert.equal(state.splitPane, true); + state.key('tab'); assert.equal(state.focus, 'detail'); + state.key('tab', { shift: true }); assert.equal(state.focus, 'list'); + state.key('escape'); assert.equal(state.focus, 'header'); assert.equal(state.headerIndex, 2); + state.headerIndex = 0; state.activateHeader(); assert.equal(state.protectedSection, true); assert.equal(state.splitPane, false); assert.equal(state.focus, 'detail'); }); -test('host-local sections expose empty list/detail focus regions and Esc returns to initiator', () => { +test('help, q semantics, minimum guard and narrow return are deterministic', () => { const state = new ShellState(); - state.headerIndex = 2; - press(state, 'return'); - assert.equal(state.activeSection, 2); - assert.equal(state.focus, 'list'); - press(state, 'tab'); assert.equal(state.focus, 'detail'); - press(state, 'tab'); assert.equal(state.focus, 'header'); - press(state, 'tab', { shift: true }); assert.equal(state.focus, 'detail'); - press(state, 'escape'); - assert.equal(state.focus, 'header'); - assert.equal(state.headerIndex, 2); + assert.equal(state.key('?'), 'render'); assert.equal(state.helpOpen, true); + assert.equal(state.key('q'), 'none'); assert.equal(state.helpOpen, true); + assert.equal(state.key('escape'), 'render'); assert.equal(state.helpOpen, false); + assert.equal(state.key('q'), 'quit'); + state.resize(49, 20); assert.equal(state.key('q'), 'none'); assert.equal(state.key('c', { ctrl: true }), 'none'); assert.equal(state.key('q', { ctrl: true }), 'quit'); + state.resize(50, 20); state.activateHeader(2); assert.equal(state.splitPane, false); assert.equal(state.narrowPane, 'list'); + state.key('return'); assert.equal(state.narrowPane, 'detail'); state.key('escape'); assert.equal(state.narrowPane, 'list'); assert.equal(state.focus, 'list'); }); -test('owner control opens an empty owner pane and Esc restores its header control', () => { - const state = new ShellState(); - state.headerIndex = destinations.length; - press(state, 'return'); - assert.equal(state.activeOwner, true); - assert.equal(state.focus, 'detail'); - press(state, 'escape'); - assert.equal(state.focus, 'header'); - assert.equal(state.headerIndex, destinations.length); -}); - -test('q-only advertised quit semantics and truthful guide', () => { - const state = new ShellState(); - assert.equal(shortcutGuide, '←→ header Enter open Tab panes Esc return ? help q quit'); - assert.equal(press(state, 'x'), 'none'); - assert.equal(press(state, 'q'), 'quit'); - assert.equal(press(state, 'c', { ctrl: true }), 'quit'); - assert.equal(press(state, 'q', { ctrl: true }), 'quit'); - assert.equal(press(state, 'q', { belowMinimum: true }), 'none'); - assert.equal(press(state, 'q', { ctrl: true, belowMinimum: true }), 'quit'); - assert.equal(press(state, '?'), 'render'); assert.equal(state.helpOpen, true); - assert.equal(press(state, 'escape'), 'render'); assert.equal(state.helpOpen, false); +test('prototype breakpoint formulas are exact', () => { + assert.equal(listWidth(120), 35); assert.equal(listWidth(90), 28); assert.equal(listWidth(72), 25); assert.equal(listWidth(50), 50); });