File size: 8,153 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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | const path = require('path');
const { pathToFileURL } = require('url');
const chromium = require("playwright").chromium;
const fs = require('node:fs');
const {languageValue, selectLanguage, selectUnsupportedLanguage} = require('./language_options');
async function hasUnsupportedPopup(page, dialogs) {
if (dialogs.some(message => /unsupport/i.test(message))) return true;
const popups = page.locator('dialog:visible, [role="dialog"]:visible, [role="alertdialog"]:visible');
return (await popups.allInnerTexts()).some(text => /unsupport/i.test(text));
}
async function dismissHtmlPopup(page) {
const popup = page.locator('dialog:visible, [role="dialog"]:visible, [role="alertdialog"]:visible').first();
if (await popup.count()) {
const close = popup.getByRole('button', {name: /ok|close|确认|确定|关闭/i}).first();
assert(await close.count(), 'popup needs an accessible close button');
await close.click();
}
}
const entry = path.join(process.cwd(), 'index.html');
function assert(condition, message) {
if (!condition) throw new Error(message);
}
async function openFixture(browser) {
const context = await browser.newContext({ viewport: { width: 1000, height: 760 } });
const attemptedNetwork = [];
await context.route(/^https?:\/\//, route => {
attemptedNetwork.push(route.request().url());
return route.abort('blockedbyclient');
});
const page = await context.newPage();
const errors = [];
page.on('pageerror', error => errors.push(error.message || String(error)));
page.on('console', message => {
if (message.type() === 'error') errors.push(message.text());
});
await page.goto(pathToFileURL(entry).href, { waitUntil: 'domcontentloaded' });
return { context, page, attemptedNetwork, errors };
}
async function login(page, username, password) {
await page.locator('[data-testid="username"]').fill(username);
await page.locator('[data-testid="password"]').fill(password);
await page.locator('[data-testid="login-button"]').click();
}
async function finishCase(context, attemptedNetwork, errors) {
assert(attemptedNetwork.length === 0, `HTTP(S) requests are forbidden: ${attemptedNetwork.join(', ')}`);
assert(errors.length === 0, `browser errors: ${errors.join(' | ')}`);
await context.close();
}
(async () => {
const browser = await chromium.launch({
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage'],
});
try {
// ENHANCED_CASE_1: the whitelist rejects a different username even with the right password.
{
const state = await openFixture(browser);
await login(state.page, 'not-admin', 'admin');
assert(!(await state.page.locator('[data-testid="language-screen"]').isVisible()), 'a non-whitelisted username entered language selection');
await finishCase(state.context, state.attemptedNetwork, state.errors);
}
{
const state = await openFixture(browser);
await login(state.page, 'admin', 'admin');
assert(await state.page.locator('[data-testid="language-screen"]').isVisible(), 'admin/admin did not enter language selection');
await finishCase(state.context, state.attemptedNetwork, state.errors);
}
// ENHANCED_CASE_2: both explicitly supported languages proceed and expose all required lessons.
for (const language of ['Chinese', 'English']) {
const state = await openFixture(browser);
await login(state.page, 'admin', 'admin');
await selectLanguage(state.page, language);
await state.page.locator('[data-testid="next-button"]').click();
const lesson = state.page.locator('[data-testid="lesson-screen"]');
assert(await lesson.isVisible(), `${language} did not proceed to the lesson screen`);
const text = await lesson.innerText();
for (const word of ['boy', 'women', 'man', 'apple', '男孩', '女人', '男人', '苹果']) {
assert(text.includes(word), `${language} lesson did not render required vocabulary: ${word}`);
}
fs.mkdirSync('/logs/verifier/visual-audit', {recursive: true});
await state.page.screenshot({path: `/logs/verifier/visual-audit/lesson-${language}.png`, fullPage: true});
await finishCase(state.context, state.attemptedNetwork, state.errors);
}
// ENHANCED_CASE_3: an unsupported available language reports unsupport and does not show lessons.
{
const state = await openFixture(browser);
const dialogs = [];
state.page.on('dialog', async dialog => {
dialogs.push(dialog.message());
await dialog.dismiss();
});
await login(state.page, 'admin', 'admin');
await selectUnsupportedLanguage(state.page);
await state.page.locator('[data-testid="next-button"]').click();
await state.page.waitForTimeout(50);
const feedback = `${await state.page.locator('body').innerText()} ${dialogs.join(' ')}`.toLowerCase();
assert(feedback.includes('unsupport'), 'unsupported language did not display unsupport feedback');
assert(!(await state.page.locator('[data-testid="lesson-screen"]').isVisible()), 'unsupported language exposed lesson content');
await finishCase(state.context, state.attemptedNetwork, state.errors);
}
// ENHANCED_CASE_4: changing from a supported language to an unsupported one must revoke the lesson.
{
const state = await openFixture(browser);
const dialogs = [];
state.page.on('dialog', async dialog => {dialogs.push(dialog.message()); await dialog.dismiss();});
await login(state.page, 'admin', 'admin');
await selectLanguage(state.page, 'Chinese');
await state.page.locator('[data-testid="next-button"]').click();
assert(await state.page.locator('[data-testid="lesson-screen"]').isVisible(), 'supported choice did not enter lesson');
await selectUnsupportedLanguage(state.page);
await state.page.locator('[data-testid="next-button"]').click();
assert(await hasUnsupportedPopup(state.page, dialogs), 'unsupported choice must show a native or HTML popup');
assert(!(await state.page.locator('[data-testid="lesson-screen"]').isVisible()), 'old lesson remained visible after unsupported choice');
await dismissHtmlPopup(state.page);
const englishValue = await languageValue(state.page, 'English');
await selectLanguage(state.page, 'English');
await state.page.getByTestId('next-button').click();
assert(await state.page.getByTestId('lesson-screen').isVisible(), 'supported lesson needs to be reachable again');
const loginControls = ['username', 'password', 'login-button'];
if (!(await Promise.all(loginControls.map(id => state.page.getByTestId(id).isVisible()))).every(Boolean)) {
// A lesson may unmount its sign-in screen. Re-open the offline page to
// exercise the same public login controls without requiring both views.
await state.page.reload({waitUntil: 'domcontentloaded'});
}
await login(state.page, 'admin', 'wrong');
assert(!(await state.page.locator('[data-testid="language-screen"]').isVisible()), 'failed re-login kept language screen');
assert(!(await state.page.getByTestId('lesson-screen').isVisible()), 'failed re-login kept the old lesson');
// Protected controls may be hidden, disabled or unmounted after login failure.
await state.page.getByTestId('language-select').evaluateAll((controls, value) => {
for (const select of controls) {
select.value = value;
select.dispatchEvent(new Event('change', {bubbles: true}));
}
}, englishValue);
await state.page.getByTestId('next-button').evaluateAll(controls => controls.forEach(control => control.click()));
assert(!(await state.page.locator('[data-testid="lesson-screen"]').isVisible()), 'failed login could bypass access with hidden button');
await finishCase(state.context, state.attemptedNetwork, state.errors);
}
} finally {
await browser.close();
}
console.log('enhanced cases passed for task 21');
})().catch(error => {
console.error(error && error.stack || error);
process.exit(1);
});
|