|
| 1 | +/** |
| 2 | + * Comprehensive test suite for FitAddon |
| 3 | + * |
| 4 | + * Note: Most FitAddon tests require DOM APIs (document, window, getComputedStyle). |
| 5 | + * These tests focus on basic functionality that doesn't require DOM. |
| 6 | + * For full integration tests, see examples/terminal-demo.html |
| 7 | + */ |
| 8 | + |
| 9 | +import { describe, test, expect, beforeEach, afterEach } from 'bun:test'; |
| 10 | +import { FitAddon } from './fit'; |
| 11 | + |
| 12 | +// ============================================================================ |
| 13 | +// Mock Terminal Implementation |
| 14 | +// ============================================================================ |
| 15 | + |
| 16 | +class MockTerminal { |
| 17 | + public element?: HTMLElement; |
| 18 | + public cols = 80; |
| 19 | + public rows = 24; |
| 20 | + public renderer = { |
| 21 | + getMetrics: () => ({ width: 9, height: 16, baseline: 12 }) |
| 22 | + }; |
| 23 | + |
| 24 | + public resize(cols: number, rows: number): void { |
| 25 | + this.cols = cols; |
| 26 | + this.rows = rows; |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +// ============================================================================ |
| 31 | +// Test Suite |
| 32 | +// ============================================================================ |
| 33 | + |
| 34 | +describe('FitAddon', () => { |
| 35 | + let addon: FitAddon; |
| 36 | + let terminal: MockTerminal; |
| 37 | + |
| 38 | + beforeEach(() => { |
| 39 | + addon = new FitAddon(); |
| 40 | + terminal = new MockTerminal(); |
| 41 | + }); |
| 42 | + |
| 43 | + afterEach(() => { |
| 44 | + addon.dispose(); |
| 45 | + }); |
| 46 | + |
| 47 | + // ========================================================================== |
| 48 | + // Activation & Disposal Tests |
| 49 | + // ========================================================================== |
| 50 | + |
| 51 | + test('activates successfully', () => { |
| 52 | + expect(() => addon.activate(terminal as any)).not.toThrow(); |
| 53 | + }); |
| 54 | + |
| 55 | + test('disposes successfully', () => { |
| 56 | + addon.activate(terminal as any); |
| 57 | + expect(() => addon.dispose()).not.toThrow(); |
| 58 | + }); |
| 59 | + |
| 60 | + test('can activate and dispose multiple times', () => { |
| 61 | + addon.activate(terminal as any); |
| 62 | + addon.dispose(); |
| 63 | + addon.activate(terminal as any); |
| 64 | + addon.dispose(); |
| 65 | + }); |
| 66 | + |
| 67 | + // ========================================================================== |
| 68 | + // proposeDimensions() Tests |
| 69 | + // ========================================================================== |
| 70 | + |
| 71 | + test('proposeDimensions returns undefined without element', () => { |
| 72 | + addon.activate(terminal as any); |
| 73 | + const dims = addon.proposeDimensions(); |
| 74 | + expect(dims).toBeUndefined(); |
| 75 | + }); |
| 76 | + |
| 77 | + test('proposeDimensions returns undefined without renderer', () => { |
| 78 | + // Remove renderer |
| 79 | + (terminal as any).renderer = undefined; |
| 80 | + terminal.element = {} as HTMLElement; |
| 81 | + addon.activate(terminal as any); |
| 82 | + |
| 83 | + const dims = addon.proposeDimensions(); |
| 84 | + expect(dims).toBeUndefined(); |
| 85 | + }); |
| 86 | + |
| 87 | + // ========================================================================== |
| 88 | + // fit() Tests |
| 89 | + // ========================================================================== |
| 90 | + |
| 91 | + test('fit() does nothing without element', () => { |
| 92 | + addon.activate(terminal as any); |
| 93 | + const originalCols = terminal.cols; |
| 94 | + const originalRows = terminal.rows; |
| 95 | + |
| 96 | + addon.fit(); |
| 97 | + |
| 98 | + expect(terminal.cols).toBe(originalCols); |
| 99 | + expect(terminal.rows).toBe(originalRows); |
| 100 | + }); |
| 101 | + |
| 102 | + // ========================================================================== |
| 103 | + // observeResize() Tests |
| 104 | + // ========================================================================== |
| 105 | + |
| 106 | + test('observeResize() does not throw without element', () => { |
| 107 | + addon.activate(terminal as any); |
| 108 | + expect(() => addon.observeResize()).not.toThrow(); |
| 109 | + }); |
| 110 | + |
| 111 | + // ========================================================================== |
| 112 | + // Integration Tests |
| 113 | + // ========================================================================== |
| 114 | + |
| 115 | + test('full workflow: activate → fit → observeResize → dispose', () => { |
| 116 | + // Activate |
| 117 | + addon.activate(terminal as any); |
| 118 | + |
| 119 | + // Initial fit (no-op without element) |
| 120 | + addon.fit(); |
| 121 | + |
| 122 | + // Setup auto-resize (no-op without element) |
| 123 | + addon.observeResize(); |
| 124 | + |
| 125 | + // Dispose |
| 126 | + addon.dispose(); |
| 127 | + }); |
| 128 | + |
| 129 | + test('fit() after dispose does nothing', () => { |
| 130 | + addon.activate(terminal as any); |
| 131 | + addon.dispose(); |
| 132 | + |
| 133 | + const originalCols = terminal.cols; |
| 134 | + const originalRows = terminal.rows; |
| 135 | + |
| 136 | + addon.fit(); |
| 137 | + |
| 138 | + expect(terminal.cols).toBe(originalCols); |
| 139 | + expect(terminal.rows).toBe(originalRows); |
| 140 | + }); |
| 141 | + |
| 142 | + test('fit() prevents feedback loops by tracking dimensions', () => { |
| 143 | + addon.activate(terminal as any); |
| 144 | + |
| 145 | + // Track how many times resize is called |
| 146 | + let resizeCallCount = 0; |
| 147 | + const originalResize = terminal.resize.bind(terminal); |
| 148 | + terminal.resize = (cols: number, rows: number) => { |
| 149 | + resizeCallCount++; |
| 150 | + originalResize(cols, rows); |
| 151 | + }; |
| 152 | + |
| 153 | + // First fit() should call resize |
| 154 | + addon.fit(); |
| 155 | + expect(resizeCallCount).toBe(0); // No element, so no resize |
| 156 | + |
| 157 | + // Calling fit() multiple times without dimension change should not resize again |
| 158 | + addon.fit(); |
| 159 | + addon.fit(); |
| 160 | + addon.fit(); |
| 161 | + expect(resizeCallCount).toBe(0); // Still 0 because no element |
| 162 | + }); |
| 163 | +}); |
0 commit comments