189 lines
8.4 KiB
Go
189 lines
8.4 KiB
Go
package domain
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestDemoSupportPolicyUsesExplicitDemoTargetsAndRequestLimit(t *testing.T) {
|
|
policy := DemoSupportPolicy()
|
|
if policy.SupportResponseDays() != 5 || policy.ComplaintResponseDays() != 5 ||
|
|
policy.ManualAssessmentResponseDays() != 5 || policy.AppealResponseDays() != 10 ||
|
|
policy.MaxRequestsPerWindow() != 10 || policy.RequestWindow() != time.Hour ||
|
|
policy.MaxAppealsPerTarget() != 1 || policy.PausesWhenWaitingForUser() || policy.BusinessCalendarVersion() != 1 {
|
|
t.Fatalf("DemoSupportPolicy() = %+v, want the documented demo defaults", policy)
|
|
}
|
|
}
|
|
|
|
func TestNewSupportPolicyRejectsOutOfRangeInputs(t *testing.T) {
|
|
valid := SupportPolicyInput{
|
|
SupportResponseDays: 5, ComplaintResponseDays: 5, ManualAssessmentResponseDays: 5,
|
|
AppealResponseDays: 10, MaxRequestsPerWindow: 10, RequestWindow: time.Hour,
|
|
MaxAppealsPerTarget: 1, BusinessCalendarVersion: 1,
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
change func(*SupportPolicyInput)
|
|
}{
|
|
{name: "zero target", change: func(input *SupportPolicyInput) { input.SupportResponseDays = 0 }},
|
|
{name: "target above bound", change: func(input *SupportPolicyInput) { input.AppealResponseDays = 61 }},
|
|
{name: "zero request limit", change: func(input *SupportPolicyInput) { input.MaxRequestsPerWindow = 0 }},
|
|
{name: "request limit above bound", change: func(input *SupportPolicyInput) { input.MaxRequestsPerWindow = 1001 }},
|
|
{name: "zero window", change: func(input *SupportPolicyInput) { input.RequestWindow = 0 }},
|
|
{name: "window above bound", change: func(input *SupportPolicyInput) { input.RequestWindow = 24*time.Hour + time.Nanosecond }},
|
|
{name: "fractional-second window", change: func(input *SupportPolicyInput) { input.RequestWindow = time.Second + time.Nanosecond }},
|
|
{name: "appeals per target above bound", change: func(input *SupportPolicyInput) { input.MaxAppealsPerTarget = 2 }},
|
|
{name: "paused while waiting", change: func(input *SupportPolicyInput) { input.PauseWhenWaitingForUser = true }},
|
|
{name: "unsupported calendar", change: func(input *SupportPolicyInput) { input.BusinessCalendarVersion = 2 }},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
input := valid
|
|
test.change(&input)
|
|
if _, err := NewSupportPolicy(input); !errors.Is(err, ErrInvalidSupportPolicy) {
|
|
t.Fatalf("NewSupportPolicy() error = %v, want ErrInvalidSupportPolicy", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSupportPolicyDocumentRoundTripAndLegacyOmission(t *testing.T) {
|
|
reader := documentReader{}
|
|
if decoded := decodeOptionalSupportPolicy(&reader, nil); decoded != nil || reader.missing != "" {
|
|
t.Fatalf("omitted support section decoded as %v with missing field %q, want no policy and no error", decoded, reader.missing)
|
|
}
|
|
policy := DemoSupportPolicy()
|
|
encodedDocument, err := encodeSupportPolicy(policy)
|
|
if err != nil {
|
|
t.Fatalf("encodeSupportPolicy() error = %v", err)
|
|
}
|
|
encoded, err := json.Marshal(encodedDocument)
|
|
if err != nil {
|
|
t.Fatalf("Marshal(support policy) error = %v", err)
|
|
}
|
|
decoder := json.NewDecoder(bytes.NewReader(encoded))
|
|
decoder.DisallowUnknownFields()
|
|
var parsedDocument supportPolicyDocument
|
|
if err := decoder.Decode(&parsedDocument); err != nil {
|
|
t.Fatalf("Decode(support policy document) error = %v", err)
|
|
}
|
|
reader = documentReader{}
|
|
decoded := decodeOptionalSupportPolicy(&reader, &parsedDocument)
|
|
if reader.missing != "" || decoded == nil || !reflect.DeepEqual(*decoded, policy) {
|
|
t.Fatalf("decoded policy = %+v, missing = %q, want %+v", decoded, reader.missing, policy)
|
|
}
|
|
if err := decoded.Validate(); err != nil {
|
|
t.Fatalf("decoded.Validate() error = %v", err)
|
|
}
|
|
if !strings.Contains(string(encoded), `"pause_when_waiting_for_user":false`) ||
|
|
!strings.Contains(string(encoded), `"business_calendar_version":1`) {
|
|
t.Fatalf("document omitted explicit false or calendar version: %s", encoded)
|
|
}
|
|
}
|
|
|
|
func TestSupportPolicyDocumentRequiresEveryFieldAndRejectsUnknownFields(t *testing.T) {
|
|
encodedDocument, err := encodeSupportPolicy(DemoSupportPolicy())
|
|
if err != nil {
|
|
t.Fatalf("encodeSupportPolicy() error = %v", err)
|
|
}
|
|
encoded, err := json.Marshal(encodedDocument)
|
|
if err != nil {
|
|
t.Fatalf("Marshal(support policy) error = %v", err)
|
|
}
|
|
var parsedDocument supportPolicyDocument
|
|
decoder := json.NewDecoder(bytes.NewReader(append(encoded[:len(encoded)-1], []byte(`,"unexpected":true}`)...)))
|
|
decoder.DisallowUnknownFields()
|
|
if err := decoder.Decode(&parsedDocument); err == nil {
|
|
t.Fatal("support policy document accepted an unknown field")
|
|
}
|
|
|
|
var complete supportPolicyDocument
|
|
if err := json.Unmarshal(encoded, &complete); err != nil {
|
|
t.Fatalf("Unmarshal(support policy) error = %v", err)
|
|
}
|
|
complete.RequestWindowSeconds = nil
|
|
reader := documentReader{}
|
|
decoded := decodeSupportPolicy(&reader, &complete)
|
|
if reader.missing != "support.request_window_seconds" || !decoded.IsZero() {
|
|
t.Fatalf("missing field = %q and decoded = %+v, want exact missing-field path and zero policy", reader.missing, decoded)
|
|
}
|
|
}
|
|
|
|
func TestSupportPolicyValidationDoesNotFillOmittedValues(t *testing.T) {
|
|
if err := (SupportPolicy{}).Validate(); !errors.Is(err, ErrInvalidSupportPolicy) {
|
|
t.Fatalf("zero SupportPolicy.Validate() error = %v, want ErrInvalidSupportPolicy", err)
|
|
}
|
|
policy := DemoProductPolicyV1()
|
|
if policy.Support != nil {
|
|
t.Fatal("legacy DemoProductPolicyV1 unexpectedly contains support defaults")
|
|
}
|
|
if err := policy.Validate(); err != nil {
|
|
t.Fatalf("legacy policy without optional support section must remain valid: %v", err)
|
|
}
|
|
v2 := DemoProductPolicyV2()
|
|
if v2.Support == nil || v2.Support.SupportResponseDays() != 5 || v2.Support.AppealResponseDays() != 10 {
|
|
t.Fatalf("DemoProductPolicyV2 support section = %+v, want explicit support demo values", v2.Support)
|
|
}
|
|
v2.Support = &SupportPolicy{}
|
|
if err := v2.Validate(); !errors.Is(err, ErrInvalidPolicy) || !errors.Is(err, ErrInvalidSupportPolicy) {
|
|
t.Fatalf("ProductPolicy.Validate() error = %v, want both policy and support policy errors", err)
|
|
}
|
|
}
|
|
|
|
func TestSupportPolicyResponseTargetUsesVersionedWeekdaysInUTC(t *testing.T) {
|
|
policy := DemoSupportPolicy()
|
|
createdAt := time.Date(2026, time.October, 9, 15, 30, 0, 0, time.FixedZone("local", 3*60*60)) // Friday in UTC.
|
|
tests := []struct {
|
|
caseType SupportCaseType
|
|
wantDays BusinessDays
|
|
}{
|
|
{caseType: SupportCaseTypeSupport, wantDays: 5},
|
|
{caseType: SupportCaseTypeComplaint, wantDays: 5},
|
|
{caseType: SupportCaseTypeManualAssessment, wantDays: 5},
|
|
{caseType: SupportCaseTypeGradeAppeal, wantDays: 10},
|
|
{caseType: SupportCaseTypeAnticheatAppeal, wantDays: 10},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(string(test.caseType), func(t *testing.T) {
|
|
target, err := policy.ResponseTarget(test.caseType, 7, createdAt)
|
|
if err != nil {
|
|
t.Fatalf("ResponseTarget() error = %v", err)
|
|
}
|
|
wantDueAt := time.Date(2026, time.October, 16, 12, 30, 0, 0, time.UTC)
|
|
if test.wantDays == 10 {
|
|
wantDueAt = time.Date(2026, time.October, 23, 12, 30, 0, 0, time.UTC)
|
|
}
|
|
if target.BusinessDays() != test.wantDays || target.DueAt() != wantDueAt || target.PolicyVersion() != 7 ||
|
|
target.BusinessCalendarVersion() != 1 || target.Snapshot().CalendarVersion != 1 ||
|
|
target.Kind() != SupportResponseTargetOperationalGoal {
|
|
t.Fatalf("target = %+v, want %d business days through %s at policy 7", target, test.wantDays, wantDueAt)
|
|
}
|
|
})
|
|
}
|
|
if DemoSupportPolicy().PausesWhenWaitingForUser() {
|
|
t.Fatal("waiting for the user must not pause an MVP response target")
|
|
}
|
|
}
|
|
|
|
func TestSupportPolicyRejectsUnsetPolicyUnknownCaseAndInvalidVersion(t *testing.T) {
|
|
createdAt := time.Date(2026, time.October, 7, 10, 0, 0, 0, time.UTC)
|
|
if _, err := (SupportPolicy{}).ResponseTarget(SupportCaseTypeSupport, 1, createdAt); !errors.Is(err, ErrInvalidSupportPolicy) {
|
|
t.Fatalf("zero policy error = %v, want ErrInvalidSupportPolicy", err)
|
|
}
|
|
policy := DemoSupportPolicy()
|
|
if _, err := policy.ResponseTarget("unknown", 1, createdAt); !errors.Is(err, ErrInvalidSupportPolicy) {
|
|
t.Fatalf("unknown case type error = %v, want ErrInvalidSupportPolicy", err)
|
|
}
|
|
if _, err := policy.ResponseTarget(SupportCaseTypeSupport, 0, createdAt); !errors.Is(err, ErrInvalidSupportPolicy) {
|
|
t.Fatalf("zero policy version error = %v, want ErrInvalidSupportPolicy", err)
|
|
}
|
|
if _, err := policy.ResponseTarget(SupportCaseTypeSupport, 1, time.Time{}); !errors.Is(err, ErrInvalidSupportPolicy) {
|
|
t.Fatalf("zero creation time error = %v, want ErrInvalidSupportPolicy", err)
|
|
}
|
|
}
|