180 lines
4.9 KiB
Go
180 lines
4.9 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/coverage"
|
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/faqtable"
|
|
)
|
|
|
|
type supportNotificationRepoStub struct{}
|
|
|
|
func (*supportNotificationRepoStub) Claim(context.Context, int, int, time.Time) ([]coverage.PendingSupportCase, error) {
|
|
return nil, nil
|
|
}
|
|
func (*supportNotificationRepoStub) MarkNotified(context.Context, string) error { return nil }
|
|
func (*supportNotificationRepoStub) MarkFailed(context.Context, string, string) error { return nil }
|
|
|
|
func testFAQRecordLinks(t *testing.T) *faqtable.RecordLinkBuilder {
|
|
t.Helper()
|
|
builder, err := faqtable.NewRecordLinkBuilder("base", "sheet", "view")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return builder
|
|
}
|
|
|
|
type supportDispatcherStub struct {
|
|
mu sync.Mutex
|
|
calls int
|
|
errs []error
|
|
callCh chan int
|
|
}
|
|
|
|
func (s *supportDispatcherStub) RunOnce(context.Context) (coverage.DispatcherStats, error) {
|
|
s.mu.Lock()
|
|
s.calls++
|
|
call := s.calls
|
|
var err error
|
|
if call <= len(s.errs) {
|
|
err = s.errs[call-1]
|
|
}
|
|
s.mu.Unlock()
|
|
if s.callCh != nil {
|
|
select {
|
|
case s.callCh <- call:
|
|
default:
|
|
}
|
|
}
|
|
return coverage.DispatcherStats{Claimed: 1, Notified: 1}, err
|
|
}
|
|
|
|
func TestBuildSupportDispatcherDisabledWithoutGroup(t *testing.T) {
|
|
dispatcher, err := buildSupportDispatcher(nil, "", "", " ", nil)
|
|
if err != nil || dispatcher != nil {
|
|
t.Fatalf("dispatcher=%v err=%v, want disabled", dispatcher, err)
|
|
}
|
|
}
|
|
|
|
func TestBuildSupportDispatcherBlocksWithoutFAQLinks(t *testing.T) {
|
|
dispatcher, err := buildSupportDispatcher(&supportNotificationRepoStub{}, "client", "secret", "group", nil)
|
|
if err != nil || dispatcher != nil {
|
|
t.Fatalf("dispatcher=%v err=%v, want blocked without process failure", dispatcher, err)
|
|
}
|
|
}
|
|
|
|
func TestBuildSupportDispatcherEnabledFailsClosed(t *testing.T) {
|
|
repo := &supportNotificationRepoStub{}
|
|
links := testFAQRecordLinks(t)
|
|
for _, tc := range []struct {
|
|
name string
|
|
repo coverage.NotificationRepository
|
|
clientID, secret, gid string
|
|
links *faqtable.RecordLinkBuilder
|
|
}{
|
|
{name: "missing repo", clientID: "client", secret: "secret", gid: "group", links: links},
|
|
{name: "missing client", repo: repo, secret: "secret", gid: "group", links: links},
|
|
{name: "missing secret", repo: repo, clientID: "client", gid: "group", links: links},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if _, err := buildSupportDispatcher(tc.repo, tc.clientID, tc.secret, tc.gid, tc.links); err == nil {
|
|
t.Fatal("expected configuration error")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestBuildSupportDispatcherValidDoesNotSend(t *testing.T) {
|
|
dispatcher, err := buildSupportDispatcher(&supportNotificationRepoStub{}, " client ", " secret ", " group ", testFAQRecordLinks(t))
|
|
if err != nil {
|
|
t.Fatalf("buildSupportDispatcher: %v", err)
|
|
}
|
|
if dispatcher == nil {
|
|
t.Fatal("expected dispatcher")
|
|
}
|
|
}
|
|
|
|
func TestRunSupportDispatcherRunsImmediatelyAndStops(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
stub := &supportDispatcherStub{callCh: make(chan int, 4)}
|
|
done := make(chan struct{})
|
|
go func() {
|
|
runSupportDispatcher(ctx, stub, time.Hour)
|
|
close(done)
|
|
}()
|
|
|
|
select {
|
|
case <-stub.callCh:
|
|
case <-time.After(500 * time.Millisecond):
|
|
t.Fatal("dispatcher did not run immediately")
|
|
}
|
|
cancel()
|
|
select {
|
|
case <-done:
|
|
case <-time.After(500 * time.Millisecond):
|
|
t.Fatal("dispatcher did not stop after context cancellation")
|
|
}
|
|
}
|
|
|
|
func TestRunSupportDispatcherContinuesAfterError(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
stub := &supportDispatcherStub{
|
|
errs: []error{errors.New("temporary db error")},
|
|
callCh: make(chan int, 4),
|
|
}
|
|
done := make(chan struct{})
|
|
go func() {
|
|
runSupportDispatcher(ctx, stub, 10*time.Millisecond)
|
|
close(done)
|
|
}()
|
|
|
|
for want := 1; want <= 2; want++ {
|
|
select {
|
|
case got := <-stub.callCh:
|
|
if got != want {
|
|
t.Fatalf("call=%d, want %d", got, want)
|
|
}
|
|
case <-time.After(500 * time.Millisecond):
|
|
t.Fatalf("missing dispatcher call %d", want)
|
|
}
|
|
}
|
|
cancel()
|
|
<-done
|
|
}
|
|
|
|
func TestRunSupportDispatcherRejectsNonPositiveInterval(t *testing.T) {
|
|
stub := &supportDispatcherStub{}
|
|
runSupportDispatcher(context.Background(), stub, 0)
|
|
stub.mu.Lock()
|
|
defer stub.mu.Unlock()
|
|
if stub.calls != 0 {
|
|
t.Fatalf("calls=%d, want 0", stub.calls)
|
|
}
|
|
}
|
|
|
|
func TestMainWiresCoverageSupportNotifications(t *testing.T) {
|
|
data, err := os.ReadFile("main.go")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
source := string(data)
|
|
for _, fragment := range []string{
|
|
`os.Getenv("DINGTALK_SUPPORT_GROUP_OPEN_CONVERSATION_ID")`,
|
|
`coverage.NewService(store)`,
|
|
`qa.WithSucceededTurnObserver`,
|
|
`case <-runtimeReady:`,
|
|
`runSupportDispatcher(ctx, supportDispatcher, 5*time.Second)`,
|
|
} {
|
|
if !strings.Contains(source, fragment) {
|
|
t.Errorf("main support notification wiring missing %q", fragment)
|
|
}
|
|
}
|
|
}
|