package domain import ( "errors" "strings" "testing" ) func TestDemoSignalPolicyIsValidAndRoundTripsThroughTheDocument(t *testing.T) { policy := DemoProductPolicyV3() if err := policy.Validate(); err != nil { t.Fatalf("DemoProductPolicyV3().Validate() error = %v", err) } encoded, _, err := encodeProductPolicy(policy) if err != nil { t.Fatalf("encodeProductPolicy() error = %v", err) } decoded, err := decodeProductPolicy(encoded) if err != nil { t.Fatalf("decodeProductPolicy() error = %v", err) } if decoded.Assessment.Signals == nil || *decoded.Assessment.Signals != DemoAssessmentSignalPolicy() { t.Fatalf("decoded signals = %+v, want the demo values", decoded.Assessment.Signals) } if DemoProductPolicyV2().Assessment.Signals != nil { t.Fatal("DemoProductPolicyV2 gained a signal section; a stored version must not change") } } func TestSignalSectionAloneIsASchemaTwoExtensionAndNeverAcquiresDefaults(t *testing.T) { signals := DemoAssessmentSignalPolicy() policy := DemoProductPolicyV1() policy.Assessment.Signals = &signals encoded, _, err := encodeProductPolicy(policy) if err != nil { t.Fatalf("encodeProductPolicy() error = %v", err) } decoded, err := decodeProductPolicy(encoded) if err != nil || decoded.Assessment.Signals == nil || decoded.Assessment.Attempt != nil || decoded.Support != nil { t.Fatalf("decode signals-only schema 2 = %+v, error %v", decoded.Assessment, err) } legacy := editPolicySchemaDocument(t, []byte(demoProductPolicyV1Document), func(document map[string]any) { policySection(t, document, "assessment")["signals"] = nil }) if _, err := decodeProductPolicy(legacy); !errors.Is(err, ErrInvalidPolicy) { t.Fatalf("decodeProductPolicy(schema 1 with a signals key) error = %v, want ErrInvalidPolicy", err) } missingField := editPolicySchemaDocument(t, encoded, func(document map[string]any) { delete(policySection(t, policySection(t, document, "assessment"), "signals"), "fast_floor_integer_ms") }) if _, err := decodeProductPolicy(missingField); !errors.Is(err, ErrInvalidPolicy) { t.Fatalf("decodeProductPolicy(missing signal field) error = %v, want ErrInvalidPolicy", err) } } func TestSignalPolicyRejectsOutOfRangeAndInconsistentValues(t *testing.T) { tests := []struct { name string change func(*AssessmentSignalPolicy) }{ {"zero section", func(policy *AssessmentSignalPolicy) { *policy = AssessmentSignalPolicy{} }}, {"floor in seconds instead of milliseconds", func(policy *AssessmentSignalPolicy) { policy.FastFloorIntegerMS = 5 }}, {"review below attention", func(policy *AssessmentSignalPolicy) { policy.FastAnswersReview = policy.FastAnswersAttention - 1 }}, {"duplicate review below attention", func(policy *AssessmentSignalPolicy) { policy.DuplicateReviewBits = policy.DuplicateAttentionBits - 1 }}, {"deviation ratio below two", func(policy *AssessmentSignalPolicy) { policy.DeviationRatio = 15000 }}, {"deviation needs several answers", func(policy *AssessmentSignalPolicy) { policy.DeviationMinAnswers = 2 }}, {"evidence bits beyond a hash", func(policy *AssessmentSignalPolicy) { policy.DuplicateSequenceCapBits = 65 }}, {"client threshold zero", func(policy *AssessmentSignalPolicy) { policy.ClientClipboardEventsAttention = 0 }}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { policy := DemoAssessmentSignalPolicy() test.change(&policy) if err := policy.Validate(); !errors.Is(err, ErrInvalidPolicy) { t.Fatalf("Validate() error = %v, want ErrInvalidPolicy", err) } }) } } // A version published before the capture hints existed carries none of their // fields. It must decode with the hints switched off and encode back to the same // document, so its content hash and the bands of its attempts never change. func TestPolicyWithoutCaptureThresholdsDecodesOffAndEncodesUnchanged(t *testing.T) { policy := DemoProductPolicyV3() encoded, _, err := encodeProductPolicy(policy) if err != nil { t.Fatalf("encodeProductPolicy() error = %v", err) } for _, field := range []string{"client_screenshot_keys_attention", "client_devtools_attention", "extra_agents_attention", "extra_agents_review"} { if strings.Contains(string(encoded), field) { t.Fatalf("the document of the published policy holds %q, want it absent", field) } } decoded, err := decodeProductPolicy(encoded) if err != nil { t.Fatalf("decodeProductPolicy() error = %v", err) } signals := decoded.Assessment.Signals if signals == nil || signals.ClientScreenshotKeysAttention != 0 || signals.ClientDevtoolsAttention != 0 || signals.ExtraAgentsAttention != 0 || signals.ExtraAgentsReview != 0 { t.Fatalf("decoded signals = %+v, want the capture hints off", signals) } again, _, err := encodeProductPolicy(decoded) if err != nil || string(again) != string(encoded) { t.Fatalf("re-encoded document differs from the stored one (error %v)", err) } } func TestCaptureThresholdsRoundTripAndAreValidated(t *testing.T) { signals := DemoAssessmentSignalPolicyWithCaptureHints() if signals.ClientScreenshotKeysAttention == 0 || signals.ClientDevtoolsAttention == 0 || signals.ExtraAgentsAttention == 0 { t.Fatalf("DemoAssessmentSignalPolicyWithCaptureHints() = %+v, want the hints switched on", signals) } policy := DemoProductPolicyV3() policy.Assessment.Signals = &signals encoded, _, err := encodeProductPolicy(policy) if err != nil { t.Fatalf("encodeProductPolicy() error = %v", err) } decoded, err := decodeProductPolicy(encoded) if err != nil || decoded.Assessment.Signals == nil || *decoded.Assessment.Signals != signals { t.Fatalf("decoded signals = %+v, error %v, want the thresholds back", decoded.Assessment.Signals, err) } if DemoAssessmentSignalPolicy().ClientScreenshotKeysAttention != 0 { t.Fatal("DemoAssessmentSignalPolicy gained a capture threshold; a stored version must not change") } for name, change := range map[string]func(*AssessmentSignalPolicy){ "negative threshold": func(policy *AssessmentSignalPolicy) { policy.ClientDevtoolsAttention = -1 }, "threshold beyond the bound": func(policy *AssessmentSignalPolicy) { policy.ClientScreenshotKeysAttention = maxSignalCount + 1 }, "review without attention": func(policy *AssessmentSignalPolicy) { policy.ExtraAgentsAttention, policy.ExtraAgentsReview = 0, 3 }, "review below attention": func(policy *AssessmentSignalPolicy) { policy.ExtraAgentsAttention, policy.ExtraAgentsReview = 4, 3 }, "attention without a review": func(policy *AssessmentSignalPolicy) { policy.ExtraAgentsAttention, policy.ExtraAgentsReview = 2, 0 }, } { t.Run(name, func(t *testing.T) { policy := DemoAssessmentSignalPolicyWithCaptureHints() change(&policy) if err := policy.Validate(); !errors.Is(err, ErrInvalidPolicy) { t.Fatalf("Validate() error = %v, want ErrInvalidPolicy", err) } }) } }