|
| 1 | +import { DecisionCode, parseArgs, printTestCaseResult, startHttpServer } from '@exercode/problem-utils'; |
| 2 | +import type { TestCaseResult } from '@exercode/problem-utils'; |
| 3 | +import assert from 'node:assert'; |
| 4 | +import puppeteer from 'puppeteer'; |
| 5 | +import type { Page } from 'puppeteer'; |
| 6 | + |
| 7 | +const TEST_CASES: readonly [string, (page: Page) => Promise<Omit<TestCaseResult, 'testCaseId'>>][] = [ |
| 8 | + [ |
| 9 | + '01_h1', |
| 10 | + async (page) => { |
| 11 | + try { |
| 12 | + const h1Handle = await page.locator('h1').waitHandle(); |
| 13 | + const h1Text = await h1Handle.evaluate((e) => e.textContent.trim()); |
| 14 | + assert.strictEqual(h1Text, '今日の天気予報'); |
| 15 | + } catch (error) { |
| 16 | + return { |
| 17 | + decisionCode: DecisionCode.WRONG_ANSWER, |
| 18 | + stderr: error instanceof Error ? error.message : String(error), |
| 19 | + feedbackMarkdown: '`h1`タグによる見出し`今日の天気予報`が見つかりません。', |
| 20 | + }; |
| 21 | + } |
| 22 | + return { decisionCode: DecisionCode.ACCEPTED }; |
| 23 | + }, |
| 24 | + ], |
| 25 | + [ |
| 26 | + '02_hr', |
| 27 | + async (page) => { |
| 28 | + try { |
| 29 | + await page.locator('hr').waitHandle(); |
| 30 | + } catch (error) { |
| 31 | + return { |
| 32 | + decisionCode: DecisionCode.WRONG_ANSWER, |
| 33 | + stderr: error instanceof Error ? error.message : String(error), |
| 34 | + feedbackMarkdown: '`hr`タグによる水平線が見つかりません。', |
| 35 | + }; |
| 36 | + } |
| 37 | + return { decisionCode: DecisionCode.ACCEPTED }; |
| 38 | + }, |
| 39 | + ], |
| 40 | + [ |
| 41 | + '03_p', |
| 42 | + async (page) => { |
| 43 | + const requiredTexts = ['晴れ', '最高気温:25℃', '最低気温:18℃', '降水確率:0%']; |
| 44 | + |
| 45 | + const pTexts = await page.$$eval('p', (es) => es.map((e) => e.textContent?.trim() ?? '')); |
| 46 | + |
| 47 | + if (pTexts.length !== requiredTexts.length) { |
| 48 | + return { |
| 49 | + decisionCode: DecisionCode.WRONG_ANSWER, |
| 50 | + feedbackMarkdown: `\`p\`タグの件数が一致しません。\n${requiredTexts.length}件必要ですが、${pTexts.length}件見つかりました。`, |
| 51 | + }; |
| 52 | + } |
| 53 | + |
| 54 | + for (const [i, expected] of requiredTexts.entries()) { |
| 55 | + if (pTexts[i] === expected) continue; |
| 56 | + return { |
| 57 | + decisionCode: DecisionCode.WRONG_ANSWER, |
| 58 | + feedbackMarkdown: `\`p\`タグの内容が一致しません。\n${i + 1}番目には\`${expected}\`が期待されていますが、\`${pTexts[i]}\`が見つかりました。`, |
| 59 | + }; |
| 60 | + } |
| 61 | + |
| 62 | + return { decisionCode: DecisionCode.ACCEPTED }; |
| 63 | + }, |
| 64 | + ], |
| 65 | +]; |
| 66 | + |
| 67 | +const args = parseArgs(process.argv); |
| 68 | +await using server = startHttpServer(args.cwd); |
| 69 | + |
| 70 | +const browser = await puppeteer.launch({ |
| 71 | + args: process.env.CI || process.env.WB_DOCKER === '1' ? ['--no-sandbox', '--disable-setuid-sandbox'] : [], |
| 72 | +}); |
| 73 | +const page = await browser.newPage(); |
| 74 | +page.setDefaultTimeout(1000); |
| 75 | + |
| 76 | +await page.goto(server.url, { waitUntil: 'domcontentloaded' }); |
| 77 | + |
| 78 | +for (const [testCaseId, test] of TEST_CASES) { |
| 79 | + const result = await test(page); |
| 80 | + printTestCaseResult({ testCaseId, ...result }); |
| 81 | + if (result.decisionCode !== DecisionCode.ACCEPTED) break; |
| 82 | +} |
| 83 | + |
| 84 | +await browser.close(); |
0 commit comments