Skip to content

test: add unit tests for pkg/server/response package#201

Open
mason5052 wants to merge 1 commit intovxcontrol:masterfrom
mason5052:test/server-response-coverage
Open

test: add unit tests for pkg/server/response package#201
mason5052 wants to merge 1 commit intovxcontrol:masterfrom
mason5052:test/server-response-coverage

Conversation

@mason5052
Copy link
Contributor

Description of Change

Problem: The pkg/server/response package has no unit test coverage. This package defines the HttpError type, 90+ predefined error variables, and the Success/Error HTTP response functions used across all API endpoints.

Solution: Add unit tests for HttpError type (constructor, accessors, error interface implementation), predefined error variables (HTTP codes and error codes across 12 domain categories), and Success/Error response functions with gin test context including dev mode vs production mode behavior for error detail exposure.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Security update
  • Test update
  • Documentation update
  • Configuration change

Areas Affected

  • Core Services (Frontend UI / Backend API)
  • AI Agents (Researcher / Developer / Executor)
  • Security Tools Integration
  • Memory System (Vector Store / Knowledge Base)
  • Monitoring Stack (Grafana / OpenTelemetry)
  • Analytics & Reporting
  • External Integrations (LLM Providers / Search Engines / Security APIs)
  • Documentation
  • Infrastructure / DevOps

Testing and Verification

Test Configuration

  • PentAGI Version: v1.2.0 (master)
  • Go Version: 1.24.1
  • Host OS: Windows 11

Test Steps

  1. Run go test ./pkg/server/response/... -v

Test Results

=== RUN   TestNewHttpError
--- PASS: TestNewHttpError (0.00s)
=== RUN   TestHttpError_Error
--- PASS: TestHttpError_Error (0.00s)
=== RUN   TestHttpError_ImplementsError
--- PASS: TestHttpError_ImplementsError (0.00s)
=== RUN   TestPredefinedErrors
--- PASS: TestPredefinedErrors (0.00s)
=== RUN   TestSuccessResponse
--- PASS: TestSuccessResponse (0.00s)
=== RUN   TestSuccessResponse_Created
--- PASS: TestSuccessResponse_Created (0.00s)
=== RUN   TestErrorResponse
--- PASS: TestErrorResponse (0.00s)
=== RUN   TestErrorResponse_DevMode
--- PASS: TestErrorResponse_DevMode (0.00s)
=== RUN   TestErrorResponse_ProductionMode
--- PASS: TestErrorResponse_ProductionMode (0.00s)
=== RUN   TestErrorResponse_NilOriginalError
--- PASS: TestErrorResponse_NilOriginalError (0.00s)
PASS
ok  	pentagi/pkg/server/response	2.723s

Checklist

  • Code follows project coding standards
  • Tests added for changes
  • All tests pass
  • go fmt and go vet run
  • Changes are backward compatible

Add unit tests for HttpError type (constructor, accessors, error interface),
predefined error variables (HTTP codes, error codes for 12 categories),
Success/Error response functions with gin test context including dev mode
vs production mode behavior for error detail exposure.
Copilot AI review requested due to automatic review settings March 12, 2026 18:01
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds initial unit test coverage for the pkg/server/response package to validate HttpError behavior, a sample of predefined HttpError variables, and the JSON contract of Success/Error response helpers (including dev vs production behavior around exposing original errors).

Changes:

  • Add unit tests for HttpError constructor/accessors and error interface formatting.
  • Add table-driven assertions for selected predefined *HttpError variables (HTTP status + error code).
  • Add gin-based tests for Success and Error JSON responses, including develop-mode vs production-mode error detail exposure.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +112 to +115
func TestErrorResponse_DevMode(t *testing.T) {
// Enable dev mode
version.PackageVer = ""

Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test mutates the global version.PackageVer to force dev mode but never restores the prior value. If PackageVer is set via build flags (or future tests set it), this can leak state across tests in this package. Capture the current value before changing it and defer restoring it at the end of the test.

Copilot uses AI. Check for mistakes.
Comment on lines +133 to +134
version.PackageVer = "1.0.0"
defer func() { version.PackageVer = "" }()
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TestErrorResponse_ProductionMode resets version.PackageVer to an empty string unconditionally. If PackageVer had a non-empty value before the test (e.g., injected via ldflags), this will not restore the original state. Store the previous value and restore it in the deferred function instead of hardcoding "".

Suggested change
version.PackageVer = "1.0.0"
defer func() { version.PackageVer = "" }()
originalPackageVer := version.PackageVer
version.PackageVer = "1.0.0"
defer func() { version.PackageVer = originalPackageVer }()

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants