WuKongIM 管理台 Cluster Nodes / Slots 二级视图重构:设计思路、Surface 标记规范与测试驱动的实施路径

原创2026-10-15 18:41:221,726 阅读
文章标签:即时通讯后端

WuKongIM 管理台 Cluster Nodes / Slots 二级视图重构:设计思路、Surface 标记规范与测试驱动的实施路径

导读

本文围绕 WuKongIM 仓库内的一份前端实施计划文档(docs/superpowers/plans/2026-07-08-web-cluster-nodes-slots-subviews-redesign.md),完整讲解 Web 管理台 /cluster/nodes 与 /cluster/slots 两个二级页面的视觉语言统一改造。改造不改变任何集群操作行为,而是通过稳定 Surface 标记(data-node-surface / data-slot-surface)、紧凑概览条(summary strip) 与 无障碍异常表格 三类手段,将节点/槽位页纳入既有编辑控制台的视觉规范。读完本文,你将掌握该计划的完整文件结构、两大任务的逐步实施细节、测试先行(TDD)的验证方式,以及源码层面的最终落点,可直接对照 web/src/pages/nodes/page.tsx 与 web/src/pages/slots/page.tsx 复盘。

一、计划背景与架构约束

1.1 目标:视觉统一,行为不变

该计划的核心目标是:

将 /cluster/nodes 和 /cluster/slots 二级页面带入已批准的编辑控制台(editorial console)视觉语言,不改变集群操作行为。

关键词是"二级页面(secondary surfaces)"——即节点列表/详情、槽位清单/详情这类支撑性视图,与主控制台采用统一的设计语汇:更紧凑的圆角(rounded-md 取代 rounded-lg/rounded-xl)、统一的边框与背景语义(border-border、bg-card、bg-muted/20、bg-muted/30)、以及可供测试与自动化工具稳定寻址的 DOM 标记。

1.2 架构原则:改动局部化

计划明确要求改动严格限制在现有 nodes 与 slots 页面模块及其聚焦测试内,并保留:

  • 当前可见的 List/Logs 双 Tab 结构;
  • 列表工作台(list workbench)的既有行为;
  • 后端 manager API 调用;
  • 动作门控(action gates,如 canWriteNodes、canWriteSlots 权限判断);
  • 对话框(dialogs)、详情行为(detail sheets)。

同时列出了一份禁止改动清单:后端 manager API、路由定义、认证规则、i18n 文案、cluster-dashboard、business-dashboard、拓扑、频道、诊断、监控、业务页面与系统页面一律不动。这一点在源码中得到了印证——本次改造涉及的文件仅 web/src/pages/nodes/page.tsx、web/src/pages/nodes/page.test.tsx、web/src/pages/slots/page.tsx、web/src/pages/slots/page.test.tsx 四个文件。

1.3 技术栈

计划文档列出的技术栈与仓库 web/package.json 完全一致:Vite、React 19、TypeScript、Tailwind CSS v4、React Intl、Vitest、Testing Library、Bun(packageManager: bun@1.3.11,测试命令 vitest run)。

二、文件结构与任务总览

计划将实施拆成三个任务,每个任务都有独立的提交:

任务 修改文件 测试文件 提交信息
Task 1:Node 详情/概览/异常面板 web/src/pages/nodes/page.tsx web/src/pages/nodes/page.test.tsx style(web): refine node subview surfaces
Task 2:Slot 详情/结果/概览/异常面板 web/src/pages/slots/page.tsx web/src/pages/slots/page.test.tsx style(web): refine slot subview surfaces
Task 3:集成验证与源码清理 检查以上四个文件与 web/dist/index.html — —

Node 侧需要新增的 Surface 标记包括:data-node-surface="detail"、overview-unhealthy、overview-runtime、unhealthy、unhealthy-table;Slot 侧为:data-slot-surface="detail"、rebalance-result、batch-transfer-result、inventory、overview-unhealthy、overview-runtime、unhealthy、unhealthy-table,外加 slots-overview-summary-strip 测试定位标识。这些标记在最终源码中均已就位,下文会逐一对应。

三、Task 1:Node 详情、概览与异常面板改造

