mm_config 2.1.5 → 2.1.7

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.
package/test.js DELETED
@@ -1,199 +0,0 @@
1
- const { create, createAsync, Config, clear, clearAll } = require('./index.js');
2
-
3
- // 测试同步API
4
- function testSyncConfig() {
5
- $.log.debug('=== 测试同步配置 API ===');
6
-
7
- // 方式1:提供配置对象和文件路径
8
- const config1 = create("./config1.json", {
9
- name: "demo",
10
- state: 1
11
- });
12
-
13
- // 修改配置,会自动保存
14
- config1.name = "test1";
15
- config1.sort = 1;
16
- $.log.debug('配置1:', config1);
17
-
18
- // 方式2:创建空配置
19
- const config2 = create("./config2.json");
20
-
21
- config2.name = "test2";
22
- config2.sort = 2;
23
- $.log.debug('配置2:', config2);
24
-
25
- // 从文件加载配置
26
- const config3 = create("./config1.json");
27
- $.log.debug('从文件加载的配置:', config3);
28
-
29
- // 手动保存
30
- config3.age = 26;
31
- config3._saveSync(); // 显式调用保存
32
- $.log.debug('更新后的配置3:', config3);
33
- }
34
-
35
- // 测试异步API
36
- async function testAsyncConfig() {
37
- $.log.debug('\n=== 测试异步配置 API ===');
38
-
39
- try {
40
- // 创建异步配置
41
- const configAsync = await createAsync("./config_async.json", {
42
- name: "asyncDemo",
43
- state: 3
44
- });
45
-
46
- // 修改配置,会异步自动保存
47
- configAsync.name = "asyncTest2";
48
- configAsync.timestamp = new Date().toISOString();
49
- $.log.debug('异步配置:', configAsync);
50
-
51
- // 显式调用异步保存
52
- // await configAsync._saveAsync();
53
- $.log.debug('异步保存完成');
54
-
55
- } catch (err) {
56
- $.log.error('异步配置测试出错:', err);
57
- }
58
- }
59
-
60
- // 测试高级功能:防抖
61
- function testDebounce() {
62
- $.log.debug('\n=== 测试防抖功能 ===');
63
-
64
- // 创建带防抖的配置(500ms延迟保存)
65
- const configDebounce = create("./config_debounce.json", {
66
- name: "debounceDemo"
67
- }, {
68
- debounce_time: 500, // 500ms防抖时间
69
- pretty: true // 美化JSON输出
70
- });
71
-
72
- // 快速多次修改,应该只保存最后一次
73
- $.log.debug('开始多次快速修改...');
74
- for (let i = 0; i < 5; i++) {
75
- configDebounce.value = i;
76
- configDebounce.timestamp = new Date().toISOString();
77
- }
78
-
79
- $.log.debug('修改完成,等待防抖触发保存...');
80
- $.log.debug('最终配置:', configDebounce);
81
- }
82
-
83
- // 测试JSON5格式
84
- function testJson5() {
85
- $.log.debug('\n=== 测试JSON5格式支持 ===');
86
-
87
- // 创建配置时指定JSON5格式
88
- const configJson5 = create("./config_json5.json", {
89
- name: "json5Demo"
90
- }, {
91
- format: 'json5',
92
- pretty: true
93
- });
94
-
95
- configJson5.description = "This uses JSON5 format";
96
- $.log.debug('JSON5配置:', configJson5);
97
- }
98
-
99
- /**
100
- * 测试配置方法
101
- */
102
- async function testMethods() {
103
- $.log.debug('\n=== 测试配置方法 ===');
104
-
105
- // 创建测试配置
106
- const cg = new Config({
107
- name: "methodsDemo",
108
- sort: 100,
109
- state: 1
110
- });
111
-
112
- // 测试设置和获取
113
- await cg.set('name', 'testMethods');
114
- const name = await cg.get('name');
115
- $.log.debug('获取到的名称:', name);
116
-
117
- // 测试检查存在
118
- const hasName = await cg.has('name');
119
- $.log.debug('是否存在名称:', hasName);
120
-
121
- // 测试删除
122
- await cg.del('name');
123
- const hasNameAfterDelete = await cg.has('name');
124
- $.log.debug('删除后是否存在名称:', hasNameAfterDelete);
125
-
126
- // 测试获取所有键
127
- const keys = await cg.keys();
128
- $.log.debug('所有键:', keys);
129
-
130
- const state = await cg.get('state');
131
- $.log.debug('获取到的状态:', state);
132
-
133
- // 测试设置选项
134
- await cg.setOptions({
135
- sort: 200,
136
- state: "2"
137
- });
138
- $.log.debug('设置后的选项:', await cg.options);
139
- }
140
-
141
- // 测试缓存清理功能
142
- async function testCacheClear() {
143
- $.log.debug('\n=== 测试缓存清理功能 ===');
144
-
145
- // 创建几个配置实例
146
- const config1 = create("./cache_test1.json", { test: "cache1" });
147
- const config2 = create("./cache_test2.json", { test: "cache2" });
148
- const config3 = await createAsync("./cache_test3.json", { test: "cache3" });
149
-
150
- $.log.debug('创建了3个配置实例');
151
-
152
- // 测试清理单个缓存
153
- clear("./cache_test1.json");
154
- $.log.debug('已清理cache_test1.json的缓存');
155
-
156
- // 重新创建应该会创建新实例
157
- const config1New = create("./cache_test1.json", { test: "cache1_new" });
158
- $.log.debug('重新创建cache_test1.json');
159
-
160
- // 测试清理所有缓存
161
- clearAll();
162
- $.log.debug('已清理所有缓存');
163
-
164
- // 再次创建应该都是新实例
165
- const config2New = create("./cache_test2.json", { test: "cache2_new" });
166
- const config3New = await createAsync("./cache_test3.json", { test: "cache3_new" });
167
- $.log.debug('重新创建所有配置实例');
168
- }
169
-
170
- // 运行所有测试
171
- async function runAllTests() {
172
- $.log.debug('开始运行配置管理器测试...');
173
-
174
- // 运行同步测试
175
- testSyncConfig();
176
-
177
- // 运行异步测试
178
- await testAsyncConfig();
179
-
180
- // 运行防抖测试
181
- testDebounce();
182
-
183
- // 运行JSON5测试
184
- testJson5();
185
-
186
- // 运行缓存清理测试
187
- await testCacheClear();
188
-
189
- $.log.debug('\n所有测试完成!');
190
- $.log.debug('请查看生成的配置文件以验证保存结果。');
191
-
192
- // 运行方法测试
193
- await testMethods();
194
- }
195
-
196
- // 执行测试
197
- runAllTests().catch(err => {
198
- $.log.error('测试过程中出现错误:', err);
199
- });