x
", 500) if err := os.WriteFile(fp, []byte(big), 0o644); err != nil { t.Fatal(err) } dev, got := newCapabilityDevice(t, "cap-path", func(string) map[string]interface{} { return map[string]interface{}{"status": "ok", "output": "ok"} }) if _, err := dev.Execute("screensue", map[string]interface{}{ "device_id": "cap-path", "content_path": fp, }); err != nil { t.Fatalf("content_path 应成功: %v", err) } // 内容来自文件,逐字节一致 if !strings.Contains((*got)[0], big) { t.Errorf("命令应含文件全文(%d 字节),实际长度 %d", len(big), len((*got)[0])) } } func TestDeviceContentArg_RejectsBothOrNeither(t *testing.T) { // 两个都给 ⇒ 报错(歧义比默认取一个更危险) if _, err := deviceContentArg("x", "/tmp/y", "content"); err == nil { t.Error("content 与 content_path 同时给必须报错") } // 都不给 ⇒ 报错,且说明该怎么给 _, err := deviceContentArg("", "", "content") if err == nil { t.Fatal("都不给必须报错") } if !strings.Contains(err.Error(), "content_path") { t.Errorf("错误应提示可用 content_path,实际: %v", err) } // 文件不存在 ⇒ 带路径 if _, err := deviceContentArg("", "/nope/x.html", "content"); err == nil || !strings.Contains(err.Error(), "/nope/x.html") { t.Errorf("文件读取失败应带路径,实际: %v", err) } // 空文件 ⇒ 与「不存在」区分 dir := t.TempDir() empty := filepath.Join(dir, "e.html") os.WriteFile(empty, nil, 0o644) if _, err := deviceContentArg("", empty, "content"); err == nil || !strings.Contains(err.Error(), "空") { t.Errorf("空文件应明说,实际: %v", err) } // 超限 ⇒ 说明大小与上限,并提示改用文件路径 big := filepath.Join(dir, "big.html") os.WriteFile(big, []byte(strings.Repeat("x", deviceContentMaxBytes+10)), 0o644) _, err = deviceContentArg("", big, "content") if err == nil || !strings.Contains(err.Error(), "过大") { t.Errorf("超限应报错并说明,实际: %v", err) } } // ---- ③ camerasue:拍照/录像由 duration_seconds 区分 ---- func TestCamerasue_PhotoVsVideo(t *testing.T) { dev, got := newCapabilityDevice(t, "cap-cam", func(cmd string) map[string]interface{} { if cmd == "camerasue" { return map[string]interface{}{"status": "ok", "output": "photo ok", "mime": "image/jpeg", "size": 123} } return map[string]interface{}{"status": "ok", "output": "video ok", "mime": "video/mp4", "size": 456} }) // 拍照:不带时长 res, err := dev.Execute("camerasue", map[string]interface{}{"device_id": "cap-cam"}) if err != nil { t.Fatalf("拍照应成功: %v", err) } if (*got)[0] != "camerasue" { t.Errorf("拍照命令应为裸 camerasue,实际 %q", (*got)[0]) } if m, _ := res.(map[string]interface{}); m["mode"] != "photo" { t.Errorf("mode 应为 photo,实际 %v", m["mode"]) } // ★ mime/size 必须透传:录像不能只回一句「成功」。 // size 经 JSON 回来是 float64(不是 int)—— 这是边界事实, // 断言要按 JSON 的实际类型比,否则判据自己会假红。 if m, _ := res.(map[string]interface{}); m["mime"] != "image/jpeg" || toIntArg(m["size"]) != 123 { t.Errorf("mime/size 应透传,实际 mime=%v size=%v", m["mime"], m["size"]) } // 录像:带时长 res, err = dev.Execute("camerasue", map[string]interface{}{ "device_id": "cap-cam", "duration_seconds": 5, }) if err != nil { t.Fatalf("录像应成功: %v", err) } if (*got)[1] != "camerasue 5" { t.Errorf("录像命令应为 camerasue 5,实际 %q", (*got)[1]) } if m, _ := res.(map[string]interface{}); m["mode"] != "video" { t.Errorf("mode 应为 video,实际 %v", m["mode"]) } } func TestCamerasue_RejectsNegativeAndOverlong(t *testing.T) { dev, got := newCapabilityDevice(t, "cap-cam2", func(string) map[string]interface{} { return map[string]interface{}{"status": "ok", "output": "ok"} }) if _, err := dev.Execute("camerasue", map[string]interface{}{ "device_id": "cap-cam2", "duration_seconds": -1, }); err == nil { t.Error("负时长必须报错") } if _, err := dev.Execute("camerasue", map[string]interface{}{ "device_id": "cap-cam2", "duration_seconds": 9999, }); err == nil || !strings.Contains(err.Error(), "300") { t.Errorf("超长录像应报错并说明上限,实际: %v", err) } if len(*got) != 0 { t.Errorf("被拒的请求不应下发命令,实际下发 %v", *got) } } // ---- ④ speakeruse ---- func TestSpeakeruse_StructuredArg(t *testing.T) { dev, got := newCapabilityDevice(t, "cap-spk", func(string) map[string]interface{} { return map[string]interface{}{"status": "ok", "output": "spoken"} }) if _, err := dev.Execute("speakeruse", map[string]interface{}{ "device_id": "cap-spk", "text": "三点开会,请准时", }); err != nil { t.Fatalf("speakeruse 应成功: %v", err) } if (*got)[0] != "speakeruse 三点开会,请准时" { t.Errorf("命令形状错:%q", (*got)[0]) } // text_path 变体 dir := t.TempDir() fp := filepath.Join(dir, "say.txt") os.WriteFile(fp, []byte("来自文件的问候"), 0o644) if _, err := dev.Execute("speakeruse", map[string]interface{}{ "device_id": "cap-spk", "text_path": fp, }); err != nil { t.Fatalf("text_path 应成功: %v", err) } if (*got)[1] != "speakeruse 来自文件的问候" { t.Errorf("text_path 内容未送达:%q", (*got)[1]) } } // ---- ⑤ 数字参数的字符串形态(模型常给 "30" 而不是 30)---- func TestToIntArg_AcceptsStringNumbers(t *testing.T) { // 类型断言静默失败退回默认值,正是「参数丢了但不报错」—— // 模型给字符串 "30" 是常见形态,必须认。 for _, v := range []interface{}{30, int64(30), float64(30), "30", " 30 "} { if got := toIntArg(v); got != 30 { t.Errorf("toIntArg(%#v)=%d,期望 30", v, got) } } if got := toIntArg("abc"); got != 0 { t.Errorf("非数字应返回 0,实际 %d", got) } } // ---- ⑥ 错误必须可操作:不在线 / 缺能力 ---- func TestCapabilityTools_OfflineErrorSaysRetry(t *testing.T) { reg := NewRegistry() dev := &devicectlDevice{reg: reg} // 设备根本不存在 _, err := dev.Execute("screensue", map[string]interface{}{ "device_id": "ghost", "content": "x", }) if err == nil || !strings.Contains(err.Error(), "devicedetect") { t.Errorf("设备不存在应指向 devicedetect,实际: %v", err) } } func TestScreensee_TimeoutErrorDistinguishesNotYetReplied(t *testing.T) { // 设备在线但永不回执 ⇒ 错误必须说清「命令已下发但没回」, // 而不是让 agent 以为设备拒绝执行(实测它会换参数重试)。 dev, _ := newCapabilityDevice(t, "cap-slow", func(string) map[string]interface{} { time.Sleep(200 * time.Millisecond) return map[string]interface{}{"status": "ok", "output": "late"} }) // 用一个必然超时的短超时直接测 pushHomeagentCapability _, reqID, err := dev.pushHomeagentCapability("cap-slow", "speakeruse hi", 50*time.Millisecond) if err == nil { t.Fatal("超时必须报错") } msg := err.Error() if !strings.Contains(msg, "回执") || !strings.Contains(msg, reqID) { t.Errorf("超时错误应说明「未回执」并带上 req_id=%s 便于复查,实际: %v", reqID, err) } if !strings.Contains(msg, "device_ctl_cmdresult") { t.Errorf("超时错误应指向复查工具,实际: %v", err) } }