~ 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
101 lines · 4.9 KB Raw History
import Foundation
import Testing

@testable import HUDKit

// The adaptive layout parts: a size token as points, a grid's columns (a
// List's and Covers') as many as fit and no more than its cap, and a
// Split side by side where both its sides fit, else one under the other.

@Suite struct Adapting {
	@Test func aSizeTokenIsItsPointsAndAnotherNone() {
		#expect(Adaptive.size("sm") == 96)
		#expect(Adaptive.size("md") == 160)
		#expect(Adaptive.size("lg") == 240)
		#expect(Adaptive.size("xl") == nil)
		#expect(Adaptive.size(nil) == nil)
	}

	@Test func aColumnsCapIsAWholeNumberFromOneToSix() {
		#expect(Adaptive.cap(.number(1)) == 1)
		#expect(Adaptive.cap(.number(6)) == 6)
		#expect(Adaptive.cap(.number(0)) == nil)
		#expect(Adaptive.cap(.number(7)) == nil)
		#expect(Adaptive.cap(.number(2.5)) == nil)
		#expect(Adaptive.cap("3") == nil)
		#expect(Adaptive.cap(nil) == nil)
	}

	@Test func aGridTakesAsManyColumnsAsFitAndNoMoreThanItsCap() {
		// A grid's 160 points, 12 apart: three in 600, one in 100.
		#expect(Adaptive.columns(width: 600, min: 160, gap: 12, cap: nil) == 3)
		#expect(Adaptive.columns(width: 100, min: 160, gap: 12, cap: nil) == 1)
		// Exactly two with the gap between them.
		#expect(Adaptive.columns(width: 332, min: 160, gap: 12, cap: nil) == 2)
		#expect(Adaptive.columns(width: 331, min: 160, gap: 12, cap: nil) == 1)
		// The cap is the most, not how many.
		#expect(Adaptive.columns(width: 600, min: 160, gap: 12, cap: 2) == 2)
		#expect(Adaptive.columns(width: 600, min: 160, gap: 12, cap: 6) == 3)
		#expect(Adaptive.columns(width: 2000, min: 96, gap: 12, cap: 6) == 6)
		#expect(Adaptive.columns(width: 0, min: 96, gap: 12, cap: 4) == 1)
	}

	@Test func coversTakeTheirColumnsAsTheirStyleAndCapSay() {
		// Tiles: three at the least, more while each keeps 120 points.
		#expect(ResolvedCovers.across(width: 390, tiles: true, min: nil, cap: nil) == 3)
		#expect(ResolvedCovers.across(width: 200, tiles: true, min: nil, cap: nil) == 3)
		#expect(ResolvedCovers.across(width: 1000, tiles: true, min: nil, cap: nil) == 8)
		#expect(ResolvedCovers.across(width: 1000, tiles: true, min: nil, cap: 4) == 4)
		#expect(ResolvedCovers.across(width: 390, tiles: true, min: nil, cap: 2) == 2)
		// A min of their own: as many as fit, one at the least.
		#expect(ResolvedCovers.across(width: 200, tiles: true, min: 96, cap: nil) == 2)
		#expect(ResolvedCovers.across(width: 200, tiles: true, min: 240, cap: nil) == 1)
		// Cards: as many of 150 as fit.
		#expect(ResolvedCovers.across(width: 390, tiles: false, min: nil, cap: nil) == 2)
		#expect(ResolvedCovers.across(width: 390, tiles: false, min: nil, cap: 1) == 1)
		#expect(ResolvedCovers.across(width: 390, tiles: false, min: 240, cap: nil) == 1)
		#expect(ResolvedCovers.across(width: 1200, tiles: false, min: 240, cap: 3) == 3)
	}

	@Test func aSplitSitsSideBySideWhereBothItsSidesFit() {
		// md and sm where none is given, 12 apart: 268 points.
		let end = SplitLayout(["side": "end"])
		#expect(end.widths(268) == [160, 96])
		#expect(end.widths(267) == nil)
		// The aside at its least, the main the rest, in their order.
		#expect(end.widths(500) == [392, 96])
		#expect(SplitLayout(nil).widths(500) == [392, 96])
		#expect(SplitLayout(["side": "start"]).widths(500) == [96, 392])
		// Sizes of its own; an unknown one its default.
		let wide = SplitLayout(["main": "lg", "aside": "md"])
		#expect(wide.widths(411) == nil)
		#expect(wide.widths(412) == [240, 160])
		#expect(SplitLayout(["main": "xl", "aside": "huge"]).widths(268) == [160, 96])
	}

	@Test func aSplitsSkeletonKeepsBothSidesOrWhatIsLeftOfThem() throws {
		let text: JSON = ["c": "Text", "p": ["value": "Hello", "n": 5]]
		let shape = try #require(Skeleton.of(text))
		let split: JSON = ["c": "Split", "p": ["side": "start", "aside": "lg"], "k": [text, text]]
		#expect(Skeleton.of(split) == ["c": "Split", "p": ["side": "start", "aside": "lg"], "k": [shape, shape]])
		let one: JSON = ["c": "Split", "k": [text, ["c": "Menu", "p": [:]]]]
		#expect(Skeleton.of(one) == ["c": "Stack", "p": ["gap": "md"], "k": [shape]])
	}

	@Test func aMeasuredStackKeepsTheReadingWidthAndARoomyBoxIsAPhonesAndMore() throws {
		// The web's 37.5rem and 36rem.
		#expect(Adaptive.reading == 600)
		#expect(Adaptive.isRoomy(576))
		#expect(!Adaptive.isRoomy(575))
		let text: JSON = ["c": "Text", "p": ["value": "Hello", "n": 5]]
		let shape = try #require(Skeleton.of(text))
		#expect(Skeleton.of(["c": "Stack", "p": ["gap": "md", "measure": "content"], "k": [text]]) == ["c": "Stack", "p": ["gap": "md", "measure": "content"], "k": [shape]])
	}

	@Test func aGridsSkeletonKeepsItsMinAndColumns() {
		let grid: JSON = ["c": "List", "p": ["style": "grid", "min": "sm", "columns": 2], "items": [["k": [["c": "Text", "p": ["n": 4]]]]]]
		#expect(Skeleton.of(grid)?["p"] == ["style": "grid", "min": "sm", "columns": 2])
		let rows: JSON = ["c": "List", "p": ["style": "rows", "min": "sm"], "items": [["k": [["c": "Text", "p": ["n": 4]]]]]]
		#expect(Skeleton.of(rows)?["p"] == ["style": "rows"])
	}
}