writing-review-detail.test.mjs 1.7 KB

123456789101112131415161718192021
  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. const source=readFileSync(new URL('../deploy-admin-functions.mjs',import.meta.url),'utf8').match(/const appGatewayCode = String.raw`([\s\S]*?)\n`;/)[1];
  6. function context({category='primary_writing',learned=0,student=20,status=30,authorized=true,words='[]'}={}) {
  7. let legacyCalls=0;
  8. const ctx=vm.createContext({Parse:{},Psql:{query:async()=>[{company:'co',generalId:100,__addon:{userId:20,dsid:200,xxqs:words,learned}}]}});
  9. vm.runInContext(source,ctx);
  10. ctx.authorizeLearningRecordAccess=async()=>{if(!authorized)throw Error('无权查看');};
  11. ctx.orderDetailRow=async()=>({szyh:student,dszt:status,courseCategoryKey:category});
  12. ctx.legacyStudyRecordWords=async()=>{legacyCalls++;return[];};
  13. return {run:()=>ctx.recordDetailData({id:100},{}),legacyCalls:()=>legacyCalls};
  14. }
  15. for(const category of ['primary_writing','middle_writing','high_writing'])test(category+' empty confirmed source is an intentional notes review',async()=>{
  16. const c=context({category}),result=await c.run();assert.equal(result.words.length,0);assert.equal(result.addon[0].reviewContentMode,'notes');assert.equal(c.legacyCalls(),0);
  17. });
  18. for(const options of [{category:'word'},{learned:1},{student:21},{status:10}])test('missing words are not converted into notes: '+JSON.stringify(options),async()=>{
  19. const c=context(options),result=await c.run();assert.equal(result.addon[0].reviewContentMode,undefined);assert.equal(c.legacyCalls(),1);
  20. });
  21. test('notes review still requires source record authorization',async()=>{const c=context({authorized:false});await assert.rejects(c.run(),/无权查看/);assert.equal(c.legacyCalls(),0);});