---
phase: 01.1-hexagonal-architecture
plan: 05
type: execute
wave: 2
depends_on: ["01.1-01"]
files_modified: [src/server/index.test.ts, src/server/shutdown.test.ts, src/server/index.ts, src/server/shutdown.ts]
autonomous: true
---

<objective>
Add test coverage and event emission to the Server module.

Purpose: Server events (started/stopped) are critical for coordination. The shutdown handler orchestrates cleanup - events enable reactive patterns.
Output: Fully tested server module with event-driven lifecycle.
</objective>

<execution_context>
@~/.claude/get-shit-done/workflows/execute-plan.md
@~/.claude/get-shit-done/templates/summary.md
</execution_context>

<context>
@.planning/PROJECT.md
@.planning/ROADMAP.md
@.planning/STATE.md
@.planning/phases/01.1-hexagonal-architecture/01.1-01-SUMMARY.md

@src/server/index.ts
@src/server/shutdown.ts
@src/server/types.ts
@src/events/index.ts
</context>

<tasks>

<task type="auto">
  <name>Task 1: Write unit tests for CoordinationServer</name>
  <files>src/server/index.test.ts</files>
  <action>
    Create src/server/index.test.ts:

    Test server lifecycle:
    - Test start() begins listening on configured port
    - Test start() throws if already running
    - Test start() throws if PID file exists (another server)
    - Test stop() stops the server
    - Test stop() is idempotent (no error if not running)
    - Test isRunning() returns correct state

    Test HTTP endpoints:
    - Test GET /health returns status 'ok' with uptime
    - Test GET /status returns server info
    - Test GET /unknown returns 404
    - Test POST /health returns 405 (method not allowed)

    Test PID file:
    - Test PID file created on start
    - Test PID file removed on stop
    - Test stale PID file is cleaned up

    Testing approach:
    - Create server with random high port to avoid conflicts
    - Use temporary directory for PID file
    - Create mock ProcessManager and LogManager (they're not used in tests)
    - Use fetch() or http.request() to test endpoints
    - Clean up after each test (stop server, remove temp files)
  </action>
  <verify>npm test passes with server tests green</verify>
  <done>CoordinationServer has comprehensive test coverage</done>
</task>

<task type="auto">
  <name>Task 2: Add event emission to server and shutdown handler</name>
  <files>src/server/index.ts, src/server/shutdown.ts</files>
  <action>
    Modify CoordinationServer constructor:
    - Add optional eventBus parameter: EventBus | undefined
    - Store as private readonly property

    Add event emission in start():
    - After server starts listening, emit ServerStarted:
      { type: 'server.started', timestamp: new Date(), payload: { port, host, pid: process.pid } }

    Add event emission in stop():
    - Before stopping, emit ServerStopped:
      { type: 'server.stopped', timestamp: new Date(), payload: { uptime } }

    Modify GracefulShutdown:
    - Add optional eventBus parameter
    - Emit events during shutdown sequence if event bus provided:
      - Could emit ShutdownInitiated, ShutdownComplete events
      - Or rely on ServerStopped from CoordinationServer (simpler)
    - Keep it simple: just ensure CoordinationServer emits ServerStopped

    Update tests:
    - Add mock EventBus to server tests
    - Verify ServerStarted emitted on start
    - Verify ServerStopped emitted on stop

    Create src/server/shutdown.test.ts:
    - Test shutdown() calls server.stop()
    - Test shutdown() calls processManager.stopAll()
    - Test signal handlers are installed
    - Mock dependencies (server, processManager, logManager)
  </action>
  <verify>npm test passes with all server module tests green</verify>
  <done>Server emits lifecycle events, shutdown handler tested</done>
</task>

</tasks>

<verification>
Before declaring plan complete:
- [ ] npm run build succeeds
- [ ] npm test passes (all server module tests)
- [ ] CoordinationServer accepts optional eventBus
- [ ] ServerStarted event emitted on start
- [ ] ServerStopped event emitted on stop
- [ ] GracefulShutdown has test coverage
</verification>

<success_criteria>

- All tasks completed
- All verification checks pass
- Server lifecycle events flow through event bus
- Shutdown sequence is tested
</success_criteria>

<output>
After completion, create `.planning/phases/01.1-hexagonal-architecture/01.1-05-SUMMARY.md`
</output>
