metatext/ServiceLayer/Tests/ServiceLayerTests/InstanceURLServiceTests.swift

100 lines
2.9 KiB
Swift
Raw Normal View History

2020-09-07 04:56:18 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import CodableBloomFilter
import Combine
import CombineExpectations
@testable import ServiceLayer
@testable import ServiceLayerMocks
import Stubbing
import XCTest
2020-11-09 06:22:20 +00:00
final class InstanceURLServiceTests: XCTestCase {
2020-09-07 04:56:18 +00:00
func testFiltering() throws {
2020-09-10 04:51:31 +00:00
let sut = InstanceURLService(environment: .mock())
2020-09-07 04:56:18 +00:00
2021-01-17 20:24:50 +00:00
guard case .success = sut.url(text: "unfiltered.instance") else {
2020-09-23 07:11:35 +00:00
XCTFail("Expected success")
2021-01-17 20:24:50 +00:00
return
2020-09-23 07:11:35 +00:00
}
2021-01-17 20:24:50 +00:00
guard case let .failure(error) = sut.url(text: "filtered.instance"),
case InstanceURLError.instanceNotSupported = error
else {
XCTFail("Expected instance not supported error")
return
2020-09-23 07:11:35 +00:00
}
2021-01-17 20:24:50 +00:00
guard case .failure = sut.url(text: "subdomain.filtered.instance"),
case InstanceURLError.instanceNotSupported = error
else {
XCTFail("Expected instance not supported error")
return
2020-09-23 07:11:35 +00:00
}
2020-09-07 04:56:18 +00:00
}
func testUpdating() throws {
let environment = AppEnvironment.mock()
2020-09-10 04:51:31 +00:00
var sut = InstanceURLService(environment: environment)
2020-09-07 04:56:18 +00:00
2021-01-17 20:24:50 +00:00
guard case let .failure(error0) = sut.url(text: "filtered.instance"),
case InstanceURLError.instanceNotSupported = error0
else {
XCTFail("Expected instance not supported error")
return
2020-09-23 07:11:35 +00:00
}
2021-01-17 20:24:50 +00:00
guard case .success = sut.url(text: "instance.filtered") else {
2020-09-23 07:11:35 +00:00
XCTFail("Expected success")
2021-01-17 20:24:50 +00:00
return
2020-09-23 07:11:35 +00:00
}
2020-09-07 04:56:18 +00:00
2020-11-13 00:42:45 +00:00
var updatedFilter = BloomFilter<String>(hashes: [.djb232, .sdbm32], byteCount: 16)
2020-09-07 04:56:18 +00:00
updatedFilter.insert("instance.filtered")
let updatedFilterData = try JSONEncoder().encode(updatedFilter)
2020-10-05 22:50:05 +00:00
let stub: HTTPStub = .success((HTTPURLResponse(), updatedFilterData))
2020-09-07 04:56:18 +00:00
2020-09-13 00:02:56 +00:00
StubbingURLProtocol.setStub(stub, forURL: URL(string: "https://filter.metabolist.com/filter")!)
2020-09-07 04:56:18 +00:00
let updateRecorder = sut.updateFilter().collect().record()
_ = try wait(for: updateRecorder.next(), timeout: 1)
2021-01-17 20:24:50 +00:00
guard case .success = sut.url(text: "filtered.instance") else {
2020-09-23 07:11:35 +00:00
XCTFail("Expected success")
2021-01-17 20:24:50 +00:00
return
2020-09-23 07:11:35 +00:00
}
2021-01-17 20:24:50 +00:00
guard case let .failure(error1) = sut.url(text: "instance.filtered"),
case InstanceURLError.instanceNotSupported = error1
else {
XCTFail("Expected instance not supported error")
return
2020-09-23 07:11:35 +00:00
}
2020-09-07 04:56:18 +00:00
2020-09-10 04:51:31 +00:00
sut = InstanceURLService(environment: environment)
2020-09-07 04:56:18 +00:00
2021-01-17 20:24:50 +00:00
guard case .success = sut.url(text: "filtered.instance") else {
2020-09-23 07:11:35 +00:00
XCTFail("Expected success")
2021-01-17 20:24:50 +00:00
return
2020-09-23 07:11:35 +00:00
}
2021-01-17 20:24:50 +00:00
guard case let .failure(error2) = sut.url(text: "instance.filtered"),
case InstanceURLError.instanceNotSupported = error2
else {
XCTFail("Expected instance not supported error")
return
2020-09-23 07:11:35 +00:00
}
2020-09-07 04:56:18 +00:00
}
}