mobile-teaching.test.mjs 3.4 KB

123456789101112131415161718192021222324252627
  1. import {test} from 'node:test';import assert from 'node:assert/strict';import{readFileSync}from'node:fs';import{PGlite}from'@electric-sql/pglite';
  2. const sql=readFileSync(new URL('../sql/mobile-teaching.sql',import.meta.url),'utf8');
  3. test('教学事务:重复及并发请求只增一次,关联记录同事务,失败整体回滚',async()=>{
  4. const db=new PGlite();try{
  5. 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);
  6. 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)]);
  7. 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']};
  8. await Promise.all(Array.from({length:8},()=>run('request-0001',[op])));assert.equal((await db.query('SELECT xxcs FROM "PracticeRecord"')).rows[0].xxcs,1);
  9. await run('request-0002',[op]);assert.equal((await db.query('SELECT xxcs FROM "PracticeRecord"')).rows[0].xxcs,2);
  10. await assert.rejects(run('request-0001',[op],'changed'),/different content/);
  11. 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);
  12. 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'}}];
  13. 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);
  14. }finally{await db.close();}
  15. });
  16. test('完课确认:学生与老师依次确认,重复确认幂等,越权和取消预约拒绝',async()=>{
  17. const db=new PGlite();try{
  18. 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);
  19. const confirm=(status,actor)=>db.query('select xs_mobile_confirm_appointment($1,$2,$3,$4,false) result',['co','100',status,actor]);
  20. await assert.rejects(confirm(20,99),/无权/);await assert.rejects(confirm(30,20),/状态已变更/);
  21. assert.equal((await confirm(20,10)).rows[0].result.status,20);
  22. assert.equal((await confirm(20,10)).rows[0].result.unchanged,true);
  23. assert.equal((await confirm(30,20)).rows[0].result.status,30);
  24. assert.equal((await confirm(20,10)).rows[0].result.status,30);
  25. await db.exec('UPDATE "CommonModel" SET status=-2');await assert.rejects(confirm(30,20),/已取消/);
  26. }finally{await db.close();}
  27. });