~ 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
437 lines · 21.5 KB Raw History
import CoreGraphics
import Foundation
import ImageIO
import Testing
import UIKit
import UniformTypeIdentifiers

@testable import HUDKit

// What a `Compose` form sends: its values from its fields (absent ones
// left out, the view's `given` beside them), its text's links, mentions
// and tags by the server's rules (crates/hud-core/src/richtext.rs), and a
// photo's EXIF as Grain writes it, made ready to upload.

@Suite struct Composing {
	private static let reply: JSON = ["root": ["uri": "at://did:plc:a/app.bsky.feed.post/1", "cid": "bafy"], "parent": ["uri": "at://did:plc:a/app.bsky.feed.post/2", "cid": "bafz"]]

	private static let p: JSON = [
		"key": "k", "label": "Reply", "send": "Reply",
		"fields": [
			["name": "text", "kind": "text", "max": 300, "facets": true],
			["name": "photos", "kind": "photos", "min": 0, "max": 4, "alt": true, "maxBytes": 1_000_000, "maxSide": 2000],
			["name": "langs", "kind": "langs", "max": 3],
			["name": "labels", "kind": "labels", "options": [["value": "porn", "label": "Adult"]]],
			[
				"name": "gate", "kind": "choice", "multiple": true,
				"options": [
					["label": "Everybody", "value": .null, "only": true],
					["label": "Nobody", "value": [], "only": true],
					["label": "Mentioned", "value": [["$type": "app.bsky.feed.threadgate#mentionRule"]]],
					["label": "Following", "value": [["$type": "app.bsky.feed.threadgate#followingRule"]]],
				],
			],
			["name": "place", "kind": "place"],
			["name": "card", "kind": "card", "from": "text"],
			["name": "kind", "kind": "unknown"],
		],
		"given": ["reply": reply],
		"writes": [],
	]

