Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
console: make the search box work and guard unsaved policy edits
Antonio Ojea03a2890a9e5e
diff --git a/cmd/sam-console/public/app.js b/cmd/sam-console/public/app.jsindex 9d257dc4..95f9a592 100644--- a/cmd/sam-console/public/app.js+++ b/cmd/sam-console/public/app.js@@ -3,6 +3,9 @@ const REFRESH_INTERVAL_MS = 15000;let refreshTimer = null;let refreshInFlight = false;+// Last policy the server confirmed, so an edit is never silently overwritten by+// a background refresh or thrown away by a stray click.+let policyBaseline = '';document.addEventListener('DOMContentLoaded', () => {// Navigation handling@@ -21,10 +24,55 @@ document.addEventListener('DOMContentLoaded', () => {window.addEventListener('hashchange', () => switchTab(tabFromHash()));switchTab(tabFromHash());+ document.getElementById('resource-search').addEventListener('input', applySearchFilter);++ // Warn before a reload or a close discards an in-progress policy edit.+ window.addEventListener('beforeunload', (e) => {+ if (policyIsDirty()) {+ e.preventDefault();+ e.returnValue = '';+ }+ });+// Check auth status on loadcheckAuthAndLoad();});+// Filters every rendered table and router card, so the result stays consistent+// when the operator switches views or a background refresh re-renders a table.+function applySearchFilter() {+ const input = document.getElementById('resource-search');+ const raw = input.value.trim();+ const query = raw.toLowerCase();++ document.querySelectorAll('.data-table tbody').forEach(tbody => {+ tbody.querySelectorAll('tr[data-filter-empty]').forEach(tr => tr.remove());++ const rows = Array.from(tbody.querySelectorAll('tr'));+ const matches = rows.filter(tr => {+ const match = !query || tr.textContent.toLowerCase().includes(query);+ tr.hidden = !match;+ return match;+ });++ if (query && rows.length > 0 && matches.length === 0) {+ const table = tbody.closest('table');+ const tr = document.createElement('tr');+ tr.dataset.filterEmpty = 'true';+ const td = document.createElement('td');+ td.colSpan = table.querySelectorAll('thead th').length || 1;+ td.className = 'text-center';+ td.textContent = `No rows match "${raw}"`;+ tr.appendChild(td);+ tbody.appendChild(tr);+ }+ });++ document.querySelectorAll('.router-item-card').forEach(card => {+ card.hidden = query !== '' && !card.textContent.toLowerCase().includes(query);+ });+}+function tabFromHash() {const raw = window.location.hash.replace(/^#/, '');let target = raw;@@ -200,13 +248,15 @@ async function loadData() {renderRoutersTable(data.active_routers || []);renderRouterTopography(data.active_routers || []);renderBootstrapTokensTable(data.bootstrap_tokens || []);-- // Populate policy yaml if empty+const policyArea = document.getElementById('policy-yaml');- if (policyArea && !policyArea.value && data.policy_yaml) {- policyArea.value = data.policy_yaml;+ if (policyArea && data.policy_yaml !== undefined && !policyIsDirty()) {+ policyArea.value = data.policy_yaml || '';+ policyBaseline = policyArea.value;}+ applySearchFilter();+} catch (error) {console.error('Failed to load dashboard data:', error);const errMsg = `Error loading data: ${error.message}`;@@ -433,6 +483,11 @@ function updateUIForRole(role, userId) {}}+function policyIsDirty() {+ const area = document.getElementById('policy-yaml');+ return area !== null && area.value !== policyBaseline;+}+function switchTab(target) {const navItems = document.querySelectorAll('.nav-item');const viewSections = document.querySelectorAll('.view-section');@@ -444,6 +499,17 @@ function switchTab(target) {activeNav = Array.from(navItems).find(n => n.getAttribute('data-target') === target);}+ const onPolicy = document.getElementById('view-policy').classList.contains('active');+ if (onPolicy && target !== 'policy' && policyIsDirty()) {+ if (!confirm('You have unsaved policy changes. Leave without saving?')) {+ // The hash may already have moved, e.g. via the back button.+ if (tabFromHash() !== 'policy') {+ window.location.hash = 'policy';+ }+ return;+ }+ }+navItems.forEach(n => n.classList.remove('active'));viewSections.forEach(v => v.classList.remove('active'));@@ -619,6 +685,7 @@ window.savePolicy = async function() {headers: headers,body: pbBytes}); if (response.ok) {+ policyBaseline = yamlContent;alert('Mesh policy updated and applied successfully!');loadData();} else {diff --git a/cmd/sam-console/public/index.html b/cmd/sam-console/public/index.htmlindex e07f364c..c21e0858 100644--- a/cmd/sam-console/public/index.html+++ b/cmd/sam-console/public/index.html@@ -101,7 +101,7 @@</button><div class="search-bar"><svg viewBox="0 0 24 24" width="18" height="18" stroke="currentColor" stroke-width="2" fill="none"><circle cx="11" cy="11" r="8"></circle><line x1="21" y1="21" x2="16.65" y2="16.65"></line></svg>- <input type="text" placeholder="Search resources...">+ <input type="search" id="resource-search" placeholder="Search resources..." aria-label="Search resources"></div><div class="user-profile"><div class="avatar">A</div>diff --git a/tests/ui/console.spec.js b/tests/ui/console.spec.jsindex 6a22df2a..1ef8d52f 100644--- a/tests/ui/console.spec.js+++ b/tests/ui/console.spec.js@@ -143,3 +143,58 @@ test('an unknown hash falls back to the overview', async ({ page }) => {await page.goto('/#not-a-view');await expect(page.locator('#view-overview')).toBeVisible();});++test('the search box filters rendered rows', async ({ page }) => {+ await login(page);+ await page.click('.nav-item[data-target="bootstrap"]');++ // Guarantee at least one row to filter, independent of test ordering.+ await page.click('#form-generate-token button[type="submit"]');+ await expect(page.locator('#table-bootstrap')).toContainText('root-admin');++ await page.fill('#resource-search', 'root-admin');+ await expect(page.locator('#table-bootstrap tr:visible')).not.toHaveCount(0);+ await expect(page.locator('#table-bootstrap tr[data-filter-empty]')).toHaveCount(0);++ await page.fill('#resource-search', 'zzz-no-such-resource');+ await expect(page.locator('#table-bootstrap tr[data-filter-empty]')).toContainText('No rows match');++ await page.fill('#resource-search', '');+ await expect(page.locator('#table-bootstrap tr[data-filter-empty]')).toHaveCount(0);+ await expect(page.locator('#table-bootstrap')).toContainText('root-admin');+});++// The policy textarea is the one place in the console where a stray click can+// destroy work that exists nowhere else.+test('leaving the policy view warns about unsaved edits', async ({ page }) => {+ await login(page);+ await page.click('.nav-item[data-target="policy"]');+ await page.fill('#policy-yaml', '# unsaved edit\n');++ let message = '';+ page.once('dialog', async (d) => {+ message = d.message();+ await d.dismiss();+ });+ await page.click('.nav-item[data-target="overview"]');+ await expect(page.locator('#view-policy')).toBeVisible();+ expect(message).toContain('unsaved policy changes');++ page.once('dialog', (d) => d.accept());+ await page.click('.nav-item[data-target="overview"]');+ await expect(page.locator('#view-overview')).toBeVisible();+});++test('an unedited policy view navigates away without prompting', async ({ page }) => {+ await login(page);+ await page.click('.nav-item[data-target="policy"]');++ let prompted = false;+ page.once('dialog', async (d) => {+ prompted = true;+ await d.dismiss();+ });+ await page.click('.nav-item[data-target="overview"]');+ await expect(page.locator('#view-overview')).toBeVisible();+ expect(prompted).toBe(false);+});