~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/hud

A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts

0
0
74 lines · 3.3 KB Raw History
import SwiftUI
import Testing

@testable import HUDKit

// The shapes a notification is drawn with, as Grain draws one: a pile of
// faces (as many as it draws, and `+N` past them), a reason's glyph alone
// (filled, in its tint's text colour), a reply's quote, and a thumbnail.

@Suite struct NotificationShapes {
	private func pile(faces: Int, count: Int, max: Int = 5, expand: Bool = true) -> JSON {
		let face: JSON = ["src": .null, "name": "Alice", "handle": "alice.test"]
		var p: [String: JSON] = ["style": "pile", "faces": .array(Array(repeating: face, count: faces)), "count": .number(Double(count)), "max": .number(Double(max))]
		if expand { p["expand"] = true }
		return .object(p)
	}

	@Test func aPileDrawsUpToMaxAndCountsTheRest() {
		#expect(ResolvedPile.shown(pile(faces: 7, count: 7)) == (5, 2))
		#expect(ResolvedPile.shown(pile(faces: 3, count: 3)) == (3, 0))
		#expect(ResolvedPile.shown(pile(faces: 1, count: 1)) == (1, 0))
		// A count past the faces sent still counts them.
		#expect(ResolvedPile.shown(pile(faces: 100, count: 140)) == (5, 135))
	}

	@Test func onlyAPilePastMaxWithExpandLists() {
		#expect(ResolvedPile.lists(pile(faces: 7, count: 7)))
		#expect(!ResolvedPile.lists(pile(faces: 7, count: 7, expand: false)))
		#expect(!ResolvedPile.lists(pile(faces: 4, count: 4)))
	}

	@Test func aGlyphIsFilledWhereItsSquareIsNot() {
		#expect(ResolvedIcon.symbol("heart", glyph: true) == .system("heart.fill"))
		#expect(ResolvedIcon.symbol("heart") == .system("heart"))
		#expect(ResolvedIcon.symbol("follow", glyph: true) == .system("person.fill.badge.plus"))
		#expect(ResolvedIcon.symbol("comment", glyph: true) == .system("text.bubble.fill"))
		#expect(ResolvedIcon.symbol("reply", glyph: true) == .system("arrowshape.turn.up.backward.fill"))
		#expect(ResolvedIcon.symbol("mention", glyph: true) == .system("at"))
		#expect(ResolvedIcon.symbol("unknown", glyph: true) == nil)
	}

	@Test @MainActor func aGlyphTakesItsTintsTextColour() {
		#expect(ResolvedIcon.tone("red")?.role == .danger)
		#expect(ResolvedIcon.tone("blue")?.role == .accent)
		#expect(ResolvedIcon.tone("green")?.role == .success)
		#expect(ResolvedIcon.tone("purple")?.role == .done)
		#expect(ResolvedIcon.tone("gray")?.role == .textFaint)
		#expect(ResolvedIcon.tone("orange")?.role == .attention)
		#expect(ResolvedIcon.tone("pink") == nil)
	}

	@Test func aQuoteIsSmallAndQuiet() {
		#expect(TextBody.quiet("quote"))
		#expect(TextBody.quiet("muted"))
		#expect(!TextBody.quiet("body"))
		#expect(ImageBox.side("thumb") == 44)
	}

	@Test @MainActor func anInlineRowIsOneTextOfItsWords() throws {
		func text(_ s: String, _ style: String? = nil) -> JSON {
			var p: [String: JSON] = ["runs": [["text": .string(s)]]]
			if let style { p["style"] = .string(style) }
			return ["c": "Text", "p": .object(p)]
		}
		let line = try #require(Resolved.inline([text("Alice", "strong"), text("and 6 others"), text("favorited your gallery"), ["c": "Time", "p": ["value": "not a time"]]]))
		#expect(String(line.characters) == "Alice and 6 others favorited your gallery not a time")
		let name = try #require(line.runs.first)
		#expect(String(line[name.range].characters) == "Alice")
		#expect(name.inlinePresentationIntent == .stronglyEmphasized)
		#expect(line.runs.last?.foregroundColor == .secondary)
		// Anything else draws as a Row does.
		#expect(Resolved.inline([text("Alice"), ["c": "Image", "p": [:]]]) == nil)
	}
}