Skip to content

Commit

Permalink
Remove outdated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
flypaper0 committed Jan 19, 2024
1 parent 7d70977 commit 7195db9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Sources/WalletConnectUtils/RPCHistory/RPCHistory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ public final class RPCHistory {
try? storage.get(key: recordId.string)
}

public func set(_ request: RPCRequest, forTopic topic: String, emmitedBy origin: Record.Origin) throws {
public func set(_ request: RPCRequest, forTopic topic: String, emmitedBy origin: Record.Origin, time: TimeProvider = DefaultTimeProvider()) throws {
guard let id = request.id else {
throw HistoryError.unidentifiedRequest
}
guard get(recordId: id) == nil else {
throw HistoryError.requestDuplicateNotAllowed
}
let record = Record(id: id, topic: topic, origin: origin, request: request, response: nil, timestamp: Date())
let record = Record(id: id, topic: topic, origin: origin, request: request, response: nil, timestamp: time.currentDate)
storage.set(record, forKey: "\(record.id)")
}

Expand Down
12 changes: 12 additions & 0 deletions Sources/WalletConnectUtils/TimeProvider.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Foundation

public protocol TimeProvider {
var currentDate: Date { get }
}

public struct DefaultTimeProvider: TimeProvider {
public init() {}
public var currentDate: Date {
return Date()
}
}
23 changes: 23 additions & 0 deletions Tests/WalletConnectUtilsTests/RPCHistoryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,27 @@ final class RPCHistoryTests: XCTestCase {
XCTAssertEqual(expectedError, error as? RPCHistory.HistoryError)
}
}

func testRemoveOutdated() throws {
let request1 = RPCRequest.stub()
let request2 = RPCRequest.stub()

let time1 = TestTimeProvider(currentDate: .distantPast)
let time2 = TestTimeProvider(currentDate: Date())

try sut.set(request1, forTopic: .randomTopic(), emmitedBy: .local, time: time1)
try sut.set(request2, forTopic: .randomTopic(), emmitedBy: .local, time: time2)

XCTAssertEqual(sut.get(recordId: request1.id!)?.request, request1)
XCTAssertEqual(sut.get(recordId: request2.id!)?.request, request2)

sut.removeOutdated()

XCTAssertEqual(sut.get(recordId: request1.id!)?.request, nil)
XCTAssertEqual(sut.get(recordId: request2.id!)?.request, request2)
}

struct TestTimeProvider: TimeProvider {
var currentDate: Date
}
}

0 comments on commit 7195db9

Please sign in to comment.