Cotton code
134 lines · 6.8 KB · ShellRaw
#!/usr/bin/env bash# The faces walk (lane faces, 23 Oct 2026): an XCUITest that walks the# Cotton flavour on a simulator over a database it makes itself and fills# with the device tool's samples — never a person's file. It opens each# deck the faces lane draws, swipes its views and cards, holds a card and# pinches, and saves NN-tool-step.png (and the step's accessibility tree,# NN-tool-step.txt) into the out folder, with walk.log.##   scripts/faces-walk/walk.sh [<simulator udid>] [<out dir>]## FACES_WALK_STATE names a phone state `seed.sh` made (an invented household# with Money, Machines, Quotas, Subscriptions, Birthdays and Artifacts): it# is put in the fresh app's data folder before launch. Without it the walk# starts a new, empty database itself.## Out of main's checks: the project is generated in the lane's derived# folder, never in the tree, and is run only by hand. The walk fails only on# a crash or a missing control; everything else is a line in walk.log.set -euo pipefailREPO="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"# A walk's builds go ahead of other local runs waiting on the build lock# (`with-build-lock.sh`, "A WALK GOES BEFORE OTHER LOCAL RUNS").. "$REPO/scripts/lib/caller-class.sh"rank_as_walkSIM="${1:-${COTTON_SIM:-}}"OUT="${2:-$HOME/lanes/faces/walk}"[ -n "$SIM" ] || { echo "faces-walk: name the simulator (udid or COTTON_SIM)" >&2; exit 2; }. "$REPO/scripts/lib/owned-root.sh"# The app builds where every xcodebuild of this repo does (the one helper:# COTTON_DERIVED_DATA, else this branch's folder under the derived root), and# the walk's own test project in a folder inside it.# shellcheck source=../lib/derived-data.sh. "$REPO/scripts/lib/derived-data.sh"DERIVED="$(cotton_derived_data)"W="$DERIVED/faces-walk"refuse_wide_root "the walk's build folder" "$W"refuse_wide_root "the walk's out folder" "$OUT"mkdir -p "$W" "$OUT"rm -f "$OUT"/*.png "$OUT"/*.txt "$OUT"/walk.log# The app: the Cotton flavour, built as the tree builds it, installed fresh# so the walk's database is the one it starts.bash "$REPO/scripts/with-build-lock.sh" xcodebuild build -project "$REPO/apps/cotton/Cotton.xcodeproj" \  -scheme tinmen-cotton -configuration Debug -destination 'generic/platform=iOS Simulator' \  -derivedDataPath "$DERIVED" ARCHS=arm64 ONLY_ACTIVE_ARCH=YES -jobs 4 -quiet > "$W/app-build.log" 2>&1 \  || { echo "faces-walk: the app did not build (see $W/app-build.log)" >&2; exit 1; }APP="$DERIVED/Build/Products/Debug-iphonesimulator/Cotton.app"# The app links the Rust core from apps/cotton/.ffi (scripts/build-xcframework.sh),# which this build does not remake: a core older than the crates' last commit# cannot read definitions a newer cotton-headless seeded (a card face it does# not know refuses tools.list and read.home, and the fan shows no tools).FFI_LIB="$REPO/apps/cotton/.ffi/CottonFFI.xcframework/ios-arm64-simulator/libcotton_ffi.a"CRATES_AT="$(git -C "$REPO" log -1 --format=%ct -- crates/cotton-core crates/cotton-ffi crates/cotton-tools crates/cotton-client)"if [ -f "$FFI_LIB" ] && [ "$(stat -f %m "$FFI_LIB")" -lt "${CRATES_AT:-0}" ]; then  echo "faces-walk: WARNING the app's Rust core ($FFI_LIB, $(date -r "$(stat -f %m "$FFI_LIB")" '+%F %T')) is older than the crates' last commit ($(date -r "$CRATES_AT" '+%F %T')); rebuild it with scripts/build-xcframework.sh --debug --simulator-only" >&2fiBID="$(/usr/libexec/PlistBuddy -c "Print CFBundleIdentifier" "$APP/Info.plist")"xcrun simctl terminate "$SIM" "$BID" >/dev/null 2>&1 || trueif [ -z "${FACES_WALK_KEEP:-}" ]; then xcrun simctl uninstall "$SIM" "$BID" >/dev/null 2>&1 || true; fixcrun simctl install "$SIM" "$APP"if [ -n "${FACES_WALK_STATE:-}" ]; then  [ -f "$FACES_WALK_STATE/client/connection.json" ] || { echo "faces-walk: $FACES_WALK_STATE is not a seed.sh state" >&2; exit 2; }  xcrun simctl privacy "$SIM" grant all "$BID" >/dev/null 2>&1 || true  DATA="$(xcrun simctl get_app_container "$SIM" "$BID" data)"  # The app's own data folder inside a simulator's container, nothing else.  [[ "$DATA" =~ /CoreSimulator/Devices/[0-9A-F-]{36}/data/Containers/Data/Application/[0-9A-F-]{36}$ ]] \    || { echo "faces-walk: not a simulator app container: $DATA" >&2; exit 2; }  FOLDER="$DATA/Library/Application Support/Cotton"  rm -rf "$FOLDER"  mkdir -p "$FOLDER"  cp -R "$FACES_WALK_STATE/client" "$FOLDER/client"fi# The walk: a bare host app and the UI-test bundle that drives Cotton by its# bundle id.cp "$REPO/scripts/faces-walk/FacesWalkUITests.swift" "$W/FacesWalkUITests.swift"cat > "$W/Host.swift" <<'SWIFT'import SwiftUI@main struct FacesWalkHost: App { var body: some Scene { WindowGroup { Text(verbatim: "faces walk") } } }SWIFTcat > "$W/project.yml" <<'YAML'name: FacesWalkoptions:  bundleIdPrefix: com.cottondbtargets:  FacesWalkHost:    type: application    platform: iOS    deploymentTarget: '26.0'    sources: [Host.swift]    settings:      base:        PRODUCT_BUNDLE_IDENTIFIER: com.cottondb.faceswalkhost        GENERATE_INFOPLIST_FILE: YES        INFOPLIST_KEY_UILaunchScreen_Generation: YES        INFOPLIST_KEY_UIApplicationSceneManifest_Generation: YES        SWIFT_VERSION: '6.0'        CODE_SIGN_IDENTITY: '-'  FacesWalk:    type: bundle.ui-testing    platform: iOS    deploymentTarget: '26.0'    sources: [FacesWalkUITests.swift]    dependencies:      - target: FacesWalkHost    settings:      base:        GENERATE_INFOPLIST_FILE: YES        PRODUCT_BUNDLE_IDENTIFIER: com.cottondb.faceswalk        TEST_TARGET_NAME: FacesWalkHost        SWIFT_VERSION: '6.0'        CODE_SIGN_IDENTITY: '-'schemes:  FacesWalk:    build:      targets:        FacesWalkHost: all        FacesWalk: [test]    test:      targets: [FacesWalk]YAML(cd "$W" && xcodegen generate >/dev/null)bash "$REPO/scripts/with-build-lock.sh" xcodebuild build-for-testing -project "$W/FacesWalk.xcodeproj" -scheme FacesWalk \  -destination "platform=iOS Simulator,id=$SIM" -derivedDataPath "$W/derived" -quiet > "$W/walk-build.log" 2>&1 \  || { echo "faces-walk: the walk did not build (see $W/walk-build.log)" >&2; exit 1; }XCTESTRUN="$(ls "$W"/derived/Build/Products/FacesWalk_*.xctestrun | head -n 1)"STATUS=0TEST_RUNNER_FACES_WALK_OUT="$OUT" TEST_RUNNER_FACES_WALK_APP="$BID" TEST_RUNNER_FACES_WALK_TOOLS="${FACES_WALK_TOOLS:-}" \  xcodebuild test-without-building -xctestrun "$XCTESTRUN" -destination "platform=iOS Simulator,id=$SIM" \  -only-testing:"FacesWalk/FacesWalk/test${FACES_WALK_TEST:-Walk}" > "$W/walk-run.log" 2>&1 || STATUS=$?grep -E "MISSING|CRASH|error:" "$OUT/walk.log" "$W/walk-run.log" 2>/dev/null | grep -v "exit code 0" | head -20 || true[ "$STATUS" = 0 ] || { echo "faces-walk: the walk failed (see $W/walk-run.log and $OUT/walk.log)" >&2; exit "$STATUS"; }echo "faces-walk: $(ls "$OUT"/*.png 2>/dev/null | wc -l | tr -d ' ') frames in $OUT"