(
rp: RemotePage,
opts: CommonWorkOptions,
config: {
type: QuestionTypes;
root: string;
elements: WorkOptions<any>['elements'];
onUpload?: (uploadable: boolean) => any;
questionTitleTransform?: (ques: string, result_transform?: boolean) => string;
test_type: 'reading-choice' | 'reading-replay' | 'choice' | 'complete';
}
)
| 1041 | } |
| 1042 | |
| 1043 | async function handleCommonUnitTest( |
| 1044 | rp: RemotePage, |
| 1045 | opts: CommonWorkOptions, |
| 1046 | config: { |
| 1047 | type: QuestionTypes; |
| 1048 | root: string; |
| 1049 | elements: WorkOptions<any>['elements']; |
| 1050 | onUpload?: (uploadable: boolean) => any; |
| 1051 | questionTitleTransform?: (ques: string, result_transform?: boolean) => string; |
| 1052 | test_type: 'reading-choice' | 'reading-replay' | 'choice' | 'complete'; |
| 1053 | } |
| 1054 | ) { |
| 1055 | if (opts.answererWrappers === undefined || opts.answererWrappers.length === 0) { |
| 1056 | return await answerWrapperEmptyWarning(0); |
| 1057 | } |
| 1058 | const visual_state = CommonProject.scripts.render.cfg.visual; |
| 1059 | // 最大化面板 |
| 1060 | CommonProject.scripts.render.methods.normal(); |
| 1061 | CommonProject.scripts.workResults.methods.init(); |
| 1062 | // 固定显示答题结果面板 |
| 1063 | CommonProject.scripts.render.methods.pin(CommonProject.scripts.workResults); |
| 1064 | |
| 1065 | const titleTransform = (titles: (HTMLElement | undefined)[]) => { |
| 1066 | const res = titles.map((t) => (t ? t.innerText : '')).join('\n\n'); |
| 1067 | return config.questionTitleTransform ? config.questionTitleTransform(res) : res; |
| 1068 | }; |
| 1069 | |
| 1070 | /** 新建答题器 */ |
| 1071 | const worker = new OCSWorker({ |
| 1072 | root: config.root, |
| 1073 | elements: { |
| 1074 | title: config.elements.title, |
| 1075 | options: config.elements.options |
| 1076 | }, |
| 1077 | thread: opts.thread ?? 1, |
| 1078 | answerSeparators: opts.answerSeparators.split(',').map((s) => s.trim()), |
| 1079 | /** 默认搜题方法构造器 */ |
| 1080 | answerer: async (elements, ctx) => { |
| 1081 | const title = titleTransform(elements.title); |
| 1082 | console.log('title', title); |
| 1083 | if (title.trim()) { |
| 1084 | return CommonProject.scripts.apps.methods.searchAnswerInCaches(title.trim(), async () => { |
| 1085 | await $.sleep((opts.period ?? 3) * 1000); |
| 1086 | return defaultAnswerWrapperHandler(opts.answererWrappers, { |
| 1087 | type: ctx.type, |
| 1088 | title, |
| 1089 | options: ctx.type === 'completion' ? '' : ctx.elements.options.map((o) => o.innerText).join('\n') |
| 1090 | }); |
| 1091 | }); |
| 1092 | } else { |
| 1093 | throw new Error('题目为空,请查看题目是否为空,或者忽略此题'); |
| 1094 | } |
| 1095 | }, |
| 1096 | |
| 1097 | work: { |
| 1098 | type(ctx) { |
| 1099 | return config.type; |
| 1100 | }, |
no test coverage detected