From f0bb99d5cdf8394134bd8640938eb353284b699a Mon Sep 17 00:00:00 2001 From: codex Date: Sun, 1 Nov 2026 15:59:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=94=A8=E6=88=B7=20VPC=20?= =?UTF-8?q?=E9=A1=B5=E7=AD=BE=E6=97=A0=E5=88=B7=E6=96=B0=E5=88=87=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/public/cloud/vpcs.js | 10 ++++-- web/resources/views/venix/layout_assets.tpl | 2 +- web/tests/CloudVpcPageBehaviorTest.mjs | 40 ++++++++++++++++++++- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/web/public/cloud/vpcs.js b/web/public/cloud/vpcs.js index 8499b9e0..9b8f0fdc 100644 --- a/web/public/cloud/vpcs.js +++ b/web/public/cloud/vpcs.js @@ -221,6 +221,10 @@ function membersHref(vpc, joining = false) { return `/user/cloud/vpcs?vpc=${encodeURIComponent(vpcKey(vpc))}${joining ? '&view=join' : ''}`; } +function membersRouteHref(vpc, joining = false) { + return `#/${encodeURIComponent(vpcKey(vpc))}/instances${joining ? '/join' : ''}`; +} + function currentVpc() { const route = pageRoute(); return state.items.find(vpc => vpcKey(vpc) === route.key) || null; @@ -470,7 +474,7 @@ function renderInstanceManager(vpc, joining) { section.append(joining ? emptyState('暂无可加入的实例', '只显示同地区且尚未加入 VPC 的实例。') : emptyState('尚未加入实例', '将同地区实例连接到这张私网。', - membersHref(vpc, true), '加入实例 →')); + membersRouteHref(vpc, true), '加入实例 →')); return section; } let search = null; @@ -607,12 +611,12 @@ function renderMembers(vpc, joining) { const members = byId('cloud-vpc-members-link'); members.replaceChildren(element('span', '', '已加入实例'), element('span', 'cloud-vpc-tab-count', instancesForVpc(vpc).length)); - members.href = membersHref(vpc); + members.href = membersRouteHref(vpc); const join = byId('cloud-vpc-join-link'); const joinable = candidatesForVpc(vpc).filter(instance => instance.can_attach === true).length; join.replaceChildren(element('span', '', '加入实例'), element('span', 'cloud-vpc-tab-count', joinable)); - join.href = membersHref(vpc, true); + join.href = membersRouteHref(vpc, true); for (const [link, active] of [[members, !joining], [join, joining]]) { if (active) link.setAttribute('aria-current', 'page'); else link.removeAttribute('aria-current'); diff --git a/web/resources/views/venix/layout_assets.tpl b/web/resources/views/venix/layout_assets.tpl index 80b4f894..b428ff1c 100644 --- a/web/resources/views/venix/layout_assets.tpl +++ b/web/resources/views/venix/layout_assets.tpl @@ -1,2 +1,2 @@ {* Shared Venix CSS/JS cache key. Increment whenever a shared browser asset changes. *} -{assign var=venix_asset_version value='2026110109' scope='global'} +{assign var=venix_asset_version value='2026110110' scope='global'} diff --git a/web/tests/CloudVpcPageBehaviorTest.mjs b/web/tests/CloudVpcPageBehaviorTest.mjs index 4d89f1a8..eb5f6dbc 100644 --- a/web/tests/CloudVpcPageBehaviorTest.mjs +++ b/web/tests/CloudVpcPageBehaviorTest.mjs @@ -47,8 +47,17 @@ class Node { this.listeners.get(name).push(listener); } async fire(name) { - await Promise.all((this.listeners.get(name) || []).map(fn => fn({ target: this, preventDefault() {} }))); + const event = { + target: this, + defaultPrevented: false, + preventDefault() { this.defaultPrevented = true; }, + }; + await Promise.all((this.listeners.get(name) || []).map(fn => fn(event))); await tick(); + if (name === 'click' && !event.defaultPrevented && this.tagName === 'A' && this.href) { + await this.document.navigate(this.href); + } + return event; } focus() { this.document.activeElement = this; } scrollIntoView() {} @@ -96,6 +105,20 @@ async function setup(hash = '#/', payload = fixtures()) { const location = new URL(`https://panel.example.test/user/cloud/vpcs${hash}`); const window = { location, VenixDialog: { confirm: async () => true }, addEventListener: (name, listener) => events.set(name, listener) }; + let fullPageNavigations = 0; + document.navigate = async target => { + const next = new URL(target, location.href); + if (next.origin === location.origin && next.pathname === location.pathname + && next.search === location.search) { + if (next.hash !== location.hash) { + location.hash = next.hash; + events.get('hashchange')?.(); + await tick(); + } + return; + } + fullPageNavigations += 1; + }; let data = structuredClone(payload); let respond = async (url, options) => { if (!options.method) return structuredClone(data); @@ -127,6 +150,7 @@ async function setup(hash = '#/', payload = fixtures()) { await context.page.initialize(); return { nodes, calls, observations, storage, location, data: () => data, + fullPageNavigations: () => fullPageNavigations, update: next => { data = structuredClone(next); context.page.apply(structuredClone(next)); }, respond: fn => { respond = fn; }, event: (name, event = {}) => events.get(name)?.(event), @@ -185,6 +209,20 @@ for (const [query, page, joining] of [ assert.equal(t.nodes.get('cloud-vpcs-home').hidden, false); assert.equal(mutations(t).length, 0, 'Navigation must never mutate a VPC'); } +{ + const t = await setup(`?vpc=${A}`); + await t.nodes.get('cloud-vpc-join-link').fire('click'); + assert.equal(t.fullPageNavigations(), 0, + 'Switching to joinable instances must not trigger a full-page navigation'); + assert.equal(t.nodes.get('cloud-vpc-instance-manager').querySelector('section') + .classList.contains('joining'), true); + await t.nodes.get('cloud-vpc-members-link').fire('click'); + assert.equal(t.fullPageNavigations(), 0, + 'Switching to attached instances must not trigger a full-page navigation'); + assert.equal(t.nodes.get('cloud-vpc-instance-manager').querySelector('section') + .classList.contains('joining'), false); + assert.equal(mutations(t).length, 0, 'Tab switching must not mutate a VPC'); +} { const t = await setup(`${href(A)}/join`); const search = control(t, 'instance-search');