Files

206 lines
7.7 KiB
Go

package commandcenter
import (
"context"
"fmt"
"testing"
"time"
"github.com/glebarez/sqlite"
"github.com/yibaiba/hideck/internal/db"
"github.com/yibaiba/hideck/internal/notify"
"gorm.io/gorm"
)
type commandReplyCapture struct{}
func (commandReplyCapture) Reply(string) {}
func (commandReplyCapture) Confirm(string) bool { return true }
type channelReplyCapture struct {
replies []string
attachments [][]notify.CommandAttachment
}
func (c *channelReplyCapture) Reply(text string) {
c.replies = append(c.replies, text)
}
func (c *channelReplyCapture) Confirm(string) bool { return true }
func (c *channelReplyCapture) ReplyWithAttachments(text string, attachments []notify.CommandAttachment) {
c.replies = append(c.replies, text)
c.attachments = append(c.attachments, append([]notify.CommandAttachment(nil), attachments...))
}
func TestServicePersistsAsyncCommandTimeline(t *testing.T) {
service := newTestService(t, map[string]notify.CommandHandler{
"send": func(ctx notify.CommandContext, _ []string) string {
ctx.Reply("发送短信 / 完成")
return "发送短信 / 已受理"
},
})
execution, err := service.Execute(context.Background(), ExecuteRequest{Input: "/send wwan0 10086 test"})
if err != nil {
t.Fatalf("Execute() error = %v", err)
}
events := waitForEvents(t, service, 0, 3)
if events[0].Kind != EventAccepted || events[1].Kind != EventProgress || events[2].Kind != EventResult {
t.Fatalf("event kinds = %q, %q, %q", events[0].Kind, events[1].Kind, events[2].Kind)
}
if events[2].Execution == nil || events[2].Execution.ID != execution.ID || events[2].Execution.State != StateCompleted {
t.Fatalf("completed execution = %+v", events[2].Execution)
}
}
func TestServicePersistsAsyncCommandAudioAttachmentReturnedBeforeActivation(t *testing.T) {
service := newTestService(t, map[string]notify.CommandHandler{
"vocall": func(ctx notify.CommandContext, _ []string) string {
progress, ok := ctx.(interface{ Progress(string) })
if !ok {
t.Fatal("command context does not support progress")
}
progress.Progress("发起 VoWiFi 呼叫 / 已接通")
rich, ok := ctx.(interface {
ReplyWithAttachments(string, []notify.CommandAttachment)
})
if !ok {
t.Fatal("command context does not support attachments")
}
rich.ReplyWithAttachments("发起 VoWiFi 呼叫 / 完成", []notify.CommandAttachment{{
Type: "audio", Recording: "call_wwan1_20260813_100108.890350649.mp3", ContentType: "audio/mpeg",
}})
return "发起 VoWiFi 呼叫 / 已受理"
},
})
if _, err := service.Execute(context.Background(), ExecuteRequest{Input: "/vocall wwan1 888 10"}); err != nil {
t.Fatal(err)
}
events := waitForEvents(t, service, 0, 4)
if events[1].Text != "发起 VoWiFi 呼叫 / 已受理" || events[2].Text != "发起 VoWiFi 呼叫 / 已接通" {
t.Fatalf("progress order = %+v", events)
}
result := events[3]
if result.Kind != EventResult || len(result.Attachments) != 1 {
t.Fatalf("result event = %+v", result)
}
if result.Attachments[0].Recording != "call_wwan1_20260813_100108.890350649.mp3" {
t.Fatalf("attachment = %+v", result.Attachments[0])
}
reloaded, err := service.ListEvents(context.Background(), 0, 20)
if err != nil || len(reloaded) != 4 || len(reloaded[3].Attachments) != 1 {
t.Fatalf("reloaded events = %+v, err=%v", reloaded, err)
}
}
func TestServiceMirrorsChannelCommandIntoTimelineAndOriginalReply(t *testing.T) {
service := newTestService(t, map[string]notify.CommandHandler{
"vocall": func(ctx notify.CommandContext, _ []string) string {
ctx.(interface{ Progress(string) }).Progress("发起 VoWiFi 呼叫 / 已接通")
ctx.(interface {
ReplyWithAttachments(string, []notify.CommandAttachment)
}).ReplyWithAttachments("发起 VoWiFi 呼叫 / 完成", []notify.CommandAttachment{{
Type: "audio", Recording: "call_test.mp3", ContentType: "audio/mpeg", Path: "/tmp/call_test.mp3",
}})
return "发起 VoWiFi 呼叫 / 已受理"
},
})
reply := &channelReplyCapture{}
handler := service.commands.Handlers()["vocall"]
result := service.ExecuteChannelCommand(reply, notify.ChannelCommandRequest{
Channel: "qq", Name: "vocall", Arguments: []string{"wwan1", "10086", "10"},
}, handler)
if result != "发起 VoWiFi 呼叫 / 已受理" {
t.Fatalf("result = %q", result)
}
events := waitForEvents(t, service, 0, 4)
if events[0].Execution == nil || events[0].Execution.Source != "qq" ||
events[0].Execution.Input != "/vocall wwan1 10086 10" {
t.Fatalf("channel execution = %+v", events[0].Execution)
}
if events[2].Kind != EventProgress || events[3].Kind != EventResult || len(events[3].Attachments) != 1 {
t.Fatalf("channel events = %+v", events)
}
if len(reply.replies) != 2 || reply.replies[0] != "发起 VoWiFi 呼叫 / 已接通" ||
len(reply.attachments) != 1 || reply.attachments[0][0].Path != "/tmp/call_test.mp3" {
t.Fatalf("channel replies = %+v, attachments = %+v", reply.replies, reply.attachments)
}
}
func TestServiceCursorAndClearPreserveRunning(t *testing.T) {
service := newTestService(t, map[string]notify.CommandHandler{
"list": func(_ notify.CommandContext, _ []string) string { return "设备列表 / 完成" },
"send": func(_ notify.CommandContext, _ []string) string { return "发送短信 / 已受理" },
})
if _, err := service.Execute(context.Background(), ExecuteRequest{Input: "/list"}); err != nil {
t.Fatal(err)
}
if _, err := service.Execute(context.Background(), ExecuteRequest{Input: "/send wwan0 10086 test"}); err != nil {
t.Fatal(err)
}
events := waitForEvents(t, service, 0, 4)
after := events[1].ID
cursorEvents, err := service.ListEvents(context.Background(), after, 20)
if err != nil || len(cursorEvents) != len(events)-2 {
t.Fatalf("ListEvents(after) = %d, %v", len(cursorEvents), err)
}
deleted, err := service.ClearCompleted(context.Background())
if err != nil || deleted != 1 {
t.Fatalf("ClearCompleted() = %d, %v", deleted, err)
}
remaining, err := service.ListEvents(context.Background(), 0, 20)
if err != nil || len(remaining) != 2 || remaining[0].Execution.State != StateRunning {
t.Fatalf("remaining events = %+v, %v", remaining, err)
}
}
func TestServiceListsLatestEventsBeforeCursor(t *testing.T) {
service := newTestService(t, map[string]notify.CommandHandler{
"list": func(_ notify.CommandContext, _ []string) string { return "设备列表 / 完成" },
})
for range 3 {
if _, err := service.Execute(context.Background(), ExecuteRequest{Input: "/list"}); err != nil {
t.Fatal(err)
}
}
all := waitForEvents(t, service, 0, 6)
latest, err := service.ListEventsBefore(context.Background(), 0, 2)
if err != nil || latest[0].ID != all[4].ID || latest[1].ID != all[5].ID {
t.Fatalf("latest events = %+v, %v", latest, err)
}
earlier, err := service.ListEventsBefore(context.Background(), latest[0].ID, 2)
if err != nil || earlier[1].ID != all[3].ID {
t.Fatalf("earlier events = %+v, %v", earlier, err)
}
}
func newTestService(t *testing.T, handlers map[string]notify.CommandHandler) *Service {
t.Helper()
database, err := gorm.Open(sqlite.Open(fmt.Sprintf("file:%s?mode=memory&cache=shared", t.Name())), &gorm.Config{})
if err != nil {
t.Fatal(err)
}
if err := database.AutoMigrate(&db.CommandExecution{}, &db.CommandEvent{}); err != nil {
t.Fatal(err)
}
return NewService(notify.NewCommandService(handlers), NewDatabaseStore(database))
}
func waitForEvents(t *testing.T, service *Service, after uint64, count int) []Event {
t.Helper()
deadline := time.Now().Add(time.Second)
for time.Now().Before(deadline) {
events, err := service.ListEvents(context.Background(), after, 20)
if err != nil {
t.Fatal(err)
}
if len(events) >= count {
return events
}
time.Sleep(10 * time.Millisecond)
}
t.Fatalf("timed out waiting for %d events", count)
return nil
}