import Testing @testable import HUDKit // A tab's root chosen from the account's data (its `first`), as Grain's // Home opens the first feed pinned: the first listed path with a screen. @Suite struct TabRootChoosing { private static func feed(_ id: String, _ more: [String: JSON] = [:]) -> Call { var params: [String: JSON] = ["feed": .string("at://did:web:grain.social/social.grain.feed.generator/\(id)")] params.merge(more) { _, new in new } return Call("query", ["method": "social.grain.feed.getFeed", "params": .object(params)]) } private static let recent = feed("recent") private static let routes: [Route] = [ Route(path: "/feeds/recent", call: recent), Route(path: "/feeds/following", call: feed("following")), Route(path: "/feeds/for-you", call: feed("foryou")), Route(path: "/camera/{name}", call: feed("camera", ["camera": "{name}"]), tab: "Explore"), Route(path: "/location/{h3}?name={name}", call: feed("location", ["location": "{h3}", "name": "{name}"]), tab: "Explore"), ] private static let first = TabFirst([ "call": ["name": "query", "args": ["method": "social.grain.actor.getPreferences", "params": [:]]], "at": "preferences.@pinnedFeedsPref.items", "path": "path", "after": "social.grain.actor.setPreference", ])! private static func pinned(_ paths: [JSON], wrapped: Bool = true) -> JSON { let items: [JSON] = paths.map { ["id": "x", "label": "x", "type": "feed", "path": $0] } let prefs: JSON = ["preferences": [["$type": "social.grain.actor.defs#themePref", "theme": "dark"], ["$type": "social.grain.actor.defs#pinnedFeedsPref", "items": .array(items)]]] return wrapped ? ["response": prefs] : prefs } private static func root(_ paths: [JSON], wrapped: Bool = true) -> Call? { first.root(in: pinned(paths, wrapped: wrapped), own: recent, ownPath: "/", routes: routes) } @Test func readsTheAppFilesFirst() { #expect(Self.first.call == Call("query", ["method": "social.grain.actor.getPreferences", "params": [:]])) #expect(Self.first.at == "preferences.@pinnedFeedsPref.items") #expect(Self.first.path == "path") #expect(Self.first.after == "social.grain.actor.setPreference") #expect(TabFirst(["call": ["name": "query"], "at": "a", "path": "p"])?.after == nil) #expect(TabFirst(nil) == nil) #expect(TabFirst(["at": "a", "path": "p"]) == nil) #expect(TabFirst(["call": ["name": "query"], "path": "p"]) == nil) #expect(TabFirst(["call": ["name": "query"], "at": "a"]) == nil) } @Test func theFirstListedPathWithAScreenIsTheRoot() { #expect(Self.root(["/feeds/for-you", "/feeds/recent"]) == Self.feed("foryou")) #expect(Self.root(["/feeds/following"], wrapped: false) == Self.feed("following")) #expect(Self.root(["/feeds/groups", 5, "/feeds/following"]) == Self.feed("following")) #expect(Self.root(["/camera/Fuji X100V"]) == Self.feed("camera", ["camera": "Fuji X100V"])) #expect(Self.root(["/location/8a2a1072b59ffff?name=Brooklyn%2C+NY"]) == Self.feed("location", ["location": "8a2a1072b59ffff", "name": "Brooklyn, NY"])) // The tab's own path is its own call: Recent, as grain-ios pinned it. #expect(Self.root(["/", "/feeds/for-you"]) == Self.recent) #expect(Self.root(["/?utm_source=x"]) == Self.recent) } @Test func nothingWithAScreenChoosesNothing() { #expect(Self.root([]) == nil) #expect(Self.root(["//evil.test/feeds/recent", "/\\evil.test/feeds/recent", "https://evil.test/feeds/recent", "feeds/recent"]) == nil) #expect(Self.first.root(in: nil, own: Self.recent, ownPath: "/", routes: Self.routes) == nil) #expect(Self.first.root(in: ["preferences": []], own: Self.recent, ownPath: "/", routes: Self.routes) == nil) #expect(Self.first.root(in: Self.pinned(["/"]), own: Self.recent, ownPath: nil, routes: Self.routes) == nil) } @Test func aPathReadsListsByTheirTypes() { let answer: JSON = ["prefs": [["$type": "themePref", "theme": "light"], ["$type": "a.b#pinnedFeedsPref", "items": []]]] #expect(answer.at("prefs.@themePref.theme") == "light") #expect(answer.at("prefs.@pinnedFeedsPref.items") == []) #expect(answer.at("prefs.@nothing") == nil) #expect(answer.at("prefs.theme") == nil) #expect(answer.at("nothing.@themePref") == nil) } }