Skip to content

Commit

Permalink
increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
LiranCohen committed May 8, 2024
1 parent f92479b commit df0ebe3
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions packages/agent/tests/dwn-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,31 @@ describe('AgentDwnApi', () => {
expect(writeReply.status.code).to.equal(202);
});

it('returns a 202 Accepted status when the request is not stored', async () => {
// spy on dwn.processMessage
const processMessageSpy = sinon.spy(testHarness.agent.dwn, 'processMessage');

// Attempt to process the RecordsWrite
const dataBytes = Convert.string('Hello, world!').toUint8Array();
let writeResponse = await testHarness.agent.dwn.processRequest({
author : alice.did.uri,
target : alice.did.uri,
messageType : DwnInterface.RecordsWrite,
messageParams : {
dataFormat: 'text/plain'
},
dataStream: new Blob([dataBytes])
});

// Verify the response.
expect(writeResponse).to.have.property('message');
expect(writeResponse.reply.status.code).to.equal(202);
expect(writeResponse.reply.status.detail).to.equal('Accepted');

// dwnProcessMessage should not have been called
expect(processMessageSpy.called).to.be.false;
});

it('handles RecordsWrite messages to sign as owner', async () => {
// bob authors a public record to his dwn
const dataStream = new Blob([ Convert.string('Hello, world!').toUint8Array() ]);
Expand Down Expand Up @@ -1502,6 +1527,29 @@ describe('AgentDwnApi', () => {
}
});

it('throws an error if sendDwnRequest fails', async () => {
// stub sendDwnRequest to reject with an error
sinon.stub(testHarness.agent.rpc, 'sendDwnRequest').rejects(new Error('sendDwnRequest Error'));

try {
await testHarness.agent.dwn.sendRequest({
author : alice.did.uri,
target : alice.did.uri,
messageType : DwnInterface.RecordsQuery,
messageParams : {
filter: {
schema: 'https://schemas.xyz/example'
}
},
});
expect.fail('Expected an error to be thrown');

} catch (error: any) {
expect(error.message).to.include('Failed to send DWN RPC request');
expect(error.message).to.include('sendDwnRequest Error');
}
});

it('throws an error if target DID method is not supported by the Agent DID Resolver', async () => {
try {
await testHarness.agent.dwn.sendRequest({
Expand Down

0 comments on commit df0ebe3

Please sign in to comment.