fix: rename assistant and gate exports behind collapsible switch

This commit is contained in:
薇星幻海
2026-10-10 04:14:09 +08:00
parent 0ea302bbab
commit ed24cf18d0
8 changed files with 145 additions and 60 deletions
+6 -2
View File
@@ -6,9 +6,10 @@ const os = require('node:os');
const http = require('node:http');
const { chromium } = require('playwright');
const workspace = path.resolve(__dirname, '..');
const script = fs.readFileSync(path.join(workspace, '谊安学题库导出.user.js'), 'utf8');
const script = fs.readFileSync(path.join(workspace, '谊安学助手 2.0.user.js'), 'utf8');
assert(script.includes('// @match *://*/*'));
assert(!/\/\/ @match\s+https?:\/\/10\./.test(script));
assert(script.includes('// @name 谊安学助手 2.0'));
const qaDir = process.env.YIAN_QA_DIR || path.join(os.tmpdir(), 'yianxue-assistant-qa');
fs.mkdirSync(qaDir, { recursive: true });
@@ -48,7 +49,7 @@ function documentHTML(practice) {
}
(async () => {
const memory = new Map([['yianxue.assistant.setting.v2.autoExport', false]]);
const memory = new Map([['yianxue.assistant.setting.v2.autoExport', true], ['yianxue.assistant.setting.v2.exportEnabled', true]]);
let clipboard = '';
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html;charset=utf-8' });
@@ -76,6 +77,8 @@ function documentHTML(practice) {
assert.equal(page.url(), startURL); assert.equal(await page.title(), '普通网页验证');
assert(await page.getByRole('heading', { name: '任意网址的题库搜索' }).isVisible());
await page.getByRole('button', { name: '展开题库助手' }).click();
assert(await page.locator('#yian-question-assistant .brand').textContent() === '谊安学助手 2.0');
assert.equal(await page.getByRole('switch', { name: '导出题库', exact: true }).isChecked(), false);
assert(await page.getByRole('switch', { name: '自动选择答案' }).isDisabled());
assert.equal(await page.evaluate(() => typeof window.GM_getValue), 'undefined');
console.log('PASS 任意路径显示搜索面板,非练习页禁止自动选择,沙箱API兼容');
@@ -134,6 +137,7 @@ function documentHTML(practice) {
const secondURL = `http://localhost:${port}/different/application/route?flag=exercise`;
await second.goto(secondURL); await second.addScriptTag({ content: sandboxScript }); await second.evaluate(() => YianAssistant.ready);
await second.waitForFunction(() => YianAssistant.getState().pageReady);
assert.equal(await second.getByRole('switch', { name: '导出题库', exact: true }).isChecked(), false, '旧版导出偏好不能开启新版开关');
assert.equal(await second.evaluate(() => YianAssistant.getSnapshot().total), input.total);
assert.equal(await second.evaluate(() => YianAssistant.getState().role), '主操');
assert.equal(await second.getByRole('switch', { name: '自动选择答案' }).isChecked(), false);
+38 -3
View File
@@ -8,7 +8,7 @@ const { chromium } = require('playwright');
const root = path.resolve(__dirname, '..');
const coreSource = fs.readFileSync(path.join(root, 'question-bank-core.js'), 'utf8');
const scriptSource = fs.readFileSync(path.join(root, '谊安学题库导出.user.js'), 'utf8');
const scriptSource = fs.readFileSync(path.join(root, '谊安学助手 2.0.user.js'), 'utf8');
const qaDir = process.env.YIAN_QA_DIR || path.join(os.tmpdir(), 'yianxue-export-qa');
fs.mkdirSync(qaDir, { recursive: true });
const samplePath = path.join(root, '网页样本', '考试答案', '谊安学.html');
@@ -68,9 +68,21 @@ function parseCSV(text) {
const practiceURL = title => origin + '/clientcn/exam/' + encodeURIComponent(title) + '/questions?title=' + encodeURIComponent(title) + '&flag=exercise';
await page.goto(practiceURL('模拟练习'));
await page.evaluate(() => { window.answerClicks = 0; });
const firstDownload = page.waitForEvent('download');
await page.addScriptTag({ content: scriptSource });
await page.evaluate(() => YianAssistant.ready);
await page.waitForFunction(() => YianAssistant.getState().pageReady);
await page.waitForTimeout(800);
assert.equal(downloads.length, 0, '导出开关默认关闭,不应自动下载');
assert.equal(await page.getByRole('switch', { name: '导出题库', exact: true }).isChecked(), false);
assert(await page.getByRole('button', { name: '导出 CSV', exact: true, includeHidden: true }).isHidden());
const blockedMessage = await page.evaluate(() => {
try { YianAssistant.exportCurrent('csv'); return ''; } catch (error) { return error.message; }
});
assert(blockedMessage.includes('开关'), '开关关闭时导出入口必须拒绝手动下载');
const firstDownload = page.waitForEvent('download');
await page.getByRole('switch', { name: '导出题库', exact: true }).check();
const downloaded = await firstDownload;
assert(await page.getByRole('button', { name: '导出 CSV', exact: true }).isVisible());
await downloaded.saveAs(path.join(qaDir, 'synthetic.csv'));
const bank = await page.evaluate(() => QuestionBankCore.extract(document));
assert.equal(bank.total, 3); assert.equal(bank.answered, 3); assert.equal(bank.complete, true);
@@ -140,6 +152,26 @@ function parseCSV(text) {
assert.equal(await page.evaluate(() => window.answerClicks), 0);
console.log('PASS 缺题等待、考试页面不触发、不改变答题状态');
await page.getByRole('switch', { name: '导出题库', exact: true }).uncheck();
assert(await page.getByRole('button', { name: '导出 CSV', exact: true, includeHidden: true }).isHidden());
const beforeClosedRoute = downloads.length;
await page.evaluate(({ url, html }) => {
history.pushState({}, '', url);
document.querySelector('app-examtest').innerHTML = new DOMParser().parseFromString(html, 'text/html').querySelector('app-examtest').innerHTML;
}, { url: practiceURL('开关关闭练习'), html: fixture('开关关闭练习') });
await page.waitForFunction(() => YianAssistant.getState().pageReady);
await page.waitForTimeout(800);
assert.equal(downloads.length, beforeClosedRoute, '开关关闭时新练习也不自动导出');
const reopening = page.waitForEvent('download');
await page.getByRole('switch', { name: '导出题库', exact: true }).check();
assert.equal((await reopening).suggestedFilename(), '开关关闭练习_题库_3题.csv');
await page.getByRole('switch', { name: '导出题库', exact: true }).uncheck();
const closedManual = await page.evaluate(() => {
try { YianAssistant.exportCurrent('json'); return ''; } catch (error) { return error.message; }
});
assert(closedManual.includes('开关'));
console.log('PASS 导出默认关闭与折叠、关闭后换页不下载、手动入口拒绝、开启才导出');
const sampleHTML = fs.existsSync(samplePath) ? fs.readFileSync(samplePath, 'utf8') : fixture();
const tool = await context.newPage();
tool.on('pageerror', error => errors.push(error.message));
@@ -183,7 +215,10 @@ function parseCSV(text) {
const localHTML = '<!doctype html><!-- saved from url=(0100)http://10.119.67.7/clientcn/exam/local/questions?flag=exercise -->' + fixture('本地练习');
const localPath = path.join(qaDir, 'local-practice.html'); fs.writeFileSync(localPath, localHTML);
await local.goto(require('node:url').pathToFileURL(localPath).href);
const localDownload = local.waitForEvent('download'); await local.addScriptTag({ content: scriptSource });
await local.addScriptTag({ content: scriptSource });
await local.evaluate(() => YianAssistant.ready);
const localDownload = local.waitForEvent('download');
await local.getByRole('switch', { name: '导出题库', exact: true }).check();
assert.equal((await localDownload).suggestedFilename(), '本地练习_题库_3题.csv');
assert.equal(errors.length, 0, '浏览器不应有控制台/运行时错误:' + errors.join('\n'));
console.log('PASS 本地保存网页自动识别与浏览器错误检查');