@ray-js/lamp-schedule-core 1.0.0-beta-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (246) hide show
  1. package/README.md +565 -0
  2. package/lib/config/dpCodes.d.ts +27 -0
  3. package/lib/config/dpCodes.js +30 -0
  4. package/lib/config/index.d.ts +2 -0
  5. package/lib/config/index.js +2 -0
  6. package/lib/config/rhythms/defaultValue.d.ts +18 -0
  7. package/lib/config/rhythms/defaultValue.js +72 -0
  8. package/lib/config/rhythms/index.d.ts +10 -0
  9. package/lib/config/rhythms/index.js +35 -0
  10. package/lib/conflict/ConflictResolver.d.ts +2 -0
  11. package/lib/conflict/ConflictResolver.js +284 -0
  12. package/lib/conflict/__test__/ConflictResolver.test.d.ts +1 -0
  13. package/lib/conflict/__test__/ConflictResolver.test.js +183 -0
  14. package/lib/conflict/__test__/index.test.d.ts +1 -0
  15. package/lib/conflict/__test__/index.test.js +385 -0
  16. package/lib/conflict/__test__/scheduleDataManager.test.d.ts +1 -0
  17. package/lib/conflict/__test__/scheduleDataManager.test.js +170 -0
  18. package/lib/conflict/__test__/transform.test.d.ts +1 -0
  19. package/lib/conflict/__test__/transform.test.js +454 -0
  20. package/lib/conflict/__test__/updateDeviceDp.d.ts +1 -0
  21. package/lib/conflict/__test__/updateDeviceDp.js +114 -0
  22. package/lib/conflict/index.d.ts +42 -0
  23. package/lib/conflict/index.js +103 -0
  24. package/lib/conflict/scheduleDataManager.d.ts +12 -0
  25. package/lib/conflict/scheduleDataManager.js +79 -0
  26. package/lib/conflict/transform.d.ts +71 -0
  27. package/lib/conflict/transform.js +251 -0
  28. package/lib/conflict/type.d.ts +55 -0
  29. package/lib/conflict/type.js +17 -0
  30. package/lib/conflict/updateDeviceDp.d.ts +1 -0
  31. package/lib/conflict/updateDeviceDp.js +41 -0
  32. package/lib/constant/index.d.ts +5 -0
  33. package/lib/constant/index.js +5 -0
  34. package/lib/context/index.d.ts +3 -0
  35. package/lib/context/index.js +3 -0
  36. package/lib/context/rhythms/actions.d.ts +20 -0
  37. package/lib/context/rhythms/actions.js +52 -0
  38. package/lib/context/rhythms/index.d.ts +5 -0
  39. package/lib/context/rhythms/index.js +51 -0
  40. package/lib/context/rhythms/reducer.d.ts +24 -0
  41. package/lib/context/rhythms/reducer.js +38 -0
  42. package/lib/context/schedule/actions.d.ts +7 -0
  43. package/lib/context/schedule/actions.js +51 -0
  44. package/lib/context/schedule/context.d.ts +6 -0
  45. package/lib/context/schedule/context.js +52 -0
  46. package/lib/context/schedule/index.d.ts +1 -0
  47. package/lib/context/schedule/index.js +1 -0
  48. package/lib/context/schedule/reducer.d.ts +16 -0
  49. package/lib/context/schedule/reducer.js +104 -0
  50. package/lib/context/schedule/type.d.ts +41 -0
  51. package/lib/context/schedule/type.js +11 -0
  52. package/lib/context/timer/actions.d.ts +8 -0
  53. package/lib/context/timer/actions.js +52 -0
  54. package/lib/context/timer/context.d.ts +10 -0
  55. package/lib/context/timer/context.js +53 -0
  56. package/lib/context/timer/index.d.ts +1 -0
  57. package/lib/context/timer/index.js +1 -0
  58. package/lib/context/timer/reducer.d.ts +15 -0
  59. package/lib/context/timer/reducer.js +70 -0
  60. package/lib/context/timer/type.d.ts +46 -0
  61. package/lib/context/timer/type.js +24 -0
  62. package/lib/dpParser/README.md +63 -0
  63. package/lib/dpParser/__test__/cycle.test.d.ts +1 -0
  64. package/lib/dpParser/__test__/cycle.test.js +258 -0
  65. package/lib/dpParser/__test__/random.test.d.ts +1 -0
  66. package/lib/dpParser/__test__/random.test.js +211 -0
  67. package/lib/dpParser/__test__/rhythms.test.d.ts +1 -0
  68. package/lib/dpParser/__test__/rhythms.test.js +71 -0
  69. package/lib/dpParser/__test__/rtcTimer.test.d.ts +1 -0
  70. package/lib/dpParser/__test__/rtcTimer.test.js +235 -0
  71. package/lib/dpParser/__test__/sleep.test.d.ts +1 -0
  72. package/lib/dpParser/__test__/sleep.test.js +71 -0
  73. package/lib/dpParser/__test__/sleepCommon.test.d.ts +1 -0
  74. package/lib/dpParser/__test__/sleepCommon.test.js +197 -0
  75. package/lib/dpParser/__test__/timerReport.test.d.ts +1 -0
  76. package/lib/dpParser/__test__/timerReport.test.js +100 -0
  77. package/lib/dpParser/__test__/wakeup.test.d.ts +1 -0
  78. package/lib/dpParser/__test__/wakeup.test.js +69 -0
  79. package/lib/dpParser/__test__/wakeupCommon.test.d.ts +1 -0
  80. package/lib/dpParser/__test__/wakeupCommon.test.js +131 -0
  81. package/lib/dpParser/cycle/index.d.ts +15 -0
  82. package/lib/dpParser/cycle/index.js +134 -0
  83. package/lib/dpParser/index.d.ts +23 -0
  84. package/lib/dpParser/index.js +43 -0
  85. package/lib/dpParser/random/index.d.ts +15 -0
  86. package/lib/dpParser/random/index.js +127 -0
  87. package/lib/dpParser/rhythms.d.ts +41 -0
  88. package/lib/dpParser/rhythms.js +116 -0
  89. package/lib/dpParser/rtcTimer.d.ts +17 -0
  90. package/lib/dpParser/rtcTimer.js +300 -0
  91. package/lib/dpParser/sleep/index.d.ts +9 -0
  92. package/lib/dpParser/sleep/index.js +29 -0
  93. package/lib/dpParser/sleep/sleepCommon.d.ts +11 -0
  94. package/lib/dpParser/sleep/sleepCommon.js +117 -0
  95. package/lib/dpParser/sleep/sleepSigmesh.d.ts +0 -0
  96. package/lib/dpParser/sleep/sleepSigmesh.js +101 -0
  97. package/lib/dpParser/timerReport.d.ts +11 -0
  98. package/lib/dpParser/timerReport.js +43 -0
  99. package/lib/dpParser/utils.d.ts +27 -0
  100. package/lib/dpParser/utils.js +53 -0
  101. package/lib/dpParser/wakeup/index.d.ts +10 -0
  102. package/lib/dpParser/wakeup/index.js +34 -0
  103. package/lib/dpParser/wakeup/wakeupCommon.d.ts +11 -0
  104. package/lib/dpParser/wakeup/wakeupCommon.js +106 -0
  105. package/lib/dpParser/wakeup/wakeupSigmesh.d.ts +15 -0
  106. package/lib/dpParser/wakeup/wakeupSigmesh.js +86 -0
  107. package/lib/hooks/__test__/useCommon.test.d.ts +1 -0
  108. package/lib/hooks/__test__/useCommon.test.js +72 -0
  109. package/lib/hooks/__test__/useCountdownDp.test.d.ts +1 -0
  110. package/lib/hooks/__test__/useCountdownDp.test.js +82 -0
  111. package/lib/hooks/__test__/useCycleDp.test.d.ts +1 -0
  112. package/lib/hooks/__test__/useCycleDp.test.js +92 -0
  113. package/lib/hooks/__test__/useRandomDp.test.d.ts +1 -0
  114. package/lib/hooks/__test__/useRandomDp.test.js +88 -0
  115. package/lib/hooks/__test__/useRhythmsDp.test.d.ts +1 -0
  116. package/lib/hooks/__test__/useRhythmsDp.test.js +81 -0
  117. package/lib/hooks/__test__/useSleepDp.test.d.ts +1 -0
  118. package/lib/hooks/__test__/useSleepDp.test.js +127 -0
  119. package/lib/hooks/__test__/useTimeFlush.test.d.ts +1 -0
  120. package/lib/hooks/__test__/useTimeFlush.test.js +109 -0
  121. package/lib/hooks/__test__/useTimerDp.test.d.ts +1 -0
  122. package/lib/hooks/__test__/useTimerDp.test.js +79 -0
  123. package/lib/hooks/__test__/useTimerOperate.test.d.ts +1 -0
  124. package/lib/hooks/__test__/useTimerOperate.test.js +286 -0
  125. package/lib/hooks/__test__/useTimerOperateCloud.test.d.ts +1 -0
  126. package/lib/hooks/__test__/useTimerOperateCloud.test.js +190 -0
  127. package/lib/hooks/__test__/useTimerOperateLocal.test.d.ts +1 -0
  128. package/lib/hooks/__test__/useTimerOperateLocal.test.js +216 -0
  129. package/lib/hooks/__test__/useTimerReportDp.test.d.ts +1 -0
  130. package/lib/hooks/__test__/useTimerReportDp.test.js +93 -0
  131. package/lib/hooks/__test__/useTimerSupport.test.d.ts +1 -0
  132. package/lib/hooks/__test__/useTimerSupport.test.js +110 -0
  133. package/lib/hooks/__test__/useWakeUpDp.test.d.ts +1 -0
  134. package/lib/hooks/__test__/useWakeUpDp.test.js +129 -0
  135. package/lib/hooks/useBaseLightDp.d.ts +21 -0
  136. package/lib/hooks/useBaseLightDp.js +165 -0
  137. package/lib/hooks/useCommon.d.ts +29 -0
  138. package/lib/hooks/useCommon.js +59 -0
  139. package/lib/hooks/useCommonSupport.d.ts +25 -0
  140. package/lib/hooks/useCommonSupport.js +241 -0
  141. package/lib/hooks/useCountdownDp.d.ts +13 -0
  142. package/lib/hooks/useCountdownDp.js +28 -0
  143. package/lib/hooks/useCountdownSupport.d.ts +9 -0
  144. package/lib/hooks/useCountdownSupport.js +13 -0
  145. package/lib/hooks/useCycleDp.d.ts +7 -0
  146. package/lib/hooks/useCycleDp.js +21 -0
  147. package/lib/hooks/useCycleSupport.d.ts +12 -0
  148. package/lib/hooks/useCycleSupport.js +16 -0
  149. package/lib/hooks/useRandomDp.d.ts +7 -0
  150. package/lib/hooks/useRandomDp.js +21 -0
  151. package/lib/hooks/useRandomSupport.d.ts +12 -0
  152. package/lib/hooks/useRandomSupport.js +16 -0
  153. package/lib/hooks/useRhythmsDp.d.ts +7 -0
  154. package/lib/hooks/useRhythmsDp.js +23 -0
  155. package/lib/hooks/useSleepDp.d.ts +6 -0
  156. package/lib/hooks/useSleepDp.js +21 -0
  157. package/lib/hooks/useSleepSupport.d.ts +8 -0
  158. package/lib/hooks/useSleepSupport.js +13 -0
  159. package/lib/hooks/useTimeFlush.d.ts +8 -0
  160. package/lib/hooks/useTimeFlush.js +31 -0
  161. package/lib/hooks/useTimerDp.d.ts +5 -0
  162. package/lib/hooks/useTimerDp.js +25 -0
  163. package/lib/hooks/useTimerFlushList.d.ts +11 -0
  164. package/lib/hooks/useTimerFlushList.js +53 -0
  165. package/lib/hooks/useTimerOperate.d.ts +68 -0
  166. package/lib/hooks/useTimerOperate.js +403 -0
  167. package/lib/hooks/useTimerOperateCloud.d.ts +31 -0
  168. package/lib/hooks/useTimerOperateCloud.js +233 -0
  169. package/lib/hooks/useTimerOperateLocal.d.ts +40 -0
  170. package/lib/hooks/useTimerOperateLocal.js +310 -0
  171. package/lib/hooks/useTimerReportDp.d.ts +15 -0
  172. package/lib/hooks/useTimerReportDp.js +48 -0
  173. package/lib/hooks/useTimerSupport.d.ts +13 -0
  174. package/lib/hooks/useTimerSupport.js +38 -0
  175. package/lib/hooks/useWakeUpDp.d.ts +6 -0
  176. package/lib/hooks/useWakeUpDp.js +21 -0
  177. package/lib/hooks/useWakeUpSupport.d.ts +8 -0
  178. package/lib/hooks/useWakeUpSupport.js +13 -0
  179. package/lib/hooks/useWakeupDp.d.ts +6 -0
  180. package/lib/hooks/useWakeupDp.js +21 -0
  181. package/lib/hooks/useWakeupSupport.d.ts +8 -0
  182. package/lib/hooks/useWakeupSupport.js +13 -0
  183. package/lib/i18n/index.d.ts +142 -0
  184. package/lib/i18n/index.js +7 -0
  185. package/lib/i18n/rhythms.d.ts +73 -0
  186. package/lib/i18n/rhythms.js +72 -0
  187. package/lib/i18n/strings.d.ts +73 -0
  188. package/lib/i18n/strings.js +6 -0
  189. package/lib/index.d.ts +51 -0
  190. package/lib/index.js +64 -0
  191. package/lib/types/countdown.d.ts +3 -0
  192. package/lib/types/countdown.js +1 -0
  193. package/lib/types/cycle.d.ts +22 -0
  194. package/lib/types/cycle.js +1 -0
  195. package/lib/types/global.d.ts +9 -0
  196. package/lib/types/index.d.ts +331 -0
  197. package/lib/types/index.js +76 -0
  198. package/lib/types/random.d.ts +20 -0
  199. package/lib/types/random.js +1 -0
  200. package/lib/types/rhythms.d.ts +79 -0
  201. package/lib/types/rhythms.js +15 -0
  202. package/lib/types/sleep.d.ts +36 -0
  203. package/lib/types/sleep.js +1 -0
  204. package/lib/types/timer.d.ts +107 -0
  205. package/lib/types/timer.js +1 -0
  206. package/lib/types/wakeup.d.ts +38 -0
  207. package/lib/types/wakeup.js +1 -0
  208. package/lib/utils/ScheduleCache.d.ts +19 -0
  209. package/lib/utils/ScheduleCache.js +39 -0
  210. package/lib/utils/ScheduleCloudProperty.d.ts +75 -0
  211. package/lib/utils/ScheduleCloudProperty.js +220 -0
  212. package/lib/utils/ScheduleDataSync.d.ts +82 -0
  213. package/lib/utils/ScheduleDataSync.js +127 -0
  214. package/lib/utils/ScheduleEmit.d.ts +67 -0
  215. package/lib/utils/ScheduleEmit.js +61 -0
  216. package/lib/utils/ScheduleLogger.d.ts +19 -0
  217. package/lib/utils/ScheduleLogger.js +71 -0
  218. package/lib/utils/ScheduleSupport.d.ts +131 -0
  219. package/lib/utils/ScheduleSupport.js +373 -0
  220. package/lib/utils/ScheduleUtils.d.ts +12 -0
  221. package/lib/utils/ScheduleUtils.js +51 -0
  222. package/lib/utils/__test__/ScheduleCloudProperty.test.d.ts +1 -0
  223. package/lib/utils/__test__/ScheduleCloudProperty.test.js +320 -0
  224. package/lib/utils/__test__/ScheduleSupport.test.d.ts +1 -0
  225. package/lib/utils/__test__/ScheduleSupport.test.js +211 -0
  226. package/lib/utils/__test__/ScheduleUtils.test.d.ts +1 -0
  227. package/lib/utils/__test__/ScheduleUtils.test.js +119 -0
  228. package/lib/utils/__test__/common.test.d.ts +1 -0
  229. package/lib/utils/__test__/common.test.js +147 -0
  230. package/lib/utils/__test__/storage.test.d.ts +1 -0
  231. package/lib/utils/__test__/storage.test.js +98 -0
  232. package/lib/utils/__test__/time.test.d.ts +1 -0
  233. package/lib/utils/__test__/time.test.js +146 -0
  234. package/lib/utils/common.d.ts +23 -0
  235. package/lib/utils/common.js +109 -0
  236. package/lib/utils/dpState.d.ts +5 -0
  237. package/lib/utils/dpState.js +27 -0
  238. package/lib/utils/getCachedSystemInfo.d.ts +39 -0
  239. package/lib/utils/getCachedSystemInfo.js +20 -0
  240. package/lib/utils/isInIDE.d.ts +1 -0
  241. package/lib/utils/isInIDE.js +5 -0
  242. package/lib/utils/storage.d.ts +5 -0
  243. package/lib/utils/storage.js +32 -0
  244. package/lib/utils/time.d.ts +40 -0
  245. package/lib/utils/time.js +84 -0
  246. package/package.json +78 -0
