Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: use event listener instead of while loop #778

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 16 additions & 20 deletions packages/core/tests/oob.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { OutOfBandInvitation } from '../src/modules/oob/messages'
import { sleep } from '../src/utils/sleep'

import { TestMessage } from './TestMessage'
import { getBaseConfig, prepareForIssuance } from './helpers'
import { getBaseConfig, prepareForIssuance, waitForCredentialRecord } from './helpers'

import {
AgentEventTypes,
Expand Down Expand Up @@ -340,17 +340,14 @@ describe('out of band', () => {

const urlMessage = outOfBandInvitation.toUrl({ domain: 'http://example.com' })

const aliceCredentialRecordPromise = waitForCredentialRecord(aliceAgent, {
state: CredentialState.OfferReceived,
threadId: message.threadId,
})
await aliceAgent.oob.receiveInvitationFromUrl(urlMessage, receiveInvitationConfig)

let credentials: CredentialExchangeRecord[] = []
while (credentials.length < 1) {
credentials = await aliceAgent.credentials.getAll()
await sleep(100)
}

expect(credentials).toHaveLength(1)
const [credential] = credentials
expect(credential.state).toBe(CredentialState.OfferReceived)
const aliceCredentialRecord = await aliceCredentialRecordPromise
expect(aliceCredentialRecord.state).toBe(CredentialState.OfferReceived)
})

test('do not process requests when a connection is not ready', async () => {
Expand Down Expand Up @@ -389,6 +386,13 @@ describe('out of band', () => {
}
)

const aliceCredentialRecordPromise = waitForCredentialRecord(aliceAgent, {
state: CredentialState.OfferReceived,
threadId: message.threadId,
// We need to create the connection beforehand so it can take a while to complete
timeoutMs: 20000,
})

// Accept connection invitation
let { connectionRecord: aliceFaberConnection } = await aliceAgent.oob.acceptInvitation(
aliceFaberOutOfBandRecord.id,
Expand All @@ -406,16 +410,8 @@ describe('out of band', () => {
expect(faberAliceConnection).toBeConnectedWith(aliceFaberConnection)
expect(aliceFaberConnection).toBeConnectedWith(faberAliceConnection)

// The credential should be processed when connection is made. It asynchronous so it can take a moment.
let credentials: CredentialExchangeRecord[] = []
while (credentials.length < 1) {
credentials = await aliceAgent.credentials.getAll()
await sleep(100)
}

expect(credentials).toHaveLength(1)
const [credential] = credentials
expect(credential.state).toBe(CredentialState.OfferReceived)
const aliceCredentialRecord = await aliceCredentialRecordPromise
expect(aliceCredentialRecord.state).toBe(CredentialState.OfferReceived)
})

test('do not create a new connection when no messages and handshake reuse succeeds', async () => {
Expand Down