Skip to content

Add an in-memory Bus implementation for tests and cover internal/bus/natsbus publish/close #43

Description

@Jagadeeshftw

📌 Description

The bus.Bus interface in internal/bus/bus.go has only one implementation (internal/bus/natsbus/natsbus.go) and no tests. Handlers that publish events (e.g. internal/handlers/github_webhooks.go) can only be exercised against a real NATS server, making them hard to test. There is no in-memory/fake Bus.

💡 Why it matters: A fake bus unblocks fast, hermetic tests for every publisher and documents the contract the real NATS bus must satisfy.

🧩 Requirements and context

  • Add an in-memory Bus implementation (e.g. internal/bus/membus) recording published subjects/payloads and supporting Close.
  • Cover natsbus.Publish behavior: nil-connection guard and context-cancellation handling, using a connection stub or nats-server test helper.
  • Document when to use the fake vs the real bus in PATCHWORK_BACKEND_ARCHITECTURE.md.
  • Keep the Bus interface unchanged.
  • Wire the fake into at least one handler test to prove the seam.

Non-functional requirements

  • Must be secure, tested, and documented.
  • Should be efficient and easy to review.

🛠️ Suggested execution

1. Fork the repo and create a branch

git checkout -b test/in-memory-bus

2. Implement changes

  • Write/modify the relevant source: new internal/bus/membus/membus.go
  • Write comprehensive tests: internal/bus/membus/membus_test.go, internal/bus/natsbus/natsbus_test.go
  • Add documentation: architecture doc note on testing the bus
  • Include GoDoc comments on the fake
  • Validate security assumptions: fake must not be selectable in production builds

3. Test and commit

  • Run tests:
go test ./internal/bus/...
  • Cover edge cases: publish after close, cancelled context, nil conn
  • Include test output and security notes in the PR description.

Example commit message

test(bus): add in-memory bus and cover natsbus publish/close

✅ Acceptance criteria

  • In-memory Bus implementation added and tested
  • natsbus nil-conn and context-cancel paths covered
  • At least one handler test uses the fake bus
  • Bus interface unchanged

🔒 Security notes

Ensure the in-memory bus is test-only and cannot be accidentally wired into cmd/api/cmd/worker.

📋 Guidelines

  • Minimum 95% test coverage
  • Clear documentation
  • Timeframe: 96 hours

Metadata

Metadata

Assignees

No one assigned

    Labels

    GrantFox OSSGrantFox open-source programMaybe RewardedGrantFox: potentially rewarded contributionOfficial CampaignGrantFox official campaign issuebackendBackend / API workrefactortestingTests and coverage

    Type

    No fields configured for Task.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions