summaryrefslogtreecommitdiff
path: root/SoraTests/ViewDerivedDataTests.swift
diff options
context:
space:
mode:
authorFuwn <[email protected]>2026-03-22 13:20:36 +0000
committerFuwn <[email protected]>2026-03-22 13:20:36 +0000
commit2b62f719849cce5dd43fef1b679fff3a2243fcdf (patch)
treecf49424478aa71fcfb8a94b9af4a7b3b01f49908 /SoraTests/ViewDerivedDataTests.swift
parentperf: cache danbooru page token floor and reuse search-history payloads (diff)
downloadsora-testing-2b62f719849cce5dd43fef1b679fff3a2243fcdf.tar.xz
sora-testing-2b62f719849cce5dd43fef1b679fff3a2243fcdf.zip
feat: add configurable booru user agent settings
Diffstat (limited to 'SoraTests/ViewDerivedDataTests.swift')
-rw-r--r--SoraTests/ViewDerivedDataTests.swift135
1 files changed, 135 insertions, 0 deletions
diff --git a/SoraTests/ViewDerivedDataTests.swift b/SoraTests/ViewDerivedDataTests.swift
index d19b51a..f844a7c 100644
--- a/SoraTests/ViewDerivedDataTests.swift
+++ b/SoraTests/ViewDerivedDataTests.swift
@@ -738,6 +738,32 @@ final class ViewDerivedDataTests: XCTestCase { // swiftlint:disable:this type_b
)
}
+ func testSettingsManagerPersistsBooruUserAgentSettings() throws {
+ let source = try loadSource(at: "Sora/Data/Settings/SettingsManager.swift")
+ let sendUserAgentSettingCount = tokenCount(
+ matching: #"\@AppStorage\("sendBooruUserAgent"\)\s*var\s+sendBooruUserAgent\s*=\s*true"#,
+ in: source
+ )
+ let customUserAgentSettingCount = tokenCount(
+ matching:
+ #"\@AppStorage\("customBooruUserAgent"\)\s*var\s+customBooruUserAgent\s*=\s*"""#,
+ in: source
+ )
+
+ // swiftlint:disable:next prefer_nimble
+ XCTAssertGreaterThan(
+ sendUserAgentSettingCount,
+ 0,
+ "SettingsManager should persist a booru User-Agent toggle with a default of true."
+ )
+ // swiftlint:disable:next prefer_nimble
+ XCTAssertGreaterThan(
+ customUserAgentSettingCount,
+ 0,
+ "SettingsManager should persist a custom booru User-Agent override."
+ )
+ }
+
func testMainViewPassesShowHeldMoebooruPostsToBooruManager() throws {
let source = try loadSource(at: "Sora/Views/MainView.swift")
let managerShowHeldWiringCount = tokenCount(
@@ -763,6 +789,51 @@ final class ViewDerivedDataTests: XCTestCase { // swiftlint:disable:this type_b
)
}
+ func testMainViewPassesBooruUserAgentSettingsToBooruManager() throws {
+ let source = try loadSource(at: "Sora/Views/MainView.swift")
+ let sendUserAgentWiringCount = tokenCount(
+ matching: #"sendUserAgent:\s*settings\.sendBooruUserAgent"#,
+ in: source
+ )
+ let customUserAgentWiringCount = tokenCount(
+ matching: #"customUserAgent:\s*settings\.customBooruUserAgent"#,
+ in: source
+ )
+ let sendUserAgentChangeObserverCount = tokenCount(
+ matching: #"\.onChange\s*\(\s*of:\s*settings\.sendBooruUserAgent\s*\)"#,
+ in: source
+ )
+ let customUserAgentChangeObserverCount = tokenCount(
+ matching: #"\.onChange\s*\(\s*of:\s*settings\.customBooruUserAgent\s*\)"#,
+ in: source
+ )
+
+ // swiftlint:disable:next prefer_nimble
+ XCTAssertGreaterThan(
+ sendUserAgentWiringCount,
+ 1,
+ "MainView should wire booru User-Agent toggle into all BooruManager reconstructions."
+ )
+ // swiftlint:disable:next prefer_nimble
+ XCTAssertGreaterThan(
+ customUserAgentWiringCount,
+ 1,
+ "MainView should wire booru custom User-Agent into all BooruManager reconstructions."
+ )
+ // swiftlint:disable:next prefer_nimble
+ XCTAssertGreaterThan(
+ sendUserAgentChangeObserverCount,
+ 0,
+ "MainView should rebuild BooruManager when booru User-Agent sending changes."
+ )
+ // swiftlint:disable:next prefer_nimble
+ XCTAssertGreaterThan(
+ customUserAgentChangeObserverCount,
+ 0,
+ "MainView should rebuild BooruManager when custom booru User-Agent changes."
+ )
+ }
+
func testBooruManagerSupportsShowHeldMoebooruPostsMode() throws {
let source = try loadSource(at: "Sora/Data/Booru/BooruManager.swift")
let initSignatureCount = tokenCount(
@@ -1012,6 +1083,70 @@ final class ViewDerivedDataTests: XCTestCase { // swiftlint:disable:this type_b
)
}
+ func testBooruManagerSupportsOptionalAndCustomUserAgentHeaders() throws {
+ let source = try loadSource(at: "Sora/Data/Booru/BooruManager.swift")
+ let initSignatureCount = tokenCount(
+ matching:
+ #"sendUserAgent:\s*Bool\s*=\s*true,\s*customUserAgent:\s*String\s*=\s*"""#,
+ in: source
+ )
+ let userAgentResolverSection = try extractFunction(
+ named: "private static func resolvedUserAgent(",
+ from: source
+ )
+ let requestURLSection = try extractFunction(
+ named: "func requestURL(_ url: URL) async throws -> Data",
+ from: source
+ )
+ let disableBypassCount = tokenCount(
+ matching: #"guard\s+sendUserAgent\s+else\s*\{\s*return\s+nil\s*\}"#,
+ in: userAgentResolverSection
+ )
+ let customUserAgentTrimCount = tokenCount(
+ matching: #"customUserAgent\.trimmingCharacters"#,
+ in: userAgentResolverSection
+ )
+ let optionalHeaderMappingCount = tokenCount(
+ matching: #"userAgent\.map"#,
+ in: requestURLSection
+ )
+ let explicitUserAgentHeaderCount = tokenCount(
+ matching: #"HTTPHeader\(name:\s*"User-Agent",\s*value:\s*value\)"#,
+ in: requestURLSection
+ )
+
+ // swiftlint:disable:next prefer_nimble
+ XCTAssertGreaterThan(
+ initSignatureCount,
+ 0,
+ "BooruManager should accept booru User-Agent enable and custom override options."
+ )
+ // swiftlint:disable:next prefer_nimble
+ XCTAssertGreaterThan(
+ disableBypassCount,
+ 0,
+ "BooruManager should allow booru requests to omit the User-Agent header."
+ )
+ // swiftlint:disable:next prefer_nimble
+ XCTAssertGreaterThan(
+ customUserAgentTrimCount,
+ 0,
+ "BooruManager should normalize custom booru User-Agent values before use."
+ )
+ // swiftlint:disable:next prefer_nimble
+ XCTAssertGreaterThan(
+ optionalHeaderMappingCount,
+ 0,
+ "BooruManager should only attach request headers when a User-Agent is available."
+ )
+ // swiftlint:disable:next prefer_nimble
+ XCTAssertGreaterThan(
+ explicitUserAgentHeaderCount,
+ 0,
+ "BooruManager should keep using the User-Agent header name for booru requests."
+ )
+ }
+
func testBooruManagerDanbooruFallsBackToUnauthenticatedRequestsOnUnauthorized() throws {
let source = try loadSource(at: "Sora/Data/Booru/BooruManager.swift")
let retrySection = try extractFunction(