Skip to content

Commit

Permalink
adding methods for testing actions/data objects directly
Browse files Browse the repository at this point in the history
  • Loading branch information
ralphv committed Aug 14, 2024
1 parent 214f63c commit 02e8940
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 11 deletions.
23 changes: 17 additions & 6 deletions src/GallifreyRulesEngine.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { BaseActionPayload, BaseActionResponse, BaseEventPayload } from './base-interfaces/BaseTypes';
import {
BaseActionPayload,
BaseActionResponse,
BaseDataObjectRequest,
BaseDataObjectResponse,
BaseEventPayload,
} from './base-interfaces/BaseTypes';
import CriticalError from './errors/CriticalError';
import WarningError from './errors/WarningError';
import InfoError from './errors/InfoError';
Expand Down Expand Up @@ -618,16 +624,19 @@ export class GallifreyRulesEngine {
}
}

private async pullDataObject(
protected async pullDataObject<
DataObjectRequestType extends BaseDataObjectRequest,
DataObjectResponseType extends BaseDataObjectResponse,
>(
event: GallifreyEventTypeInternal<any>,
engineEventContext: EngineEventContext,
dataObjectName: string,
request?: any,
request: DataObjectRequestType,
) {
logger.info(`pullDataObject: ${dataObjectName}`);

if (this.isPullDataObjectHookAttached()) {
return await this.callPullDataObject(dataObjectName, request);
return (await this.callPullDataObject(dataObjectName, request)) as DataObjectResponseType;
}

const eventStoreKey = this.getDataObjectEventStoreKey(dataObjectName, request);
Expand All @@ -648,7 +657,9 @@ export class GallifreyRulesEngine {
this.schemaLoader.getEventLevelConfig(event.entityName, event.eventName),
);

const [dataObjectInstance] = await this.instancesFactory.getModulesInstances<DataObjectInterface<any, any>>(
const [dataObjectInstance] = await this.instancesFactory.getModulesInstances<
DataObjectInterface<DataObjectRequestType, DataObjectResponseType>
>(
engineEventContext,
configAccessor,
[moduleData],
Expand All @@ -658,7 +669,7 @@ export class GallifreyRulesEngine {
);
logger.debug(`Fetched data object module instance: ${dataObjectName}`);

const engineDataObject = new EngineDataObject(
const engineDataObject = new EngineDataObject<DataObjectRequestType>(
configAccessor,
engineEventContext,
`data object - ${dataObjectName}`,
Expand Down
55 changes: 51 additions & 4 deletions src/GallifreyRulesEngineForTesting.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { GallifreyRulesEngine } from './GallifreyRulesEngine';
import { AssertNotNull } from './lib/Utils';
import { GallifreyEventType } from './GallifreyEventType';
import {
BaseActionPayload,
BaseActionResponse,
BaseDataObjectRequest,
BaseDataObjectResponse,
} from './base-interfaces/BaseTypes';

/**
* Allows for easier testing
Expand Down Expand Up @@ -31,11 +37,14 @@ export class GallifreyRulesEngineForTesting extends GallifreyRulesEngine {
return await AssertNotNull(this.pullDataObjectHook)(dataObjectName, request);
}

public async testDoAction(
public async testDoAction<
ActionPayloadType extends BaseActionPayload,
ActionResponseType extends BaseActionResponse,
>(
actionName: string,
request: any,
request: ActionPayloadType,
event: GallifreyEventType<any> | undefined = undefined,
): Promise<any> {
): Promise<ActionResponseType> {
if (event === undefined) {
event = {
entityName: 'entity',
Expand All @@ -57,6 +66,44 @@ export class GallifreyRulesEngineForTesting extends GallifreyRulesEngine {
event.eventId,
event.source,
);
return await this.doAction<any, any>(internalEvent, engineEventContext, actionName, request);
return await this.doAction<ActionPayloadType, ActionResponseType>(
internalEvent,
engineEventContext,
actionName,
request,
);
}

public async testPullDataObject<
DataObjectRequestType extends BaseDataObjectRequest,
DataObjectResponseType extends BaseDataObjectResponse,
>(dataObjectName: string, request: any, event: GallifreyEventType<any> | undefined = undefined) {
if (event === undefined) {
event = {
entityName: 'entity',
eventName: 'event',
source: 'source',
eventId: '1',
payload: {},
eventLag: 0,
};
}
const namespace = AssertNotNull(this.getNamespace());
const internalEvent = {
...event,
namespace,
};
const engineEventContext = await this.createEngineEventContext(
internalEvent.entityName,
internalEvent.eventName,
event.eventId,
event.source,
);
return await this.pullDataObject<DataObjectRequestType, DataObjectResponseType>(
internalEvent,
engineEventContext,
dataObjectName,
request,
);
}
}
8 changes: 8 additions & 0 deletions tests/unit/for-testing/modules/TestDataObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { DataObjectInterface, EngineDataObjectInterface, GallifreyPlugin, PluginType } from '../../../../src';

@GallifreyPlugin(PluginType.DataObject)
export default class TestDataObject implements DataObjectInterface<{ key: string }, string> {
async get(engine: EngineDataObjectInterface<{ key: string }>): Promise<string> {
return `sample-action: ${engine.getRequest().key}`;
}
}
13 changes: 12 additions & 1 deletion tests/unit/for-testing/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { GallifreyRulesEngineForTesting, NamespaceSchema } from '../../../src';
import TestAction from './modules/TestAction';
import { expect } from 'chai';
import path from 'node:path';
import TestDataObject from './modules/TestDataObject';

describe('for-testing', () => {
it('AssertNotNull', async () => {
it('testDoAction', async () => {
const engine = new GallifreyRulesEngineForTesting();
await engine.initialize({
$namespace: 'test-namespace',
Expand All @@ -14,4 +15,14 @@ describe('for-testing', () => {
const result = await engine.testDoAction(TestAction.name, { key: 'for-testing' });
expect(result).equal('sample-action: for-testing');
});
it('testPullDataObject', async () => {
const engine = new GallifreyRulesEngineForTesting();
await engine.initialize({
$namespace: 'test-namespace',
$entities: {},
$modulesPaths: ['$', path.join(__dirname, './modules')],
} as NamespaceSchema);
const result = await engine.testPullDataObject(TestDataObject.name, { key: 'for-testing' });
expect(result).equal('sample-action: for-testing');
});
});

0 comments on commit 02e8940

Please sign in to comment.