@chainlesschain/personal-data-hub 0.4.29 → 0.4.31

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 (199) hide show
  1. package/lib/forensics/qq-nt-collect.js +190 -0
  2. package/lib/prompt-builder.js +15 -1
  3. package/package.json +8 -3
  4. package/__tests__/adapter-guide.test.js +0 -47
  5. package/__tests__/adapter-spec.test.js +0 -78
  6. package/__tests__/adapters/ai-chat-cookie-capture-spec.test.js +0 -211
  7. package/__tests__/adapters/ai-chat-health-checker.test.js +0 -262
  8. package/__tests__/adapters/ai-chat-history.test.js +0 -396
  9. package/__tests__/adapters/ai-chat-http-client.test.js +0 -242
  10. package/__tests__/adapters/ai-chat-vendors.test.js +0 -874
  11. package/__tests__/adapters/alipay-bill-adapter.test.js +0 -538
  12. package/__tests__/adapters/apple-health.test.js +0 -95
  13. package/__tests__/adapters/bank-family.test.js +0 -125
  14. package/__tests__/adapters/biz-tianyancha.test.js +0 -159
  15. package/__tests__/adapters/browser-history-chrome.test.js +0 -377
  16. package/__tests__/adapters/browser-history-edge.test.js +0 -159
  17. package/__tests__/adapters/car-mercedesme.test.js +0 -74
  18. package/__tests__/adapters/doc-baidu-netdisk.test.js +0 -102
  19. package/__tests__/adapters/doc-camscanner.test.js +0 -147
  20. package/__tests__/adapters/doc-platforms.test.js +0 -177
  21. package/__tests__/adapters/edu-huawei-learning-live.test.js +0 -198
  22. package/__tests__/adapters/edu-zuoyebang-live.test.js +0 -226
  23. package/__tests__/adapters/email-adapter-snapshot.test.js +0 -237
  24. package/__tests__/adapters/email-adapter.test.js +0 -742
  25. package/__tests__/adapters/email-classifier.test.js +0 -347
  26. package/__tests__/adapters/email-imap-session.test.js +0 -334
  27. package/__tests__/adapters/email-parser.test.js +0 -244
  28. package/__tests__/adapters/email-pdf-extractor.test.js +0 -529
  29. package/__tests__/adapters/email-providers.test.js +0 -84
  30. package/__tests__/adapters/email-retry-progress.test.js +0 -294
  31. package/__tests__/adapters/email-templates.test.js +0 -822
  32. package/__tests__/adapters/family-23-collectors-scaffold.test.js +0 -182
  33. package/__tests__/adapters/finance-alipay-live.test.js +0 -258
  34. package/__tests__/adapters/finance-dcep.test.js +0 -74
  35. package/__tests__/adapters/fitness-joyrun.test.js +0 -82
  36. package/__tests__/adapters/game-genshin-live.test.js +0 -238
  37. package/__tests__/adapters/game-genshin-scaffold.test.js +0 -108
  38. package/__tests__/adapters/game-honor-of-kings-live.test.js +0 -230
  39. package/__tests__/adapters/git-activity.test.js +0 -222
  40. package/__tests__/adapters/gov-12123.test.js +0 -103
  41. package/__tests__/adapters/gov-ixiamen.test.js +0 -150
  42. package/__tests__/adapters/gov-tax.test.js +0 -135
  43. package/__tests__/adapters/health-meiyou.test.js +0 -125
  44. package/__tests__/adapters/local-files.test.js +0 -264
  45. package/__tests__/adapters/local-im-pc.test.js +0 -154
  46. package/__tests__/adapters/messaging-whatsapp.test.js +0 -289
  47. package/__tests__/adapters/music-kugou.test.js +0 -187
  48. package/__tests__/adapters/music-qq.test.js +0 -112
  49. package/__tests__/adapters/netease-music-live.test.js +0 -244
  50. package/__tests__/adapters/netease-music.test.js +0 -74
  51. package/__tests__/adapters/pc-local-discovery.test.js +0 -141
  52. package/__tests__/adapters/qq-pc-direct-read.test.js +0 -227
  53. package/__tests__/adapters/reading-family.test.js +0 -108
  54. package/__tests__/adapters/recruit-boss.test.js +0 -180
  55. package/__tests__/adapters/shell-history.test.js +0 -180
  56. package/__tests__/adapters/shopping-base.test.js +0 -179
  57. package/__tests__/adapters/shopping-dianping.test.js +0 -239
  58. package/__tests__/adapters/social-bilibili-adb-api-client.test.js +0 -721
  59. package/__tests__/adapters/social-bilibili-adb-chromium-cookies-reader.test.js +0 -346
  60. package/__tests__/adapters/social-bilibili-adb-collector.test.js +0 -284
  61. package/__tests__/adapters/social-bilibili-adb-cookies-extension.test.js +0 -343
  62. package/__tests__/adapters/social-bilibili-adb-snapshot-builder.test.js +0 -296
  63. package/__tests__/adapters/social-csdn.test.js +0 -175
  64. package/__tests__/adapters/social-dongchedi.test.js +0 -165
  65. package/__tests__/adapters/social-douyin-adb-aweme-detail.test.js +0 -165
  66. package/__tests__/adapters/social-douyin-adb-collector.test.js +0 -254
  67. package/__tests__/adapters/social-douyin-adb-db-extension.test.js +0 -114
  68. package/__tests__/adapters/social-douyin-adb-im-db-parser.test.js +0 -304
  69. package/__tests__/adapters/social-douyin-adb-snapshot-builder.test.js +0 -216
  70. package/__tests__/adapters/social-douyin-adb-usage-profile.test.js +0 -229
  71. package/__tests__/adapters/social-douyin-adb-watch-history.test.js +0 -269
  72. package/__tests__/adapters/social-kuaishou-adb-api-client.test.js +0 -496
  73. package/__tests__/adapters/social-kuaishou-adb-collector.test.js +0 -276
  74. package/__tests__/adapters/social-kuaishou-adb-cookies-extension.test.js +0 -152
  75. package/__tests__/adapters/social-kuaishou-adb-snapshot-builder.test.js +0 -178
  76. package/__tests__/adapters/social-toutiao-adb-account-reader.test.js +0 -135
  77. package/__tests__/adapters/social-toutiao-adb-api-client.test.js +0 -626
  78. package/__tests__/adapters/social-toutiao-adb-article.test.js +0 -155
  79. package/__tests__/adapters/social-toutiao-adb-collector.test.js +0 -378
  80. package/__tests__/adapters/social-toutiao-adb-cookies-extension.test.js +0 -193
  81. package/__tests__/adapters/social-toutiao-adb-snapshot-builder.test.js +0 -196
  82. package/__tests__/adapters/social-toutiao-kuaishou-scaffold.test.js +0 -311
  83. package/__tests__/adapters/social-weibo-adb-api-client.test.js +0 -362
  84. package/__tests__/adapters/social-weibo-adb-collector.test.js +0 -201
  85. package/__tests__/adapters/social-weibo-adb-cookies-extension.test.js +0 -167
  86. package/__tests__/adapters/social-weibo-adb-snapshot-builder.test.js +0 -189
  87. package/__tests__/adapters/social-xiaohongshu-adb-api-client.test.js +0 -431
  88. package/__tests__/adapters/social-xiaohongshu-adb-collector.test.js +0 -207
  89. package/__tests__/adapters/social-xiaohongshu-adb-cookies-extension.test.js +0 -0
  90. package/__tests__/adapters/social-xiaohongshu-adb-sign-provider-injection.test.js +0 -351
  91. package/__tests__/adapters/social-xiaohongshu-adb-sign.test.js +0 -130
  92. package/__tests__/adapters/social-xiaohongshu-adb-snapshot-builder.test.js +0 -200
  93. package/__tests__/adapters/social-zhihu.test.js +0 -246
  94. package/__tests__/adapters/system-data-adapter.test.js +0 -443
  95. package/__tests__/adapters/system-data-android-ingest.test.js +0 -144
  96. package/__tests__/adapters/system-data-android.test.js +0 -519
  97. package/__tests__/adapters/system-data-disclosure.test.js +0 -153
  98. package/__tests__/adapters/travel-12306.test.js +0 -512
  99. package/__tests__/adapters/travel-amap.test.js +0 -219
  100. package/__tests__/adapters/travel-baidu-map.test.js +0 -305
  101. package/__tests__/adapters/travel-base.test.js +0 -205
  102. package/__tests__/adapters/travel-ctrip.test.js +0 -377
  103. package/__tests__/adapters/travel-didi-consumer.test.js +0 -66
  104. package/__tests__/adapters/travel-didi.test.js +0 -204
  105. package/__tests__/adapters/travel-tencent-map.test.js +0 -207
  106. package/__tests__/adapters/travel-tongcheng.test.js +0 -289
  107. package/__tests__/adapters/video-platforms.test.js +0 -152
  108. package/__tests__/adapters/video-xigua.test.js +0 -106
  109. package/__tests__/adapters/vscode.test.js +0 -299
  110. package/__tests__/adapters/wechat-bootstrap.test.js +0 -240
  111. package/__tests__/adapters/wechat-env-probe.test.js +0 -162
  112. package/__tests__/adapters/wechat-frida-agent.test.js +0 -322
  113. package/__tests__/adapters/wechat-frida-integration.test.js +0 -149
  114. package/__tests__/adapters/wechat-frida-key-provider.test.js +0 -188
  115. package/__tests__/adapters/wechat-md5-key-provider.test.js +0 -101
  116. package/__tests__/adapters/wechat-pc-direct-read.test.js +0 -365
  117. package/__tests__/adapters/wechat-pc-group-topic.test.js +0 -63
  118. package/__tests__/adapters/wechat-pc-v4-sidecar.test.js +0 -72
  119. package/__tests__/adapters/weread.test.js +0 -123
  120. package/__tests__/adapters/wework-pc.test.js +0 -124
  121. package/__tests__/adapters/win-recent.test.js +0 -192
  122. package/__tests__/analysis-skills.test.js +0 -754
  123. package/__tests__/analysis.test.js +0 -1845
  124. package/__tests__/audio-ximalaya-snapshot.test.js +0 -279
  125. package/__tests__/batch.test.js +0 -133
  126. package/__tests__/bridges-cc-kg.test.js +0 -231
  127. package/__tests__/bridges-cc-llm.test.js +0 -191
  128. package/__tests__/bridges-cc-rag.test.js +0 -162
  129. package/__tests__/categories.test.js +0 -92
  130. package/__tests__/e2e/ai-chat-cross-source-journey.test.js +0 -213
  131. package/__tests__/e2e/full-user-journey.test.js +0 -188
  132. package/__tests__/e2e/local-data-adapters-cli.e2e.test.js +0 -146
  133. package/__tests__/entity-resolver-ingest-hook.test.js +0 -177
  134. package/__tests__/entity-resolver-stages.test.js +0 -411
  135. package/__tests__/entity-resolver-vault.test.js +0 -249
  136. package/__tests__/entity-resolver.test.js +0 -526
  137. package/__tests__/fitness-keep-snapshot.test.js +0 -224
  138. package/__tests__/fixtures/entity-resolver-200-mock.json +0 -96
  139. package/__tests__/ids.test.js +0 -45
  140. package/__tests__/integration/ai-chat-history-registry.test.js +0 -228
  141. package/__tests__/integration/aichat-wizard-end-to-end.test.js +0 -282
  142. package/__tests__/integration/cross-adapter-pipelines.test.js +0 -396
  143. package/__tests__/integration/local-data-adapters-pipeline.test.js +0 -373
  144. package/__tests__/integration/social-bilibili-pipeline.test.js +0 -261
  145. package/__tests__/integration/wechat-bootstrap-end-to-end.test.js +0 -390
  146. package/__tests__/key-providers.test.js +0 -126
  147. package/__tests__/kg-derive.test.js +0 -219
  148. package/__tests__/llm-client.test.js +0 -122
  149. package/__tests__/longtail-adapters.test.js +0 -281
  150. package/__tests__/messaging-qq-snapshot.test.js +0 -294
  151. package/__tests__/mobile-extractor-encrypted.test.js +0 -460
  152. package/__tests__/mobile-extractor.test.js +0 -288
  153. package/__tests__/mock-adapter.test.js +0 -93
  154. package/__tests__/prompt-builder.test.js +0 -249
  155. package/__tests__/query-parser.test.js +0 -365
  156. package/__tests__/rag-derive.test.js +0 -169
  157. package/__tests__/registry-readiness.test.js +0 -292
  158. package/__tests__/registry.test.js +0 -420
  159. package/__tests__/salvage-ingest.test.js +0 -97
  160. package/__tests__/schemas.test.js +0 -331
  161. package/__tests__/shopping-adapters.test.js +0 -392
  162. package/__tests__/shopping-eleme-snapshot.test.js +0 -454
  163. package/__tests__/shopping-pinduoduo-snapshot.test.js +0 -484
  164. package/__tests__/shopping-snapshot.test.js +0 -438
  165. package/__tests__/shopping-vipshop-snapshot.test.js +0 -425
  166. package/__tests__/shopping-xianyu-snapshot.test.js +0 -451
  167. package/__tests__/sidecar-contacts-cross-validate.test.js +0 -186
  168. package/__tests__/sidecar-supervisor.test.js +0 -128
  169. package/__tests__/sign-providers.test.js +0 -62
  170. package/__tests__/social-adapters.test.js +0 -280
  171. package/__tests__/social-bilibili-snapshot.test.js +0 -278
  172. package/__tests__/social-douban-snapshot.test.js +0 -351
  173. package/__tests__/social-douyin-im-direct-read.test.js +0 -377
  174. package/__tests__/social-douyin-salvage-collector.test.js +0 -98
  175. package/__tests__/social-douyin-salvage-mapper.test.js +0 -90
  176. package/__tests__/social-douyin-snapshot.test.js +0 -256
  177. package/__tests__/social-kuaishou-snapshot.test.js +0 -362
  178. package/__tests__/social-toutiao-snapshot.test.js +0 -366
  179. package/__tests__/social-weibo-snapshot.test.js +0 -234
  180. package/__tests__/social-weibo-sqlite-device.test.js +0 -174
  181. package/__tests__/social-xiaohongshu-snapshot.test.js +0 -232
  182. package/__tests__/sqlite-leaf-salvage.test.js +0 -97
  183. package/__tests__/travel-adapters.test.js +0 -483
  184. package/__tests__/travel-maps-snapshot.test.js +0 -426
  185. package/__tests__/vault-driver-error.test.js +0 -74
  186. package/__tests__/vault-search-helpers.test.js +0 -104
  187. package/__tests__/vault-search.test.js +0 -423
  188. package/__tests__/vault.test.js +0 -767
  189. package/__tests__/wechat-adapter.test.js +0 -594
  190. package/__tests__/whatsapp-adapter.test.js +0 -138
  191. package/scripts/_make-fixture-all.js +0 -126
  192. package/scripts/_make-fixture-contacts.js +0 -84
  193. package/scripts/evaluate-entity-resolver.js +0 -213
  194. package/scripts/run-native-tests-sandbox.sh +0 -55
  195. package/scripts/smoke-phase-5-5.js +0 -196
  196. package/scripts/smoke-phase-5-7.js +0 -181
  197. package/scripts/smoke-system-data-contacts.js +0 -309
  198. package/scripts/smoke-system-data.js +0 -312
  199. package/vitest.config.js +0 -88
