Files
DashBoard/frontend/tests/portal-shell-sidebar.test.js
egg 7cb0985b12 feat(modernization): full architecture blueprint with hardening follow-up
Implement phased modernization infrastructure for transitioning from
multi-page legacy routing to SPA portal-shell architecture, plus
post-delivery hardening fixes for policy loading, fallback consistency,
and governance drift detection.

Key changes:
- Add route contract enrichment with scope/visibility/compatibility policies
- Canonical 302 redirects from legacy direct-entry to /portal-shell/ routes
- Asset readiness enforcement and runtime fallback retirement for in-scope routes
- Shared feature-flag helpers (env > config > default) replacing duplicated _to_bool
- Defensive copy for lru_cached policy payloads preventing mutation corruption
- Unified retired-fallback response helper across app and blueprint routes
- Frontend/backend route-contract cross-validation in governance gates
- Shell CSS token fallback values for routes rendered outside shell scope
- Local-safe .env.example defaults with production recommendation comments
- Legacy contract fallback warning logging and single-hop redirect optimization

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-12 11:26:02 +08:00

97 lines
3.4 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { existsSync, readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import {
SIDEBAR_STORAGE_KEY,
buildSidebarUiState,
parseSidebarCollapsedPreference,
serializeSidebarCollapsedPreference,
} from '../src/portal-shell/sidebarState.js';
function readSource(relativePath) {
const directPath = resolve(process.cwd(), relativePath);
if (existsSync(directPath)) {
return readFileSync(directPath, 'utf8');
}
return readFileSync(resolve(process.cwd(), 'frontend', relativePath), 'utf8');
}
test('buildSidebarUiState marks desktop collapse correctly', () => {
const expanded = buildSidebarUiState({
isMobile: false,
sidebarCollapsed: false,
sidebarMobileOpen: false,
});
assert.equal(expanded.sidebarClass['sidebar--collapsed'], false);
assert.equal(expanded.ariaExpanded, 'true');
const collapsed = buildSidebarUiState({
isMobile: false,
sidebarCollapsed: true,
sidebarMobileOpen: false,
});
assert.equal(collapsed.sidebarClass['sidebar--collapsed'], true);
assert.equal(collapsed.sidebarClass['sidebar--mobile-open'], false);
assert.equal(collapsed.ariaExpanded, 'false');
});
test('buildSidebarUiState marks mobile overlay states correctly', () => {
const closed = buildSidebarUiState({
isMobile: true,
sidebarCollapsed: true,
sidebarMobileOpen: false,
});
assert.equal(closed.sidebarClass['sidebar--mobile-closed'], true);
assert.equal(closed.sidebarClass['sidebar--collapsed'], false);
assert.equal(closed.ariaExpanded, 'false');
const open = buildSidebarUiState({
isMobile: true,
sidebarCollapsed: true,
sidebarMobileOpen: true,
});
assert.equal(open.sidebarClass['sidebar--mobile-open'], true);
assert.equal(open.sidebarClass['sidebar--mobile-closed'], false);
assert.equal(open.ariaExpanded, 'true');
});
test('sidebar collapsed preference serializes and parses as expected', () => {
assert.equal(serializeSidebarCollapsedPreference(true), 'true');
assert.equal(serializeSidebarCollapsedPreference(false), 'false');
assert.equal(parseSidebarCollapsedPreference('true'), true);
assert.equal(parseSidebarCollapsedPreference('false'), false);
assert.equal(parseSidebarCollapsedPreference(null), false);
});
test('portal shell template uses shell-content and overlay close wiring', () => {
const source = readSource('src/portal-shell/App.vue');
assert.match(source, /<section class="shell-content">/);
assert.doesNotMatch(source, /<section class="content">/);
assert.match(source, /class="sidebar-overlay"/);
assert.match(source, /@click="closeMobileSidebar"/);
assert.match(source, /event\.key === 'Escape'/);
assert.match(source, /SIDEBAR_STORAGE_KEY/);
});
test('toggleSidebar keeps desktop collapse and persistence wiring', () => {
const source = readSource('src/portal-shell/App.vue');
assert.match(source, /function toggleSidebar\(\)/);
assert.match(source, /sidebarCollapsed\.value = !sidebarCollapsed\.value/);
assert.match(source, /persistSidebarPreference\(\)/);
});
test('sessionStorage preference restore wiring exists', () => {
const source = readSource('src/portal-shell/App.vue');
assert.match(source, /function restoreSidebarPreference\(\)/);
assert.match(source, /window\.sessionStorage\.getItem\(SIDEBAR_STORAGE_KEY\)/);
});
test('sidebar sessionStorage key remains stable', () => {
assert.equal(SIDEBAR_STORAGE_KEY, 'portal-shell:sidebar-collapsed');
});