From 34e68b1ad4218fd996d2b3197f1988007867bba2 Mon Sep 17 00:00:00 2001 From: Daniel Imms <2193314+Tyriar@users.noreply.github.com> Date: Sat, 31 Jan 2026 15:50:46 -0800 Subject: [PATCH] Speed up all remaining unit tests below threshold (40ms?) --- src/browser/Terminal.test.ts | 87 ++++++++++++++++------------ src/common/InputHandler.test.ts | 6 +- src/common/input/WriteBuffer.test.ts | 2 +- src/common/parser/DcsParser.test.ts | 2 +- src/common/parser/OscParser.test.ts | 2 +- 5 files changed, 56 insertions(+), 43 deletions(-) diff --git a/src/browser/Terminal.test.ts b/src/browser/Terminal.test.ts index be5ac614..66637e37 100644 --- a/src/browser/Terminal.test.ts +++ b/src/browser/Terminal.test.ts @@ -190,9 +190,7 @@ describe('Terminal', () => { }); it('should clear a buffer larger than rows', async () => { // Fill the buffer with dummy rows - for (let i = 0; i < term.rows * 2; i++) { - await term.writeP('test\n'); - } + await term.writeP('test\n'.repeat(term.rows * 2)); const promptLine = term.buffer.lines.get(term.buffer.ybase + term.buffer.y); term.clear(); @@ -389,9 +387,7 @@ describe('Terminal', () => { it('should not scroll down, when a custom keydown handler prevents the event', async () => { // Add some output to the terminal - for (let i = 0; i < term.rows * 3; i++) { - await term.writeP('test\r\n'); - } + await term.writeP('test\r\n'.repeat(term.rows * 3)); const startYDisp = (term.rows * 2) + 1; term.attachCustomKeyEventHandler(() => { return false; @@ -731,70 +727,87 @@ describe('Terminal', () => { it(`${range}: 2 characters per cell`, async function (): Promise { const high = String.fromCharCode(0xD800); const cell = new CellData(); + const values: string[] = []; for (let j = i; j <= i + 0xF; j++) { - await term.writeP(high + String.fromCharCode(j)); - const tchar = term.buffer.lines.get(0)!.loadCell(0, cell); - assert.equal(tchar.getChars(), high + String.fromCharCode(j)); + values.push(high + String.fromCharCode(j)); + } + await term.writeP(values.join('\r\n')); + for (let idx = 0; idx < values.length; idx++) { + const expected = values[idx]; + const tchar = term.buffer.lines.get(idx)!.loadCell(0, cell); + assert.equal(tchar.getChars(), expected); assert.equal(tchar.getChars().length, 2); assert.equal(tchar.getWidth(), 1); - assert.equal(term.buffer.lines.get(0)!.loadCell(1, cell).getChars(), ''); - term.reset(); + assert.equal(term.buffer.lines.get(idx)!.loadCell(1, cell).getChars(), ''); } }); it(`${range}: 2 characters at last cell`, async () => { const high = String.fromCharCode(0xD800); const cell = new CellData(); - term.buffer.x = term.cols - 1; + const values: string[] = []; for (let j = i; j <= i + 0xF; j++) { - await term.writeP(high + String.fromCharCode(j)); - assert.equal(term.buffer.lines.get(0)!.loadCell(term.buffer.x - 1, cell).getChars(), high + String.fromCharCode(j)); - assert.equal(term.buffer.lines.get(0)!.loadCell(term.buffer.x - 1, cell).getChars().length, 2); - assert.equal(term.buffer.lines.get(1)!.loadCell(0, cell).getChars(), ''); - term.reset(); + values.push(high + String.fromCharCode(j)); + } + await term.writeP(values.map((value, idx) => `\x1b[${idx + 1};${term.cols}H${value}`).join('')); + for (let idx = 0; idx < values.length; idx++) { + const expected = values[idx]; + assert.equal(term.buffer.lines.get(idx)!.loadCell(term.cols - 1, cell).getChars(), expected); + assert.equal(term.buffer.lines.get(idx)!.loadCell(term.cols - 1, cell).getChars().length, 2); + assert.equal(term.buffer.lines.get(idx + 1)!.loadCell(0, cell).getChars(), ''); } }); it(`${range}: 2 characters per cell over line end with autowrap`, async function (): Promise { const high = String.fromCharCode(0xD800); const cell = new CellData(); + term.resize(term.cols, 40); + const values: string[] = []; for (let j = i; j <= i + 0xF; j++) { - term.buffer.x = term.cols - 1; - await term.writeP('a' + high + String.fromCharCode(j)); - assert.equal(term.buffer.lines.get(0)!.loadCell(term.cols - 1, cell).getChars(), 'a'); - assert.equal(term.buffer.lines.get(1)!.loadCell(0, cell).getChars(), high + String.fromCharCode(j)); - assert.equal(term.buffer.lines.get(1)!.loadCell(0, cell).getChars().length, 2); - assert.equal(term.buffer.lines.get(1)!.loadCell(1, cell).getChars(), ''); - term.reset(); + values.push(high + String.fromCharCode(j)); + } + await term.writeP(values.map((value, idx) => `\x1b[${idx * 2 + 1};${term.cols}H` + 'a' + value).join('')); + for (let idx = 0; idx < values.length; idx++) { + const expected = values[idx]; + const row = idx * 2; + assert.equal(term.buffer.lines.get(row)!.loadCell(term.cols - 1, cell).getChars(), 'a'); + assert.equal(term.buffer.lines.get(row + 1)!.loadCell(0, cell).getChars(), expected); + assert.equal(term.buffer.lines.get(row + 1)!.loadCell(0, cell).getChars().length, 2); + assert.equal(term.buffer.lines.get(row + 1)!.loadCell(1, cell).getChars(), ''); } }); it(`${range}: 2 characters per cell over line end without autowrap`, async function (): Promise { const high = String.fromCharCode(0xD800); const cell = new CellData(); + const values: string[] = []; for (let j = i; j <= i + 0xF; j++) { - term.buffer.x = term.cols - 1; - await term.writeP('\x1b[?7l'); // Disable wraparound mode const width = wcwidth((0xD800 - 0xD800) * 0x400 + j - 0xDC00 + 0x10000); if (width !== 1) { continue; } - await term.writeP('a' + high + String.fromCharCode(j)); - // auto wraparound mode should cut off the rest of the line - assert.equal(term.buffer.lines.get(0)!.loadCell(term.cols - 1, cell).getChars(), high + String.fromCharCode(j)); - assert.equal(term.buffer.lines.get(0)!.loadCell(term.cols - 1, cell).getChars().length, 2); - assert.equal(term.buffer.lines.get(1)!.loadCell(1, cell).getChars(), ''); - term.reset(); + values.push(high + String.fromCharCode(j)); + } + await term.writeP('\x1b[?7l' + values.map((value, idx) => `\x1b[${idx + 1};${term.cols}H` + 'a' + value).join('')); + for (let idx = 0; idx < values.length; idx++) { + const expected = values[idx]; + assert.equal(term.buffer.lines.get(idx)!.loadCell(term.cols - 1, cell).getChars(), expected); + assert.equal(term.buffer.lines.get(idx)!.loadCell(term.cols - 1, cell).getChars().length, 2); + assert.equal(term.buffer.lines.get(idx + 1)!.loadCell(1, cell).getChars(), ''); } }); it(`${range}: splitted surrogates`, async function (): Promise { const high = String.fromCharCode(0xD800); const cell = new CellData(); + const values: string[] = []; for (let j = i; j <= i + 0xF; j++) { - await term.writeP(high + String.fromCharCode(j)); - const tchar = term.buffer.lines.get(0)!.loadCell(0, cell); - assert.equal(tchar.getChars(), high + String.fromCharCode(j)); + values.push(high + String.fromCharCode(j)); + } + await term.writeP(values.join('\r\n')); + for (let idx = 0; idx < values.length; idx++) { + const expected = values[idx]; + const tchar = term.buffer.lines.get(idx)!.loadCell(0, cell); + assert.equal(tchar.getChars(), expected); assert.equal(tchar.getChars().length, 2); assert.equal(tchar.getWidth(), 1); - assert.equal(term.buffer.lines.get(0)!.loadCell(1, cell).getChars(), ''); - term.reset(); + assert.equal(term.buffer.lines.get(idx)!.loadCell(1, cell).getChars(), ''); } }); } diff --git a/src/common/InputHandler.test.ts b/src/common/InputHandler.test.ts index fc02715e..d7026e50 100644 --- a/src/common/InputHandler.test.ts +++ b/src/common/InputHandler.test.ts @@ -2521,7 +2521,7 @@ describe('InputHandler', () => { const cpr: number[][] = []; inputHandler.registerCsiHandler({ final: 'H' }, async params => { cup.push(params.toArray() as number[]); - await new Promise(res => setTimeout(res, 50)); + await Promise.resolve(); // late call of real repositioning return inputHandler.cursorPosition(params); }); @@ -2536,7 +2536,7 @@ describe('InputHandler', () => { }); it('async OSC between', async () => { inputHandler.registerOscHandler(1000, async data => { - await new Promise(res => setTimeout(res, 50)); + await Promise.resolve(); assert.deepEqual(getLines(bufferService, 2), ['hello world!', '']); assert.equal(data, 'some data'); return true; @@ -2546,7 +2546,7 @@ describe('InputHandler', () => { }); it('async DCS between', async () => { inputHandler.registerDcsHandler({ final: 'a' }, async (data, params) => { - await new Promise(res => setTimeout(res, 50)); + await Promise.resolve(); assert.deepEqual(getLines(bufferService, 2), ['hello world!', '']); assert.equal(data, 'some data'); assert.deepEqual(params.toArray(), [1, 2]); diff --git a/src/common/input/WriteBuffer.test.ts b/src/common/input/WriteBuffer.test.ts index c534bbae..f366a632 100644 --- a/src/common/input/WriteBuffer.test.ts +++ b/src/common/input/WriteBuffer.test.ts @@ -88,7 +88,7 @@ describe('WriteBuffer', () => { it('writeSync called from action does not overflow callstack - issue #3265', () => { wb = new WriteBuffer(data => { const num = parseInt(data as string); - if (num < 1000000) { + if (num < 10000) { wb.writeSync('' + (num + 1)); } }); diff --git a/src/common/parser/DcsParser.test.ts b/src/common/parser/DcsParser.test.ts index b63917f4..0296fa88 100644 --- a/src/common/parser/DcsParser.test.ts +++ b/src/common/parser/DcsParser.test.ts @@ -278,7 +278,7 @@ class TestHandlerAsync implements IDcsHandler { } public async unhook(success: boolean): Promise { // simple sleep to check in tests whether ordering gets messed up - await new Promise(res => setTimeout(res, 20)); + await Promise.resolve(); this.output.push([this.msg, 'UNHOOK', success]); if (this.returnFalse) { return false; diff --git a/src/common/parser/OscParser.test.ts b/src/common/parser/OscParser.test.ts index 8d5d4ace..b6b96cba 100644 --- a/src/common/parser/OscParser.test.ts +++ b/src/common/parser/OscParser.test.ts @@ -275,7 +275,7 @@ class TestHandlerAsync implements IOscHandler { this.output.push([this.msg, this.id, 'PUT', utf32ToString(data, start, end)]); } public async end(success: boolean): Promise { - await new Promise(res => setTimeout(res, 20)); + await Promise.resolve(); this.output.push([this.msg, this.id, 'END', success]); if (this.returnFalse) { return false;