package settings import ( "context" "strings" "testing" "time" ) type memoryRepo struct { admins map[string]bool prefs map[string]string bindings map[string]ResourceBinding candidate BootstrapCandidate sessions map[string]SettingSession } func newMemoryRepo() *memoryRepo { return &memoryRepo{admins: map[string]bool{}, prefs: map[string]string{}, bindings: map[string]ResourceBinding{}, sessions: map[string]SettingSession{}} } func (r *memoryRepo) IsAdministrator(_ context.Context, tenant, staff string) (bool, error) { return r.admins[tenant+"/"+staff], nil } func (r *memoryRepo) SetPreference(_ context.Context, tenant, channel, staff, locale string) error { r.prefs[tenant+"/"+staff] = locale return nil } func (r *memoryRepo) SaveBinding(_ context.Context, b ResourceBinding) error { r.bindings[b.Kind] = b return nil } func (r *memoryRepo) DetectBootstrapCandidate(_ context.Context, c BootstrapCandidate) error { r.candidate = c return nil } func (r *memoryRepo) GetSettingSession(_ context.Context, tenant, staff string) (SettingSession, bool, error) { s, ok := r.sessions[tenant+"/"+staff] return s, ok, nil } func (r *memoryRepo) SaveSettingSession(_ context.Context, s SettingSession) error { r.sessions[s.TenantKey+"/"+s.StaffID] = s return nil } func (r *memoryRepo) DeleteSettingSession(_ context.Context, tenant, staff string) error { delete(r.sessions, tenant+"/"+staff) return nil } func TestControllerLanguageForOrdinaryUser(t *testing.T) { r := newMemoryRepo() c := NewController(r) out, err := c.Handle(context.Background(), SettingInput{TenantKey: "t", Channel: "dingtalk", StaffID: "u", Text: "/setting language ja", ConversationType: "direct"}) if err != nil { t.Fatal(err) } if r.prefs["t/u"] != "ja" || out == "" { t.Fatalf("prefs=%v out=%q", r.prefs, out) } } func TestControllerRejectsOrdinaryUserSystemBinding(t *testing.T) { r := newMemoryRepo() c := NewController(r) out, err := c.Handle(context.Background(), SettingInput{TenantKey: "t", StaffID: "u", Text: "/setting support", ConversationType: "group", ConversationID: "g"}) if err != nil { t.Fatal(err) } if len(r.bindings) != 0 || out != "你没有系统管理权限。" { t.Fatalf("bindings=%v out=%q", r.bindings, out) } } func TestControllerBindsAdminSupportGroup(t *testing.T) { r := newMemoryRepo() r.admins["t/a"] = true c := NewController(r) _, err := c.Handle(context.Background(), SettingInput{TenantKey: "t", StaffID: "a", Text: "/setting support", ConversationType: "group", ConversationID: "g", ConversationTitle: "技术支持"}) if err != nil { t.Fatal(err) } if r.bindings["support_group"].ConversationID != "g" { t.Fatalf("binding=%+v", r.bindings) } } func TestControllerHelpIsHandledLocally(t *testing.T) { r := newMemoryRepo() c := NewController(r) if !c.ShouldHandle(context.Background(), "t", "u", "/help") { t.Fatal("help must be intercepted before QA") } out, err := c.Handle(context.Background(), SettingInput{TenantKey: "t", StaffID: "u", Text: "/help", ConversationType: "direct"}) if err != nil { t.Fatal(err) } want := "设置回答语言:/setting language \n例如:/setting language en" if out != want { t.Fatalf("help=%q want %q", out, want) } } func TestControllerParsesAdminQuestionTable(t *testing.T) { r := newMemoryRepo() r.admins["t/a"] = true c := NewController(r) _, err := c.Handle(context.Background(), SettingInput{TenantKey: "t", StaffID: "a", Text: "/setting question https://alidocs.dingtalk.com/i/nodes/base?sheetId=sheet&viewId=view", ConversationType: "direct"}) if err != nil { t.Fatal(err) } if r.bindings["question_table"].BaseID != "base" { t.Fatalf("binding=%+v", r.bindings) } } func TestControllerAcceptsTableLinkInNextMessage(t *testing.T) { r := newMemoryRepo() r.admins["t/a"] = true c := NewController(r) out, err := c.Handle(context.Background(), SettingInput{TenantKey: "t", Channel: "dingtalk", StaffID: "a", Text: "/setting question", ConversationType: "direct"}) if err != nil || !strings.Contains(out, "链接") { t.Fatalf("out=%q err=%v", out, err) } if !c.ShouldHandle(context.Background(), "t", "a", "https://alidocs.dingtalk.com/i/nodes/base?sheetId=sheet&viewId=view") { t.Fatal("active setting session must intercept next message") } _, err = c.Handle(context.Background(), SettingInput{TenantKey: "t", Channel: "dingtalk", StaffID: "a", UnionID: "union", Text: "https://alidocs.dingtalk.com/i/nodes/base?sheetId=sheet&viewId=view", ConversationType: "direct"}) if err != nil { t.Fatal(err) } if r.bindings["question_table"].BaseID != "base" { t.Fatalf("binding=%+v", r.bindings) } if _, ok := r.sessions["t/a"]; ok { t.Fatal("setting session must be cleared") } } func TestControllerDetectsBootstrapCandidate(t *testing.T) { r := newMemoryRepo() c := NewController(r) c.now = func() time.Time { return time.Unix(100, 0) } _, err := c.Handle(context.Background(), SettingInput{TenantKey: "t", StaffID: "a", UnionID: "union", DisplayName: "Alice", Text: "/setting 123456", ConversationType: "direct"}) if err != nil { t.Fatal(err) } if r.candidate.Code != "123456" || r.candidate.StaffID != "a" { t.Fatalf("candidate=%+v", r.candidate) } }