@yunzai-ng/core 0.1.0

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 (357) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +51 -0
  3. package/dist/adapter/accounts.d.ts +174 -0
  4. package/dist/adapter/accounts.d.ts.map +1 -0
  5. package/dist/adapter/accounts.js +653 -0
  6. package/dist/adapter/accounts.js.map +1 -0
  7. package/dist/adapter/bots.d.ts +142 -0
  8. package/dist/adapter/bots.d.ts.map +1 -0
  9. package/dist/adapter/bots.js +454 -0
  10. package/dist/adapter/bots.js.map +1 -0
  11. package/dist/adapter/host.d.ts +114 -0
  12. package/dist/adapter/host.d.ts.map +1 -0
  13. package/dist/adapter/host.js +90 -0
  14. package/dist/adapter/host.js.map +1 -0
  15. package/dist/adapter/login.d.ts +174 -0
  16. package/dist/adapter/login.d.ts.map +1 -0
  17. package/dist/adapter/login.js +345 -0
  18. package/dist/adapter/login.js.map +1 -0
  19. package/dist/adapter/registry.d.ts +102 -0
  20. package/dist/adapter/registry.d.ts.map +1 -0
  21. package/dist/adapter/registry.js +150 -0
  22. package/dist/adapter/registry.js.map +1 -0
  23. package/dist/config/core-config.d.ts +151 -0
  24. package/dist/config/core-config.d.ts.map +1 -0
  25. package/dist/config/core-config.js +338 -0
  26. package/dist/config/core-config.js.map +1 -0
  27. package/dist/config/schema.d.ts +449 -0
  28. package/dist/config/schema.d.ts.map +1 -0
  29. package/dist/config/schema.js +871 -0
  30. package/dist/config/schema.js.map +1 -0
  31. package/dist/config/store.d.ts +184 -0
  32. package/dist/config/store.d.ts.map +1 -0
  33. package/dist/config/store.js +428 -0
  34. package/dist/config/store.js.map +1 -0
  35. package/dist/config/yaml.d.ts +23 -0
  36. package/dist/config/yaml.d.ts.map +1 -0
  37. package/dist/config/yaml.js +108 -0
  38. package/dist/config/yaml.js.map +1 -0
  39. package/dist/http/client.d.ts +82 -0
  40. package/dist/http/client.d.ts.map +1 -0
  41. package/dist/http/client.js +658 -0
  42. package/dist/http/client.js.map +1 -0
  43. package/dist/index.d.ts +77 -0
  44. package/dist/index.d.ts.map +1 -0
  45. package/dist/index.js +94 -0
  46. package/dist/index.js.map +1 -0
  47. package/dist/kernel/app.d.ts +244 -0
  48. package/dist/kernel/app.d.ts.map +1 -0
  49. package/dist/kernel/app.js +654 -0
  50. package/dist/kernel/app.js.map +1 -0
  51. package/dist/kernel/kv-sink.d.ts +34 -0
  52. package/dist/kernel/kv-sink.d.ts.map +1 -0
  53. package/dist/kernel/kv-sink.js +25 -0
  54. package/dist/kernel/kv-sink.js.map +1 -0
  55. package/dist/kernel/policy.d.ts +77 -0
  56. package/dist/kernel/policy.d.ts.map +1 -0
  57. package/dist/kernel/policy.js +99 -0
  58. package/dist/kernel/policy.js.map +1 -0
  59. package/dist/kernel/runtime.d.ts +117 -0
  60. package/dist/kernel/runtime.d.ts.map +1 -0
  61. package/dist/kernel/runtime.js +197 -0
  62. package/dist/kernel/runtime.js.map +1 -0
  63. package/dist/kernel/sql-sink.d.ts +36 -0
  64. package/dist/kernel/sql-sink.d.ts.map +1 -0
  65. package/dist/kernel/sql-sink.js +114 -0
  66. package/dist/kernel/sql-sink.js.map +1 -0
  67. package/dist/logger/format.d.ts +67 -0
  68. package/dist/logger/format.d.ts.map +1 -0
  69. package/dist/logger/format.js +218 -0
  70. package/dist/logger/format.js.map +1 -0
  71. package/dist/logger/index.d.ts +97 -0
  72. package/dist/logger/index.d.ts.map +1 -0
  73. package/dist/logger/index.js +363 -0
  74. package/dist/logger/index.js.map +1 -0
  75. package/dist/logger/rotate.d.ts +47 -0
  76. package/dist/logger/rotate.d.ts.map +1 -0
  77. package/dist/logger/rotate.js +242 -0
  78. package/dist/logger/rotate.js.map +1 -0
  79. package/dist/message/segment.d.ts +255 -0
  80. package/dist/message/segment.d.ts.map +1 -0
  81. package/dist/message/segment.js +510 -0
  82. package/dist/message/segment.js.map +1 -0
  83. package/dist/message/split.d.ts +34 -0
  84. package/dist/message/split.d.ts.map +1 -0
  85. package/dist/message/split.js +79 -0
  86. package/dist/message/split.js.map +1 -0
  87. package/dist/message/target.d.ts +29 -0
  88. package/dist/message/target.d.ts.map +1 -0
  89. package/dist/message/target.js +34 -0
  90. package/dist/message/target.js.map +1 -0
  91. package/dist/pipeline/cooldown.d.ts +84 -0
  92. package/dist/pipeline/cooldown.d.ts.map +1 -0
  93. package/dist/pipeline/cooldown.js +94 -0
  94. package/dist/pipeline/cooldown.js.map +1 -0
  95. package/dist/pipeline/dispatch.d.ts +98 -0
  96. package/dist/pipeline/dispatch.d.ts.map +1 -0
  97. package/dist/pipeline/dispatch.js +323 -0
  98. package/dist/pipeline/dispatch.js.map +1 -0
  99. package/dist/pipeline/event.d.ts +130 -0
  100. package/dist/pipeline/event.d.ts.map +1 -0
  101. package/dist/pipeline/event.js +538 -0
  102. package/dist/pipeline/event.js.map +1 -0
  103. package/dist/pipeline/middleware.d.ts +63 -0
  104. package/dist/pipeline/middleware.d.ts.map +1 -0
  105. package/dist/pipeline/middleware.js +174 -0
  106. package/dist/pipeline/middleware.js.map +1 -0
  107. package/dist/pipeline/prompt.d.ts +80 -0
  108. package/dist/pipeline/prompt.d.ts.map +1 -0
  109. package/dist/pipeline/prompt.js +155 -0
  110. package/dist/pipeline/prompt.js.map +1 -0
  111. package/dist/pipeline/router.d.ts +98 -0
  112. package/dist/pipeline/router.d.ts.map +1 -0
  113. package/dist/pipeline/router.js +489 -0
  114. package/dist/pipeline/router.js.map +1 -0
  115. package/dist/platform/detect.d.ts +24 -0
  116. package/dist/platform/detect.d.ts.map +1 -0
  117. package/dist/platform/detect.js +178 -0
  118. package/dist/platform/detect.js.map +1 -0
  119. package/dist/platform/paths.d.ts +38 -0
  120. package/dist/platform/paths.d.ts.map +1 -0
  121. package/dist/platform/paths.js +129 -0
  122. package/dist/platform/paths.js.map +1 -0
  123. package/dist/platform/system.d.ts +71 -0
  124. package/dist/platform/system.d.ts.map +1 -0
  125. package/dist/platform/system.js +242 -0
  126. package/dist/platform/system.js.map +1 -0
  127. package/dist/plugin/context.d.ts +92 -0
  128. package/dist/plugin/context.d.ts.map +1 -0
  129. package/dist/plugin/context.js +573 -0
  130. package/dist/plugin/context.js.map +1 -0
  131. package/dist/plugin/define.d.ts +92 -0
  132. package/dist/plugin/define.d.ts.map +1 -0
  133. package/dist/plugin/define.js +82 -0
  134. package/dist/plugin/define.js.map +1 -0
  135. package/dist/plugin/discover.d.ts +106 -0
  136. package/dist/plugin/discover.d.ts.map +1 -0
  137. package/dist/plugin/discover.js +254 -0
  138. package/dist/plugin/discover.js.map +1 -0
  139. package/dist/plugin/events.d.ts +109 -0
  140. package/dist/plugin/events.d.ts.map +1 -0
  141. package/dist/plugin/events.js +209 -0
  142. package/dist/plugin/events.js.map +1 -0
  143. package/dist/plugin/hooks.d.ts +266 -0
  144. package/dist/plugin/hooks.d.ts.map +1 -0
  145. package/dist/plugin/hooks.js +84 -0
  146. package/dist/plugin/hooks.js.map +1 -0
  147. package/dist/plugin/host.d.ts +160 -0
  148. package/dist/plugin/host.d.ts.map +1 -0
  149. package/dist/plugin/host.js +546 -0
  150. package/dist/plugin/host.js.map +1 -0
  151. package/dist/plugin/market.d.ts +264 -0
  152. package/dist/plugin/market.d.ts.map +1 -0
  153. package/dist/plugin/market.js +700 -0
  154. package/dist/plugin/market.js.map +1 -0
  155. package/dist/plugin/services.d.ts +116 -0
  156. package/dist/plugin/services.d.ts.map +1 -0
  157. package/dist/plugin/services.js +213 -0
  158. package/dist/plugin/services.js.map +1 -0
  159. package/dist/plugin/tar.d.ts +65 -0
  160. package/dist/plugin/tar.d.ts.map +1 -0
  161. package/dist/plugin/tar.js +277 -0
  162. package/dist/plugin/tar.js.map +1 -0
  163. package/dist/render/registry.d.ts +127 -0
  164. package/dist/render/registry.d.ts.map +1 -0
  165. package/dist/render/registry.js +306 -0
  166. package/dist/render/registry.js.map +1 -0
  167. package/dist/scheduler/index.d.ts +67 -0
  168. package/dist/scheduler/index.d.ts.map +1 -0
  169. package/dist/scheduler/index.js +279 -0
  170. package/dist/scheduler/index.js.map +1 -0
  171. package/dist/server/api.d.ts +179 -0
  172. package/dist/server/api.d.ts.map +1 -0
  173. package/dist/server/api.js +602 -0
  174. package/dist/server/api.js.map +1 -0
  175. package/dist/server/auth.d.ts +83 -0
  176. package/dist/server/auth.d.ts.map +1 -0
  177. package/dist/server/auth.js +178 -0
  178. package/dist/server/auth.js.map +1 -0
  179. package/dist/server/browse.d.ts +60 -0
  180. package/dist/server/browse.d.ts.map +1 -0
  181. package/dist/server/browse.js +187 -0
  182. package/dist/server/browse.js.map +1 -0
  183. package/dist/server/files.d.ts +27 -0
  184. package/dist/server/files.d.ts.map +1 -0
  185. package/dist/server/files.js +76 -0
  186. package/dist/server/files.js.map +1 -0
  187. package/dist/server/index.d.ts +189 -0
  188. package/dist/server/index.d.ts.map +1 -0
  189. package/dist/server/index.js +1123 -0
  190. package/dist/server/index.js.map +1 -0
  191. package/dist/server/table.d.ts +105 -0
  192. package/dist/server/table.d.ts.map +1 -0
  193. package/dist/server/table.js +289 -0
  194. package/dist/server/table.js.map +1 -0
  195. package/dist/store/index.d.ts +50 -0
  196. package/dist/store/index.d.ts.map +1 -0
  197. package/dist/store/index.js +120 -0
  198. package/dist/store/index.js.map +1 -0
  199. package/dist/store/json.d.ts +73 -0
  200. package/dist/store/json.d.ts.map +1 -0
  201. package/dist/store/json.js +174 -0
  202. package/dist/store/json.js.map +1 -0
  203. package/dist/store/kv.d.ts +126 -0
  204. package/dist/store/kv.d.ts.map +1 -0
  205. package/dist/store/kv.js +232 -0
  206. package/dist/store/kv.js.map +1 -0
  207. package/dist/store/level.d.ts +103 -0
  208. package/dist/store/level.d.ts.map +1 -0
  209. package/dist/store/level.js +119 -0
  210. package/dist/store/level.js.map +1 -0
  211. package/dist/store/memory.d.ts +61 -0
  212. package/dist/store/memory.d.ts.map +1 -0
  213. package/dist/store/memory.js +94 -0
  214. package/dist/store/memory.js.map +1 -0
  215. package/dist/store/sql.d.ts +142 -0
  216. package/dist/store/sql.d.ts.map +1 -0
  217. package/dist/store/sql.js +246 -0
  218. package/dist/store/sql.js.map +1 -0
  219. package/dist/testing/fake.d.ts +109 -0
  220. package/dist/testing/fake.d.ts.map +1 -0
  221. package/dist/testing/fake.js +196 -0
  222. package/dist/testing/fake.js.map +1 -0
  223. package/dist/testing/index.d.ts +16 -0
  224. package/dist/testing/index.d.ts.map +1 -0
  225. package/dist/testing/index.js +16 -0
  226. package/dist/testing/index.js.map +1 -0
  227. package/dist/testing/mock-adapter.d.ts +205 -0
  228. package/dist/testing/mock-adapter.d.ts.map +1 -0
  229. package/dist/testing/mock-adapter.js +309 -0
  230. package/dist/testing/mock-adapter.js.map +1 -0
  231. package/dist/util/deep.d.ts +108 -0
  232. package/dist/util/deep.d.ts.map +1 -0
  233. package/dist/util/deep.js +275 -0
  234. package/dist/util/deep.js.map +1 -0
  235. package/dist/util/defer.d.ts +107 -0
  236. package/dist/util/defer.d.ts.map +1 -0
  237. package/dist/util/defer.js +163 -0
  238. package/dist/util/defer.js.map +1 -0
  239. package/dist/util/dispose.d.ts +88 -0
  240. package/dist/util/dispose.d.ts.map +1 -0
  241. package/dist/util/dispose.js +143 -0
  242. package/dist/util/dispose.js.map +1 -0
  243. package/dist/util/duration.d.ts +28 -0
  244. package/dist/util/duration.d.ts.map +1 -0
  245. package/dist/util/duration.js +77 -0
  246. package/dist/util/duration.js.map +1 -0
  247. package/dist/util/fs.d.ts +110 -0
  248. package/dist/util/fs.d.ts.map +1 -0
  249. package/dist/util/fs.js +269 -0
  250. package/dist/util/fs.js.map +1 -0
  251. package/dist/util/id.d.ts +49 -0
  252. package/dist/util/id.d.ts.map +1 -0
  253. package/dist/util/id.js +87 -0
  254. package/dist/util/id.js.map +1 -0
  255. package/dist/util/lru.d.ts +74 -0
  256. package/dist/util/lru.d.ts.map +1 -0
  257. package/dist/util/lru.js +156 -0
  258. package/dist/util/lru.js.map +1 -0
  259. package/dist/util/queue.d.ts +64 -0
  260. package/dist/util/queue.d.ts.map +1 -0
  261. package/dist/util/queue.js +147 -0
  262. package/dist/util/queue.js.map +1 -0
  263. package/dist/util/text.d.ts +86 -0
  264. package/dist/util/text.d.ts.map +1 -0
  265. package/dist/util/text.js +182 -0
  266. package/dist/util/text.js.map +1 -0
  267. package/package.json +44 -0
  268. package/src/adapter/accounts.ts +758 -0
  269. package/src/adapter/bots.ts +582 -0
  270. package/src/adapter/host.ts +224 -0
  271. package/src/adapter/login.ts +481 -0
  272. package/src/adapter/registry.ts +202 -0
  273. package/src/config/core-config.test.ts +60 -0
  274. package/src/config/core-config.ts +401 -0
  275. package/src/config/schema.test.ts +179 -0
  276. package/src/config/schema.ts +1036 -0
  277. package/src/config/store.test.ts +245 -0
  278. package/src/config/store.ts +506 -0
  279. package/src/config/yaml.ts +119 -0
  280. package/src/http/client.test.ts +568 -0
  281. package/src/http/client.ts +777 -0
  282. package/src/index.ts +106 -0
  283. package/src/kernel/app.test.ts +267 -0
  284. package/src/kernel/app.ts +843 -0
  285. package/src/kernel/kv-sink.ts +58 -0
  286. package/src/kernel/policy.ts +120 -0
  287. package/src/kernel/runtime.test.ts +542 -0
  288. package/src/kernel/runtime.ts +307 -0
  289. package/src/kernel/sql-sink.ts +154 -0
  290. package/src/logger/format.ts +259 -0
  291. package/src/logger/index.ts +427 -0
  292. package/src/logger/rotate.ts +265 -0
  293. package/src/message/segment.test.ts +335 -0
  294. package/src/message/segment.ts +569 -0
  295. package/src/message/split.ts +97 -0
  296. package/src/message/target.ts +48 -0
  297. package/src/pipeline/cooldown.ts +136 -0
  298. package/src/pipeline/dispatch.ts +419 -0
  299. package/src/pipeline/event.ts +735 -0
  300. package/src/pipeline/middleware.test.ts +426 -0
  301. package/src/pipeline/middleware.ts +199 -0
  302. package/src/pipeline/prompt.ts +212 -0
  303. package/src/pipeline/router.test.ts +432 -0
  304. package/src/pipeline/router.ts +575 -0
  305. package/src/platform/detect.ts +178 -0
  306. package/src/platform/paths.ts +147 -0
  307. package/src/platform/system.test.ts +102 -0
  308. package/src/platform/system.ts +289 -0
  309. package/src/plugin/context.test.ts +459 -0
  310. package/src/plugin/context.ts +757 -0
  311. package/src/plugin/define.test.ts +120 -0
  312. package/src/plugin/define.ts +142 -0
  313. package/src/plugin/discover.test.ts +253 -0
  314. package/src/plugin/discover.ts +353 -0
  315. package/src/plugin/events.test.ts +162 -0
  316. package/src/plugin/events.ts +266 -0
  317. package/src/plugin/hooks.ts +373 -0
  318. package/src/plugin/host.test.ts +696 -0
  319. package/src/plugin/host.ts +738 -0
  320. package/src/plugin/market.test.ts +781 -0
  321. package/src/plugin/market.ts +870 -0
  322. package/src/plugin/services.test.ts +135 -0
  323. package/src/plugin/services.ts +261 -0
  324. package/src/plugin/tar.test.ts +158 -0
  325. package/src/plugin/tar.ts +309 -0
  326. package/src/render/registry.test.ts +127 -0
  327. package/src/render/registry.ts +439 -0
  328. package/src/scheduler/index.ts +357 -0
  329. package/src/server/api.test.ts +802 -0
  330. package/src/server/api.ts +869 -0
  331. package/src/server/auth.ts +201 -0
  332. package/src/server/browse.test.ts +193 -0
  333. package/src/server/browse.ts +240 -0
  334. package/src/server/files.ts +97 -0
  335. package/src/server/index.test.ts +566 -0
  336. package/src/server/index.ts +1386 -0
  337. package/src/server/table.ts +351 -0
  338. package/src/store/index.ts +156 -0
  339. package/src/store/json.ts +198 -0
  340. package/src/store/kv.test.ts +307 -0
  341. package/src/store/kv.ts +256 -0
  342. package/src/store/level.ts +167 -0
  343. package/src/store/memory.ts +109 -0
  344. package/src/store/sql.test.ts +204 -0
  345. package/src/store/sql.ts +324 -0
  346. package/src/testing/fake.ts +297 -0
  347. package/src/testing/index.ts +15 -0
  348. package/src/testing/mock-adapter.ts +588 -0
  349. package/src/util/deep.ts +272 -0
  350. package/src/util/defer.ts +211 -0
  351. package/src/util/dispose.ts +169 -0
  352. package/src/util/duration.ts +82 -0
  353. package/src/util/fs.ts +279 -0
  354. package/src/util/id.ts +94 -0
  355. package/src/util/lru.ts +185 -0
  356. package/src/util/queue.ts +167 -0
  357. package/src/util/text.ts +189 -0
