Download task_007/tests/validate_aaav_dynamic.js from Lego-X/UXInteract: direct link, hf CLI and curl.
- Browser
- Download file 8.5 kB
-
https://huggingface.co/datasets/Lego-X/UXInteract/resolve/main/task_007/tests/validate_aaav_dynamic.js
- Command line
-
hf download hf://datasets/Lego-X/UXInteract/task_007/tests/validate_aaav_dynamic.js
-
curl -L -o validate_aaav_dynamic.js https://huggingface.co/datasets/Lego-X/UXInteract/resolve/main/task_007/tests/validate_aaav_dynamic.js
8.5 kB
| const fs = require('fs'); | |
| const path = require('path'); | |
| const { pathToFileURL } = require('url'); | |
| const { chromium } = require('playwright'); | |
| const TASK_ID = 8; | |
| const KIND = "browser"; | |
| const TESTIDS = [ | |
| "question-input", | |
| "upload-question", | |
| "question-item", | |
| "edit-question", | |
| "offline-question", | |
| "delete-question", | |
| "role-select", | |
| "answer-record", | |
| "admin-dashboard", | |
| "question-bank", | |
| "user-management", | |
| "permission-management" | |
| ]; | |
| // Only `question-input`, `upload-question`, `question-item` are disclosed in the | |
| // instruction (with "等"). For the remaining hooks the instruction does not pin an | |
| // exact testid, so accept reasonable aliases instead of forcing the reference's | |
| // literal testid. The reference still uses the canonical testid, so it keeps passing. | |
| const TESTID_SELECTORS = { | |
| "question-input": '[data-testid="question-input"]', | |
| "upload-question": '[data-testid="upload-question"]', | |
| "question-item": '[data-testid="question-item"]', | |
| "edit-question": '[data-testid="edit-question"], [data-testid="edit-btn"], .edit-question', | |
| "offline-question": '[data-testid="offline-question"], [data-testid="toggle-question"], [data-testid="toggle-status"], .offline-question', | |
| "delete-question": '[data-testid="delete-question"], [data-testid="remove-question"], .delete-question', | |
| "role-select": '[data-testid="role-select"], [data-testid="role-selector"], select[name="role"]', | |
| "answer-record": '[data-testid="answer-record"], [data-answer-record], [data-testid="record-answer"]', | |
| "admin-dashboard": '[data-testid="admin-dashboard"], [data-testid="dashboard"]', | |
| "question-bank": '[data-testid="question-bank"], [data-testid="question-management"]', | |
| "user-management": '[data-testid="user-management"], [data-testid="user-admin"]', | |
| "permission-management": '[data-testid="permission-management"], [data-testid="role-management"]' | |
| }; | |
| function selectorFor(id) { return TESTID_SELECTORS[id] || `[data-testid="${id}"]`; } | |
| const HAS_QUESTION_WORKFLOW = true; | |
| const SKIP_DIRS = new Set([ | |
| "tests", | |
| "logs", | |
| "node_modules", | |
| ".git" | |
| ]); | |
| function assert(condition, message) { | |
| if (!condition) throw new Error(message); | |
| } | |
| function walk(dir) { | |
| let out = []; | |
| for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { | |
| if (SKIP_DIRS.has(entry.name)) continue; | |
| const p = path.join(dir, entry.name); | |
| if (entry.isDirectory()) out = out.concat(walk(p)); | |
| else out.push(p); | |
| } | |
| return out; | |
| } | |
| function findEntry() { | |
| const files = walk(process.cwd()); | |
| if (KIND === 'svg') return files.find(f => f.toLowerCase().endsWith('.svg')); | |
| return files.find(f => f.toLowerCase().endsWith('.html')) || files.find(f => f.toLowerCase().endsWith('.svg')); | |
| } | |
| async function snapshot(page) { | |
| return await page.evaluate(() => JSON.stringify({ | |
| text: document.body?.innerText || '', | |
| htmlLength: document.body?.innerHTML?.length || 0, | |
| controls: document.querySelectorAll('button,[role="button"],input,select,textarea,canvas,svg,a[href]').length, | |
| storage: window.localStorage ? window.localStorage.length : 0, | |
| canvas: Array.from(document.querySelectorAll('canvas')).map(c => { | |
| try { return c.toDataURL().slice(0, 80); } catch { return ''; } | |
| }), | |
| })); | |
| } | |
| async function runQuestionWorkflow(page) { | |
| const input = page.locator('[data-testid="question-input"]'); | |
| const upload = page.locator('[data-testid="upload-question"]'); | |
| const items = page.locator('[data-testid="question-item"]'); | |
| const before = await items.count(); | |
| const text = 'AAAV dynamic question ' + Date.now(); | |
| await input.fill(text); | |
| await upload.click(); | |
| await page.waitForTimeout(150); | |
| assert(await items.count() > before, 'upload-question should add a question item'); | |
| const first = items.first(); | |
| const offlineSel = selectorFor('offline-question'); | |
| const editSel = selectorFor('edit-question'); | |
| const roleSel = selectorFor('role-select'); | |
| const deleteSel = selectorFor('delete-question'); | |
| if (await first.locator(offlineSel).count()) { | |
| const oldStatus = await first.getAttribute('data-status'); | |
| await first.locator(offlineSel).click(); | |
| await page.waitForTimeout(100); | |
| assert(oldStatus !== await first.getAttribute('data-status'), 'offline-question should toggle data-status'); | |
| } | |
| if (await first.locator(editSel).count()) { | |
| await first.locator(editSel).click(); | |
| await page.waitForTimeout(100); | |
| assert(await first.getAttribute('data-edited') === 'true', 'edit-question should mark item as edited'); | |
| } | |
| if (await page.locator(roleSel).count()) { | |
| await page.locator(roleSel).selectOption('normal user').catch(() => {}); | |
| await page.waitForTimeout(100); | |
| assert(!(await page.locator(deleteSel).first().isVisible().catch(() => false)), 'delete should hide for normal user'); | |
| await page.locator(roleSel).selectOption('admin').catch(() => {}); | |
| await page.waitForTimeout(100); | |
| assert(await page.locator(deleteSel).first().isVisible().catch(() => false), 'delete should show for admin'); | |
| } | |
| } | |
| (async () => { | |
| const entry = findEntry(); | |
| assert(entry, 'no browser-openable entrypoint found'); | |
| const browser = await chromium.launch({ headless: true }); | |
| const page = await browser.newPage({ viewport: { width: 1100, height: 760 } }); | |
| const errors = []; | |
| page.on('pageerror', err => errors.push(err.message)); | |
| page.on('console', msg => { if (msg.type() === 'error') errors.push(msg.text()); }); | |
| await page.goto(pathToFileURL(entry).href, { waitUntil: 'domcontentloaded' }); | |
| await page.waitForTimeout(500); | |
| assert(errors.length === 0, 'browser errors on load: ' + errors.join(' | ')); | |
| if (KIND === 'svg') { | |
| const stats = await page.evaluate(() => ({ | |
| svg: document.querySelectorAll('svg').length, | |
| elems: document.querySelectorAll('svg *').length, | |
| text: (document.body?.innerText || '').trim().length, | |
| })); | |
| assert(stats.svg >= 1, 'svg task should render an svg root'); | |
| assert(stats.elems >= 2 || stats.text >= 5, 'svg task should render visible structure'); | |
| } else { | |
| const stats = await page.evaluate(() => ({ | |
| visible: Array.from(document.querySelectorAll('body *')).filter(el => { | |
| const r = el.getBoundingClientRect(); | |
| const s = getComputedStyle(el); | |
| return r.width > 0 && r.height > 0 && s.visibility !== 'hidden' && s.display !== 'none'; | |
| }).length, | |
| text: (document.body?.innerText || '').trim().length, | |
| controls: document.querySelectorAll('button,[role="button"],input,select,textarea,canvas,svg,a[href]').length, | |
| })); | |
| assert(stats.visible >= 3 || stats.text >= 20, 'page should render meaningful visible content'); | |
| for (const id of TESTIDS) { | |
| const count = await page.locator(selectorFor(id)).count(); | |
| assert(count > 0, `missing required hook: ${id}`); | |
| } | |
| if (HAS_QUESTION_WORKFLOW) await runQuestionWorkflow(page); | |
| const controls = await page.$$('button,[role="button"],input,select,textarea,canvas,svg,a[href]'); | |
| if (controls.length) { | |
| const before = await snapshot(page); | |
| for (const control of controls.slice(0, 6)) { | |
| const tag = await control.evaluate(el => el.tagName.toLowerCase()).catch(() => ''); | |
| if (tag === 'input' || tag === 'textarea') await control.fill('aaav-dynamic-input').catch(() => {}); | |
| else if (tag === 'select') { | |
| const value = await control.evaluate(el => Array.from(el.options || [])[1]?.value || Array.from(el.options || [])[0]?.value || '').catch(() => ''); | |
| if (value) await control.selectOption(value).catch(() => {}); | |
| } else { | |
| await control.click({ force: true }).catch(() => {}); | |
| } | |
| } | |
| await page.keyboard.press('Enter').catch(() => {}); | |
| await page.keyboard.press('Space').catch(() => {}); | |
| await page.waitForTimeout(200); | |
| await snapshot(page); | |
| // State changes are informative but not mandatory: static dashboards and | |
| // diagrams may be valid even when generic clicks do not mutate state. | |
| void before; | |
| } | |
| } | |
| assert(errors.length === 0, 'browser errors after interaction: ' + errors.join(' | ')); | |
| await browser.close(); | |
| console.log(`AAAV targeted dynamic test passed for task ${TASK_ID}`); | |
| })().catch(err => { | |
| console.error(err && err.stack || err); | |
| process.exit(1); | |
| }); | |