@@ -0,0 +1,385 @@
1
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
+ import { Conflict } from '../index';
3
+ import { scheduleLogger } from '../../utils/ScheduleLogger';
4
+ import { checkConflicts } from '../ConflictResolver';
5
+ import { ScheduleDataManager } from '../scheduleDataManager';
6
+ import { transScheduleListToConflictList } from '../transform';
7
+ import { EScheduleFunctionType } from '../../types';
8
+ // 模拟日志函数
9
+ jest.mock('../../utils/ScheduleLogger', () => ({
10
+ scheduleLogger: {
11
+ debug: jest.fn(),
12
+ error: jest.fn(),
13
+ warn: jest.fn(),
14
+ info: jest.fn()
15
+ }
16
+ }));
17
+
18
+ // 模拟 ConflictResolver
19
+ jest.mock('../ConflictResolver', () => ({
20
+ checkConflicts: jest.fn()
21
+ }));
22
+
23
+ // 模拟 ScheduleDataManager
24
+ jest.mock('../scheduleDataManager', () => {
25
+ const mockInstance = {
26
+ getData: jest.fn(),
27
+ setData: jest.fn(),
28
+ clearData: jest.fn(),
29
+ deleteData: jest.fn()
30
+ };
31
+ return {
32
+ ScheduleDataManager: {
33
+ getInstance: jest.fn(() => mockInstance)
34
+ }
35
+ };
36
+ });
37
+
38
+ // 模拟 transform
39
+ jest.mock('../transform', () => ({
40
+ transScheduleListToConflictList: jest.fn()
41
+ }));
42
+ describe('Conflict', () => {
43
+ // 在每个测试前重置模拟函数和 Conflict 类的状态
44
+ beforeEach(() => {
45
+ jest.clearAllMocks();
46
+ Conflict.rule = undefined;
47
+ Conflict.isInit = false;
48
+ });
49
+ describe('addConflictRule', () => {
50
+ it('应该添加冲突规则', () => {
51
+ const rule = {
52
+ isConflict: jest.fn(),
53
+ shouldIgnore: jest.fn()
54
+ };
55
+ Conflict.addConflictRule(rule);
56
+ expect(Conflict.rule).toBe(rule);
57
+ expect(scheduleLogger.debug).toHaveBeenCalled();
58
+ });
59
+ });
60
+ describe('add', () => {
61
+ it('应该添加无冲突的日程', () => {
62
+ const schedule = {
63
+ type: EScheduleFunctionType.TIMER,
64
+ detail: [{
65
+ timerId: '1',
66
+ time: '10:00',
67
+ loops: '1000000',
68
+ status: true,
69
+ dps: {},
70
+ aliasName: '',
71
+ isAppPush: false
72
+ }]
73
+ };
74
+ const transformedSchedule = {
75
+ id: 'timer_10:00_1000000',
76
+ type: EScheduleFunctionType.TIMER,
77
+ data: {
78
+ status: true,
79
+ weeks: [1, 0, 0, 0, 0, 0, 0],
80
+ startTime: '10:00',
81
+ endTime: '10:00'
82
+ }
83
+ };
84
+ const mockInstance = ScheduleDataManager.getInstance();
85
+ transScheduleListToConflictList.mockReturnValue([transformedSchedule]);
86
+ checkConflicts.mockReturnValue([]);
87
+ const result = Conflict.add(schedule);
88
+ expect(result).toEqual({
89
+ isConflict: false,
90
+ conflictList: []
91
+ });
92
+ expect(mockInstance.setData).toHaveBeenCalledWith(transformedSchedule);
93
+ expect(scheduleLogger.debug).toHaveBeenCalled();
94
+ });
95
+ it('应该检测到冲突并不添加日程', () => {
96
+ const schedule = {
97
+ type: EScheduleFunctionType.TIMER,
98
+ detail: [{
99
+ timerId: '11',
100
+ time: '10:00',
101
+ loops: '1000000',
102
+ status: true,
103
+ dps: {},
104
+ aliasName: '',
105
+ isAppPush: false
106
+ }]
107
+ };
108
+ const transformedSchedule = {
109
+ id: 'timer_10:00_1000000',
110
+ type: EScheduleFunctionType.TIMER,
111
+ data: {
112
+ status: true,
113
+ weeks: [1, 0, 0, 0, 0, 0, 0],
114
+ startTime: '10:00',
115
+ endTime: '10:00'
116
+ }
117
+ };
118
+ const existingSchedule = {
119
+ id: 'timer_10:00_0100000',
120
+ type: EScheduleFunctionType.TIMER,
121
+ data: {
122
+ status: true,
123
+ weeks: [0, 1, 0, 0, 0, 0, 0],
124
+ startTime: '10:00',
125
+ endTime: '10:00'
126
+ }
127
+ };
128
+ const conflictResult = {
129
+ current: transformedSchedule,
130
+ other: existingSchedule
131
+ };
132
+ const mockInstance = ScheduleDataManager.getInstance();
133
+ transScheduleListToConflictList.mockReturnValue([transformedSchedule]);
134
+ checkConflicts.mockReturnValue([conflictResult]);
135
+ const result = Conflict.add(schedule);
136
+ expect(result).toEqual({
137
+ isConflict: true,
138
+ conflictList: [conflictResult]
139
+ });
140
+ expect(mockInstance.setData).not.toHaveBeenCalled();
141
+ });
142
+ });
143
+ describe('remove', () => {
144
+ it('应该删除日程', () => {
145
+ const schedule = {
146
+ type: EScheduleFunctionType.TIMER,
147
+ detail: [{
148
+ timerId: '1',
149
+ time: '10:00',
150
+ loops: '1000000',
151
+ status: true,
152
+ dps: {},
153
+ aliasName: '',
154
+ isAppPush: false
155
+ }]
156
+ };
157
+ const transformedSchedule = {
158
+ id: 'timer_10:00_1000000',
159
+ type: EScheduleFunctionType.TIMER,
160
+ data: {
161
+ status: true,
162
+ weeks: [1, 0, 0, 0, 0, 0, 0],
163
+ startTime: '10:00',
164
+ endTime: '10:00'
165
+ }
166
+ };
167
+ const mockInstance = ScheduleDataManager.getInstance();
168
+ transScheduleListToConflictList.mockReturnValue([transformedSchedule]);
169
+ Conflict.remove(schedule);
170
+ expect(mockInstance.deleteData).toHaveBeenCalledWith(transformedSchedule);
171
+ expect(scheduleLogger.debug).toHaveBeenCalled();
172
+ });
173
+ });
174
+ describe('update', () => {
175
+ it('应该更新无冲突的日程', () => {
176
+ const currentSchedule = {
177
+ type: EScheduleFunctionType.TIMER,
178
+ detail: [{
179
+ timerId: '1',
180
+ time: '11:00',
181
+ loops: '1000000',
182
+ status: true,
183
+ dps: {},
184
+ aliasName: '',
185
+ isAppPush: false
186
+ }]
187
+ };
188
+ const prevSchedule = {
189
+ type: EScheduleFunctionType.TIMER,
190
+ detail: [{
191
+ timerId: '1',
192
+ time: '10:00',
193
+ loops: '1000000',
194
+ status: true,
195
+ dps: {},
196
+ aliasName: '',
197
+ isAppPush: false
198
+ }]
199
+ };
200
+ const transformedCurrent = {
201
+ id: 'timer_11:00_1000000',
202
+ type: EScheduleFunctionType.TIMER,
203
+ data: {
204
+ status: true,
205
+ weeks: [1, 0, 0, 0, 0, 0, 0],
206
+ startTime: '11:00',
207
+ endTime: '11:00'
208
+ }
209
+ };
210
+ const transformedPrev = {
211
+ id: 'timer_10:00_1000000',
212
+ type: EScheduleFunctionType.TIMER,
213
+ data: {
214
+ status: true,
215
+ weeks: [1, 0, 0, 0, 0, 0, 0],
216
+ startTime: '10:00',
217
+ endTime: '10:00'
218
+ }
219
+ };
220
+ const mockInstance = ScheduleDataManager.getInstance();
221
+ transScheduleListToConflictList.mockReturnValue([transformedCurrent, transformedPrev]);
222
+ checkConflicts.mockReturnValue([]);
223
+ const result = Conflict.update(currentSchedule, prevSchedule);
224
+ expect(result).toEqual({
225
+ isConflict: false,
226
+ conflictList: []
227
+ });
228
+ expect(mockInstance.setData).toHaveBeenCalledWith(_objectSpread(_objectSpread({}, transformedCurrent), {}, {
229
+ prevId: transformedPrev.id
230
+ }));
231
+ });
232
+ it('应该检测到冲突并不更新日程', () => {
233
+ const currentSchedule = {
234
+ type: EScheduleFunctionType.TIMER,
235
+ detail: [{
236
+ timerId: '1',
237
+ time: '11:00',
238
+ loops: '1000000',
239
+ status: true,
240
+ dps: {},
241
+ aliasName: '',
242
+ isAppPush: false
243
+ }]
244
+ };
245
+ const prevSchedule = {
246
+ type: EScheduleFunctionType.TIMER,
247
+ detail: [{
248
+ timerId: '1',
249
+ time: '10:00',
250
+ loops: '1000000',
251
+ status: true,
252
+ dps: {},
253
+ aliasName: '',
254
+ isAppPush: false
255
+ }]
256
+ };
257
+ const transformedCurrent = {
258
+ id: 'timer_11:00_1000000',
259
+ type: EScheduleFunctionType.TIMER,
260
+ data: {
261
+ status: true,
262
+ weeks: [1, 0, 0, 0, 0, 0, 0],
263
+ startTime: '11:00',
264
+ endTime: '11:00'
265
+ }
266
+ };
267
+ const transformedPrev = {
268
+ id: 'timer_10:00_1000000',
269
+ type: EScheduleFunctionType.TIMER,
270
+ data: {
271
+ status: true,
272
+ weeks: [1, 0, 0, 0, 0, 0, 0],
273
+ startTime: '10:00',
274
+ endTime: '10:00'
275
+ }
276
+ };
277
+ const existingSchedule = {
278
+ id: 'timer_11:00_0100000',
279
+ type: EScheduleFunctionType.TIMER,
280
+ data: {
281
+ status: true,
282
+ weeks: [0, 1, 0, 0, 0, 0, 0],
283
+ startTime: '11:00',
284
+ endTime: '11:00'
285
+ }
286
+ };
287
+ const conflictResult = {
288
+ current: _objectSpread(_objectSpread({}, transformedCurrent), {}, {
289
+ prevId: transformedPrev.id
290
+ }),
291
+ other: existingSchedule
292
+ };
293
+ const mockInstance = ScheduleDataManager.getInstance();
294
+ transScheduleListToConflictList.mockReturnValue([transformedCurrent, transformedPrev]);
295
+ checkConflicts.mockReturnValue([conflictResult]);
296
+ const result = Conflict.update(currentSchedule, prevSchedule);
297
+ expect(result).toEqual({
298
+ isConflict: true,
299
+ conflictList: [conflictResult]
300
+ });
301
+ expect(mockInstance.deleteData).not.toHaveBeenCalled();
302
+ expect(mockInstance.setData).not.toHaveBeenCalled();
303
+ });
304
+ it('应该处理缺少 prevSchedule 的情况', () => {
305
+ const currentSchedule = {
306
+ type: EScheduleFunctionType.TIMER,
307
+ detail: [{
308
+ timerId: '1',
309
+ time: '11:00',
310
+ loops: '1000000',
311
+ status: true,
312
+ dps: {},
313
+ aliasName: '',
314
+ isAppPush: false
315
+ }]
316
+ };
317
+ Conflict.update(currentSchedule, null);
318
+ expect(scheduleLogger.error).toHaveBeenCalled();
319
+ });
320
+ });
321
+ describe('init', () => {
322
+ it('应该初始化日程数据', () => {
323
+ const scheduleList = [{
324
+ type: EScheduleFunctionType.TIMER,
325
+ detail: [{
326
+ timerId: '1',
327
+ time: '10:00',
328
+ loops: '1000000',
329
+ status: true,
330
+ dps: {},
331
+ aliasName: '',
332
+ isAppPush: false
333
+ }]
334
+ }, {
335
+ type: EScheduleFunctionType.RANDOM,
336
+ detail: [{
337
+ startTime: 600,
338
+ endTime: 720,
339
+ loops: '0100000',
340
+ onOff: true,
341
+ channel: 1,
342
+ index: 1,
343
+ color: {
344
+ hue: 0,
345
+ saturation: 0,
346
+ value: 1,
347
+ brightness: 0,
348
+ temperature: 0
349
+ }
350
+ }]
351
+ }];
352
+ const transformedSchedules = [{
353
+ id: 'timer_10:00_1000000',
354
+ type: EScheduleFunctionType.TIMER,
355
+ data: {
356
+ status: true,
357
+ weeks: [1, 0, 0, 0, 0, 0, 0],
358
+ startTime: '10:00',
359
+ endTime: '10:00'
360
+ }
361
+ }, {
362
+ id: 'random_600_720_0100000',
363
+ type: EScheduleFunctionType.RANDOM,
364
+ data: {
365
+ status: true,
366
+ weeks: [0, 1, 0, 0, 0, 0, 0],
367
+ startTime: '10:00',
368
+ endTime: '12:00'
369
+ }
370
+ }];
371
+ const mockInstance = ScheduleDataManager.getInstance();
372
+ transScheduleListToConflictList.mockReturnValue(transformedSchedules);
373
+ Conflict.init(scheduleList);
374
+ expect(mockInstance.setData).toHaveBeenCalledTimes(1);
375
+ expect(mockInstance.setData).toHaveBeenCalledWith(transformedSchedules);
376
+ });
377
+ it('应该处理无效的日程列表', () => {
378
+ const mockInstance = ScheduleDataManager.getInstance();
379
+
380
+ // @ts-ignore - 故意传入无效数据进行测试
381
+ Conflict.init(null);
382
+ expect(mockInstance.setData).not.toHaveBeenCalled();
383
+ });
384
+ });
385
+ });
@@ -0,0 +1,170 @@
1
+ import { ScheduleDataManager } from '../scheduleDataManager';
2
+ import { EScheduleFunctionType } from '../../types';
3
+ describe('ScheduleDataManager', () => {
4
+ // 每个测试前重置单例
5
+ beforeEach(() => {
6
+ // 清空数据
7
+ ScheduleDataManager.getInstance().clearData();
8
+ });
9
+
10
+ // 测试单例模式
11
+ it('should be a singleton', () => {
12
+ const instance1 = ScheduleDataManager.getInstance();
13
+ const instance2 = ScheduleDataManager.getInstance();
14
+ expect(instance1).toBe(instance2);
15
+ });
16
+
17
+ // 测试获取数据
18
+ it('should return empty array when no data is set', () => {
19
+ const instance = ScheduleDataManager.getInstance();
20
+ expect(instance.getData()).toEqual([]);
21
+ });
22
+
23
+ // 测试设置单个数据项
24
+ it('should set a single data item correctly', () => {
25
+ const instance = ScheduleDataManager.getInstance();
26
+ const schedule = {
27
+ id: 'test_1',
28
+ type: EScheduleFunctionType.TIMER,
29
+ data: {
30
+ status: true,
31
+ weeks: [1, 0, 0, 0, 0, 0, 0],
32
+ startTime: '10:00',
33
+ endTime: '11:00'
34
+ }
35
+ };
36
+ instance.setData(schedule);
37
+ const data = instance.getData();
38
+ expect(data.length).toBe(1);
39
+ expect(data[0]).toEqual(schedule);
40
+ });
41
+
42
+ // 测试设置多个数据项
43
+ it('should set multiple data items correctly', () => {
44
+ const instance = ScheduleDataManager.getInstance();
45
+ const schedule1 = {
46
+ id: 'test_1',
47
+ type: EScheduleFunctionType.TIMER,
48
+ data: {
49
+ status: true,
50
+ weeks: [1, 0, 0, 0, 0, 0, 0],
51
+ startTime: '10:00',
52
+ endTime: '11:00'
53
+ }
54
+ };
55
+ const schedule2 = {
56
+ id: 'test_2',
57
+ type: EScheduleFunctionType.TIMER,
58
+ data: {
59
+ status: true,
60
+ weeks: [0, 1, 0, 0, 0, 0, 0],
61
+ startTime: '12:00',
62
+ endTime: '13:00'
63
+ }
64
+ };
65
+ instance.setData([schedule1, schedule2]);
66
+ const data = instance.getData();
67
+ expect(data.length).toBe(2);
68
+ expect(data).toContainEqual(schedule1);
69
+ expect(data).toContainEqual(schedule2);
70
+ });
71
+
72
+ // 测试更新已存在的数据项
73
+ it('should update existing data item when id is the same', () => {
74
+ const instance = ScheduleDataManager.getInstance();
75
+ const schedule1 = {
76
+ id: 'test_1',
77
+ type: EScheduleFunctionType.TIMER,
78
+ data: {
79
+ status: true,
80
+ weeks: [1, 0, 0, 0, 0, 0, 0],
81
+ startTime: '10:00',
82
+ endTime: '11:00'
83
+ }
84
+ };
85
+ const schedule2 = {
86
+ id: 'test_1',
87
+ // 相同的ID
88
+ type: EScheduleFunctionType.TIMER,
89
+ data: {
90
+ status: true,
91
+ weeks: [1, 0, 0, 0, 0, 0, 0],
92
+ startTime: '12:00',
93
+ // 不同的时间
94
+ endTime: '13:00'
95
+ }
96
+ };
97
+ instance.setData(schedule1);
98
+ instance.setData(schedule2);
99
+ const data = instance.getData();
100
+ expect(data.length).toBe(1);
101
+ expect(data[0]).toEqual(schedule2); // 应该是更新后的数据
102
+ });
103
+
104
+ // 测试清空数据
105
+ it('should clear all data', () => {
106
+ const instance = ScheduleDataManager.getInstance();
107
+ const schedule = {
108
+ id: 'test_1',
109
+ type: EScheduleFunctionType.TIMER,
110
+ data: {
111
+ status: true,
112
+ weeks: [1, 0, 0, 0, 0, 0, 0],
113
+ startTime: '10:00',
114
+ endTime: '11:00'
115
+ }
116
+ };
117
+ instance.setData(schedule);
118
+ expect(instance.getData().length).toBe(1);
119
+ instance.clearData();
120
+ expect(instance.getData().length).toBe(0);
121
+ });
122
+
123
+ // 测试删除数据
124
+ it('should delete a specific data item', () => {
125
+ const instance = ScheduleDataManager.getInstance();
126
+ const schedule1 = {
127
+ id: 'test_1',
128
+ type: EScheduleFunctionType.TIMER,
129
+ data: {
130
+ status: true,
131
+ weeks: [1, 0, 0, 0, 0, 0, 0],
132
+ startTime: '10:00',
133
+ endTime: '11:00'
134
+ }
135
+ };
136
+ const schedule2 = {
137
+ id: 'test_2',
138
+ type: EScheduleFunctionType.TIMER,
139
+ data: {
140
+ status: true,
141
+ weeks: [0, 1, 0, 0, 0, 0, 0],
142
+ startTime: '12:00',
143
+ endTime: '13:00'
144
+ }
145
+ };
146
+ instance.setData([schedule1, schedule2]);
147
+ expect(instance.getData().length).toBe(2);
148
+ instance.deleteData(schedule1);
149
+ const data = instance.getData();
150
+ expect(data.length).toBe(1);
151
+ expect(data[0]).toEqual(schedule2);
152
+ });
153
+
154
+ // 测试处理无效数据
155
+ it('should handle invalid data gracefully', () => {
156
+ const instance = ScheduleDataManager.getInstance();
157
+
158
+ // @ts-ignore - 故意传入无效数据进行测试
159
+ instance.setData(null);
160
+ expect(instance.getData().length).toBe(0);
161
+
162
+ // @ts-ignore - 故意传入无效数据进行测试
163
+ instance.setData({});
164
+ expect(instance.getData().length).toBe(0);
165
+
166
+ // @ts-ignore - 故意传入无效数据进行测试
167
+ instance.deleteData(null);
168
+ expect(instance.getData().length).toBe(0);
169
+ });
170
+ });
@@ -0,0 +1 @@
1
+ export {};