From cddb00713ce7b09b3f18acdaae559703759369bc Mon Sep 17 00:00:00 2001 From: Fergal Date: Tue, 7 Jan 2025 09:14:57 -0300 Subject: [PATCH] fix: EventResult should await OK response (#299) * fix: EventResult should await OK response * refactor: pretty run * test: ensure rejected response is caught --- src/keri/app/aiding.ts | 17 ++++++----------- test/app/aiding.test.ts | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/keri/app/aiding.ts b/src/keri/app/aiding.ts index a0863748..8287db3a 100644 --- a/src/keri/app/aiding.ts +++ b/src/keri/app/aiding.ts @@ -266,7 +266,7 @@ export class Identifier { jsondata[algo] = keeper.params(); this.client.pidx = this.client.pidx + 1; - const res = this.client.fetch('/identifiers', 'POST', jsondata); + const res = await this.client.fetch('/identifiers', 'POST', jsondata); return new EventResult(serder, sigs, res); } @@ -447,7 +447,7 @@ export class Identifier { sigs: sigs, }; - const res = this.client.fetch( + const res = await this.client.fetch( '/identifiers/' + name + '/endroles', 'POST', jsondata @@ -500,16 +500,12 @@ export class Identifier { export class EventResult { private readonly _serder: Serder; private readonly _sigs: string[]; - private readonly promise: Promise | Response; + private readonly response: Response; - constructor( - serder: Serder, - sigs: string[], - promise: Promise | Response - ) { + constructor(serder: Serder, sigs: string[], response: Response) { this._serder = serder; this._sigs = sigs; - this.promise = promise; + this.response = response; } get serder() { @@ -521,7 +517,6 @@ export class EventResult { } async op(): Promise { - const res = await this.promise; - return await res.json(); + return await this.response.json(); } } diff --git a/test/app/aiding.test.ts b/test/app/aiding.test.ts index bbe910c7..7d9bce8c 100644 --- a/test/app/aiding.test.ts +++ b/test/app/aiding.test.ts @@ -200,6 +200,16 @@ describe('Aiding', () => { assert.deepEqual(lastCall.body.salty.transferable, true); }); + it('Should throw error if fetch call fails when creating identifier', async () => { + const error = new Error(`Fail ${randomUUID()}`); + client.fetch.mockRejectedValue(error); + await expect( + client + .identifiers() + .create('aid1', { bran: '0123456789abcdefghijk' }) + ).rejects.toThrow(error); + }); + it('Can rotate salty identifier', async () => { const aid1 = await createMockIdentifierState('aid1', bran, {}); client.fetch.mockResolvedValueOnce(Response.json(aid1)); @@ -346,6 +356,14 @@ describe('Aiding', () => { }); }); + it('Should throw error if fetch call fails when adding end role', async () => { + const error = new Error(`Fail ${randomUUID()}`); + client.fetch.mockRejectedValue(error); + await expect( + client.identifiers().addEndRole('aid1', 'agent') + ).rejects.toThrow(error); + }); + it('Can get members', async () => { client.fetch.mockResolvedValue(Response.json({})); await client.identifiers().members('aid1');