package copydesk import ( "strings" "testing" ) func TestDelegationOffSkipsCopy(t *testing.T) { if ShouldRun(Delegation{}, true) { t.Fatal("disabled delegation ran Copy") } if ShouldRun(Delegation{Owner: "paul", Revision: 1}, false) { t.Fatal("no strings ran Copy") } if !ShouldRun(Delegation{Owner: "paul", Revision: 1}, true) { t.Fatal("delegated strings skipped") } } func TestForgedAcceptIsPending(t *testing.T) { d := Delegation{Owner: "paul", Revision: 2} s := Literal{Key: "save", Text: "Save changes"} if Accepted(s, "head", d, nil) { t.Fatal("marker without server record accepted") } r := Record{Key: s.Key, Hash: Hash(s.Text), Candidate: "head", Owner: "paul", Revision: 2} if !Accepted(s, "head", d, []Record{r}) { t.Fatal("valid server record refused") } for _, bad := range []Record{{Key: s.Key, Hash: Hash("old"), Candidate: "head", Owner: "paul", Revision: 2}, {Key: s.Key, Hash: Hash(s.Text), Candidate: "head", Owner: "other", Revision: 2}, {Key: s.Key, Hash: Hash(s.Text), Candidate: "old", Owner: "paul", Revision: 2}} { if Accepted(s, "head", d, []Record{bad}) { t.Fatal("stale record accepted") } } } func TestScopeRejectsLogicAndUnknownKeys(t *testing.T) { before := []byte(`if ok { show("Old words") }`) s := Literal{Key: "save", Path: "a.go", Start: 14, End: 23, Text: "Old words"} review := Review{Version: 1, Strings: []Wording{{Key: "save", Text: "New words", Action: "accept"}}} want := []byte(`if ok { show("New words") }`) if err := ValidateScope(before, want, []Literal{s}, review); err != nil { t.Fatal(err) } if ValidateScope(before, []byte(`if no { show("New words") }`), []Literal{s}, review) == nil { t.Fatal("logic edit allowed") } review.Strings[0].Key = "unknown" if _, err := ValidateReview([]Literal{s}, review); err == nil { t.Fatal("unknown key accepted") } } func TestUnknownNeverEqualsUnknown(t *testing.T) { if SameFailure(Signature{}, Signature{}) { t.Fatal("unknown equals unknown") } a := Classify(Input{Version: 1, Units: []string{"copy"}}, Classification{Version: 1, Complete: true, Failures: []Failure{{Unit: "copy", Test: "inventory", Code: "pending", Subject: "save", Category: "copy"}}}, "adapter") if !SameFailure(a, a) { t.Fatal("complete classified signatures differ") } b := a b.Complete = false if SameFailure(a, b) { t.Fatal("incomplete equals complete") } b = a b.Adapter = "" if SameFailure(a, b) { t.Fatal("unclassified equals classified") } if SameFailure(Classify(Input{Version: 1, Units: []string{"copy", "test"}}, Classification{Version: 1, Complete: true, Failures: a.Failures}, "adapter"), a) { t.Fatal("missing unit was complete") } } func TestOnlyDelegatingOwnerTeaches(t *testing.T) { d := Delegation{Owner: "paul", Revision: 1} f := Feedback{SourceID: "event-1", Actor: "other", Key: "save", Before: "old", After: "new", Origin: "equipo"} if CanLearn(d, f) { t.Fatal("other owner's edit taught Copy") } f.Actor = "paul" if !CanLearn(d, f) { t.Fatal("owner's edit refused") } f.Origin = "forge" if CanLearn(d, f) { t.Fatal("forge inference taught Copy") } } func TestReviewPreservesPlaceholdersAndQuestions(t *testing.T) { s := []Literal{{Key: "k", Text: "Hello %s"}} r := Review{Version: 1, Strings: []Wording{{Key: "k", Text: "Welcome", Action: "accept"}}} if _, err := ValidateReview(s, r); err == nil { t.Fatal("placeholder removed") } r.Strings[0] = Wording{Key: "k", Text: "Welcome %s", Action: "accept", Flag: "Check this wording"} if _, err := ValidateReview(s, r); err != nil { t.Fatal(err) } r.Strings = append(r.Strings, r.Strings[0]) if _, err := ValidateReview(s, r); err == nil { t.Fatal("duplicate key") } } func TestClassifierDoesNotLoseUnknownUnits(t *testing.T) { s := Classify(Input{Version: 1, Units: []string{"tests"}, Log: strings.Repeat("x", 200)}, Classification{Version: 1, Complete: true}, "adapter") if s.Complete || s.CopyOnly() || len(s.Failures) != 1 || s.Failures[0].Category != "other" { t.Fatalf("unknown became copy: %+v", s) } } func TestCopyBookkeepingSkipsModelOnlyWithRecords(t *testing.T) { d := Delegation{Owner: "paul", Revision: 1} ex := []Literal{{Key: "save", Text: "Save"}} if _, ok := ReviewedWords(ex, "head", d, nil); ok { t.Fatal("marker alone skipped model") } r, ok := ReviewedWords(ex, "head", d, []Record{{Key: "save", Hash: Hash("Save"), Owner: "paul", Revision: 1, Candidate: "head", Flag: "Check wording"}}) if !ok || len(r.Strings) != 1 || r.Strings[0].Text != "Save" || r.Strings[0].Flag != "Check wording" { t.Fatal("matching record did not skip model") } } func TestGenericScopeRejectsLiteralInjection(t *testing.T) { for _, tc := range []struct { before, text string start, end int }{{"show('Old words')", "New'); run(); ('", 6, 15}, {"show(`Old words`)", "New ${(()=>{run()})()}", 6, 15}} { t.Run(tc.before, func(t *testing.T) { before := []byte(tc.before) after := append(append(append([]byte{}, before[:tc.start]...), []byte(tc.text)...), before[tc.end:]...) ex := []Literal{{Key: "k", Text: "Old words", Start: tc.start, End: tc.end}} review := Review{Version: 1, Strings: []Wording{{Key: "k", Text: tc.text, Action: "accept"}}} if ValidateScope(before, after, ex, review) == nil { t.Fatalf("literal injection accepted: %s", tc.text) } }) } }