~ 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
118 lines · 4.8 KB Raw History
import Testing

@testable import HUDKit

/// An `Action` whose `call` repeats, as Grain's Delete account (a view's
/// `deletes`, drawn as its call): confirmed by the account's handle typed,
/// `call_procedure` called until its answer is done, the button counting
/// what went, then the account signed out.
struct PlanTests {
	let call: JSON = [
		"method": "social.grain.server.getAccountDeletionPlan",
		"service": "did:web:grain.social#grain_appview",
		"input": [:],
	]
	var plan: JSON {
		[
			"label": "Delete account",
			"confirm": "Delete your Grain account?",
			"tone": "danger",
			"handle": "alice.test",
			"signOut": true,
			"repeat": true,
			"call": call,
		]
	}

	/// Its props: the call, repeated, whether it signs out, and the handle
	/// (only with `confirm`, no @); without `repeat` it is a call as any.
	@Test func aRepeatedCallIsRead() throws {
		let spec = try #require(ActionSpec(plan))
		let method = "social.grain.server.getAccountDeletionPlan"
		let service = "did:web:grain.social#grain_appview"
		#expect(spec.request == .repeated(method: method, service: service, input: [:], signOut: true))
		#expect(spec.handle == "alice.test" && spec.danger)
		#expect(try #require(ActionSpec(plan.merged(with: ["handle": "@alice.test"]))).handle == "alice.test")
		#expect(try #require(ActionSpec(plan.merged(with: ["confirm": .null]))).handle == nil)
		#expect(try #require(ActionSpec(plan.merged(with: ["repeat": .null]))).request == .call(method: method, service: service, input: [:]))
	}

	/// It draws only in a trusted view whose app calls tools, and one that
	/// signs out only where the app signs out.
	@MainActor @Test func aRepeatedCallDrawsWhereTheAppCanDoAllOfIt() throws {
		let spec = try #require(ActionSpec(plan))
		let stays = try #require(ActionSpec(plan.merged(with: ["signOut": .null])))
		let view = ViewContext()
		view.onTool = { _ in [:] }
		#expect(!ActionButton.available(spec.request, view))
		view.trusted = true
		#expect(!ActionButton.available(spec.request, view) && ActionButton.available(stays.request, view))
		view.onHost = { _ in }
		#expect(ActionButton.available(spec.request, view))
		view.onTool = nil
		#expect(!ActionButton.available(spec.request, view) && !ActionButton.available(stays.request, view))
	}

	/// The handle typed matches with a leading @, whitespace and case aside.
	@Test func theHandleTypedConfirmsIt() {
		#expect(ActionRun.matches("alice.test", "alice.test"))
		#expect(ActionRun.matches("  @Alice.Test ", "alice.test"))
		#expect(!ActionRun.matches("alice", "alice.test"))
		#expect(!ActionRun.matches("", ""))
	}

	/// What a running repeated call reads.
	@Test func aRunningDeletionCountsWhatWent() {
		#expect(ActionRun.deleting(0) == "Deleting…")
		#expect(ActionRun.deleting(1) == "Deleting… 1 record")
		#expect(ActionRun.deleting(42) == "Deleting… 42 records")
	}

	/// It calls `call_procedure` again while its answer is not done, summing
	/// the records each deleted; a failure stops it, said, nothing signed
	/// out, and sent again it goes on counting; an answer it cannot read
	/// stops it too; done, the account is signed out.
	@MainActor @Test func aRepeatedCallRunsUntilDoneThenSignsOut() async throws {
		let spec = try #require(ActionSpec(plan))
		let view = ViewContext()
		view.trusted = true
		func gone(_ n: Int, done: Bool) -> Result<JSON, HUDError> {
			.success(["output": ["deleted": .array(Array(repeating: "at://x", count: n)), "done": .bool(done)]])
		}
		var answers: [Result<JSON, HUDError>] = [
			gone(100, done: false),
			gone(50, done: false),
			.failure(.tool("The PDS is down.")),
			.success(["output": ["deleted": 3]]),
			gone(7, done: false),
			gone(0, done: true),
		]
		var calls: [Call] = []
		var hosted: [Hosted] = []
		view.onTool = { call in
			calls.append(call)
			return try answers.removeFirst().get()
		}
		view.onHost = { hosted.append($0) }
		let run = ActionRun()
		let write = ActionRun.Pending.write(.call(method: "social.grain.server.getAccountDeletionPlan", service: "did:web:grain.social#grain_appview", input: [:]))
		func send() async {
			run.send(write, spec, view)
			#expect(run.busy && run.word(spec).hasPrefix("Deleting…"))
			var turns = 0
			while run.busy, turns < 1000 {
				await Task.yield()
				turns += 1
			}
		}
		await send()
		#expect(calls.count == 3 && run.deleted == 150 && run.error == "The PDS is down.")
		#expect(hosted.isEmpty && !run.ran.done && run.word(spec) == "Delete account")
		await send()
		#expect(calls.count == 4 && run.deleted == 150 && run.error == "HUD's answer to the deletion could not be read.")
		await send()
		#expect(calls.count == 6 && run.deleted == 157 && run.error == nil)
		#expect(hosted == [.signOut] && run.ran.done)
		#expect(calls.allSatisfy { $0.name == "call_procedure" && $0.args["method"] == "social.grain.server.getAccountDeletionPlan" })
	}
}