3.1 先写失败测试(TDD 第 1 步)

计划要求先改写 web/src/pages/nodes/page.test.tsx 的导入,把单页面导入升级为三个导出符号:

import {
  NodeClusterOverviewPanel,
  NodeClusterUnhealthyPanel,
  NodesPage,
} from "@/pages/nodes/page"

随后追加三个新测试,分别覆盖三类 Surface。第一个测试验证节点详情是一个紧凑检查面:

test("marks node detail as a compact inspection surface", async () => {
  getNodesMock.mockResolvedValueOnce({
    generated_at: "2026-04-23T08:00:01Z",
    controller_leader_id: 1,
    total: 1,
    items: [nodeRow],
  })
  getNodeMock.mockResolvedValueOnce(nodeDetail)

  const user = userEvent.setup()
  renderNodesPage()

  expect(await screen.findByText("127.0.0.1:7000")).toBeInTheDocument()
  await user.click(screen.getByRole("button", { name: "Inspect node 1" }))

  const detailSurface = (await screen.findByText("Hosted IDs")).closest("[data-node-surface='detail']")
  expect(detailSurface).toHaveClass("rounded-md", "border", "border-border", "bg-card", "p-3")
  expect(detailSurface).toHaveTextContent("127.0.0.1:7000")
  expect(detailSurface).toHaveTextContent("1, 2, 3")
})

第二个测试直接渲染导出的 NodeClusterOverviewPanel,验证概览次级面:nodes-summary-strip 汇总条、overview-unhealthy 分解文本("Unhealthy: 0; runtime unknown: 0; draining: 0.")、以及 overview-runtime 运行时三列网格(Gateway sessions / Active online / runtime unknown 的值 5、4、0)。第三个测试渲染 NodeClusterUnhealthyPanel,用构造的 dead 状态节点验证异常表格具备 aria-label("Unhealthy Nodes")且被 unhealthy 与 unhealthy-table 两层标记包裹。

3.2 运行并确认测试失败(第 2 步)

cd web && /Users/tt/.bun/bin/bun run test -- src/pages/nodes/page.test.tsx -t "marks node"

预期输出 FAIL,原因正是各类标记与可访问标签尚不存在——这是 TDD 的红灯阶段,为后续实现提供了精确验收标准。

3.3 源码实现落点

详情面:在 web/src/pages/nodes/page.tsx 的 DetailSheet 内,详情内容被包进:

<div className="rounded-md border border-border bg-card p-3" data-node-surface="detail">
  <KeyValueList
    items={[
      // address / status / membershipRole / joinState / schedulable /
      // controllerRole / controllerVoter / controllerRaftHealth / controllerRaftWatermark /
      // controllerRaftLink / lastHeartbeat / capacityWeight / slotSummary /
      // runtime / hostedIds / leaderIds ...
    ]}
  />
</div>

注意 KeyValueList 来自共享组件 web/src/components/manager/key-value-list.tsx,其 item 数组在改造中保持完全不变。

概览面:NodeClusterOverviewPanel(nodes/page.tsx)中,不健康分解块被标记为 data-node-surface="overview-unhealthy",运行时网格被标记为 data-node-surface="overview-runtime",三张运行时指标卡从 rounded-lg 收紧为 rounded-md。汇总条使用已有的 NodeSummaryCell(每格 text-2xl font-mono tabular-nums 大数字)并保留 data-testid="nodes-summary-strip"。概览数据来自 getNodes(),运行时总数由 nodeRuntimeTotals 对全部节点的 gateway_sessions、active_online 与 unknown 计数做归约。

异常表:NodeClusterUnhealthyPanel(nodes/page.tsx)通过 isUnhealthyNode 过滤节点——该函数判定 healthStatus !== "alive"、join state 非 active、不可调度、quorum_lost_count > 0、unreported_count > 0、runtime unknown 或 draining 任一条件即视为异常。面板与表格两层 wrapper 分别带上 data-node-surface="unhealthy" 与 data-node-surface="unhealthy-table",表格本身获得:

<table aria-label={intl.formatMessage({ id: "nodes.unhealthy.title" })} className="w-full border-collapse text-sm">

