lesson-study-meaning.test.mjs 3.0 KB

12345678910111213141516171819202122
  1. import {test} from 'node:test';import assert from 'node:assert/strict';import {readFileSync} from 'node:fs';import vm from 'node:vm';
  2. const source=readFileSync(new URL('../cloud/lesson-study.js',import.meta.url),'utf8');
  3. test('待确认批次从词库批量补中文释义,保留词序与掌握状态',async()=>{
  4. const calls=[];const context={DEFAULT_COMPANY_ID:'co',assertLessonTenant:()=>{},orderDetailRow:async()=>({szyh:20}),parseArray:v=>Array.isArray(v)?v:JSON.parse(v||'[]'),fail:(_s,m)=>{throw Error(m);},Psql:{oneOrNone:async()=>null,query:async(sql,args)=>{calls.push({sql,args});if(sql.includes('LessonStudyBatch'))return[{batchId:'b',status:'draft',words:[{GeneralID:12,Title:'crop',check:1},{GeneralID:13,Title:'gather',check:2}]},{batchId:'recovered',status:'confirmed',provenance:'user-confirmed-recovery',words:[{GeneralID:14,Title:'church',check:0},{GeneralID:15,Title:'other',jssy:'旧词义',check:0}]}];if(sql.includes('VocabularyWord'))return[{generalId:13,sy:'聚集'},{generalId:12,sy:'庄稼'},{generalId:14,sy:'教堂'}];return[];}}};
  5. vm.createContext(context);vm.runInContext(source,context);const result=await context.appLessonStudy({orderId:'1'},{});
  6. assert.deepEqual(Array.from(result.batches[0].words,w=>[w.Title,w.sy,w.check]),[['crop','庄稼',1],['gather','聚集',2]]);
  7. assert.equal(result.batches[1].words[0].sy,'教堂');assert.equal(result.batches[1].words[1].jssy,'旧词义');assert.equal(result.batches[1].words[1].sy,undefined);
  8. assert.equal(calls.filter(c=>c.sql.includes('VocabularyWord')).length,1);
  9. });
  10. test('真实 SQL 可按单词编号关联词库并恢复含掌握状态的释义',async()=>{
  11. const {PGlite}=await import('@electric-sql/pglite');const db=new PGlite();try{
  12. await db.exec(`CREATE TABLE "LessonStudyBatch"(company text,"orderId" text,"batchId" text,status text,provenance text,"bindingId" text,words jsonb,"updatedAt" timestamptz);
  13. CREATE TABLE "CourseBinding"(company text,"objectId" text,kcid text);CREATE TABLE "Node"(company text,"nodeId" numeric,"nodeName" text);
  14. CREATE TABLE "DailyStudyRecord"(company text,id numeric,dsid text,"recordScope" text,xxqs text);
  15. CREATE TABLE "CommonModel"(company text,"modelId" numeric,"itemId" numeric,"generalId" numeric);
  16. CREATE TABLE "VocabularyWord"(company text,id numeric,sy text);
  17. INSERT INTO "LessonStudyBatch" VALUES('co','1','recovery','confirmed','user-confirmed-recovery',null,'[{"GeneralID":12,"Title":"crop","check":2}]',now());
  18. INSERT INTO "CommonModel" VALUES('co',52,7,12);INSERT INTO "VocabularyWord" VALUES('co',7,'庄稼');`);
  19. const query=async(s,p=[])=>(await db.query(s,p)).rows,context={DEFAULT_COMPANY_ID:'co',assertLessonTenant:()=>{},orderDetailRow:async()=>({szyh:20}),parseArray:v=>Array.isArray(v)?v:JSON.parse(v||'[]'),Psql:{query,oneOrNone:async(s,p)=>(await query(s,p))[0]||null}};
  20. vm.createContext(context);vm.runInContext(source,context);const result=await context.appLessonStudy({orderId:'1'},{});assert.equal(result.batches[0].words[0].sy,'庄稼');assert.equal(result.batches[0].words[0].check,2);
  21. }finally{await db.close();}
  22. });