Skip to content

♻️ Refactor commands to use functional modules directly#133

Merged
Robdel12 merged 3 commits into
mainfrom
commands-functional-refactor
Dec 14, 2025
Merged

♻️ Refactor commands to use functional modules directly#133
Robdel12 merged 3 commits into
mainfrom
commands-functional-refactor

Conversation

@Robdel12
Copy link
Copy Markdown
Contributor

Summary

  • Commands now import from functional modules instead of createServices()
    • API: import { createApiClient, getBuild } from '../api/index.js'
    • Auth: import { createAuthClient, whoami } from '../auth/index.js'
    • Test runner: import { runTests } from '../test-runner/index.js'
  • Added createTokenStore() helper to auth module for commands
  • Commands create lightweight inline objects for serverManager/buildManager instead of using class instances

Test plan

  • All tests pass
  • CI passes
  • Manual test: vizzly run, vizzly tdd, vizzly upload commands work

Part 2 of 3 for functional decomposition refactor (VIZ-128)

Depends on #132

Base automatically changed from test-cleanup to main December 14, 2025 18:44
@claude
Copy link
Copy Markdown

claude Bot commented Dec 14, 2025

Code Review: Functional Refactor of Commands

This PR successfully migrates commands from the service container pattern to direct functional imports. The refactoring improves code clarity and reduces indirection. However, there are several important issues to address.

Critical Issues

1. Code Duplication: createServerManager Function

Location: src/commands/run.js:40-84 and src/commands/tdd.js

The createServerManager function is duplicated across both run.js and tdd.js. This violates DRY principles and creates maintenance burden. If the server manager interface changes, you'll need to update both files identically.

Recommendation: Extract createServerManager to src/server-manager/index.js as an exported helper, then import it in both commands.

2. Circular Dependency Risk in createTokenStore

Location: src/auth/index.js:46-70

The late-import pattern using dynamic import() to avoid circular dependencies is a code smell. Since you're already re-exporting the token functions at the top of the file (lines 11-15), just use them directly. This would be simpler, synchronous, and eliminate the circular dependency concern.

Medium Priority Issues

3. Inconsistent Error Handling in Cleanup

Location: src/commands/run.js:111-149 and src/commands/tdd.js:53-64

The cleanup functions silently swallow all errors with empty catch blocks. At minimum, log errors in verbose mode for debugging.

4. Missing Null Check in run.js Cleanup

Location: src/commands/run.js:129-144

The finalize build logic checks if (buildId && config) but doesn't validate that serverManager is available, even though it's used in the deps object.

5. Process Signal Handler Cleanup

Location: src/commands/run.js:158-159

Signal handlers are registered but never removed, which could cause memory leaks in long-running processes or test suites.

What's Good

  1. Clearer dependency flow - Direct imports make it obvious where functions come from
  2. Reduced abstraction - Removing the service container layer simplifies the mental model
  3. Consistency - All commands now follow the same pattern
  4. Better testability - Dependency injection via deps parameters makes mocking easier

Testing Concerns

The PR description shows manual testing is incomplete. Before merging, ensure all manual tests pass, especially:

  • vizzly tdd start (daemon mode)
  • vizzly tdd run (one-off mode)
  • vizzly run with --wait (build polling)
  • Error scenarios

Summary

This is a solid refactoring moving in the right direction. Main concerns:

  1. Fix code duplication (critical)
  2. Simplify createTokenStore (critical)
  3. Improve error handling visibility (medium)
  4. Complete manual testing (blocker for merge)

Recommendation: Request changes for items 1 and 2, then approve after fixes and manual testing completion.

Commands now import from functional modules instead of createServices():
- API: import { createApiClient, getBuild } from '../api/index.js'
- Auth: import { createAuthClient, whoami } from '../auth/index.js'
- Test runner: import { runTests } from '../test-runner/index.js'

Added createTokenStore() helper to auth module for commands.

Commands create lightweight inline objects for serverManager/buildManager
instead of using class instances.
@Robdel12 Robdel12 force-pushed the commands-functional-refactor branch from b340169 to b11b71b Compare December 14, 2025 18:57
Removes duplication between run.js and tdd.js by moving the
createServerManager factory to server-manager/index.js where it
belongs with the other server management functions.
The dynamic import was defensive code for a circular dependency that
doesn't exist. global-config.js only imports from node: modules and
output.js - it has no dependency on auth/.
@Robdel12 Robdel12 force-pushed the commands-functional-refactor branch from a8dd071 to a50b567 Compare December 14, 2025 19:14
@Robdel12 Robdel12 enabled auto-merge (squash) December 14, 2025 19:15
@Robdel12 Robdel12 merged commit 811571b into main Dec 14, 2025
17 checks passed
@Robdel12 Robdel12 deleted the commands-functional-refactor branch December 14, 2025 19:15
Robdel12 added a commit that referenced this pull request Dec 14, 2025
## Summary

**Deleted thin wrapper classes** (only used by createServices):
- `api-service.js`, `auth-service.js`, `config-service.js`,
`project-service.js`
- `tdd-service.js` re-export (now imports from `tdd/` directly)
- `BuildManager` class (kept pure functions)
- `html-report-generator.js` and `report-generator/` assets

**Simplified `createServices()`** to only create:
- `testRunner` (EventEmitter for plugin API)
- `serverManager` (plugin API)

**Added:**
- `report-generator/` module with functional operations
- Test helpers in `tests/helpers/`

## Stats

- **Net deletion:** ~700 lines from services layer
- Services layer reduced from 783 → 681 lines

## Migration path (future)

The `testRunner` and `serverManager` classes remain for plugin API
backwards compatibility. Future work:
- Document deprecation, recommend plugins use functional modules
- Remove class wrappers in next major version

## Test plan

- [x] All 56 test files pass (1086 tests)
- [ ] CI passes
- [ ] Plugin API still works

---

**Part 3 of 3** for functional decomposition refactor (VIZ-128)

Depends on #133
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.

1 participant