chadtmiller.com/hud
A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts
place.hud.hudkit 0.4.0 Latest 5 packages
0
0
import Foundation
import Testing
@testable import HUDKit
// An app's own sign-in screen as its app file's `signIn` says, as Grain's
// does: its lines, the typeahead's URL built from what is typed, and its
// answer's items read through the app's paths.
@Suite struct SigningInAsAnAppSays {
/// Grain's app file, its domain and its sign-in.
private static let grain: JSON = [
"domain": "grain.social",
"signIn": [
"title": "Log in with your atmosphere account",
"text": "[What is an atmosphere account?](https://atmosphereaccount.com/)",
"placeholder": "e.g. user.bsky.social",
"legal": "By signing in you agree to our [Terms](/support/terms), [Privacy Policy](/support/privacy), and [Community Guidelines](/support/community-guidelines).",
"create": ["text": "Don't have an account?", "label": "Create one"],
"typeahead": [
"url": "https://typeahead.waow.tech",
"method": "tech.waow.typeahead.searchActors",
"param": "q",
"params": ["limit": 5],
"items": "actors",
"did": "did",
"handle": "handle",
"name": "displayName",
"avatar": "avatar",
"avatarFallback": "avatarFallback",
],
],
]
private static let plan = SignInPlan(app: grain)!
@Test func grainsSignInIsRead() throws {
let plan = Self.plan
#expect(plan.title == "Log in with your atmosphere account")
#expect(plan.text == "[What is an atmosphere account?](https://atmosphereaccount.com/)")
#expect(plan.placeholder == "e.g. user.bsky.social")
#expect(plan.legal?.hasPrefix("By signing in you agree") == true)
#expect(plan.create == SignInPlan.Create(text: "Don't have an account?", label: "Create one"))
#expect(plan.site == URL(string: "https://grain.social"))
let typeahead = try #require(plan.typeahead)
#expect(typeahead.url == URL(string: "https://typeahead.waow.tech"))
#expect(typeahead.method == "tech.waow.typeahead.searchActors")
#expect(typeahead.params == ["limit": "5"])
#expect(typeahead.paths == FoundActor.Paths(did: "did", handle: "handle", name: "displayName", avatar: "avatar", avatarFallback: "avatarFallback"))
}
@Test func anAppWithoutASignInHasNone() {
#expect(SignInPlan(app: ["domain": "grain.social"]) == nil)
#expect(SignInPlan(app: ["signIn": ["text": "No heading"]]) == nil)
let bare = SignInPlan(app: ["signIn": ["title": "Sign in", "create": ["text": "No label"], "typeahead": ["url": "http://typeahead.test", "method": "a.b.c"]]])
#expect(bare?.title == "Sign in")
#expect(bare?.create == nil)
#expect(bare?.typeahead == nil)
}
@Test func itsLinksArePathsOfTheSiteOrHTTPS() {
let legal = Self.plan.linked(Self.plan.legal!)
let links = legal.runs.compactMap(\.link)
#expect(links == [
URL(string: "https://grain.social/support/terms"), URL(string: "https://grain.social/support/privacy"),
URL(string: "https://grain.social/support/community-guidelines"),
])
#expect(Self.plan.linked(Self.plan.text!).runs.compactMap(\.link) == [URL(string: "https://atmosphereaccount.com/")])
#expect(SignInPlan.linked("[x](javascript:alert(1)) [y](http://a.test)", site: nil).runs.compactMap(\.link).isEmpty)
}
@Test func theTypeaheadIsAskedWhatIsTyped() throws {
let typeahead = try #require(Self.plan.typeahead)
let url = try #require(typeahead.request(for: " @chadtm "))
let components = try #require(URLComponents(url: url, resolvingAgainstBaseURL: false))
#expect(components.scheme == "https")
#expect(components.host == "typeahead.waow.tech")
#expect(components.path == "/xrpc/tech.waow.typeahead.searchActors")
#expect(components.queryItems == [URLQueryItem(name: "q", value: "chadtm"), URLQueryItem(name: "limit", value: "5")])
}
@Test func underTwoLettersAsksNothing() async throws {
let typeahead = try #require(Self.plan.typeahead)
#expect(typeahead.request(for: "c") == nil)
#expect(typeahead.request(for: " @c ") == nil)
#expect(await typeahead.search("@") == [])
}
@Test func itemsAreReadThroughTheAppsPaths() {
let typeahead = Typeahead(
url: URL(string: "https://typeahead.test")!, method: "a.b.c", items: "result.people",
paths: FoundActor.Paths(did: "id", handle: "profile.handle", name: "profile.name", avatar: "pic.main", avatarFallback: "pic.backup")
)
let answer: JSON = [
"result": [
"people": [
[
"id": "did:plc:a", "profile": ["handle": "alice.test", "name": "Alice"],
"pic": ["main": "https://cdn.test/a.jpg", "backup": "https://fallback.test/a.jpg"],
],
["id": "did:plc:b", "profile": ["handle": "bob.test"], "pic": ["main": "http://cdn.test/b.jpg", "backup": "https://fallback.test/b.jpg"]],
["id": "did:plc:c", "profile": ["handle": "handle.invalid"]],
["id": "carol", "profile": ["handle": "carol.test"]],
["profile": ["handle": "dave.test"]],
["id": "did:plc:e"],
],
],
]
let found = typeahead.actors(in: answer)
#expect(found.map(\.handle) == ["alice.test", "bob.test"])
#expect(found[0].displayName == "Alice")
#expect(found[0].avatar == URL(string: "https://cdn.test/a.jpg"))
#expect(found[0].avatarFallback == URL(string: "https://fallback.test/a.jpg"))
#expect(found[1].displayName == nil)
#expect(found[1].avatar == nil)
#expect(found[1].avatarFallback == URL(string: "https://fallback.test/b.jpg"))
#expect(typeahead.actors(in: ["people": []]) == [])
}
@Test func hudsOwnTypeaheadAsksItsServer() throws {
let typeahead = Typeahead(url: URL(string: "http://localhost:8080")!, method: "place.hud.searchActors", params: ["limit": "6"])
let url = try #require(typeahead.request(for: "al"))
#expect(url.absoluteString == "http://localhost:8080/xrpc/place.hud.searchActors?q=al&limit=6")
}
}