-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create peer connection storage
- Loading branch information
1 parent
35a2cee
commit 532b7d0
Showing
19 changed files
with
495 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
export * from "./credentialMetadataRecord"; | ||
export * from "./identifierMetadataRecord"; | ||
export * from "./peerConnectionMetadataRecord"; | ||
export * from "./basicRecord"; | ||
export * from "./credentialStorage"; | ||
export * from "./identifierStorage"; | ||
export * from "./peerConnectionStorage"; | ||
export * from "./basicStorage"; |
34 changes: 34 additions & 0 deletions
34
src/core/agent/records/peerConnectionMetadataRecord.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 { | ||
PeerConnectionMetadataRecord, | ||
PeerConnectionMetadataRecordProps, | ||
} from "./peerConnectionMetadataRecord"; | ||
|
||
const mockData: PeerConnectionMetadataRecordProps = { | ||
id: "id", | ||
name: "name", | ||
url: "url", | ||
iconB64: "icon", | ||
selectedAid: "aid", | ||
isPending: false, | ||
}; | ||
|
||
describe("Peer Connection Record", () => { | ||
test("should fill the record based on supplied props", () => { | ||
const createdAt = new Date(); | ||
const settingsRecord = new PeerConnectionMetadataRecord({ | ||
...mockData, | ||
createdAt: createdAt, | ||
}); | ||
settingsRecord.getTags(); | ||
expect(settingsRecord.type).toBe(PeerConnectionMetadataRecord.type); | ||
expect(settingsRecord.id).toBe(mockData.id); | ||
expect(settingsRecord.name).toBe(mockData.name); | ||
expect(settingsRecord.url).toBe(mockData.url); | ||
expect(settingsRecord.selectedAid).toBe(mockData.selectedAid); | ||
expect(settingsRecord.iconB64).toBe(mockData.iconB64); | ||
expect(settingsRecord.getTags()).toMatchObject({ | ||
isPending: mockData.isPending, | ||
}); | ||
expect(settingsRecord.createdAt).toBe(createdAt); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { PeerConnectionMetadataRecord } from "./peerConnectionMetadataRecord"; | ||
import { PeerConnectionStorage } from "./peerConnectionStorage"; | ||
|
||
const storageService = jest.mocked({ | ||
save: jest.fn(), | ||
delete: jest.fn(), | ||
deleteById: jest.fn(), | ||
update: jest.fn(), | ||
findById: jest.fn(), | ||
findAllByQuery: jest.fn(), | ||
getAll: jest.fn(), | ||
}); | ||
|
||
const peerConnectionStorage = new PeerConnectionStorage(storageService as any); | ||
|
||
const peerConnectionMetadataRecordProps = { | ||
id: "id", | ||
name: "name", | ||
url: "url", | ||
iconB64: "icon", | ||
selectedAid: "aid", | ||
isPending: true, | ||
createdAt: new Date(), | ||
}; | ||
|
||
const peerConnectionMetadataRecord = new PeerConnectionMetadataRecord({ | ||
...peerConnectionMetadataRecordProps, | ||
}); | ||
|
||
const peerConnectionMetadataRecord2 = new PeerConnectionMetadataRecord({ | ||
...peerConnectionMetadataRecordProps, | ||
id: "id2", | ||
}); | ||
|
||
describe("Connection service of agent", () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
test("Should get all credentials", async () => { | ||
storageService.findAllByQuery.mockResolvedValue([ | ||
peerConnectionMetadataRecord, | ||
peerConnectionMetadataRecord2, | ||
]); | ||
expect(await peerConnectionStorage.getAllPeerConnectionMetadata()).toEqual([ | ||
peerConnectionMetadataRecord, | ||
peerConnectionMetadataRecord2, | ||
]); | ||
}); | ||
|
||
test("Should get credential metadata", async () => { | ||
storageService.findById.mockResolvedValue(peerConnectionMetadataRecord); | ||
expect( | ||
await peerConnectionStorage.getPeerConnectionMetadata( | ||
peerConnectionMetadataRecord.id | ||
) | ||
).toEqual(peerConnectionMetadataRecord); | ||
}); | ||
|
||
test("Should throw if peerConnection metadata record is missing", async () => { | ||
storageService.findById.mockResolvedValue(null); | ||
await expect( | ||
peerConnectionStorage.getPeerConnectionMetadata( | ||
peerConnectionMetadataRecord.id | ||
) | ||
).rejects.toThrowError( | ||
PeerConnectionStorage.PEER_CONNECTION_METADATA_RECORD_MISSING | ||
); | ||
}); | ||
|
||
test("Should save peerConnection metadata record", async () => { | ||
await peerConnectionStorage.createPeerConnectionMetadataRecord( | ||
peerConnectionMetadataRecordProps | ||
); | ||
expect(storageService.save).toBeCalledWith(peerConnectionMetadataRecord); | ||
}); | ||
|
||
test("Should update credential metadata record", async () => { | ||
storageService.findById.mockResolvedValue(peerConnectionMetadataRecord); | ||
await peerConnectionStorage.updatePeerConnectionMetadata( | ||
peerConnectionMetadataRecord.id, | ||
{ | ||
isPending: false, | ||
} | ||
); | ||
expect(storageService.update).toBeCalled(); | ||
}); | ||
}); |
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,76 @@ | ||
import { StorageService } from "../../storage/storage.types"; | ||
import { | ||
PeerConnectionMetadataRecord, | ||
PeerConnectionMetadataRecordProps, | ||
} from "./peerConnectionMetadataRecord"; | ||
|
||
class PeerConnectionStorage { | ||
static readonly PEER_CONNECTION_METADATA_RECORD_MISSING = | ||
"Peer connection metadata record does not exist"; | ||
private storageService: StorageService<PeerConnectionMetadataRecord>; | ||
|
||
constructor(storageService: StorageService<PeerConnectionMetadataRecord>) { | ||
this.storageService = storageService; | ||
} | ||
|
||
async getPeerConnectionMetadata( | ||
id: string | ||
): Promise<PeerConnectionMetadataRecord> { | ||
const metadata = await this.storageService.findById( | ||
id, | ||
PeerConnectionMetadataRecord | ||
); | ||
if (!metadata) { | ||
throw new Error( | ||
PeerConnectionStorage.PEER_CONNECTION_METADATA_RECORD_MISSING | ||
); | ||
} | ||
return metadata; | ||
} | ||
|
||
async getAllPeerConnectionMetadata(): Promise< | ||
PeerConnectionMetadataRecord[] | ||
> { | ||
const records = await this.storageService.findAllByQuery( | ||
{}, | ||
PeerConnectionMetadataRecord | ||
); | ||
return records; | ||
} | ||
|
||
async updatePeerConnectionMetadata( | ||
id: string, | ||
metadata: Partial< | ||
Pick< | ||
PeerConnectionMetadataRecord, | ||
"name" | "url" | "iconB64" | "selectedAid" | "isPending" | ||
> | ||
> | ||
): Promise<void> { | ||
const identifierMetadataRecord = await this.getPeerConnectionMetadata(id); | ||
if (metadata.name !== undefined) | ||
identifierMetadataRecord.name = metadata.name; | ||
if (metadata.url !== undefined) identifierMetadataRecord.url = metadata.url; | ||
if (metadata.iconB64 !== undefined) | ||
identifierMetadataRecord.iconB64 = metadata.iconB64; | ||
if (metadata.selectedAid !== undefined) | ||
identifierMetadataRecord.selectedAid = metadata.selectedAid; | ||
if (metadata.isPending !== undefined) | ||
identifierMetadataRecord.isPending = metadata.isPending; | ||
await this.storageService.update(identifierMetadataRecord); | ||
} | ||
|
||
async createPeerConnectionMetadataRecord( | ||
data: PeerConnectionMetadataRecordProps | ||
): Promise<void> { | ||
const record = new PeerConnectionMetadataRecord(data); | ||
await this.storageService.save(record); | ||
} | ||
|
||
async deletePeerConnectionMetadataRecord(id: string): Promise<void> { | ||
const record = await this.getPeerConnectionMetadata(id); | ||
await this.storageService.delete(record); | ||
} | ||
} | ||
|
||
export { PeerConnectionStorage }; |
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
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
Oops, something went wrong.