Parcourir la source

fix(reading): default setup choices to random without overwriting selection

彭峰 il y a 1 jour
Parent
commit
d0d986ae9a

+ 25 - 0
docs/qa/reading-default-random-2026-11-18.md

@@ -0,0 +1,25 @@
+# 阅读生成设置默认随机:本地完成
+
+用户要求截图中的“语篇类型”和“内容题材”默认均为“随机”,老师仍可自行选择具体项。
+
+## 原因及修改
+
+设置组件初值和下拉选项已有“随机”,但ngOnInit调用normalizeReadingParams时,缺失的type/theme被旧兜底“议论文/多彩生活”补齐,覆盖页面初值。原start还会将一次抽样结果回写表单,查重拦截或检查失败后再次点击不再随机。
+
+- 参数规范化函数增加可选type/theme默认值;仅设置页传“随机/随机”。练习页、PDF及历史参数的一参数调用继续保留原兜底。
+- 明确提供的普通query参数和旧打包参数仍优先,不覆盖老师已有选择。
+- 点击生成时,对仍为随机的字段分别抽样,查重与生成路由使用同一组具体类型/题材,不将“随机”字面值送入生成参数。
+- 表单保持老师的随机/手选状态,避免一次抽样将之后的选择锁死。随机允许重复,不宣称每次结果必然不同。
+- 既有年级、生词、来源学员、历史文章、三次生成限制、提示词和费用逻辑没有改变。
+
+## 验证
+
+- 新增设置页回归覆盖默认下拉、两次独立抽样、手选和随机混合、显式打包参数及查重拦截后的随机状态;旧代码4项失败/2项通过,修复后通过。
+- 新增规范化契约检查,验证设置专用默认值、旧调用兜底及query覆盖打包值的既有优先级。
+- 阅读模块与外部阅读相关60/60检查通过;移动端生产构建和git diff --check通过,独立审查无重要问题。
+- 本地浏览器验证老师390/1024px、学生390px:默认均显示随机,选项仍可手选;随机、双手选和单字段随机的查重参数均符合界面选择。老师参数保留当前学员studentId。
+- 浏览器使用模拟会话及接口,在查重阶段拦截生成,AI请求0、远端数据写入0、脚本错误0。没有以真实AI文章输出代替参数验证,也没有访问生产账号或数据。
+
+日志和截图位于`/Users/mac/.local/state/xiaoshu/qa/2026-11-18-reading-random/`。
+
+**状态:本地完成并提交推送,未上线。未发布网页、未打包APP、未改云函数或数据库。16px报表和老师首页专项训练仍为各自待确认批次。**

+ 19 - 0
projects/xiaoshu-mobile/src/app/features/english-reading/reading-setup.component.spec.ts

@@ -45,3 +45,22 @@ describe('ReadingSetupComponent', () => {
     }) });
   });
 });
+
+describe('阅读类型和题材的随机选择',()=>{
+  async function screen(extra:Record<string,unknown>={}) {
+    const router=jasmine.createSpyObj<Router>('Router',['navigate','navigateByUrl']);router.navigate.and.resolveTo(true);
+    const cloud=jasmine.createSpyObj<ReadingCloudService>('ReadingCloudService',['ensureUser','updateUser'],{user:null});cloud.ensureUser.and.resolveTo({objectId:'learner',sessionToken:'r:local'});
+    const reading=jasmine.createSpyObj<EnglishReadingService>('EnglishReadingService',['findArticles','duplicateExists','countArticles']);reading.findArticles.and.resolveTo([]);reading.duplicateExists.and.resolveTo(false);reading.countArticles.and.resolveTo(0);
+    await TestBed.configureTestingModule({imports:[ReadingSetupComponent],providers:[
+      {provide:ActivatedRoute,useValue:{snapshot:{queryParamMap:convertToParamMap({studentId:'100',GeneralID:'201',wordList:'apple,river',level:'初二',...extra})}}},
+      {provide:Router,useValue:router},{provide:SessionService,useValue:{groupId:()=>3,user:()=>({userId:300,groupId:3}),student:()=>({userId:100,groupId:1,honeyName:'测试学员'})}},
+      {provide:PlatformService,useValue:jasmine.createSpyObj<PlatformService>('PlatformService',['toast'])},
+    ]}).overrideComponent(ReadingSetupComponent,{set:{providers:[{provide:ReadingCloudService,useValue:cloud},{provide:EnglishReadingService,useValue:reading}]}}).compileComponents();
+    const fixture=TestBed.createComponent(ReadingSetupComponent);fixture.detectChanges();await fixture.whenStable();fixture.detectChanges();return{fixture,c:fixture.componentInstance,router,reading};
+  }
+  it('新进入老师设置页时两个下拉框默认显示随机并保留可选项',async()=>{const{fixture,c}=await screen();expect(c.type).toBe('随机');expect(c.theme).toBe('随机');for(const name of ['type','theme']){const select=fixture.nativeElement.querySelector(`select[name=${name}]`) as HTMLSelectElement;expect(select.value).toBe('随机');expect(select.options.length).toBeGreaterThan(1);}});
+  it('每次生成独立抽取具体类型和题材,但表单保留随机模式',async()=>{const{c,router}=await screen();spyOn(Math,'random').and.returnValues(0,0,0.999,0.999);await c.start();expect(router.navigate.calls.mostRecent().args[1]?.queryParams).toEqual(jasmine.objectContaining({type:'记叙文',theme:'科技创新',studentId:100,sourceStudyId:'201'}));expect(c.type).toBe('随机');expect(c.theme).toBe('随机');await c.start();expect(router.navigate.calls.mostRecent().args[1]?.queryParams).toEqual(jasmine.objectContaining({type:'新闻报道',theme:'古代科技'}));expect(c.type).toBe('随机');expect(c.theme).toBe('随机');});
+  it('老师手动选择时只随机仍为随机的字段',async()=>{const{fixture,c,router}=await screen();const select=fixture.nativeElement.querySelector('select[name=type]') as HTMLSelectElement;select.value='说明文';select.dispatchEvent(new Event('change'));await fixture.whenStable();fixture.detectChanges();c.theme='随机';const random=spyOn(Math,'random').and.returnValue(0.999);await c.start();expect(random).toHaveBeenCalledTimes(1);expect(router.navigate.calls.mostRecent().args[1]?.queryParams).toEqual(jasmine.objectContaining({type:'说明文',theme:'古代科技'}));expect(c.type).toBe('说明文');expect(c.theme).toBe('随机');});
+  it('保留路由或旧打包参数明确指定的类型和题材',async()=>{const{c,router}=await screen({params:encodeURIComponent(JSON.stringify({type:'新闻报道',theme:'历史发展'}))});const random=spyOn(Math,'random');await c.start();expect(random).not.toHaveBeenCalled();expect(router.navigate.calls.mostRecent().args[1]?.queryParams).toEqual(jasmine.objectContaining({type:'新闻报道',theme:'历史发展'}));});
+  it('查重拦截后不把随机模式改成一次抽取结果',async()=>{const{c,router,reading}=await screen({type:'随机',theme:'随机'});reading.duplicateExists.and.resolveTo(true);spyOn(Math,'random').and.returnValues(0,0);await c.start();expect(router.navigate).not.toHaveBeenCalled();expect(c.type).toBe('随机');expect(c.theme).toBe('随机');});
+});

