Skip to content

test: add unit tests for pkg/fileutil and pkg/health#1829

Open
perhapzz wants to merge 2 commits intosipeed:mainfrom
perhapzz:test/add-fileutil-health-tests
Open

test: add unit tests for pkg/fileutil and pkg/health#1829
perhapzz wants to merge 2 commits intosipeed:mainfrom
perhapzz:test/add-fileutil-health-tests

Conversation

@perhapzz
Copy link

@perhapzz perhapzz commented Mar 20, 2026

📝 Description

Add comprehensive unit test coverage for two packages that previously had zero tests:

  • pkg/fileutil (9 tests) — atomic file write operations
  • pkg/health (15 tests) — health/ready/reload HTTP endpoints

🗣️ Type of Change

  • 🐞 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 📖 Documentation update
  • ⚡ Code refactoring (no functional changes, no api changes)

🤖 AI Code Generation

  • 🤖 Fully AI-generated (100% AI, 0% Human)
  • 🛠️ Mostly AI-generated (AI draft, Human verified/modified)
  • 👨💻 Mostly Human-written (Human lead, AI assisted or none)

🔗 Related Issue

No existing issue — discovered during code review that these two core packages had 0% test coverage.

📚 Technical Context (Skip for Docs)

  • Reference URL: N/A
  • Reasoning: pkg/fileutil.WriteFileAtomic is used for config persistence on edge devices (SD cards, flash storage) and pkg/health endpoints are critical for Docker/K8s deployments. Both deserve test coverage to prevent regressions.

🧪 Test Environment

  • Hardware: Docker container (aarch64)
  • OS: Debian 12 (bookworm)
  • Model/Provider: N/A (unit tests only)
  • Channels: N/A

Test Details

pkg/fileutil — 9 tests:

Test Description
TestWriteFileAtomic_Basic Write and read-back verification
TestWriteFileAtomic_Permissions File permission (0600) enforcement
TestWriteFileAtomic_Overwrite Atomically overwrite existing file
TestWriteFileAtomic_EmptyData Handle empty byte slice
TestWriteFileAtomic_CreatesParentDirs Auto-create nested directories
TestWriteFileAtomic_NoTempFileOnSuccess No temp file leak after success
TestWriteFileAtomic_LargeFile 1MB file write integrity
TestWriteFileAtomic_Concurrent Concurrent write safety
TestWriteFileAtomic_InvalidPath Error on invalid path

pkg/health — 15 tests:

Test Description
TestHealthHandler_ReturnsOK /health returns 200 with status, uptime, pid
TestReadyHandler_NotReady /ready returns 503 when not ready
TestReadyHandler_Ready /ready returns 200 when ready
TestReadyHandler_FailedCheck /ready fails when registered check fails
TestReadyHandler_PassingCheck /ready passes with all checks ok
TestReloadHandler_MethodNotAllowed /reload rejects non-POST
TestReloadHandler_NoReloadFunc /reload 503 without handler
TestReloadHandler_Success /reload calls function, returns 200
TestReloadHandler_Error /reload returns 500 on function error
TestSetReady_Toggle Toggle ready state
TestRegisterCheck_MultipleChecks Multiple check interaction
TestRegisterOnMux Custom ServeMux registration
TestNewServer Default initialization
TestStartContext_Cancellation Graceful shutdown on context cancel
TestStatusString Helper function

📸 Evidence (Optional)

Click to view test output
=== pkg/fileutil ===
--- PASS: TestWriteFileAtomic_Basic (0.01s)
--- PASS: TestWriteFileAtomic_Permissions (0.00s)
--- PASS: TestWriteFileAtomic_Overwrite (0.00s)
--- PASS: TestWriteFileAtomic_EmptyData (0.00s)
--- PASS: TestWriteFileAtomic_CreatesParentDirs (0.00s)
--- PASS: TestWriteFileAtomic_NoTempFileOnSuccess (0.00s)
--- PASS: TestWriteFileAtomic_LargeFile (0.00s)
--- PASS: TestWriteFileAtomic_Concurrent (0.01s)
--- PASS: TestWriteFileAtomic_InvalidPath (0.00s)
PASS
ok  	github.com/sipeed/picoclaw/pkg/fileutil	0.057s

=== pkg/health ===
--- PASS: TestHealthHandler_ReturnsOK (0.01s)
--- PASS: TestReadyHandler_NotReady (0.00s)
--- PASS: TestReadyHandler_Ready (0.00s)
--- PASS: TestReadyHandler_FailedCheck (0.00s)
--- PASS: TestReadyHandler_PassingCheck (0.00s)
--- PASS: TestReloadHandler_MethodNotAllowed (0.00s)
--- PASS: TestReloadHandler_NoReloadFunc (0.00s)
--- PASS: TestReloadHandler_Success (0.00s)
--- PASS: TestReloadHandler_Error (0.00s)
--- PASS: TestSetReady_Toggle (0.00s)
--- PASS: TestRegisterCheck_MultipleChecks (0.00s)
--- PASS: TestRegisterOnMux (0.00s)
--- PASS: TestNewServer (0.00s)
--- PASS: TestStartContext_Cancellation (0.06s)
--- PASS: TestStatusString (0.00s)
PASS
ok  	github.com/sipeed/picoclaw/pkg/health	0.102s

☑️ Checklist

  • My code/docs follow the style of this project.
  • I have performed a self-review of my own changes.
  • I have updated the documentation accordingly.

…server

Add comprehensive test coverage for two previously untested packages:

pkg/fileutil (9 tests):
- Basic write and read-back
- File permissions (0600)
- Overwrite existing files
- Empty data handling
- Nested directory auto-creation
- No temp files left after success
- Large file (1MB) handling
- Concurrent write safety
- Invalid path error handling

pkg/health (15 tests):
- Health endpoint returns 200 with status, uptime, pid
- Ready endpoint returns 503 when not ready
- Ready endpoint returns 200 when ready
- Ready fails when any registered check fails
- Ready passes with all checks passing
- Reload rejects non-POST methods
- Reload returns 503 when no reload func set
- Reload calls registered function on success
- Reload returns 500 on function error
- SetReady toggle behavior
- Multiple health checks interaction
- RegisterOnMux works with custom ServeMux
- NewServer defaults
- StartContext graceful shutdown on cancel
- statusString helper
@CLAassistant
Copy link

CLAassistant commented Mar 20, 2026

CLA assistant check
All committers have signed the CLA.

@perhapzz perhapzz changed the title test(fileutil,health): add unit tests for two untested packages test: add unit tests for pkg/fileutil and pkg/health Mar 20, 2026
@sipeed-bot sipeed-bot bot added type: enhancement New feature or request go Pull requests that update go code labels Mar 20, 2026
Copy link
Collaborator

@afjcjsbx afjcjsbx left a comment

Choose a reason for hiding this comment

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

LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

go Pull requests that update go code type: enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants