Lentil Hoffman d7c4fda93e
refactor(resolver): overhaul plugin system and dependency handling
Core Changes:
- Completely rewrote CustomResolver reducer with dependency-ordered processing
- Enhanced plugin initialization with proper dependency injection
- Improved delta processing and property value tracking
- Added robust error handling for duplicate property IDs

Resolver Improvements:
- Updated to use new accumulator structure
- Implemented execution order processing for plugins
- Enhanced debug logging and error reporting
- Simplified TimestampResolver by removing unused initializer

Configuration Updates:
- Added TypeScript path aliases for test helpers
- Improved module resolution paths

Key Benefits:
- More robust plugin dependency management
- More efficient state updates
- Enhanced type safety
- Better error messages and debugging
- More consistent plugin initialization

This refactoring focuses on improving the robustness of the resolver,
especially around plugin lifecycle management and dependency handling.
The changes ensure better separation of concerns and more predictable
behavior when dealing with complex plugin dependencies.
2025-06-25 06:10:34 -05:00

95 lines
2.8 KiB
TypeScript

import { describe, test, expect, beforeEach } from '@jest/globals';
import { RhizomeNode, Lossless } from '@src';
import { CollapsedDelta } from '@src/views/lossless';
import { CustomResolver, ResolverPlugin } from '@src/views/resolvers/custom-resolvers';
type PropertyTypes = string | number | boolean | null;
describe('Circular Dependency Detection', () => {
let node: RhizomeNode;
let lossless: Lossless;
beforeEach(() => {
node = new RhizomeNode();
lossless = new Lossless(node);
});
test('should detect circular dependencies', () => {
// PluginA depends on PluginB
class PluginA implements ResolverPlugin<{ value: string }, string> {
readonly dependencies = ['b'] as const;
initialize() {
return { value: '' };
}
update(currentState: { value: string }, newValue: PropertyTypes, _delta: CollapsedDelta, _dependencies: { b: string }) {
return { value: String(newValue) };
}
resolve(_state: { value: string }) {
return 'a';
}
}
// PluginB depends on PluginA (circular dependency)
class PluginB implements ResolverPlugin<{ value: string }, string> {
readonly dependencies = ['a'] as const;
initialize() {
return { value: '' };
}
update(_currentState: { value: string }, newValue: PropertyTypes, _delta: CollapsedDelta, _dependencies: { a: string }) {
return { value: String(newValue) };
}
resolve(_state: { value: string }) {
return 'b';
}
}
// Should throw an error when circular dependencies are detected
expect(() => {
new CustomResolver(lossless, {
'a': new PluginA(),
'b': new PluginB()
});
}).toThrow('Circular dependency detected in plugin dependencies');
});
test('should detect longer circular dependency chains', () => {
class PluginA implements ResolverPlugin<{ value: string }, string> {
readonly dependencies = ['c'] as const;
initialize() { return { value: '' }; }
update() { return { value: '' }; }
resolve() { return 'a'; }
}
class PluginB implements ResolverPlugin<{ value: string }, string> {
readonly dependencies = ['a'] as const;
initialize() { return { value: '' }; }
update() { return { value: '' }; }
resolve() { return 'b'; }
}
class PluginC implements ResolverPlugin<{ value: string }, string> {
readonly dependencies = ['b'] as const;
initialize() { return { value: '' }; }
update() { return { value: '' }; }
resolve() { return 'c'; }
}
// Should detect the circular dependency: a -> c -> b -> a
expect(() => {
new CustomResolver(lossless, {
'a': new PluginA(),
'b': new PluginB(),
'c': new PluginC()
});
}).toThrow('Circular dependency detected in plugin dependencies');
});
});