-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-helper-fixed.mjs
More file actions
144 lines (130 loc) · 4.41 KB
/
test-helper-fixed.mjs
File metadata and controls
144 lines (130 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* Test Helper for command-stream tests
*
* IMPORTANT: Due to Bun's test runner limitations, beforeEach/afterEach hooks
* MUST be within describe() blocks to work properly.
*
* Usage:
* ```js
* import { setupTestHooks } from './test-helper-fixed.mjs';
* import { describe } from 'bun:test';
*
* describe('Your test suite', () => {
* setupTestHooks();
*
* // Your tests here
* });
* ```
*/
import { beforeEach, afterEach } from 'bun:test';
import { resetGlobalState } from '../src/$.mjs';
import { existsSync } from 'fs';
// Save the original working directory when tests start
const originalCwd = process.cwd();
// Trace function for debugging
function trace(message) {
if (process.env.DEBUG || process.env.TRACE) {
const timestamp = new Date().toISOString();
console.error(`[TRACE ${timestamp}] [test-helper] ${message}`);
}
}
trace(`Original working directory: ${originalCwd}`);
/**
* Sets up beforeEach and afterEach hooks to restore working directory
* and reset global state between tests.
*
* MUST be called inside a describe() block!
*/
export function setupTestHooks() {
beforeEach(async () => {
trace('beforeEach hook running');
// CRITICAL: Restore working directory first - MUST succeed for spawn to work
const currentDir = process.cwd();
if (currentDir !== originalCwd) {
trace(`beforeEach: Restoring cwd from ${currentDir} to ${originalCwd}`);
try {
// Force restoration regardless of current state
process.chdir(originalCwd);
} catch (e) {
// Original directory might be gone, try fallbacks
try {
if (existsSync(originalCwd)) {
process.chdir(originalCwd);
} else if (existsSync('/workspace/command-stream')) {
process.chdir('/workspace/command-stream');
} else if (process.env.HOME && existsSync(process.env.HOME)) {
process.chdir(process.env.HOME);
} else {
process.chdir('/');
}
} catch (e2) {
console.error('[test-helper] FATAL: Cannot set working directory in beforeEach');
trace('FATAL: Cannot set working directory in beforeEach');
}
}
}
// Call the comprehensive reset
resetGlobalState();
// Extra safety: ensure we're in a valid directory after reset
try {
process.cwd(); // This will throw if we're in a bad state
} catch (e) {
// Force to a known good directory
process.chdir(originalCwd);
}
// Give a tiny bit of time for any async cleanup to complete
await new Promise(resolve => setTimeout(resolve, 1));
trace('beforeEach hook completed');
});
afterEach(async () => {
trace('afterEach hook running');
// CRITICAL: Clean up and restore state after each test
const currentDir = process.cwd();
if (currentDir !== originalCwd) {
trace(`afterEach: Restoring cwd from ${currentDir} to ${originalCwd}`);
try {
// Force restoration regardless of current state
process.chdir(originalCwd);
} catch (e) {
// Original directory might be gone, try fallbacks
try {
if (existsSync(originalCwd)) {
process.chdir(originalCwd);
} else if (existsSync('/workspace/command-stream')) {
process.chdir('/workspace/command-stream');
} else if (process.env.HOME && existsSync(process.env.HOME)) {
process.chdir(process.env.HOME);
} else {
process.chdir('/');
}
} catch (e2) {
console.error('[test-helper] FATAL: Cannot set working directory in afterEach');
trace('FATAL: Cannot set working directory in afterEach');
}
}
}
// Call the comprehensive reset
resetGlobalState();
// Extra safety: ensure we're in a valid directory after reset
try {
process.cwd(); // This will throw if we're in a bad state
} catch (e) {
// Force to a known good directory
process.chdir(originalCwd);
}
// Give a tiny bit of time for any async cleanup to complete
await new Promise(resolve => setTimeout(resolve, 1));
trace('afterEach hook completed');
});
}
// Install a process exit handler to ensure cleanup even on crash
process.on('beforeExit', () => {
try {
if (process.cwd() !== originalCwd) {
process.chdir(originalCwd);
}
} catch (e) {
// Ignore
}
});
export { resetGlobalState };