Files
archie/internal/app/archied/outcome_post_test.go
T

79 lines
2.3 KiB
Go

package archied
import (
"context"
"errors"
"log/slog"
"strings"
"testing"
"unicode/utf8"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
)
type outcomeStore struct{ posted []int64 }
func (s *outcomeStore) UnpostedOutcomes(context.Context) ([]*task.Task, error) {
return []*task.Task{{ID: 7, Origin: "web:chat-1"}}, nil
}
func (s *outcomeStore) MarkOutcomePosted(_ context.Context, id int64, _ string) error {
s.posted = append(s.posted, id)
return nil
}
func TestOutcomeTextBoundsTheTitle(t *testing.T) {
title := strings.Repeat("workflow_draft returns InvalidArgument because ", 30) + "\nsecond line"
tests := []struct {
name string
task *task.Task
}{
{"approval", &task.Task{ID: 7, Title: title, Status: task.StatusWaitingApproval}},
{"pr open", &task.Task{ID: 7, Title: title, Status: task.StatusPROpen, PRNumber: 6}},
{"finished", &task.Task{ID: 7, Title: title, Status: task.StatusRejected}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
text := outcomeText(tt.task)
if strings.Contains(text, title) {
t.Fatalf("text echoes the whole title: %q", text)
}
if strings.Contains(text, "\n") {
t.Errorf("text is not one line: %q", text)
}
if got := utf8.RuneCountInString(text); got > 200 {
t.Errorf("text is %d runes, want at most 200: %q", got, text)
}
if !strings.Contains(text, "workflow_draft returns") {
t.Errorf("text dropped the start of the title: %q", text)
}
})
}
}
func TestPostPendingOutcomes(t *testing.T) {
tests := []struct {
name string
err error
posted bool
}{
{"delivered", nil, true},
{"no such channel", status.Error(codes.NotFound, "messaging: no such channel"), true},
{"channel stopped", status.Error(codes.FailedPrecondition, "messaging: channel unavailable"), false},
{"messaging down", errors.New("connection refused"), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
store := &outcomeStore{}
deliver := func(context.Context, string, string) error { return tt.err }
postPendingOutcomes(context.Background(), store, deliver, func(int64) {}, slog.New(slog.DiscardHandler))
if got := len(store.posted) == 1; got != tt.posted {
t.Fatalf("posted = %v, want %v", got, tt.posted)
}
})
}
}