test_record_watchers.uts
5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { RecordService } from "@/services/recorder/index.uts";
import TempRecord from "@/services/recorder/TempRecord.uts";
import { RecordData } from "@/services/recorder/types/index.uts";
import { commit } from "@/store/index.uts";
/**
* 测试 RecordService 的监听器功能
*
* 使用方法:
* 在 App.uvue 或任意页面的 onReady/onLoad 中调用此函数即可在控制台查看测试结果
* import { testRecordWatchers } from '@/test/test_record_watchers.uts'
* testRecordWatchers()
*/
export function testRecordWatchers() {
console.log("=== 开始测试 RecordService 监听器 ===");
const service = RecordService.getInstance();
// 1. 模拟 isReceptioning 为 true,使 handler 能执行回调
// 只有在接待中 (isReceptioning() == true) 才会响应状态变化
// 我们通过设置一个 endReasonType=1 (异常结束/暂存状态?) 的伪造记录来模拟此条件
// 或者更直接地,我们可以临时覆盖 isReceptioning 方法(如果 UTS 允许)
// 方案 A: 构造伪造数据让 isReceptioning 返回 true
// isReceptioning() => (audioManager?.isRecording) || (record?.currentRecord?.endReasonType == 1)
const mockRecordData = {
id: "mock_test_id",
endReasonType: 1
} as RecordData;
// 创建 TempRecord 实例并注入
// 注意:TempRecord 构造函数可能依赖 uni.$dbRecord,但在测试环境下可能未初始化
// 如果报错,可能需要 Mock DBRecord.getInstance()
try {
const mockTempRecord = new TempRecord(mockRecordData, null, null);
service.record = mockTempRecord;
console.log(`[Test] 注入 Mock Record, isReceptioning 当前状态: ${service.isReceptioning()}`);
} catch (error) {
console.error("[Test] 注入 Mock Record 失败:", error);
return;
}
// 2. Mock 目标方法以验证调用
// 使用 any 类型绕过类型检查进行方法覆写 (Monkey Patching)
const serviceAny = service as any;
// 备份原方法,以便测试后恢复
const originalUpdateCustomerInfo = serviceAny.updateCustomerInfo;
const originalUpdateReceptionStatus = serviceAny.updateReceptionStatus;
const originalUpdateBookList = serviceAny.updateBookList;
const originalUpdateMaterialPointList = serviceAny.updateMaterialPointList;
// 计数器
let customerInfoCount = 0;
let receptionStatusCount = 0;
let bookListCount = 0;
let materialPointListCount = 0;
// 覆写方法 (Spy)
serviceAny.updateCustomerInfo = (val: any) => {
console.log(`[Test] updateCustomerInfo 被调用,参数: ${JSON.stringify(val)}`);
customerInfoCount++;
};
serviceAny.updateReceptionStatus = (val: any) => {
console.log(`[Test] updateReceptionStatus 被调用,参数: ${val}`);
receptionStatusCount++;
};
serviceAny.updateBookList = (val: any) => {
console.log(`[Test] updateBookList 被调用`);
bookListCount++;
};
serviceAny.updateMaterialPointList = (val: any) => {
console.log(`[Test] updateMaterialPointList 被调用`);
materialPointListCount++;
};
// 3. 触发 Store 变化
console.log("[Test] 正在触发 Store 变化...");
// --- 测试 updateCustomerInfo (debounce 1000ms) ---
// 监听的是 customerKey 变化,回调中使用 customerInfo
const testCustomerInfo = { customerName: "测试客户", phoneNumber: "13800000000" };
commit("updateCustomerInfo", testCustomerInfo);
// 触发 watcher
commit("updateCustomerKey", `KEY_${ Date.now()}`);
// --- 测试 updateReceptionStatus (debounce 200ms) ---
commit("updateReceptionStatus", "1"); // 状态变更
// --- 测试 updateBookList (debounce 1500ms) ---
const testBookList = [{ id: "book1", imgUrl: "url1" }];
commit("updateBookList", testBookList); // 数组变更可能不触发 watcher?
// 注意:监听的是 storeState.booksList 引用变化。commit 会替换数组,应该能触发。
// --- 测试 updateMaterialPointList (debounce 1000ms) ---
// 监听 receptionKey,回调中使用 receptionTrack
const testTrack = [{ id: "point1", x: 10, y: 20 }];
commit("updateReceptionTrack", testTrack);
// 触发 watcher
commit("updateReceptionKey", `REC_KEY_${ Date.now()}`);
// 4. 等待 Debounce 结束
// 最长 delay 是 1500ms,我们等待 2000ms
console.log("[Test] 等待防抖结束 (2000ms)...");
setTimeout(() => {
console.log("=== 测试结果报告 ===");
const check = (name: string, count: number) => {
const pass = count >= 1;
console.log(`${name}: ${pass ? "PASS ✅" : "FAIL ❌"} (调用次数: ${count})`);
};
check("updateCustomerInfo", customerInfoCount);
check("updateReceptionStatus", receptionStatusCount);
check("updateBookList", bookListCount);
check("updateMaterialPointList", materialPointListCount);
// 5. 恢复环境
serviceAny.updateCustomerInfo = originalUpdateCustomerInfo;
serviceAny.updateReceptionStatus = originalUpdateReceptionStatus;
serviceAny.updateBookList = originalUpdateBookList;
serviceAny.updateMaterialPointList = originalUpdateMaterialPointList;
service.record = null; // 重置 record
console.log("=== 测试结束,环境已恢复 ===");
}, 2000);
}