3.4 回归与提交

第 6 步要求整文件测试通过,且既有测试仍需覆盖:隐藏的概览/异常 Tab、生命周期动作门控、slot move 动作、controller-voter 提升、controller-raft 链接、加载/空/错误状态。第 7 步以 git add + git commit -m "style(web): refine node subview surfaces" 完成提交,且只含 nodes 页面与测试两个文件。

四、Task 2:Slot 详情、结果、概览与异常面板改造

4.1 测试先行

Slot 侧同样先改测试导入——在 web/src/pages/slots/page.test.tsx 中把 render, screen 升级为 render, screen, within,并把页面导入改为三个符号:

import {
  SlotClusterOverviewPanel,
  SlotClusterUnhealthyPanel,
  SlotsPage,
} from "@/pages/slots/page"

新增的四个测试断言包括:

  • 详情面:点击 "Inspect slot 9" 后,"Desired peers" 所在容器带 data-slot-surface="detail",包含 "1, 2, 3"、错误状态 "temporary failure",且 "Transfer leader / Recover slot / Remove slot" 三个操作按钮可用;
  • 概览面:slots-overview-summary-strip 具备 grid gap-0 border-y border-border,展示 Total slots / Ready slots 等指标,overview-unhealthy 显示 "Quorum lost: 0; leader missing: 0; sync mismatch: 0.",overview-runtime 网格包含 Epoch lag、Retrying tasks、Failed tasks;
  • 异常表:通过 getOverviewMock 注入 quorum_lost 异常槽位(slot 9,quorum: "lost",desired_peers: [1, 2, 3],current_peers: [1, 2]),断言表格具备 "Unhealthy Slots" 可访问标签,行内出现 "Slot 9" 与 "Quorum lost";
  • 结果面收紧:既有 rebalance 测试断言结果容器为 rounded-md 且不再是 rounded-xl/rounded-lg;batch-transfer 测试在 "Created 1 · Existing 0 · Failed 0" 之后追加对 data-slot-surface="batch-transfer-result" 的同类断言。

4.2 实现细节:从本地 helper 到六格汇总条

本地 helper(第 3 步):在 formatTimestamp 之后新增 SlotSummaryCell,刻意不导出、不与 nodes 页共享:

function SlotSummaryCell({ label, value }: { label: string; value: number | string }) {
  return (
    <div className="border-b border-border px-1 py-3 sm:px-3">
      <div className="text-xs text-muted-foreground">{label}</div>
      <div className="mt-1 font-mono text-2xl font-medium tabular-nums text-foreground">{value}</div>
    </div>
  )
}

该 helper 在 web/src/pages/slots/page.tsx 中真实存在,与 nodes 页的 NodeSummaryCell 结构完全一致,但按计划有意不抽象成共享组件。

汇总条替换(第 6 步):SlotClusterOverviewPanel 原先的六张 SectionCard 汇总卡被整体替换为一条紧凑 strip:

<div
  className="grid gap-0 border-y border-border sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-6"
  data-testid="slots-overview-summary-strip"
>
  <SlotSummaryCell label={intl.formatMessage({ id: "slots.metric.total" })} value={overview.slots.total} />
  <SlotSummaryCell label={intl.formatMessage({ id: "slots.cards.ready.title" })} value={overview.slots.ready} />
  <SlotSummaryCell label={intl.formatMessage({ id: "slots.metric.quorumLost" })} value={overview.slots.quorum_lost} />
  <SlotSummaryCell label={intl.formatMessage({ id: "slots.metric.leaderMissing" })} value={overview.slots.leader_missing} />
  <SlotSummaryCell label={intl.formatMessage({ id: "slots.metric.peerMismatch" })} value={overview.slots.peer_mismatch} />
  <SlotSummaryCell label={intl.formatMessage({ id: "slots.metric.unreported" })} value={overview.slots.unreported} />
</div>

详情面(第 4 步):DetailSheet 中加载完成的分支由 <>...</> 片段改为带标记的容器:

