Skip to content

Commit

Permalink
refactor: api to modules
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Glastra <timo@animo.id>
  • Loading branch information
TimoGlastra committed Sep 8, 2022
1 parent 69483e5 commit 2b4a740
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 33 deletions.
6 changes: 3 additions & 3 deletions packages/core/src/agent/BaseAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export abstract class BaseAgent<AgentModules extends ModulesMap = EmptyModuleMap
public readonly wallet: WalletApi
public readonly oob: OutOfBandApi

public readonly api: AgentApi<WithoutDefaultModules<AgentModules>>
public readonly modules: AgentApi<WithoutDefaultModules<AgentModules>>

public constructor(agentConfig: AgentConfig, dependencyManager: DependencyManager) {
this.dependencyManager = dependencyManager
Expand Down Expand Up @@ -111,8 +111,8 @@ export abstract class BaseAgent<AgentModules extends ModulesMap = EmptyModuleMap
this.oob,
]

// Set the api of the registered modules on the agent
this.api = getAgentApi(this.dependencyManager, defaultApis)
// Set the api of the registered modules on the agent, excluding the default apis
this.modules = getAgentApi(this.dependencyManager, defaultApis)
}

public get isInitialized() {
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/agent/__tests__/Agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('Agent', () => {
test('does not return default modules on modules key if no modules were provided', () => {
const agent = new Agent(agentOptions)

expect(agent.api).toEqual({})
expect(agent.modules).toEqual({})
})

test('registers custom and default modules if custom modules are provided', () => {
Expand All @@ -70,8 +70,8 @@ describe('Agent', () => {
},
})

expect(agent.api.myModule.myModuleMethod).toBe(myModuleMethod)
expect(agent.api).toEqual({
expect(agent.modules.myModule.myModuleMethod).toBe(myModuleMethod)
expect(agent.modules).toEqual({
myModule: expect.any(MyApi),
})
})
Expand All @@ -89,7 +89,7 @@ describe('Agent', () => {

// Should be custom module config property, not the default value
expect(agent.mediationRecipient.config.maximumMessagePickup).toBe(42)
expect(agent.api).toEqual({
expect(agent.modules).toEqual({
myModule: expect.any(MyApi),
})
})
Expand Down
8 changes: 4 additions & 4 deletions packages/module-tenants/tests/tenant-sessions.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('Tenants Sessions E2E', () => {
test('create 100 sessions in parallel for the same tenant and close them', async () => {
const numberOfSessions = 100

const tenantRecord = await agent.api.tenants.createTenant({
const tenantRecord = await agent.modules.tenants.createTenant({
config: {
label: 'Agent 1 Tenant 1',
},
Expand All @@ -51,7 +51,7 @@ describe('Tenants Sessions E2E', () => {
const tenantAgentPromises = []

for (let session = 0; session < numberOfSessions; session++) {
tenantAgentPromises.push(agent.api.tenants.getTenantAgent({ tenantId: tenantRecord.id }))
tenantAgentPromises.push(agent.modules.tenants.getTenantAgent({ tenantId: tenantRecord.id }))
}

const tenantAgents = await Promise.all(tenantAgentPromises)
Expand All @@ -65,7 +65,7 @@ describe('Tenants Sessions E2E', () => {

const tenantRecordPromises = []
for (let tenantNo = 0; tenantNo < numberOfTenants; tenantNo++) {
const tenantRecord = agent.api.tenants.createTenant({
const tenantRecord = agent.modules.tenants.createTenant({
config: {
label: 'Agent 1 Tenant 1',
},
Expand All @@ -79,7 +79,7 @@ describe('Tenants Sessions E2E', () => {
const tenantAgentPromises = []
for (const tenantRecord of tenantRecords) {
for (let session = 0; session < numberOfSessions; session++) {
tenantAgentPromises.push(agent.api.tenants.getTenantAgent({ tenantId: tenantRecord.id }))
tenantAgentPromises.push(agent.modules.tenants.getTenantAgent({ tenantId: tenantRecord.id }))
}
}

Expand Down
40 changes: 20 additions & 20 deletions packages/module-tenants/tests/tenants.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,48 +85,48 @@ describe('Tenants E2E', () => {

test('create get and delete a tenant', async () => {
// Create tenant
let tenantRecord1 = await agent1.api.tenants.createTenant({
let tenantRecord1 = await agent1.modules.tenants.createTenant({
config: {
label: 'Tenant 1',
},
})

// Retrieve tenant record from storage
tenantRecord1 = await agent1.api.tenants.getTenantById(tenantRecord1.id)
tenantRecord1 = await agent1.modules.tenants.getTenantById(tenantRecord1.id)

// Get tenant agent
const tenantAgent = await agent1.api.tenants.getTenantAgent({
const tenantAgent = await agent1.modules.tenants.getTenantAgent({
tenantId: tenantRecord1.id,
})
await tenantAgent.endSession()

// Delete tenant agent
await agent1.api.tenants.deleteTenantById(tenantRecord1.id)
await agent1.modules.tenants.deleteTenantById(tenantRecord1.id)

// Can not get tenant agent again
await expect(agent1.api.tenants.getTenantAgent({ tenantId: tenantRecord1.id })).rejects.toThrow(
await expect(agent1.modules.tenants.getTenantAgent({ tenantId: tenantRecord1.id })).rejects.toThrow(
`TenantRecord: record with id ${tenantRecord1.id} not found.`
)
})

test('create a connection between two tenants within the same agent', async () => {
// Create tenants
const tenantRecord1 = await agent1.api.tenants.createTenant({
const tenantRecord1 = await agent1.modules.tenants.createTenant({
config: {
label: 'Tenant 1',
},
})
const tenantRecord2 = await agent1.api.tenants.createTenant({
const tenantRecord2 = await agent1.modules.tenants.createTenant({
config: {
label: 'Tenant 2',
},
})

const tenantAgent1 = await agent1.api.tenants.getTenantAgent({
const tenantAgent1 = await agent1.modules.tenants.getTenantAgent({
tenantId: tenantRecord1.id,
})

const tenantAgent2 = await agent1.api.tenants.getTenantAgent({
const tenantAgent2 = await agent1.modules.tenants.getTenantAgent({
tenantId: tenantRecord2.id,
})

Expand Down Expand Up @@ -157,27 +157,27 @@ describe('Tenants E2E', () => {
await tenantAgent2.endSession()

// Delete tenants (will also delete wallets)
await agent1.api.tenants.deleteTenantById(tenantAgent1.context.contextCorrelationId)
await agent1.api.tenants.deleteTenantById(tenantAgent2.context.contextCorrelationId)
await agent1.modules.tenants.deleteTenantById(tenantAgent1.context.contextCorrelationId)
await agent1.modules.tenants.deleteTenantById(tenantAgent2.context.contextCorrelationId)
})

test('create a connection between two tenants within different agents', async () => {
// Create tenants
const tenantRecord1 = await agent1.api.tenants.createTenant({
const tenantRecord1 = await agent1.modules.tenants.createTenant({
config: {
label: 'Agent 1 Tenant 1',
},
})
const tenantAgent1 = await agent1.api.tenants.getTenantAgent({
const tenantAgent1 = await agent1.modules.tenants.getTenantAgent({
tenantId: tenantRecord1.id,
})

const tenantRecord2 = await agent2.api.tenants.createTenant({
const tenantRecord2 = await agent2.modules.tenants.createTenant({
config: {
label: 'Agent 2 Tenant 1',
},
})
const tenantAgent2 = await agent2.api.tenants.getTenantAgent({
const tenantAgent2 = await agent2.modules.tenants.getTenantAgent({
tenantId: tenantRecord2.id,
})

Expand All @@ -198,25 +198,25 @@ describe('Tenants E2E', () => {
await tenantAgent2.endSession()

// Delete tenants (will also delete wallets)
await agent1.api.tenants.deleteTenantById(tenantRecord1.id)
await agent2.api.tenants.deleteTenantById(tenantRecord2.id)
await agent1.modules.tenants.deleteTenantById(tenantRecord1.id)
await agent2.modules.tenants.deleteTenantById(tenantRecord2.id)
})

test('perform actions within the callback of withTenantAgent', async () => {
const tenantRecord = await agent1.api.tenants.createTenant({
const tenantRecord = await agent1.modules.tenants.createTenant({
config: {
label: 'Agent 1 Tenant 1',
},
})

await agent1.api.tenants.withTenantAgent({ tenantId: tenantRecord.id }, async (tenantAgent) => {
await agent1.modules.tenants.withTenantAgent({ tenantId: tenantRecord.id }, async (tenantAgent) => {
const outOfBandRecord = await tenantAgent.oob.createInvitation()

expect(outOfBandRecord).toBeInstanceOf(OutOfBandRecord)
expect(tenantAgent.context.contextCorrelationId).toBe(tenantRecord.id)
expect(tenantAgent.config.label).toBe('Agent 1 Tenant 1')
})

await agent1.api.tenants.deleteTenantById(tenantRecord.id)
await agent1.modules.tenants.deleteTenantById(tenantRecord.id)
})
})
2 changes: 1 addition & 1 deletion samples/extension-module/requester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const run = async () => {
.subscribe(subject)

// Send a dummy request and wait for response
const record = await agent.api.dummy.request(connectionRecord.id)
const record = await agent.modules.dummy.request(connectionRecord.id)
agent.config.logger.info(`Request received for Dummy Record: ${record.id}`)

const dummyRecord = await firstValueFrom(subject)
Expand Down
2 changes: 1 addition & 1 deletion samples/extension-module/responder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const run = async () => {
// Subscribe to dummy record events
agent.events.on(DummyEventTypes.StateChanged, async (event: DummyStateChangedEvent) => {
if (event.payload.dummyRecord.state === DummyState.RequestReceived) {
await agent.api.dummy.respond(event.payload.dummyRecord.id)
await agent.modules.dummy.respond(event.payload.dummyRecord.id)
}
})

Expand Down

0 comments on commit 2b4a740

Please sign in to comment.