|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/url" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/google/go-github/v89/github" |
| 8 | + "github.com/shurcooL/githubv4" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +// bodyWithHiddenPayload embeds Unicode tag characters, which are invisible to a |
| 14 | +// human reviewer but legible to a model. |
| 15 | +const bodyWithHiddenPayload = "Looks good\U000E0001\U000E0049\U000E0067\U000E006E\U000E006F\U000E0072\U000E0065" |
| 16 | + |
| 17 | +const bodyWithCode = "Compare with:\n```go\nif a<b { fmt.Println(\"x\") }\n```\nand <Foo/> in JSX." |
| 18 | + |
| 19 | +func TestConvertToMinimalIssueCommentSanitizesBody(t *testing.T) { |
| 20 | + t.Run("strips hidden characters", func(t *testing.T) { |
| 21 | + m := convertToMinimalIssueComment(&github.IssueComment{ |
| 22 | + ID: github.Ptr(int64(1)), |
| 23 | + Body: github.Ptr(bodyWithHiddenPayload), |
| 24 | + }) |
| 25 | + assert.Equal(t, "Looks good", m.Body) |
| 26 | + }) |
| 27 | + |
| 28 | + t.Run("preserves code content", func(t *testing.T) { |
| 29 | + m := convertToMinimalIssueComment(&github.IssueComment{ |
| 30 | + ID: github.Ptr(int64(1)), |
| 31 | + Body: github.Ptr(bodyWithCode), |
| 32 | + }) |
| 33 | + assert.Equal(t, bodyWithCode, m.Body) |
| 34 | + }) |
| 35 | +} |
| 36 | + |
| 37 | +func TestConvertToMinimalPullRequestReviewSanitizesBody(t *testing.T) { |
| 38 | + t.Run("strips hidden characters", func(t *testing.T) { |
| 39 | + m := convertToMinimalPullRequestReview(&github.PullRequestReview{ |
| 40 | + ID: github.Ptr(int64(1)), |
| 41 | + Body: github.Ptr(bodyWithHiddenPayload), |
| 42 | + }) |
| 43 | + assert.Equal(t, "Looks good", m.Body) |
| 44 | + }) |
| 45 | + |
| 46 | + t.Run("preserves code content", func(t *testing.T) { |
| 47 | + m := convertToMinimalPullRequestReview(&github.PullRequestReview{ |
| 48 | + ID: github.Ptr(int64(1)), |
| 49 | + Body: github.Ptr(bodyWithCode), |
| 50 | + }) |
| 51 | + assert.Equal(t, bodyWithCode, m.Body) |
| 52 | + }) |
| 53 | +} |
| 54 | + |
| 55 | +func TestConvertToMinimalReviewCommentSanitizesBody(t *testing.T) { |
| 56 | + commentURL, err := url.Parse("https://github.com/owner/repo/pull/1#discussion_r1") |
| 57 | + require.NoError(t, err) |
| 58 | + |
| 59 | + t.Run("strips hidden characters", func(t *testing.T) { |
| 60 | + m := convertToMinimalReviewComment(reviewCommentNode{ |
| 61 | + Body: githubv4.String(bodyWithHiddenPayload), |
| 62 | + Path: githubv4.String("main.go"), |
| 63 | + URL: githubv4.URI{URL: commentURL}, |
| 64 | + }) |
| 65 | + assert.Equal(t, "Looks good", m.Body) |
| 66 | + }) |
| 67 | + |
| 68 | + t.Run("preserves code content", func(t *testing.T) { |
| 69 | + m := convertToMinimalReviewComment(reviewCommentNode{ |
| 70 | + Body: githubv4.String(bodyWithCode), |
| 71 | + Path: githubv4.String("main.go"), |
| 72 | + URL: githubv4.URI{URL: commentURL}, |
| 73 | + }) |
| 74 | + assert.Equal(t, bodyWithCode, m.Body) |
| 75 | + }) |
| 76 | +} |
0 commit comments