@@ -1,347 +0,0 @@
1
- "use strict";
2
-
3
- import { describe, it, expect } from "vitest";
4
-
5
- const {
6
- CATEGORIES,
7
- ALL_CATEGORIES,
8
- classifyLayer1,
9
- classifyLayer2,
10
- classifyEmail,
11
- parseLayer2Response,
12
- } = require("../../lib/adapters/email-imap/classifier");
13
- const { MockLLMClient } = require("../../lib/llm-client");
14
-
15
- const email = (overrides = {}) => ({
16
- from: [{ name: "Someone", address: "user@example.com" }],
17
- subject: "Hello",
18
- textBody: "",
19
- htmlBody: "",
20
- attachments: [],
21
- headers: {},
22
- indicatorHeaders: {},
23
- ...overrides,
24
- });
25
-
26
- // ─── Layer 1 ─────────────────────────────────────────────────────────────
27
-
28
- describe("Layer 1 — bank statements", () => {
29
- it("招商银行 from-domain + 对账单 subject → bill_bank with high confidence", () => {
30
- const r = classifyLayer1(email({
31
- from: [{ address: "ebank@cmbchina.com" }],
32
- subject: "招商银行信用卡 11 月对账单",
33
- }));
34
- expect(r.category).toBe(CATEGORIES.BILL_BANK);
35
- expect(r.confidence).toBeGreaterThanOrEqual(0.92);
36
- expect(r.ruleName).toContain("bill_bank");
37
- });
38
-
39
- it("中国银行 from-domain alone (no subject keyword) → bill_bank confidence ~0.9", () => {
40
- const r = classifyLayer1(email({
41
- from: [{ address: "noreply@bochk.cn" }],
42
- subject: "您的账户更新",
43
- }));
44
- expect(r.category).toBe(CATEGORIES.BILL_BANK);
45
- expect(r.confidence).toBeGreaterThanOrEqual(0.85);
46
- });
47
-
48
- it("ICBC from-domain → bill_bank", () => {
49
- const r = classifyLayer1(email({ from: [{ address: "ebanking@icbc.com.cn" }] }));
50
- expect(r.category).toBe(CATEGORIES.BILL_BANK);
51
- });
52
-
53
- it("credit-card subject keyword overrides generic bill_bank", () => {
54
- const r = classifyLayer1(email({
55
- from: [{ address: "card@cmbchina.com" }],
56
- subject: "信用卡 11 月对账单",
57
- }));
58
- // bill_bank.cn-bank-major fires at 0.95, bill_credit at 0.92 — bank
59
- // wins on confidence. The point is BOTH match; we don't break.
60
- expect([CATEGORIES.BILL_BANK, CATEGORIES.BILL_CREDIT]).toContain(r.category);
61
- expect(r.confidence).toBeGreaterThanOrEqual(0.9);
62
- });
63
-
64
- it("subject 信用卡账单 alone (no bank domain) → bill_credit", () => {
65
- const r = classifyLayer1(email({
66
- from: [{ address: "noreply@somerandombank.example" }],
67
- subject: "您的信用卡账单已生成",
68
- }));
69
- expect(r.category).toBe(CATEGORIES.BILL_CREDIT);
70
- });
71
- });
72
-
73
- describe("Layer 1 — e-commerce orders", () => {
74
- it("淘宝 + 订单 → order high confidence", () => {
75
- const r = classifyLayer1(email({
76
- from: [{ address: "service@taobao.com" }],
77
- subject: "您的订单 1234567 已发货",
78
- }));
79
- expect(r.category).toBe(CATEGORIES.ORDER);
80
- expect(r.confidence).toBeGreaterThanOrEqual(0.92);
81
- });
82
-
83
- it("京东 domain alone → order", () => {
84
- const r = classifyLayer1(email({ from: [{ address: "marketing@jd.com" }], subject: "限时优惠" }));
85
- expect(r.category).toBe(CATEGORIES.ORDER);
86
- });
87
-
88
- it("拼多多 + 已签收 → order", () => {
89
- const r = classifyLayer1(email({
90
- from: [{ address: "no-reply@pinduoduo.com" }],
91
- subject: "已签收,请确认",
92
- }));
93
- expect(r.category).toBe(CATEGORIES.ORDER);
94
- });
95
-
96
- it("Amazon → order", () => {
97
- const r = classifyLayer1(email({ from: [{ address: "auto-confirm@amazon.com" }], subject: "Your order has shipped" }));
98
- expect(r.category).toBe(CATEGORIES.ORDER);
99
- });
100
- });
101
-
102
- describe("Layer 1 — travel", () => {
103
- it("携程 → travel", () => {
104
- const r = classifyLayer1(email({ from: [{ address: "noreply@ctrip.com" }], subject: "您的酒店预订" }));
105
- expect(r.category).toBe(CATEGORIES.TRAVEL);
106
- });
107
-
108
- it("12306 → travel", () => {
109
- const r = classifyLayer1(email({ from: [{ address: "no-reply@12306.cn" }], subject: "出票成功" }));
110
- expect(r.category).toBe(CATEGORIES.TRAVEL);
111
- });
112
-
113
- it("航班 keyword without travel domain → travel medium-confidence", () => {
114
- const r = classifyLayer1(email({
115
- from: [{ address: "noreply@example.com" }],
116
- subject: "您的航班 CA1234 出票成功",
117
- }));
118
- expect(r.category).toBe(CATEGORIES.TRAVEL);
119
- expect(r.confidence).toBeGreaterThanOrEqual(0.85);
120
- });
121
- });
122
-
123
- describe("Layer 1 — government", () => {
124
- it(".gov.cn → government", () => {
125
- const r = classifyLayer1(email({
126
- from: [{ address: "notify@beijing.gov.cn" }],
127
- subject: "您的纳税申报已提交",
128
- }));
129
- expect(r.category).toBe(CATEGORIES.GOVERNMENT);
130
- });
131
-
132
- it("社保 keyword alone → government", () => {
133
- const r = classifyLayer1(email({
134
- from: [{ address: "info@somewebsite.com" }],
135
- subject: "您的社保缴费记录",
136
- }));
137
- expect(r.category).toBe(CATEGORIES.GOVERNMENT);
138
- });
139
- });
140
-
141
- describe("Layer 1 — register", () => {
142
- it("验证码 → register", () => {
143
- const r = classifyLayer1(email({
144
- from: [{ address: "noreply@somesite.com" }],
145
- subject: "您的验证码是 1234",
146
- }));
147
- expect(r.category).toBe(CATEGORIES.REGISTER);
148
- });
149
-
150
- it("Password reset → register", () => {
151
- const r = classifyLayer1(email({
152
- from: [{ address: "support@app.io" }],
153
- subject: "Password reset request",
154
- }));
155
- expect(r.category).toBe(CATEGORIES.REGISTER);
156
- });
157
-
158
- it("Welcome to → register lower confidence", () => {
159
- const r = classifyLayer1(email({ subject: "Welcome to ChainlessChain" }));
160
- expect(r.category).toBe(CATEGORIES.REGISTER);
161
- expect(r.confidence).toBeLessThan(0.85); // → Layer 2 in orchestrator
162
- });
163
- });
164
-
165
- describe("Layer 1 — notify (marketing)", () => {
166
- it("List-Unsubscribe header → notify", () => {
167
- const r = classifyLayer1(email({
168
- from: [{ address: "newsletter@example.com" }],
169
- subject: "Our weekly digest",
170
- indicatorHeaders: { "list-unsubscribe": "<mailto:unsub@example.com>" },
171
- }));
172
- expect(r.category).toBe(CATEGORIES.NOTIFY);
173
- });
174
-
175
- it("Precedence: bulk → notify", () => {
176
- const r = classifyLayer1(email({
177
- headers: { precedence: "bulk" },
178
- }));
179
- expect(r.category).toBe(CATEGORIES.NOTIFY);
180
- });
181
-
182
- it("Auto-Submitted: auto-generated → notify", () => {
183
- const r = classifyLayer1(email({
184
- headers: { "auto-submitted": "auto-generated" },
185
- }));
186
- expect(r.category).toBe(CATEGORIES.NOTIFY);
187
- });
188
-
189
- it("Marketing header doesn't override bill_bank (specificity wins)", () => {
190
- const r = classifyLayer1(email({
191
- from: [{ address: "promo@cmbchina.com" }],
192
- subject: "招行卡片新优惠",
193
- indicatorHeaders: { "list-unsubscribe": "<...>" },
194
- }));
195
- expect(r.category).toBe(CATEGORIES.BILL_BANK);
196
- });
197
- });
198
-
199
- describe("Layer 1 — defaults & malformed input", () => {
200
- it("no signals → other with 0 confidence", () => {
201
- const r = classifyLayer1(email({
202
- from: [{ address: "friend@gmail.com" }],
203
- subject: "Hi how are you",
204
- }));
205
- expect(r.category).toBe(CATEGORIES.OTHER);
206
- expect(r.confidence).toBe(0);
207
- });
208
-
209
- it("null input → other", () => {
210
- const r = classifyLayer1(null);
211
- expect(r.category).toBe(CATEGORIES.OTHER);
212
- });
213
-
214
- it("layer field is 'L1'", () => {
215
- const r = classifyLayer1(email({ from: [{ address: "x@taobao.com" }] }));
216
- expect(r.layer).toBe("L1");
217
- });
218
- });
219
-
220
- // ─── Layer 2 — LLM disambiguation ────────────────────────────────────────
221
-
222
- describe("Layer 2 LLM classifier", () => {
223
- it("parses LLM JSON response into category/confidence/reason", async () => {
224
- const llm = new MockLLMClient({
225
- reply: '{"category":"order","confidence":0.88,"reason":"shipment notification"}',
226
- });
227
- const r = await classifyLayer2(email({ subject: "ambiguous" }), { llm });
228
- expect(r.category).toBe(CATEGORIES.ORDER);
229
- expect(r.confidence).toBe(0.88);
230
- expect(r.reason).toContain("shipment");
231
- expect(r.layer).toBe("L2");
232
- });
233
-
234
- it("strips markdown code fences", async () => {
235
- const llm = new MockLLMClient({
236
- reply: '```json\n{"category":"travel","confidence":0.9}\n```',
237
- });
238
- const r = await classifyLayer2(email(), { llm });
239
- expect(r.category).toBe(CATEGORIES.TRAVEL);
240
- });
241
-
242
- it("regex-falls back to find first {...} when commentary precedes JSON", async () => {
243
- const llm = new MockLLMClient({
244
- reply: 'Sure, here is my classification: {"category":"register","confidence":0.6}',
245
- });
246
- const r = await classifyLayer2(email(), { llm });
247
- expect(r.category).toBe(CATEGORIES.REGISTER);
248
- });
249
-
250
- it("malformed JSON → falls back to layer1 result", async () => {
251
- const llm = new MockLLMClient({ reply: "I think it's an order email" });
252
- const fallback = { category: CATEGORIES.NOTIFY, confidence: 0.4, ruleName: "fallback" };
253
- const r = await classifyLayer2(email(), { llm, fallback });
254
- expect(r.category).toBe(CATEGORIES.NOTIFY);
255
- expect(r.layer).toBe("L1-fallback");
256
- });
257
-
258
- it("LLM throws → falls back to layer1 result", async () => {
259
- const llm = new MockLLMClient({});
260
- llm.chat = async () => { throw new Error("Ollama down"); };
261
- const fallback = { category: CATEGORIES.BILL_BANK, confidence: 0.7 };
262
- const r = await classifyLayer2(email(), { llm, fallback });
263
- expect(r.category).toBe(CATEGORIES.BILL_BANK);
264
- expect(r.layer).toBe("L1-fallback");
265
- });
266
-
267
- it("unknown category from LLM → falls back to fallback / OTHER", async () => {
268
- const llm = new MockLLMClient({ reply: '{"category":"alien-spam","confidence":0.9}' });
269
- const r = await classifyLayer2(email(), { llm });
270
- expect(r.category).toBe(CATEGORIES.OTHER);
271
- expect(r.layer).toBe("L1-fallback");
272
- });
273
-
274
- it("no LLM → throws", async () => {
275
- await expect(classifyLayer2(email(), {})).rejects.toThrow(/llm/i);
276
- });
277
-
278
- it("parseLayer2Response: strict JSON", () => {
279
- expect(parseLayer2Response('{"category":"order"}')).toEqual({ category: "order" });
280
- });
281
-
282
- it("parseLayer2Response: empty / non-json → null", () => {
283
- expect(parseLayer2Response("")).toBeNull();
284
- expect(parseLayer2Response("just text no json")).toBeNull();
285
- });
286
- });
287
-
288
- // ─── classifyEmail orchestrator ─────────────────────────────────────────
289
-
290
- describe("classifyEmail orchestrator", () => {
291
- it("high-confidence layer1 short-circuits layer2 even when LLM provided", async () => {
292
- const llm = new MockLLMClient({ reply: '{"category":"other"}' });
293
- const r = await classifyEmail(email({
294
- from: [{ address: "x@taobao.com" }],
295
- subject: "订单已发货",
296
- }), { llm });
297
- expect(r.layer).toBe("L1");
298
- expect(r.category).toBe(CATEGORIES.ORDER);
299
- expect(llm.calls).toHaveLength(0); // LLM NOT called
300
- });
301
-
302
- it("low-confidence layer1 → falls through to layer2", async () => {
303
- const llm = new MockLLMClient({
304
- reply: '{"category":"register","confidence":0.7}',
305
- });
306
- const r = await classifyEmail(email({
307
- from: [{ address: "service@unknown-bank.example" }],
308
- subject: "Welcome to our service",
309
- }), { llm });
310
- expect(r.layer).toBe("L2");
311
- expect(r.category).toBe(CATEGORIES.REGISTER);
312
- expect(llm.calls).toHaveLength(1);
313
- });
314
-
315
- it("no LLM + low-confidence layer1 → returns layer1 as-is", async () => {
316
- const r = await classifyEmail(email());
317
- expect(r.layer).toBe("L1");
318
- expect(r.category).toBe(CATEGORIES.OTHER);
319
- });
320
-
321
- it("disableLayer2 forces layer1-only even with LLM provided", async () => {
322
- const llm = new MockLLMClient({ reply: '{"category":"order"}' });
323
- const r = await classifyEmail(email(), { llm, disableLayer2: true });
324
- expect(r.layer).toBe("L1");
325
- expect(llm.calls).toHaveLength(0);
326
- });
327
-
328
- it("minLayer1Confidence threshold respected (0.99 forces layer2)", async () => {
329
- const llm = new MockLLMClient({ reply: '{"category":"travel","confidence":0.7}' });
330
- const r = await classifyEmail(email({
331
- from: [{ address: "x@taobao.com" }],
332
- subject: "订单",
333
- }), { llm, minLayer1Confidence: 0.99 });
334
- // taobao + 订单 hits 0.95 < 0.99 → layer 2 fires
335
- expect(r.layer).toBe("L2");
336
- });
337
-
338
- it("ALL_CATEGORIES contains the 8 documented categories", () => {
339
- expect(ALL_CATEGORIES).toHaveLength(8);
340
- expect(ALL_CATEGORIES).toEqual(
341
- expect.arrayContaining([
342
- "bill_bank", "bill_credit", "order", "travel",
343
- "government", "register", "notify", "other",
344
- ])
345
- );
346
- });
347
- });
@@ -1,334 +0,0 @@
1
- "use strict";
2
-
3
- import { describe, it, expect } from "vitest";
4
-
5
- const {
6
- ImapSession,
7
- ImapAuthFailedError,
8
- ImapConnectionFailedError,
9
- ImapMailboxNotFoundError,
10
- } = require("../../lib/adapters/email-imap/imap-session");
11
-
12
- function makeFakeFactory({
13
- connectThrows = null,
14
- mailboxThrows = null,
15
- fetchRows = [],
16
- mailboxInfo = { uidValidity: 1, uidNext: 100, exists: 0 },
17
- } = {}) {
18
- return function FakeImapFlow(_opts) {
19
- return {
20
- connect: async () => {
21
- if (connectThrows) throw connectThrows;
22
- },
23
- mailboxOpen: async (name) => {
24
- if (mailboxThrows) throw mailboxThrows;
25
- return { path: name, name, ...mailboxInfo };
26
- },
27
- list: async () => [
28
- { name: "INBOX", path: "INBOX", flags: [], specialUse: null },
29
- { name: "Sent", path: "Sent", flags: [], specialUse: "\\Sent" },
30
- ],
31
- fetch: function () {
32
- const rows = fetchRows.slice();
33
- let i = 0;
34
- return {
35
- [Symbol.asyncIterator]() {
36
- return {
37
- next: async () =>
38
- i < rows.length
39
- ? { value: rows[i++], done: false }
40
- : { value: undefined, done: true },
41
- };
42
- },
43
- };
44
- },
45
- logout: async () => {},
46
- close: async () => {},
47
- };
48
- };
49
- }
50
-
51
- describe("ImapSession construction", () => {
52
- it("rejects missing required opts", () => {
53
- expect(() => new ImapSession()).toThrow();
54
- expect(() => new ImapSession({})).toThrow(/host/);
55
- expect(() => new ImapSession({ host: "x", port: 993, user: "u" })).toThrow(/authCode/);
56
- expect(() => new ImapSession({ host: "x", port: 993, authCode: "z" })).toThrow(/user/);
57
- });
58
-
59
- it("constructs cleanly with required opts", () => {
60
- const s = new ImapSession({
61
- host: "imap.test", port: 993, secure: true, user: "u@test.com", authCode: "abc",
62
- });
63
- expect(s.host).toBe("imap.test");
64
- expect(s.port).toBe(993);
65
- expect(s.secure).toBe(true);
66
- });
67
-
68
- it("defaults secure=true when not specified", () => {
69
- const s = new ImapSession({ host: "x", port: 993, user: "u@t", authCode: "z" });
70
- expect(s.secure).toBe(true);
71
- });
72
- });
73
-
74
- describe("ImapSession.connect", () => {
75
- it("connects successfully with injected factory", async () => {
76
- const s = new ImapSession({
77
- host: "x", port: 993, user: "u@t", authCode: "z",
78
- imapFlowFactory: makeFakeFactory(),
79
- });
80
- await expect(s.connect()).resolves.toBeUndefined();
81
- });
82
-
83
- it("classifies auth failure as ImapAuthFailedError", async () => {
84
- const s = new ImapSession({
85
- host: "x", port: 993, user: "u@t", authCode: "z",
86
- imapFlowFactory: makeFakeFactory({
87
- connectThrows: new Error("Authentication failed - invalid credentials"),
88
- }),
89
- });
90
- await expect(s.connect()).rejects.toBeInstanceOf(ImapAuthFailedError);
91
- });
92
-
93
- it("classifies generic network errors as ImapConnectionFailedError", async () => {
94
- const s = new ImapSession({
95
- host: "x", port: 993, user: "u@t", authCode: "z",
96
- imapFlowFactory: makeFakeFactory({
97
- connectThrows: new Error("ECONNREFUSED"),
98
- }),
99
- });
100
- await expect(s.connect()).rejects.toBeInstanceOf(ImapConnectionFailedError);
101
- });
102
-
103
- it("enforces connectTimeoutMs", async () => {
104
- function slowFactory() {
105
- return {
106
- connect: () => new Promise(() => {}),
107
- close: async () => {},
108
- };
109
- }
110
- const s = new ImapSession({
111
- host: "x", port: 993, user: "u@t", authCode: "z",
112
- connectTimeoutMs: 50,
113
- imapFlowFactory: slowFactory,
114
- });
115
- await expect(s.connect()).rejects.toThrow(/timed out/);
116
- });
117
-
118
- it("requires connect() before openMailbox", async () => {
119
- const s = new ImapSession({
120
- host: "x", port: 993, user: "u@t", authCode: "z",
121
- imapFlowFactory: makeFakeFactory(),
122
- });
123
- await expect(s.openMailbox("INBOX")).rejects.toThrow(/not connected/);
124
- });
125
- });
126
-
127
- describe("ImapSession.openMailbox", () => {
128
- it("returns uidValidity / uidNext / exists snapshot", async () => {
129
- const s = new ImapSession({
130
- host: "x", port: 993, user: "u@t", authCode: "z",
131
- imapFlowFactory: makeFakeFactory({
132
- mailboxInfo: { uidValidity: 42, uidNext: 200, exists: 150 },
133
- }),
134
- });
135
- await s.connect();
136
- const r = await s.openMailbox("INBOX");
137
- expect(r.uidValidity).toBe(42);
138
- expect(r.uidNext).toBe(200);
139
- expect(r.exists).toBe(150);
140
- });
141
-
142
- it("converts 'not found' messages to ImapMailboxNotFoundError", async () => {
143
- const s = new ImapSession({
144
- host: "x", port: 993, user: "u@t", authCode: "z",
145
- imapFlowFactory: makeFakeFactory({
146
- mailboxThrows: new Error("Mailbox doesn't exist: NoSuch"),
147
- }),
148
- });
149
- await s.connect();
150
- await expect(s.openMailbox("NoSuch")).rejects.toBeInstanceOf(ImapMailboxNotFoundError);
151
- });
152
-
153
- it("rejects non-string mailbox name", async () => {
154
- const s = new ImapSession({
155
- host: "x", port: 993, user: "u@t", authCode: "z",
156
- imapFlowFactory: makeFakeFactory(),
157
- });
158
- await s.connect();
159
- await expect(s.openMailbox("")).rejects.toThrow(/non-empty string/);
160
- });
161
- });
162
-
163
- describe("ImapSession.listMailboxes", () => {
164
- it("returns name / path / flags / specialUse", async () => {
165
- const s = new ImapSession({
166
- host: "x", port: 993, user: "u@t", authCode: "z",
167
- imapFlowFactory: makeFakeFactory(),
168
- });
169
- await s.connect();
170
- const list = await s.listMailboxes();
171
- expect(list).toHaveLength(2);
172
- expect(list[0].name).toBe("INBOX");
173
- expect(list[1].specialUse).toBe("\\Sent");
174
- });
175
- });
176
-
177
- describe("ImapSession.fetchEnvelopesSince", () => {
178
- it("yields normalized envelope rows", async () => {
179
- const fakeRow = {
180
- uid: 42,
181
- internalDate: new Date("2026-04-01T10:00:00Z"),
182
- flags: new Set(["\\Seen"]),
183
- size: 1234,
184
- envelope: {
185
- messageId: "<msg-1@example.com>",
186
- subject: "Hello",
187
- from: [{ name: "Alice", address: "alice@example.com" }],
188
- to: [{ name: "Me", address: "me@example.com" }],
189
- cc: [],
190
- date: new Date("2026-04-01T10:00:00Z"),
191
- },
192
- };
193
- const s = new ImapSession({
194
- host: "x", port: 993, user: "u@t", authCode: "z",
195
- imapFlowFactory: makeFakeFactory({ fetchRows: [fakeRow] }),
196
- });
197
- await s.connect();
198
- await s.openMailbox("INBOX");
199
- const got = [];
200
- for await (const env of s.fetchEnvelopesSince(0)) got.push(env);
201
- expect(got).toHaveLength(1);
202
- expect(got[0].uid).toBe(42);
203
- expect(got[0].subject).toBe("Hello");
204
- expect(got[0].messageId).toBe("<msg-1@example.com>");
205
- expect(got[0].from[0].address).toBe("alice@example.com");
206
- expect(got[0].flags).toEqual(["\\Seen"]);
207
- expect(got[0].size).toBe(1234);
208
- expect(got[0].internalDate).toBeInstanceOf(Date);
209
- });
210
-
211
- it("emits empty for an empty mailbox", async () => {
212
- const s = new ImapSession({
213
- host: "x", port: 993, user: "u@t", authCode: "z",
214
- imapFlowFactory: makeFakeFactory({ fetchRows: [] }),
215
- });
216
- await s.connect();
217
- await s.openMailbox("INBOX");
218
- const got = [];
219
- for await (const env of s.fetchEnvelopesSince(0)) got.push(env);
220
- expect(got).toEqual([]);
221
- });
222
-
223
- it("preserves order from underlying iterator", async () => {
224
- const rows = [10, 20, 30].map((uid) => ({
225
- uid,
226
- internalDate: new Date(),
227
- flags: [],
228
- size: 0,
229
- envelope: { messageId: `<m-${uid}@x>`, subject: `s${uid}`, from: [], to: [], cc: [], date: new Date() },
230
- }));
231
- const s = new ImapSession({
232
- host: "x", port: 993, user: "u@t", authCode: "z",
233
- imapFlowFactory: makeFakeFactory({ fetchRows: rows }),
234
- });
235
- await s.connect();
236
- await s.openMailbox("INBOX");
237
- const uids = [];
238
- for await (const env of s.fetchEnvelopesSince(0)) uids.push(env.uid);
239
- expect(uids).toEqual([10, 20, 30]);
240
- });
241
- });
242
-
243
- describe("ImapSession.fetchFullSince", () => {
244
- it("yields source bytes alongside envelope", async () => {
245
- const raw = Buffer.from("RAW RFC822 SOURCE BYTES", "utf8");
246
- const fakeRow = {
247
- uid: 100,
248
- internalDate: new Date(),
249
- flags: [],
250
- size: raw.length,
251
- source: raw,
252
- envelope: {
253
- messageId: "<m@x>",
254
- subject: "s",
255
- from: [{ name: "A", address: "a@b" }],
256
- to: [],
257
- cc: [],
258
- date: new Date(),
259
- },
260
- };
261
- const s = new ImapSession({
262
- host: "x", port: 993, user: "u@t", authCode: "z",
263
- imapFlowFactory: makeFakeFactory({ fetchRows: [fakeRow] }),
264
- });
265
- await s.connect();
266
- await s.openMailbox("INBOX");
267
- const got = [];
268
- for await (const row of s.fetchFullSince(0)) got.push(row);
269
- expect(got).toHaveLength(1);
270
- expect(Buffer.isBuffer(got[0].source)).toBe(true);
271
- expect(got[0].source.toString()).toBe("RAW RFC822 SOURCE BYTES");
272
- expect(got[0].uid).toBe(100);
273
- expect(got[0].subject).toBe("s");
274
- });
275
-
276
- it("emits empty Buffer when server returns no source", async () => {
277
- const fakeRow = {
278
- uid: 1,
279
- internalDate: new Date(),
280
- flags: [],
281
- size: 0,
282
- // no `source` field
283
- envelope: { messageId: "<m@x>", subject: "", from: [], to: [], cc: [], date: new Date() },
284
- };
285
- const s = new ImapSession({
286
- host: "x", port: 993, user: "u@t", authCode: "z",
287
- imapFlowFactory: makeFakeFactory({ fetchRows: [fakeRow] }),
288
- });
289
- await s.connect();
290
- await s.openMailbox("INBOX");
291
- const got = [];
292
- for await (const row of s.fetchFullSince(0)) got.push(row);
293
- expect(Buffer.isBuffer(got[0].source)).toBe(true);
294
- expect(got[0].source.length).toBe(0);
295
- });
296
- });
297
-
298
- describe("ImapSession.close", () => {
299
- it("is idempotent on never-connected session", async () => {
300
- const s = new ImapSession({
301
- host: "x", port: 993, user: "u@t", authCode: "z",
302
- imapFlowFactory: makeFakeFactory(),
303
- });
304
- await expect(s.close()).resolves.toBeUndefined();
305
- });
306
-
307
- it("clears client reference so post-close calls fail clean", async () => {
308
- const s = new ImapSession({
309
- host: "x", port: 993, user: "u@t", authCode: "z",
310
- imapFlowFactory: makeFakeFactory(),
311
- });
312
- await s.connect();
313
- await s.close();
314
- await expect(s.openMailbox("INBOX")).rejects.toThrow(/not connected/);
315
- });
316
-
317
- it("doesn't fail when underlying logout throws", async () => {
318
- let logoutCalled = false;
319
- function factory() {
320
- return {
321
- connect: async () => {},
322
- logout: async () => { logoutCalled = true; throw new Error("conn already dead"); },
323
- close: async () => {},
324
- };
325
- }
326
- const s = new ImapSession({
327
- host: "x", port: 993, user: "u@t", authCode: "z",
328
- imapFlowFactory: factory,
329
- });
330
- await s.connect();
331
- await expect(s.close()).resolves.toBeUndefined();
332
- expect(logoutCalled).toBe(true);
333
- });
334
- });