import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { setTerminalWebglDiagnosticRecorder } from '../../../../shared/terminal-webgl-diagnostics' import type { ManagedPaneInternal } from './pane-manager-types' import { resumePaneRendering, suspendPaneRendering } from './pane-rendering-control' import { collectPaneRenderingDiagnostics } from './pane-rendering-diagnostics' import { schedulePaneRevealPresent } from './pane-reveal-repaint' import { attachWebgl, primeTerminalWebglAddon, resetTerminalWebglSuggestion } from './pane-webgl-renderer' import { rebuildAttachedWebgl } from './pane-webgl-reattach' function createPane(options: { loadAddon?: () => void } = {}): ManagedPaneInternal { const leafId = '11111111-1111-4111-8111-111111111111' as never return { id: 1, leafId, stablePaneId: leafId, terminal: { options: { cursorBlink: true }, cols: 80, rows: 24, refresh: vi.fn(), blur: vi.fn(), loadAddon: vi.fn(options.loadAddon) } as never, container: {} as never, xtermContainer: {} as never, linkTooltip: {} as never, terminalGpuAcceleration: 'on', gpuRenderingEnabled: true, webglAttachmentDeferred: false, webglDisabledAfterContextLoss: false, hasComplexScriptOutput: false, webglAddon: null, imageAddon: null, ligaturesAddon: null, fitResizeObserver: null, pendingObservedFitRafId: null, pendingWebglRefreshRafId: null, fitAddon: { proposeDimensions: vi.fn(() => ({ cols: 80, rows: 23 })), fit: vi.fn() } as never, searchAddon: {} as never, serializeAddon: {} as never, unicode11Addon: {} as never, webLinksAddon: {} as never, compositionHandler: null, pendingSplitScrollState: null, debugLabel: null } } function throwWebglUnavailable(): never { // Mirrors the addon's activate() throw when getContext('webgl2') returns null. throw new Error('WebGL2 not supported null') } function fireContextLoss(pane: ManagedPaneInternal): void { // Use the real context-loss emitter to cover production latch/resume wiring. const addon = pane.webglAddon as unknown as { _onContextLoss: { fire: () => void } } addon._onContextLoss.fire() } describe('terminal WebGL context recovery', () => { beforeEach(async () => { await primeTerminalWebglAddon() resetTerminalWebglSuggestion() vi.spyOn(console, 'warn').mockImplementation(() => {}) vi.stubGlobal('requestAnimationFrame', (callback: FrameRequestCallback) => { callback(16) return 1 }) vi.stubGlobal('cancelAnimationFrame', vi.fn()) }) afterEach(() => { vi.unstubAllGlobals() vi.restoreAllMocks() }) it('backs off after a failed attach instead of retrying on every call', () => { const pane = createPane({ loadAddon: throwWebglUnavailable }) attachWebgl(pane) expect(pane.terminal.loadAddon).toHaveBeenCalledTimes(1) expect(pane.webglAddon).toBeNull() // Title changes reenter attach; while blocked, they must neither allocate addons nor log again. attachWebgl(pane) attachWebgl(pane) expect(pane.terminal.loadAddon).toHaveBeenCalledTimes(1) expect(console.warn).toHaveBeenCalledTimes(1) }) it('retries a backed-off attach on the next rendering resume', () => { const pane = createPane({ loadAddon: throwWebglUnavailable }) attachWebgl(pane) attachWebgl(pane) expect(pane.terminal.loadAddon).toHaveBeenCalledTimes(1) resumePaneRendering([pane]) expect(pane.terminal.loadAddon).toHaveBeenCalledTimes(2) }) it('recovers a context-lost pane on the next rendering resume', () => { const pane = createPane() attachWebgl(pane) expect(pane.webglAddon).not.toBeNull() fireContextLoss(pane) expect(pane.webglDisabledAfterContextLoss).toBe(true) expect(pane.webglAddon).toBeNull() resumePaneRendering([pane]) expect(pane.webglDisabledAfterContextLoss).toBe(false) expect(pane.webglAddon).not.toBeNull() }) it('defers retained replay rebuilds until rendering resumes', () => { const owner = {} const pane = createPane() attachWebgl(pane) const retainedAddon = pane.webglAddon suspendPaneRendering([pane], { owner, livePanes: () => [pane] }) rebuildAttachedWebgl(pane) expect(pane.webglAddon).toBe(retainedAddon) expect(pane.webglRebuildDeferred).toBe(true) expect(pane.terminal.loadAddon).toHaveBeenCalledTimes(1) resumePaneRendering([pane], owner) expect(pane.webglAddon).not.toBe(retainedAddon) expect(pane.webglRebuildDeferred).toBe(false) expect(pane.terminal.loadAddon).toHaveBeenCalledTimes(2) }) it('replaces a retained context lost before xterm dispatches its loss event', () => { const pane = createPane() const dispose = vi.fn() const lostAddon = { dispose, _renderer: { _gl: { getExtension: vi.fn(() => null), isContextLost: vi.fn(() => true) } } } pane.webglAddon = lostAddon as never resumePaneRendering([pane]) expect(dispose).toHaveBeenCalledTimes(1) expect(pane.webglAddon).not.toBe(lostAddon) expect(pane.terminal.loadAddon).toHaveBeenCalledTimes(1) }) it('re-latches when the retried context is lost again', () => { const pane = createPane() attachWebgl(pane) fireContextLoss(pane) resumePaneRendering([pane]) expect(pane.webglAddon).not.toBeNull() fireContextLoss(pane) expect(pane.webglDisabledAfterContextLoss).toBe(true) expect(pane.webglAddon).toBeNull() }) it('stops resume retries after repeated losses in the retry window', () => { const pane = createPane() attachWebgl(pane) fireContextLoss(pane) resumePaneRendering([pane]) fireContextLoss(pane) resumePaneRendering([pane]) fireContextLoss(pane) expect(pane.webglContextLossTimestamps).toHaveLength(3) expect(pane.webglDisabledAfterContextLoss).toBe(true) expect(pane.webglAddon).toBeNull() resumePaneRendering([pane]) expect(pane.webglDisabledAfterContextLoss).toBe(true) expect(pane.webglAddon).toBeNull() expect(pane.terminal.loadAddon).toHaveBeenCalledTimes(3) }) it('stops reveal retries after repeated losses in the retry window', () => { const pane = createPane() attachWebgl(pane) fireContextLoss(pane) resumePaneRendering([pane]) fireContextLoss(pane) resumePaneRendering([pane]) fireContextLoss(pane) schedulePaneRevealPresent(() => [pane]) expect(pane.webglDisabledAfterContextLoss).toBe(true) expect(pane.webglAddon).toBeNull() expect(pane.terminal.loadAddon).toHaveBeenCalledTimes(3) }) // GPU death affects all panes; coalesced crumbs need the same census keys as fit retry to preserve population evidence. it('carries the pane census under the same field names the fit-retry crumb uses', () => { const recorded: { kind: string; detail?: Record }[] = [] setTerminalWebglDiagnosticRecorder((kind, detail) => recorded.push({ kind, detail })) try { const pane = createPane() attachWebgl(pane) fireContextLoss(pane) } finally { setTerminalWebglDiagnosticRecorder(null) } expect(recorded).toEqual([ { kind: 'webgl-context-loss', detail: { paneId: 1, lossesInWindow: 1, livePanes: expect.any(Number), livePaneManagers: expect.any(Number) } } ]) }) it('reports only recent context losses in rendering diagnostics', () => { const now = 1_700_000_000_000 vi.spyOn(Date, 'now').mockReturnValue(now) const pane = createPane() pane.webglContextLossTimestamps = [now - 60_001, now - 1_000] const [diagnostics] = collectPaneRenderingDiagnostics(new Map([[pane.id, pane]])) expect(diagnostics.webglContextLossesInWindow).toBe(1) expect(pane.webglContextLossTimestamps).toEqual([now - 60_001, now - 1_000]) }) })