From 55ad5038565d3b4852333b9ce272ef847f2f4600 Mon Sep 17 00:00:00 2001 From: PJ Date: Thu, 23 Apr 2026 16:35:39 +0700 Subject: [PATCH] test(ios): add unit tests for pickSimulator and iOS flag parsing Tests for all pickSimulator branches (by name, by UDID, unknown, empty list, iPhone preference, fallback to first). Also tests BootedUDID on a canceled context and verifies --platform ios and --ios-device flags are accepted by parseTestArgs. --- cmd/sanderling/main_test.go | 29 ++++++++++++ internal/ios/ios_test.go | 89 +++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 internal/ios/ios_test.go diff --git a/cmd/sanderling/main_test.go b/cmd/sanderling/main_test.go index ada500c..470f180 100644 --- a/cmd/sanderling/main_test.go +++ b/cmd/sanderling/main_test.go @@ -146,6 +146,35 @@ func TestRun_Doctor(t *testing.T) { } } +func TestParseTestArgs_AcceptsIosPlatform(t *testing.T) { + options, err := parseTestArgs([]string{ + "--spec", "s.ts", + "--bundle-id", "com.example.app", + "--platform", "ios", + }, io.Discard) + if err != nil { + t.Fatalf("unexpected error for ios platform: %v", err) + } + if options.platform != "ios" { + t.Errorf("expected platform=ios, got %q", options.platform) + } +} + +func TestParseTestArgs_IosDeviceFlag(t *testing.T) { + options, err := parseTestArgs([]string{ + "--spec", "s.ts", + "--bundle-id", "com.example.app", + "--platform", "ios", + "--ios-device", "iPhone 15 Pro", + }, io.Discard) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if options.iosDevice != "iPhone 15 Pro" { + t.Errorf("expected iosDevice=iPhone 15 Pro, got %q", options.iosDevice) + } +} + func TestRun_TestSubcommand_PipelineErrors(t *testing.T) { // Without a real spec, a real device, or a bootable AVD the pipeline // must surface a specific error rather than panicking — proves the flag diff --git a/internal/ios/ios_test.go b/internal/ios/ios_test.go new file mode 100644 index 0000000..d957fe6 --- /dev/null +++ b/internal/ios/ios_test.go @@ -0,0 +1,89 @@ +package ios + +import ( + "context" + "testing" +) + +func TestPickSimulator_ByName(t *testing.T) { + available := []simDevice{ + {UDID: "aaa", Name: "iPad Pro", IsAvailable: true}, + {UDID: "bbb", Name: "iPhone 15", IsAvailable: true}, + } + got, err := pickSimulator("iPhone 15", available) + if err != nil { + t.Fatal(err) + } + if got.UDID != "bbb" { + t.Errorf("got %q, want bbb", got.UDID) + } +} + +func TestPickSimulator_ByUDID(t *testing.T) { + available := []simDevice{ + {UDID: "aaa", Name: "iPad Pro", IsAvailable: true}, + {UDID: "bbb", Name: "iPhone 14", IsAvailable: true}, + } + got, err := pickSimulator("aaa", available) + if err != nil { + t.Fatal(err) + } + if got.Name != "iPad Pro" { + t.Errorf("got %q, want iPad Pro", got.Name) + } +} + +func TestPickSimulator_UnknownName(t *testing.T) { + available := []simDevice{ + {UDID: "aaa", Name: "iPad Pro", IsAvailable: true}, + } + _, err := pickSimulator("Pixel 7", available) + if err == nil { + t.Fatal("expected error for unknown simulator name") + } +} + +func TestPickSimulator_EmptyName_PrefersIPhone(t *testing.T) { + available := []simDevice{ + {UDID: "aaa", Name: "iPad mini", IsAvailable: true}, + {UDID: "bbb", Name: "iPhone 16", IsAvailable: true}, + {UDID: "ccc", Name: "Apple Watch", IsAvailable: true}, + } + got, err := pickSimulator("", available) + if err != nil { + t.Fatal(err) + } + if got.UDID != "bbb" { + t.Errorf("got %q, want bbb (iPhone)", got.UDID) + } +} + +func TestPickSimulator_EmptyName_FallsBackToFirst(t *testing.T) { + available := []simDevice{ + {UDID: "aaa", Name: "iPad Air", IsAvailable: true}, + {UDID: "bbb", Name: "Apple TV", IsAvailable: true}, + } + got, err := pickSimulator("", available) + if err != nil { + t.Fatal(err) + } + if got.UDID != "aaa" { + t.Errorf("got %q, want aaa (first available)", got.UDID) + } +} + +func TestPickSimulator_EmptyList(t *testing.T) { + _, err := pickSimulator("", nil) + if err == nil { + t.Fatal("expected error for empty simulator list") + } +} + +func TestBootedUDID_CanceledContext(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + cancel() + udid := BootedUDID(ctx) + if udid != "" { + t.Errorf("expected empty UDID on canceled context, got %q", udid) + } +}