+ 1 - 3
projects/xiaoshu-mobile/src/app/features/english-reading/reading-setup.component.ts

@@ -70,7 +70,7 @@ export class ReadingSetupComponent implements OnInit, OnDestroy {
       const values = this.route.snapshot.queryParamMap.getAll(key);
       return [key, values.length > 1 ? values : values[0]];
     }));
-    const params = normalizeReadingParams(raw);
+    const params = normalizeReadingParams(raw, { type: '随机', theme: '随机' });
     this.routeParams = params;
     const appUser = this.readingLearner();
     const ncloudUser = this.cloud.user;
@@ -143,8 +143,6 @@ export class ReadingSetupComponent implements OnInit, OnDestroy {
       await this.ensureUser();
       const selectedType = this.type === '随机' ? this.random(this.types) : this.type;
       const selectedTheme = this.theme === '随机' ? this.random(this.themes) : this.theme;
-      this.type = selectedType;
-      this.theme = selectedTheme;
       const params: ReadingRouteParams = {
         studentId:this.readingLearner()?.userId,
         sourceStudyId:this.routeParams.sourceStudyId,

+ 8 - 0
projects/xiaoshu-mobile/src/app/features/english-reading/reading.utils.spec.ts

@@ -2,6 +2,14 @@ import { ReadingArticle, ReadingQuestion } from './reading.models';
 import { highlightWords, normalizeReadingParams, normalizeReadingQuestions, normalizeReadingReturnUrl, readingStage, readingStudentName, readingWordLimit, splitWords } from './reading.utils';
 
 describe('reading utilities', () => {
+  it('allows setup-specific defaults without changing exercise/PDF fallbacks or explicit packed choices', () => {
+    const defaults = { type: '随机', theme: '随机' };
+    expect(normalizeReadingParams({}, defaults)).toEqual(jasmine.objectContaining(defaults));
+    expect(normalizeReadingParams({})).toEqual(jasmine.objectContaining({ type: '议论文', theme: '多彩生活' }));
+    const packed = encodeURIComponent(JSON.stringify({ type: '新闻报道', theme: '历史发展' }));
+    expect(normalizeReadingParams({ params: packed }, defaults)).toEqual(jasmine.objectContaining({ type: '新闻报道', theme: '历史发展' }));
+    expect(normalizeReadingParams({ params: packed, type: '说明文' }, defaults)).toEqual(jasmine.objectContaining({ type: '说明文', theme: '历史发展' }));
+  });
   it('normalizes packed UniApp params and common word separators', () => {
     const packed = encodeURIComponent(JSON.stringify({ wordList: ['apple', ' book '], token: 'session', returnUrl:'/pages/his/his?date=2026-09-10' }));
     const params = normalizeReadingParams({ params: packed });

+ 3 - 3
projects/xiaoshu-mobile/src/app/features/english-reading/reading.utils.ts

@@ -29,7 +29,7 @@ export function splitWords(value: unknown): string[] {
   return source.map((word) => String(word).trim()).filter(Boolean);
 }
 
-export function normalizeReadingParams(value: Record<string, unknown>): ReadingRouteParams {
+export function normalizeReadingParams(value: Record<string, unknown>, defaults: Pick<ReadingRouteParams, 'type' | 'theme'> = { type: '议论文', theme: '多彩生活' }): ReadingRouteParams {
   const nested = parsePackedParams(value['params']);
   const params = { ...nested, ...value };
   delete params['params'];
@@ -41,8 +41,8 @@ export function normalizeReadingParams(value: Record<string, unknown>): ReadingR
     phone: String(params['phone'] ?? params['mobile'] ?? ''),
     level: String(params['level'] ?? '九年级'),
     wordList: routeWords(params['wordList']),
-    type: String(params['type'] ?? '议论文'),
-    theme: String(params['theme'] ?? '多彩生活'),
+    type: String(params['type'] ?? defaults.type),
+    theme: String(params['theme'] ?? defaults.theme),
     token: typeof params['token'] === 'string' ? params['token'] : '',
     t: Number(params['t']) || undefined,
     renew: params['renew'] === true || params['renew'] === 'true',