diff --git a/src/client/builtins/tabs.tsx b/src/client/builtins/tabs.tsx index e9f25bbe..179c3edc 100644 --- a/src/client/builtins/tabs.tsx +++ b/src/client/builtins/tabs.tsx @@ -269,7 +269,17 @@ export function builtinTabs(ctx: Context): readonly TabDescriptor[] { // reattach), so closing the TAB is the only thing that ends the process // — without it every closed bottom terminal would hold one of the host's // per-session terminal slots forever. - onClose: (tab, scope) => { closeBottomTerminal(ctx, tab, scope?.sessionId ?? '') }, + onClose: (tab, scope) => { + // Every close path resolves a scope (the type makes it required), so + // this guard should be unreachable. It returns instead of substituting + // `''` for a missing one because an empty session id is the ONE option + // that is worse than doing nothing: the host would look the terminal up + // in a session that does not exist, no-op, and leave the shell running + // — invisibly holding a per-session terminal slot for the page's life. + const sessionId: string | undefined = scope?.sessionId + if (sessionId === undefined || sessionId === '') return + closeBottomTerminal(ctx, tab, sessionId) + }, }, { id: 'diff', diff --git a/tests/native-registration.spec.ts b/tests/native-registration.spec.ts index 3996b697..64262fcc 100644 --- a/tests/native-registration.spec.ts +++ b/tests/native-registration.spec.ts @@ -41,12 +41,16 @@ import { createSidebarStore } from '../src/client/state.ts' function createRegistry() { const registered = new Map void>() const events: string[] = [] + /** Every definition the plugin handed the host, guide rows included. */ + const definitions: Array<{ id: string; kind: string; guide?: Array<{ id?: unknown }> }> = [] let failReleaseOf: string | undefined return { registered, events, + definitions, failRelease(id: string): void { failReleaseOf = id }, - register(definition: { id: string }): () => void { + register(definition: { id: string; kind: string; guide?: Array<{ id?: unknown }> }): () => void { + definitions.push(definition) if (registered.has(definition.id)) { throw new Error(`sidebarRight: tab type id "${definition.id}" is already registered`) } @@ -239,3 +243,55 @@ describe('native tab registration lifecycle', () => { } }) }) + +/** + * `bottomOnly` (#774): the type lives in the plugin's own workbench only. + * + * The mount lane pins the OUTCOME (the host's guide still lists exactly one + * `terminal` capsule), but until this case existed the MECHANISM had no unit + * guard at all: the #774 verification measured 14 related specs / 158 + * assertions staying green with the `bottomOnly` skip in `sync()` deleted, so + * only a full deployment could catch it. This is the unit half, written on what + * actually reached the host rather than on what the plugin intended to send. + */ +describe('a bottomOnly descriptor never reaches the native surface', () => { + it('registers no tab type, no slot and no guide row for it', () => { + const host = createHost() + const { store, service, records } = createPlugins() + // The sixth built-in (#774), declared through the same public API an + // external plugin uses. + service.registerTab({ + id: 'terminal-bottom', + title: () => 'Terminal', + description: () => 'Bottom shell', + bottomOnly: true, + single: true, + component: () => null, + }) + const dispose = registerNativeSurface({ ctx: host.ctx as never, store, service, records }) + + // Positive control FIRST: the other descriptors did reach the host, and the + // visible one did contribute its guide row — so the absences below cannot + // be satisfied by a surface that registers nothing at all. (The `files` + // takeover follows the descriptor loop, hence its place at the end.) + expect(host.registry.events).toEqual([ + 'register dsh-better-sidebar:editor', + 'register dsh-better-sidebar:git', + 'register dsh-better-sidebar:files', + ]) + const guideIds = host.registry.definitions + .flatMap(definition => (definition.guide ?? []).map(entry => entry.id)) + expect(guideIds.sort()).toEqual(['files', 'git']) + + // The absence rides the EVENT LOG, not the live key set: a type registered + // and then orphaned leaves a key set that looks exactly like this one. + expect(host.registry.registered.has('dsh-better-sidebar:terminal-bottom')).toBe(false) + expect(host.registry.events.filter(event => event.includes('terminal-bottom'))).toEqual([]) + expect(host.slotEvents.filter(event => event.includes('terminal-bottom'))).toEqual([]) + + // A notification re-runs `sync()`; the skip must survive it. + store.setPrefs(store.getPrefs()) + expect(host.registry.events.filter(event => event.includes('terminal-bottom'))).toEqual([]) + dispose() + }) +}) diff --git a/tests/terminal-tab.spec.tsx b/tests/terminal-tab.spec.tsx index 2b2832dd..8d27b0aa 100644 --- a/tests/terminal-tab.spec.tsx +++ b/tests/terminal-tab.spec.tsx @@ -31,7 +31,7 @@ import { renderRoot, setupReactAct } from './test-utils.ts' import { t } from '../src/client/locales.ts' import type { Context } from '../src/context-types.ts' import type { SidebarTab } from '../src/client/state.ts' -import { createSidebarStore } from '../src/client/state.ts' +import { allLeaves, createSidebarStore, openTabInBottomPane } from '../src/client/state.ts' import { createBetterSidebarService, type TabDescriptor } from '../src/client/service.ts' import { builtinTabs } from '../src/client/builtins/tabs.tsx' import { @@ -477,20 +477,38 @@ describe('terminal-bottom: degradation and recovery', () => { unmount() }) - it('an ended terminal offers the same new-terminal action, and says so once', async () => { + it('an ended terminal owns the only status line, and offers the new-terminal action', async () => { const terminals = new FakeTerminals() const { ctx } = viewSetup(terminals) const { container, unmount } = render(ctx, bottomTab()) await flush() const view = terminals.views.get('s1\u0000' + bottomTerminalKey(0))! act(() => { - view.patch({ info: { id: view.id, title: 'Terminal', cwd: '/w', cols: 80, rows: 24, state: 'exited', exitCode: 0 } }) + view.patch({ + info: { id: view.id, title: 'Terminal', cwd: '/w', cols: 80, rows: 24, state: 'exited', exitCode: 0 }, + // The phase is what makes this case DISCRIMINATING. A patch that only + // flipped `info.state` to `exited` would leave the pending-phase status + // line undefined either way (`statusOf` has no line for `connected`), + // so the assertion below ("the exited sentence appears once") held with + // and without the gate in `TerminalBottomView`. `disconnected` HAS a + // line of its own, and the gate is exactly what must stand it — with + // its Retry button — down: a process that is gone cannot be reconnected + // to, and the reader would get the same terminal described twice. + phase: 'disconnected', + writable: false, + }) }) - expect(container.querySelector('button')).not.toBeNull() - // The ended panel carries its own line: the pending-phase status line must - // stand down, or the reader sees the same sentence twice. + // The ended panel carries its own line, exactly once… const line = t('exited') expect(container.textContent!.split(line).length - 1).toBe(1) + // …it is the ONLY status line on screen (the suppressed one was a + // `role="status"` div as well)… + expect(container.querySelectorAll('[role="status"]')).toHaveLength(1) + // …and the only action is the new terminal: the suppressed line's Retry + // button would come FIRST in DOM order. + expect([...container.querySelectorAll('button')].map(button => button.textContent)) + .toEqual([t('terminalNew')]) + expect(container.textContent).not.toContain(t('disconnected')) unmount() }) @@ -507,6 +525,59 @@ describe('terminal-bottom: degradation and recovery', () => { expect(view.refreshed).toBe(0) unmount() }) + + it('the new-terminal action rides the REAL updateTab route (native record first, own layout second)', async () => { + // Every other case here stubs `betterSidebar.updateTab`, which is only the + // call the view MAKES: the write itself travels + // `service.updateTab` → `surface.update` → `store.reduce(patchTab)`, and + // that route was uncovered. It matters most for this type: a `bottomOnly` + // tab never has a native record, so the store reduce is the only path its + // "new terminal" can take — with a stub, a broken route would still look + // green here and fail only in the browser. + const terminals = new FakeTerminals() + const store = createSidebarStore() + store.setSession('s1') + const service = createBetterSidebarService(store) + const ctx = ctxWith({ webTerminals: terminals, betterSidebar: service }) + // The workbench's own state: the tab IS open. `patchTab` is a documented + // no-op for a missing id, so a case that skipped this would assert nothing. + store.reduce(state => openTabInBottomPane(state, bottomTab())) + const tabInStore = (): SidebarTab | undefined => allLeaves(store.getSnapshot().state!.bottomSplits) + .flatMap(leaf => leaf.tabs).find(tab => tab.id === 'terminal-bottom') + expect(tabInStore()?.meta).toBeUndefined() + + const { container, unmount } = render(ctx, bottomTab()) + await flush() + const view = terminals.views.get('s1\u0000' + bottomTerminalKey(0))! + act(() => { view.patch({ phase: 'failed', writable: false, issue: 'missingTerminal' }) }) + const action = container.querySelector('button')! + + // ── the native half: an id the native surface owns is patched THERE ──── + const nativePatches: Array<{ tabId: string; patch: unknown; sessionId: string | undefined }> = [] + service.setSurface({ + openTab: () => {}, + openResource: () => {}, + fileAddress: () => 'dsh-resource://file/s1/x', + close: () => undefined, + update: (tabId, patch, sessionId) => { + nativePatches.push({ tabId, patch, sessionId }) + return true + }, + activate: () => false, + has: () => false, + }) + act(() => { action.dispatchEvent(new MouseEvent('click', { bubbles: true })) }) + expect(nativePatches).toEqual([ + { tabId: 'terminal-bottom', patch: { meta: { terminalRun: 1 } }, sessionId: 's1' }, + ]) + expect(tabInStore()?.meta, 'a native record owns the patch — the store stays out of it').toBeUndefined() + + // ── the own-layout half: no native record, so the bottom tree takes it ── + service.setSurface(undefined) + act(() => { action.dispatchEvent(new MouseEvent('click', { bubbles: true })) }) + expect(tabInStore()?.meta).toEqual({ terminalRun: 1 }) + unmount() + }) }) describe('terminal-bottom: identity helpers', () => {