|
| 1 | +/** |
| 2 | + * #4238: `daemon status` printed the TTL of the status process's own |
| 3 | + * default-config WorkerDaemon (12h), not the running background daemon's, |
| 4 | + * so a daemon started with `--ttl 0` was reported as self-shutting down. |
| 5 | + * The background daemon persists its real config in daemon-state.json. |
| 6 | + */ |
| 7 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 8 | +import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from 'node:fs'; |
| 9 | +import { join } from 'node:path'; |
| 10 | +import { tmpdir } from 'node:os'; |
| 11 | +import { daemonCommand } from '../src/commands/daemon.js'; |
| 12 | +import { output } from '../src/output.js'; |
| 13 | +import type { CommandContext } from '../src/types.js'; |
| 14 | + |
| 15 | +const originalCwd = process.cwd(); |
| 16 | +let workspace: string | undefined; |
| 17 | + |
| 18 | +afterEach(() => { |
| 19 | + process.chdir(originalCwd); |
| 20 | + vi.restoreAllMocks(); |
| 21 | + if (workspace) rmSync(workspace, { recursive: true, force: true }); |
| 22 | + workspace = undefined; |
| 23 | +}); |
| 24 | + |
| 25 | +async function statusBoxFor(config: Record<string, unknown>): Promise<string> { |
| 26 | + workspace = mkdtempSync(join(tmpdir(), 'daemon-status-ttl-')); |
| 27 | + const stateDir = join(workspace, '.claude-flow'); |
| 28 | + mkdirSync(stateDir, { recursive: true }); |
| 29 | + // A live PID stands in for the background daemon. |
| 30 | + writeFileSync(join(stateDir, 'daemon.pid'), String(process.pid)); |
| 31 | + writeFileSync(join(stateDir, 'daemon-state.json'), JSON.stringify({ running: true, config })); |
| 32 | + process.chdir(workspace); |
| 33 | + const lines: string[] = []; |
| 34 | + vi.spyOn(output, 'printBox').mockImplementation((content: string | string[]) => { |
| 35 | + lines.push(...(Array.isArray(content) ? content : [content])); |
| 36 | + }); |
| 37 | + vi.spyOn(output, 'writeln').mockImplementation(() => undefined); |
| 38 | + const status = daemonCommand.subcommands!.find(c => c.name === 'status')!; |
| 39 | + await status.action!({ args: [], flags: {}, cwd: workspace, interactive: false } as unknown as CommandContext); |
| 40 | + return lines.join('\n'); |
| 41 | +} |
| 42 | + |
| 43 | +describe('daemon status TTL (#4238)', () => { |
| 44 | + it('reports TTL off when the running background daemon has ttlMs 0', async () => { |
| 45 | + const box = await statusBoxFor({ ttlMs: 0 }); |
| 46 | + expect(box).toContain('off (runs until stopped)'); |
| 47 | + expect(box).not.toContain('(self-shutdown)'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('reports the background daemon TTL rather than the 12h default', async () => { |
| 51 | + expect(await statusBoxFor({ ttlMs: 2 * 3600000 })).toContain('TTL: 2h (self-shutdown)'); |
| 52 | + }); |
| 53 | +}); |
0 commit comments