@chainlesschain/personal-data-hub 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (154) hide show
  1. package/__tests__/adapters/ai-chat-cookie-capture-spec.test.js +211 -0
  2. package/__tests__/adapters/ai-chat-health-checker.test.js +262 -0
  3. package/__tests__/adapters/ai-chat-history.test.js +396 -0
  4. package/__tests__/adapters/ai-chat-http-client.test.js +242 -0
  5. package/__tests__/adapters/ai-chat-vendors.test.js +874 -0
  6. package/__tests__/adapters/alipay-bill-adapter.test.js +538 -0
  7. package/__tests__/adapters/email-adapter.test.js +138 -1
  8. package/__tests__/adapters/email-classifier.test.js +347 -0
  9. package/__tests__/adapters/email-pdf-extractor.test.js +529 -0
  10. package/__tests__/adapters/email-retry-progress.test.js +294 -0
  11. package/__tests__/adapters/email-templates.test.js +699 -0
  12. package/__tests__/adapters/social-toutiao-kuaishou-scaffold.test.js +269 -0
  13. package/__tests__/adapters/system-data-adapter.test.js +440 -0
  14. package/__tests__/adapters/system-data-android-ingest.test.js +144 -0
  15. package/__tests__/adapters/system-data-android.test.js +387 -0
  16. package/__tests__/adapters/system-data-disclosure.test.js +153 -0
  17. package/__tests__/adapters/wechat-bootstrap.test.js +240 -0
  18. package/__tests__/adapters/wechat-env-probe.test.js +162 -0
  19. package/__tests__/adapters/wechat-frida-agent.test.js +191 -0
  20. package/__tests__/adapters/wechat-frida-integration.test.js +149 -0
  21. package/__tests__/adapters/wechat-frida-key-provider.test.js +188 -0
  22. package/__tests__/adapters/wechat-md5-key-provider.test.js +101 -0
  23. package/__tests__/analysis-skills.test.js +556 -0
  24. package/__tests__/analysis.test.js +329 -1
  25. package/__tests__/e2e/ai-chat-cross-source-journey.test.js +213 -0
  26. package/__tests__/e2e/full-user-journey.test.js +188 -0
  27. package/__tests__/entity-resolver-ingest-hook.test.js +177 -0
  28. package/__tests__/entity-resolver-stages.test.js +411 -0
  29. package/__tests__/entity-resolver-vault.test.js +246 -0
  30. package/__tests__/entity-resolver.test.js +526 -0
  31. package/__tests__/fixtures/entity-resolver-200-mock.json +96 -0
  32. package/__tests__/integration/ai-chat-history-registry.test.js +228 -0
  33. package/__tests__/integration/aichat-wizard-end-to-end.test.js +282 -0
  34. package/__tests__/integration/cross-adapter-pipelines.test.js +396 -0
  35. package/__tests__/integration/wechat-bootstrap-end-to-end.test.js +390 -0
  36. package/__tests__/longtail-adapters.test.js +217 -0
  37. package/__tests__/mobile-extractor.test.js +288 -0
  38. package/__tests__/registry.test.js +4 -2
  39. package/__tests__/shopping-adapters.test.js +296 -0
  40. package/__tests__/sidecar-contacts-cross-validate.test.js +163 -0
  41. package/__tests__/sidecar-supervisor.test.js +120 -0
  42. package/__tests__/social-adapters.test.js +206 -0
  43. package/__tests__/travel-adapters.test.js +325 -0
  44. package/__tests__/vault.test.js +3 -3
  45. package/__tests__/wechat-adapter.test.js +476 -0
  46. package/__tests__/whatsapp-adapter.test.js +135 -0
  47. package/lib/adapter-spec.js +12 -0
  48. package/lib/adapters/_python-sidecar-base.js +207 -0
  49. package/lib/adapters/ai-chat-history/ai-chat-adapter.js +374 -0
  50. package/lib/adapters/ai-chat-history/cookie-auth.js +109 -0
  51. package/lib/adapters/ai-chat-history/cookie-capture-spec.js +331 -0
  52. package/lib/adapters/ai-chat-history/health-checker.js +210 -0
  53. package/lib/adapters/ai-chat-history/http-client.js +211 -0
  54. package/lib/adapters/ai-chat-history/index.js +28 -0
  55. package/lib/adapters/ai-chat-history/schema-map.js +258 -0
  56. package/lib/adapters/ai-chat-history/vendor-spec.js +86 -0
  57. package/lib/adapters/ai-chat-history/vendors/coze.js +179 -0
  58. package/lib/adapters/ai-chat-history/vendors/deepseek.js +199 -0
  59. package/lib/adapters/ai-chat-history/vendors/doubao.js +255 -0
  60. package/lib/adapters/ai-chat-history/vendors/dreamina.js +174 -0
  61. package/lib/adapters/ai-chat-history/vendors/hunyuan.js +176 -0
  62. package/lib/adapters/ai-chat-history/vendors/kimi.js +182 -0
  63. package/lib/adapters/ai-chat-history/vendors/qianfan.js +160 -0
  64. package/lib/adapters/ai-chat-history/vendors/tongyi.js +193 -0
  65. package/lib/adapters/ai-chat-history/vendors/zhipu.js +202 -0
  66. package/lib/adapters/ai-chat-history/wizard-controller.js +473 -0
  67. package/lib/adapters/alipay-bill/alipay-bill-adapter.js +311 -0
  68. package/lib/adapters/alipay-bill/counterparty.js +129 -0
  69. package/lib/adapters/alipay-bill/csv-parser.js +217 -0
  70. package/lib/adapters/alipay-bill/index.js +41 -0
  71. package/lib/adapters/alipay-bill/zip-decryptor.js +111 -0
  72. package/lib/adapters/email-imap/classifier.js +495 -0
  73. package/lib/adapters/email-imap/email-adapter.js +419 -8
  74. package/lib/adapters/email-imap/index.js +42 -0
  75. package/lib/adapters/email-imap/pdf-extractor.js +192 -0
  76. package/lib/adapters/email-imap/templates/bill.js +232 -0
  77. package/lib/adapters/email-imap/templates/government.js +120 -0
  78. package/lib/adapters/email-imap/templates/index.js +78 -0
  79. package/lib/adapters/email-imap/templates/order.js +186 -0
  80. package/lib/adapters/email-imap/templates/other.js +114 -0
  81. package/lib/adapters/email-imap/templates/register.js +113 -0
  82. package/lib/adapters/email-imap/templates/travel.js +157 -0
  83. package/lib/adapters/email-imap/templates/utils.js +275 -0
  84. package/lib/adapters/email-imap/transactions.js +234 -0
  85. package/lib/adapters/messaging-qq/index.js +158 -0
  86. package/lib/adapters/messaging-telegram/index.js +142 -0
  87. package/lib/adapters/messaging-whatsapp/index.js +189 -0
  88. package/lib/adapters/shopping-base/index.js +208 -0
  89. package/lib/adapters/shopping-jd/index.js +150 -0
  90. package/lib/adapters/shopping-meituan/index.js +154 -0
  91. package/lib/adapters/shopping-taobao/index.js +176 -0
  92. package/lib/adapters/social-bilibili/index.js +171 -0
  93. package/lib/adapters/social-douyin/index.js +116 -0
  94. package/lib/adapters/social-kuaishou/index.js +237 -0
  95. package/lib/adapters/social-toutiao/index.js +236 -0
  96. package/lib/adapters/social-weibo/index.js +164 -0
  97. package/lib/adapters/social-xiaohongshu/index.js +96 -0
  98. package/lib/adapters/system-data/disclosure.js +166 -0
  99. package/lib/adapters/system-data/index.js +34 -0
  100. package/lib/adapters/system-data/system-data-adapter.js +344 -0
  101. package/lib/adapters/system-data-android/adapter.js +348 -0
  102. package/lib/adapters/system-data-android/index.js +76 -0
  103. package/lib/adapters/travel-12306/index.js +151 -0
  104. package/lib/adapters/travel-amap/index.js +164 -0
  105. package/lib/adapters/travel-baidu-map/index.js +162 -0
  106. package/lib/adapters/travel-base/index.js +240 -0
  107. package/lib/adapters/travel-ctrip/index.js +151 -0
  108. package/lib/adapters/wechat/bootstrap.js +146 -0
  109. package/lib/adapters/wechat/content-parser.js +326 -0
  110. package/lib/adapters/wechat/db-reader.js +209 -0
  111. package/lib/adapters/wechat/env-probe.js +218 -0
  112. package/lib/adapters/wechat/frida-agent/loader.js +67 -0
  113. package/lib/adapters/wechat/frida-agent/wechat-key-hook.js +126 -0
  114. package/lib/adapters/wechat/index.js +37 -0
  115. package/lib/adapters/wechat/key-extractor.js +158 -0
  116. package/lib/adapters/wechat/key-providers/frida-key-provider.js +244 -0
  117. package/lib/adapters/wechat/key-providers/index.js +22 -0
  118. package/lib/adapters/wechat/key-providers/key-provider-base.js +44 -0
  119. package/lib/adapters/wechat/key-providers/md5-key-provider.js +81 -0
  120. package/lib/adapters/wechat/normalize.js +220 -0
  121. package/lib/adapters/wechat/wechat-adapter.js +205 -0
  122. package/lib/analysis-skills/base.js +113 -0
  123. package/lib/analysis-skills/footprint.js +167 -0
  124. package/lib/analysis-skills/index.js +58 -0
  125. package/lib/analysis-skills/interests.js +161 -0
  126. package/lib/analysis-skills/relations.js +226 -0
  127. package/lib/analysis-skills/spending.js +219 -0
  128. package/lib/analysis-skills/timeline.js +167 -0
  129. package/lib/analysis.js +191 -2
  130. package/lib/entity-resolver/embedding-stage.js +198 -0
  131. package/lib/entity-resolver/entity-resolver.js +384 -0
  132. package/lib/entity-resolver/index.js +42 -0
  133. package/lib/entity-resolver/llm-stage.js +191 -0
  134. package/lib/entity-resolver/rule-stage.js +208 -0
  135. package/lib/entity-resolver/worker.js +149 -0
  136. package/lib/index.js +131 -0
  137. package/lib/migrations.js +73 -0
  138. package/lib/mobile-extractor/android.js +193 -0
  139. package/lib/mobile-extractor/index.js +9 -0
  140. package/lib/mobile-extractor/ios.js +223 -0
  141. package/lib/prompt-builder.js +11 -1
  142. package/lib/query-parser.js +7 -1
  143. package/lib/registry.js +42 -0
  144. package/lib/sidecar/index.js +15 -0
  145. package/lib/sidecar/supervisor.js +359 -0
  146. package/lib/vault.js +343 -0
  147. package/package.json +36 -3
  148. package/scripts/_make-fixture-all.js +126 -0
  149. package/scripts/_make-fixture-contacts.js +84 -0
  150. package/scripts/evaluate-entity-resolver.js +213 -0
  151. package/scripts/smoke-phase-5-5.js +196 -0
  152. package/scripts/smoke-phase-5-7.js +181 -0
  153. package/scripts/smoke-system-data-contacts.js +309 -0
  154. package/scripts/smoke-system-data.js +312 -0
