From 6e6277d521284039d5009afee1cd7573cf9069ca Mon Sep 17 00:00:00 2001 From: Sycun <165354365+Sycun@users.noreply.github.com> Date: Fri, 13 Nov 2026 21:02:11 +0800 Subject: [PATCH] fix(tasks): load goal task controller from the app entrypoint --- scripts/long-tasks-browser.test.cjs | 29 ++++++++++++++++---- web/static/css/long-tasks.css | 3 ++ web/static/js/long-tasks-entrypoint.test.cjs | 20 ++++++++++++++ web/static/js/long-tasks.js | 5 ++-- web/templates/index.html | 9 +++--- 5 files changed, 55 insertions(+), 11 deletions(-) create mode 100644 web/static/js/long-tasks-entrypoint.test.cjs diff --git a/scripts/long-tasks-browser.test.cjs b/scripts/long-tasks-browser.test.cjs index fd601580..a4d148bb 100644 --- a/scripts/long-tasks-browser.test.cjs +++ b/scripts/long-tasks-browser.test.cjs @@ -13,17 +13,26 @@ const read = f => fs.readFileSync(path.join(root, f), 'utf8'); test('goal task page: authenticated controls, stable polling, XSS, report and responsive layout', async () => { const template = read('web/templates/index.html'); const pageHTML = template.slice(template.indexOf('
')).replace('class="page"','class="page active"'); + // Load exactly the entrypoint's scripts in their original order. Do not inject + // long-tasks.js independently: that hid a missing production script tag. + const scriptTags = [...template.matchAll(/]*\bsrc=["']([^"']+)["'][^>]*><\/script>/g)] + .filter(match => /^\/static\/js\/(router|auth|tasks|long-tasks)\.js(?:\?|$)/.test(match[1])) + .map(match => match[0]).join(''); + const styleTags = [...template.matchAll(/]*\bhref=["']([^"']+)["'][^>]*>/g)] + .filter(match => /^\/static\/css\/(style|mobile|long-tasks)\.css(?:\?|$)/.test(match[1])) + .map(match => match[0]).join(''); const locale = JSON.parse(read('web/static/i18n/zh-CN.json')); const task = { id: 'fixture', name: '本地材料整理 ', status: 'draft', revision: 0, objective: 'Produce a verified report', scope: 'Local fixture only', criteria: 'Evidence and gaps', strategy: '', summary: '', steps: [], guidance: [], question: '', rounds: 0, max_rounds: 40, remaining_seconds: 3600, updated_at: new Date().toISOString() }; const authenticated = [], actions = []; - const server = http.createServer(async (req, res) => { - if (req.url.startsWith('/static/')) { const file = path.join(root, 'web', req.url); res.setHeader('Content-Type', file.endsWith('.css') ? 'text/css; charset=utf-8' : 'text/javascript; charset=utf-8'); return res.end(fs.readFileSync(file)); } + const server = http.createServer(async (req, res) => { + if (req.url.startsWith('/static/')) { const file = path.join(root, 'web', new URL(req.url, 'http://fixture').pathname); res.setHeader('Content-Type', file.endsWith('.css') ? 'text/css; charset=utf-8' : 'text/javascript; charset=utf-8'); return res.end(fs.readFileSync(file)); } if (req.url === '/') { res.setHeader('Content-Type', 'text/html; charset=utf-8'); - const html = `
${pageHTML}
`; return res.end(html); + const html = `${styleTags}
${pageHTML}
${scriptTags}`; return res.end(html); } if (req.url === '/api/auth/validate') { res.setHeader('Content-Type','application/json'); return res.end('{}'); } if (req.url === '/favicon.ico') { res.statusCode=204; return res.end(); } + if (req.url.startsWith('/api/batch-tasks')) { res.setHeader('Content-Type','application/json'); return res.end(JSON.stringify({queues:[],total:0,total_pages:1})); } if (req.url.startsWith('/api/long-tasks')) { authenticated.push(req.headers.authorization); if (req.headers.authorization !== 'Bearer fixture-token') { res.statusCode = 401; return res.end('{}'); } @@ -54,7 +63,11 @@ test('goal task page: authenticated controls, stable polling, XSS, report and re const errors = []; page.on('pageerror', e => { errors.push(e.message); process.stderr.write('PAGE: ' + e.stack + '\n'); }); page.on('console', m => { if (m.type() === 'error' || m.type() === 'warning') process.stderr.write('CONSOLE: ' + m.text() + '\n'); }); page.setDefaultTimeout(10000); - await page.goto('http://127.0.0.1:' + server.address().port); + await page.goto('http://127.0.0.1:' + server.address().port + '/#tasks'); + await page.waitForFunction(() => document.getElementById('long-task-workspace').dataset.initialized === 'true'); + await page.evaluate(() => applyRBACToUI(document.getElementById('page-tasks'))); + assert.equal(await page.locator('#task-new-goal').isVisible(), true); + assert.equal(await page.locator('#task-new-batch').isVisible(), false); await page.locator('[data-lt-action=select]').click(); await page.locator('#lt-name').waitFor(); await page.waitForFunction(() => document.getElementById('lt-name').textContent.includes('本地材料')); @@ -95,7 +108,13 @@ test('goal task page: authenticated controls, stable polling, XSS, report and re await page.locator('[data-task-tab=batch]').click(); assert.equal(await page.locator('#long-task-workspace').isVisible(), false); assert.equal(await page.locator('#task-batch-workspace').isVisible(), true); - await page.evaluate(() => cleanupLongTasks()); + await page.evaluate(() => applyRBACToUI(document.getElementById('page-tasks'))); + assert.equal(await page.locator('#task-new-goal').isVisible(), false); + assert.equal(await page.locator('#task-new-batch').isVisible(), true); + await page.evaluate(() => { saveAuth('fixture-token',new Date(Date.now()+3600000),{permissions:['tasks:read'],scope:'own'}); applyRBACToUI(); }); + await page.locator('[data-task-tab=goals]').click(); + assert.equal(await page.locator('#task-new-goal').isVisible(), false, 'tab switches must preserve denied write permission'); + await page.evaluate(() => cleanupTasksPage()); assert.equal(authenticated.every(a => a === 'Bearer fixture-token'), true); assert.ok(actions.includes('create') && actions.includes('report') && actions.includes('pause')); assert.deepEqual(errors, []); diff --git a/web/static/css/long-tasks.css b/web/static/css/long-tasks.css index e234b6c3..e747cfd8 100644 --- a/web/static/css/long-tasks.css +++ b/web/static/css/long-tasks.css @@ -61,3 +61,6 @@ #page-tasks .btn-primary { color:var(--bg-primary,#fff); background:var(--accent-color,#2563eb); } #task-new-goal[hidden],#task-new-batch[hidden] { display:none !important; } .lt-status-cleanup_unconfirmed { background:#fee2e2; color:#b91c1c; } + +#page-tasks[data-task-mode="goals"] #task-new-batch, +#page-tasks[data-task-mode="batch"] #task-new-goal { display:none !important; } diff --git a/web/static/js/long-tasks-entrypoint.test.cjs b/web/static/js/long-tasks-entrypoint.test.cjs new file mode 100644 index 00000000..18f99d0f --- /dev/null +++ b/web/static/js/long-tasks-entrypoint.test.cjs @@ -0,0 +1,20 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); + +const web = path.resolve(__dirname, '../..'); +const template = fs.readFileSync(path.join(web, 'templates/index.html'), 'utf8'); + +test('the production task page loads its goal-task controller and styles', () => { + const scripts = [...template.matchAll(/]*\bsrc=["']([^"']+)["'][^>]*>/g)] + .map(match => new URL(match[1], 'http://fixture').pathname); + for (const script of ['router', 'tasks', 'long-tasks']) { + const url = `/static/js/${script}.js`; + assert.equal(scripts.filter(src => src === url).length, 1, `${url} must be included once by the real entrypoint`); + assert.ok(fs.existsSync(path.join(web, url)), `${url} must exist`); + } + const styles = [...template.matchAll(/]*\bhref=["']([^"']+)["'][^>]*>/g)] + .map(match => new URL(match[1], 'http://fixture').pathname); + assert.ok(styles.includes('/static/css/long-tasks.css')); +}); diff --git a/web/static/js/long-tasks.js b/web/static/js/long-tasks.js index 132580e0..cf19c1b3 100644 --- a/web/static/js/long-tasks.js +++ b/web/static/js/long-tasks.js @@ -19,8 +19,9 @@ state.tab = tab; el('long-task-workspace').hidden = tab !== 'goals'; el('task-batch-workspace').hidden = tab !== 'batch'; - el('task-new-goal').hidden = tab !== 'goals'; - el('task-new-batch').hidden = tab !== 'batch'; + // Page mode and RBAC own separate visibility rules. Permission refreshes + // must not reveal the other mode's button, or tab switches reveal denied controls. + el('page-tasks').dataset.taskMode = tab; document.querySelectorAll('[data-task-tab]').forEach(b => b.setAttribute('aria-selected', String(b.dataset.taskTab === tab))); if (tab === 'goals') refresh(); } diff --git a/web/templates/index.html b/web/templates/index.html index 6770fecf..dd6059e6 100644 --- a/web/templates/index.html +++ b/web/templates/index.html @@ -50,7 +50,7 @@ - +