const assert = require('node:assert/strict') const { chromium } = require('./vue3-test-runtime.cjs') const base = process.env.VUE3_URL || 'http://127.0.0.1:8088' const backend = process.env.BACKEND_URL || 'http://127.0.0.1:8080' ;(async () => { const login = await (await fetch(backend + '/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username: process.env.TEST_USERNAME || 'admin', password: process.env.TEST_PASSWORD || 'admin123', code: '', uuid: '' }) })).json() assert.equal(login.code, 200) const result = await (await fetch(backend + '/project/info/options?includeArchived=true', { headers: { Authorization: 'Bearer ' + login.token } })).json() assert.equal(result.code, 200) const projects = (result.data || []).filter(row => Number.isSafeInteger(row.id) && row.id > 0).slice(0, 2) assert.equal(projects.length, 2, '需要至少两个现有项目进行缓存隔离回归;测试不会创建项目') const browser = await chromium.launch({ headless: true }) try { const context = await browser.newContext({ viewport: { width: 1440, height: 900 } }) await context.addCookies([{ name: 'Admin-Token', value: login.token, url: base }]) const page = await context.newPage(), errors = [], invalidRequests = [], writes = [] page.setDefaultTimeout(12000) page.on('pageerror', error => errors.push(error.message)) await page.route('**/dev-api/**', route => { const request = route.request(), path = new URL(request.url()).pathname if (!['GET', 'HEAD'].includes(request.method())) { writes.push(path); return route.abort('blockedbyclient') } if (/\/(NaN|undefined|null)(\/|$)/.test(path)) { invalidRequests.push(path); return route.fulfill({ json: { code: 500, msg: '回归拦截:非法项目编号' } }) } return route.continue() }) const navigate = target => page.locator('.desktop-viewport').evaluate((el, target) => el.__vueParentComponent.proxy.$router.push(target), target) async function projectReady(project) { await page.waitForFunction(project => { const workspace = document.querySelector('.workspace-heading')?.__vueParentComponent?.proxy return workspace && !workspace.loading && workspace.project.id === project.id && workspace.project.projectName === project.projectName }, project) await page.locator('.workspace-heading').evaluate((el, project) => { window.__projectCacheRegression ||= {} const workspace = el.__vueParentComponent.proxy window.__projectCacheRegression[project.id] = workspace }, project) assert.equal(await page.locator('.workspace-heading strong').innerText(), project.projectName) } const [first, second] = projects await page.goto(base + '/project/detail/' + first.id) await projectReady(first) await navigate('/index') await page.locator('.business-home').waitFor() assert.equal(await page.evaluate(id => window.__projectCacheRegression[id].project.id, first.id), first.id) await navigate('/project/detail/' + first.id) await projectReady(first) console.log('PASS project detail → home → same project; no invalid ID or blank cached page') await navigate('/project/detail/' + second.id) await projectReady(second) assert.equal(await page.evaluate(id => window.__projectCacheRegression[id].project.id, first.id), first.id) await navigate('/index') await page.locator('.business-home').waitFor() await navigate('/project/detail/' + first.id) await projectReady(first) await navigate('/project/detail/' + second.id) await projectReady(second) const cache = await page.evaluate(ids => ids.map(id => ({ expected: id, actual: window.__projectCacheRegression[id].project.id })), projects.map(row => row.id)) for (const row of cache) assert.equal(row.actual, row.expected) console.log('PASS switching between different project tabs preserves each cached project identity') const before = await page.evaluate(id => { const workspace = window.__projectCacheRegression[id] workspace.__cacheDraft = 'unsaved local input' return workspace.$.uid }, second.id) await navigate('/index') await page.locator('.business-home').waitFor() await page.locator('.desktop-viewport').evaluate(async (el, id) => { const app = el.__vueParentComponent.appContext.config.globalProperties const tags = app.$pinia._s.get('tags-view') await app.$tab.closePage(tags.visitedViews.find(view => view.path === '/project/detail/' + id)) }, first.id) const released = await page.evaluate(ids => ids.map(id => window.__projectCacheRegression[id].$.isUnmounted), [first.id, second.id]) assert.deepEqual(released, [true, false], 'Only the closed project instance is released') await navigate('/project/detail/' + second.id) await projectReady(second) const after = await page.evaluate(id => ({ uid: window.__projectCacheRegression[id].$.uid, draft: window.__projectCacheRegression[id].__cacheDraft }), second.id) assert.equal(after.uid, before) assert.equal(after.draft, 'unsaved local input') console.log('PASS closing project A precisely releases A and preserves project B state') assert.deepEqual(invalidRequests, []) assert.deepEqual(errors, []) assert.deepEqual(writes, []) console.log('PROJECT DETAIL REGRESSION PASS (all business writes blocked; existing projects only)') } finally { await browser.close() } })().catch(error => { console.error(error); process.exitCode = 1 })