|
@@ -0,0 +1,60 @@
|
|
|
|
|
+import {test,before,after} 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';
|
|
|
|
|
+const db=new PGlite(),read=n=>readFileSync(new URL(n,import.meta.url),'utf8');
|
|
|
|
|
+const q=async(s,p=[])=>(await db.query(s,p)).rows,one=async(s,p=[])=>(await q(s,p))[0];
|
|
|
|
|
+let ctx;
|
|
|
|
|
+class Row {
|
|
|
|
|
+ constructor(name,data={}){this.className=name;this.data={...data};this.id=data.objectId;}
|
|
|
|
|
+ get(k){return this.data[k];}set(k,v){this.data[k]=v;}
|
|
|
|
|
+ toJSON(){return JSON.parse(JSON.stringify({...this.data,...(this.id?{objectId:this.id}:{})}));}
|
|
|
|
|
+}
|
|
|
|
|
+before(async()=>{
|
|
|
|
|
+ await db.exec(`CREATE TABLE "PracticeRecord"("objectId" text primary key,"sourceKey" text UNIQUE,company text,"createdAt" timestamptz,"updatedAt" timestamptz,id numeric,yhid numeric,scid numeric,kcid text,xxcs numeric,jrscb text);
|
|
|
|
|
+ CREATE TABLE "CommonModel"(company text,"modelId" numeric,"generalId" numeric);
|
|
|
|
|
+ INSERT INTO "CommonModel" VALUES('co',52,101),('co',52,102),('co',52,103);
|
|
|
|
|
+ INSERT INTO "PracticeRecord" VALUES('old','legacy:old','co','2025-01-01','2025-01-01',1001,20,101,'80',1,'0'),('new','legacy:new','co','2026-01-01','2026-01-01',1002,20,101,'80',2,'1'),('normal','legacy:normal','co','2026-01-01','2026-01-01',1003,20,102,'80',1,'0');`);
|
|
|
|
|
+ await db.exec(read('../sql/mobile-teaching.sql'));
|
|
|
|
|
+ const app=read('../deploy-admin-functions.mjs').match(/const appGatewayCode = String.raw`([\s\S]*?)\n`;/)[1].replace("const DEFAULT_COMPANY_ID = '7pIbDBJmKx'","const DEFAULT_COMPANY_ID = 'co'");
|
|
|
|
|
+ ctx=vm.createContext({Psql:{query:q,one},Parse:{Object:Row,Query:class{constructor(name){this.name=name;}async get(id){return new Row(this.name,await one('SELECT * FROM "PracticeRecord" WHERE "objectId"=$1',[id]));}}}});
|
|
|
|
|
+ vm.runInContext(app+read('../cloud/mobile-teaching.js'),ctx);ctx.authorizeRequestedUser=async()=>20;
|
|
|
|
|
+});
|
|
|
|
|
+after(()=>db.close());
|
|
|
|
|
+const teacher={id:'teacher',get:k=>({legacyUserId:30,legacyGroupId:3,company:{id:'co'}}[k])};
|
|
|
|
|
+const save=(key,ids='101,102',extra={})=>ctx.updateLearningWords({action:'e_add_words',uid:20,wordsId:ids,kcid:'80',save:1,requestKey:key,...extra},teacher);
|
|
|
|
|
+test('重复历史记录的整批保存保留各自身份与生词标记,推进最高学习次数,重试不重复增加',async()=>{
|
|
|
|
|
+ await db.exec('BEGIN');try{
|
|
|
|
|
+ const result=await save('duplicate-batch-request');assert.equal(result.affected,2);
|
|
|
|
|
+ const rows=await q('SELECT "objectId","sourceKey",id,xxcs,jrscb FROM "PracticeRecord" ORDER BY "objectId"');
|
|
|
|
|
+ assert.deepEqual(rows.map(r=>[r.objectId,r.sourceKey,Number(r.id),Number(r.xxcs),r.jrscb]),[['new','legacy:new',1002,3,'1'],['normal','legacy:normal',1003,2,'0'],['old','legacy:old',1001,1,'0']]);
|
|
|
|
|
+ await save('duplicate-batch-request');assert.deepEqual(await q('SELECT "objectId","sourceKey",id,xxcs,jrscb FROM "PracticeRecord" ORDER BY "objectId"'),rows);
|
|
|
|
|
+ assert.equal((await one('SELECT count(*)::int n FROM "MobileTeachingCommand"')).n,1);
|
|
|
|
|
+ }finally{await db.exec('ROLLBACK');}
|
|
|
|
|
+});
|
|
|
|
|
+test('底层事务防止旧快照串改来源键,重复记录按最高次数稳定选择',async()=>{
|
|
|
|
|
+ await db.exec('BEGIN');try{
|
|
|
|
|
+ const op={table:'PracticeRecord',match:{company:'co',yhid:20,scid:101},data:{objectId:'old',company:'co',yhid:20,scid:101,sourceKey:'legacy:old',id:1001,xxcs:1},increment:['xxcs']};
|
|
|
|
|
+ await q('SELECT xs_mobile_teaching_write($1,$2,$3,$4,$5::jsonb)',['co','20','stale-practice-snapshot','same',JSON.stringify([op])]);
|
|
|
|
|
+ assert.equal((await one('SELECT xxcs FROM "PracticeRecord" WHERE "objectId"=\'new\'')).xxcs,'3');
|
|
|
|
|
+ assert.equal((await one('SELECT "sourceKey" FROM "PracticeRecord" WHERE "objectId"=\'new\'')).sourceKey,'legacy:new');
|
|
|
|
|
+ }finally{await db.exec('ROLLBACK');}
|
|
|
|
|
+});
|
|
|
|
|
+test('新词首次创建与只改生词标记保持兼容,不降低现有学习次数',async()=>{
|
|
|
|
|
+ await db.exec('BEGIN');try{
|
|
|
|
|
+ assert.equal((await save('create-new-practice','103')).created,1);
|
|
|
|
|
+ await save('mark-duplicate-new-word','101',{save:0,ifnew:1});
|
|
|
|
|
+ assert.equal((await one('SELECT xxcs FROM "PracticeRecord" WHERE "objectId"=\'new\'')).xxcs,'2');
|
|
|
|
|
+ assert.equal((await one('SELECT jrscb FROM "PracticeRecord" WHERE "objectId"=\'new\'')).jrscb,'1');
|
|
|
|
|
+ await assert.rejects(save('invalid-vocabulary','999'),/不存在/);
|
|
|
|
|
+ }finally{await db.exec('ROLLBACK');}
|
|
|
|
|
+});
|
|
|
|
|
+test('与线上相同的同时间、同次数重复记录按 objectId 稳定选择,保留两份历史',async()=>{
|
|
|
|
|
+ await db.exec('BEGIN');try{
|
|
|
|
|
+ await db.exec(`INSERT INTO "PracticeRecord" VALUES('a-tie','legacy:tie-a','co','2025-01-01','2025-01-01',1004,20,103,'80',1,'1'),('z-tie','legacy:tie-z','co','2025-01-01','2025-01-01',1005,20,103,'80',1,'0')`);
|
|
|
|
|
+ assert.equal((await save('equal-progress-duplicate','103')).affected,1);
|
|
|
|
|
+ const rows=await q('SELECT "objectId","sourceKey",xxcs,jrscb FROM "PracticeRecord" WHERE scid=103 ORDER BY "objectId"');
|
|
|
|
|
+ assert.deepEqual(rows.map(r=>[r.objectId,r.sourceKey,Number(r.xxcs),r.jrscb]),[['a-tie','legacy:tie-a',2,'1'],['z-tie','legacy:tie-z',1,'0']]);
|
|
|
|
|
+ }finally{await db.exec('ROLLBACK');}
|
|
|
|
|
+});
|