@@ -0,0 +1,538 @@
1
+ "use strict";
2
+
3
+ import { describe, it, expect } from "vitest";
4
+
5
+ const {
6
+ AlipayBillAdapter,
7
+ mapAlipayTypeToSubtype,
8
+ parseAlipayDateTime,
9
+ } = require("../../lib/adapters/alipay-bill/alipay-bill-adapter");
10
+ const {
11
+ parseAlipayCsv,
12
+ parseAlipayCsvBuffer,
13
+ decodeBuffer,
14
+ splitCsvLine,
15
+ FIELD_ORDER,
16
+ } = require("../../lib/adapters/alipay-bill/csv-parser");
17
+ const {
18
+ classifyCounterparty,
19
+ counterpartyToPersonId,
20
+ normalizeCounterpartyName,
21
+ KNOWN_MERCHANTS,
22
+ } = require("../../lib/adapters/alipay-bill/counterparty");
23
+ const { assertAdapter } = require("../../lib/adapter-spec");
24
+
25
+ // ─── CSV parser ─────────────────────────────────────────────────────────
26
+
27
+ describe("csv-parser — splitCsvLine", () => {
28
+ it("simple comma-separated line", () => {
29
+ expect(splitCsvLine("a,b,c")).toEqual(["a", "b", "c"]);
30
+ });
31
+
32
+ it("Alipay row with empty fields", () => {
33
+ expect(splitCsvLine("2024,,,d")).toEqual(["2024", "", "", "d"]);
34
+ });
35
+
36
+ it("quoted field with comma inside", () => {
37
+ expect(splitCsvLine('a,"b, c",d')).toEqual(["a", "b, c", "d"]);
38
+ });
39
+
40
+ it("doubled-quote escape", () => {
41
+ expect(splitCsvLine('a,"b""c",d')).toEqual(["a", 'b"c', "d"]);
42
+ });
43
+ });
44
+
45
+ describe("csv-parser — decodeBuffer", () => {
46
+ it("UTF-8 with BOM strips BOM and matches Alipay header", () => {
47
+ const text = "支付宝交易记录明细查询\n交易号,商家订单号";
48
+ const buf = Buffer.concat([Buffer.from([0xef, 0xbb, 0xbf]), Buffer.from(text, "utf-8")]);
49
+ const r = decodeBuffer(buf);
50
+ expect(r.encoding).toBe("utf-8");
51
+ expect(r.text).toContain("支付宝交易记录");
52
+ });
53
+
54
+ it("UTF-8 without BOM still detects Alipay magic header", () => {
55
+ const buf = Buffer.from("交易号,商家订单号", "utf-8");
56
+ const r = decodeBuffer(buf);
57
+ expect(r.encoding).toBe("utf-8");
58
+ });
59
+
60
+ it("falls back to GBK when text is not valid UTF-8", () => {
61
+ // Use injected iconv stub to avoid pulling the dep
62
+ const fakeBuf = Buffer.from([0x80, 0x81, 0x82]); // not valid Alipay-ish UTF-8
63
+ const r = decodeBuffer(fakeBuf, {
64
+ iconvImpl: (buf, enc) => `<gbk-decoded ${enc} ${buf.length}b>`,
65
+ });
66
+ expect(r.encoding).toBe("gbk");
67
+ expect(r.text).toContain("gbk-decoded");
68
+ });
69
+
70
+ it("throws on non-Buffer input", () => {
71
+ expect(() => decodeBuffer("not-a-buffer")).toThrow(/Buffer/);
72
+ });
73
+ });
74
+
75
+ const SAMPLE_CSV = [
76
+ "支付宝交易记录明细查询",
77
+ "账号:[user@example.com]",
78
+ "起始日期:[2024-04-01 00:00:00] 终止日期:[2024-05-01 00:00:00]",
79
+ "---------------------------------交易记录明细列表------------------------------",
80
+ "交易号,商家订单号,交易创建时间,付款时间,最近修改时间,交易来源地,类型,交易对方,商品名称,金额(元),收/支,交易状态,服务费(元),成功退款(元),备注,资金状态",
81
+ "2024040122001112345678,T20240401XXXX,2024-04-01 09:23:11,2024-04-01 09:23:13,2024-04-01 09:23:13,支付宝网站,即时到账交易,美团,美团外卖订单,38.50,支出,交易成功,0.00,0.00,,已支出",
82
+ "2024040522001112345679,,2024-04-05 14:00:00,2024-04-05 14:00:02,2024-04-05 14:00:02,客户端,转账,张三,生日礼物,500.00,支出,交易成功,0.00,0.00,生日快乐,已支出",
83
+ "2024041022001112345680,REFUND123,2024-04-10 10:00:00,2024-04-10 10:00:05,2024-04-10 10:00:05,支付宝网站,退款,淘宝,运动鞋退款,299.00,收入,退款成功,0.00,299.00,,已收入",
84
+ "---------------------------------交易记录明细列表结束------------------------------",
85
+ "导出时间:[2024-05-02 09:00:00] 用户姓名:[张三]",
86
+ ].join("\n");
87
+
88
+ describe("csv-parser — parseAlipayCsv", () => {
89
+ it("parses header metadata + 3 rows from a valid CSV", () => {
90
+ const r = parseAlipayCsv(SAMPLE_CSV);
91
+ expect(r.header.account).toBe("user@example.com");
92
+ expect(r.header.startDate).toBe("2024-04-01 00:00:00");
93
+ expect(r.header.endDate).toBe("2024-05-01 00:00:00");
94
+ expect(r.rows).toHaveLength(3);
95
+ });
96
+
97
+ it("first row has all 16 fields populated correctly", () => {
98
+ const r = parseAlipayCsv(SAMPLE_CSV);
99
+ const row = r.rows[0];
100
+ expect(row.txId).toBe("2024040122001112345678");
101
+ expect(row.merchantOrderNumber).toBe("T20240401XXXX");
102
+ expect(row.counterparty).toBe("美团");
103
+ expect(row.itemName).toBe("美团外卖订单");
104
+ expect(row.amount).toBe("38.50");
105
+ expect(row.direction).toBe("支出");
106
+ expect(row.status).toBe("交易成功");
107
+ expect(row.fundStatus).toBe("已支出");
108
+ });
109
+
110
+ it("transfer row preserves note", () => {
111
+ const r = parseAlipayCsv(SAMPLE_CSV);
112
+ expect(r.rows[1].counterparty).toBe("张三");
113
+ expect(r.rows[1].note).toBe("生日快乐");
114
+ });
115
+
116
+ it("refund row direction = 收入", () => {
117
+ const r = parseAlipayCsv(SAMPLE_CSV);
118
+ expect(r.rows[2].direction).toBe("收入");
119
+ expect(r.rows[2].refundedAmount).toBe("299.00");
120
+ });
121
+
122
+ it("returns empty rows with warning when header missing", () => {
123
+ const r = parseAlipayCsv("no header here\njust some text");
124
+ expect(r.rows).toEqual([]);
125
+ expect(r.warning).toContain("header row");
126
+ });
127
+
128
+ it("stops at the terminator marker", () => {
129
+ const r = parseAlipayCsv(SAMPLE_CSV);
130
+ // The "导出时间:[...]" trailer line is OUTSIDE the data list — must not be a row
131
+ for (const row of r.rows) {
132
+ expect(row.txId.startsWith("2024")).toBe(true);
133
+ }
134
+ });
135
+
136
+ it("skips rows with too few commas", () => {
137
+ const csv = SAMPLE_CSV.replace(
138
+ "2024040522001112345679,,2024-04-05 14:00:00,2024-04-05 14:00:02,2024-04-05 14:00:02,客户端,转账,张三,生日礼物,500.00,支出,交易成功,0.00,0.00,生日快乐,已支出",
139
+ "garbage,line,with,few,commas",
140
+ );
141
+ const r = parseAlipayCsv(csv);
142
+ expect(r.rows).toHaveLength(2); // the original 3 minus the broken one
143
+ });
144
+
145
+ it("returns empty rows for empty input", () => {
146
+ expect(parseAlipayCsv("")).toEqual({ header: {}, rows: [] });
147
+ expect(parseAlipayCsv(null)).toEqual({ header: {}, rows: [] });
148
+ });
149
+ });
150
+
151
+ describe("csv-parser — parseAlipayCsvBuffer", () => {
152
+ it("decodes UTF-8 + parses end-to-end", () => {
153
+ const buf = Buffer.from(SAMPLE_CSV, "utf-8");
154
+ const r = parseAlipayCsvBuffer(buf);
155
+ expect(r.encoding).toBe("utf-8");
156
+ expect(r.rows).toHaveLength(3);
157
+ });
158
+ });
159
+
160
+ // ─── counterparty classifier ────────────────────────────────────────────
161
+
162
+ describe("counterparty — classifyCounterparty", () => {
163
+ it("recognizes well-known merchants (substring)", () => {
164
+ expect(classifyCounterparty("美团")).toBe("merchant");
165
+ expect(classifyCounterparty("美团外卖")).toBe("merchant");
166
+ expect(classifyCounterparty("淘宝")).toBe("merchant");
167
+ expect(classifyCounterparty("天猫超市")).toBe("merchant");
168
+ expect(classifyCounterparty("12306")).toBe("merchant");
169
+ expect(classifyCounterparty("星巴克咖啡")).toBe("merchant");
170
+ });
171
+
172
+ it("recognizes merchant suffix heuristic", () => {
173
+ expect(classifyCounterparty("北京三联书店")).toBe("merchant");
174
+ expect(classifyCounterparty("XX 科技有限公司")).toBe("merchant");
175
+ expect(classifyCounterparty("华润万家超市")).toBe("merchant");
176
+ expect(classifyCounterparty("普仁医院")).toBe("merchant");
177
+ expect(classifyCounterparty("中通快递")).toBe("merchant");
178
+ });
179
+
180
+ it("classifies 2-4 char Chinese names as contact", () => {
181
+ expect(classifyCounterparty("张三")).toBe("contact");
182
+ expect(classifyCounterparty("李小明")).toBe("contact");
183
+ expect(classifyCounterparty("欧阳娜娜")).toBe("contact");
184
+ });
185
+
186
+ it("strips contact-info brackets before classifying", () => {
187
+ expect(classifyCounterparty("张三(186****1234)")).toBe("contact");
188
+ expect(classifyCounterparty("王五(北京)")).toBe("contact");
189
+ });
190
+
191
+ it("returns unknown for anything that doesn't fit", () => {
192
+ expect(classifyCounterparty("ABC123")).toBe("unknown");
193
+ expect(classifyCounterparty("")).toBe("unknown");
194
+ expect(classifyCounterparty(null)).toBe("unknown");
195
+ });
196
+
197
+ it("KNOWN_MERCHANTS includes >= 80 entries (broad coverage)", () => {
198
+ expect(KNOWN_MERCHANTS.size).toBeGreaterThanOrEqual(80);
199
+ });
200
+ });
201
+
202
+ describe("counterparty — counterpartyToPersonId", () => {
203
+ it("returns same id for same name (idempotent)", () => {
204
+ const a = counterpartyToPersonId("美团");
205
+ const b = counterpartyToPersonId("美团");
206
+ expect(a).toBe(b);
207
+ expect(a.startsWith("person-alipay-")).toBe(true);
208
+ });
209
+
210
+ it("different names → different ids", () => {
211
+ expect(counterpartyToPersonId("淘宝")).not.toBe(counterpartyToPersonId("京东"));
212
+ });
213
+
214
+ it("strips parens before slugifying", () => {
215
+ const a = counterpartyToPersonId("张三(186****1234)");
216
+ const b = counterpartyToPersonId("张三");
217
+ expect(a).toBe(b);
218
+ });
219
+ });
220
+
221
+ describe("normalizeCounterpartyName", () => {
222
+ it("strips parens / asterisks", () => {
223
+ expect(normalizeCounterpartyName("张三(186****1234)")).toBe("张三");
224
+ expect(normalizeCounterpartyName("公司***北京")).toBe("公司北京");
225
+ });
226
+ });
227
+
228
+ // ─── subtype mapping ───────────────────────────────────────────────────
229
+
230
+ describe("mapAlipayTypeToSubtype", () => {
231
+ it("transfer / refund / investment keywords", () => {
232
+ expect(mapAlipayTypeToSubtype("转账给好友", "支出")).toBe("transfer");
233
+ expect(mapAlipayTypeToSubtype("退款", "收入")).toBe("refund");
234
+ expect(mapAlipayTypeToSubtype("余额宝转入", "支出")).toBe("investment");
235
+ expect(mapAlipayTypeToSubtype("理财申购", "支出")).toBe("investment");
236
+ expect(mapAlipayTypeToSubtype("红包", "支出")).toBe("redenvelope");
237
+ expect(mapAlipayTypeToSubtype("缴费", "支出")).toBe("utility");
238
+ expect(mapAlipayTypeToSubtype("交易关闭", "支出")).toBe("cancelled");
239
+ });
240
+
241
+ it("default by direction", () => {
242
+ expect(mapAlipayTypeToSubtype("即时到账交易", "支出")).toBe("payment");
243
+ expect(mapAlipayTypeToSubtype("收款", "收入")).toBe("income");
244
+ });
245
+ });
246
+
247
+ // ─── parseAlipayDateTime ────────────────────────────────────────────────
248
+
249
+ describe("parseAlipayDateTime", () => {
250
+ it("parses 'YYYY-MM-DD HH:MM:SS' to ms epoch", () => {
251
+ const ms = parseAlipayDateTime("2024-04-01 09:23:13");
252
+ const d = new Date(ms);
253
+ expect(d.getFullYear()).toBe(2024);
254
+ expect(d.getMonth()).toBe(3); // April
255
+ expect(d.getDate()).toBe(1);
256
+ });
257
+
258
+ it("returns null on bad input", () => {
259
+ expect(parseAlipayDateTime("")).toBeNull();
260
+ expect(parseAlipayDateTime(null)).toBeNull();
261
+ expect(parseAlipayDateTime("garbage")).toBeNull();
262
+ });
263
+ });
264
+
265
+ // ─── AlipayBillAdapter — contract + sync + normalize ───────────────────
266
+
267
+ describe("AlipayBillAdapter contract", () => {
268
+ const a = new AlipayBillAdapter({
269
+ account: { email: "u@example.com" },
270
+ });
271
+
272
+ it("conforms to PersonalDataAdapter spec", () => {
273
+ const r = assertAdapter(a);
274
+ expect(r.ok).toBe(true);
275
+ if (!r.ok) console.log(r.errors);
276
+ });
277
+
278
+ it("name + version + capabilities + sensitivity", () => {
279
+ expect(a.name).toBe("alipay-bill");
280
+ expect(a.version).toBe("0.1.0");
281
+ expect(a.capabilities).toContain("import:csv-zip");
282
+ expect(a.dataDisclosure.sensitivity).toBe("high");
283
+ });
284
+
285
+ it("authenticate returns ok:true (no server auth)", async () => {
286
+ const r = await a.authenticate();
287
+ expect(r.ok).toBe(true);
288
+ expect(r.account).toBe("u@example.com");
289
+ });
290
+
291
+ it("healthCheck returns ok:true", async () => {
292
+ const r = await a.healthCheck();
293
+ expect(r.ok).toBe(true);
294
+ });
295
+
296
+ it("rejects missing account", () => {
297
+ expect(() => new AlipayBillAdapter()).toThrow();
298
+ expect(() => new AlipayBillAdapter({})).toThrow(/account/);
299
+ expect(() => new AlipayBillAdapter({ account: {} })).toThrow(/email/);
300
+ });
301
+ });
302
+
303
+ describe("AlipayBillAdapter.sync", () => {
304
+ it("returns 0 events when no zipPath/csvPath given (idle)", async () => {
305
+ const a = new AlipayBillAdapter({ account: { email: "u@example.com" } });
306
+ const raws = [];
307
+ for await (const r of a.sync()) raws.push(r);
308
+ expect(raws).toHaveLength(0);
309
+ });
310
+
311
+ it("yields one raw per row when csvPath provided (mocked parser)", async () => {
312
+ // Write a temp CSV file
313
+ const fs = require("node:fs");
314
+ const os = require("node:os");
315
+ const path = require("node:path");
316
+ const tmp = path.join(os.tmpdir(), `alipay-test-${Date.now()}.csv`);
317
+ fs.writeFileSync(tmp, SAMPLE_CSV, "utf-8");
318
+
319
+ const events = [];
320
+ const a = new AlipayBillAdapter({
321
+ account: { email: "u@example.com" },
322
+ // Use real parser; CSV is a valid Alipay export shape
323
+ });
324
+ const raws = [];
325
+ for await (const r of a.sync({
326
+ csvPath: tmp,
327
+ onProgress: (e) => events.push(e.phase),
328
+ })) raws.push(r);
329
+
330
+ expect(raws).toHaveLength(3);
331
+ expect(raws[0].adapter).toBe("alipay-bill");
332
+ expect(raws[0].originalId).toBe("2024040122001112345678");
333
+ expect(events).toContain("opening");
334
+ expect(events).toContain("parsing");
335
+ expect(events).toContain("parsed");
336
+ expect(events).toContain("done");
337
+ expect(events.filter((p) => p === "row")).toHaveLength(3);
338
+
339
+ fs.unlinkSync(tmp);
340
+ });
341
+
342
+ it("uses injected zipExtractor when zipPath provided", async () => {
343
+ const events = [];
344
+ const a = new AlipayBillAdapter({
345
+ account: { email: "u@example.com" },
346
+ zipPassword: "OPENME-mock",
347
+ zipExtractor: async (zipPath, opts) => {
348
+ events.push({ kind: "zip", zipPath, password: opts.password });
349
+ return { buffer: Buffer.from(SAMPLE_CSV, "utf-8"), filename: "test.csv" };
350
+ },
351
+ });
352
+ const raws = [];
353
+ for await (const r of a.sync({ zipPath: "/fake/path.zip" })) raws.push(r);
354
+ expect(raws).toHaveLength(3);
355
+ expect(events[0].password).toBe("OPENME-mock");
356
+ });
357
+ });
358
+
359
+ describe("AlipayBillAdapter.normalize", () => {
360
+ const a = new AlipayBillAdapter({ account: { email: "u@example.com" } });
361
+
362
+ it("payment event has correct subtype + amount.direction=out", () => {
363
+ const raw = {
364
+ adapter: "alipay-bill",
365
+ originalId: "TX1",
366
+ capturedAt: Date.now(),
367
+ payload: {
368
+ row: {
369
+ txId: "TX1",
370
+ merchantOrderNumber: "MO1",
371
+ createdAt: "2024-04-01 10:00:00",
372
+ paidAt: "2024-04-01 10:00:02",
373
+ lastModifiedAt: "2024-04-01 10:00:02",
374
+ sourceChannel: "支付宝网站",
375
+ alipayType: "即时到账交易",
376
+ counterparty: "美团",
377
+ itemName: "美团外卖订单",
378
+ amount: "38.50",
379
+ direction: "支出",
380
+ status: "交易成功",
381
+ serviceFee: "0.00",
382
+ refundedAmount: "0.00",
383
+ note: "",
384
+ fundStatus: "已支出",
385
+ },
386
+ accountEmail: "u@example.com",
387
+ },
388
+ };
389
+ const batch = a.normalize(raw);
390
+ expect(batch.events).toHaveLength(1);
391
+ const ev = batch.events[0];
392
+ expect(ev.subtype).toBe("payment");
393
+ expect(ev.content.amount.value).toBe(38.5);
394
+ expect(ev.content.amount.direction).toBe("out");
395
+ expect(ev.actor).toBe("person-self");
396
+ expect(ev.source.adapter).toBe("alipay-bill");
397
+ expect(ev.source.originalId).toBe("TX1");
398
+ expect(ev.extra.merchantOrderNumber).toBe("MO1");
399
+ expect(ev.extra.alipayType).toBe("即时到账交易");
400
+ expect(ev.extra.counterpartyKind).toBe("merchant");
401
+ });
402
+
403
+ it("transfer creates contact person", () => {
404
+ const raw = {
405
+ adapter: "alipay-bill",
406
+ originalId: "TX2",
407
+ capturedAt: Date.now(),
408
+ payload: {
409
+ row: {
410
+ txId: "TX2",
411
+ merchantOrderNumber: "",
412
+ createdAt: "2024-04-05 14:00:00",
413
+ paidAt: "2024-04-05 14:00:02",
414
+ lastModifiedAt: "2024-04-05 14:00:02",
415
+ sourceChannel: "客户端",
416
+ alipayType: "转账",
417
+ counterparty: "张三",
418
+ itemName: "生日礼物",
419
+ amount: "500.00",
420
+ direction: "支出",
421
+ status: "交易成功",
422
+ serviceFee: "0.00",
423
+ refundedAmount: "0.00",
424
+ note: "生日快乐",
425
+ fundStatus: "已支出",
426
+ },
427
+ accountEmail: "u@example.com",
428
+ },
429
+ };
430
+ const batch = a.normalize(raw);
431
+ expect(batch.events[0].subtype).toBe("transfer");
432
+ expect(batch.events[0].content.text).toBe("生日快乐");
433
+ expect(batch.persons).toHaveLength(1);
434
+ expect(batch.persons[0].subtype).toBe("contact");
435
+ expect(batch.persons[0].names).toContain("张三");
436
+ });
437
+
438
+ it("refund flips direction to in", () => {
439
+ const raw = {
440
+ adapter: "alipay-bill",
441
+ originalId: "TX3",
442
+ capturedAt: Date.now(),
443
+ payload: {
444
+ row: {
445
+ txId: "TX3", merchantOrderNumber: "REFUND123",
446
+ createdAt: "2024-04-10 10:00:00", paidAt: "2024-04-10 10:00:05",
447
+ lastModifiedAt: "2024-04-10 10:00:05",
448
+ sourceChannel: "支付宝网站", alipayType: "退款",
449
+ counterparty: "淘宝", itemName: "运动鞋退款", amount: "299.00",
450
+ direction: "收入", status: "退款成功",
451
+ serviceFee: "0.00", refundedAmount: "299.00",
452
+ note: "", fundStatus: "已收入",
453
+ },
454
+ accountEmail: "u@example.com",
455
+ },
456
+ };
457
+ const batch = a.normalize(raw);
458
+ expect(batch.events[0].subtype).toBe("refund");
459
+ expect(batch.events[0].content.amount.direction).toBe("in");
460
+ expect(batch.events[0].extra.refundedAmount).toBe(299);
461
+ });
462
+
463
+ it("cancelled transactions get subtype=cancelled", () => {
464
+ const raw = {
465
+ adapter: "alipay-bill", originalId: "TX4",
466
+ capturedAt: Date.now(),
467
+ payload: {
468
+ row: {
469
+ txId: "TX4", merchantOrderNumber: "",
470
+ createdAt: "2024-04-15 12:00:00", paidAt: "2024-04-15 12:00:00",
471
+ lastModifiedAt: "2024-04-15 12:00:00",
472
+ sourceChannel: "支付宝网站", alipayType: "即时到账交易",
473
+ counterparty: "测试商家", itemName: "test", amount: "100.00",
474
+ direction: "支出", status: "交易关闭",
475
+ serviceFee: "0.00", refundedAmount: "0.00",
476
+ note: "", fundStatus: "冻结",
477
+ },
478
+ accountEmail: "u@example.com",
479
+ },
480
+ };
481
+ const batch = a.normalize(raw);
482
+ expect(batch.events[0].subtype).toBe("cancelled");
483
+ });
484
+
485
+ it("unknown counterparty stamps needsResolve=true", () => {
486
+ const raw = {
487
+ adapter: "alipay-bill", originalId: "TX5",
488
+ capturedAt: Date.now(),
489
+ payload: {
490
+ row: {
491
+ txId: "TX5", merchantOrderNumber: "",
492
+ createdAt: "2024-04-20 09:00:00", paidAt: "2024-04-20 09:00:00",
493
+ lastModifiedAt: "2024-04-20 09:00:00",
494
+ sourceChannel: "支付宝网站", alipayType: "即时到账交易",
495
+ counterparty: "ABC123XYZ", itemName: "unclassifiable", amount: "10.00",
496
+ direction: "支出", status: "交易成功",
497
+ serviceFee: "0.00", refundedAmount: "0.00",
498
+ note: "", fundStatus: "已支出",
499
+ },
500
+ accountEmail: "u@example.com",
501
+ },
502
+ };
503
+ const batch = a.normalize(raw);
504
+ expect(batch.events[0].extra.counterpartyKind).toBe("unknown");
505
+ expect(batch.events[0].extra.needsResolve).toBe(true);
506
+ expect(batch.persons[0].extra.needsResolve).toBe(true);
507
+ });
508
+
509
+ it("creates an Item when itemName is distinct from alipayType", () => {
510
+ const raw = {
511
+ adapter: "alipay-bill", originalId: "TX6",
512
+ capturedAt: Date.now(),
513
+ payload: {
514
+ row: {
515
+ txId: "TX6", merchantOrderNumber: "MO6",
516
+ createdAt: "2024-04-25 09:00:00", paidAt: "2024-04-25 09:00:00",
517
+ lastModifiedAt: "2024-04-25 09:00:00",
518
+ sourceChannel: "支付宝网站", alipayType: "即时到账交易",
519
+ counterparty: "京东", itemName: "iPhone 17 Pro 256GB", amount: "9999.00",
520
+ direction: "支出", status: "交易成功",
521
+ serviceFee: "0.00", refundedAmount: "0.00",
522
+ note: "", fundStatus: "已支出",
523
+ },
524
+ accountEmail: "u@example.com",
525
+ },
526
+ };
527
+ const batch = a.normalize(raw);
528
+ expect(batch.items).toHaveLength(1);
529
+ expect(batch.items[0].name).toBe("iPhone 17 Pro 256GB");
530
+ expect(batch.items[0].price.value).toBe(9999);
531
+ });
532
+
533
+ it("rejects missing raw.payload", () => {
534
+ expect(() => a.normalize(null)).toThrow();
535
+ expect(() => a.normalize({})).toThrow();
536
+ expect(() => a.normalize({ payload: {} })).toThrow(/row/);
537
+ });
538
+ });
@@ -100,10 +100,11 @@ describe("EmailAdapter contract", () => {
100
100
  sessionFactory: makeMockSession({}).factory,
101
101
  });
