| 1 | package copydesk |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestDelegationOffSkipsCopy(t *testing.T) { |
| 9 | if ShouldRun(Delegation{}, true) { |
| 10 | t.Fatal("disabled delegation ran Copy") |
| 11 | } |
| 12 | if ShouldRun(Delegation{Owner: "paul", Revision: 1}, false) { |
| 13 | t.Fatal("no strings ran Copy") |
| 14 | } |
| 15 | if !ShouldRun(Delegation{Owner: "paul", Revision: 1}, true) { |
| 16 | t.Fatal("delegated strings skipped") |
| 17 | } |
| 18 | } |
| 19 | func TestForgedAcceptIsPending(t *testing.T) { |
| 20 | d := Delegation{Owner: "paul", Revision: 2} |
| 21 | s := Literal{Key: "save", Text: "Save changes"} |
| 22 | if Accepted(s, "head", d, nil) { |
| 23 | t.Fatal("marker without server record accepted") |
| 24 | } |
| 25 | r := Record{Key: s.Key, Hash: Hash(s.Text), Candidate: "head", Owner: "paul", Revision: 2} |
| 26 | if !Accepted(s, "head", d, []Record{r}) { |
| 27 | t.Fatal("valid server record refused") |
| 28 | } |
| 29 | 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}} { |
| 30 | if Accepted(s, "head", d, []Record{bad}) { |
| 31 | t.Fatal("stale record accepted") |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | func TestScopeRejectsLogicAndUnknownKeys(t *testing.T) { |
| 36 | before := []byte(`if ok { show("Old words") }`) |
| 37 | s := Literal{Key: "save", Path: "a.go", Start: 14, End: 23, Text: "Old words"} |
| 38 | review := Review{Version: 1, Strings: []Wording{{Key: "save", Text: "New words", Action: "accept"}}} |
| 39 | want := []byte(`if ok { show("New words") }`) |
| 40 | if err := ValidateScope(before, want, []Literal{s}, review); err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | if ValidateScope(before, []byte(`if no { show("New words") }`), []Literal{s}, review) == nil { |
| 44 | t.Fatal("logic edit allowed") |
| 45 | } |
| 46 | review.Strings[0].Key = "unknown" |
| 47 | if _, err := ValidateReview([]Literal{s}, review); err == nil { |
| 48 | t.Fatal("unknown key accepted") |
| 49 | } |
| 50 | } |
| 51 | func TestUnknownNeverEqualsUnknown(t *testing.T) { |
| 52 | if SameFailure(Signature{}, Signature{}) { |
| 53 | t.Fatal("unknown equals unknown") |
| 54 | } |
| 55 | 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") |
| 56 | if !SameFailure(a, a) { |
| 57 | t.Fatal("complete classified signatures differ") |
| 58 | } |
| 59 | b := a |
| 60 | b.Complete = false |
| 61 | if SameFailure(a, b) { |
| 62 | t.Fatal("incomplete equals complete") |
| 63 | } |
| 64 | b = a |
| 65 | b.Adapter = "" |
| 66 | if SameFailure(a, b) { |
| 67 | t.Fatal("unclassified equals classified") |
| 68 | } |
| 69 | if SameFailure(Classify(Input{Version: 1, Units: []string{"copy", "test"}}, Classification{Version: 1, Complete: true, Failures: a.Failures}, "adapter"), a) { |
| 70 | t.Fatal("missing unit was complete") |
| 71 | } |
| 72 | } |
| 73 | func TestOnlyDelegatingOwnerTeaches(t *testing.T) { |
| 74 | d := Delegation{Owner: "paul", Revision: 1} |
| 75 | f := Feedback{SourceID: "event-1", Actor: "other", Key: "save", Before: "old", After: "new", Origin: "equipo"} |
| 76 | if CanLearn(d, f) { |
| 77 | t.Fatal("other owner's edit taught Copy") |
| 78 | } |
| 79 | f.Actor = "paul" |
| 80 | if !CanLearn(d, f) { |
| 81 | t.Fatal("owner's edit refused") |
| 82 | } |
| 83 | f.Origin = "forge" |
| 84 | if CanLearn(d, f) { |
| 85 | t.Fatal("forge inference taught Copy") |
| 86 | } |
| 87 | } |
| 88 | func TestReviewPreservesPlaceholdersAndQuestions(t *testing.T) { |
| 89 | s := []Literal{{Key: "k", Text: "Hello %s"}} |
| 90 | r := Review{Version: 1, Strings: []Wording{{Key: "k", Text: "Welcome", Action: "accept"}}} |
| 91 | if _, err := ValidateReview(s, r); err == nil { |
| 92 | t.Fatal("placeholder removed") |
| 93 | } |
| 94 | r.Strings[0] = Wording{Key: "k", Text: "Welcome %s", Action: "accept", Flag: "Check this wording"} |
| 95 | if _, err := ValidateReview(s, r); err != nil { |
| 96 | t.Fatal(err) |
| 97 | } |
| 98 | r.Strings = append(r.Strings, r.Strings[0]) |
| 99 | if _, err := ValidateReview(s, r); err == nil { |
| 100 | t.Fatal("duplicate key") |
| 101 | } |
| 102 | } |
| 103 | func TestClassifierDoesNotLoseUnknownUnits(t *testing.T) { |
| 104 | s := Classify(Input{Version: 1, Units: []string{"tests"}, Log: strings.Repeat("x", 200)}, Classification{Version: 1, Complete: true}, "adapter") |
| 105 | if s.Complete || s.CopyOnly() || len(s.Failures) != 1 || s.Failures[0].Category != "other" { |
| 106 | t.Fatalf("unknown became copy: %+v", s) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | func TestCopyBookkeepingSkipsModelOnlyWithRecords(t *testing.T) { |
| 111 | d := Delegation{Owner: "paul", Revision: 1} |
| 112 | ex := []Literal{{Key: "save", Text: "Save"}} |
| 113 | if _, ok := ReviewedWords(ex, "head", d, nil); ok { |
| 114 | t.Fatal("marker alone skipped model") |
| 115 | } |
| 116 | r, ok := ReviewedWords(ex, "head", d, []Record{{Key: "save", Hash: Hash("Save"), Owner: "paul", Revision: 1, Candidate: "head", Flag: "Check wording"}}) |
| 117 | if !ok || len(r.Strings) != 1 || r.Strings[0].Text != "Save" || r.Strings[0].Flag != "Check wording" { |
| 118 | t.Fatal("matching record did not skip model") |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | func TestGenericScopeRejectsLiteralInjection(t *testing.T) { |
| 123 | for _, tc := range []struct { |
| 124 | before, text string |
| 125 | start, end int |
| 126 | }{{"show('Old words')", "New'); run(); ('", 6, 15}, {"show(`Old words`)", "New ${(()=>{run()})()}", 6, 15}} { |
| 127 | t.Run(tc.before, func(t *testing.T) { |
| 128 | before := []byte(tc.before) |
| 129 | after := append(append(append([]byte{}, before[:tc.start]...), []byte(tc.text)...), before[tc.end:]...) |
| 130 | ex := []Literal{{Key: "k", Text: "Old words", Start: tc.start, End: tc.end}} |
| 131 | review := Review{Version: 1, Strings: []Wording{{Key: "k", Text: tc.text, Action: "accept"}}} |
| 132 | if ValidateScope(before, after, ex, review) == nil { |
| 133 | t.Fatalf("literal injection accepted: %s", tc.text) |
| 134 | } |
| 135 | }) |
| 136 | } |
| 137 | } |
| 138 | |