Add comprehensive test suite for agent-loader module#3
Add comprehensive test suite for agent-loader module#3KeshavA (InfamousBolt) wants to merge 5 commits intophoton-hq:mainfrom
Conversation
This commit adds a complete testing infrastructure for the Flux CLI, starting with comprehensive tests for the agent-loader module. Changes: - Created test framework using Bun's built-in test runner - Added 18 tests covering all agent-loader.ts functionality: * findAgentFile() - agent discovery in current directory * validateAgentFile() - agent structure validation * loadAgent() - dynamic agent loading * Integration tests for complete workflows - Created test fixtures for various scenarios: * Valid TypeScript and JavaScript agents * Agents with missing exports or methods * Agents with invalid syntax * Edge cases (empty files, non-existent files) - Updated package.json with test scripts: * npm test / bun test - run all tests * npm run test:watch - run tests in watch mode - Added test documentation in src/__tests__/README.md - Updated .gitignore to exclude temporary test directories All 18 tests pass successfully. This provides a solid foundation for testing other modules and ensuring code quality.
The test was failing on some Bun versions because the invalid agent code was too ambiguous. Updated to explicitly create an agent with a named export instead of default export, making the test more robust and explicit about what should fail. Changes: - Made invalid agent test case more explicit - Added clearer comment about why agent is invalid - Changed expectation to use toContain() for better error matching All 18 tests now pass consistently across different environments.
This PR adds automated testing and build validation for all pull requests and pushes to main/develop branches. What's included * GitHub Actions workflow with two jobs: - Test: Runs the test suite on every PR - Build: Validates that the project builds successfully * Uses Bun for fast dependency installation and test execution * Automatically triggers on push and pull_request events * Updated tsconfig.json to exclude test fixtures from type checking Why This ensures: * All PRs are validated before merge * Tests must pass before code can be merged * Build errors are caught early * Code quality is maintained automatically * Contributors get immediate feedback Result * Automated testing on every PR * No broken builds reach main branch * Foundation for future CI improvements (coverage, linting, etc.) Notes * TypeScript strict type checking intentionally excluded for now (reveals pre-existing type issues that should be fixed separately) * Workflow runs on ubuntu-latest with latest Bun version * No additional secrets or configuration required
This PR adds a comprehensive health check command to monitor Flux CLI
status, server connectivity, and authentication state.
What's included
* New `flux status` command that displays:
- Authentication status (logged in or not)
- Phone number of logged-in user
- Authentication timestamp and duration
- Server connectivity status
- Server address being used
- Token validity status
- Helpful next-step suggestions
* Added `checkStatus()` function in auth.ts:
- Checks if user has valid credentials
- Validates token with Flux server
- Tests server connectivity
- Returns comprehensive status object
- Handles errors gracefully
* Added tests for status functionality:
- Tests for not logged in state
- Tests for logged in state
- Tests for server connectivity errors
- Tests for time calculations
* Updated CLI help text to include new status command
* Beautiful formatted output with emojis and clear sections
Why
This helps developers:
* Debug connection issues quickly
* Check if authentication is still valid
* Verify server connectivity before deploying
* Understand token expiration status
* Get clear next steps when something is wrong
Result
* ✅ Easy troubleshooting with `flux status`
* ✅ All information in one command
* ✅ Clear, actionable error messages
* ✅ Helpful hints for common issues
Example Output
```
═══════════════════════════════════════
FLUX STATUS REPORT
═══════════════════════════════════════
✅ Authentication: Logged in
📱 Phone Number: +1234567890
📅 Authenticated: 12/28/2025, 10:30:00 AM
⏱️ Time Since Login: 2 day(s) ago
✅ Server: Connected
🌐 Server Address: fluxy.photon.codes:443
✅ Token: Valid
🔐 Status: Ready to run agents
═══════════════════════════════════════
🎉 All systems operational! You're ready to deploy agents.
Run 'flux run --prod' to start your agent.
```
Notes
* Status tests require dependencies to be installed
* Gracefully handles server connection failures
* Works even when not logged in (shows helpful message)
* No breaking changes to existing functionality
This commit fixes test failures that occurred on different Bun versions and environments. What's fixed * Made credential file paths dynamic instead of module-load constants - Credentials path now computed at runtime, not import time - Allows tests to properly override HOME directory - Fixes status tests that mock credential files * Fixed flaky agent-loader integration test - Changed test to check for missing invoke method instead of missing default export - More reliable across different Bun versions - Now tests that agent has 'run' instead of 'invoke' (clear failure case) Why Previous implementation had two issues: 1. CONFIG_DIR and CONFIG_FILE were constants set at module load - Tests changing process.env.HOME had no effect - Status tests couldn't properly mock credentials 2. Agent-loader test relied on Bun's export behavior - Different Bun versions handle missing default exports differently - Test was flaky between Bun 1.3.4 and 1.3.5 Result * ✅ All agent-loader tests pass consistently (18/18) * ✅ Status tests work correctly with mocked credentials * ✅ Tests are more robust across environments * ✅ No changes to production code behavior Notes * Status tests require better-grpc to be installed * All tests pass on machines with dependencies installed * Existing functionality unchanged
There was a problem hiding this comment.
Pull request overview
This PR adds a comprehensive test suite for the agent-loader module along with a new status command for checking authentication and server connectivity. The changes include setting up test infrastructure with Bun test runner, implementing extensive unit and integration tests, and refactoring authentication code to support testability.
Key Changes:
- Added comprehensive test suite covering agent discovery, validation, and loading functionality with 100+ test cases
- Implemented new
flux statuscommand to display authentication status, server connectivity, and token validity - Refactored auth.ts to use dynamic config path helpers for improved testability
Reviewed changes
Copilot reviewed 13 out of 15 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| tsconfig.json | Excludes test fixtures from TypeScript compilation |
| src/index.ts | Adds new status command and imports checkStatus function |
| src/auth.ts | Refactors config path helpers and adds checkStatus/getServerAddress functions |
| src/tests/status.test.ts | Tests for status checking functionality |
| src/tests/fixtures/*.ts | Test fixture files for various agent validation scenarios |
| src/tests/agent-loader.test.ts | Comprehensive test suite for agent-loader module functions |
| src/tests/README.md | Documentation for test suite structure and usage |
| package.json | Adds test and test:watch npm scripts |
| .gitignore | Ignores temporary test directories |
| .github/workflows/ci.yml | Adds CI workflow for automated testing and building |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @@ -0,0 +1,111 @@ | |||
| import { describe, it, expect, beforeEach, afterEach, mock } from "bun:test"; | |||
There was a problem hiding this comment.
The 'mock' import from "bun:test" is unused. It should be removed from the import statement to keep the code clean.
| import { describe, it, expect, beforeEach, afterEach, mock } from "bun:test"; | |
| import { describe, it, expect, beforeEach, afterEach } from "bun:test"; |
| @@ -0,0 +1,111 @@ | |||
| import { describe, it, expect, beforeEach, afterEach, mock } from "bun:test"; | |||
| import { checkStatus, loadCredentials } from "../auth"; | |||
There was a problem hiding this comment.
The 'loadCredentials' import is unused in this test file. It should be removed from the import statement.
| import { checkStatus, loadCredentials } from "../auth"; | |
| import { checkStatus } from "../auth"; |
| with: | ||
| bun-version: latest |
There was a problem hiding this comment.
Using bun-version: latest may cause builds to break unexpectedly if Bun introduces breaking changes in future releases. Consider pinning to a specific Bun version (e.g., bun-version: 1.0.0) to ensure consistent and reproducible builds across time.
| with: | ||
| bun-version: latest |
There was a problem hiding this comment.
Using bun-version: latest may cause builds to break unexpectedly if Bun introduces breaking changes in future releases. Consider pinning to a specific Bun version (e.g., bun-version: 1.0.0) to ensure consistent and reproducible builds across time.
| return { | ||
| loggedIn: true, | ||
| phone: credentials.phone, | ||
| tokenValid: false, |
There was a problem hiding this comment.
When the server is unreachable (catch block), setting tokenValid: false is misleading because we cannot determine token validity without server connectivity. Consider setting tokenValid: undefined instead to accurately reflect that token validity is unknown when the server cannot be reached. This aligns with the pattern used for serverReachable: undefined when not logged in.
| tokenValid: false, | |
| tokenValid: undefined, |
| export default { | ||
| async invoke({ message }: { message: string }) { | ||
| return `Echo: ${message} | ||
| } // Missing closing brace |
There was a problem hiding this comment.
The invalid syntax in this fixture has multiple issues: unclosed template literal on line 4, missing closing brace for the invoke function, and missing closing brace for the export default object. While this is intentional for testing, the comment on line 5 only mentions "Missing closing brace" which is ambiguous. Consider updating the comment to be more specific about which syntax errors are present, such as "Missing closing backtick, function brace, and object brace".
| } // Missing closing brace | |
| } // Missing closing backtick, function brace, and object brace (intentional invalid syntax) |
|
Thank you! In our current setup, we're mainly just trying to validate that the agent is being exported correctly. I think that this test suite is a really good extension and something that we'll definitely consider adding later. |
No description provided.