test-article-reading-model.mjs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import assert from 'node:assert/strict'
  2. import { readFile } from 'node:fs/promises'
  3. import ts from 'typescript'
  4. const unitId = 'SHEP-5-上-Unit2'
  5. const topics = JSON.parse(
  6. await readFile(new URL('../src/views/Editor/EnglishSpeaking/data/topicDiscussionTasks.json', import.meta.url), 'utf8'),
  7. )
  8. const articles = JSON.parse(
  9. await readFile(new URL('../src/views/Editor/EnglishSpeaking/data/articleReadingTasks.json', import.meta.url), 'utf8'),
  10. )
  11. const article = articles[unitId][0]
  12. // 模型的所有导入都是 `import type`,转译后被抹除,因此模块自足、可直接在 Node 里跑。
  13. const sourceUrl = new URL('../src/views/Editor/EnglishSpeaking/preview/articleReadingModel.ts', import.meta.url)
  14. const source = await readFile(sourceUrl, 'utf8')
  15. const compiled = ts.transpileModule(source, {
  16. compilerOptions: { module: ts.ModuleKind.ESNext, target: ts.ScriptTarget.ES2020 },
  17. }).outputText
  18. const mod = await import(`data:text/javascript,${encodeURIComponent(compiled)}#${Math.random()}`)
  19. const empty = mod.buildEmptyArticleConfig('五年级')
  20. assert.deepEqual(empty, {
  21. type: 'article',
  22. grade: '五年级',
  23. title: '',
  24. content: '',
  25. practice: { mode: 'time', duration: 10 },
  26. evaluation: { showReport: true },
  27. })
  28. const fromTask = mod.buildArticleConfig(article, '五年级')
  29. assert.equal(fromTask.type, 'article')
  30. assert.equal(fromTask.grade, '五年级')
  31. assert.equal(fromTask.title, 'A Day at the Zoo')
  32. assert.equal(fromTask.content, article.content)
  33. assert.equal(mod.scoreToStars(0), 0)
  34. assert.equal(mod.scoreToStars(79), 3.95)
  35. assert.equal(mod.scoreToStars(100), 5)
  36. const allTasks = [...topics[unitId], article]
  37. assert.equal(mod.filterSpeakingTasks(allTasks, 'all').length, 5)
  38. assert.equal(mod.filterSpeakingTasks(allTasks, 'topic-discussion').length, 4)
  39. assert.equal(mod.filterSpeakingTasks(allTasks, 'article-reading').length, 1)
  40. assert.equal(mod.isFinalReportReady({ paragraphs: [{ status: 'generating' }], overallStatus: null }), false)
  41. assert.equal(mod.isFinalReportReady({ paragraphs: [{ status: 'completed' }], overallStatus: null }), true)
  42. assert.equal(mod.isFinalReportReady({ paragraphs: [{ status: 'failed' }], overallStatus: 'failed' }), true)
  43. assert.equal(mod.pollDelayMs(0), 1000)
  44. assert.equal(mod.pollDelayMs(5), 8000)