mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
129 lines
5.2 KiB
Swift
129 lines
5.2 KiB
Swift
2 years ago
|
// Copyright © 2023 Rangeproof Pty Ltd. All rights reserved.
|
||
|
|
||
|
import Combine
|
||
|
import GRDB
|
||
|
import Quick
|
||
|
import Nimble
|
||
2 years ago
|
import SessionSnodeKit
|
||
|
import SessionUtilitiesKit
|
||
|
import SessionUIKit
|
||
2 years ago
|
|
||
2 years ago
|
@testable import SessionMessagingKit
|
||
2 years ago
|
|
||
2 years ago
|
class MessageReceiverSpec: QuickSpec {
|
||
|
override class func spec() {
|
||
|
// MARK: Configuration
|
||
2 years ago
|
|
||
2 years ago
|
@TestState var mockStorage: Storage! = SynchronousStorage(
|
||
|
customWriter: try! DatabaseQueue(),
|
||
|
customMigrationTargets: [
|
||
|
SNUtilitiesKit.self,
|
||
|
SNSnodeKit.self,
|
||
|
SNMessagingKit.self,
|
||
|
SNUIKit.self
|
||
|
],
|
||
|
initialData: { db in
|
||
|
try SessionThread.fetchOrCreate(
|
||
|
db,
|
||
|
id: "TestId",
|
||
|
variant: .contact,
|
||
|
shouldBeVisible: true
|
||
2 years ago
|
)
|
||
|
}
|
||
2 years ago
|
)
|
||
|
@TestState var mockProto: SNProtoContent! = {
|
||
|
let proto = SNProtoContent.builder()
|
||
|
let dataMessage = SNProtoDataMessage.builder()
|
||
|
proto.setExpirationType(.deleteAfterRead)
|
||
|
proto.setExpirationTimer(UInt32(DisappearingMessagesConfiguration.DefaultDuration.disappearAfterRead.seconds))
|
||
|
proto.setLastDisappearingMessageChangeTimestamp((1234567890 - (60 * 10)) * 1000)
|
||
|
dataMessage.setBody("Test")
|
||
|
proto.setDataMessage(try! dataMessage.build())
|
||
|
return try? proto.build()
|
||
|
}()
|
||
|
@TestState var mockMessage: VisibleMessage! = {
|
||
|
let result = VisibleMessage(
|
||
|
sender: "TestId",
|
||
|
sentTimestamp: ((1234567890 - (60 * 10)) * 1000),
|
||
|
recipient: "05\(TestConstants.publicKey)",
|
||
|
text: "Test"
|
||
|
)
|
||
|
result.receivedTimestamp = (1234567890 * 1000)
|
||
|
return result
|
||
|
}()
|
||
|
@TestState var dependencies: Dependencies! = Dependencies(
|
||
|
storage: mockStorage
|
||
|
)
|
||
|
|
||
|
// MARK: - a MessageReceiver
|
||
|
describe("a MessageReceiver") {
|
||
|
// MARK: -- when receiving an outdated disappearing message config update
|
||
2 years ago
|
context("when receiving an outdated disappearing message config update") {
|
||
2 years ago
|
// MARK: ---- does NOT update local config
|
||
2 years ago
|
it("does NOT update local config") {
|
||
2 years ago
|
let config: DisappearingMessagesConfiguration = DisappearingMessagesConfiguration
|
||
|
.defaultWith("TestId")
|
||
|
.with(
|
||
|
isEnabled: false,
|
||
|
durationSeconds: 0,
|
||
|
type: .unknown,
|
||
2 years ago
|
lastChangeTimestampMs: (1234567890 * 1000)
|
||
2 years ago
|
)
|
||
|
|
||
|
mockStorage.write { db in
|
||
|
try config.save(db)
|
||
|
|
||
2 years ago
|
try MessageReceiver.handle(
|
||
|
db,
|
||
|
threadId: "TestId",
|
||
|
threadVariant: .contact,
|
||
|
message: mockMessage,
|
||
|
serverExpirationTimestamp: nil,
|
||
|
associatedWithProto: mockProto
|
||
|
)
|
||
2 years ago
|
}
|
||
|
|
||
|
let updatedConfig: DisappearingMessagesConfiguration? = mockStorage.read { db in
|
||
|
try DisappearingMessagesConfiguration.fetchOne(db, id: "TestId")
|
||
|
}
|
||
|
|
||
2 years ago
|
expect(updatedConfig?.isEnabled).to(beFalse())
|
||
|
expect(updatedConfig?.durationSeconds).to(equal(0))
|
||
|
expect(updatedConfig?.type).to(equal(.unknown))
|
||
2 years ago
|
}
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
// MARK: -- when receiving a newer disappearing message config update
|
||
2 years ago
|
context("when receiving a newer disappearing message config update") {
|
||
2 years ago
|
// MARK: ---- updates the local config properly
|
||
2 years ago
|
it("updates the local config properly") {
|
||
|
mockStorage.write { db in
|
||
2 years ago
|
do {
|
||
2 years ago
|
try MessageReceiver.handle(
|
||
2 years ago
|
db,
|
||
|
threadId: "TestId",
|
||
|
threadVariant: .contact,
|
||
|
message: mockMessage,
|
||
2 years ago
|
serverExpirationTimestamp: nil,
|
||
|
associatedWithProto: mockProto
|
||
2 years ago
|
)
|
||
|
}
|
||
|
catch {
|
||
|
print(error)
|
||
|
}
|
||
2 years ago
|
}
|
||
|
|
||
|
let updatedConfig: DisappearingMessagesConfiguration? = mockStorage.read { db in
|
||
|
try DisappearingMessagesConfiguration.fetchOne(db, id: "TestId")
|
||
|
}
|
||
|
|
||
2 years ago
|
expect(updatedConfig?.isEnabled).to(beTrue())
|
||
2 years ago
|
expect(updatedConfig?.durationSeconds)
|
||
|
.to(equal(DisappearingMessagesConfiguration.DefaultDuration.disappearAfterRead.seconds))
|
||
2 years ago
|
expect(updatedConfig?.type).to(equal(.disappearAfterRead))
|
||
2 years ago
|
}
|
||
|
}
|
||
2 years ago
|
}
|
||
|
}
|
||
|
}
|