package app

import (
	"testing"

	"github.com/stretchr/testify/require"
)

func TestModerationKeywordMatchesASCIIKeywordsOnTokenBoundaries(t *testing.T) {
	t.Parallel()

	tests := []struct {
		name    string
		content string
		keyword string
		want    bool
	}{
		{
			name:    "does not match inside json",
			content: "保留原来的json格式文件",
			keyword: "JS",
			want:    false,
		},
		{
			name:    "matches standalone js",
			content: "请检查 JS 脚本",
			keyword: "JS",
			want:    true,
		},
		{
			name:    "does not match inside lsm",
			content: "TiKV shares one LSM tree",
			keyword: "SM",
			want:    false,
		},
		{
			name:    "does not match inside analysis",
			content: "analysis report",
			keyword: "anal",
			want:    false,
		},
		{
			name:    "matches ascii keyword before cjk",
			content: "JS代码",
			keyword: "js",
			want:    true,
		},
		{
			name:    "does not match ascii keyword inside word before cjk",
			content: "nodejs代码",
			keyword: "js",
			want:    false,
		},
		{
			name:    "does not match inside underscore token",
			content: "check foo_js_bar",
			keyword: "js",
			want:    false,
		},
		{
			name:    "matches between punctuation",
			content: "check (js).",
			keyword: "JS",
			want:    true,
		},
		{
			name:    "does not match digit letter keyword inside larger token",
			content: "test123pattern",
			keyword: "3P",
			want:    false,
		},
		{
			name:    "matches digit letter keyword as token",
			content: "blocked 3P content",
			keyword: "3P",
			want:    true,
		},
	}

	for _, tt := range tests {
		tt := tt
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()

			require.Equal(t, tt.want, moderationKeywordMatches(tt.content, tt.keyword))
		})
	}
}

func TestModerationKeywordMatchesNonASCIIKeywordsBySubstring(t *testing.T) {
	t.Parallel()

	require.True(t, moderationKeywordMatches("这段内容涉及赌博风险", "赌博"))
	require.True(t, moderationKeywordMatches("背景色区分", "色区"))
	require.False(t, moderationKeywordMatches("正常内容", "赌博"))
}
