mirror of
https://github.com/Studio-Saelix/sencho.git
synced 2026-08-20 07:13:05 +00:00
feat(nodes): hide hub-only views when active node is remote (#1007)
* feat(nodes): hide hub-only views when active node is remote Fleet, Schedules, Audit, Logs, and Auto-Update operate on hub-owned state (node registry, fleet schedules, centralized audit, fleet-wide log aggregation, fleet-wide update preview). When the active node is remote, proxying those surfaces would show that remote's own disconnected state instead of the hub's. Hide them from the nav strip and force-redirect to Home if one was open during the node switch. Backend hubOnlyGuard middleware sits between nodeContextMiddleware and the remote proxy and rejects /api/scheduled-tasks, /api/audit-log, and /api/notification-routes with 403 + HUB_ONLY_ENDPOINT when nodeId resolves to a remote, closing the script-bypass path the UI gating cannot reach. Settings sub-sections were already gated via the hiddenOnRemote registry; this extends the same model to top-level views. * docs(nodes): note hub-only visibility on Fleet, Schedules, Audit, Logs, Auto-Update Each of the five hub-only feature pages now points readers to the canonical "What top-level views show when a remote node is active" section in multi-node.mdx, so users landing directly on a feature page understand why the nav item disappears when they switch to a remote node.
This commit is contained in:
@@ -2,11 +2,19 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { renderHook, act } from '@testing-library/react';
|
||||
import * as AuthContext from '@/context/AuthContext';
|
||||
import * as LicenseContext from '@/context/LicenseContext';
|
||||
import * as NodeContext from '@/context/NodeContext';
|
||||
import { SENCHO_NAVIGATE_EVENT } from '@/components/NodeManager';
|
||||
import { useViewNavigationState } from '../hooks/useViewNavigationState';
|
||||
|
||||
vi.mock('@/context/AuthContext');
|
||||
vi.mock('@/context/LicenseContext');
|
||||
vi.mock('@/context/NodeContext');
|
||||
|
||||
function mockActiveNode(type: 'local' | 'remote' | null) {
|
||||
vi.mocked(NodeContext.useNodes).mockReturnValue({
|
||||
activeNode: type === null ? null : { type, id: 1, name: 'n' },
|
||||
} as unknown as ReturnType<typeof NodeContext.useNodes>);
|
||||
}
|
||||
|
||||
function mockCommunityUser() {
|
||||
vi.mocked(AuthContext.useAuth).mockReturnValue({
|
||||
@@ -44,6 +52,7 @@ function mockSkipperAdmin() {
|
||||
describe('useViewNavigationState', () => {
|
||||
beforeEach(() => {
|
||||
mockCommunityUser();
|
||||
mockActiveNode('local');
|
||||
});
|
||||
|
||||
// ── initial state ──────────────────────────────────────────────────────────
|
||||
@@ -227,4 +236,85 @@ describe('useViewNavigationState', () => {
|
||||
expect(values).not.toContain('audit-log');
|
||||
expect(values).not.toContain('scheduled-ops');
|
||||
});
|
||||
|
||||
// ── navItems: hub-only gating on remote node ───────────────────────────────
|
||||
|
||||
it('hides hub-only views from the nav strip when active node is remote', () => {
|
||||
mockAdmiralAdmin();
|
||||
mockActiveNode('remote');
|
||||
const { result } = renderHook(() => useViewNavigationState());
|
||||
const values = result.current.navItems.map(i => i.value);
|
||||
expect(values).not.toContain('fleet');
|
||||
expect(values).not.toContain('scheduled-ops');
|
||||
expect(values).not.toContain('audit-log');
|
||||
expect(values).not.toContain('global-observability');
|
||||
expect(values).not.toContain('auto-updates');
|
||||
// Node-level views remain visible.
|
||||
expect(values).toContain('dashboard');
|
||||
expect(values).toContain('resources');
|
||||
expect(values).toContain('templates');
|
||||
expect(values).toContain('host-console');
|
||||
});
|
||||
|
||||
it('shows hub-only views again when active node switches back to local', () => {
|
||||
mockAdmiralAdmin();
|
||||
mockActiveNode('remote');
|
||||
const { result, rerender } = renderHook(() => useViewNavigationState());
|
||||
expect(result.current.navItems.map(i => i.value)).not.toContain('fleet');
|
||||
|
||||
mockActiveNode('local');
|
||||
rerender();
|
||||
const values = result.current.navItems.map(i => i.value);
|
||||
expect(values).toContain('fleet');
|
||||
expect(values).toContain('scheduled-ops');
|
||||
expect(values).toContain('audit-log');
|
||||
});
|
||||
|
||||
// ── auto-redirect when on a hub-only view and node switches to remote ──────
|
||||
|
||||
it('auto-redirects to dashboard when active view is hub-only and node becomes remote', () => {
|
||||
const onNavigateToDashboard = vi.fn();
|
||||
mockAdmiralAdmin();
|
||||
mockActiveNode('local');
|
||||
const { result, rerender } = renderHook(() =>
|
||||
useViewNavigationState({ onNavigateToDashboard }),
|
||||
);
|
||||
|
||||
act(() => {
|
||||
window.dispatchEvent(
|
||||
new CustomEvent(SENCHO_NAVIGATE_EVENT, { detail: { view: 'fleet', nodeId: 7 } }),
|
||||
);
|
||||
});
|
||||
expect(result.current.activeView).toBe('fleet');
|
||||
expect(result.current.filterNodeId).toBe(7);
|
||||
|
||||
mockActiveNode('remote');
|
||||
rerender();
|
||||
|
||||
expect(result.current.activeView).toBe('dashboard');
|
||||
expect(result.current.filterNodeId).toBeNull();
|
||||
expect(onNavigateToDashboard).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it('does not redirect when a non-hub-only view is active and node becomes remote', () => {
|
||||
const onNavigateToDashboard = vi.fn();
|
||||
mockAdmiralAdmin();
|
||||
mockActiveNode('local');
|
||||
const { result, rerender } = renderHook(() =>
|
||||
useViewNavigationState({ onNavigateToDashboard }),
|
||||
);
|
||||
|
||||
act(() => {
|
||||
window.dispatchEvent(
|
||||
new CustomEvent(SENCHO_NAVIGATE_EVENT, { detail: { view: 'resources' } }),
|
||||
);
|
||||
});
|
||||
expect(result.current.activeView).toBe('resources');
|
||||
|
||||
mockActiveNode('remote');
|
||||
rerender();
|
||||
|
||||
expect(result.current.activeView).toBe('resources');
|
||||
expect(onNavigateToDashboard).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user