{!detailLoading && !detailError && detail ? (
  <div className="rounded-md border border-border bg-card p-3" data-slot-surface="detail">
    <KeyValueList
      items={[
        // desiredPeers / currentPeers / hashSlots / taskStatus / taskStep /
        // quorum / sync / leaderId / healthyVoters / configEpoch /
        // observedEpoch / lastReport / lastError ...
      ]}
    />
  </div>
) : null}

操作结果面收紧(第 5 步):rebalance 结果块与 batch-transfer 结果块的外层容器统一为 data-slot-surface="rebalance-result" / data-slot-surface="batch-transfer-result" + rounded-md border border-border bg-card p-3;各自的结果条目外壳(rebalance 用 key={item.hash_slot}、batch 用 key={item.slot_id})从 rounded-lg 收紧为 rounded-md,但计划数据、mutation 调用、对话框状态与结果文案一律不动。在 web/src/pages/slots/page.tsx 可以看到 batch-transfer-result 块的真实实现:摘要行显示 created/existing/failed 三元组,结果条目以 md:grid-cols-2 xl:grid-cols-3 网格排布。

异常表(第 8 步):SlotClusterUnhealthyPanel(slots/page.tsx)通过 slotAnomalyRows 把 quorum_lost、leader_missing、sync_mismatch 三类异常槽位合并成行,表格带 aria-label={intl.formatMessage({ id: "slots.unhealthy.title" })},面板/表格两层分别标记 data-slot-surface="unhealthy" 与 unhealthy-table。异常原因标签由 slotAnomalyReasonMessageId 映射到 slots.unhealthy.reason.quorumLost 等 i18n 文案。

4.3 回归与提交

第 9 步要求整文件测试通过,既有测试仍需覆盖:隐藏的概览/异常 Tab、库存密度、节点过滤、增/删/恢复/转移/rebalance/batch-transfer 行为、加载/空/错误状态。第 10 步提交信息为 style(web): refine slot subview surfaces。

五、Task 3:集成验证与源码清理

第 3 阶段聚焦四个文件的整体质量门禁,命令序列如下:

# 1. 聚焦前端测试
cd web && /Users/tt/.bun/bin/bun run test -- src/pages/nodes/page.test.tsx src/pages/slots/page.test.tsx

# 2. TypeScript 编译校验
cd web && /Users/tt/.bun/bin/bunx tsc -b

# 3. 生产构建(Vite chunk-size 警告如与既往批次一致可接受)
cd web && /Users/tt/.bun/bin/bun run build

# 4. 若 web/dist/index.html 出现非预期的 asset-hash 变化则还原
git status --short web/dist/index.html
git restore web/dist/index.html

# 5. 空白符检查
git diff --check

# 6. 最终差异审计
git diff --stat
git diff -- web/src/pages/nodes/page.tsx web/src/pages/nodes/page.test.tsx web/src/pages/slots/page.tsx web/src/pages/slots/page.test.tsx

# 7. 确认 git 状态
git status --short --branch

验收标准:两个测试文件全绿、tsc -b 退出码 0、构建退出码 0、diff 中只包含二级 Surface 重构改动,绝无后端/路由/认证/i18n/仪表盘等越界修改;web/dist/index.html 这类生成产物的哈希抖动不残留于源码 diff。此外计划特别提醒:仓库中既有的未跟踪文件 docs/superpowers/plans/2026-07-05-legacy-app-slot-store-adapter.md 应保持原样,除非用户显式要求处理。

六、源码级复盘:Surface 标记与数据流

6.1 标记体系一览

改造完成后,仓库内实际存在的标记与语义如下:

Node 侧(web/src/pages/nodes/page.tsx)

标记 位置 语义
data-node-surface="detail" 详情 KeyValueList 外壳(L1249) 节点详情紧凑检查面
data-node-surface="config" NodeConfigSection(L407) 生效配置段
data-node-surface="overview-unhealthy" 概览异常分解块(L1428) 异常汇总文本
data-node-surface="overview-runtime" 概览运行时三列网格(L1443) 会话/在线/未知统计
data-node-surface="unhealthy" 异常面板外壳(L1541) 异常事件面板
data-node-surface="unhealthy-table" 异常表格滚动容器(L1543) 可访问异常表格

Slot 侧(web/src/pages/slots/page.tsx)

