mobile-teaching.test.mjs 5.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import {test} from 'node:test';import assert from 'node:assert/strict';import{readFileSync}from'node:fs';import vm from'node:vm';import{PGlite}from'@electric-sql/pglite';
  2. const sql=readFileSync(new URL('../sql/mobile-teaching.sql',import.meta.url),'utf8');
  3. const gatewaySource=readFileSync(new URL('../deploy-admin-functions.mjs',import.meta.url),'utf8');
  4. test('教学事务:重复及并发请求只增一次,关联记录同事务,失败整体回滚',async()=>{
  5. const db=new PGlite();try{
  6. await db.exec(`CREATE TABLE "PracticeRecord"("objectId" text primary key,"createdAt" timestamptz,"updatedAt" timestamptz,company text,"yhid" int,"scid" int,"xxcs" int);CREATE TABLE "DailyStudyRecord"("objectId" text primary key,"createdAt" timestamptz,"updatedAt" timestamptz,company text,id bigint,"sourceKey" text);CREATE TABLE "CommonModel"("objectId" text primary key,"createdAt" timestamptz,"updatedAt" timestamptz,company text,"itemId" bigint,"generalId" bigint,"sourceKey" text);`);await db.exec(sql);
  7. const run=(key,ops,fingerprint='same')=>db.query('select xs_mobile_teaching_write($1,$2,$3,$4,$5::jsonb) result',['co','learner',key,fingerprint,JSON.stringify(ops)]);
  8. const op={table:'PracticeRecord',match:{company:'co',yhid:1,scid:2},data:{objectId:'word',createdAt:new Date(),company:'co',yhid:1,scid:2,xxcs:1},increment:['xxcs']};
  9. await Promise.all(Array.from({length:8},()=>run('request-0001',[op])));assert.equal((await db.query('SELECT xxcs FROM "PracticeRecord"')).rows[0].xxcs,1);
  10. await run('request-0002',[op]);assert.equal((await db.query('SELECT xxcs FROM "PracticeRecord"')).rows[0].xxcs,2);
  11. await assert.rejects(run('request-0001',[op],'changed'),/different content/);
  12. await assert.rejects(run('request-0003',[op,{table:'Wrong',match:{id:1},data:{}}]),/not allowed/);assert.equal((await db.query('SELECT xxcs FROM "PracticeRecord"')).rows[0].xxcs,2);
  13. const pair=[{table:'DailyStudyRecord',match:{company:'co',sourceKey:'study'},data:{objectId:'d',createdAt:new Date(),company:'co',id:77,sourceKey:'study'}},{table:'CommonModel',match:{company:'co',sourceKey:'study'},data:{objectId:'c',createdAt:new Date(),company:'co',itemId:999,generalId:88,sourceKey:'study'},reference:{itemId:'id'}}];
  14. await run('request-0004',pair);await run('request-0005',pair);assert.equal((await db.query('SELECT "itemId" FROM "CommonModel"')).rows[0].itemId,77);assert.equal((await db.query('SELECT COUNT(*) n FROM "DailyStudyRecord"')).rows[0].n,1);
  15. }finally{await db.close();}
  16. });
  17. test('完课确认:学生与老师依次确认,重复确认幂等,越权和取消预约拒绝',async()=>{
  18. const db=new PGlite();try{
  19. await db.exec(`CREATE TABLE "CourseAppointment"("objectId" text,company text,id bigint,szyh bigint,pl bigint,dszt text,"updatedAt" timestamptz);CREATE TABLE "CommonModel"(company text,"modelId" int,"itemId" bigint,"generalId" bigint,status int);INSERT INTO "CourseAppointment" VALUES('a','co',1,10,20,'11',now());INSERT INTO "CommonModel" VALUES('co',54,1,100,0);`);await db.exec(sql);
  20. const confirm=(status,actor)=>db.query('select xs_mobile_confirm_appointment($1,$2,$3,$4,false) result',['co','100',status,actor]);
  21. await assert.rejects(confirm(20,99),/无权/);await assert.rejects(confirm(30,20),/状态已变更/);
  22. assert.equal((await confirm(20,10)).rows[0].result.status,20);
  23. assert.equal((await confirm(20,10)).rows[0].result.unchanged,true);
  24. assert.equal((await confirm(30,20)).rows[0].result.status,30);
  25. assert.equal((await confirm(20,10)).rows[0].result.status,30);
  26. await db.exec('UPDATE "CommonModel" SET status=-2');await assert.rejects(confirm(30,20),/已取消/);
  27. }finally{await db.close();}
  28. });
  29. test('学习记录:仅关联已经提交答题记录的历史阅读',async()=>{
  30. const db=new PGlite();try{
  31. await db.exec('CREATE TABLE "SurveyItem"("objectId" text primary key,"createdAt" timestamptz,"updatedAt" timestamptz,"user" text,"type" text,"title" text,"createOptions" jsonb,"isDeleted" boolean);CREATE TABLE "SurveyLog"("objectId" text primary key,"createdAt" timestamptz,"updatedAt" timestamptz,"user" text,"surveyItem" text);');
  32. await db.query('INSERT INTO "SurveyItem" VALUES($1,now(),now(),$2,$3,$4,$5::jsonb,false),($6,now(),now(),$2,$3,$7,$8::jsonb,false)', ['article201','_User$learner','reading','已完成文章',JSON.stringify({params:{sourceStudyId:'201'}}),'article202','未完成文章',JSON.stringify({params:{sourceStudyId:'202'}})]);
  33. await db.query('INSERT INTO "SurveyLog" VALUES($1,now(),now(),$2,$3)', ['log201','_User$learner','SurveyItem$article201']);
  34. const source=gatewaySource.match(/async function appOverviewCompletedReadingMap[\s\S]*?\n}\nasync function appLearningTarget/)?.[0].replace(/\nasync function appLearningTarget[\s\S]*$/,'');assert.ok(source);
  35. const context={Psql:{query:async(statement,args)=>(await db.query(statement,args)).rows},legacyUserObject:async()=>({id:'learner'})};vm.runInNewContext(source+';this.readingMap=appOverviewCompletedReadingMap',context);
  36. const map=await context.readingMap([{GeneralID:201},{GeneralID:202}],100);assert.equal(map.get('201').readingArticleId,'article201');assert.equal(map.get('201').readingTitle,'已完成文章');assert.equal(map.has('202'),false);
  37. }finally{await db.close();}
  38. });