-
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(core): Peer connection storage (#453)
* feat: add peer connection metadata * feat: create peer connection storage * feat: remove isPending in peerConnection metadata * feat: remove peerConnectionService * feat: remove peerConnectionStorage in AgentServicesProps * feat: remove storages in AgentServicesProps * feat: remove storages in AgentServicesProps * feat: remove storages in AgentServicesProps in tests * revert: revert changes in services unit tests
- Loading branch information
1 parent
63d0aa5
commit 3b0a775
Showing
10 changed files
with
247 additions
and
23 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
30 changes: 30 additions & 0 deletions
30
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,30 @@ | ||
import { | ||
PeerConnectionMetadataRecord, | ||
PeerConnectionMetadataRecordProps, | ||
} from "./peerConnectionMetadataRecord"; | ||
|
||
const mockData: PeerConnectionMetadataRecordProps = { | ||
id: "id", | ||
name: "name", | ||
url: "url", | ||
iconB64: "icon", | ||
selectedAid: "aid", | ||
}; | ||
|
||
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.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { BaseRecord } from "../../storage/storage.types"; | ||
|
||
interface PeerConnectionMetadataRecordProps { | ||
id: string; | ||
name?: string; | ||
url?: string; | ||
createdAt?: Date; | ||
iconB64?: string; | ||
selectedAid?: string; | ||
} | ||
|
||
class PeerConnectionMetadataRecord extends BaseRecord { | ||
name?: string; | ||
url?: string; | ||
iconB64?: string; | ||
selectedAid?: string; | ||
|
||
static readonly type = "PeerConnectionMetadataRecord"; | ||
readonly type = PeerConnectionMetadataRecord.type; | ||
|
||
constructor(props: PeerConnectionMetadataRecordProps) { | ||
super(); | ||
|
||
if (props) { | ||
this.id = props.id; | ||
this.name = props.name ?? ""; | ||
this.url = props.url ?? ""; | ||
this.selectedAid = props.selectedAid ?? ""; | ||
this.createdAt = props.createdAt ?? new Date(); | ||
this.iconB64 = props.iconB64 ?? ""; | ||
} | ||
} | ||
|
||
getTags() { | ||
return { | ||
...this._tags, | ||
}; | ||
} | ||
} | ||
|
||
export type { PeerConnectionMetadataRecordProps }; | ||
export { PeerConnectionMetadataRecord }; |
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,87 @@ | ||
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", | ||
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.getAll.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, | ||
{ | ||
name: "update name", | ||
} | ||
); | ||
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,73 @@ | ||
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.getAll( | ||
PeerConnectionMetadataRecord | ||
); | ||
return records; | ||
} | ||
|
||
async updatePeerConnectionMetadata( | ||
id: string, | ||
metadata: Partial< | ||
Pick< | ||
PeerConnectionMetadataRecord, | ||
"name" | "url" | "iconB64" | "selectedAid" | ||
> | ||
> | ||
): 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; | ||
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