102
102
  expect(a.name).toBe("email-imap");
103
- expect(a.version).toBe("0.2.0"); // Phase 5.2body parsing added
103
+ expect(a.version).toBe("0.6.0"); // Phase 5.7retry + progress streaming
104
104
  expect(a.capabilities).toContain("sync:imap");
105
105
  expect(a.capabilities).toContain("auth:authcode");
106
106
  expect(a.capabilities).toContain("parse:mime-body");
107
+ expect(a.capabilities).toContain("classify:layer1-rules");
107
108
  expect(a.dataDisclosure.sensitivity).toBe("high");
108
109
  });
109
110
 
@@ -585,6 +586,142 @@ describe("EmailAdapter — body parsing (Phase 5.2)", () => {
585
586
  });
586
587
  });
587
588
 
589
+ // ─── Phase 5.3: classification integration ─────────────────────────────
590
+
591
+ describe("EmailAdapter — classification (Phase 5.3)", () => {
592
+ function bankEnv(uid = 1) {
593
+ return env(uid, {
594
+ from: [{ address: "ebank@cmbchina.com" }],
595
+ subject: "招商银行 11 月对账单",
596
+ source: Buffer.from("RAW", "utf8"),
597
+ });
598
+ }
599
+
600
+ it("sync attaches classification to payload (Layer 1 short-circuit)", async () => {
601
+ const { factory } = makeMockSession({
602
+ mailboxes: { INBOX: { uidValidity: 1, envelopes: [bankEnv()] } },
603
+ });
604
+ const a = new EmailAdapter({
605
+ account: { provider: "qq", email: "u@qq.com", authCode: "x", folders: ["INBOX"] },
606
+ sessionFactory: factory,
607
+ parser: async () => ({ textBody: "stmt", attachments: [] }),
608
+ });
609
+ const raws = [];
610
+ for await (const r of a.sync()) raws.push(r);
611
+ expect(raws[0].payload.classification).toBeDefined();
612
+ expect(raws[0].payload.classification.category).toBe("bill_bank");
613
+ expect(raws[0].payload.classification.layer).toBe("L1");
614
+ });
615
+
616
+ it("normalize copies classification into extra.classified + .classification", () => {
617
+ const a = new EmailAdapter({
618
+ account: { provider: "qq", email: "u@qq.com", authCode: "x" },
619
+ sessionFactory: makeMockSession({}).factory,
620
+ parser: async () => ({}),
621
+ });
622
+ const raw = {
623
+ adapter: "email-imap",
624
+ originalId: "<m@x>",
625
+ capturedAt: 0,
626
+ payload: {
627
+ ...env(1, { from: [{ address: "x@cmbchina.com" }], subject: "招商银行账单" }),
628
+ folder: "INBOX",
629
+ classification: {
630
+ category: "bill_bank",
631
+ confidence: 0.95,
632
+ layer: "L1",
633
+ ruleName: "bill_bank.cn-bank-major",
634
+ },
635
+ },
636
+ };
637
+ const batch = a.normalize(raw);
638
+ expect(batch.events[0].extra.classified).toBe("bill_bank");
639
+ expect(batch.events[0].extra.classification.category).toBe("bill_bank");
640
+ expect(batch.events[0].extra.classification.layer).toBe("L1");
641
+ expect(batch.events[0].extra.classification.ruleName).toContain("bill_bank");
642
+ });
643
+
644
+ it("ambiguous email triggers Layer 2 when LLM is provided", async () => {
645
+ const { MockLLMClient } = require("../../lib/llm-client");
646
+ const llm = new MockLLMClient({
647
+ reply: '{"category":"register","confidence":0.85,"reason":"verification code"}',
648
+ });
649
+ const { factory } = makeMockSession({
650
+ mailboxes: {
651
+ INBOX: {
652
+ uidValidity: 1,
653
+ envelopes: [env(2, {
654
+ from: [{ address: "noreply@unknown-service.example" }],
655
+ subject: "Welcome",
656
+ source: Buffer.from("RAW", "utf8"),
657
+ })],
658
+ },
659
+ },
660
+ });
661
+ const a = new EmailAdapter({
662
+ account: { provider: "qq", email: "u@qq.com", authCode: "x", folders: ["INBOX"] },
663
+ sessionFactory: factory,
664
+ parser: async () => ({ textBody: "Welcome aboard, here is your verification link." }),
665
+ llm,
666
+ });
667
+ const raws = [];
668
+ for await (const r of a.sync()) raws.push(r);
669
+ // Layer 1 likely returned 'register' at ~0.75 (welcome rule) — falls to Layer 2
670
+ expect(raws[0].payload.classification.category).toBe("register");
671
+ // Either L2 fired (if L1 conf < 0.85) or L1 stuck.
672
+ expect(["L1", "L2"]).toContain(raws[0].payload.classification.layer);
673
+ });
674
+
675
+ it("classifier error inside sync degrades to OTHER (doesn't abort sync)", async () => {
676
+ const { factory } = makeMockSession({
677
+ mailboxes: { INBOX: { uidValidity: 1, envelopes: [bankEnv()] } },
678
+ });
679
+ const a = new EmailAdapter({
680
+ account: { provider: "qq", email: "u@qq.com", authCode: "x", folders: ["INBOX"] },
681
+ sessionFactory: factory,
682
+ parser: async () => ({}),
683
+ classifier: async () => { throw new Error("classifier exploded"); },
684
+ });
685
+ const raws = [];
686
+ for await (const r of a.sync()) raws.push(r);
687
+ expect(raws).toHaveLength(1);
688
+ expect(raws[0].payload.classification.category).toBe("other");
689
+ expect(raws[0].payload.classification.error).toContain("classifier exploded");
690
+ });
691
+
692
+ it("disableClassification skips both layers entirely", async () => {
693
+ const { factory } = makeMockSession({
694
+ mailboxes: { INBOX: { uidValidity: 1, envelopes: [bankEnv()] } },
695
+ });
696
+ const a = new EmailAdapter({
697
+ account: { provider: "qq", email: "u@qq.com", authCode: "x", folders: ["INBOX"] },
698
+ sessionFactory: factory,
699
+ parser: async () => ({}),
700
+ disableClassification: true,
701
+ });
702
+ const raws = [];
703
+ for await (const r of a.sync()) raws.push(r);
704
+ expect(raws[0].payload.classification).toBeUndefined();
705
+ });
706
+
707
+ it("capabilities advertise classifier surface", () => {
708
+ const a = new EmailAdapter({
709
+ account: { provider: "qq", email: "u@qq.com", authCode: "x" },
710
+ sessionFactory: makeMockSession({}).factory,
711
+ });
712
+ expect(a.capabilities).toContain("classify:layer1-rules");
713
+ expect(a.capabilities).not.toContain("classify:layer2-llm"); // no LLM provided
714
+
715
+ const { MockLLMClient } = require("../../lib/llm-client");
716
+ const b = new EmailAdapter({
717
+ account: { provider: "qq", email: "u@qq.com", authCode: "x" },
718
+ sessionFactory: makeMockSession({}).factory,
719
+ llm: new MockLLMClient({ reply: "{}" }),
720
+ });
721
+ expect(b.capabilities).toContain("classify:layer2-llm");
722
+ });
723
+ });
724
+
588
725
  describe("parseWatermark / formatWatermark", () => {
589
726
  it("parses well-formed strings", () => {
590
727
  expect(parseWatermark("42:100")).toEqual({ uidValidity: "42", lastUid: 100 });