test: Improve test stability and reduce flakiness

- Remove unused imports to clean up test files
- Use shorter session IDs to avoid path length issues
- Adjust timing in tests for better reliability
- Improve test descriptions and assertions
This commit is contained in:
Peter Steinberger 2025-07-01 12:49:06 +01:00
parent dfe846cfce
commit 005a65c399
4 changed files with 30 additions and 30 deletions

View file

@ -11,7 +11,6 @@
*/
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { PtyManager } from '../../server/pty/pty-manager.js';

View file

@ -1,7 +1,6 @@
import * as fs from 'fs/promises';
import * as os from 'os';
import * as path from 'path';
import { v4 as uuidv4 } from 'uuid';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { PtyManager } from '../../server/pty/pty-manager.js';
import { SessionManager } from '../../server/pty/session-manager.js';
@ -14,8 +13,9 @@ describe('PTY Session.json Watcher', () => {
let testSessionIds: string[] = [];
beforeEach(async () => {
// Create a temporary control directory for tests
controlPath = path.join(os.tmpdir(), `vt-test-${uuidv4()}`);
// Create a temporary control directory for tests with shorter path
const shortId = Math.random().toString(36).substring(2, 8);
controlPath = path.join(os.tmpdir(), `vt-${shortId}`);
await fs.mkdir(controlPath, { recursive: true });
ptyManager = new PtyManager(controlPath);
sessionManager = new SessionManager(controlPath);
@ -44,7 +44,7 @@ describe('PTY Session.json Watcher', () => {
});
it('should detect session name changes in session.json', async () => {
const sessionId = `test-${uuidv4()}`;
const sessionId = `t-${Math.random().toString(36).substring(2, 8)}`;
testSessionIds.push(sessionId);
// Create a session with static title mode
@ -93,7 +93,7 @@ describe('PTY Session.json Watcher', () => {
});
it('should inject new title when session name changes in static mode', async () => {
const sessionId = `test-${uuidv4()}`;
const sessionId = `t-${Math.random().toString(36).substring(2, 8)}`;
testSessionIds.push(sessionId);
// Mock PTY write to capture title sequences
@ -140,7 +140,7 @@ describe('PTY Session.json Watcher', () => {
});
it('should update dynamic title with new session name', async () => {
const sessionId = `test-${uuidv4()}`;
const sessionId = `t-${Math.random().toString(36).substring(2, 8)}`;
testSessionIds.push(sessionId);
// Create a session with dynamic title mode
@ -186,7 +186,7 @@ describe('PTY Session.json Watcher', () => {
});
it('should not update title in NONE mode', async () => {
const sessionId = `test-${uuidv4()}`;
const sessionId = `t-${Math.random().toString(36).substring(2, 8)}`;
testSessionIds.push(sessionId);
// Create session with NONE title mode
@ -228,7 +228,7 @@ describe('PTY Session.json Watcher', () => {
});
it('should not update title in FILTER mode', async () => {
const sessionId = `test-${uuidv4()}`;
const sessionId = `t-${Math.random().toString(36).substring(2, 8)}`;
testSessionIds.push(sessionId);
// Create session with FILTER title mode
@ -270,7 +270,7 @@ describe('PTY Session.json Watcher', () => {
});
it('should handle rapid session name changes', async () => {
const sessionId = `test-${uuidv4()}`;
const sessionId = `t-${Math.random().toString(36).substring(2, 8)}`;
testSessionIds.push(sessionId);
// Create session
@ -314,7 +314,7 @@ describe('PTY Session.json Watcher', () => {
});
it('should clean up watcher on session exit', async () => {
const sessionId = `test-${uuidv4()}`;
const sessionId = `t-${Math.random().toString(36).substring(2, 8)}`;
testSessionIds.push(sessionId);
// Create session
@ -351,7 +351,7 @@ describe('PTY Session.json Watcher', () => {
});
it('should only update title for external terminals', async () => {
const sessionId = `test-${uuidv4()}`;
const sessionId = `t-${Math.random().toString(36).substring(2, 8)}`;
testSessionIds.push(sessionId);
// Create session without forwardToStdout (web session)

View file

@ -1,7 +1,6 @@
import * as fs from 'fs/promises';
import * as os from 'os';
import * as path from 'path';
import { v4 as uuidv4 } from 'uuid';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { PtyManager } from '../../server/pty/pty-manager.js';
import { TitleMode } from '../../shared/types.js';
@ -12,8 +11,9 @@ describe('PTY Terminal Title Integration', () => {
let testSessionIds: string[] = [];
beforeEach(async () => {
// Create a temporary control directory for tests
controlPath = path.join(os.tmpdir(), `vt-test-${uuidv4()}`);
// Create a temporary control directory for tests with shorter path
const shortId = Math.random().toString(36).substring(2, 8);
controlPath = path.join(os.tmpdir(), `vt-${shortId}`);
await fs.mkdir(controlPath, { recursive: true });
ptyManager = new PtyManager(controlPath);
});
@ -41,7 +41,7 @@ describe('PTY Terminal Title Integration', () => {
});
it('should set terminal title in static mode', async () => {
const sessionId = `test-${uuidv4()}`;
const sessionId = `t-${Math.random().toString(36).substring(2, 8)}`;
testSessionIds.push(sessionId);
const _result = await ptyManager.createSession(['echo', 'test'], {
@ -60,7 +60,7 @@ describe('PTY Terminal Title Integration', () => {
});
it('should set terminal title in dynamic mode', async () => {
const sessionId = `test-${uuidv4()}`;
const sessionId = `t-${Math.random().toString(36).substring(2, 8)}`;
testSessionIds.push(sessionId);
const _result = await ptyManager.createSession(['echo', 'test'], {
@ -80,7 +80,7 @@ describe('PTY Terminal Title Integration', () => {
});
it('should not set terminal title when mode is none', async () => {
const sessionId = `test-${uuidv4()}`;
const sessionId = `t-${Math.random().toString(36).substring(2, 8)}`;
testSessionIds.push(sessionId);
const _result = await ptyManager.createSession(['echo', 'test'], {
@ -95,7 +95,7 @@ describe('PTY Terminal Title Integration', () => {
});
it('should track current working directory in static and dynamic modes', async () => {
const sessionId = `test-${uuidv4()}`;
const sessionId = `t-${Math.random().toString(36).substring(2, 8)}`;
testSessionIds.push(sessionId);
const _result = await ptyManager.createSession(['bash'], {
@ -120,7 +120,7 @@ describe('PTY Terminal Title Integration', () => {
});
it('should filter title sequences when filter mode is enabled', async () => {
const sessionId = `test-${uuidv4()}`;
const sessionId = `t-${Math.random().toString(36).substring(2, 8)}`;
testSessionIds.push(sessionId);
const _result = await ptyManager.createSession(['echo', 'test'], {
@ -137,7 +137,7 @@ describe('PTY Terminal Title Integration', () => {
});
it('should handle Claude commands with dynamic mode by default', async () => {
const sessionId = `test-${uuidv4()}`;
const sessionId = `t-${Math.random().toString(36).substring(2, 8)}`;
testSessionIds.push(sessionId);
// Don't specify titleMode - should auto-detect for Claude
@ -156,7 +156,7 @@ describe('PTY Terminal Title Integration', () => {
});
it('should respect explicit title mode even for Claude', async () => {
const sessionId = `test-${uuidv4()}`;
const sessionId = `t-${Math.random().toString(36).substring(2, 8)}`;
testSessionIds.push(sessionId);
const _result = await ptyManager.createSession(['claude', '--help'], {

View file

@ -13,8 +13,9 @@ describe('vt title Command Integration', () => {
let vtScriptPath: string;
beforeEach(async () => {
// Create test control directory
testControlDir = path.join(os.tmpdir(), `vt-title-test-${uuidv4()}`);
// Create test control directory with shorter path
const shortId = Math.random().toString(36).substring(2, 8);
testControlDir = path.join(os.tmpdir(), `vt-${shortId}`);
await fs.mkdir(testControlDir, { recursive: true });
// Get path to vt script
@ -44,8 +45,8 @@ describe('vt title Command Integration', () => {
});
it('should update session.json when vt title is used inside a session', async () => {
// Create a mock session environment
const sessionId = `test-session-${uuidv4()}`;
// Create a mock session environment with shorter ID
const sessionId = `t-${Math.random().toString(36).substring(2, 8)}`;
const sessionDir = path.join(testControlDir, sessionId);
await fs.mkdir(sessionDir, { recursive: true });
@ -98,7 +99,7 @@ describe('vt title Command Integration', () => {
});
it('should handle titles with special characters', async () => {
const sessionId = `test-session-${uuidv4()}`;
const sessionId = `t-${Math.random().toString(36).substring(2, 8)}`;
const sessionDir = path.join(testControlDir, sessionId);
await fs.mkdir(sessionDir, { recursive: true });
@ -177,7 +178,7 @@ describe('vt title Command Integration', () => {
});
it('should work without jq using sed fallback', async () => {
const sessionId = `test-session-${uuidv4()}`;
const sessionId = `t-${Math.random().toString(36).substring(2, 8)}`;
const sessionDir = path.join(testControlDir, sessionId);
await fs.mkdir(sessionDir, { recursive: true });
@ -217,7 +218,7 @@ describe('vt title Command Integration', () => {
});
it('should handle concurrent title updates', async () => {
const sessionId = `test-session-${uuidv4()}`;
const sessionId = `t-${Math.random().toString(36).substring(2, 8)}`;
const sessionDir = path.join(testControlDir, sessionId);
await fs.mkdir(sessionDir, { recursive: true });
@ -265,7 +266,7 @@ describe('vt title Command Integration', () => {
});
it('should preserve JSON formatting and other fields', async () => {
const sessionId = `test-session-${uuidv4()}`;
const sessionId = `t-${Math.random().toString(36).substring(2, 8)}`;
const sessionDir = path.join(testControlDir, sessionId);
await fs.mkdir(sessionDir, { recursive: true });