-
Notifications
You must be signed in to change notification settings - Fork 0
Closed
Description
Problem
SchemaManager cannot be used with MockAppSheetClient for testing because:
- SchemaManager constructor only accepts
SchemaConfig, not a pre-configured ConnectionManager - ConnectionManager.register() only accepts
ConnectionConfigand always creates a newAppSheetClientinternally
This makes it impossible to inject a MockAppSheetClient for unit/integration testing while using the SchemaManager API with its automatic validation features.
Current API (v2.1.0)
// SchemaManager creates its own ConnectionManager internally
const schemaManager = new SchemaManager(schema);
// ConnectionManager only accepts config, not a ready client
connectionManager.register(config: ConnectionConfig); // Always creates new AppSheetClientProposed API
Option 1: SchemaManager accepts optional ConnectionManager
export class SchemaManager {
constructor(schema: SchemaConfig, connectionManager?: ConnectionManager) {
this.schema = schema;
this.connectionManager = connectionManager || new ConnectionManager();
if (!connectionManager) {
this.initialize(); // Only auto-initialize if no ConnectionManager provided
}
}
}Option 2: ConnectionManager.registerClient() method
export class ConnectionManager {
// Existing method - creates new client from config
register(config: ConnectionConfig): void;
// NEW method - accepts pre-created client
registerClient(name: string, client: AppSheetClientInterface): void {
if (this.connections.has(name)) {
throw new Error(`Connection "${name}" is already registered`);
}
this.connections.set(name, client);
}
}Use Case: TSyringe DI with Mock Support
// container.base.ts
export function registerSchemaManager(c: DependencyContainer): void {
c.register(TOKENS.SchemaManager, {
useFactory: (container) => {
const client = container.resolve(TOKENS.AppSheetClient); // Real or Mock
const schema = SchemaLoader.fromJson('config/appsheet-schema.json');
const connectionManager = new ConnectionManager();
connectionManager.registerClient('default', client); // ← NEW API
return new SchemaManager(schema, connectionManager); // ← NEW API
}
});
}
// container.development.ts - Uses MockAppSheetClient
registerMockAppSheetClient(container);
registerSchemaManager(container); // SchemaManager uses MockAppSheetClient
// container.production.ts - Uses real AppSheetClient
registerRealAppSheetClient(container);
registerSchemaManager(container); // SchemaManager uses real AppSheetClientBenefits
- Testability: Unit/integration tests can use MockAppSheetClient with full SchemaManager features
- DI Support: Works with TSyringe, InversifyJS, and other DI containers
- Validation: DynamicTable runtime validation still works (the core value of SchemaManager)
- Backward Compatible: Existing code using
new SchemaManager(schema)continues to work
Related
- This is a follow-up to Issue Feature Request: Add per-request email support to DynamicTable API #3 (Per-request user context)
- Enables full adoption of SchemaManager API in projects using DI patterns
Metadata
Metadata
Assignees
Labels
No labels