Lentil Hoffman 50ac2b7b35
refactor: rename Lossless to Hyperview and Lossy to View
- Renamed Lossless class to Hyperview for clarity
- Renamed Lossy class to View for simplicity
- Updated all imports and references throughout the codebase
- Updated documentation to reflect new terminology
- Fixed test files to use new class names
- Added proper exports in index files
2025-07-09 14:28:52 -05:00

144 lines
3.8 KiB
TypeScript

import { describe, test, expect, beforeEach } from '@jest/globals';
import { RhizomeNode, Hyperview, createDelta, CollapsedDelta } from '@src';
import {
CustomResolver,
ResolverPlugin,
DependencyStates
} from '@src/views/resolvers/custom-resolvers';
import { PropertyTypes } from '@src/core/types';
import Debug from 'debug';
const debug = Debug('rz:plugin-lifecycle');
// A simple plugin for testing lifecycle methods
class LifecycleTestPlugin extends ResolverPlugin<LifecycleTestState> {
readonly dependencies = [] as const;
private initialState: LifecycleTestState = {
initialized: true,
updated: false,
resolved: false
};
initialize(): LifecycleTestState {
return { ...this.initialState };
}
update(
currentState: LifecycleTestState,
_newValue: PropertyTypes,
_delta: CollapsedDelta,
_dependencies: DependencyStates
): LifecycleTestState {
return { ...currentState, updated: true };
}
resolve(
state: LifecycleTestState,
_dependencies: DependencyStates
): PropertyTypes {
// Return a valid PropertyTypes value (string, number, boolean, or null)
// We'll use a JSON string representation of the state
return JSON.stringify({ ...state, resolved: true });
}
}
type LifecycleTestState = {
initialized: boolean;
updated: boolean;
resolved: boolean;
};
describe('Plugin Lifecycle', () => {
let node: RhizomeNode;
let hyperview: Hyperview;
beforeEach(() => {
node = new RhizomeNode();
hyperview = new Hyperview(node);
});
test('should call initialize, update, and resolve in order', () => {
const resolver = new CustomResolver(hyperview, {
test: new LifecycleTestPlugin()
});
// Add some data
hyperview.ingestDelta(
createDelta('user1', 'host1')
.withTimestamp(1000)
.setProperty('test1', 'test', 'value1')
.buildV1()
);
const results = resolver.resolve();
expect(results).toBeDefined();
debug(`Results: ${JSON.stringify(results, null, 2)}`)
const entity = results!['test1']
expect(entity).toBeDefined();
// Verify all lifecycle methods were called in the correct order
const testProperty = entity?.properties.test;
expect(testProperty).toBeDefined();
// The resolved value should be the return value from resolve() which is a JSON string
const parsed = JSON.parse(testProperty as string);
expect(parsed).toEqual({
initialized: true,
updated: true,
resolved: true
});
});
test('should handle multiple updates correctly', () => {
const resolver = new CustomResolver(hyperview, {
test: new LifecycleTestPlugin()
});
// First update
hyperview.ingestDelta(
createDelta('user1', 'host1')
.withTimestamp(1000)
.setProperty('test2', 'test', 'value1')
.buildV1()
);
// Second update
hyperview.ingestDelta(
createDelta('user1', 'host1')
.withTimestamp(2000)
.setProperty('test2', 'test', 'value2')
.buildV1()
);
const results = resolver.resolve();
expect(results).toBeDefined();
const entity = results!['test2'];
expect(entity).toBeDefined();
// Verify state after multiple updates
const testProperty = entity?.properties.test;
expect(testProperty).toBeDefined();
// The resolved value should be the return value from resolve() which is a JSON string
const parsed = JSON.parse(testProperty as string);
expect(parsed).toEqual({
initialized: true,
updated: true, // Should be true from the last update
resolved: true
});
});
test('should handle empty state', () => {
const resolver = new CustomResolver(hyperview, {
test: new LifecycleTestPlugin()
});
const results = resolver.resolve();
expect(results).toBeDefined();
expect(results).toMatchObject({});
});
});