Skip to content

Commit b2a5d90

Browse files
committed
feat(live-session): 前端采集控制单一状态源(reducer+provider,暂停 reason 契约)
1 parent 8933167 commit b2a5d90

4 files changed

Lines changed: 726 additions & 0 deletions

File tree

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
/**
2+
* liveCaptureState.test — 采集控制单一状态源纯收敛规则(批 2b)。
3+
* AAA:事件按到达序幂等收敛;漏检合成对(快速往返)终态=最后事件;watchdog
4+
* tick 判定 20s 未确认放弃;快照权威校正(守卫错自愈收敛终点);守卫错分类
5+
* 与文案映射(文案即 Rust 错误串,变更需同步本文件)。
6+
* @vitest-environment node(纯 reducer,无需 DOM)
7+
*/
8+
import { describe, expect, it } from "vitest";
9+
import type { LiveSessionStatus, PauseSource } from "../types";
10+
import {
11+
AUTO_RESUME_HINTS,
12+
PAUSE_LABELS,
13+
START_UNCONFIRMED_MS,
14+
WATCHDOG_INTERVAL_MS,
15+
classifyGuardError,
16+
initialState,
17+
isAutoPaused,
18+
liveCaptureReducer,
19+
parsePauseReason,
20+
} from "./liveCaptureState";
21+
22+
/** 构造快照载荷(camelCase 契约;reason 传原始字符串模拟 Option<String>) */
23+
function status(over: Partial<LiveSessionStatus> & { pausedReason?: string | null } = {}): LiveSessionStatus {
24+
return {
25+
active: false,
26+
sessionId: null,
27+
prepared: false,
28+
paused: false,
29+
pausedReason: null,
30+
tier: null,
31+
...over,
32+
} as LiveSessionStatus;
33+
}
34+
35+
const t0 = 1_000_000;
36+
/** 快照:活动 + 手动暂停 */
37+
const snapPausedManual = status({ active: true, sessionId: 7, paused: true, pausedReason: "manual" });
38+
/** 快照:活动 + 自动(媒体)暂停 */
39+
const snapPausedMedia = status({ active: true, sessionId: 7, paused: true, pausedReason: "media" });
40+
41+
describe("事件序列幂等收敛", () => {
42+
it("暂停原因跟随最后到达事件(reason 化载荷)", () => {
43+
let s = liveCaptureReducer(initialState, { type: "snapshot", status: status({ active: true }) });
44+
s = liveCaptureReducer(s, { type: "paused", reason: "media" });
45+
expect(s.pausedReason).toBe("media");
46+
s = liveCaptureReducer(s, { type: "paused", reason: "manual" });
47+
expect(s.pausedReason).toBe("manual");
48+
s = liveCaptureReducer(s, { type: "resumed" });
49+
expect(s.pausedReason).toBeNull();
50+
// resumed 后重复/滞后 paused(含旧载荷无 reason → manual 防御)也收敛
51+
s = liveCaptureReducer(s, { type: "paused" });
52+
expect(s.pausedReason).toBe("manual");
53+
});
54+
55+
it("漏检合成对(紧邻 paused→resumed)终态=最后事件,无需去抖", () => {
56+
let s = liveCaptureReducer(initialState, { type: "snapshot", status: status({ active: true }) });
57+
s = liveCaptureReducer(s, { type: "paused", reason: "media" });
58+
s = liveCaptureReducer(s, { type: "resumed" });
59+
expect(s.pausedReason).toBeNull();
60+
expect(s.active).toBe(true);
61+
// 反向合成对(resumed 是补发的历史恢复,paused 才是真实现状)
62+
s = liveCaptureReducer(s, { type: "resumed" });
63+
s = liveCaptureReducer(s, { type: "paused", reason: "foreground" });
64+
expect(s.pausedReason).toBe("foreground");
65+
});
66+
67+
it("多来源叠加以 reason 为准(manual 锁存期 media 事件也如实覆盖显示)", () => {
68+
let s = liveCaptureReducer(initialState, { type: "snapshot", status: status({ active: true }) });
69+
s = liveCaptureReducer(s, { type: "paused", reason: "media" });
70+
// 手动恢复但 auto 条件仍持 → 后端再发 paused(media):终态=media
71+
s = liveCaptureReducer(s, { type: "resumed", reason: "manual" });
72+
s = liveCaptureReducer(s, { type: "paused", reason: "media" });
73+
expect(s.pausedReason).toBe("media");
74+
});
75+
76+
it("会话未活动时的滞后 paused/resumed 事件丢弃(快照权威,防停止后残留)", () => {
77+
const idle = liveCaptureReducer(initialState, { type: "status", payload: "stopped" });
78+
expect(idle).toEqual(initialState);
79+
const s = liveCaptureReducer(idle, { type: "paused", reason: "media" });
80+
expect(s.pausedReason).toBeNull();
81+
// active=false 但 paused 状态真存在(快照)时事件仍应正常翻转
82+
const fromSnap = liveCaptureReducer(initialState, {
83+
type: "snapshot",
84+
status: status({ active: true, paused: true, pausedReason: "foreground" }),
85+
});
86+
expect(liveCaptureReducer(fromSnap, { type: "resumed" }).pausedReason).toBeNull();
87+
});
88+
});
89+
90+
describe("watchdog tick(20s 未确认放弃)", () => {
91+
it("starting 且未见 active:<20s 保持等待,≥20s 放弃并给 notice", () => {
92+
let s = liveCaptureReducer(initialState, {
93+
type: "start-resolved",
94+
sessionId: 9,
95+
engineReady: false,
96+
nowMs: t0,
97+
});
98+
expect(s.starting).toBe(true);
99+
expect(s.startingAtMs).toBe(t0);
100+
s = liveCaptureReducer(s, { type: "watchdog-tick", nowMs: t0 + WATCHDOG_INTERVAL_MS * 3 }); // 15s
101+
expect(s.starting).toBe(true);
102+
s = liveCaptureReducer(s, { type: "watchdog-tick", nowMs: t0 + START_UNCONFIRMED_MS });
103+
expect(s.starting).toBe(false);
104+
expect(s.notice).toBe("start-unconfirmed");
105+
expect(s.active).toBe(false);
106+
});
107+
108+
it("tick 期间快照确认 active → 过渡态即时收口(无需等 20s)", () => {
109+
let s = liveCaptureReducer(initialState, {
110+
type: "start-resolved",
111+
sessionId: 9,
112+
engineReady: false,
113+
nowMs: t0,
114+
});
115+
s = liveCaptureReducer(s, { type: "snapshot", status: status({ active: true, sessionId: 9 }) });
116+
expect(s.starting).toBe(false);
117+
expect(s.active).toBe(true);
118+
expect(s.startingAtMs).toBeNull();
119+
// 已收口后 tick 不再产生 notice
120+
s = liveCaptureReducer(s, { type: "watchdog-tick", nowMs: t0 + 60_000 });
121+
expect(s.notice).toBeNull();
122+
});
123+
124+
it("engineReady 直通路径不设簿记起点", () => {
125+
const s = liveCaptureReducer(initialState, {
126+
type: "start-resolved",
127+
sessionId: 9,
128+
engineReady: true,
129+
nowMs: t0,
130+
});
131+
expect(s.active).toBe(true);
132+
expect(s.starting).toBe(false);
133+
expect(s.startingAtMs).toBeNull();
134+
});
135+
136+
it("非 starting/active 状态 tick 为空操作", () => {
137+
const s = liveCaptureReducer(initialState, { type: "watchdog-tick", nowMs: t0 + 60_000 });
138+
expect(s).toEqual(initialState);
139+
});
140+
});
141+
142+
describe("快照权威校正(挂载/看门狗/守卫错自愈共用)", () => {
143+
it("漂移的暂停原因被快照覆写(事件丢失双轨残留)", () => {
144+
let s = liveCaptureReducer(initialState, { type: "snapshot", status: status({ active: true }) });
145+
s = liveCaptureReducer(s, { type: "paused", reason: "foreground" });
146+
// 实际已恢复(手动)——快照校正回 null
147+
s = liveCaptureReducer(s, { type: "snapshot", status: status({ active: true, sessionId: 7 }) });
148+
expect(s.pausedReason).toBeNull();
149+
// 反向:事件丢失但后端已暂停 media——快照补上
150+
s = liveCaptureReducer(s, { type: "snapshot", status: snapPausedMedia });
151+
expect(s.pausedReason).toBe("media");
152+
expect(s.sessionId).toBe(7);
153+
});
154+
155+
it("快照 inactive 全量清停采/暂停/停过渡(stopping 期间轮询收口)", () => {
156+
let s = liveCaptureReducer(initialState, { type: "snapshot", status: status({ active: true, paused: true, pausedReason: "manual" }) });
157+
s = liveCaptureReducer(s, { type: "stopping-intent" });
158+
expect(s.stopping).toBe(true);
159+
s = liveCaptureReducer(s, { type: "snapshot", status: status({ active: false }) });
160+
expect(s).toMatchObject({ active: false, sessionId: null, pausedReason: null, stopping: false });
161+
});
162+
163+
it("stopped/failed/fusing 事件全量复位(含 recovering/notice)", () => {
164+
let s = liveCaptureReducer(initialState, { type: "snapshot", status: status({ active: true, paused: true, pausedReason: "media" }) });
165+
s = liveCaptureReducer(s, { type: "recovering" });
166+
expect(s.recovering).toBe(true);
167+
s = liveCaptureReducer(s, { type: "fusing" });
168+
expect(s).toEqual(initialState);
169+
s = liveCaptureReducer(initialState, { type: "snapshot", status: status({ active: true }) });
170+
s = liveCaptureReducer(s, { type: "paused", reason: "manual" });
171+
expect(liveCaptureReducer(s, { type: "status", payload: "stopped" })).toEqual(initialState);
172+
expect(liveCaptureReducer(s, { type: "status", payload: "failed" })).toEqual(initialState);
173+
});
174+
175+
it("engine-error 只退等待态不动 active(录制中错误走文案面)", () => {
176+
let s = liveCaptureReducer(initialState, {
177+
type: "start-resolved",
178+
sessionId: 9,
179+
engineReady: false,
180+
nowMs: t0,
181+
});
182+
s = liveCaptureReducer(s, { type: "engine-error" });
183+
expect(s).toMatchObject({ starting: false, stopping: false });
184+
const running = liveCaptureReducer(initialState, {
185+
type: "snapshot",
186+
status: status({ active: true, sessionId: 5 }),
187+
});
188+
expect(liveCaptureReducer(running, { type: "engine-error" }).active).toBe(true);
189+
});
190+
191+
it("recording 事件=新会话起点:清暂停/过渡态", () => {
192+
const s = liveCaptureReducer(initialState, { type: "snapshot", status: status({ active: true, paused: true, pausedReason: "media" }) });
193+
const after = liveCaptureReducer(s, { type: "status", payload: "recording" });
194+
expect(after).toMatchObject({ active: true, pausedReason: null, starting: false, stopping: false, recovering: false });
195+
});
196+
});
197+
198+
describe("守卫错分类与文案", () => {
199+
it("四类后端错误串映射(文案变更需同步——契约见 Rust command 层)", () => {
200+
expect(classifyGuardError("会话已处于暂停")).toBe("already-paused");
201+
expect(classifyGuardError("会话未处于暂停")).toBe("not-paused");
202+
expect(classifyGuardError("已有进行中的实时会话,请先停止")).toBe("active-session");
203+
expect(classifyGuardError("无活动实时会话")).toBe("no-session");
204+
expect(classifyGuardError("音频设备初始化失败")).toBeNull();
205+
});
206+
207+
it("reason 三态文案与 auto 提示齐备", () => {
208+
expect(PAUSE_LABELS.manual).toBe("⏸ 已暂停");
209+
expect(PAUSE_LABELS.media).toBe("⏸ 已随视频暂停");
210+
expect(PAUSE_LABELS.foreground).toBe("⏸ 已自动暂停(切走)");
211+
expect(AUTO_RESUME_HINTS.media.length).toBeGreaterThan(0);
212+
expect(AUTO_RESUME_HINTS.foreground.length).toBeGreaterThan(0);
213+
expect(isAutoPaused("manual")).toBe(false);
214+
expect(isAutoPaused("media")).toBe(true);
215+
expect(isAutoPaused(null)).toBe(false);
216+
});
217+
218+
it("parsePauseReason 边界防御(Option<String> 未知值按 null)", () => {
219+
expect(parsePauseReason("manual")).toBe("manual");
220+
expect(parsePauseReason("media")).toBe("media");
221+
expect(parsePauseReason("foreground")).toBe("foreground");
222+
expect(parsePauseReason(null)).toBeNull();
223+
expect(parsePauseReason(undefined)).toBeNull();
224+
expect(parsePauseReason("window-blur" as PauseSource)).toBeNull();
225+
});
226+
});

0 commit comments

Comments
 (0)