File size: 4,417 Bytes
ef987bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const fs = require('fs');
const path = require('path');
const {pathToFileURL} = require('url');
const {chromium} = require('playwright');
const {editQuestion} = require('./edit_question');
const assert = (value, message) => { if (!value) throw Error(message); };

(async () => {
  const browser = await chromium.launch({headless: true, args: ['--no-sandbox']});
  try {
    const page = await browser.newPage({viewport: {width: 1200, height: 900}});
    await page.route(/^https?:/, route => route.abort());
    await page.goto(pathToFileURL(path.join(process.cwd(), 'index.html')).href);
    const items = page.getByTestId('question-item');
    const input = page.getByTestId('question-input');
    async function upload(text) {
      const before = await items.count();
      await input.fill(text);
      await page.getByTestId('upload-question').click();
      assert(await items.count() === before + 1 && (await items.last().innerText()).includes(text),
        'upload must append the supplied question text');
    }
    await upload('保留题甲');
    await upload('待编辑题乙');
    await input.fill('已编辑题乙');
    await editQuestion(page, items.last().getByRole('button', {name: /edit|编辑|修改/i}).first(), '已编辑题乙');
    assert((await items.nth((await items.count()) - 2).innerText()).includes('保留题甲')
      && (await items.last().innerText()).includes('已编辑题乙'), 'editing must update only the selected record');

    const role = page.getByRole('combobox').first();
    const options = await role.locator('option').evaluateAll(elements => elements.map(e => ({value: e.value, text: e.textContent})));
    const normal = options.find(x => /normal user|普通用户/i.test(x.text + ' ' + x.value));
    const admin = options.find(x => /admin|管理员/i.test(x.text + ' ' + x.value));
    assert(normal && admin, 'roles must distinguish normal user and administrator');
    const output = '/logs/verifier/visual';
    fs.mkdirSync(output, {recursive: true});
    await page.screenshot({path: path.join(output, '01-admin.png'), fullPage: true});
    await role.selectOption(normal.value);
    const deleteControl = items.last().getByRole('button', {name: /delete|remove|删除/i});
    if (await deleteControl.count()) {
      assert(!await deleteControl.isVisible() || !await deleteControl.isEnabled(), 'normal user has enabled delete control');
    }
    await page.screenshot({path: path.join(output, '02-normal.png'), fullPage: true});
    await role.selectOption(admin.value);
    const before = await items.count();
    await items.last().getByRole('button', {name: /delete|remove|删除/i}).click();
    assert(await items.count() === before - 1, 'admin deletion did not remove exactly one record');
    assert((await items.last().innerText()).includes('保留题甲'), 'deletion removed the wrong record');
    await page.screenshot({path: path.join(output, '03-delete.png'), fullPage: true});
    const users = [
      {id: 'review-a', name: '甲用户', email: 'a@fixture.invalid', records: ['甲-微积分-正确', '甲-几何-错误']},
      {id: 'review-b', name: '乙用户', email: 'b@fixture.invalid', records: ['乙-代数-正确']},
      {id: 'review-empty', name: '新用户', email: 'empty@fixture.invalid', records: []},
    ];
    await page.evaluate(users => window.__setUsers(users), users);
    assert(await page.getByTestId('user-card').count() === users.length, 'user injection must replace the user list');
    for (const user of users) {
      const card = page.locator(`[data-testid="user-card"][data-user-id="${user.id}"]`);
      assert(await card.isVisible(), `missing user card ${user.id}`);
      const text = await card.innerText();
      assert(text.includes(user.name) && text.includes(user.email), 'user basic info is not associated with the card');
      const records = await card.getByTestId('answer-record').allInnerTexts();
      assert(records.length === user.records.length && user.records.every(record => records.some(text => text.includes(record))),
        `answer records associated with wrong user: ${user.id}`);
    }
    await page.screenshot({path: path.join(output, '04-user-record-association.png'), fullPage: true});
    console.log('Question CRUD, record independence and role restoration passed');
  } finally { await browser.close(); }
})().catch(error => { console.error(error); process.exit(1); });