@@ -2,6 +2,7 @@ import { Either, Left } from "purify-ts";
22import { Observable } from "rxjs" ;
33
44import { type DmkConfig } from "@api/DmkConfig" ;
5+ import { connectedDeviceStubBuilder } from "@api/transport/model/TransportConnectedDevice.stub" ;
56import { DEVICE_SESSION_REFRESHER_DEFAULT_OPTIONS } from "@internal/device-session/data/DeviceSessionRefresherConst" ;
67import { type DeviceSession } from "@internal/device-session/model/DeviceSession" ;
78import { deviceSessionStubBuilder } from "@internal/device-session/model/DeviceSession.stub" ;
@@ -24,23 +25,26 @@ vi.mock("@internal/manager-api/data/AxiosManagerApiDataSource");
2425let sessionService : DefaultDeviceSessionService ;
2526let loggerService : DefaultLoggerPublisherService ;
2627let deviceSession : DeviceSession ;
28+ let deviceSession1 : DeviceSession ;
29+ let deviceSession2 : DeviceSession ;
2730let managerApiDataSource : ManagerApiDataSource ;
2831let managerApi : ManagerApiService ;
2932let secureChannelDataSource : SecureChannelDataSource ;
3033let secureChannel : SecureChannelService ;
3134
3235describe ( "DefaultDeviceSessionService" , ( ) => {
36+ // Initialize shared resources
37+ loggerService = new DefaultLoggerPublisherService ( [ ] , "deviceSession" ) ;
38+ managerApiDataSource = new AxiosManagerApiDataSource ( { } as DmkConfig ) ;
39+ managerApi = new DefaultManagerApiService ( managerApiDataSource ) ;
40+ secureChannelDataSource = new DefaultSecureChannelDataSource ( { } as DmkConfig ) ;
41+ secureChannel = new DefaultSecureChannelService ( secureChannelDataSource ) ;
42+
3343 beforeEach ( ( ) => {
3444 vi . restoreAllMocks ( ) ;
35- loggerService = new DefaultLoggerPublisherService ( [ ] , "deviceSession" ) ;
45+ // Create a new instance before each test
3646 sessionService = new DefaultDeviceSessionService ( ( ) => loggerService ) ;
37- managerApiDataSource = new AxiosManagerApiDataSource ( { } as DmkConfig ) ;
38- managerApi = new DefaultManagerApiService ( managerApiDataSource ) ;
39- secureChannelDataSource = new DefaultSecureChannelDataSource (
40- { } as DmkConfig ,
41- ) ;
42- secureChannel = new DefaultSecureChannelService ( secureChannelDataSource ) ;
43-
47+ // Create a device session stub with default properties
4448 deviceSession = deviceSessionStubBuilder (
4549 { } ,
4650 ( ) => loggerService ,
@@ -54,43 +58,96 @@ describe("DefaultDeviceSessionService", () => {
5458 deviceSession . close ( ) ;
5559 } ) ;
5660
57- it ( "should have an empty sessions list" , ( ) => {
61+ it ( "should have an empty DeviceSession list" , ( ) => {
5862 expect ( sessionService . getDeviceSessions ( ) ) . toEqual ( [ ] ) ;
5963 } ) ;
6064
61- it ( "should add a deviceSession" , ( ) => {
62- sessionService . addDeviceSession ( deviceSession ) ;
63- expect ( sessionService . getDeviceSessions ( ) ) . toEqual ( [ deviceSession ] ) ;
64- } ) ;
65-
66- it ( "should not add a deviceSession if it already exists" , ( ) => {
67- sessionService . addDeviceSession ( deviceSession ) ;
68- sessionService . addDeviceSession ( deviceSession ) ;
69- expect ( sessionService . getDeviceSessions ( ) ) . toEqual ( [ deviceSession ] ) ;
70- } ) ;
71-
72- it ( "should remove a deviceSession" , ( ) => {
73- sessionService . addDeviceSession ( deviceSession ) ;
74- sessionService . removeDeviceSession ( deviceSession . id ) ;
75- expect ( sessionService . getDeviceSessions ( ) ) . toEqual ( [ ] ) ;
65+ describe ( "DeviceSessionService addDeviceSession" , ( ) => {
66+ it ( "should add a DeviceSession if it does not already exist" , ( ) => {
67+ sessionService . addDeviceSession ( deviceSession ) ;
68+ expect ( sessionService . getDeviceSessions ( ) ) . toEqual ( [ deviceSession ] ) ;
69+ } ) ;
70+ it ( "should not add a DeviceSession if it already exists" , ( ) => {
71+ sessionService . addDeviceSession ( deviceSession ) ;
72+ sessionService . addDeviceSession ( deviceSession ) ;
73+ expect ( sessionService . getDeviceSessions ( ) ) . toEqual ( [ deviceSession ] ) ;
74+ } ) ;
7675 } ) ;
7776
78- it ( "should not remove a deviceSession if it does not exist" , ( ) => {
79- sessionService . removeDeviceSession ( deviceSession . id ) ;
80- expect ( sessionService . getDeviceSessions ( ) ) . toEqual ( [ ] ) ;
77+ describe ( "DeviceSessionService removeDeviceSession" , ( ) => {
78+ it ( "should remove the DeviceSession of given ID" , ( ) => {
79+ sessionService . addDeviceSession ( deviceSession ) ;
80+ sessionService . removeDeviceSession ( deviceSession . id ) ;
81+ expect ( sessionService . getDeviceSessions ( ) ) . toEqual ( [ ] ) ;
82+ } ) ;
83+ it ( "should not remove the DeviceSession of given ID if it does not exist" , ( ) => {
84+ sessionService . addDeviceSession ( deviceSession ) ;
85+ sessionService . removeDeviceSession ( "non-existent-id" ) ;
86+ expect ( sessionService . getDeviceSessions ( ) ) . toEqual ( [ deviceSession ] ) ;
87+ } ) ;
8188 } ) ;
8289
83- it ( "should get a deviceSession" , ( ) => {
84- sessionService . addDeviceSession ( deviceSession ) ;
85- expect ( sessionService . getDeviceSessionById ( deviceSession . id ) ) . toEqual (
86- Either . of ( deviceSession ) ,
87- ) ;
90+ describe ( "DeviceSessionService getDeviceSessionById" , ( ) => {
91+ it ( "should get the DeviceSession of given ID if it exists" , ( ) => {
92+ sessionService . addDeviceSession ( deviceSession ) ;
93+ expect ( sessionService . getDeviceSessionById ( deviceSession . id ) ) . toEqual (
94+ Either . of ( deviceSession ) ,
95+ ) ;
96+ } ) ;
97+ it ( "should not get the DeviceSession if it does not exist" , ( ) => {
98+ sessionService . addDeviceSession ( deviceSession ) ;
99+ expect ( sessionService . getDeviceSessionById ( "non-existent-id" ) ) . toEqual (
100+ Left ( new DeviceSessionNotFound ( ) ) ,
101+ ) ;
102+ } ) ;
88103 } ) ;
89104
90- it ( "should not get a deviceSession if it does not exist" , ( ) => {
91- expect ( sessionService . getDeviceSessionById ( deviceSession . id ) ) . toEqual (
92- Left ( new DeviceSessionNotFound ( ) ) ,
93- ) ;
105+ describe ( "DeviceSessionService getDeviceSessionsByDeviceId" , ( ) => {
106+ it ( "should not get device sessions by deviceId if none exist" , ( ) => {
107+ sessionService . addDeviceSession ( deviceSession ) ;
108+ expect (
109+ sessionService . getDeviceSessionsByDeviceId ( "non-existent-device-id" ) ,
110+ ) . toEqual ( Left ( new DeviceSessionNotFound ( ) ) ) ;
111+ } ) ;
112+ it ( "should get a single device session by deviceId" , ( ) => {
113+ sessionService . addDeviceSession ( deviceSession ) ;
114+ expect (
115+ sessionService . getDeviceSessionsByDeviceId (
116+ deviceSession . connectedDevice . id ,
117+ ) ,
118+ ) . toEqual ( Either . of ( [ deviceSession ] ) ) ;
119+ } ) ;
120+ it ( "should get device sessions by deviceId" , ( ) => {
121+ sessionService . addDeviceSession ( deviceSession ) ;
122+ deviceSession1 = deviceSessionStubBuilder (
123+ {
124+ connectedDevice : connectedDeviceStubBuilder ( { id : "device-1" } ) ,
125+ id : "session-1" ,
126+ } ,
127+ ( ) => loggerService ,
128+ managerApi ,
129+ secureChannel ,
130+ DEVICE_SESSION_REFRESHER_DEFAULT_OPTIONS ,
131+ ) ;
132+
133+ deviceSession2 = deviceSessionStubBuilder (
134+ {
135+ connectedDevice : connectedDeviceStubBuilder ( { id : "device-1" } ) ,
136+ id : "session-2" ,
137+ } ,
138+ ( ) => loggerService ,
139+ managerApi ,
140+ secureChannel ,
141+ DEVICE_SESSION_REFRESHER_DEFAULT_OPTIONS ,
142+ ) ;
143+ sessionService . addDeviceSession ( deviceSession1 ) ;
144+ sessionService . addDeviceSession ( deviceSession2 ) ;
145+ expect ( sessionService . getDeviceSessionsByDeviceId ( "device-1" ) ) . toEqual (
146+ Either . of ( [ deviceSession1 , deviceSession2 ] ) ,
147+ ) ;
148+ deviceSession1 . close ( ) ;
149+ deviceSession2 . close ( ) ;
150+ } ) ;
94151 } ) ;
95152
96153 it ( "should get all sessions" , ( ) => {
0 commit comments