Files
apes-sidebar/tests/session-metrics.spec.ts
T
Menghuan1918 9ccc147e00 feat(tasks): 第二轮真机反馈——聚合条动作行、模式 chip、弹窗用量/耗时、原生侧栏转录、动效
用户在自己跑的 3080 实例上测完报回六条,逐条修:

1. 「已完成的聚合卡下方空荡荡」= 上一轮把计数收进徽标后那条只剩一个
   chevron(我的取舍造成)。小条改为动作行:组图标 + 「展开」(新词条
   tasksFoldExpandShort × 20 词典)+ 出错尾注 + 展开箭头。
2. 「展开后的子代理点不回去」= 真缺陷:expandedGroups 压过了 foldedIds,
   展开组内卡片的 chevron 失效。改成手动折叠优先(手动 → 页级规则 →
   组展开),补模型 + 组件两层判别性用例。
3. 「一次性一直显示」→ 模式词搬进徽标行成 chip(用户口径)。智能体卡不再
   有 mono 元信息行,卡片高度预算拆成 GRAPH_AGENT_NODE_H / GRAPH_NODE_H
   (只有 workflow run 卡保留元信息行),rowStrideFor(nodes) 按模型里有没有
   run 选行距。
4. 动效:接入 @formkit/auto-animate(用户选定,~3KB、无依赖无安装脚本)+
   useAutoAnimateRef 包装(WAAPI 守卫 → jsdom 空转、尊重 reduced-motion),
   挂到画布卡片层 / 树视图 / 任务抽屉;详情卡另加 140ms 入场关键帧。核心
   bundle +56KB(未压缩 CJS)。
5. 弹窗多两项:Token 用量(宿主 tokenUsage 四桶求和,悬停给明细)+ 耗时
   (宿主 subagentTiming,按 3s 轮询刷新)。SidebarProjectionSnapshot 镜像
   补两个键、模型新增 metrics 入参;**按存在性渲染**,没有测量就不显示这一
   行(0 是投影初始态,与「没工作过」不可区分)。
6. 「查看转录」改为在原生右侧栏打开:宿主 ui-subagent 的 subagentchat tab
   类型(宿主自己的子代理列表就是点行=主窗、点 chevron=侧栏)。插件照抄其
   地址公式并调 openResource(..., { kind, preferNewPane: true }),三级降级回
   主窗;页脚变「主按钮 + caret 菜单(在主窗口中打开)」。

测试 2238 passed / 17 skipped(191 文件)、tsc 0 错误、eslint 0 错误、构建
通过;新构建已装进 sidebar-test profile,:3080 上挂载正常、0 pageerror。
动效观感与真实数据的两行数值未在真机逐帧核验(右栏在无头上下文里被
transform 移出视口,合成点击不稳)——已如实记进设计文档的第二轮偏差段。
2026-10-11 23:15:40 +08:00

68 lines
2.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Unit tests for the Tasks page's per-session metrics (token usage + turn
* timing): the fold from the host's projection snapshot, the two totals the
* popover prints, and the rule that a session with no measurements reports
* nothing rather than a zero it cannot justify.
*/
import { describe, expect, it } from 'vitest'
import {
elapsedMs, inputTokens, sessionMetrics, totalTokens,
} from '../src/client/session-metrics.ts'
import type { SidebarProjectionSnapshot } from '../src/context-types.ts'
/** One projection row carrying the metric keys under test. */
function snapshot(values: SidebarProjectionSnapshot['values'], state: 'idle' | 'ready' = 'ready'): SidebarProjectionSnapshot {
return { values, state, error: null }
}
/** A usage row with the four disjoint buckets. */
const USAGE = {
uncachedInputTokens: 1_000,
outputTokens: 4_000,
cacheReadTokens: 20_000,
cacheWriteTokens: 500,
}
describe('sessionMetrics', () => {
it('folds the two metric keys per session and skips sessions without any', () => {
const metrics = sessionMetrics({
a: snapshot({ tokenUsage: USAGE }),
b: snapshot({ subagentTiming: { settledMs: 1_500 } }),
c: snapshot({ subagentCatalog: [] }),
d: snapshot({ tokenUsage: USAGE, subagentTiming: { settledMs: 0 } }),
})
expect(Object.keys(metrics).sort()).toEqual(['a', 'b', 'd'])
expect(metrics.a?.usage?.outputTokens).toBe(4_000)
expect(metrics.a?.timing).toBeUndefined()
expect(metrics.b?.usage).toBeUndefined()
expect(metrics.d?.timing?.settledMs).toBe(0)
})
it('answers an empty map for a host without the projection store', () => {
expect(sessionMetrics(undefined)).toEqual({})
})
it('sums the four DISJOINT buckets as the session total', () => {
expect(inputTokens(USAGE)).toBe(21_500)
expect(totalTokens(USAGE)).toBe(25_500)
})
})
describe('elapsedMs', () => {
it('counts closed turns plus the open one, measured to the caller’s clock', () => {
// `active.through` only advances when an event commits, so a running node's
// elapsed time is measured against the render clock, not `through`.
expect(elapsedMs({ settledMs: 1_000, active: { since: 10_000, through: 11_000 } }, 13_500))
.toBe(4_500)
expect(elapsedMs({ settledMs: 2_500 }, 99_999)).toBe(2_500)
})
it('reports nothing when the host published no timing, or none of it is work', () => {
// 0 is the projection's init state, indistinguishable from "never worked".
expect(elapsedMs(undefined, 1_000)).toBeUndefined()
expect(elapsedMs({ settledMs: 0 }, 1_000)).toBeUndefined()
// A clock that moved backwards must not print a negative duration.
expect(elapsedMs({ settledMs: 5, active: { since: 10_000, through: 10_000 } }, 9_000)).toBe(5)
})
})