|
| 1 | +/** |
| 2 | + * Tests for two-phase daemon startup behavior. |
| 3 | + * Verifies socket creation before profile loading and proper status reporting. |
| 4 | + */ |
| 5 | + |
| 6 | +import { readFile, access } from 'fs/promises'; |
| 7 | +import { join } from 'path'; |
| 8 | +import { |
| 9 | + createTestContext, |
| 10 | + cleanupTestContext, |
| 11 | + pq, |
| 12 | + pqFail, |
| 13 | + type PqTestContext, |
| 14 | +} from './utils'; |
| 15 | + |
| 16 | +describe('daemon startup (two-phase)', () => { |
| 17 | + let ctx: PqTestContext; |
| 18 | + |
| 19 | + beforeEach(async () => { |
| 20 | + ctx = await createTestContext(); |
| 21 | + }); |
| 22 | + |
| 23 | + afterEach(async () => { |
| 24 | + await cleanupTestContext(ctx); |
| 25 | + }); |
| 26 | + |
| 27 | + test('daemon creates socket and metadata before loading profile', async () => { |
| 28 | + const startTime = Date.now(); |
| 29 | + |
| 30 | + const result = await pq(ctx, [ |
| 31 | + 'load', |
| 32 | + 'src/test/fixtures/upgrades/processed-1.json', |
| 33 | + ]); |
| 34 | + |
| 35 | + const endTime = Date.now(); |
| 36 | + const duration = endTime - startTime; |
| 37 | + |
| 38 | + expect(result.exitCode).toBe(0); |
| 39 | + |
| 40 | + // Should complete quickly (< 1 second for local file) |
| 41 | + // The key improvement is that we don't wait for profile parsing |
| 42 | + // before getting success feedback |
| 43 | + expect(duration).toBeLessThan(2000); |
| 44 | + |
| 45 | + // Extract session ID |
| 46 | + expect(typeof result.stdout).toBe('string'); |
| 47 | + const match = (result.stdout as string).match(/Session started: (\w+)/); |
| 48 | + const sessionId = match![1]; |
| 49 | + |
| 50 | + // Verify metadata file exists and contains correct info |
| 51 | + const metadataPath = join(ctx.sessionDir, `${sessionId}.json`); |
| 52 | + const metadata = JSON.parse(await readFile(metadataPath, 'utf-8')); |
| 53 | + |
| 54 | + expect(metadata.id).toBe(sessionId); |
| 55 | + expect(metadata.socketPath).toContain(sessionId); |
| 56 | + expect(metadata.pid).toBeNumber(); |
| 57 | + expect(metadata.profilePath).toContain('processed-1.json'); |
| 58 | + }); |
| 59 | + |
| 60 | + test('load returns non-zero exit code on profile load failure', async () => { |
| 61 | + // Create an invalid JSON file |
| 62 | + const invalidProfile = join(ctx.sessionDir, 'invalid.json'); |
| 63 | + const { writeFile } = await import('fs/promises'); |
| 64 | + await writeFile(invalidProfile, '{ invalid json content', 'utf-8'); |
| 65 | + |
| 66 | + const result = await pqFail(ctx, ['load', invalidProfile]); |
| 67 | + |
| 68 | + expect(result.exitCode).not.toBe(0); |
| 69 | + const output = String(result.stdout || '') + String(result.stderr || ''); |
| 70 | + expect(output).toMatch(/Profile load failed|Failed to|parse|invalid/i); |
| 71 | + }); |
| 72 | + |
| 73 | + test('daemon startup fails fast with short timeout', async () => { |
| 74 | + // This test verifies Phase 1 timeout behavior |
| 75 | + // We can't easily force a daemon startup failure, but we can |
| 76 | + // verify the timeout is reasonable by checking it doesn't wait forever |
| 77 | + |
| 78 | + const result = await pqFail(ctx, ['load', '/nonexistent/file.json']); |
| 79 | + |
| 80 | + // Should fail quickly (Phase 1: 500ms for daemon, Phase 2: fails on validation) |
| 81 | + expect(result.exitCode).not.toBe(0); |
| 82 | + }); |
| 83 | + |
| 84 | + test('load blocks until profile is fully loaded', async () => { |
| 85 | + // Start loading |
| 86 | + await pq(ctx, ['load', 'src/test/fixtures/upgrades/processed-1.json']); |
| 87 | + |
| 88 | + // If load returned, profile should be ready immediately |
| 89 | + const result = await pq(ctx, ['profile', 'info']); |
| 90 | + expect(result.exitCode).toBe(0); |
| 91 | + expect(result.stdout).toContain('This profile contains'); |
| 92 | + }); |
| 93 | + |
| 94 | + test('validates session before returning (checks process + socket)', async () => { |
| 95 | + const result = await pq(ctx, [ |
| 96 | + 'load', |
| 97 | + 'src/test/fixtures/upgrades/processed-1.json', |
| 98 | + ]); |
| 99 | + |
| 100 | + expect(typeof result.stdout).toBe('string'); |
| 101 | + const match = (result.stdout as string).match(/Session started: (\w+)/); |
| 102 | + const sessionId = match![1]; |
| 103 | + |
| 104 | + // Verify both socket and metadata exist (validateSession checks both) |
| 105 | + const socketPath = join(ctx.sessionDir, `${sessionId}.sock`); |
| 106 | + const metadataPath = join(ctx.sessionDir, `${sessionId}.json`); |
| 107 | + |
| 108 | + await expect(access(socketPath)).resolves.toBeUndefined(); |
| 109 | + await expect(access(metadataPath)).resolves.toBeUndefined(); |
| 110 | + |
| 111 | + // Process should be running (metadata contains PID) |
| 112 | + const metadata = JSON.parse(await readFile(metadataPath, 'utf-8')); |
| 113 | + expect(metadata.pid).toBeNumber(); |
| 114 | + expect(metadata.pid).toBeGreaterThan(0); |
| 115 | + }); |
| 116 | +}); |
0 commit comments