@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,822 +0,0 @@
1
- "use strict";
2
-
3
- import { describe, it, expect } from "vitest";
4
-
5
- const {
6
- extractAmounts,
7
- extractDates,
8
- extractAccountTails,
9
- extractOrderNumbers,
10
- extractTrackingNumbers,
11
- detectVerificationCodes,
12
- selectPrimaryAmount,
13
- dateToMs,
14
- normalizeCurrency,
15
- } = require("../../lib/adapters/email-imap/templates/utils");
16
-
17
- const { extractBill } = require("../../lib/adapters/email-imap/templates/bill");
18
- const { extractOrder } = require("../../lib/adapters/email-imap/templates/order");
19
- const { extractTravel } = require("../../lib/adapters/email-imap/templates/travel");
20
- const { extractGovernment } = require("../../lib/adapters/email-imap/templates/government");
21
- const { extractRegister } = require("../../lib/adapters/email-imap/templates/register");
22
- const { extractOther } = require("../../lib/adapters/email-imap/templates/other");
23
- const {
24
- extractFields,
25
- CATEGORY_TO_EXTRACTOR,
26
- } = require("../../lib/adapters/email-imap/templates");
27
-
28
- const { EmailAdapter } = require("../../lib/adapters/email-imap/email-adapter");
29
-
30
- // Helper email factory
31
- const emailOf = (overrides = {}) => ({
32
- from: [{ name: "Sender", address: "sender@example.com" }],
33
- subject: "",
34
- textBody: "",
35
- htmlBody: "",
36
- attachments: [],
37
- headers: {},
38
- ...overrides,
39
- });
40
-
41
- // ─── utils.js ───────────────────────────────────────────────────────────
42
-
43
- describe("templates/utils — extractAmounts", () => {
44
- it("recognizes ¥1,234.50 form", () => {
45
- const out = extractAmounts("应还金额 ¥1,234.50 元");
46
- expect(out.length).toBeGreaterThanOrEqual(1);
47
- expect(out[0].value).toBe(1234.5);
48
- expect(out[0].currency).toBe("CNY");
49
- });
50
-
51
- it("recognizes 99.00 元 trailing form", () => {
52
- const out = extractAmounts("总价 99.00 元");
53
- expect(out[0].value).toBe(99);
54
- expect(out[0].currency).toBe("CNY");
55
- });
56
-
57
- it("recognizes USD $100", () => {
58
- const out = extractAmounts("Total: USD 100.00");
59
- expect(out[0].currency).toBe("USD");
60
- expect(out[0].value).toBe(100);
61
- });
62
-
63
- it("tags out direction near 应还 keyword", () => {
64
- const out = extractAmounts("本期应还 ¥500 元");
65
- expect(out[0].direction).toBe("out");
66
- });
67
-
68
- it("tags in direction near 退款 keyword", () => {
69
- const out = extractAmounts("退款 ¥50.00 已到账");
70
- expect(out[0].direction).toBe("in");
71
- });
72
-
73
- it("returns [] for empty input", () => {
74
- expect(extractAmounts("")).toEqual([]);
75
- expect(extractAmounts(null)).toEqual([]);
76
- });
77
-
78
- it("ignores zero / negative-looking matches", () => {
79
- expect(extractAmounts("¥0")).toEqual([]);
80
- });
81
- });
82
-
83
- describe("templates/utils — extractDates", () => {
84
- it("parses YYYY-MM-DD", () => {
85
- const out = extractDates("到期: 2026-12-25");
86
- expect(out).toHaveLength(1);
87
- expect(out[0].date.getFullYear()).toBe(2026);
88
- expect(out[0].date.getMonth()).toBe(11);
89
- expect(out[0].date.getDate()).toBe(25);
90
- expect(out[0].hasYear).toBe(true);
91
- });
92
-
93
- it("parses YYYY年M月D日 (first hit has full year)", () => {
94
- const out = extractDates("最后还款日 2026年11月5日");
95
- expect(out.length).toBeGreaterThanOrEqual(1);
96
- // The full-year match is always first (sorted by source index)
97
- expect(out[0].hasYear).toBe(true);
98
- expect(out[0].date.getFullYear()).toBe(2026);
99
- expect(out[0].date.getMonth()).toBe(10);
100
- expect(out[0].date.getDate()).toBe(5);
101
- });
102
-
103
- it("year-less M月D日 fills current year", () => {
104
- const ref = new Date(2026, 5, 1).getTime(); // June 1
105
- const out = extractDates("活动日期: 7月15日", ref);
106
- expect(out).toHaveLength(1);
107
- expect(out[0].date.getFullYear()).toBe(2026);
108
- expect(out[0].hasYear).toBe(false);
109
- });
110
-
111
- it("returns [] for empty / invalid input", () => {
112
- expect(extractDates("")).toEqual([]);
113
- expect(extractDates(null)).toEqual([]);
114
- expect(extractDates("no dates here")).toEqual([]);
115
- });
116
- });
117
-
118
- describe("templates/utils — extractAccountTails", () => {
119
- it("recognizes 尾号 1234", () => {
120
- const out = extractAccountTails("信用卡 尾号 1234 本期账单");
121
- expect(out).toHaveLength(1);
122
- expect(out[0].last4).toBe("1234");
123
- });
124
-
125
- it("recognizes **** 5678", () => {
126
- const out = extractAccountTails("Card **** 5678");
127
- expect(out[0].last4).toBe("5678");
128
- });
129
-
130
- it("recognizes ending in 9999", () => {
131
- const out = extractAccountTails("Card ending in 9999 charged");
132
- expect(out[0].last4).toBe("9999");
133
- });
134
-
135
- it("returns [] for empty input", () => {
136
- expect(extractAccountTails("")).toEqual([]);
137
- });
138
- });
139
-
140
- describe("templates/utils — extractOrderNumbers", () => {
141
- it("recognizes 订单号: 12345678", () => {
142
- const out = extractOrderNumbers("订单号: 12345678 已发货");
143
- expect(out[0].orderNumber).toBe("12345678");
144
- });
145
-
146
- it("recognizes Order # ABC-1234", () => {
147
- const out = extractOrderNumbers("Order # ABC-1234 confirmed");
148
- expect(out[0].orderNumber).toBe("ABC-1234");
149
- });
150
- });
151
-
152
- describe("templates/utils — extractTrackingNumbers", () => {
153
- it("recognizes 快递单号: SF1234567", () => {
154
- const out = extractTrackingNumbers("快递单号: SF1234567 已揽收");
155
- expect(out[0].trackingNumber).toBe("SF1234567");
156
- });
157
-
158
- it("recognizes tracking number: YT9876543", () => {
159
- const out = extractTrackingNumbers("Tracking number: YT9876543");
160
- expect(out[0].trackingNumber).toBe("YT9876543");
161
- });
162
- });
163
-
164
- describe("templates/utils — detectVerificationCodes (REDACTED)", () => {
165
- it("counts occurrences without returning the code", () => {
166
- const r = detectVerificationCodes("您的验证码为 123456,5分钟内有效");
167
- expect(r.count).toBe(1);
168
- // CRITICAL — the code itself must NEVER be in the returned hit.
169
- expect(r.hits[0].raw).not.toMatch(/123456/);
170
- expect(r.hits[0].raw).toMatch(/\*+/);
171
- });
172
-
173
- it("recognizes OTP / 动态密码 / 安全码 too", () => {
174
- expect(detectVerificationCodes("OTP is 8888").count).toBe(1);
175
- expect(detectVerificationCodes("动态密码 9999").count).toBe(1);
176
- });
177
-
178
- it("returns 0 for non-verification text", () => {
179
- expect(detectVerificationCodes("纯营销邮件").count).toBe(0);
180
- });
181
- });
182
-
183
- describe("templates/utils — selectPrimaryAmount + dateToMs + normalizeCurrency", () => {
184
- it("selectPrimaryAmount picks directed over directionless", () => {
185
- const amounts = [
186
- { value: 100, currency: "CNY", raw: "100", index: 0 },
187
- { value: 50, currency: "CNY", raw: "50", direction: "out", index: 5 },
188
- ];
189
- const p = selectPrimaryAmount(amounts);
190
- expect(p.value).toBe(50);
191
- expect(p.direction).toBe("out");
192
- });
193
-
194
- it("dateToMs returns null for non-Date", () => {
195
- expect(dateToMs(null)).toBeNull();
196
- expect(dateToMs("2026-01-01")).toBeNull();
197
- expect(dateToMs(new Date(2026, 0, 1))).toBeGreaterThan(0);
198
- });
199
-
200
- it("normalizeCurrency maps Chinese markers", () => {
201
- expect(normalizeCurrency("¥")).toBe("CNY");
202
- expect(normalizeCurrency("元")).toBe("CNY");
203
- expect(normalizeCurrency("美元")).toBe("USD");
204
- expect(normalizeCurrency("$")).toBe("USD");
205
- });
206
- });
207
-
208
- // ─── bill.js ────────────────────────────────────────────────────────────
209
-
210
- describe("extractBill — bank statement", () => {
211
- it("招行月结: pulls amount + dueDate + accountIdentifier + institution", async () => {
212
- const r = await extractBill(emailOf({
213
- from: [{ address: "ebank@cmbchina.com" }],
214
- subject: "招商银行信用卡 11 月对账单",
215
- textBody: "尊敬的客户:\n您的招商银行信用卡 尾号 1234 本期应还金额 ¥3,256.78 元,最后还款日 2026-12-05。",
216
- }));
217
- expect(r.template).toBe("bill");
218
- expect(r.fields.amount.value).toBe(3256.78);
219
- expect(r.fields.amount.currency).toBe("CNY");
220
- expect(r.fields.accountIdentifier).toBe("**** 1234");
221
- expect(r.fields.institution).toBe("招商银行");
222
- expect(r.fields.dueDate).toBeGreaterThan(0);
223
- });
224
-
225
- it("dueAmount extraction via 应还金额 keyword window", async () => {
226
- const r = await extractBill(emailOf({
227
- from: [{ address: "card@cmbchina.com" }],
228
- subject: "信用卡账单",
229
- textBody: "本期账单金额 ¥5,000,应还金额 ¥3,000。",
230
- }));
231
- expect(r.fields.dueAmount).toBeDefined();
232
- expect(r.fields.dueAmount.value).toBe(3000);
233
- });
234
-
235
- it("billingMonth heuristic from subject when no dates parsed", async () => {
236
- const r = await extractBill(emailOf({
237
- from: [{ address: "ebank@ccb.com.cn" }],
238
- subject: "建设银行 8 月对账单",
239
- textBody: "",
240
- }));
241
- expect(r.fields.billingMonth).toMatch(/-08$/);
242
- });
243
-
244
- it("graceful fallback: no amount → empty fields + warning", async () => {
245
- const r = await extractBill(emailOf({
246
- from: [{ address: "ebank@somebank.com" }],
247
- subject: "通知",
248
- textBody: "提醒:账单已生成,请登录查看。",
249
- }));
250
- expect(r.fields.amount).toBeUndefined();
251
- expect(r.warnings).toContain("no monetary amount detected");
252
- });
253
-
254
- it("returns confidence in [0,1]", async () => {
255
- const r = await extractBill(emailOf({
256
- from: [{ address: "x@icbc.com.cn" }],
257
- subject: "工商银行 12 月对账单",
258
- textBody: "应还金额 ¥800 元 尾号 5555 最后还款日 2026-12-25",
259
- }));
260
- expect(r.confidence).toBeGreaterThan(0);
261
- expect(r.confidence).toBeLessThanOrEqual(1);
262
- });
263
- });
264
-
265
- // ─── bill.js Phase 5.5 — LLM gap-fill ─────────────────────────────────────
266
-
267
- describe("extractBill — Phase 5.5 LLM gap-fill", () => {
268
- // A body the regex can't crack (HTML-stripped marketing-style prose with
269
- // no recognizable keywords) so coverage stays under 0.6 and the LLM fires.
270
- // from:[] so even `institution` (otherwise derived from sender domain)
271
- // is missing — keeps regex coverage at 0 so the LLM path is exercised.
272
- const opaqueEmail = () => emailOf({
273
- from: [],
274
- subject: "Your statement is ready",
275
- textBody: "Hello, your latest statement is now available. Please sign in to view the details of your account activity for this period.",
276
- });
277
-
278
- const llmReturning = (obj, sink) => ({
279
- async chat(messages, _opts) {
280
- if (sink) sink.messages = messages;
281
- return { text: JSON.stringify(obj) };
282
- },
283
- });
284
-
285
- it("fills missing fields from LLM when regex coverage < 60%", async () => {
286
- const r = await extractBill(opaqueEmail(), {
287
- llm: llmReturning({
288
- amount: { value: 1234.5, currency: "CNY" },
289
- dueAmount: { value: 1000, currency: "CNY" },
290
- dueDate: "2026-12-20",
291
- billingPeriod: { start: "2026-11-01", end: "2026-11-30" },
292
- accountIdentifier: "6225 8801 2345 6789",
293
- institution: "Example Bank",
294
- billingMonth: "2026-11",
295
- }),
296
- });
297
- expect(r.fields.amount.value).toBe(1234.5);
298
- expect(r.fields.amount.direction).toBe("out");
299
- expect(r.fields.dueAmount.value).toBe(1000);
300
- expect(r.fields.dueDate).toBeGreaterThan(0);
301
- expect(r.fields.billingPeriod.startMs).toBeLessThan(r.fields.billingPeriod.endMs);
302
- // accountIdentifier coerced to last-4 only — never the full PAN
303
- expect(r.fields.accountIdentifier).toBe("**** 6789");
304
- expect(r.fields.institution).toBe("Example Bank");
305
- expect(r.fields.billingMonth).toBe("2026-11");
306
- expect(r.llmFilled).toEqual(
307
- expect.arrayContaining(["amount", "dueAmount", "dueDate", "accountIdentifier", "institution", "billingMonth"]),
308
- );
309
- });
310
-
311
- it("regex wins: LLM fills only the gaps, never overwrites a regex field", async () => {
312
- // Only an amount is regex-extractable → coverage 1/7 < 0.6, LLM fires.
313
- const r = await extractBill(emailOf({
314
- from: [],
315
- subject: "statement",
316
- textBody: "您的账单金额为 ¥3,256.78。",
317
- }), {
318
- llm: llmReturning({ amount: { value: 99999, currency: "USD" }, institution: "LLM Bank" }),
319
- });
320
- // regex amount retained, LLM's bogus 99999/USD ignored
321
- expect(r.fields.amount.value).toBe(3256.78);
322
- expect(r.fields.amount.currency).toBe("CNY");
323
- // institution was missing → LLM allowed to fill it
324
- expect(r.fields.institution).toBe("LLM Bank");
325
- expect(r.llmFilled).toEqual(["institution"]);
326
- });
327
-
328
- it("does NOT call the LLM when regex coverage already ≥ 60%", async () => {
329
- let called = false;
330
- const r = await extractBill(emailOf({
331
- from: [{ address: "ebank@ccb.com.cn" }],
332
- subject: "建设银行 11 月对账单",
333
- textBody: "本期应还金额 ¥800 元,尾号 5555,账单周期 2026-10-01 至 2026-10-31,最后还款日 2026-11-25。",
334
- }), {
335
- llm: { async chat() { called = true; return { text: "{}" }; } },
336
- });
337
- expect(called).toBe(false);
338
- expect(r.llmFilled).toBeUndefined();
339
- });
340
-
341
- it("drops malformed LLM values (bad dates, zero amounts, short account)", async () => {
342
- const r = await extractBill(opaqueEmail(), {
343
- llm: llmReturning({
344
- amount: { value: 0 }, // non-positive → dropped
345
- dueDate: "2026-13-45", // impossible date → dropped
346
- accountIdentifier: "12", // < 4 digits → dropped
347
- billingMonth: "2026-99", // bad month → dropped
348
- institution: " ", // blank → dropped
349
- }),
350
- });
351
- expect(r.fields.amount).toBeUndefined();
352
- expect(r.fields.dueDate).toBeUndefined();
353
- expect(r.fields.accountIdentifier).toBeUndefined();
354
- expect(r.fields.billingMonth).toBeUndefined();
355
- expect(r.fields.institution).toBeUndefined();
356
- expect(r.llmFilled).toBeUndefined();
357
- });
358
-
359
- it("records a warning when the LLM returns unparseable output", async () => {
360
- const r = await extractBill(opaqueEmail(), {
361
- llm: { async chat() { return { text: "sorry, I can't help with that" }; } },
362
- });
363
- expect(r.warnings.some((w) => w.includes("not parseable JSON"))).toBe(true);
364
- });
365
-
366
- it("records a warning when the LLM call throws", async () => {
367
- const r = await extractBill(opaqueEmail(), {
368
- llm: { async chat() { throw new Error("rate limited"); } },
369
- });
370
- expect(r.warnings.some((w) => w.includes("LLM bill fill failed") && w.includes("rate limited"))).toBe(true);
371
- });
372
-
373
- it("tolerates LLM output wrapped in ```json fences", async () => {
374
- const r = await extractBill(opaqueEmail(), {
375
- llm: { async chat() { return { text: "```json\n{\"institution\":\"Fenced Bank\"}\n```" }; } },
376
- });
377
- expect(r.fields.institution).toBe("Fenced Bank");
378
- expect(r.llmFilled).toEqual(["institution"]);
379
- });
380
-
381
- it("no LLM provided → behaves exactly as before (no llmFilled, no warning)", async () => {
382
- const r = await extractBill(opaqueEmail());
383
- expect(r.llmFilled).toBeUndefined();
384
- expect(r.warnings.every((w) => !w.includes("LLM"))).toBe(true);
385
- });
386
- });
387
-
388
- // ─── order.js ───────────────────────────────────────────────────────────
389
-
390
- describe("extractOrder — e-commerce", () => {
391
- it("淘宝订单: extracts orderNumber + totalAmount + merchantPlatform", async () => {
392
- const r = await extractOrder(emailOf({
393
- from: [{ address: "notify@taobao.com" }],
394
- subject: "您的订单已发货",
395
- textBody: "订单号: 1234567890,共3件商品,总价 ¥299.00 元。已发货。",
396
- }));
397
- expect(r.template).toBe("order");
398
- expect(r.fields.orderNumber).toBe("1234567890");
399
- expect(r.fields.totalAmount.value).toBe(299);
400
- expect(r.fields.merchantPlatform).toBe("淘宝");
401
- expect(r.fields.itemCount).toBe(3);
402
- expect(r.fields.orderStatus).toBe("shipped");
403
- });
404
-
405
- it("京东订单: status=delivered when 签收 in body", async () => {
406
- const r = await extractOrder(emailOf({
407
- from: [{ address: "notice@jd.com" }],
408
- subject: "包裹已签收",
409
- textBody: "您的订单 JD-998877 已签收。快递单号: YT123456789",
410
- }));
411
- expect(r.fields.merchantPlatform).toBe("京东");
412
- expect(r.fields.orderStatus).toBe("delivered");
413
- expect(r.fields.trackingNumber).toBe("YT123456789");
414
- });
415
-
416
- it("recipient extraction from 收件人 keyword", async () => {
417
- const r = await extractOrder(emailOf({
418
- from: [{ address: "service@pinduoduo.com" }],
419
- subject: "订单",
420
- textBody: "订单号: PDD-1111\n收件人: 张三",
421
- }));
422
- expect(r.fields.recipient).toBe("张三");
423
- });
424
-
425
- it("graceful fallback: no orderNumber → warning", async () => {
426
- const r = await extractOrder(emailOf({
427
- from: [{ address: "x@taobao.com" }],
428
- subject: "营销邮件",
429
- textBody: "看看这些好物",
430
- }));
431
- expect(r.fields.orderNumber).toBeUndefined();
432
- expect(r.warnings).toContain("orderNumber not detected");
433
- });
434
- });
435
-
436
- // ─── travel.js ──────────────────────────────────────────────────────────
437
-
438
- describe("extractTravel — flight / hotel / train", () => {
439
- it("flight: vehicleType=flight + carrier mapped from ctrip.com", async () => {
440
- const r = await extractTravel(emailOf({
441
- from: [{ address: "ticket@ctrip.com" }],
442
- subject: "您的航班预订确认",
443
- textBody: "航班号 CA1234,从 北京 → 上海,起飞时间 2026-06-15。",
444
- }));
445
- expect(r.template).toBe("travel");
446
- expect(r.fields.vehicleType).toBe("flight");
447
- expect(r.fields.carrier).toBe("携程");
448
- expect(r.fields.from).toBeDefined();
449
- expect(r.fields.to).toBeDefined();
450
- });
451
-
452
- it("train: 12306 carrier mapping + 高铁 vehicle keyword", async () => {
453
- const r = await extractTravel(emailOf({
454
- from: [{ address: "noreply@12306.cn" }],
455
- subject: "您的高铁车票",
456
- textBody: "车次 G123,北京 → 上海,发车时间 2026-07-01。",
457
- }));
458
- expect(r.fields.vehicleType).toBe("train");
459
- expect(r.fields.carrier).toBe("12306");
460
- });
461
-
462
- it("hotel: vehicleType=hotel + traveler detected", async () => {
463
- const r = await extractTravel(emailOf({
464
- from: [{ address: "reservations@hotel.example" }],
465
- subject: "酒店预订确认",
466
- textBody: "酒店入住 2026-05-20,退房 2026-05-22。入住人: 王五",
467
- }));
468
- expect(r.fields.vehicleType).toBe("hotel");
469
- expect(r.fields.traveler).toBe("王五");
470
- });
471
- });
472
-
473
- // ─── government.js ─────────────────────────────────────────────────────
474
-
475
- describe("extractGovernment", () => {
476
- it("tax_declaration recognized", async () => {
477
- const r = await extractGovernment(emailOf({
478
- from: [{ name: "国家税务总局", address: "tax@tax.gov.cn" }],
479
- subject: "个税申报确认",
480
- textBody: "您的个税申报已完成。属期 2026-04。申报编号: T2026040000001",
481
- }));
482
- expect(r.template).toBe("government");
483
- expect(r.fields.documentType).toBe("tax_declaration");
484
- expect(r.fields.period).toBe("2026-04");
485
- expect(r.fields.agencyName).toBe("国家税务总局");
486
- expect(r.fields.referenceNumber).toBeDefined();
487
- });
488
-
489
- it("social_security recognized", async () => {
490
- const r = await extractGovernment(emailOf({
491
- from: [{ address: "noreply@sb.gov.cn" }],
492
- subject: "社保缴费提醒",
493
- textBody: "您的社保医疗保险已缴纳 ¥1,250.00",
494
- }));
495
- expect(r.fields.documentType).toBe("social_security");
496
- expect(r.fields.amount).toBeDefined();
497
- });
498
-
499
- it("documentType=other when no keyword matches + warning", async () => {
500
- const r = await extractGovernment(emailOf({
501
- from: [{ address: "x@somewhere.gov.cn" }],
502
- subject: "general notification",
503
- textBody: "no keywords here",
504
- }));
505
- expect(r.fields.documentType).toBe("other");
506
- expect(r.warnings).toContain("documentType could not be narrowed");
507
- });
508
- });
509
-
510
- // ─── register.js ────────────────────────────────────────────────────────
511
-
512
- describe("extractRegister — verification code REDACTION", () => {
513
- it("2fa_code email: verificationCodePresent=true, code NEVER in fields", async () => {
514
- const r = await extractRegister(emailOf({
515
- from: [{ name: "GitHub", address: "noreply@github.com" }],
516
- subject: "Your verification code",
517
- textBody: "您的验证码为 654321,5分钟内有效。",
518
- }));
519
- expect(r.template).toBe("register");
520
- expect(r.fields.actionType).toBe("2fa_code");
521
- expect(r.fields.verificationCodePresent).toBe(true);
522
- // The literal code must NEVER appear in the returned fields.
523
- const serialized = JSON.stringify(r.fields);
524
- expect(serialized).not.toMatch(/654321/);
525
- expect(r.fields.serviceName).toBe("GitHub");
526
- });
527
-
528
- it("password_reset action recognized", async () => {
529
- const r = await extractRegister(emailOf({
530
- from: [{ name: "Apple ID", address: "noreply@apple.com" }],
531
- subject: "Reset your password",
532
- textBody: "Someone requested a password reset for your account.",
533
- }));
534
- expect(r.fields.actionType).toBe("password_reset");
535
- });
536
-
537
- it("register action recognized", async () => {
538
- const r = await extractRegister(emailOf({
539
- from: [{ name: "Service", address: "x@service.example" }],
540
- subject: "Welcome to Service",
541
- textBody: "Your account has been created. Welcome to Service!",
542
- }));
543
- expect(r.fields.actionType).toBe("register");
544
- });
545
-
546
- it("login_alert action", async () => {
547
- const r = await extractRegister(emailOf({
548
- from: [{ address: "alerts@bank.example" }],
549
- subject: "New sign-in alert",
550
- textBody: "新设备登录 from Beijing",
551
- }));
552
- expect(r.fields.actionType).toBe("login_alert");
553
- });
554
-
555
- it("no verification code → verificationCodePresent=false", async () => {
556
- const r = await extractRegister(emailOf({
557
- from: [{ address: "x@example.com" }],
558
- subject: "Welcome",
559
- textBody: "Welcome to our service.",
560
- }));
561
- expect(r.fields.verificationCodePresent).toBe(false);
562
- });
563
- });
564
-
565
- // ─── other.js ──────────────────────────────────────────────────────────
566
-
567
- describe("extractOther — fallback", () => {
568
- it("no-LLM fallback: produces summary from first sentence", async () => {
569
- const r = await extractOther(emailOf({
570
- from: [{ name: "Newsletter", address: "news@example.com" }],
571
- subject: "Weekly update",
572
- textBody: "This week we are excited to announce a new feature. There's more!",
573
- }));
574
- expect(r.template).toBe("other");
575
- expect(r.fields.summary).toBeDefined();
576
- expect(r.fields.summary).toContain("This week");
577
- });
578
-
579
- it("with LLM: parses JSON {summary, topics}", async () => {
580
- const llm = {
581
- chat: async () => ({ text: '{"summary":"AI news roundup","topics":["ai","news"]}' }),
582
- };
583
- const r = await extractOther(emailOf({
584
- textBody: "Some long body content",
585
- }), { llm });
586
- expect(r.fields.summary).toBe("AI news roundup");
587
- expect(r.fields.topics).toEqual(["ai", "news"]);
588
- });
589
-
590
- it("LLM throws → falls back to deterministic summary + warning", async () => {
591
- const llm = { chat: async () => { throw new Error("LLM down"); } };
592
- const r = await extractOther(emailOf({
593
- textBody: "Plain body text.",
594
- }), { llm });
595
- expect(r.fields.summary).toBe("Plain body text");
596
- expect(r.warnings.some((w) => w.includes("LLM"))).toBe(true);
597
- });
598
-
599
- it("empty body → confidence 0 + no summary", async () => {
600
- const r = await extractOther(emailOf({ textBody: "" }));
601
- expect(r.fields.summary).toBeUndefined();
602
- expect(r.confidence).toBe(0);
603
- });
604
- });
605
-
606
- // ─── dispatcher (templates/index.js) ───────────────────────────────────
607
-
608
- describe("extractFields dispatcher", () => {
609
- it("routes bill_bank → extractBill", async () => {
610
- const r = await extractFields(
611
- emailOf({ from: [{ address: "x@cmbchina.com" }], textBody: "应还 ¥100 元" }),
612
- { category: "bill_bank" },
613
- );
614
- expect(r.template).toBe("bill");
615
- });
616
-
617
- it("routes order → extractOrder", async () => {
618
- const r = await extractFields(
619
- emailOf({ from: [{ address: "x@taobao.com" }], textBody: "订单号: ABC123" }),
620
- { category: "order" },
621
- );
622
- expect(r.template).toBe("order");
623
- });
624
-
625
- it("routes travel → extractTravel", async () => {
626
- const r = await extractFields(
627
- emailOf({ from: [{ address: "x@ctrip.com" }], textBody: "航班 CA123" }),
628
- { category: "travel" },
629
- );
630
- expect(r.template).toBe("travel");
631
- });
632
-
633
- it("routes government → extractGovernment", async () => {
634
- const r = await extractFields(
635
- emailOf({ textBody: "完税证明" }),
636
- { category: "government" },
637
- );
638
- expect(r.template).toBe("government");
639
- });
640
-
641
- it("routes register → extractRegister", async () => {
642
- const r = await extractFields(
643
- emailOf({ textBody: "验证码 9999" }),
644
- { category: "register" },
645
- );
646
- expect(r.template).toBe("register");
647
- });
648
-
649
- it("routes notify + other + unknown → extractOther", async () => {
650
- expect((await extractFields(emailOf({ textBody: "X" }), { category: "notify" })).template).toBe("other");
651
- expect((await extractFields(emailOf({ textBody: "X" }), { category: "other" })).template).toBe("other");
652
- expect((await extractFields(emailOf({ textBody: "X" }), { category: "unknown" })).template).toBe("other");
653
- expect((await extractFields(emailOf({ textBody: "X" }), null)).template).toBe("other");
654
- });
655
-
656
- it("CATEGORY_TO_EXTRACTOR is frozen + covers 8 categories", () => {
657
- expect(Object.isFrozen(CATEGORY_TO_EXTRACTOR)).toBe(true);
658
- const keys = Object.keys(CATEGORY_TO_EXTRACTOR);
659
- expect(keys).toContain("bill_bank");
660
- expect(keys).toContain("bill_credit");
661
- expect(keys).toContain("order");
662
- expect(keys).toContain("travel");
663
- expect(keys).toContain("government");
664
- expect(keys).toContain("register");
665
- expect(keys).toContain("notify");
666
- expect(keys).toContain("other");
667
- });
668
-
669
- it("extractor throwing degrades gracefully", async () => {
670
- // Synthesize by passing a malformed email that crashes most code paths
671
- const r = await extractFields(null, { category: "bill_bank" });
672
- expect(r.template).toBe("other");
673
- expect(r.fields).toEqual({});
674
- expect(r.warnings[0]).toContain("email missing");
675
- });
676
- });
677
-
678
- // ─── EmailAdapter integration (Phase 5.4) ───────────────────────────────
679
-
680
- function makeSession(envelopes) {
681
- return (_opts) => {
682
- let openMb = null;
683
- return {
684
- async connect() {},
685
- async openMailbox(name) {
686
- openMb = name;
687
- return { uidValidity: 1, uidNext: 9999, exists: envelopes.length };
688
- },
689
- async *fetchFullSince(sinceUid = 0) {
690
- for (const env of envelopes) {
691
- if (env.uid > sinceUid) yield { ...env, source: env.source || Buffer.alloc(0) };
692
- }
693
- },
694
- async close() {},
695
- };
696
- };
697
- }
698
-
699
- describe("EmailAdapter — Phase 5.4 extraction integration", () => {
700
- const env1 = (overrides) => ({
701
- uid: 1,
702
- internalDate: new Date("2026-05-01T10:00:00Z"),
703
- flags: ["\\Seen"],
704
- messageId: "<m1@x>",
705
- subject: "招商银行 11 月对账单",
706
- from: [{ name: "招商银行", address: "ebank@cmbchina.com" }],
707
- to: [{ address: "me@example.com" }],
708
- cc: [],
709
- date: new Date("2026-05-01T10:00:00Z"),
710
- size: 2048,
711
- source: Buffer.from("RAW", "utf8"),
712
- ...overrides,
713
- });
714
-
715
- it("capabilities advertise extract:6-templates", () => {
716
- const a = new EmailAdapter({
717
- account: { provider: "qq", email: "u@qq.com", authCode: "x" },
718
- sessionFactory: makeSession([]),
719
- });
720
- expect(a.capabilities).toContain("extract:6-templates");
721
- });
722
-
723
- it("sync attaches extraction.fields to payload for bill emails", async () => {
724
- const factory = makeSession([env1()]);
725
- const a = new EmailAdapter({
726
- account: { provider: "qq", email: "u@qq.com", authCode: "x", folders: ["INBOX"] },
727
- sessionFactory: factory,
728
- parser: async () => ({
729
- textBody: "尾号 1234 应还金额 ¥3,000 元 最后还款日 2026-12-05",
730
- attachments: [],
731
- }),
732
- });
733
- const raws = [];
734
- for await (const r of a.sync()) raws.push(r);
735
- expect(raws).toHaveLength(1);
736
- expect(raws[0].payload.extraction).toBeDefined();
737
- expect(raws[0].payload.extraction.template).toBe("bill");
738
- expect(raws[0].payload.extraction.fields.amount.value).toBe(3000);
739
- expect(raws[0].payload.extraction.fields.accountIdentifier).toBe("**** 1234");
740
- });
741
-
742
- it("normalize copies extraction.fields into Event.extra.fields", async () => {
743
- const factory = makeSession([env1()]);
744
- const a = new EmailAdapter({
745
- account: { provider: "qq", email: "u@qq.com", authCode: "x", folders: ["INBOX"] },
746
- sessionFactory: factory,
747
- parser: async () => ({ textBody: "应还 ¥500 元 尾号 9999", attachments: [] }),
748
- });
749
- const raws = [];
750
- for await (const r of a.sync()) raws.push(r);
751
- const batch = a.normalize(raws[0]);
752
- expect(batch.events).toHaveLength(1);
753
- const ev = batch.events[0];
754
- expect(ev.extra.fields).toBeDefined();
755
- expect(ev.extra.fields.amount.value).toBe(500);
756
- expect(ev.extra.extractionTemplate).toBe("bill");
757
- expect(ev.extra.extractionConfidence).toBeGreaterThan(0);
758
- });
759
-
760
- it("verification-code email REDACTS event.content.text", async () => {
761
- const verifEnv = env1({
762
- uid: 2,
763
- messageId: "<m2@x>",
764
- subject: "Your verification code",
765
- from: [{ name: "GitHub", address: "noreply@github.com" }],
766
- });
767
- const factory = makeSession([verifEnv]);
768
- const a = new EmailAdapter({
769
- account: { provider: "qq", email: "u@qq.com", authCode: "x", folders: ["INBOX"] },
770
- sessionFactory: factory,
771
- parser: async () => ({
772
- textBody: "您的验证码为 998877,5分钟内有效。",
773
- attachments: [],
774
- }),
775
- });
776
- const raws = [];
777
- for await (const r of a.sync()) raws.push(r);
778
- expect(raws[0].payload.extraction.template).toBe("register");
779
- expect(raws[0].payload.extraction.fields.verificationCodePresent).toBe(true);
780
- const batch = a.normalize(raws[0]);
781
- expect(batch.events[0].content.text).toBe("(redacted: verification code email)");
782
- // The literal code must NEVER survive into the normalized event.
783
- expect(JSON.stringify(batch.events[0])).not.toMatch(/998877/);
784
- });
785
-
786
- it("disableExtraction skips extractor entirely", async () => {
787
- const factory = makeSession([env1()]);
788
- const a = new EmailAdapter({
789
- account: { provider: "qq", email: "u@qq.com", authCode: "x", folders: ["INBOX"] },
790
- sessionFactory: factory,
791
- parser: async () => ({ textBody: "应还 ¥500 元", attachments: [] }),
792
- disableExtraction: true,
793
- });
794
- const raws = [];
795
- for await (const r of a.sync()) raws.push(r);
796
- expect(raws[0].payload.extraction).toBeUndefined();
797
- });
798
-
799
- it("extractor throwing → degrades to template:other (sync still emits)", async () => {
800
- const factory = makeSession([env1()]);
801
- const a = new EmailAdapter({
802
- account: { provider: "qq", email: "u@qq.com", authCode: "x", folders: ["INBOX"] },
803
- sessionFactory: factory,
804
- parser: async () => ({ textBody: "X", attachments: [] }),
805
- extractor: async () => { throw new Error("boom"); },
806
- });
807
- const raws = [];
808
- for await (const r of a.sync()) raws.push(r);
809
- expect(raws).toHaveLength(1);
810
- expect(raws[0].payload.extraction.template).toBe("other");
811
- expect(raws[0].payload.extraction.confidence).toBe(0);
812
- expect(raws[0].payload.extraction.warnings[0]).toContain("boom");
813
- });
814
-
815
- it("version reflects 0.6.0 (Phase 5.7)", () => {
816
- const a = new EmailAdapter({
817
- account: { provider: "qq", email: "u@qq.com", authCode: "x" },
818
- sessionFactory: makeSession([]),
819
- });
820
- expect(a.version).toBe("0.7.0");
821
- });
822
- });