notes
This commit is contained in:
parent
4db1ac5ac1
commit
c075639bc6
1 changed files with 240 additions and 0 deletions
240
service-architecture-analysis.md
Normal file
240
service-architecture-analysis.md
Normal file
|
|
@ -0,0 +1,240 @@
|
|||
# Service Architecture Analysis: Is There a Fundamental Problem?
|
||||
|
||||
## The Current Architecture
|
||||
|
||||
### What It Does Well
|
||||
1. **Separation of Concerns**: Each service handles a specific domain (auth, config, model, etc.)
|
||||
2. **Reactive Updates**: EventEmitter pattern allows UI to react to service changes
|
||||
3. **Lazy Loading**: Services load on-demand, improving startup time
|
||||
4. **Dependency Management**: Services can declare dependencies on other services
|
||||
|
||||
### Where It Creates Problems
|
||||
|
||||
## 1. Testing Complexity
|
||||
|
||||
The "mock hell" stems from several architectural decisions:
|
||||
|
||||
### Global Singleton Pattern
|
||||
```typescript
|
||||
// Single global instance
|
||||
import { serviceContainer } from '../services/ServiceContainer.js';
|
||||
```
|
||||
|
||||
**Problem**: Global state makes test isolation nearly impossible. Tests can affect each other.
|
||||
|
||||
**Alternative**: Dependency injection with React Context
|
||||
```typescript
|
||||
// Better approach
|
||||
const ServiceContext = React.createContext<ServiceContainer>();
|
||||
|
||||
// In tests
|
||||
<ServiceContext.Provider value={mockServiceContainer}>
|
||||
<TUIChat />
|
||||
</ServiceContext.Provider>
|
||||
```
|
||||
|
||||
### Event-Based Communication
|
||||
```typescript
|
||||
// Current: Events for everything
|
||||
serviceContainer.on('auth:ready', handler);
|
||||
serviceContainer.on('auth:error', handler);
|
||||
serviceContainer.on('auth:loading', handler);
|
||||
```
|
||||
|
||||
**Problem**: Asynchronous events create timing issues and race conditions in tests.
|
||||
|
||||
**Alternative**: State-based approach
|
||||
```typescript
|
||||
// Better: Synchronous state updates
|
||||
const authState = useServiceState('auth');
|
||||
// Returns { value, loading, error } synchronously
|
||||
```
|
||||
|
||||
### Complex Service Dependencies
|
||||
```typescript
|
||||
// Current: Services depend on each other
|
||||
register('model', modelFactory, ['auth', 'config']);
|
||||
```
|
||||
|
||||
**Problem**: Creates cascading initialization that's hard to mock.
|
||||
|
||||
**Alternative**: Explicit dependencies
|
||||
```typescript
|
||||
// Better: Pass dependencies directly
|
||||
function createModel(auth: AuthService, config: ConfigService) {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
## 2. Architectural Alternatives
|
||||
|
||||
### Option 1: Redux-Style State Management
|
||||
```typescript
|
||||
interface AppState {
|
||||
auth: { status: 'idle' | 'loading' | 'ready' | 'error'; data?: AuthData; error?: Error };
|
||||
config: { status: 'idle' | 'loading' | 'ready' | 'error'; data?: Config; error?: Error };
|
||||
// ...
|
||||
}
|
||||
|
||||
// Predictable, testable state updates
|
||||
dispatch({ type: 'AUTH_LOADED', payload: authData });
|
||||
```
|
||||
|
||||
**Pros**:
|
||||
- Predictable state updates
|
||||
- Easy to test with mock stores
|
||||
- Time-travel debugging
|
||||
|
||||
**Cons**:
|
||||
- More boilerplate
|
||||
- May be overkill for this use case
|
||||
|
||||
### Option 2: React Query / TanStack Query Pattern
|
||||
```typescript
|
||||
function TUIChat() {
|
||||
const auth = useQuery(['auth'], fetchAuth);
|
||||
const config = useQuery(['config'], fetchConfig, {
|
||||
enabled: auth.isSuccess // Dependency handling
|
||||
});
|
||||
|
||||
if (auth.isLoading || config.isLoading) {
|
||||
return <LoadingScreen />;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Pros**:
|
||||
- Built for async data fetching
|
||||
- Excellent caching and invalidation
|
||||
- Easy to test with QueryClient
|
||||
|
||||
**Cons**:
|
||||
- Another dependency
|
||||
- Might not fit all service types
|
||||
|
||||
### Option 3: Simplified Service Layer
|
||||
```typescript
|
||||
// Keep services but make them simpler
|
||||
interface ServiceProvider {
|
||||
auth: AuthService;
|
||||
config: ConfigService;
|
||||
model: ModelService;
|
||||
}
|
||||
|
||||
function TUIChat({ services }: { services: ServiceProvider }) {
|
||||
// Direct service usage, no events
|
||||
}
|
||||
|
||||
// In tests
|
||||
<TUIChat services={mockServices} />
|
||||
```
|
||||
|
||||
**Pros**:
|
||||
- Simple and explicit
|
||||
- Easy to mock
|
||||
- No global state
|
||||
|
||||
**Cons**:
|
||||
- Less reactive
|
||||
- Manual coordination needed
|
||||
|
||||
## 3. The Real Problem: Mixing Concerns
|
||||
|
||||
The fundamental issue might be that the service layer is trying to do too much:
|
||||
|
||||
1. **Data Fetching**: Getting auth tokens, configs, etc.
|
||||
2. **State Management**: Tracking loading/error states
|
||||
3. **Event Broadcasting**: Notifying components of changes
|
||||
4. **Dependency Resolution**: Managing service dependencies
|
||||
5. **Lifecycle Management**: Initialization and cleanup
|
||||
|
||||
This violates the Single Responsibility Principle.
|
||||
|
||||
## 4. A Pragmatic Solution
|
||||
|
||||
Instead of a complete rewrite, consider:
|
||||
|
||||
### 1. Add a Testing Mode
|
||||
```typescript
|
||||
interface TUIChatProps {
|
||||
services?: ServiceProvider; // For testing
|
||||
remoteUrl?: string; // For remote mode
|
||||
// ... other props
|
||||
}
|
||||
|
||||
function TUIChat({ services, remoteUrl, ...props }) {
|
||||
// Use provided services OR real services
|
||||
const realServices = useServices();
|
||||
const activeServices = services || realServices;
|
||||
}
|
||||
```
|
||||
|
||||
### 2. Create Service Facades for Testing
|
||||
```typescript
|
||||
// Testing utilities
|
||||
export function createMockServices(): ServiceProvider {
|
||||
return {
|
||||
auth: { isAuthenticated: true, user: mockUser },
|
||||
config: { config: mockConfig },
|
||||
model: { model: mockModel, llmApi: mockApi }
|
||||
};
|
||||
}
|
||||
|
||||
// In tests
|
||||
<TUIChat services={createMockServices()} />
|
||||
```
|
||||
|
||||
### 3. Keep Remote Mode as Integration Test
|
||||
- Use remote mode for UI behavior tests (what we did)
|
||||
- Add separate service integration tests
|
||||
- Test the full stack in e2e tests
|
||||
|
||||
## 5. Is the Architecture Fundamentally Wrong?
|
||||
|
||||
**Not necessarily wrong, but overcomplicated for testing.**
|
||||
|
||||
The service architecture makes sense for production:
|
||||
- Services need to load asynchronously
|
||||
- They have real dependencies
|
||||
- State changes need to propagate
|
||||
- Error handling is important
|
||||
|
||||
But for testing, we need:
|
||||
- Synchronous, predictable behavior
|
||||
- Easy mocking
|
||||
- Test isolation
|
||||
- Fast execution
|
||||
|
||||
## 6. Recommendations
|
||||
|
||||
### Short Term (What We Did)
|
||||
✅ Use remote mode to bypass complexity
|
||||
✅ Focus on UI behavior testing
|
||||
✅ Accept the trade-off
|
||||
|
||||
### Medium Term
|
||||
1. Add explicit service injection for tests
|
||||
2. Create comprehensive service mocks
|
||||
3. Write separate service unit tests
|
||||
4. Keep using remote mode for UI tests
|
||||
|
||||
### Long Term
|
||||
Consider refactoring to:
|
||||
1. Separate data fetching from state management
|
||||
2. Use React Context for dependency injection
|
||||
3. Replace events with state-based updates
|
||||
4. Make services more testable by design
|
||||
|
||||
## Conclusion
|
||||
|
||||
The service architecture isn't fundamentally wrong—it's just optimized for production use rather than testing. The complexity comes from trying to handle many concerns in one system.
|
||||
|
||||
The "mock hell" is a symptom of:
|
||||
1. Tight coupling between services
|
||||
2. Global state management
|
||||
3. Event-based async communication
|
||||
4. Lack of dependency injection
|
||||
|
||||
Testing local mode is indeed important, but the current architecture makes it expensive. The pragmatic approach (using remote mode) is a reasonable compromise until the architecture can be refactored with testing in mind.
|
||||
|
||||
The lesson: **Design for testability from the start**, not as an afterthought.
|
||||
Loading…
Add table
Reference in a new issue