Skip to content

test: add unit tests for hook/emit.go — event emission and rotation #18

@jpleva91

Description

@jpleva91

Problem

internal/hook/emit.go has zero test coverage. emitEvent() writes governance events to a JSONL file and RotateEvents() rotates the file for ingestion by Sentinel. If these break, telemetry goes dark.

Acceptance Criteria

  • Test emitEvent() appends valid JSON line to <workspace>/.chitin/events.jsonl
  • Test emitEvent() creates the .chitin directory if missing
  • Test emitEvent() is a no-op when CHITIN_WORKSPACE is empty
  • Test detectAgent() returns correct agent name for CHITIN_AGENT, CLAUDE_HOOK_EVENT_NAME, COPILOT_HOOK_EVENT, and unknown
  • Test truncate() truncates strings longer than max and passes through shorter strings
  • Test RotateEvents() renames events.jsonl to a timestamped filename
  • Test RotateEvents() returns empty string when no events file exists
  • Test RotateEvents() returns empty string when CHITIN_WORKSPACE is empty
  • All tests use t.TempDir() and t.Setenv()
  • All tests pass with go test ./internal/hook/...

Hints

  • File: internal/hook/emit.go
  • Functions: emitEvent(), detectAgent(), truncate(), RotateEvents()
  • emitEvent() is unexported — test via package-level test file in hook package
  • Events file: <CHITIN_WORKSPACE>/.chitin/events.jsonl
  • Create test file: internal/hook/emit_test.go

Generated by /forge fill

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions