From 5df61df5b948a60aadd7c2ded167a85979f7e405 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=83=E5=B7=9D=E7=99=BD=E6=B5=AA?= <1679659@qq.com> Date: Thu, 3 Dec 2026 22:02:53 -0700 Subject: [PATCH] test(frontend): reject import cycles The theme engine and the interactive UI SDK imported each other's types; the SDK now owns the types of its own interface. Co-Authored-By: Claude Opus 5.5 --- frontend/scripts/test-import-cycles.mjs | 45 +++++++++++++++++++++++++ frontend/src/App.tsx | 3 +- frontend/src/lib/themeEngine.ts | 22 +----------- frontend/src/lib/uiSdk.ts | 22 +++++++++++- frontend/src/ui/themeVisuals.tsx | 3 +- 5 files changed, 71 insertions(+), 24 deletions(-) create mode 100644 frontend/scripts/test-import-cycles.mjs diff --git a/frontend/scripts/test-import-cycles.mjs b/frontend/scripts/test-import-cycles.mjs new file mode 100644 index 0000000..bc050a0 --- /dev/null +++ b/frontend/scripts/test-import-cycles.mjs @@ -0,0 +1,45 @@ +// Rejects import cycles in src/, type imports included: two files that import +// each other are one module split in the wrong place. +import test from 'node:test' +import assert from 'node:assert/strict' +import fs from 'node:fs' +import path from 'node:path' +import ts from 'typescript' + +const src = path.resolve(import.meta.dirname, '..', 'src') +const rel = (file) => path.relative(src, file).split(path.sep).join('/') + +function importGraph() { + const files = fs.readdirSync(src, { recursive: true }) + .filter((name) => /\.tsx?$/.test(name)) + .map((name) => path.join(src, name)) + const known = new Set(files) + return new Map(files.map((file) => { + const { importedFiles } = ts.preProcessFile(fs.readFileSync(file, 'utf8'), true, true) + const targets = importedFiles + .filter(({ fileName }) => fileName.startsWith('.')) + .map(({ fileName }) => path.resolve(path.dirname(file), fileName)) + .map((base) => ['', '.ts', '.tsx', `${path.sep}index.ts`].map((ext) => base + ext).find((p) => known.has(p))) + .filter(Boolean) + return [file, targets] + })) +} + +test('source modules do not import each other in a cycle', () => { + const graph = importGraph() + assert.ok(graph.size > 50, `expected to scan src/, found ${graph.size} files`) + const done = new Set() + const cycles = [] + const visit = (file, trail) => { + const start = trail.indexOf(file) + if (start >= 0) { + cycles.push([...trail.slice(start), file].map(rel).join(' -> ')) + return + } + if (done.has(file)) return + for (const next of graph.get(file)) visit(next, [...trail, file]) + done.add(file) + } + for (const file of graph.keys()) visit(file, []) + assert.deepEqual(cycles, []) +}) diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 097b836..454e9e0 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -19,7 +19,8 @@ import { resolveFullscreenHotkey } from './lib/fullscreenHotkey' import { LogLine, appendLog } from './lib/logs' import { macOSMajor } from './macos/macosRelease' import { currentTask, finishedTaskTTL, isActiveInstallerTask, pruneFinishedTasks, upsertTask } from './lib/tasks' -import { applyActiveLocaleFonts, applyActiveTheme, disposeActiveThemeAndLocaleFonts, notifyInteractiveThemeState, type UIStateSnapshot } from './lib/themeEngine' +import { applyActiveLocaleFonts, applyActiveTheme, disposeActiveThemeAndLocaleFonts, notifyInteractiveThemeState } from './lib/themeEngine' +import type { UIStateSnapshot } from './lib/uiSdk' import { hasVisibleDialog } from './lib/dialogState' import { latestVMStats, refreshSubscribedVMStats, refreshVMStats } from './lib/vmStats' import { useAppearance } from './lib/useAppearance' diff --git a/frontend/src/lib/themeEngine.ts b/frontend/src/lib/themeEngine.ts index e9e57d6..15dca4c 100644 --- a/frontend/src/lib/themeEngine.ts +++ b/frontend/src/lib/themeEngine.ts @@ -3,6 +3,7 @@ import { UI_CONTRACT_LAYOUT_OPTIONS, UI_CONTRACT_TOKENS, type UIContractSDKEvent } from '../styles/ui-contract' import type { ActiveThemePayload, ThemeDecoration, ThemeDecorationState, TokenDeriveRule } from '../types' +import type { ThemeSDKHostActions, UIStateSnapshot } from './uiSdk' const LEGACY_CACHE_KEY = 'reims-go-theme-tokens-v1' const THEME_CACHE_KEY = 'reims-go-theme-tokens-v2' @@ -26,27 +27,6 @@ interface ThemeTokenCache { dark?: Record } -export interface UIStateSnapshot { - appearance: 'light' | 'dark' - motion: 'full' | 'reduced' - locale: string - dir: 'ltr' | 'rtl' - view: string - vmName: string - vmState: string - busy: boolean - hasError: boolean - sidebarCollapsed: boolean - safeMode: boolean - vms: Array<{ name: string; state: string }> -} - -export interface ThemeSDKHostActions { - toggleSidebar: () => void - openSettings: () => void - focusVMWindow: (name: string) => void -} - let activeUIScriptCleanup: (() => void) | null = null let activeUIScriptNotifier: ((snap: UIStateSnapshot) => void) | null = null let activeUIScriptURL = '' diff --git a/frontend/src/lib/uiSdk.ts b/frontend/src/lib/uiSdk.ts index 2d503c9..6129cdd 100644 --- a/frontend/src/lib/uiSdk.ts +++ b/frontend/src/lib/uiSdk.ts @@ -6,7 +6,27 @@ import { type UIContractLayer, type UIContractPart, type UIContractSDKEvent, type UIContractSlot, } from '../styles/ui-contract' import { hideWailsGlobals } from '../wails' -import type { ThemeSDKHostActions, UIStateSnapshot } from './themeEngine' + +export interface UIStateSnapshot { + appearance: 'light' | 'dark' + motion: 'full' | 'reduced' + locale: string + dir: 'ltr' | 'rtl' + view: string + vmName: string + vmState: string + busy: boolean + hasError: boolean + sidebarCollapsed: boolean + safeMode: boolean + vms: Array<{ name: string; state: string }> +} + +export interface ThemeSDKHostActions { + toggleSidebar: () => void + openSettings: () => void + focusVMWindow: (name: string) => void +} interface ReimsUISDK { readonly uiApi: string diff --git a/frontend/src/ui/themeVisuals.tsx b/frontend/src/ui/themeVisuals.tsx index f82a6ab..5ecba89 100644 --- a/frontend/src/ui/themeVisuals.tsx +++ b/frontend/src/ui/themeVisuals.tsx @@ -5,7 +5,8 @@ import React, { createContext, useContext, useEffect, useMemo, useState } from 'react' import { createPortal } from 'react-dom' import { locText } from '../i18n' -import { matchDecorationState, themeAssetURL, type UIStateSnapshot } from '../lib/themeEngine' +import { matchDecorationState, themeAssetURL } from '../lib/themeEngine' +import type { UIStateSnapshot } from '../lib/uiSdk' import { UI_CONTRACT_ICONS, UI_CONTRACT_LAYERS, UI_CONTRACT_SLOTS, type UIContractIcon } from '../styles/ui-contract' import type { ActiveThemePayload, Locale, ThemeDecoration } from '../types'