|
| 1 | +/** |
| 2 | + * Tests for dev-watch.js hot reload functionality |
| 3 | + */ |
| 4 | + |
| 5 | +import { describe, test, expect, beforeAll, afterAll } from 'bun:test'; |
| 6 | +import { writeFile, readFile, mkdir, unlink, rm } from 'node:fs/promises'; |
| 7 | +import { existsSync, readFileSync } from 'node:fs'; |
| 8 | +import { tmpdir } from 'node:os'; |
| 9 | +import path from 'path'; |
| 10 | + |
| 11 | +const PROJECT_ROOT = path.resolve(__dirname, '../..'); |
| 12 | +const DEV_SCRIPT = path.resolve(PROJECT_ROOT, 'scripts/dev-watch.js'); |
| 13 | +const TEST_DIR = path.join(tmpdir(), `eliza-dev-watch-test-${Date.now()}`); |
| 14 | +const TEST_FILE = path.join(TEST_DIR, '__test-hot-reload__.ts'); |
| 15 | + |
| 16 | +describe('Hot Reload Functionality', () => { |
| 17 | + let testFileCreated = false; |
| 18 | + |
| 19 | + beforeAll(async () => { |
| 20 | + // Create temp directory for tests |
| 21 | + await mkdir(TEST_DIR, { recursive: true }); |
| 22 | + }); |
| 23 | + |
| 24 | + // Helper to wait for a condition |
| 25 | + async function waitFor( |
| 26 | + condition: () => boolean, |
| 27 | + timeout = 30000, |
| 28 | + interval = 100 |
| 29 | + ): Promise<boolean> { |
| 30 | + const start = Date.now(); |
| 31 | + while (!condition()) { |
| 32 | + if (Date.now() - start > timeout) { |
| 33 | + return false; |
| 34 | + } |
| 35 | + await new Promise((resolve) => setTimeout(resolve, interval)); |
| 36 | + } |
| 37 | + return true; |
| 38 | + } |
| 39 | + |
| 40 | + // Helper to create or modify a test file |
| 41 | + async function modifyTestFile(content: string) { |
| 42 | + await writeFile(TEST_FILE, content); |
| 43 | + testFileCreated = true; |
| 44 | + } |
| 45 | + |
| 46 | + // Helper to clean up test file |
| 47 | + async function cleanupTestFile() { |
| 48 | + if (testFileCreated && existsSync(TEST_FILE)) { |
| 49 | + await unlink(TEST_FILE).catch(() => { |
| 50 | + /* ignore */ |
| 51 | + }); |
| 52 | + testFileCreated = false; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + afterAll(async () => { |
| 57 | + // Clean up test file |
| 58 | + await cleanupTestFile(); |
| 59 | + |
| 60 | + // Clean up test directory |
| 61 | + try { |
| 62 | + await rm(TEST_DIR, { recursive: true, force: true }); |
| 63 | + } catch (error) { |
| 64 | + // Ignore cleanup errors |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + test('dev-watch script should exist', async () => { |
| 69 | + expect(existsSync(DEV_SCRIPT)).toBe(true); |
| 70 | + }); |
| 71 | + |
| 72 | + test('file watcher should detect TypeScript file changes in temp directory', async () => { |
| 73 | + // Create a test file in temp directory |
| 74 | + const initialContent = `// Test file for hot reload - ${Date.now()}\nexport const test = true;\n`; |
| 75 | + await modifyTestFile(initialContent); |
| 76 | + |
| 77 | + // Wait a moment for file system |
| 78 | + await new Promise((resolve) => setTimeout(resolve, 500)); |
| 79 | + |
| 80 | + // Verify file was created |
| 81 | + expect(existsSync(TEST_FILE)).toBe(true); |
| 82 | + |
| 83 | + // Modify the file to simulate a code change |
| 84 | + const modifiedContent = `// Test file for hot reload - ${Date.now()}\nexport const test = false;\n`; |
| 85 | + await modifyTestFile(modifiedContent); |
| 86 | + |
| 87 | + // Wait for file system to register the change |
| 88 | + await new Promise((resolve) => setTimeout(resolve, 500)); |
| 89 | + |
| 90 | + // Verify file was modified |
| 91 | + const content = await readFile(TEST_FILE, 'utf-8'); |
| 92 | + expect(content).toContain('export const test = false'); |
| 93 | + |
| 94 | + // Clean up |
| 95 | + await cleanupTestFile(); |
| 96 | + }, 10000); |
| 97 | + |
| 98 | + test('watched directories should include all CLI dependencies', () => { |
| 99 | + // Read the dev-watch.js file and verify it watches the right directories |
| 100 | + const devWatchContent = readFileSync(DEV_SCRIPT, 'utf-8'); |
| 101 | + |
| 102 | + // Check that critical packages are being watched |
| 103 | + const expectedPackages = [ |
| 104 | + 'cli/src', |
| 105 | + 'core/src', |
| 106 | + 'server/src', |
| 107 | + 'api-client/src', |
| 108 | + 'plugin-bootstrap/src', |
| 109 | + 'plugin-sql/src', |
| 110 | + 'config/src', |
| 111 | + ]; |
| 112 | + |
| 113 | + expectedPackages.forEach((pkg) => { |
| 114 | + expect(devWatchContent).toContain(pkg); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + test('debounce mechanism should prevent rapid rebuilds', async () => { |
| 119 | + // This test verifies that the debounce timer is set |
| 120 | + const devWatchContent = readFileSync(DEV_SCRIPT, 'utf-8'); |
| 121 | + |
| 122 | + // Check for debounce implementation |
| 123 | + expect(devWatchContent).toContain('rebuildDebounceTimer'); |
| 124 | + expect(devWatchContent).toContain('clearTimeout'); |
| 125 | + expect(devWatchContent).toContain('setTimeout'); |
| 126 | + }); |
| 127 | + |
| 128 | + test('rebuild function should stop server before rebuilding', () => { |
| 129 | + const devWatchContent = readFileSync(DEV_SCRIPT, 'utf-8'); |
| 130 | + |
| 131 | + // Check that rebuild logic includes server shutdown |
| 132 | + expect(devWatchContent).toContain('rebuildAndRestartServer'); |
| 133 | + expect(devWatchContent).toContain('kill'); |
| 134 | + expect(devWatchContent).toContain('SIGTERM'); |
| 135 | + }); |
| 136 | + |
| 137 | + test('file watcher should only watch TypeScript files', () => { |
| 138 | + const devWatchContent = readFileSync(DEV_SCRIPT, 'utf-8'); |
| 139 | + |
| 140 | + // Check that file watcher filters for TypeScript files |
| 141 | + expect(devWatchContent).toContain('.ts'); |
| 142 | + expect(devWatchContent).toContain('.tsx'); |
| 143 | + }); |
| 144 | + |
| 145 | + test('cleanup should handle file watchers properly', () => { |
| 146 | + const devWatchContent = readFileSync(DEV_SCRIPT, 'utf-8'); |
| 147 | + |
| 148 | + // Check that cleanup includes watcher handling |
| 149 | + expect(devWatchContent).toContain("type === 'watcher'"); |
| 150 | + expect(devWatchContent).toContain('child.close'); |
| 151 | + }); |
| 152 | + |
| 153 | + test('rebuild queuing should handle file changes during rebuild', () => { |
| 154 | + const devWatchContent = readFileSync(DEV_SCRIPT, 'utf-8'); |
| 155 | + |
| 156 | + // Check for rebuild queueing mechanism |
| 157 | + expect(devWatchContent).toContain('rebuildQueued'); |
| 158 | + expect(devWatchContent).toContain('isRebuilding'); |
| 159 | + }); |
| 160 | + |
| 161 | + test('directory existence check before watching', () => { |
| 162 | + const devWatchContent = readFileSync(DEV_SCRIPT, 'utf-8'); |
| 163 | + |
| 164 | + // Check for directory existence verification |
| 165 | + expect(devWatchContent).toContain('existsSync'); |
| 166 | + expect(devWatchContent).toContain('Skipping'); |
| 167 | + }); |
| 168 | + |
| 169 | + test('SIGKILL fallback uses exit event listener', () => { |
| 170 | + const devWatchContent = readFileSync(DEV_SCRIPT, 'utf-8'); |
| 171 | + |
| 172 | + // Check for proper exit event handling |
| 173 | + expect(devWatchContent).toContain('exited'); |
| 174 | + expect(devWatchContent).toContain('SIGKILL'); |
| 175 | + }); |
| 176 | + |
| 177 | + test('server health check after rebuild', () => { |
| 178 | + const devWatchContent = readFileSync(DEV_SCRIPT, 'utf-8'); |
| 179 | + |
| 180 | + // Check for health check in rebuild path |
| 181 | + expect(devWatchContent).toContain('waitForServer'); |
| 182 | + expect(devWatchContent).toContain('rebuild'); |
| 183 | + }); |
| 184 | + |
| 185 | + test('uses Bun.spawn instead of Node.js spawn', () => { |
| 186 | + const devWatchContent = readFileSync(DEV_SCRIPT, 'utf-8'); |
| 187 | + |
| 188 | + // Check that Bun.spawn is used |
| 189 | + expect(devWatchContent).toContain('Bun.spawn'); |
| 190 | + |
| 191 | + // Check that child_process is NOT imported |
| 192 | + expect(devWatchContent).not.toContain("from 'child_process'"); |
| 193 | + expect(devWatchContent).not.toContain('require("child_process")'); |
| 194 | + }); |
| 195 | +}); |
| 196 | + |
| 197 | +describe('Hot Reload Integration', () => { |
| 198 | + test('manual verification instructions', () => { |
| 199 | + console.log('\n=== Manual Hot Reload Test Instructions ==='); |
| 200 | + console.log('1. Run: bun run dev'); |
| 201 | + console.log('2. Wait for server to start (you should see "Development environment fully ready!")'); |
| 202 | + console.log('3. Modify a file in packages/cli/src/, packages/core/src/, or packages/server/src/'); |
| 203 | + console.log('4. Watch the console - you should see:'); |
| 204 | + console.log(' - [WATCH] File changed in <package>: <filename>'); |
| 205 | + console.log(' - [REBUILD] Rebuilding CLI...'); |
| 206 | + console.log(' - [REBUILD] Build completed, restarting server...'); |
| 207 | + console.log(' - [HEALTH] Waiting for server to be ready...'); |
| 208 | + console.log(' - [REBUILD] Server restarted successfully!'); |
| 209 | + console.log('5. Verify the server is still running and responsive'); |
| 210 | + console.log('6. If you make changes during a rebuild, they should be queued'); |
| 211 | + console.log('=========================================\n'); |
| 212 | + |
| 213 | + // This is a documentation test - always passes |
| 214 | + expect(true).toBe(true); |
| 215 | + }); |
| 216 | +}); |
0 commit comments