legacy-writing-review.test.mjs 6.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import {test} from 'node:test';
  2. import assert from 'node:assert/strict';
  3. import {readFileSync} from 'node:fs';
  4. import vm from 'node:vm';
  5. import {PGlite} from '@electric-sql/pglite';
  6. const mobile=readFileSync(new URL('../cloud/mobile-teaching.js',import.meta.url),'utf8');
  7. const gateway=readFileSync(new URL('../deploy-admin-functions.mjs',import.meta.url),'utf8').match(/const appGatewayCode = String.raw`([\s\S]*?)\n`;/)[1].replace("const DEFAULT_COMPANY_ID = '7pIbDBJmKx';","const DEFAULT_COMPANY_ID = 'co';");
  8. async function fixture(run){
  9. const db=new PGlite();try{
  10. await db.exec(readFileSync(new URL('./fixtures/teacher-review-schema.sql',import.meta.url),'utf8'));
  11. await db.exec(`ALTER TABLE "DailyStudyRecord" ADD COLUMN pl text,ADD COLUMN "canonicalStudyId" text;
  12. ALTER TABLE "CourseAppointment" ADD COLUMN pl text,ADD COLUMN kssj text,ADD COLUMN jssj text;
  13. ALTER TABLE "LessonRecord" ADD COLUMN jsmz text;
  14. INSERT INTO "CommonModel"(company,"modelId","generalId","itemId",status,"sourceKey") VALUES('co',56,100,101,99,'legacy:model:56:general:100'),('co',54,200,201,99,'legacy:model:54:general:200');
  15. INSERT INTO "DailyStudyRecord"(company,id,learned,xxqs,dsid,"userId",pl) VALUES('co',101,0,'','200',20,'30');
  16. INSERT INTO "CourseAppointment"(company,id,szyh,dszt,"courseCategoryKey",kcid,"sourceKey",pl)
  17. VALUES('co',201,'20','30',NULL,'398','legacy:model:54:general:200','30');
  18. INSERT INTO "Node" VALUES('co',398,'阅读写作');`);
  19. const query=async(s,a)=>(await db.query(s,a)).rows;
  20. const ctx=vm.createContext({Date,Promise,Parse:{},Psql:{query,one:async(s,a)=>(await query(s,a))[0],oneOrNone:async(s,a)=>(await query(s,a))[0]||null}});
  21. vm.runInContext(gateway+'\n'+mobile,ctx);
  22. ctx.authorizeRequestedUser=async()=>30;ctx.appOverviewToday=()=> '2026-10-14';ctx.appLiveVersionSnapshot=async()=>({});ctx.legacyStudyRecordWords=async()=>[];ctx.appLearningTarget=async()=>{throw Error('forbidden');};
  23. const coach={get:k=>({legacyUserId:30,legacyGroupId:3}[k])};
  24. await run({db,ctx,coach});
  25. }finally{await db.close();}
  26. }
  27. test('旧阅读写作398无学段分类时正常列出任务、统计和学员,并能直接进入笔记复习',()=>fixture(async({db,ctx,coach})=>{
  28. const page=await ctx.appReviewPage({scope:'today',pageSize:1,includeIncome:false},coach);
  29. assert.equal(page.page.itemCount,2);assert.equal(page.items.length,1);assert.equal(page.summary.todayTotal,2);assert.equal(page.summary.todayPending,1);assert.equal(page.students[0].id,'20');
  30. const next=await ctx.appReviewPage({scope:'today',page:2,pageSize:1,includeIncome:false},coach);assert.equal(next.items.length,1);
  31. const detail=await ctx.recordDetailData({id:100},coach);assert.equal(detail.words.length,0);assert.equal(detail.addon[0].reviewContentMode,'notes');assert.equal(detail.record.__notesReviewSource,undefined);
  32. assert.equal((await db.query('SELECT "courseCategoryKey" FROM "CourseAppointment"')).rows[0].courseCategoryKey,null);
  33. await assert.rejects(ctx.recordDetailData({id:100},{get:k=>({legacyUserId:99,legacyGroupId:3}[k])}),/forbidden/);
  34. }));
  35. test('普通空词表、错误学生、未完课、回收和无可信来源的398不得冒充旧写作复习',()=>fixture(async({db,ctx,coach})=>{
  36. const reset=`UPDATE "CourseAppointment" SET szyh='20',dszt='30',"courseCategoryKey"=NULL,kcid='398',"sourceKey"='legacy:model:54:general:200';UPDATE "CommonModel" SET status=99,"sourceKey"='legacy:model:54:general:200' WHERE "modelId"=54;`;
  37. for(const patch of [
  38. `UPDATE "CourseAppointment" SET "courseCategoryKey"='word'`,
  39. `UPDATE "CourseAppointment" SET kcid='399'`,
  40. `UPDATE "CourseAppointment" SET szyh='21'`,
  41. `UPDATE "CourseAppointment" SET dszt='10'`,
  42. `UPDATE "CourseAppointment" SET dszt='20'`,
  43. `UPDATE "CourseAppointment" SET "sourceKey"='cloud:appointment:new'`,
  44. `UPDATE "CommonModel" SET "sourceKey"='cloud:appointment:new' WHERE "modelId"=54`,
  45. `UPDATE "CommonModel" SET status=-2 WHERE "modelId"=54`,
  46. ]){
  47. await db.exec(reset+patch);const page=await ctx.appReviewPage({scope:'today',includeIncome:false},coach);assert.equal(page.items.length,0,patch);assert.equal(page.summary.todayTotal,0,patch);assert.equal(page.students.length,0,patch);
  48. const detail=await ctx.recordDetailData({id:100},coach);assert.equal(detail.addon[0].reviewContentMode,undefined,patch);
  49. }
  50. }));
  51. test('旧预约状态滞后但已有同一学生同一课次的真实旧完课凭据时恢复笔记复习',()=>fixture(async({db,ctx,coach})=>{
  52. await db.exec(`UPDATE "CourseAppointment" SET dszt='0',"courseCategoryKey"='high_writing';
  53. INSERT INTO "CommonModel"(company,"modelId","generalId","itemId",status,"sourceKey") VALUES('co',59,300,301,99,'legacy:model:59:general:300');
  54. INSERT INTO "LessonRecord"(company,id,yyds,xymz,kcid,"sourceKey",jsmz) VALUES('co',301,'200','20','398','legacy:model:59:general:300','30');`);
  55. const page=()=>ctx.appReviewPage({scope:'today',includeIncome:false},coach);
  56. assert.equal((await page()).page.itemCount,2);
  57. assert.equal((await ctx.recordDetailData({id:100},coach)).addon[0].reviewContentMode,'notes');
  58. assert.equal((await db.query('SELECT dszt FROM "CourseAppointment"')).rows[0].dszt,'0');
  59. for(const patch of [
  60. `UPDATE "LessonRecord" SET xymz='21'`,
  61. `UPDATE "LessonRecord" SET yyds='999'`,
  62. `UPDATE "LessonRecord" SET kcid='203'`,
  63. `UPDATE "LessonRecord" SET "sourceKey"='cloud:complete:new'`,
  64. `UPDATE "CommonModel" SET status=-2 WHERE "modelId"=59`,
  65. ]){
  66. await db.exec(`UPDATE "LessonRecord" SET xymz='20',yyds='200',kcid='398',"sourceKey"='legacy:model:59:general:300';UPDATE "CommonModel" SET status=99 WHERE "modelId"=59;`+patch);
  67. assert.equal((await page()).page.itemCount,0,patch);
  68. assert.equal((await ctx.recordDetailData({id:100},coach)).addon[0].reviewContentMode,undefined,patch);
  69. }
  70. }));
  71. test('旧系统已结束、待课后确认的阅读写作任务可显示并打开笔记',()=>fixture(async({db,ctx,coach})=>{
  72. await db.exec(`UPDATE "CourseAppointment" SET dszt='11',kssj='2026-10-13 10:43',jssj='2026-10-13 11:40';
  73. INSERT INTO "CommonModel"(company,"modelId","generalId","itemId",status,"sourceKey") VALUES('co',59,300,301,99,'legacy:model:59:general:300');
  74. INSERT INTO "LessonRecord"(company,id,yyds,xymz,kcid,"sourceKey",jsmz) VALUES('co',301,'200','20','398','legacy:model:59:general:300','30');`);
  75. const page=()=>ctx.appReviewPage({scope:'today',includeIncome:false},coach);
  76. const detail=()=>ctx.recordDetailData({id:100},coach);
  77. for(const status of ['11','20']){
  78. await db.query('UPDATE "CourseAppointment" SET dszt=$1',[status]);
  79. assert.equal((await page()).page.itemCount,2);
  80. assert.equal((await detail()).addon[0].reviewContentMode,'notes');
  81. }
  82. await db.exec(`UPDATE "LessonRecord" SET jsmz='31'`);
  83. assert.equal((await page()).page.itemCount,0);
  84. await db.exec(`UPDATE "LessonRecord" SET jsmz='30';UPDATE "CourseAppointment" SET jssj=''`);
  85. assert.equal((await page()).page.itemCount,0);
  86. }));