1
0
mirror of https://github.com/metabolist/metatext synced 2024-12-13 01:46:30 +01:00
metatext-app-ios-iphone-ipad/ServiceLayer/Tests/ServiceLayerTests/InstanceURLServiceTests.swift
Justin Mazzocchi 012f970bdb
Fix tests
2020-09-23 00:11:35 -07:00

72 lines
2.1 KiB
Swift

// Copyright © 2020 Metabolist. All rights reserved.
import CodableBloomFilter
import Combine
import CombineExpectations
@testable import ServiceLayer
@testable import ServiceLayerMocks
import Stubbing
import XCTest
class InstanceURLServiceTests: XCTestCase {
func testFiltering() throws {
let sut = InstanceURLService(environment: .mock())
if case .failure = sut.url(text: "unfiltered.instance") {
XCTFail("Expected success")
}
if case .success = sut.url(text: "filtered.instance") {
XCTFail("Expected failure")
}
if case .success = sut.url(text: "subdomain.filtered.instance") {
XCTFail("Expected failure")
}
}
func testUpdating() throws {
let environment = AppEnvironment.mock()
var sut = InstanceURLService(environment: environment)
if case .success = sut.url(text: "filtered.instance") {
XCTFail("Expected failure")
}
if case .failure = sut.url(text: "instance.filtered") {
XCTFail("Expected success")
}
var updatedFilter = try BloomFilter<String>(hashes: [.djb232, .sdbm32], byteCount: 16)
updatedFilter.insert("instance.filtered")
let updatedFilterData = try JSONEncoder().encode(updatedFilter)
let stub: HTTPStub = .success((URLResponse(), updatedFilterData))
StubbingURLProtocol.setStub(stub, forURL: URL(string: "https://filter.metabolist.com/filter")!)
let updateRecorder = sut.updateFilter().collect().record()
_ = try wait(for: updateRecorder.next(), timeout: 1)
if case .failure = sut.url(text: "filtered.instance") {
XCTFail("Expected success")
}
if case .success = sut.url(text: "instance.filtered") {
XCTFail("Expected failure")
}
sut = InstanceURLService(environment: environment)
if case .failure = sut.url(text: "filtered.instance") {
XCTFail("Expected success")
}
if case .success = sut.url(text: "instance.filtered") {
XCTFail("Expected failure")
}
}
}