|
| 1 | +import { test, expect, describe,vi, afterEach, it } from 'vitest'; |
| 2 | +import A from './features/test'; |
| 3 | +const { getQuestionToday} = require('../common/utils/getQuestionToday'); |
| 4 | +const {sourceFilePath} = require('../common/utils/createQuestion'); |
| 5 | +const {generateTemplateContent} = require('../common/utils/fulfillQuestion'); |
| 6 | +const {getQuestionById} = require('../common/utils/getQuestionById'); |
| 7 | +const {removeDomTags} = require('../common/utils/removeDomTags'); |
| 8 | +const fs = require('fs'); |
| 9 | + |
| 10 | + |
| 11 | +vi.mock('fs/promises', () => { |
| 12 | + return { |
| 13 | + writeFile: vi.fn(), |
| 14 | + } |
| 15 | +}) |
| 16 | +const funRegex = /var\s+(\w+)\s*=\s*function\s*\(([^)]*)\)\s*{\s*([^}]*)\s*}/; |
| 17 | +const isContainJsCode = (input) => funRegex.test(input); |
| 18 | +const isContainTestCase = (input) => input.includes('showLogs('); |
| 19 | + |
| 20 | +const handleText = (input) => input.replace(/\n+/g, '\n').replaceAll('\n', '\n * '); |
| 21 | +const mockKeys = [ 'enName', 'title', 'detail', 'id', 'jsCode', 'date' ]; |
| 22 | + |
| 23 | +function isValidQuestion(res) { |
| 24 | + const content = generateTemplateContent(fileContent,res); |
| 25 | + // 是否含有函数 |
| 26 | + expect(isContainJsCode(content)).toBeTruthy(); |
| 27 | + // 是否含有测试用例 |
| 28 | + expect(isContainTestCase(content)).toBeTruthy(); |
| 29 | + // 是否含有描述 |
| 30 | + expect(content.includes('示例')).toBeTruthy(); |
| 31 | + expect(content.includes('提示')).toBeTruthy(); |
| 32 | + |
| 33 | +} |
| 34 | +const fileContent = fs.readFileSync(sourceFilePath, 'utf-8'); |
| 35 | + |
| 36 | +describe('leet-create', ()=> { |
| 37 | + // 清楚mock历史记录 |
| 38 | + afterEach(()=> { |
| 39 | + vi.clearAllMocks(); |
| 40 | + }) |
| 41 | + describe('with -t option', async() => { |
| 42 | + const res = await getQuestionToday(); |
| 43 | + |
| 44 | + it('是否正确获取了今天的题目', () => { |
| 45 | + expect(Object.keys(res)).toEqual(mockKeys); |
| 46 | + // 比较日期是否相等 |
| 47 | + expect(new Date(res.date).getDate()).toEqual(new Date().getDate()); |
| 48 | + // 比较月份是否相等 |
| 49 | + expect(new Date(res.date).getMonth()).toEqual(new Date().getMonth()); |
| 50 | + // 比较年份是否相等 |
| 51 | + expect(new Date(res.date).getFullYear()).toEqual(new Date().getFullYear()); |
| 52 | + |
| 53 | + }); |
| 54 | + it('是否正确的填充了今天的题目', async ()=> { |
| 55 | + isValidQuestion(res) |
| 56 | + |
| 57 | + }) |
| 58 | + |
| 59 | + }); |
| 60 | + describe('with -r option', async () => { |
| 61 | + const id_25 = '25'; |
| 62 | + const res_25 = await getQuestionById(id_25); |
| 63 | + const id_LCS_03 = 'LCS 03'; |
| 64 | + const res_LCS_03 = await getQuestionById(id_LCS_03); |
| 65 | + it('是否正确的获取了指定id的题目 25', async () => { |
| 66 | + expect(res_25.id).toEqual(id_25); |
| 67 | + }) |
| 68 | + it('是否正确填充了指定id的题目 25', async ()=> { |
| 69 | + isValidQuestion(res_25) |
| 70 | + }) |
| 71 | + it('是否正确的获取了指定id的题目 LCS 03', async () => { |
| 72 | + expect(res_LCS_03.id).toEqual(id_LCS_03); |
| 73 | + }) |
| 74 | + it('是否正确填充了指定id的题目 9', async ()=> { |
| 75 | + isValidQuestion(res_LCS_03) |
| 76 | + }) |
| 77 | + |
| 78 | + it('是否正确的获取了指定内容的题目 主题空间', async () => { |
| 79 | + const content = '主题空间'; |
| 80 | + const res = await getQuestionById(content); |
| 81 | + expect(res.id).toEqual(null); |
| 82 | + }) |
| 83 | + |
| 84 | + }) |
| 85 | +}) |
| 86 | + |
0 commit comments