-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added unit tests and refactored plugin methods
- Loading branch information
Showing
10 changed files
with
250 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
packages/xstate-persistence/src/__tests__/localAgent.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { DataSource } from 'typeorm' | ||
import { createObjects, getConfig } from '@sphereon/ssi-sdk.agent-config' | ||
|
||
jest.setTimeout(60000) | ||
|
||
import xStatePersistenceAgentLogic from './shared/xStatePersistenceAgentLogic' | ||
|
||
let dbConnection: Promise<DataSource> | ||
let agent: any | ||
|
||
const setup = async (): Promise<boolean> => { | ||
const config = await getConfig('packages/event-logger/agent.yml') | ||
const { localAgent, db } = await createObjects(config, { localAgent: '/agent', db: '/dbConnection' }) | ||
agent = localAgent | ||
dbConnection = db | ||
|
||
return true | ||
} | ||
|
||
const tearDown = async (): Promise<boolean> => { | ||
await (await dbConnection).destroy() | ||
return true | ||
} | ||
|
||
const getAgent = () => agent | ||
const testContext = { | ||
getAgent, | ||
setup, | ||
tearDown, | ||
} | ||
|
||
describe('Local integration tests', (): void => { | ||
xStatePersistenceAgentLogic(testContext) | ||
}) |
71 changes: 71 additions & 0 deletions
71
packages/xstate-persistence/src/__tests__/restAgent.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import 'cross-fetch/polyfill' | ||
import {createAgent, IAgent, IAgentOptions} from '@veramo/core' | ||
import {AgentRestClient} from '@veramo/remote-client' | ||
import {AgentRouter, RequestWithAgentRouter} from '@veramo/remote-server' | ||
// @ts-ignore | ||
import express, {Router} from 'express' | ||
import {Server} from 'http' | ||
import {DataSource} from 'typeorm' | ||
import {createObjects, getConfig} from '@sphereon/ssi-sdk.agent-config' | ||
import {IXStatePersistence} from "../index"; | ||
import xStatePersistenceAgentLogic from './shared/xStatePersistenceAgentLogic' | ||
|
||
jest.setTimeout(60000) | ||
|
||
const port = 3002 | ||
const basePath = '/agent' | ||
|
||
let serverAgent: IAgent | ||
let restServer: Server | ||
let dbConnection: Promise<DataSource> | ||
|
||
const getAgent = (options?: IAgentOptions) => | ||
createAgent<IXStatePersistence>({ | ||
...options, | ||
plugins: [ | ||
new AgentRestClient({ | ||
url: 'http://localhost:' + port + basePath, | ||
enabledMethods: serverAgent.availableMethods(), | ||
schema: serverAgent.getSchema(), | ||
}), | ||
], | ||
}) | ||
|
||
const setup = async (): Promise<boolean> => { | ||
const config = await getConfig('packages/event-logger/agent.yml') | ||
const { agent, db } = await createObjects(config, { agent: '/agent', db: '/dbConnection' }) | ||
serverAgent = agent | ||
dbConnection = db | ||
|
||
const agentRouter: Router = AgentRouter({ | ||
exposedMethods: serverAgent.availableMethods(), | ||
}) | ||
|
||
const requestWithAgent: Router = RequestWithAgentRouter({ | ||
agent: serverAgent, | ||
}) | ||
|
||
return new Promise((resolve): void => { | ||
const app = express() | ||
app.use(basePath, requestWithAgent, agentRouter) | ||
restServer = app.listen(port, () => { | ||
resolve(true) | ||
}) | ||
}) | ||
} | ||
|
||
const tearDown = async (): Promise<boolean> => { | ||
restServer.close() | ||
await (await dbConnection).destroy() | ||
return true | ||
} | ||
|
||
const testContext = { | ||
getAgent, | ||
setup, | ||
tearDown, | ||
} | ||
|
||
describe('REST integration tests', (): void => { | ||
xStatePersistenceAgentLogic(testContext) | ||
}) |
85 changes: 85 additions & 0 deletions
85
packages/xstate-persistence/src/__tests__/shared/xStatePersistenceAgentLogic.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import {NonPersistedXStateStoreEvent, State} from "@sphereon/ssi-sdk.data-store"; | ||
import {TAgent} from '@veramo/core' | ||
import {IXStatePersistence, SQLDialect} from '../../index' | ||
|
||
type ConfiguredAgent = TAgent<IXStatePersistence> | ||
|
||
export default (testContext: { getAgent: () => ConfiguredAgent; setup: () => Promise<boolean>; tearDown: () => Promise<boolean> }): void => { | ||
describe('Event Logger Agent Plugin', (): void => { | ||
let agent: ConfiguredAgent | ||
|
||
beforeAll(async (): Promise<void> => { | ||
await testContext.setup() | ||
agent = testContext.getAgent() | ||
}) | ||
|
||
afterAll(testContext.tearDown) | ||
|
||
it('should store xstate event', async (): Promise<void> => { | ||
const xstateEvent: NonPersistedXStateStoreEvent = { | ||
state: 'test_state', | ||
type: 'b40b8474-58a2-4b23-9fde-bd6ee1902cdb', | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
completedAt: new Date(), | ||
tenantId: 'test_tenant_id' | ||
} | ||
|
||
const savedXStoreEvent: State = await agent.persistState(xstateEvent) | ||
expect(savedXStoreEvent).toBeDefined() | ||
}) | ||
|
||
it('should retrieve an xstate event', async (): Promise<void> => { | ||
const xstateEvent: NonPersistedXStateStoreEvent = { | ||
state: 'test_state', | ||
type: 'b40b8474-58a2-4b23-9fde-bd6ee1902cdb', | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
completedAt: new Date(), | ||
tenantId: 'test_tenant_id' | ||
} | ||
|
||
const savedXStoreEvent: State = await agent.persistState({...xstateEvent}) | ||
expect(savedXStoreEvent).toBeDefined() | ||
|
||
const result: State = await agent.loadState({ type: savedXStoreEvent.type }) | ||
expect(result).toBeDefined() | ||
}) | ||
|
||
it('should delete the expired records', async () => { | ||
const now = new Date() | ||
const newestXstateEvent: NonPersistedXStateStoreEvent = { | ||
state: 'test_state', | ||
type: 'test_type_1', | ||
createdAt: now, | ||
completedAt: new Date(), | ||
tenantId: 'test_tenant_id' | ||
} | ||
const middleXstateEvent: NonPersistedXStateStoreEvent = { | ||
state: 'test_state', | ||
type: 'test_type_2', | ||
createdAt: new Date(+now - 30000), | ||
completedAt: new Date(), | ||
tenantId: 'test_tenant_id' | ||
} | ||
|
||
const oldestXstateEvent: NonPersistedXStateStoreEvent = { | ||
state: 'test_state', | ||
type: 'test_type_3', | ||
createdAt: new Date(+now - 60000), | ||
completedAt: new Date(), | ||
tenantId: 'test_tenant_id' | ||
} | ||
|
||
await agent.persistState(newestXstateEvent) | ||
await agent.persistState(middleXstateEvent) | ||
await agent.persistState(oldestXstateEvent) | ||
|
||
await agent.deleteExpiredStates({ duration: 40000, dialect: SQLDialect.SQLite3 }) | ||
|
||
await expect(agent.loadState({ type: 'test_type_1'})).resolves.toBeDefined() | ||
await expect(agent.loadState({ type: 'test_type_2'})).resolves.toBeDefined() | ||
await expect(agent.loadState({ type: 'test_type_3'})).rejects.toEqual(Error('No state found for type: test_type_3')) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.