@@ -0,0 +1,245 @@
1
+ /**
2
+ * 模块职责:`ConfigStore` / `ConfigFile` 的行为测试
3
+ * 依赖方向:测试文件,依赖 config/*、util/fs
4
+ * 生命周期:每个用例一个临时目录
5
+ * 注意事项:重点覆盖四项 —— 损坏文件不导致崩溃、新增选项自动补齐、
6
+ * 细粒度失效、外部改动热加载。文件内容断言只针对"关键片段",
7
+ * 不锁定整段 YAML 排版,以免排版微调即导致测试失败。
8
+ */
9
+ import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"
10
+ import { tmpdir } from "node:os"
11
+ import { join } from "node:path"
12
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"
13
+ import type { LogLevel, Logger } from "@yunzai-ng/types"
14
+ import { ConfigStore } from "./store.js"
15
+ import { s } from "./schema.js"
16
+
17
+ /** 收集日志的假日志器,用于断言"出错时有提示而不是抛异常" */
18
+ function fakeLogger(): Logger & { lines: string[] } {
19
+ const lines: string[] = []
20
+ const push =
21
+ (level: string) =>
22
+ (msg: unknown, ...args: unknown[]) => {
23
+ lines.push(`${level} ${String(msg)} ${args.map(a => (a instanceof Error ? a.message : String(a))).join(" ")}`.trim())
24
+ }
25
+ const logger: Logger & { lines: string[] } = {
26
+ lines,
27
+ level: "trace" as LogLevel,
28
+ trace: push("trace"),
29
+ debug: push("debug"),
30
+ info: push("info"),
31
+ warn: push("warn"),
32
+ error: push("error"),
33
+ fatal: push("fatal"),
34
+ mark: push("info"),
35
+ child: () => logger,
36
+ isLevelEnabled: () => true
37
+ }
38
+ return logger
39
+ }
40
+
41
+ /** 测试用配置:一个典型的"内核 + 账号数组"形状 */
42
+ const demoSchema = s.object({
43
+ bot: s.object({
44
+ masterQQ: s.ids().default([]).title("主人 QQ"),
45
+ prefix: s.string().default("#").title("命令前缀")
46
+ }),
47
+ render: s.object({
48
+ quality: s.number().min(50).max(100).default(90).title("图片质量")
49
+ })
50
+ })
51
+
52
+ describe("ConfigStore", () => {
53
+ let dir: string
54
+ let logger: ReturnType<typeof fakeLogger>
55
+ let store: ConfigStore
56
+
57
+ beforeEach(async () => {
58
+ dir = await mkdtemp(join(tmpdir(), "yzng-conf-"))
59
+ logger = fakeLogger()
60
+ store = new ConfigStore({ dir, logger, watch: false })
61
+ })
62
+
63
+ afterEach(async () => {
64
+ store.dispose()
65
+ await rm(dir, { recursive: true, force: true })
66
+ })
67
+
68
+ it("首次声明就写出带中文注释的默认配置文件", async () => {
69
+ const conf = await store.define("demo", demoSchema, { title: "示例配置" })
70
+
71
+ expect(conf.get()).toEqual({ bot: { masterQQ: [], prefix: "#" }, render: { quality: 90 } })
72
+
73
+ const text = await readFile(conf.file, "utf8")
74
+ expect(text).toContain("Yunzai NG 配置:示例配置")
75
+ expect(text).toContain("# 主人 QQ")
76
+ expect(text).toContain("# 命令前缀")
77
+ // 注释里带上取值范围,用户不用翻文档
78
+ expect(text).toContain("取值范围:50 ~ 100")
79
+ expect(text).toContain("quality: 90")
80
+ })
81
+
82
+ it("读回自己写出的文件后值不变(序列化/解析闭环)", async () => {
83
+ const first = await store.define("demo", demoSchema)
84
+ await first.patch({ bot: { masterQQ: ["10086"] } })
85
+ const written = await readFile(first.file, "utf8")
86
+
87
+ // 换一个 store 模拟重启
88
+ const store2 = new ConfigStore({ dir, logger, watch: false })
89
+ const second = await store2.define("demo", demoSchema)
90
+ expect(second.get()).toEqual({ bot: { masterQQ: ["10086"], prefix: "#" }, render: { quality: 90 } })
91
+ // 重启不该无谓地改写文件
92
+ expect(await readFile(second.file, "utf8")).toBe(written)
93
+ store2.dispose()
94
+ })
95
+
96
+ it("升级后新增的配置项会自动补进既有文件", async () => {
97
+ // 模拟老版本留下的配置:只有 bot 段,且缺 prefix
98
+ await writeFile(join(dir, "demo.yaml"), "bot:\n masterQQ:\n - '10086'\n", "utf8")
99
+
100
+ const conf = await store.define("demo", demoSchema)
101
+ expect(conf.get()).toEqual({ bot: { masterQQ: ["10086"], prefix: "#" }, render: { quality: 90 } })
102
+
103
+ const text = await readFile(conf.file, "utf8")
104
+ expect(text).toContain("prefix:")
105
+ expect(text).toContain("quality: 90")
106
+ expect(text).toContain("# 图片质量")
107
+ // 用户原有的值一个都不能丢
108
+ expect(text).toContain("10086")
109
+ })
110
+
111
+ it("YAML 语法写坏时退回默认值、记错误、且绝不覆盖用户文件", async () => {
112
+ const broken = "bot:\n masterQQ: [1, 2\n" // 括号没闭合
113
+ const file = join(dir, "demo.yaml")
114
+ await writeFile(file, broken, "utf8")
115
+
116
+ const conf = await store.define("demo", demoSchema)
117
+
118
+ expect(conf.get()).toEqual({ bot: { masterQQ: [], prefix: "#" }, render: { quality: 90 } })
119
+ expect(logger.lines.some(l => l.startsWith("error") && l.includes("YAML 语法有误"))).toBe(true)
120
+ // 关键:坏文件原样留着,用户能照报错去修
121
+ expect(await readFile(file, "utf8")).toBe(broken)
122
+ })
123
+
124
+ it("校验不通过时沿用上一份可用配置,不覆盖用户文件", async () => {
125
+ const conf = await store.define("demo", demoSchema)
126
+ await conf.patch({ render: { quality: 70 } })
127
+
128
+ const bad = "render:\n quality: 999\n"
129
+ await writeFile(conf.file, bad, "utf8")
130
+ const issues = await conf.load("file", false)
131
+
132
+ expect(issues.some(i => i.severity === "error" && i.path === "render.quality")).toBe(true)
133
+ expect(conf.get().render.quality).toBe(70)
134
+ expect(await readFile(conf.file, "utf8")).toBe(bad)
135
+ })
136
+
137
+ it("patch 校验失败时抛错且配置与文件都不变", async () => {
138
+ const conf = await store.define("demo", demoSchema)
139
+ const before = await readFile(conf.file, "utf8")
140
+
141
+ await expect(conf.patch({ render: { quality: 5 } })).rejects.toThrow(/quality/)
142
+ expect(conf.get().render.quality).toBe(90)
143
+ expect(await readFile(conf.file, "utf8")).toBe(before)
144
+ })
145
+
146
+ it("变更事件带上精确的变更路径", async () => {
147
+ const conf = await store.define("demo", demoSchema)
148
+ const seen: string[][] = []
149
+ conf.onChange(change => seen.push(change.paths))
150
+
151
+ await conf.patch({ bot: { prefix: "/" } })
152
+ expect(seen).toEqual([["bot.prefix"]])
153
+ })
154
+
155
+ it("watch(path) 仅通知关注该路径的订阅者", async () => {
156
+ const conf = await store.define("demo", demoSchema)
157
+ const renderCb = vi.fn()
158
+ const botCb = vi.fn()
159
+ conf.watch("render", renderCb)
160
+ conf.watch("bot.masterQQ", botCb)
161
+
162
+ await conf.patch({ render: { quality: 80 } })
163
+ expect(renderCb).toHaveBeenCalledTimes(1)
164
+ expect(botCb).not.toHaveBeenCalled()
165
+
166
+ // 改父路径要通知订阅子路径的人
167
+ await conf.replace({ bot: { masterQQ: ["1"], prefix: "#" }, render: { quality: 80 } })
168
+ expect(botCb).toHaveBeenCalledTimes(1)
169
+ expect(renderCb).toHaveBeenCalledTimes(1)
170
+ })
171
+
172
+ it("值没有实质变化时不触发通知也不写盘", async () => {
173
+ const conf = await store.define("demo", demoSchema)
174
+ const cb = vi.fn()
175
+ conf.onChange(cb)
176
+
177
+ await conf.patch({ render: { quality: 90 } })
178
+ expect(cb).not.toHaveBeenCalled()
179
+ })
180
+
181
+ it("订阅回调抛错不影响其他订阅者", async () => {
182
+ const conf = await store.define("demo", demoSchema)
183
+ const good = vi.fn()
184
+ conf.onChange(() => {
185
+ throw new Error("坏回调")
186
+ })
187
+ conf.onChange(good)
188
+
189
+ await conf.patch({ bot: { prefix: "!" } })
190
+ expect(good).toHaveBeenCalledTimes(1)
191
+ expect(logger.lines.some(l => l.includes("变更回调抛错"))).toBe(true)
192
+ })
193
+
194
+ it("取消订阅后不再收到通知", async () => {
195
+ const conf = await store.define("demo", demoSchema)
196
+ const cb = vi.fn()
197
+ const off = conf.onChange(cb)
198
+ off()
199
+
200
+ await conf.patch({ bot: { prefix: "!" } })
201
+ expect(cb).not.toHaveBeenCalled()
202
+ })
203
+
204
+ it("reset 恢复默认值", async () => {
205
+ const conf = await store.define("demo", demoSchema)
206
+ await conf.patch({ bot: { prefix: "!", masterQQ: ["10086"] } })
207
+ await conf.reset()
208
+ expect(conf.get()).toEqual({ bot: { masterQQ: [], prefix: "#" }, render: { quality: 90 } })
209
+ })
210
+
211
+ it("重复声明同名配置直接抛错,而不是让后者悄悄顶掉前者", async () => {
212
+ await store.define("demo", demoSchema)
213
+ await expect(store.define("demo", demoSchema)).rejects.toThrow(/已被声明/)
214
+ })
215
+
216
+ it("拒绝非法配置名(防止越出配置目录)", async () => {
217
+ await expect(store.define("../evil", demoSchema)).rejects.toThrow(/不合法/)
218
+ await expect(store.define("a/b", demoSchema)).rejects.toThrow(/不合法/)
219
+ })
220
+
221
+ it("list() 给 WebUI 提供表单描述", async () => {
222
+ await store.define("zeta", demoSchema, { title: "Z" })
223
+ await store.define("alpha", demoSchema, { title: "A" })
224
+
225
+ const list = store.list()
226
+ expect(list.map(i => i.name)).toEqual(["alpha", "zeta"])
227
+ expect(list[0]?.schema.properties?.bot?.properties?.prefix?.title).toBe("命令前缀")
228
+ })
229
+
230
+ it("外部手改文件后自动热加载(走真实 fs.watch)", async () => {
231
+ const watched = new ConfigStore({ dir, logger, watch: true })
232
+ try {
233
+ const conf = await watched.define("demo", demoSchema)
234
+ watched.startWatching()
235
+
236
+ const changed = new Promise<string[]>(resolve => conf.onChange(c => resolve(c.paths)))
237
+ await writeFile(conf.file, "bot:\n prefix: '/'\n", "utf8")
238
+
239
+ await expect(changed).resolves.toEqual(["bot.prefix"])
240
+ expect(conf.get().bot.prefix).toBe("/")
241
+ } finally {
242
+ watched.dispose()
243
+ }
244
+ }, 10_000)
245
+ })