|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { |
| 5 | + createTempStateDir, |
| 6 | + getTestPort, |
| 7 | + runCli, |
| 8 | +} from './helpers.js'; |
| 9 | + |
| 10 | +describe('Daemon auto-restart on rebuild', () => { |
| 11 | + let stateDir: string; |
| 12 | + let port: number; |
| 13 | + |
| 14 | + beforeEach(async () => { |
| 15 | + stateDir = createTempStateDir(); |
| 16 | + port = getTestPort(); |
| 17 | + |
| 18 | + const result = await runCli(['start', `--port`, `${port}`], stateDir); |
| 19 | + expect(result.exitCode).toBe(0); |
| 20 | + }); |
| 21 | + |
| 22 | + afterEach(async () => { |
| 23 | + await runCli(['stop'], stateDir); |
| 24 | + try { |
| 25 | + fs.rmSync(stateDir, { recursive: true, force: true }); |
| 26 | + } catch { |
| 27 | + // ignore |
| 28 | + } |
| 29 | + }); |
| 30 | + |
| 31 | + it('should restart daemon when daemon.js has been rebuilt', async () => { |
| 32 | + const infoPath = path.join(stateDir, 'daemon.json'); |
| 33 | + const infoBefore = JSON.parse(fs.readFileSync(infoPath, 'utf-8')); |
| 34 | + |
| 35 | + // Simulate a rebuild by changing the stored buildMtime so it no longer |
| 36 | + // matches the actual daemon.js mtime |
| 37 | + infoBefore.buildMtime = 1000; |
| 38 | + fs.writeFileSync(infoPath, JSON.stringify(infoBefore, null, 2)); |
| 39 | + |
| 40 | + const result = await runCli(['get', 'tree'], stateDir); |
| 41 | + expect(result.exitCode).toBe(0); |
| 42 | + |
| 43 | + const infoAfter = JSON.parse(fs.readFileSync(infoPath, 'utf-8')); |
| 44 | + expect(infoAfter.pid).not.toBe(infoBefore.pid); |
| 45 | + expect(infoAfter.port).toBe(port); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should not restart when daemon is up to date', async () => { |
| 49 | + const infoPath = path.join(stateDir, 'daemon.json'); |
| 50 | + const infoBefore = JSON.parse(fs.readFileSync(infoPath, 'utf-8')); |
| 51 | + |
| 52 | + const result = await runCli(['get', 'tree'], stateDir); |
| 53 | + expect(result.exitCode).toBe(0); |
| 54 | + |
| 55 | + const infoAfter = JSON.parse(fs.readFileSync(infoPath, 'utf-8')); |
| 56 | + expect(infoAfter.pid).toBe(infoBefore.pid); |
| 57 | + }); |
| 58 | +}); |
0 commit comments