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.
132 lines
4.0 KiB
TypeScript
132 lines
4.0 KiB
TypeScript
import { describe, test, expect, beforeEach } from '@jest/globals';
|
|
import { RhizomeNode } from '@src';
|
|
import { Lossless } from '@src/views/lossless';
|
|
import { CustomResolver } from '@src/views/resolvers/custom-resolvers';
|
|
import { ResolverPlugin } from '@src/views/resolvers/custom-resolvers/plugin';
|
|
// import Debug from 'debug';
|
|
|
|
// const debug = Debug('rz:test:resolver');
|
|
|
|
// Mock plugins for testing
|
|
class TestPlugin implements ResolverPlugin<unknown, string> {
|
|
name: string;
|
|
dependencies: readonly string[];
|
|
|
|
constructor(name: string, dependencies: string[] = []) {
|
|
this.name = name;
|
|
this.dependencies = dependencies;
|
|
}
|
|
|
|
initialize() { return {}; }
|
|
update() { return {}; }
|
|
resolve() { return 'test'; }
|
|
}
|
|
|
|
describe('CustomResolver', () => {
|
|
let node: RhizomeNode;
|
|
let lossless: Lossless;
|
|
|
|
beforeEach(() => {
|
|
node = new RhizomeNode();
|
|
lossless = new Lossless(node);
|
|
});
|
|
|
|
describe('buildDependencyGraph', () => {
|
|
test('should build a simple dependency graph', () => {
|
|
// Arrange
|
|
const plugins = {
|
|
a: new TestPlugin('a'),
|
|
b: new TestPlugin('b', ['a']), // b depends on a
|
|
c: new TestPlugin('c', ['b']) // c depends on b
|
|
};
|
|
|
|
// Act
|
|
const resolver = new CustomResolver(lossless, plugins);
|
|
|
|
const graph = resolver.dependencyGraph;
|
|
|
|
// Assert
|
|
expect(graph.get('a')).toBeDefined();
|
|
expect(graph.get('b')).toBeDefined();
|
|
expect(graph.get('c')).toBeDefined();
|
|
|
|
// Check dependencies
|
|
expect(Array.from(graph.get('a') || [])).toContain('b'); // a -> b
|
|
expect(Array.from(graph.get('b') || [])).toContain('c'); // b -> c
|
|
});
|
|
|
|
test('should handle plugins with same basename but different keys', () => {
|
|
// Arrange
|
|
const plugins = {
|
|
'plugin:a': new TestPlugin('a'),
|
|
'plugin:b': new TestPlugin('b', ['a']), // depends on a
|
|
'another:b': new TestPlugin('b', ['a']) // same basename, different key
|
|
};
|
|
|
|
// Act
|
|
const resolver = new CustomResolver(lossless, plugins);
|
|
|
|
// Access private method for testing
|
|
const graph = resolver.dependencyGraph;
|
|
|
|
// Assert
|
|
expect(graph.get('a')).toBeDefined();
|
|
expect(graph.get('b')).toBeDefined();
|
|
|
|
// Both 'plugin:b' and 'another:b' should be in the graph as 'b'
|
|
expect(Array.from(graph.get('a') || [])).toContain('b');
|
|
});
|
|
|
|
test('should throw error for missing dependency', () => {
|
|
// Arrange
|
|
const plugins = {
|
|
a: new TestPlugin('a', ['nonexistent']) // depends on non-existent plugin
|
|
};
|
|
|
|
// Act & Assert
|
|
expect(() => {
|
|
new CustomResolver(lossless, plugins);
|
|
}).toThrow('Dependency nonexistent not found for plugin a');
|
|
});
|
|
|
|
test('should handle plugins with no dependencies', () => {
|
|
// Arrange
|
|
const plugins = {
|
|
a: new TestPlugin('a'),
|
|
b: new TestPlugin('b'),
|
|
c: new TestPlugin('c')
|
|
};
|
|
|
|
// Act
|
|
const resolver = new CustomResolver(lossless, plugins);
|
|
|
|
// Access private method for testing
|
|
const graph = resolver.dependencyGraph;
|
|
|
|
// Assert
|
|
expect(graph.get('a')).toBeDefined();
|
|
expect(graph.get('b')).toBeDefined();
|
|
expect(graph.get('c')).toBeDefined();
|
|
|
|
// No dependencies should be set
|
|
expect(Array.from(graph.get('a') || [])).toHaveLength(0);
|
|
expect(Array.from(graph.get('b') || [])).toHaveLength(0);
|
|
expect(Array.from(graph.get('c') || [])).toHaveLength(0);
|
|
});
|
|
|
|
test('should detect circular dependencies', () => {
|
|
// Arrange
|
|
const plugins = {
|
|
a: new TestPlugin('a', ['c']), // a depends on c
|
|
b: new TestPlugin('b', ['a']), // b depends on a
|
|
c: new TestPlugin('c', ['b']) // c depends on b (circular)
|
|
};
|
|
|
|
// Act & Assert
|
|
expect(() => {
|
|
new CustomResolver(lossless, plugins);
|
|
}).toThrow('Circular dependency detected in plugin dependencies');
|
|
});
|
|
});
|
|
});
|