From b6a07a9aa921a11c0c858b7306e8d513143d913f Mon Sep 17 00:00:00 2001 From: Pallab Maiti Date: Fri, 8 Sep 2023 10:53:36 +0530 Subject: [PATCH] feat: get session_id --- Sources/Classes/Headers/Public/RSClient.h | 2 + .../Headers/Public/RSEventRepository.h | 1 + Sources/Classes/RSClient.m | 4 ++ Sources/Classes/RSEventRepository.m | 3 ++ Tests/RudderUtilsTest.swift | 2 +- Tests/UserSessionTests.swift | 49 +++++++++++++++++++ 6 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 Tests/UserSessionTests.swift diff --git a/Sources/Classes/Headers/Public/RSClient.h b/Sources/Classes/Headers/Public/RSClient.h index ba054165..42025ff5 100644 --- a/Sources/Classes/Headers/Public/RSClient.h +++ b/Sources/Classes/Headers/Public/RSClient.h @@ -86,6 +86,8 @@ NS_ASSUME_NONNULL_BEGIN - (void)startSession:(long)sessionId; - (void)endSession; +@property (strong, nonatomic, readonly) NSNumber* _Nullable sessionId; + @end NS_ASSUME_NONNULL_END diff --git a/Sources/Classes/Headers/Public/RSEventRepository.h b/Sources/Classes/Headers/Public/RSEventRepository.h index a87a2ffa..2c9ad20d 100644 --- a/Sources/Classes/Headers/Public/RSEventRepository.h +++ b/Sources/Classes/Headers/Public/RSEventRepository.h @@ -69,6 +69,7 @@ NS_ASSUME_NONNULL_BEGIN - (void) startSession:(long) sessionId; - (void) endSession; +- (NSNumber * _Nullable)getSessionId; - (RSConfig* _Nullable) getConfig; - (void)applicationDidFinishLaunchingWithOptions:(NSDictionary *) launchOptions; diff --git a/Sources/Classes/RSClient.m b/Sources/Classes/RSClient.m index 9c30bfdf..ad6b9c8a 100644 --- a/Sources/Classes/RSClient.m +++ b/Sources/Classes/RSClient.m @@ -486,4 +486,8 @@ - (void)endSession { } } +- (NSNumber * _Nullable)sessionId { + return (_repository != nil) ? [_repository getSessionId] : nil; +} + @end diff --git a/Sources/Classes/RSEventRepository.m b/Sources/Classes/RSEventRepository.m index c8268c2a..039903e0 100644 --- a/Sources/Classes/RSEventRepository.m +++ b/Sources/Classes/RSEventRepository.m @@ -349,5 +349,8 @@ - (void) endSession { [self->userSession clearSession]; } +- (NSNumber * _Nullable)getSessionId { + return [self->userSession getSessionId]; +} @end diff --git a/Tests/RudderUtilsTest.swift b/Tests/RudderUtilsTest.swift index ca24d1f3..4b6dae8b 100644 --- a/Tests/RudderUtilsTest.swift +++ b/Tests/RudderUtilsTest.swift @@ -65,7 +65,7 @@ class RudderUtilsTest: XCTestCase { "company" : "RudderStack", "city" : "Hyderabad"] - let response:Any = RSUtils.deSerializeJSONString(jsonString) + let response = RSUtils.deSerializeJSONString(jsonString) let parsedDict:[String:String]? = response as? [String:String] ?? nil XCTAssert(parsedDict == dictObj) diff --git a/Tests/UserSessionTests.swift b/Tests/UserSessionTests.swift new file mode 100644 index 00000000..0aa685e9 --- /dev/null +++ b/Tests/UserSessionTests.swift @@ -0,0 +1,49 @@ +// +// UserSessionTests.swift +// Rudder +// +// Created by Pallab Maiti on 06/09/23. +// + +import XCTest +@testable import Rudder + +final class UserSessionTests: XCTestCase { + + var userSession: RSUserSession! + + override func setUp() { + super.setUp() + let preferenceManager = RSPreferenceManager.getInstance() + userSession = RSUserSession.initiate(10, with: preferenceManager) + } + + override func tearDown() { + super.tearDown() + } + + func test_startSession() { + userSession.start() + + XCTAssertEqual(userSession.getId(), RSPreferenceManager.getInstance().getSessionId()) + userSession.clear() + } + + func test_startSessionWithId() { + userSession.start(12345678) + + XCTAssertEqual(userSession.getId(), 12345678) + XCTAssertEqual(RSPreferenceManager.getInstance().getSessionId(), 12345678) + userSession.clear() + } + + func test_clearSession() { + userSession.start() + + XCTAssertEqual(userSession.getId(), RSPreferenceManager.getInstance().getSessionId()) + userSession.clear() + + XCTAssertNil(userSession.getId()) + XCTAssertNil(RSPreferenceManager.getInstance().getSessionId()) + } +}