MCPcopy Create free account
hub / github.com/alibaba/open-code-review / evalSessionResponseTextJS

Function evalSessionResponseTextJS

internal/viewer/server_extra_test.go:1134–1246  ·  view source on GitHub ↗

evalSessionResponseTextJS parses the .response-text formatting block from session.js and executes its exact regex/string replacement pipeline in Go so Linux CI (which runs in a Go-only container without Node.js) still exercises the behavioral regression suite instead of skipping it.

(t *testing.T, script string, inputs []string)

Source from the content-addressed store, hash-verified

1132// Linux CI (which runs in a Go-only container without Node.js) still exercises
1133// the behavioral regression suite instead of skipping it.
1134func evalSessionResponseTextJS(t *testing.T, script string, inputs []string) []string {
1135 t.Helper()
1136
1137 blockRE := regexp.MustCompile(`(?s)document\.querySelectorAll\('\.response-text'\)\.forEach\(function\(el\) \{(.*?)\n\}\);`)
1138 blockMatch := blockRE.FindStringSubmatch(script)
1139 if blockMatch == nil {
1140 t.Fatal("session.js is missing the .response-text formatter block")
1141 }
1142 block := blockMatch[1]
1143
1144 escBlockRE := regexp.MustCompile(`(?s)const esc = function\(s\) \{(.*?)\};`)
1145 escMatch := escBlockRE.FindStringSubmatch(block)
1146 if escMatch == nil {
1147 t.Fatal("session.js is missing the esc() helper")
1148 }
1149 replacePairRE := regexp.MustCompile(`\.replace\(/((?:\\/|[^/])+)/([gm]*),\s*'([^']*)'\)`)
1150 compileJSRegex := func(pattern, flags string) *regexp.Regexp {
1151 t.Helper()
1152 if strings.Contains(flags, "m") {
1153 pattern = "(?m)" + pattern
1154 }
1155 re, err := regexp.Compile(pattern)
1156 if err != nil {
1157 t.Fatalf("compile JS regex /%s/%s: %v", pattern, flags, err)
1158 }
1159 return re
1160 }
1161
1162 type regexStep struct {
1163 re *regexp.Regexp
1164 rep string
1165 }
1166 var escSteps []regexStep
1167 for _, m := range replacePairRE.FindAllStringSubmatch(escMatch[1], -1) {
1168 escSteps = append(escSteps, regexStep{re: compileJSRegex(m[1], m[2]), rep: m[3]})
1169 }
1170 if len(escSteps) != 5 {
1171 t.Fatalf("esc() replacement step count = %d, want 5", len(escSteps))
1172 }
1173
1174 mdRegionStart := strings.Index(block, "return '%%CODEBLOCK_' + (codeBlocks.length - 1) + '%%';")
1175 mdRegionEnd := strings.Index(block, "codeBlocks.forEach")
1176 if mdRegionStart == -1 || mdRegionEnd == -1 || mdRegionEnd <= mdRegionStart {
1177 t.Fatal("session.js is missing the markdown or codeBlocks restoration region")
1178 }
1179 var mdSteps []regexStep
1180 for _, m := range replacePairRE.FindAllStringSubmatch(block[mdRegionStart:mdRegionEnd], -1) {
1181 mdSteps = append(mdSteps, regexStep{re: compileJSRegex(m[1], m[2]), rep: m[3]})
1182 }
1183 if len(mdSteps) != 8 {
1184 t.Fatalf("markdown replacement step count = %d, want 8", len(mdSteps))
1185 }
1186
1187 restoreRE := regexp.MustCompile(`(?s)codeBlocks\.forEach\(function\(code,\s*i\)\s*\{\s*html\s*=\s*html\.replace\('%%CODEBLOCK_'\s*\+\s*i\s*\+\s*'%%',(.*?)\);\s*\}\);`)
1188 restoreMatch := restoreRE.FindStringSubmatch(block[mdRegionEnd:])
1189 if restoreMatch == nil {
1190 t.Fatal("session.js is missing the codeBlocks.forEach html.replace call")
1191 }

Calls 3

jsRegexReplaceAllFunction · 0.85
jsGetSubstitutionFunction · 0.85
StringMethod · 0.45

Tested by

no test coverage detected