ci(native): capture the watch screens one watch at a time, retried, and complete

Each launch is terminated first, retried after 2, 5 and 10 seconds when
the simulator refuses it, and captured once the app reports the screen
drawn instead of after a fixed wait. The job fails when any expected
image is missing.
This commit is contained in:
Maurice
2026-11-07 13:36:37 +01:00
parent d5ba4439f2
commit 92f10531d0
4 changed files with 68 additions and 22 deletions
+1 -1
View File
@@ -195,7 +195,7 @@ jobs:
# language, and the complications, as PNGs to look at without a watch.
watch-screens:
runs-on: macos-15
timeout-minutes: 60
timeout-minutes: 90
steps:
- uses: actions/checkout@v6
+13
View File
@@ -9,6 +9,8 @@ import Foundation
/// trip, days, packing, todos, lists, countdown, empty,
/// nosync, faces
/// -TrekScenario <name> running, travelday or upcoming (default by screen)
/// -TrekReady <token> once the screen has drawn, writes Documents/ready-<token>
/// in the app's container, which the script waits for
/// -TrekNow <HH:mm> the time the screens think it is; without it the
/// real time, which the capture script moves to the
/// hour it wants with the app's time zone (TZ), so the
@@ -23,6 +25,17 @@ enum DemoMode {
value(after: "-TrekDemo").flatMap(Screen.init(rawValue:))
}
/// Tells the capture script the screen is up: a moment after it appeared,
/// so the navigation to it and the first layout have settled.
static func signalReady() {
guard let token = value(after: "-TrekReady"),
let documents = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
DispatchQueue.main.asyncAfter(deadline: .now() + 1.2) {
try? FileManager.default.createDirectory(at: documents, withIntermediateDirectories: true)
try? Data().write(to: documents.appendingPathComponent("ready-\(token)"))
}
}
@MainActor
static func start() {
guard let screen else { return }
+4 -1
View File
@@ -64,7 +64,10 @@ struct RootView: View {
}
.environment(\.trekNow, now)
}
.onAppear(perform: openDemoScreen)
.onAppear {
openDemoScreen()
DemoMode.signalReady()
}
.onChange(of: scenePhase) { _, phase in
if phase == .active && !model.isDemo { WatchLink.shared.requestRefresh() }
}
+50 -20
View File
@@ -49,15 +49,37 @@ zone_for() {
if [ "$offset" -ge 0 ]; then echo "Etc/GMT-$offset"; else echo "Etc/GMT+$(( -offset ))"; fi
}
# shot <udid> <file> <screen> <scenario> <hour> <language>
# Waits for the ready file the app writes once the screen has drawn (DemoMode.signalReady).
wait_ready() {
local udid=$1 token=$2 container
for _ in $(seq 1 40); do
container=$(xcrun simctl get_app_container "$udid" "$BUNDLE" data 2>/dev/null || true)
[ -n "$container" ] && [ -f "$container/Documents/ready-$token" ] && return 0
sleep 0.25
done
return 1
}
TOKEN=0
# shot <udid> <file> <screen> <scenario> <hour> <language>: one launch, retried
# after 2, 5 and 10 seconds when the simulator refuses it.
shot() {
local udid=$1 file=$2 screen=$3 scenario=$4 hour=$5 lang=$6
local locale=${lang//-/_}
SIMCTL_CHILD_TZ=$(zone_for "$hour") xcrun simctl launch --terminate-running-process "$udid" "$BUNDLE" \
-AppleLanguages "($lang)" -AppleLocale "$locale" -TrekDemo "$screen" -TrekScenario "$scenario" >/dev/null \
|| { echo "could not launch $file"; return 0; }
case "$screen" in place) sleep 10 ;; *) sleep 4 ;; esac
xcrun simctl io "$udid" screenshot --type=png "$file" >/dev/null 2>&1 || echo "could not capture $file"
local locale=${lang//-/_} attempt
for attempt in 0 2 5 10; do
[ "$attempt" -gt 0 ] && { echo "retrying $(basename "$file") in ${attempt}s"; sleep "$attempt"; }
TOKEN=$((TOKEN + 1))
xcrun simctl terminate "$udid" "$BUNDLE" >/dev/null 2>&1 || true
sleep 0.5
SIMCTL_CHILD_TZ=$(zone_for "$hour") xcrun simctl launch "$udid" "$BUNDLE" \
-AppleLanguages "($lang)" -AppleLocale "$locale" -TrekDemo "$screen" -TrekScenario "$scenario" \
-TrekReady "$TOKEN" >/dev/null 2>&1 || continue
wait_ready "$udid" "$TOKEN" || continue
# The map's tiles come over the network after the screen is up.
[ "$screen" = place ] && sleep 6
xcrun simctl io "$udid" screenshot --type=png "$file" >/dev/null 2>&1 && return 0
done
echo "gave up on $(basename "$file")"
}
# name screen scenario hour: every screen, and the states a screen has.
@@ -85,12 +107,13 @@ faces faces running 13
MAIN="today booking countdown trip lists packing day"
LANGS="ar az ca cs de el en es et fr he hu id it ja ko nl pl pt-BR ru sk sv th tr uk vi zh-Hans zh-Hant"
EXPECTED=()
run() {
local size=$1 name=$2 udid
udid=$(boot "$name" | tail -1)
local size=$1 udid=$2 lang shotname screen scenario hour
for lang in en de ru; do
while read -r shotname screen scenario hour; do
[ -n "$shotname" ] || continue
EXPECTED+=("$size-$lang-$shotname.png")
shot "$udid" "$OUT/$size-$lang-$shotname.png" "$screen" "$scenario" "$hour" "$lang"
done <<< "$SHOTS"
done
@@ -99,21 +122,28 @@ run() {
case "$lang" in en|de|ru) continue ;; esac
while read -r shotname screen scenario hour; do
[ -n "$shotname" ] || continue
case " $MAIN " in *" $shotname "*) shot "$udid" "$OUT/$size-$lang-$shotname.png" "$screen" "$scenario" "$hour" "$lang" ;; esac
case " $MAIN " in
*" $shotname "*)
EXPECTED+=("$size-$lang-$shotname.png")
shot "$udid" "$OUT/$size-$lang-$shotname.png" "$screen" "$scenario" "$hour" "$lang"
;;
esac
done <<< "$SHOTS"
done
fi
xcrun simctl shutdown "$udid" || true
}
run large "$LARGE" &
large=$!
run medium "$MEDIUM" &
medium=$!
run small "$SMALL" &
small=$!
wait "$large"
wait "$medium"
wait "$small"
# One watch at a time: launches on several booted simulators at once is what
# makes SpringBoard refuse them ("Scene update failed").
run large "$(boot "$LARGE" | tail -1)"
run medium "$(boot "$MEDIUM" | tail -1)"
run small "$(boot "$SMALL" | tail -1)"
printf '%s\n' "large: $LARGE" "medium: $MEDIUM" "small: $SMALL" > "$OUT/devices.txt"
ls "$OUT" | wc -l
missing=0
for file in "${EXPECTED[@]}"; do
[ -s "$OUT/$file" ] || { echo "missing: $file"; missing=$((missing + 1)); }
done
echo "${#EXPECTED[@]} expected, $missing missing"
[ "$missing" -eq 0 ]