标记 位置 语义
data-slot-surface="inventory" 库存列表外壳(L554) 槽位清单工作台
data-slot-surface="detail" 详情 KeyValueList 外壳(L695) 槽位详情检查面
data-slot-surface="batch-transfer-result" 批量转移结果块(L629) 操作结果面
data-slot-surface="rebalance-result" 再平衡结果块 操作结果面
data-slot-surface="overview-unhealthy" 概览异常分解块(L1001) 异常汇总文本
data-slot-surface="overview-runtime" 概览运行时三列网格(L1016) Epoch/任务统计
data-slot-surface="unhealthy" 异常面板外壳(L1114) 异常事件面板
data-slot-surface="unhealthy-table" 异常表格滚动容器(L1116) 可访问异常表格

6.2 数据来源与 API 约束

计划强调"不修改后端 manager API",实际两个页面全部复用 web/src/lib/manager-api.ts 的既有封装:

  • 节点侧:getNodes()、getNode(nodeId)、getNodeConfig(nodeId),以及生命周期动作 activateNode、startNodeOnboarding、advanceNodeSlotMoveOut、startNodeScaleIn、setNodeScaleInDrain、advanceNodeScaleIn、removeNodeAfterScaleIn、promoteControllerVoter;
  • 槽位侧:getSlots({ nodeId })(按节点过滤)、transferSlotLeader、planSlotLeaderTransfers、executeSlotLeaderTransferBatch、getOverview()。

其中 getOverview()(manager-api.ts)是一个值得注意的聚合入口:它并行请求 /manager/nodes、/manager/slots 以及 pending/running/failed 三种状态的 controller 任务,再经 buildManagerOverview 组装出概览响应。这正是 SlotClusterOverviewPanel 与 SlotClusterUnhealthyPanel 的数据基础——异常槽位行来源于响应中 anomalies.slots 的 quorum_lost、leader_missing、sync_mismatch 三类明细。

6.3 为什么用 Surface 标记而非视觉类名断言

计划中的测试大量使用 .closest("[data-*-surface='...']") + toHaveClass(...) 的组合。这里的工程意图是双层的:

  1. data-*-surface 提供稳定的语义锚点,不随 Tailwind 类名重构而漂移,适合被端到端工具、自动化巡检与未来的可访问性测试稳定寻址;
  2. toHaveClass 断言则把视觉语言本身(rounded-md border border-border bg-card p-3)纳入回归保护,确保任何后续改动不会悄悄退回 rounded-xl/rounded-lg 等旧半径。

两个维度一个管"结构在哪里",一个管"长得对不对",共同构成了这次"视觉重构但零行为变更"的可测试性保障。

七、从计划到实现的落地经验

对照计划文档与最终源码,可以提炼出几条可直接复用的实施经验:

  1. 视觉改造也要 TDD:先写"标记 + 类名"断言并跑出红灯,再逐条落地,最后整文件回归——四类改动(详情、概览、结果、异常表)各自有独立测试,失败定位一目了然;
  2. "不导出、不共享"的本地 helper 策略:SlotSummaryCell 与 NodeSummaryCell 刻意保持页面私有,避免为一次视觉统一引入跨页面耦合,也符合"改动局部化"的架构约束;
  3. 可访问性内建:两张异常表格都通过 aria-label={intl.formatMessage(...)} 提供角色名,配合 w-full border-collapse text-sm 与 overflow-x-auto 滚动容器,兼顾屏幕阅读器与窄屏横向滚动;
  4. 生成产物与源码 diff 分离:Task 3 专门处理 web/dist/index.html 的哈希抖动还原与 git diff --check,保证提交干净、可审阅;
  5. 回归面完整:每个任务的收尾都要求既有行为测试(动作门控、Tab 切换、加载/空/错误态)保持全绿,这正是"不改行为"的硬约束在测试层的体现。

若需在本地验证这套改造,可进入 web 目录依次执行 bun install、bun run test -- src/pages/nodes/page.test.tsx src/pages/slots/page.test.tsx、bunx tsc -b 与 bun run build,对照 Task 3 的验收标准检查输出即可。

登录后查看全文
WuKongIM