-
Notifications
You must be signed in to change notification settings - Fork 2
Limit script name (50), description (200), and tags (5) to prevent SEO keyword stuffing #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
7
commits into
main
Choose a base branch
from
copilot/limit-script-name-description-length
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+300
−8
Draft
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
80a285d
Initial plan
Copilot f5a42dd
Limit script name (50), description (200), and tags (5) per requirements
Copilot 4fb06cd
Extract validateScriptMeta helper to eliminate duplication
Copilot 4f55c9c
Add format validation for name/description, skip on unchanged update,…
Copilot db1ec35
Assert specific error codes in validateScriptMeta unit tests
Copilot ab2cbee
Strengthen name/desc validation: SEO separator check and single-sente…
Copilot 5e02e37
Clarify multiSentenceRe comment and add whitespace-separated Chinese …
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| package script_svc | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/cago-frame/cago/pkg/utils/httputils" | ||
| "github.com/scriptscat/scriptlist/internal/pkg/code" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // assertErrCode checks that err is a *httputils.Error with the expected code. | ||
| func assertErrCode(t *testing.T, err error, wantCode int) { | ||
| t.Helper() | ||
| require.Error(t, err) | ||
| var herr *httputils.Error | ||
| require.ErrorAs(t, err, &herr, "expected *httputils.Error") | ||
| assert.Equal(t, wantCode, herr.Code) | ||
| } | ||
|
|
||
| func TestValidateScriptMeta(t *testing.T) { | ||
| ctx := context.Background() | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| scriptName string | ||
| description string | ||
| tags []string | ||
| nameUnchanged bool | ||
| descUnchanged bool | ||
| wantErrCode int | ||
| }{ | ||
| { | ||
| name: "valid name and description", | ||
| scriptName: "My Script", | ||
| description: "A simple one-line description.", | ||
| tags: []string{"tag1", "tag2"}, | ||
| wantErrCode: 0, | ||
| }, | ||
| { | ||
| name: "name with newline", | ||
| scriptName: "My Script\nWith Newline", | ||
| description: "A simple description.", | ||
| tags: nil, | ||
| wantErrCode: code.ScriptNameInvalid, | ||
| }, | ||
| { | ||
| name: "name with carriage return", | ||
| scriptName: "My Script\rWith CR", | ||
| description: "A simple description.", | ||
| tags: nil, | ||
| wantErrCode: code.ScriptNameInvalid, | ||
| }, | ||
| { | ||
| name: "name exactly 51 runes", | ||
| scriptName: "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy", // 51 chars | ||
| description: "A simple description.", | ||
| tags: nil, | ||
| wantErrCode: code.ScriptNameTooLong, | ||
| }, | ||
| { | ||
| name: "name exactly 50 runes", | ||
| scriptName: "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx", // 50 chars | ||
| description: "A simple description.", | ||
| tags: nil, | ||
| wantErrCode: 0, | ||
| }, | ||
| { | ||
| name: "description with newline", | ||
| scriptName: "My Script", | ||
| description: "First line.\nSecond line.", | ||
| tags: nil, | ||
| wantErrCode: code.ScriptDescInvalid, | ||
| }, | ||
| { | ||
| name: "description with carriage return", | ||
| scriptName: "My Script", | ||
| description: "First line.\rSecond line.", | ||
| tags: nil, | ||
| wantErrCode: code.ScriptDescInvalid, | ||
| }, | ||
| { | ||
| name: "description too long (>200 runes)", | ||
| scriptName: "My Script", | ||
| description: "这是一段非常长的描述这是一段非常长的描述这是一段非常长的描述这是一段非常长的描述这是一段非常长的描述这是一段非常长的描述这是一段非常长的描述这是一段非常长的描述这是一段非常长的描述这是一段非常长的描述这是一段非常长的描述这是一段非常长的描述这是一段非常长的描述这是一段非常长的描述这是一段非常长的描述这是一段非常长的描述这是一段非常长的描述这是一段非常长的描述这是一段非常长的描述这是一段非常长的描述还要再多一点", | ||
| tags: nil, | ||
| wantErrCode: code.ScriptDescTooLong, | ||
| }, | ||
| { | ||
| name: "too many tags", | ||
| scriptName: "My Script", | ||
| description: "A simple description.", | ||
| tags: []string{"t1", "t2", "t3", "t4", "t5", "t6"}, | ||
| wantErrCode: code.ScriptTagsTooMany, | ||
| }, | ||
| { | ||
| name: "exactly 5 tags", | ||
| scriptName: "My Script", | ||
| description: "A simple description.", | ||
| tags: []string{"t1", "t2", "t3", "t4", "t5"}, | ||
| wantErrCode: 0, | ||
| }, | ||
| { | ||
| name: "skip name validation when name unchanged", | ||
| scriptName: "Old Name\nWith Newline", | ||
| description: "A simple description.", | ||
| tags: nil, | ||
| nameUnchanged: true, | ||
| wantErrCode: 0, | ||
| }, | ||
| { | ||
| name: "skip desc validation when desc unchanged", | ||
| scriptName: "My Script", | ||
| description: "Old description\nWith Newline", | ||
| tags: nil, | ||
| descUnchanged: true, | ||
| wantErrCode: 0, | ||
| }, | ||
| { | ||
| name: "still validate tags when name and desc unchanged", | ||
| scriptName: "My Script", | ||
| description: "A simple description.", | ||
| tags: []string{"t1", "t2", "t3", "t4", "t5", "t6"}, | ||
| nameUnchanged: true, | ||
| descUnchanged: true, | ||
| wantErrCode: code.ScriptTagsTooMany, | ||
| }, | ||
| { | ||
| name: "do not skip desc when only name unchanged", | ||
| scriptName: "My Script", | ||
| description: "New description\nWith Newline", | ||
| tags: nil, | ||
| nameUnchanged: true, | ||
| descUnchanged: false, | ||
| wantErrCode: code.ScriptDescInvalid, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| err := validateScriptMeta(ctx, tt.scriptName, tt.description, tt.tags, tt.nameUnchanged, tt.descUnchanged) | ||
| if tt.wantErrCode == 0 { | ||
| assert.NoError(t, err) | ||
| } else { | ||
| assertErrCode(t, err, tt.wantErrCode) | ||
| } | ||
| }) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
只是判断换行符太简单了,而且本来也就不允许换行,有没有库可以判断?需要支持i18n,描述也要如此。或者名字禁止标点符号之类,描述判断是不是一句话
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated in commit 5e02e37 with two compiled regex patterns (no extra dependencies needed, standard
regexppackage):nameInvalidRe— rejects SEO keyword-list separators in names: commas (,/,), pipes (|), semicolons (;/;), and newlines. Pattern:[\r\n,,|;;]multiSentenceRe— rejects descriptions with more than one sentence:[\r\n])[。!?]) followed by optional whitespace then any non-whitespace (covers both第一句。第二句and第一句。 第二句)[.!?]) followed by at least one space then a capital letter or CJK character (catchesFirst. Secondwithout false-positives onv1.0or URLs)Pattern:
[\r\n]|[。!?]\s*\S|[.!?]\s+[A-Z\x{4e00}-\x{9fa5}]26 unit test cases covering all paths, each asserting the specific
*httputils.Errorcode.