	@MainActor @Test func valuesAreTheGivenAndTheFieldsGivenAValue() {
		let fields = ComposeField.all(Self.p)
		#expect(fields.map(\.name) == ["text", "photos", "langs", "labels", "gate", "place", "card"])
		let given = Self.p["given"]
		let form = ComposeForm(fields: fields)
		// Nothing written: the given alone (Everybody, a null standing alone, is absent).
		form.langs["langs"] = []
		#expect(form.values(fields, given: given, uploads: true) == ["reply": Self.reply])
		#expect(form.blocked(fields, given: given, uploads: true) != nil)

		form.texts["text"] = "  see https://a.example/x and @alice.test  \n"
		form.langs["langs"] = ["en"]
		form.labels["labels"] = "porn"
		form.choices["gate"] = [2, 3]
		form.places["place"] = ["name": "Lisbon", "value": "geo:38.7,-9.1"]
		let card: JSON = ["uri": "https://a.example/x", "title": "A", "description": "a page"]
		form.cards["card"] = ComposeCard(url: "https://a.example/x", card: card, image: "https://cdn.example/a.jpg")
		#expect(form.blocked(fields, given: given, uploads: true) == nil)
		#expect(
			form.values(fields, given: given, uploads: true) == [
				"reply": Self.reply,
				"text": "see https://a.example/x and @alice.test",
				"langs": ["en"],
				"labels": ["porn"],
				"gate": [["$type": "app.bsky.feed.threadgate#mentionRule"], ["$type": "app.bsky.feed.threadgate#followingRule"]],
				"place": ["name": "Lisbon", "value": "geo:38.7,-9.1"],
				"card": card,
			]
		)
		// Nobody: an empty array, sent as it is.
		form.choices["gate"] = [1]
		#expect(form.values(fields, given: given, uploads: true)["gate"] == [])
		// A quote takes no card; nor does a text whose link is another.
		var quoting = given?.object ?? [:]
		quoting["quote"] = ["uri": "at://did:plc:a/app.bsky.feed.post/3", "cid": "bafq"]
		#expect(form.values(fields, given: .object(quoting), uploads: true)["card"] == nil)
		form.texts["text"] = "see https://b.example"
		#expect(form.values(fields, given: given, uploads: true)["card"] == nil)
		// Past its limit, it is not sent.
		form.texts["text"] = String(repeating: "é", count: 301)
		#expect(form.blocked(fields, given: given, uploads: true) == "Too long")
	}

	/// A text's or line's limits are its record's lexicon's: a gallery's
	/// title 100 UTF-8 bytes, so 60 accented letters and emoji are past it;
	/// a comment's 300 graphemes and 3000 bytes, what is left said of the
	/// nearer, within the larger of 10 and a fifth of it.
	@MainActor @Test func aFieldIsHeldToItsGraphemesAndItsBytes() {
		let title = ComposeField(p: ["name": "title", "kind": "line", "maxBytes": 100, "required": true])
		let sixty = String(repeating: "é", count: 40) + String(repeating: "🌄", count: 5) + String(repeating: "a", count: 15)
		#expect(sixty.count == 60)
		#expect(title.room(sixty) == ComposeRoom(left: -15, near: true))
		#expect(title.room(String(repeating: "é", count: 50)) == ComposeRoom(left: 0, near: true))
		#expect(title.room(String(repeating: "é", count: 40)) == ComposeRoom(left: 20, near: true))
		#expect(title.room(String(repeating: "é", count: 39)) == ComposeRoom(left: 22, near: false))
		let comment = ComposeField(p: ["name": "text", "kind": "text", "max": 300, "maxBytes": 3000])
		#expect(comment.room(String(repeating: "a", count: 250)) == ComposeRoom(left: 50, near: true))
		// A letter under twelve accents is one grapheme of 25 bytes: the bytes, 3000, come long before 300 of them.
		#expect(comment.room(String(repeating: "a" + String(repeating: "\u{0301}", count: 12), count: 130)) == ComposeRoom(left: -250, near: true))
		#expect(comment.room("  a \n") == ComposeRoom(left: 299, near: false))
		#expect(ComposeField(p: ["name": "x", "kind": "text"]).room("a") == nil)
		// Past its bytes, a title is not sent; at them, it is.
		let form = ComposeForm(fields: [title])
		form.texts["title"] = sixty
		#expect(form.blocked([title], given: nil, uploads: true) == "Too long")
		form.texts["title"] = String(repeating: "é", count: 50)
		#expect(form.blocked([title], given: nil, uploads: true) == nil)
	}

	@MainActor @Test func aFormThatNeedsAPhotoIsOfferedOnlyWhereTheAppUploads() {
		let p: JSON = ["fields": [["name": "photos", "kind": "photos", "min": 1, "max": 10]]]
		let gallery = ComposeField.all(p)
		let context = ViewContext()
		context.onTool = { _ in [:] }
		#expect(!ResolvedCompose.offered(p, gallery, context))
		context.onUpload = { _, _ in [:] }
		#expect(ResolvedCompose.offered(p, gallery, context))
		context.onTool = nil
		#expect(!ResolvedCompose.offered(p, gallery, context))
	}

	/// Where it can't be sent, a Compose draws its `signIn` words where the
	/// app signs in (Sign in to comment), else its count, but a field
	/// (Add a comment…) nothing.
	@MainActor @Test func unsentItSaysSignInWhereTheAppSignsIn() {
		let context = ViewContext()
		let comment: JSON = ["label": "Comment", "count": 3, "signIn": "Sign in to comment"]
		#expect(ResolvedCompose.unsent(comment, count: 3, context) == .count(3))
		#expect(ResolvedCompose.unsent(["label": "Comment", "style": "field"], count: 3, context) == .nothing)
		context.onSignIn = {}
		#expect(ResolvedCompose.unsent(comment, count: 3, context) == .signIn("Sign in to comment"))
		#expect(ResolvedCompose.unsent(["label": "Comment"], count: nil, context) == .nothing)
	}

	/// A Compose its screen was opened at (`open`) opens its form by itself
	/// once in a view, and only in the tree in front: a story pager's page
	/// drawn ahead (`active` false) waits, and opens it once in front; drawn
	/// again (a story deleted, the view read again), it stays shut; one not
	/// `open` never does, and another view opens it.
	@MainActor @Test func aFormItsScreenWasOpenedAtOpensOnceInAView() {
		let add: JSON = ["key": "add", "label": "Add to your story", "open": true]
		let context = ViewContext()
		#expect(!ResolvedCompose.opensAtOnce(["key": "reply", "label": "Reply"], context))
		context.active = false
		#expect(!ResolvedCompose.opensAtOnce(add, context))
		context.active = true
		#expect(ResolvedCompose.opensAtOnce(add, context))
		#expect(!ResolvedCompose.opensAtOnce(add, context))
		#expect(ResolvedCompose.opensAtOnce(add, ViewContext()))
		// A page the app lets open no form (a story pager's page not tapped) never does.
		let untapped = ViewContext()
		untapped.opensForms = false
		#expect(!ResolvedCompose.opensAtOnce(add, untapped))
	}

	/// A form with a `call` sends its input with the fields' values at its
	/// top, those with none left out so the input's own stand; it is
	/// offered only in a trusted view whose app acts.
	@MainActor @Test func aCallsInputTakesTheFieldsValuesAtItsTop() throws {
		let p: JSON = [
			"label": "Comment",
			"fields": [["name": "text", "kind": "text", "max": 300], ["name": "title", "kind": "line"]],
			"call": ["method": "social.grain.comment.create", "service": "did:web:grain.test#grain", "input": ["subject": "at://did:plc:a/social.grain.gallery/1", "text": "", "title": "Untitled", "createdAt": ["$now": true]]],
		]
		let call = try #require(ResolvedCompose.procedure(p))
		#expect(call.method == "social.grain.comment.create")
		let fields = ComposeField.all(p)
		let form = ComposeForm(fields: fields)
		form.texts["text"] = "  lovely  "
		form.texts["title"] = "   "
		#expect(form.input(fields, into: call.input, given: nil, uploads: true) == ["subject": "at://did:plc:a/social.grain.gallery/1", "text": "lovely", "title": "Untitled", "createdAt": ["$now": true]])
		#expect(ResolvedCompose.procedure(["writes": []]) == nil)

		let context = ViewContext()
		context.onTool = { _ in [:] }
		#expect(!ResolvedCompose.offered(p, fields, context))
		context.onAction = { _ in nil }
		#expect(!ResolvedCompose.offered(p, fields, context))
		context.trusted = true
		#expect(ResolvedCompose.offered(p, fields, context))
	}

	/// An Edit profile form opens holding what the profile says: its texts
	/// written in, sent as they are unless changed; its avatar shown and
	/// sent as nothing (the writes keep what is stored) until another is
	/// picked. One whose value the server found none of opens empty.
	@MainActor @Test func anEditFormOpensHoldingWhatIsThere() {
		let p: JSON = [
			"key": "k", "label": "Edit profile", "send": "Save",
			"fields": [
				["name": "avatar", "kind": "photos", "min": 0, "max": 1, "maxBytes": 1_000_000, "maxSide": 1000, "round": true, "picture": ["src": "https://cdn.example/a.jpg", "fallback": .null]],
				["name": "name", "kind": "line", "max": 64, "value": "Ana"],
				["name": "description", "kind": "text", "max": 256, "facets": true, "value": "photos @bob.test"],
				["name": "note", "kind": "line", "max": 64],
			],
			"given": [:],
			"writes": [],
		]
		let fields = ComposeField.all(p)
		#expect(fields[0].picture == Source(src: "https://cdn.example/a.jpg", fallback: nil))
		#expect(fields[0].round && !fields[1].round)
		#expect(fields[3].picture == nil && fields[3].value == nil)
		let form = ComposeForm(fields: fields)
		#expect(form.texts == ["name": "Ana", "description": "photos @bob.test"])
		#expect(form.values(fields, given: p["given"], uploads: true) == ["name": "Ana", "description": "photos @bob.test"])
		#expect(form.blocked(fields, given: p["given"], uploads: true) == nil)
		// The description cleared: left out, so the update takes it out.
		form.texts["description"] = " "
		#expect(form.values(fields, given: p["given"], uploads: true) == ["name": "Ana"])
	}

	/// The table the server's, the web composer's and the Android kit's tests read too.
	@Test func marksAsEveryCopyOfTheRuleDoes() throws {
		let url = try #require(Bundle.module.url(forResource: "richtext", withExtension: "json", subdirectory: "Fixtures"))
		let table = try #require(try JSONSerialization.jsonObject(with: Data(contentsOf: url)) as? [String: Any])
		for case let c as [String: Any] in try #require(table["cases"] as? [Any]) {
			let text = try #require(c["text"] as? String)
			let want = try #require(c["marks"] as? [[String: Any]]).map { m in
				"\(m["text"]!) \(m["kind"]!) \(m["value"]!) \(m["byteStart"]!)"
			}
			let found = Facets.marks(text).map { m -> String in
				let (kind, value): (String, String) = switch m.kind {
				case .link(let v): ("link", v)
				case .mention(let v): ("mention", v)
				case .tag(let v): ("tag", v)
				}
				return "\(text[m.range]) \(kind) \(value) \(text.utf8.distance(from: text.startIndex, to: m.range.lowerBound))"
			}
			#expect(found == want, "\(text)")
		}
	}

	@Test func linksMentionsAndTagsAsTheServerMarksThem() {
		let text = "héllo @Alice.test (see https://a.example/x?y=1). #atproto #2026 @nope # #日本 #a#b http://localhost"
		let shown = Facets.marks(text).map { (String(text[$0.range]), $0.kind) }
		#expect(shown.map(\.0) == ["@Alice.test", "https://a.example/x?y=1", "#atproto", "#日本"])
		#expect(shown.map(\.1) == [.mention("alice.test"), .link("https://a.example/x?y=1"), .tag("atproto"), .tag("日本")])
		// As the editor marks them: UTF-16, é one unit.
		let first = Facets.marks(text)[0]
		#expect(NSRange(first.range, in: text) == NSRange(location: 6, length: 11))
		#expect(Facets.marks("plain words, http://localhost and ftp://a.example").isEmpty)
		#expect(Facets.marks("#" + String(repeating: "a", count: 65)).isEmpty)
		#expect(Facets.firstLink("a (https://b.example/c), https://d.example") == "https://b.example/c")
	}

	@Test func theAtWordTypedIsWhatFollowsItsAt() {
		let text = "hi @da there"
		let caret = text.index(text.startIndex, offsetBy: 6)
		let typed = Facets.typing(text, caret: caret)
		#expect(typed?.prefix == "da")
		#expect(typed.map { String(text[$0.range]) } == "@da")
		#expect(Facets.typing("hi @", caret: "hi @".endIndex) == nil)
		#expect(Facets.typing("mail@da", caret: "mail@da".endIndex) == nil)
	}

	@MainActor @Test func aPickedAccountTakesThePlaceOfTheAtWord() {
		let form = ComposeForm(fields: [])
		form.texts["text"] = "hi @da"
		form.carets["text"] = 6
		form.pick(ComposeSuggestion(did: "did:plc:dan", handle: "danabra.mov", name: "dan", avatar: nil), in: "text")
		#expect(form.texts["text"] == "hi @danabra.mov ")
		#expect(form.carets["text"] == 16)
	}

	@Test func exifIsGrainsItsTimesAndLengthsAMillionTimesLarger() {
		let properties: [String: Any] = [
			kCGImagePropertyExifDictionary as String: [
				kCGImagePropertyExifExposureTime as String: 0.004,
				kCGImagePropertyExifFNumber as String: 2.8,
				kCGImagePropertyExifFocalLenIn35mmFilm as String: 35,
				kCGImagePropertyExifISOSpeedRatings as String: [400],
				kCGImagePropertyExifFlash as String: 16,
				kCGImagePropertyExifDateTimeOriginal as String: "2024:05:01 14:03:22",
				kCGImagePropertyExifOffsetTimeOriginal as String: "+02:00",
				kCGImagePropertyExifLensModel as String: "XF23mmF2",
			],
			kCGImagePropertyTIFFDictionary as String: [
				kCGImagePropertyTIFFMake as String: "FUJIFILM",
				kCGImagePropertyTIFFModel as String: "X100V ",
			],
		]
		#expect(
			Exif.grain(properties) == [
				"dateTimeOriginal": "2024-05-01T12:03:22.000Z",
				"exposureTime": 4000,
				"fNumber": 2_800_000,
				"focalLengthIn35mmFormat": 35,
				"iSO": 400,
				"flash": "Flash did not fire, compulsory flash mode",
				"make": "FUJIFILM",
				"model": "X100V",
				"lensModel": "XF23mmF2",
			]
		)
		// No offset: the time in the zone it is read in.
		let zoned: [String: Any] = [kCGImagePropertyExifDictionary as String: [kCGImagePropertyExifDateTimeOriginal as String: "2024:05:01 14:03:22"]]
		#expect(Exif.grain(zoned, timeZone: TimeZone(identifier: "America/Los_Angeles")!) == ["dateTimeOriginal": "2024-05-01T21:03:22.000Z"])
		#expect(Exif.grain([:]) == nil)
	}

	/// Where a photo was taken, from its GPS: south and west below zero;
	/// none without both, past the degrees, or at 0, 0.
	@Test func aPhotosPointIsWhereItWasTaken() {
		let gps = { (lat: Double, latRef: String, lon: Double, lonRef: String) -> [String: Any] in
			[kCGImagePropertyGPSDictionary as String: [
				kCGImagePropertyGPSLatitude as String: lat, kCGImagePropertyGPSLatitudeRef as String: latRef,
				kCGImagePropertyGPSLongitude as String: lon, kCGImagePropertyGPSLongitudeRef as String: lonRef,
			]]
		}
		#expect(PhotoPoint.of(gps(38.7, "N", 9.1, "W")) == PhotoPoint(lat: 38.7, lon: -9.1))
		#expect(PhotoPoint.of(gps(33.9, "S", 151.2, "E")) == PhotoPoint(lat: -33.9, lon: 151.2))
		#expect(PhotoPoint.of(gps(0, "N", 0, "E")) == nil)
		#expect(PhotoPoint.of(gps(91, "N", 0, "E")) == nil)
		#expect(PhotoPoint.of([kCGImagePropertyGPSDictionary as String: [kCGImagePropertyGPSLatitude as String: 1.0]]) == nil)
		#expect(PhotoPoint.of([:]) == nil)
	}

	/// A place with `from` is filled from the place at its photos' first
	/// point, once a photo: taken away, it stays away; one picked first,
	/// or none found there, is left as it is.
	@MainActor @Test func aPlaceIsFilledFromWhereItsFirstPhotoWasTaken() async {
		let fields = ComposeField.all(["fields": [
			["name": "photos", "kind": "photos", "max": 4],
			["name": "place", "kind": "place", "from": "photos"],
			["name": "other", "kind": "place"],
		]])
		let (photosField, place, other) = (fields[0], fields[1], fields[2])
		let form = ComposeForm(fields: fields)
		let bare = ComposePhoto()
		bare.prepared = PreparedPhoto(jpeg: Data(), width: 3, height: 2, exif: nil, point: nil)
		let there = ComposePhoto()
		there.prepared = PreparedPhoto(jpeg: Data(), width: 3, height: 2, exif: nil, point: PhotoPoint(lat: 38.7, lon: -9.1))
		form.photos[photosField.name] = [bare, there]
		#expect(form.locatedPhoto(place) === there)
		#expect(form.locatedPhoto(other) == nil)
		var asked: [PhotoPoint] = []
		let lisbon: JSON = ["name": "Lisbon", "value": "8a393362cb4ffff"]
		let find = { (point: PhotoPoint) async throws -> JSON in
			asked.append(point)
			return ["places": [lisbon]]
		}
		await form.locate(place, find: find)
		#expect(form.places["place"] == lisbon)
		#expect(asked == [PhotoPoint(lat: 38.7, lon: -9.1)])
		#expect(form.locating.isEmpty)
		// Taken away, it stays away.
		form.places["place"] = nil
		await form.locate(place, find: find)
		#expect(form.places["place"] == nil)
		#expect(asked.count == 1)

		// One picked first is kept; none found there is none.
		let picked = ComposeForm(fields: fields)
		let again = ComposePhoto()
		again.prepared = there.prepared
		picked.photos[photosField.name] = [again]
		picked.places["place"] = ["name": "Porto"]
		await picked.locate(place, find: find)
		#expect(picked.places["place"] == ["name": "Porto"])
		#expect(asked.count == 1)
		picked.places["place"] = nil
		await picked.locate(place) { _ in ["places": []] }
		#expect(picked.places["place"] == nil)
	}

	@Test func aPhotoIsMadeSmallEnoughAsAJPEG() throws {
		let context = try #require(CGContext(data: nil, width: 3000, height: 2000, bitsPerComponent: 8, bytesPerRow: 0, space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.noneSkipLast.rawValue))
		for i in 0..<60 {
			context.setFillColor(red: CGFloat(i % 7) / 7, green: CGFloat(i % 5) / 5, blue: CGFloat(i % 3) / 3, alpha: 1)
			context.fill(CGRect(x: i * 50, y: (i * 37) % 2000, width: 50, height: 2000))
		}
		let image = try #require(context.makeImage())
		let png = NSMutableData()
		let out = try #require(CGImageDestinationCreateWithData(png, UTType.png.identifier as CFString, 1, nil))
		CGImageDestinationAddImage(out, image, nil)
		#expect(CGImageDestinationFinalize(out))

		let made = try PreparedPhoto.make(png as Data, maxSide: 1000, maxBytes: 200_000, exif: true)
		#expect(made.width == 1000)
		#expect(made.height == 667)
		#expect(made.jpeg.count <= 200_000)
		#expect(made.exif == nil)
		let read = try #require(CGImageSourceCreateWithData(made.jpeg as CFData, nil))
		#expect(CGImageSourceGetType(read) as String? == UTType.jpeg.identifier)
		#expect(throws: PreparedPhoto.Failure.self) { try PreparedPhoto.make(Data("not an image".utf8), maxSide: 1000, maxBytes: 1000, exif: false) }
	}

	/// A story's form (`form: photo`) is a review of its first field, one
	/// photo with Camera beside the library, and of the place filled from
	/// it; Camera with no camera on the device (a simulator) picks from the
	/// library; a photo taken is made ready and uploaded as one picked is.
	@MainActor @Test func aStoryTakesAPhotoAndReviewsIt() async throws {
		let story: JSON = ["form": "photo", "fields": [
			["name": "photos", "kind": "photos", "min": 1, "max": 1, "maxBytes": 1_000_000, "maxSide": 1000, "camera": true],
			["name": "place", "kind": "place", "from": "photos"],
			["name": "note", "kind": "line", "max": 10],
		]]
		let fields = ComposeField.all(story)
		let review = try #require(ComposeField.review(story, fields))
		#expect(review.photos.name == "photos" && review.photos.camera && review.place?.name == "place")
		#expect(!fields[2].camera)
		// Alt text is asked only where the field says `alt: true` (a gallery's, a post's), not a story's or an avatar's.
		#expect(!review.photos.alt)
		#expect(ComposeField(p: ["name": "photos", "kind": "photos", "max": 10, "alt": true]).alt)
		#expect(!ComposeField(p: ["name": "avatar", "kind": "photos", "max": 1, "round": true]).alt)
		#expect(!ComposeField(p: ["name": "photos", "kind": "photos", "max": 4, "alt": false]).alt)
		// No review without form photo, or with another field first.
		#expect(ComposeField.review(["fields": story["fields"] ?? .null], fields) == nil)
		#expect(ComposeField.review(story, Array(fields.reversed())) == nil)

		#expect(ComposeField.cameraTap(available: true) == .camera)
		#expect(ComposeField.cameraTap(available: false) == .library)
		#if targetEnvironment(simulator)
			// A simulator's camera is a source with no camera behind it.
			#expect(!CameraPicker.available)
		#endif

		let format = UIGraphicsImageRendererFormat()
		format.scale = 1
		let taken = UIGraphicsImageRenderer(size: CGSize(width: 300, height: 400), format: format).image { context in
			UIColor.systemTeal.setFill()
			context.fill(CGRect(x: 0, y: 0, width: 300, height: 400))
		}
		let form = ComposeForm(fields: fields)
		var sent: [Int] = []
		await form.take(taken, into: review.photos) { data, progress in
			sent.append(data.count)
			progress(1)
			return ["$type": "blob", "ref": ["$link": "bafy"], "mimeType": "image/jpeg", "size": .number(Double(data.count))]
		}.value
		let photo = try #require(form.photos["photos"]?.first)
		#expect(photo.value?["aspectRatio"] == ["width": 300, "height": 400])
		#expect(sent.count == 1)
		#expect(form.blocked(fields, given: nil, uploads: true) == nil)
	}
}