Skip to content

Commit 6029780

Browse files
arditticlaude
authored andcommitted
fix(install): let /api/install target iPhone and iPad without device_id
When device_id was omitted, /api/install only considered Apple TV devices, so an iPhone or iPad could never be the default target and a server with only an iPhone answered "no Apple TV devices found". Pick the target the same way the MCP install_app tool does: the device with device_id, or the only available device when it is omitted. With several devices and no device_id the request is refused with the list of device IDs instead of guessing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WiMX5YzKfrhta4tnForxux
1 parent b50e45e commit 6029780

2 files changed

Lines changed: 79 additions & 30 deletions

File tree

‎web/install_device_test.go‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package web
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/bitxeno/atvloadly/internal/model"
8+
)
9+
10+
func TestSelectInstallDevice(t *testing.T) {
11+
atv := model.Device{ID: "atv", Name: "Living Room", DeviceClass: string(model.DeviceClassAppleTV)}
12+
iphone := model.Device{ID: "phone", Name: "My iPhone", DeviceClass: string(model.DeviceClassiPhone)}
13+
ipad := model.Device{ID: "pad", Name: "My iPad", DeviceClass: string(model.DeviceClassiPad)}
14+
15+
tests := []struct {
16+
name string
17+
devices []model.Device
18+
deviceID string
19+
wantID string
20+
wantErr string
21+
}{
22+
{name: "only Apple TV", devices: []model.Device{atv}, wantID: "atv"},
23+
{name: "only iPhone", devices: []model.Device{iphone}, wantID: "phone"},
24+
{name: "only iPad", devices: []model.Device{ipad}, wantID: "pad"},
25+
{name: "explicit iPhone among several", devices: []model.Device{atv, iphone, ipad}, deviceID: "phone", wantID: "phone"},
26+
{name: "explicit Apple TV among several", devices: []model.Device{iphone, atv}, deviceID: "atv", wantID: "atv"},
27+
{name: "several without device_id", devices: []model.Device{iphone, atv}, wantErr: "device_id is required: atv (Living Room, AppleTV), phone (My iPhone, iPhone)"},
28+
{name: "unknown device_id", devices: []model.Device{atv}, deviceID: "missing", wantErr: "device_id not found: missing"},
29+
{name: "no devices", wantErr: "no available devices found"},
30+
}
31+
for _, tt := range tests {
32+
t.Run(tt.name, func(t *testing.T) {
33+
got, err := selectInstallDevice(tt.devices, tt.deviceID)
34+
if tt.wantErr != "" {
35+
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
36+
t.Fatalf("err = %v, want it to contain %q", err, tt.wantErr)
37+
}
38+
return
39+
}
40+
if err != nil {
41+
t.Fatal(err)
42+
}
43+
if got.ID != tt.wantID {
44+
t.Errorf("device = %q, want %q", got.ID, tt.wantID)
45+
}
46+
})
47+
}
48+
}

‎web/router.go‎

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"path"
1111
"path/filepath"
12+
"sort"
1213
"strconv"
1314
"strings"
1415
"time"
@@ -599,37 +600,10 @@ func route(fi *fiber.App) {
599600
manager.CleanUploadTempFiles(ipaPath)
600601
return c.Status(http.StatusOK).JSON(apiError(err.Error()))
601602
}
602-
if len(devices) == 0 {
603+
selectedDevice, err := selectInstallDevice(devices, deviceID)
604+
if err != nil {
603605
manager.CleanUploadTempFiles(ipaPath)
604-
return c.Status(http.StatusOK).JSON(apiError("no available devices found"))
605-
}
606-
607-
var selectedDevice model.Device
608-
if deviceID != "" {
609-
found := false
610-
for _, d := range devices {
611-
if d.ID == deviceID {
612-
selectedDevice = d
613-
found = true
614-
break
615-
}
616-
}
617-
if !found {
618-
manager.CleanUploadTempFiles(ipaPath)
619-
return c.Status(http.StatusOK).JSON(apiError("device_id not found: " + deviceID))
620-
}
621-
} else {
622-
atvDevices := []model.Device{}
623-
for _, d := range devices {
624-
if d.DeviceClass == string(model.DeviceClassAppleTV) {
625-
atvDevices = append(atvDevices, d)
626-
}
627-
}
628-
if len(atvDevices) == 0 {
629-
manager.CleanUploadTempFiles(ipaPath)
630-
return c.Status(http.StatusOK).JSON(apiError("no Apple TV devices found"))
631-
}
632-
selectedDevice = atvDevices[0]
606+
return c.Status(http.StatusOK).JSON(apiError(err.Error()))
633607
}
634608

635609
appModel := model.InstalledApp{
@@ -984,6 +958,33 @@ func route(fi *fiber.App) {
984958

985959
}
986960

961+
// selectInstallDevice picks the install target: the device with deviceID, or
962+
// the only available device when deviceID is empty. Several devices without
963+
// a deviceID is refused rather than guessed.
964+
func selectInstallDevice(devices []model.Device, deviceID string) (model.Device, error) {
965+
if len(devices) == 0 {
966+
return model.Device{}, fmt.Errorf("no available devices found")
967+
}
968+
if deviceID != "" {
969+
for _, d := range devices {
970+
if d.ID == deviceID {
971+
return d, nil
972+
}
973+
}
974+
return model.Device{}, fmt.Errorf("device_id not found: %s", deviceID)
975+
}
976+
if len(devices) == 1 {
977+
return devices[0], nil
978+
}
979+
980+
choices := make([]string, 0, len(devices))
981+
for _, d := range devices {
982+
choices = append(choices, fmt.Sprintf("%s (%s, %s)", d.ID, d.Name, d.DeviceClass))
983+
}
984+
sort.Strings(choices)
985+
return model.Device{}, fmt.Errorf("several devices are available, device_id is required: %s", strings.Join(choices, ", "))
986+
}
987+
987988
// removeUploadArtifacts removes the temp files of already parsed uploads in a
988989
// failed multi-file request. The client never receives those paths, so only
989990
// the server can clean them.

0 commit comments

Comments
 (0)
⌂ 换网址