Files
ai-disclosure-standard/internal/declaration/model_test.go
T
2026-07-20 21:03:05 +02:00

115 lines
4.0 KiB
Go

package declaration
import (
"errors"
"net/url"
"testing"
)
func TestPresetQuery(t *testing.T) {
v := url.Values{"preset": {"research"}, "subject": {"https://example.org/post"}, "lang": {"fr"}}
d, err := NewFromQuery(v, "https://example.org/context/v1")
if err != nil {
t.Fatal(err)
}
c := d.Components["text"]
if c.AIExtent != "assisted" || len(c.Activities) != 1 || c.Activities[0] != "research" {
t.Fatalf("unexpected component: %#v", c)
}
if d.Language != "fr" {
t.Fatalf("unexpected language: %s", d.Language)
}
}
func TestNoAIRejectsActivities(t *testing.T) {
d := Declaration{Context: "https://example.org/context/v1", Type: "AIUsageDeclaration", SchemaVersion: SchemaVersion, Language: "de", Assurance: "selfDeclared", Components: map[string]Component{"text": {AIExtent: "none", Activities: []string{"research"}, HumanReview: "editorial"}}}
if Validate(d) == nil {
t.Fatal("expected validation error")
}
}
func TestCustomRequiresPro(t *testing.T) {
v := url.Values{"preset": {"research"}, "lang": {"en"}, "customTitle": {"Custom"}}
_, err := NewFromQuery(v, "https://example.org/context/v1")
if !errors.Is(err, ErrCustomTextRequiresPro) {
t.Fatalf("unexpected error: %v", err)
}
d, err := NewFromQueryWithOptions(v, "https://example.org/context/v1", ParseOptions{AllowCustomText: true, DefaultLanguage: "en"})
if err != nil {
t.Fatal(err)
}
if d.Presentation == nil || d.Presentation.Title != "Custom" {
t.Fatalf("missing presentation: %#v", d.Presentation)
}
}
func TestLegacySchemaValidation(t *testing.T) {
d := Declaration{Context: "https://example.org/context/v1", Type: "AIUsageDeclaration", SchemaVersion: LegacySchemaVersion, Language: "en", Assurance: "selfDeclared", Components: map[string]Component{"text": {AIExtent: "assisted", HumanReview: "editorial"}}}
if err := Validate(d); err != nil {
t.Fatalf("legacy declaration should remain valid: %v", err)
}
d.Presentation = &Presentation{Title: "Not allowed in 1.0"}
if Validate(d) == nil {
t.Fatal("expected presentation to require schema 1.1")
}
}
func TestArticleDeclarationFromQuery(t *testing.T) {
q := url.Values{
"mode": {"article"}, "lang": {"de"},
"textExtent": {"none"}, "imageExtent": {"full"}, "researchExtent": {"assisted"},
}
d, err := NewFromQuery(q, "https://example.org/context/v1")
if err != nil {
t.Fatal(err)
}
if len(d.Components) != 3 {
t.Fatalf("components=%d", len(d.Components))
}
if d.Components["text"].AIExtent != "none" {
t.Fatalf("unexpected text extent")
}
if d.Components["text"].HumanReview != "none" {
t.Fatalf("no-AI component review=%q, want none", d.Components["text"].HumanReview)
}
if d.Components["research"].HumanReview != "editorial" {
t.Fatalf("AI-assisted component review=%q, want editorial default", d.Components["research"].HumanReview)
}
if got := d.Components["research"].Activities; len(got) != 1 || got[0] != "research" {
t.Fatalf("research activities=%v", got)
}
if got := d.Components["image"].Activities; len(got) != 1 || got[0] != "imageGeneration" {
t.Fatalf("image activities=%v", got)
}
}
func TestArticleDeclarationKeepsPerComponentReviews(t *testing.T) {
q := url.Values{
"mode": {"article"}, "lang": {"de"},
"textExtent": {"assisted"}, "textReview": {"basic"},
"imageExtent": {"full"}, "imageReview": {"expert"},
}
d, err := NewFromQuery(q, "https://example.org/context/v1")
if err != nil {
t.Fatal(err)
}
if got := d.Components["text"].HumanReview; got != "basic" {
t.Fatalf("text review=%q", got)
}
if got := d.Components["image"].HumanReview; got != "expert" {
t.Fatalf("image review=%q", got)
}
}
func TestAssuranceFromQuery(t *testing.T) {
for _, assurance := range []string{"selfDeclared", "technicallyRecorded", "signed", "verified"} {
d, err := NewFromQuery(url.Values{"preset": {"research"}, "lang": {"de"}, "assurance": {assurance}}, "https://example.org/context/v1")
if err != nil {
t.Fatalf("assurance %s: %v", assurance, err)
}
if d.Assurance != assurance {
t.Fatalf("assurance=%q, want %q", d.Assurance, assurance)
}
}
}