@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,742 +0,0 @@
1
- "use strict";
2
-
3
- import { describe, it, expect } from "vitest";
4
-
5
- const {
6
- EmailAdapter,
7
- parseWatermark,
8
- formatWatermark,
9
- } = require("../../lib/adapters/email-imap/email-adapter");
10
- const { assertAdapter } = require("../../lib/adapter-spec");
11
- const { validateBatch } = require("../../lib/batch");
12
-
13
- function makeMockSession(spec = {}) {
14
- const recorder = {
15
- constructorArgs: null,
16
- connectCalls: 0,
17
- closedCalls: 0,
18
- openedMailboxes: [],
19
- fetchRanges: [],
20
- };
21
- const factory = (opts) => {
22
- recorder.constructorArgs = opts;
23
- let openMb = null;
24
- return {
25
- async connect() {
26
- recorder.connectCalls += 1;
27
- if (spec.connectThrows) throw spec.connectThrows;
28
- },
29
- async openMailbox(name) {
30
- recorder.openedMailboxes.push(name);
31
- const mb = spec.mailboxes && spec.mailboxes[name];
32
- if (!mb) {
33
- const err = new Error(`Mailbox doesn't exist: ${name}`);
34
- err.code = "MAILBOX_NOT_FOUND";
35
- throw err;
36
- }
37
- openMb = { name, ...mb };
38
- return {
39
- uidValidity: mb.uidValidity,
40
- uidNext: mb.uidNext || 9999,
41
- exists: (mb.envelopes || []).length,
42
- };
43
- },
44
- async *fetchEnvelopesSince(sinceUid = 0) {
45
- recorder.fetchRanges.push({ mailbox: openMb && openMb.name, sinceUid, mode: "envelope" });
46
- if (!openMb) return;
47
- for (const env of openMb.envelopes || []) {
48
- if (env.uid > sinceUid) yield env;
49
- }
50
- },
51
- // Phase 5.2 — adapter.sync now calls fetchFullSince. The mock
52
- // emits the same envelopes with an empty source Buffer; the
53
- // adapter parses it via its injected parser (or skips parsing
54
- // when source is empty).
55
- async *fetchFullSince(sinceUid = 0) {
56
- recorder.fetchRanges.push({ mailbox: openMb && openMb.name, sinceUid, mode: "full" });
57
- if (!openMb) return;
58
- for (const env of openMb.envelopes || []) {
59
- if (env.uid > sinceUid) {
60
- yield { ...env, source: env.source || Buffer.alloc(0) };
61
- }
62
- }
63
- },
64
- async close() {
65
- recorder.closedCalls += 1;
66
- },
67
- };
68
- };
69
- return { factory, recorder };
70
- }
71
-
72
- const env = (uid, overrides = {}) => ({
73
- uid,
74
- internalDate: new Date(`2026-04-${String(uid % 30).padStart(2, "0")}T10:00:00Z`),
75
- flags: ["\\Seen"],
76
- messageId: `<msg-${uid}@example.com>`,
77
- subject: `Subject ${uid}`,
78
- from: [{ name: "Alice", address: `alice${uid}@example.com` }],
79
- to: [{ name: "Me", address: "me@example.com" }],
80
- cc: [],
81
- date: new Date(`2026-04-${String(uid % 30).padStart(2, "0")}T10:00:00Z`),
82
- size: 1024,
83
- ...overrides,
84
- });
85
-
86
- describe("EmailAdapter contract", () => {
87
- it("conforms to PersonalDataAdapter spec", () => {
88
- const a = new EmailAdapter({
89
- account: { provider: "qq", email: "u@qq.com", authCode: "abc123" },
90
- sessionFactory: makeMockSession({}).factory,
91
- });
92
- const r = assertAdapter(a);
93
- expect(r.ok).toBe(true);
94
- if (!r.ok) console.log(r.errors);
95
- });
96
-
97
- it("exposes the canonical name + version + capabilities", () => {
98
- const a = new EmailAdapter({
99
- account: { provider: "qq", email: "u@qq.com", authCode: "x" },
100
- sessionFactory: makeMockSession({}).factory,
101
- });
102
- expect(a.name).toBe("email-imap");
103
- expect(a.version).toBe("0.7.0"); // Phase 5.8 — snapshot mode for Android in-APK IMAP fetch
104
- expect(a.capabilities).toContain("sync:imap");
105
- expect(a.capabilities).toContain("auth:authcode");
106
- expect(a.capabilities).toContain("parse:mime-body");
107
- expect(a.capabilities).toContain("classify:layer1-rules");
108
- expect(a.dataDisclosure.sensitivity).toBe("high");
109
- });
110
-
111
- it("rejects missing or malformed account", () => {
112
- expect(() => new EmailAdapter()).toThrow();
113
- expect(() => new EmailAdapter({})).toThrow(/account/);
114
- expect(() => new EmailAdapter({ account: {} })).toThrow(/email/);
115
- expect(() => new EmailAdapter({ account: { email: "noatsign" } })).toThrow(/email/);
116
- expect(() => new EmailAdapter({ account: { email: "u@x.com" } })).toThrow(/authCode/);
117
- });
118
- });
119
-
120
- describe("EmailAdapter.authenticate", () => {
121
- it("returns ok:true when connect succeeds", async () => {
122
- const { factory, recorder } = makeMockSession({});
123
- const a = new EmailAdapter({
124
- account: { provider: "qq", email: "u@qq.com", authCode: "x" },
125
- sessionFactory: factory,
126
- });
127
- const r = await a.authenticate();
128
- expect(r.ok).toBe(true);
129
- expect(r.account).toBe("u@qq.com");
130
- expect(r.provider).toBe("qq");
131
- expect(recorder.connectCalls).toBe(1);
132
- expect(recorder.closedCalls).toBe(1);
133
- });
134
-
135
- it("returns ok:false reason=AUTH_FAILED on credential error", async () => {
136
- const { ImapAuthFailedError } = require("../../lib/adapters/email-imap/imap-session");
137
- const { factory } = makeMockSession({
138
- connectThrows: new ImapAuthFailedError("bad pass"),
139
- });
140
- const a = new EmailAdapter({
141
- account: { provider: "qq", email: "u@qq.com", authCode: "wrong" },
142
- sessionFactory: factory,
143
- });
144
- const r = await a.authenticate();
145
- expect(r.ok).toBe(false);
146
- expect(r.reason).toBe("AUTH_FAILED");
147
- });
148
-
149
- it("returns ok:false reason=CONNECTION_FAILED on network error", async () => {
150
- const { ImapConnectionFailedError } = require("../../lib/adapters/email-imap/imap-session");
151
- const { factory } = makeMockSession({
152
- connectThrows: new ImapConnectionFailedError("ECONNREFUSED"),
153
- });
154
- const a = new EmailAdapter({
155
- account: { provider: "qq", email: "u@qq.com", authCode: "x" },
156
- sessionFactory: factory,
157
- });
158
- const r = await a.authenticate();
159
- expect(r.ok).toBe(false);
160
- expect(r.reason).toBe("CONNECTION_FAILED");
161
- });
162
- });
163
-
164
- describe("EmailAdapter.healthCheck", () => {
165
- it("matches the registry's expected shape on success", async () => {
166
- const { factory } = makeMockSession({});
167
- const a = new EmailAdapter({
168
- account: { provider: "qq", email: "u@qq.com", authCode: "x" },
169
- sessionFactory: factory,
170
- });
171
- const r = await a.healthCheck();
172
- expect(r.ok).toBe(true);
173
- expect(r.lastChecked).toBeGreaterThan(0);
174
- });
175
-
176
- it("matches the registry's expected shape on failure", async () => {
177
- const { factory } = makeMockSession({
178
- connectThrows: new Error("Authentication invalid"),
179
- });
180
- const a = new EmailAdapter({
181
- account: { provider: "qq", email: "u@qq.com", authCode: "x" },
182
- sessionFactory: factory,
183
- });
184
- const r = await a.healthCheck();
185
- expect(r.ok).toBe(false);
186
- expect(r.reason).toBeDefined();
187
- });
188
- });
189
-
190
- describe("EmailAdapter.sync", () => {
191
- it("yields one RawEvent per envelope across all default folders", async () => {
192
- const { factory, recorder } = makeMockSession({
193
- mailboxes: {
194
- INBOX: { uidValidity: 1, envelopes: [env(1), env(2), env(3)] },
195
- "Sent Messages": { uidValidity: 1, envelopes: [env(10), env(11)] },
196
- },
197
- });
198
- const a = new EmailAdapter({
199
- account: { provider: "qq", email: "u@qq.com", authCode: "x" },
200
- sessionFactory: factory,
201
- });
202
- const raws = [];
203
- for await (const r of a.sync()) raws.push(r);
204
- expect(raws).toHaveLength(5);
205
- expect(raws.every((r) => r.adapter === "email-imap")).toBe(true);
206
- expect(raws.every((r) => r.payload.uid > 0)).toBe(true);
207
- expect(recorder.openedMailboxes).toEqual(["INBOX", "Sent Messages"]);
208
- });
209
-
210
- it("originalId uses Message-ID when present", async () => {
211
- const { factory } = makeMockSession({
212
- mailboxes: { INBOX: { uidValidity: 1, envelopes: [env(1)] } },
213
- });
214
- const a = new EmailAdapter({
215
- account: { provider: "qq", email: "u@qq.com", authCode: "x", folders: ["INBOX"] },
216
- sessionFactory: factory,
217
- });
218
- const raws = [];
219
- for await (const r of a.sync()) raws.push(r);
220
- expect(raws[0].originalId).toBe("<msg-1@example.com>");
221
- });
222
-
223
- it("falls back to synthetic originalId when Message-ID missing", async () => {
224
- const { factory } = makeMockSession({
225
- mailboxes: {
226
- INBOX: { uidValidity: 1, envelopes: [env(7, { messageId: "" })] },
227
- },
228
- });
229
- const a = new EmailAdapter({
230
- account: { provider: "qq", email: "u@qq.com", authCode: "x", folders: ["INBOX"] },
231
- sessionFactory: factory,
232
- });
233
- const raws = [];
234
- for await (const r of a.sync()) raws.push(r);
235
- expect(raws[0].originalId).toContain("mid-fallback");
236
- expect(raws[0].originalId).toContain("INBOX");
237
- expect(raws[0].originalId).toContain(":7");
238
- });
239
-
240
- it("respects sinceWatermark (UID > lastUid)", async () => {
241
- const { factory } = makeMockSession({
242
- mailboxes: {
243
- INBOX: { uidValidity: 1, envelopes: [env(1), env(2), env(3), env(4)] },
244
- },
245
- });
246
- const a = new EmailAdapter({
247
- account: { provider: "qq", email: "u@qq.com", authCode: "x", folders: ["INBOX"] },
248
- sessionFactory: factory,
249
- });
250
- const raws = [];
251
- for await (const r of a.sync({ sinceWatermark: "1:2" })) raws.push(r);
252
- expect(raws.map((r) => r.payload.uid)).toEqual([3, 4]);
253
- });
254
-
255
- it("resets sinceUid to 0 when UIDVALIDITY changed (full re-scan)", async () => {
256
- const { factory } = makeMockSession({
257
- mailboxes: {
258
- INBOX: { uidValidity: 99, envelopes: [env(1), env(2)] },
259
- },
260
- });
261
- const a = new EmailAdapter({
262
- account: { provider: "qq", email: "u@qq.com", authCode: "x", folders: ["INBOX"] },
263
- sessionFactory: factory,
264
- });
265
- const raws = [];
266
- for await (const r of a.sync({ sinceWatermark: "42:100" })) raws.push(r);
267
- expect(raws).toHaveLength(2);
268
- expect(raws.map((r) => r.payload.uid).sort()).toEqual([1, 2]);
269
- });
270
-
271
- it("respects maxPerFolder cap", async () => {
272
- const big = Array.from({ length: 50 }, (_, i) => env(i + 1));
273
- const { factory } = makeMockSession({
274
- mailboxes: { INBOX: { uidValidity: 1, envelopes: big } },
275
- });
276
- const a = new EmailAdapter({
277
- account: { provider: "qq", email: "u@qq.com", authCode: "x", folders: ["INBOX"] },
278
- sessionFactory: factory,
279
- });
280
- const raws = [];
281
- for await (const r of a.sync({ maxPerFolder: 7 })) raws.push(r);
282
- expect(raws).toHaveLength(7);
283
- });
284
-
285
- it("closes session even when sync throws mid-stream", async () => {
286
- let closed = false;
287
- const exploding = {
288
- async connect() {},
289
- async openMailbox() {
290
- return { uidValidity: 1, uidNext: 100, exists: 0 };
291
- },
292
- async *fetchFullSince() {
293
- yield { ...env(1), source: Buffer.alloc(0) };
294
- throw new Error("network drop");
295
- },
296
- async close() {
297
- closed = true;
298
- },
299
- };
300
- const factory = () => exploding;
301
- const a = new EmailAdapter({
302
- account: { provider: "qq", email: "u@qq.com", authCode: "x", folders: ["INBOX"] },
303
- sessionFactory: factory,
304
- });
305
- const raws = [];
306
- await expect(async () => {
307
- for await (const r of a.sync()) raws.push(r);
308
- }).rejects.toThrow(/network drop/);
309
- expect(closed).toBe(true);
310
- expect(raws).toHaveLength(1);
311
- });
312
- });
313
-
314
- describe("EmailAdapter.normalize", () => {
315
- it("produces a valid UnifiedSchema batch (1 Event + 1 Person per email)", () => {
316
- const a = new EmailAdapter({
317
- account: { provider: "qq", email: "u@qq.com", authCode: "x" },
318
- sessionFactory: makeMockSession({}).factory,
319
- });
320
- const raw = {
321
- adapter: "email-imap",
322
- originalId: "<msg-1@example.com>",
323
- capturedAt: 1700000000000,
324
- payload: { ...env(1), folder: "INBOX" },
325
- };
326
- const batch = a.normalize(raw);
327
- expect(batch.events).toHaveLength(1);
328
- expect(batch.persons).toHaveLength(1);
329
- expect(batch.places).toEqual([]);
330
- expect(batch.items).toEqual([]);
331
-
332
- const ev = batch.events[0];
333
- expect(ev.type).toBe("event");
334
- expect(ev.subtype).toBe("message");
335
- expect(ev.content.title).toBe("Subject 1");
336
- expect(ev.content.text).toContain("alice1@example.com");
337
- expect(ev.extra.emailFolder).toBe("INBOX");
338
- expect(ev.extra.uid).toBe(1);
339
- expect(ev.source.adapter).toBe("email-imap");
340
-
341
- const p = batch.persons[0];
342
- expect(p.id).toBe("person-email-alice1@example.com");
343
- expect(p.identifiers.email).toEqual(["alice1@example.com"]);
344
- expect(p.names).toEqual(["Alice"]);
345
-
346
- const valid = validateBatch(batch);
347
- expect(valid.valid).toBe(true);
348
- if (!valid.valid) console.log(valid.errors);
349
- });
350
-
351
- it("handles missing subject + missing from gracefully", () => {
352
- const a = new EmailAdapter({
353
- account: { provider: "qq", email: "u@qq.com", authCode: "x" },
354
- sessionFactory: makeMockSession({}).factory,
355
- });
356
- const raw = {
357
- adapter: "email-imap",
358
- originalId: "<m@x>",
359
- capturedAt: 0,
360
- payload: { ...env(2, { subject: "", from: [] }), folder: "INBOX" },
361
- };
362
- const batch = a.normalize(raw);
363
- expect(batch.events[0].content.title).toBe("(no subject)");
364
- expect(batch.persons).toHaveLength(0);
365
- expect(batch.events[0].actor).toBe("person-self");
366
- const v = validateBatch(batch);
367
- expect(v.valid).toBe(true);
368
- });
369
-
370
- it("dedups same sender across multiple emails (stable person id)", () => {
371
- const a = new EmailAdapter({
372
- account: { provider: "qq", email: "u@qq.com", authCode: "x" },
373
- sessionFactory: makeMockSession({}).factory,
374
- });
375
- const senderEnv = (uid) => env(uid, {
376
- from: [{ name: "Bob", address: "bob@example.com" }],
377
- messageId: `<m-${uid}@x>`,
378
- });
379
- const b1 = a.normalize({ adapter: "email-imap", originalId: "<m-1@x>", capturedAt: 0, payload: { ...senderEnv(1), folder: "INBOX" } });
380
- const b2 = a.normalize({ adapter: "email-imap", originalId: "<m-2@x>", capturedAt: 0, payload: { ...senderEnv(2), folder: "INBOX" } });
381
- expect(b1.persons[0].id).toBe(b2.persons[0].id);
382
- expect(b1.persons[0].id).toBe("person-email-bob@example.com");
383
- });
384
-
385
- it("rejects missing raw or payload", () => {
386
- const a = new EmailAdapter({
387
- account: { provider: "qq", email: "u@qq.com", authCode: "x" },
388
- sessionFactory: makeMockSession({}).factory,
389
- });
390
- expect(() => a.normalize()).toThrow();
391
- expect(() => a.normalize({})).toThrow(/payload/);
392
- });
393
- });
394
-
395
- // ─── Phase 5.2: body parsing integration ───────────────────────────────
396
-
397
- describe("EmailAdapter — body parsing (Phase 5.2)", () => {
398
- it("sync injects parsedBody into payload when parser succeeds", async () => {
399
- const { factory } = makeMockSession({
400
- mailboxes: {
401
- INBOX: {
402
- uidValidity: 1,
403
- envelopes: [{
404
- ...env(1),
405
- source: Buffer.from("RAW BYTES", "utf8"),
406
- }],
407
- },
408
- },
409
- });
410
- const a = new EmailAdapter({
411
- account: { provider: "qq", email: "u@qq.com", authCode: "x", folders: ["INBOX"] },
412
- sessionFactory: factory,
413
- // Inject a fake parser so test doesn't depend on mailparser
414
- parser: async (raw) => ({
415
- headers: { subject: "Parsed" },
416
- textBody: "this is the parsed text body",
417
- htmlBody: "",
418
- attachments: [
419
- { filename: "a.pdf", contentType: "application/pdf", contentDisposition: "attachment",
420
- size: 42, sha256: "abc123", isInline: false, isEncrypted: false },
421
- ],
422
- contentSha256: "deadbeef",
423
- sourceBytes: raw.length,
424
- subject: "Parsed",
425
- date: new Date("2026-05-19"),
426
- }),
427
- });
428
- const raws = [];
429
- for await (const r of a.sync()) raws.push(r);
430
- expect(raws).toHaveLength(1);
431
- expect(raws[0].payload.parsedBody).toBeDefined();
432
- expect(raws[0].payload.parsedBody.textBody).toBe("this is the parsed text body");
433
- expect(raws[0].payload.parsedBody.attachments).toHaveLength(1);
434
- // Source bytes themselves get stripped from the payload to avoid bloat
435
- expect(raws[0].payload.source).toBeUndefined();
436
- });
437
-
438
- it("normalize uses parsedBody.textBody as event.content.text", () => {
439
- const a = new EmailAdapter({
440
- account: { provider: "qq", email: "u@qq.com", authCode: "x" },
441
- sessionFactory: makeMockSession({}).factory,
442
- parser: async () => ({}),
443
- });
444
- const raw = {
445
- adapter: "email-imap",
446
- originalId: "<m@x>",
447
- capturedAt: 0,
448
- payload: {
449
- ...env(1),
450
- folder: "INBOX",
451
- parsedBody: {
452
- textBody: "Dear user, your account statement is attached.",
453
- htmlBody: "",
454
- attachments: [],
455
- contentSha256: "abc",
456
- headers: { "list-unsubscribe": "<mailto:unsub@bank.com>" },
457
- },
458
- },
459
- };
460
- const batch = a.normalize(raw);
461
- expect(batch.events[0].content.text).toBe("Dear user, your account statement is attached.");
462
- expect(batch.events[0].extra.rawSha256).toBe("abc");
463
- expect(batch.events[0].extra.indicatorHeaders["list-unsubscribe"]).toBe("<mailto:unsub@bank.com>");
464
- });
465
-
466
- it("normalize falls back to envelope prose when parsedBody is absent", () => {
467
- const a = new EmailAdapter({
468
- account: { provider: "qq", email: "u@qq.com", authCode: "x" },
469
- sessionFactory: makeMockSession({}).factory,
470
- parser: async () => ({}),
471
- });
472
- const raw = {
473
- adapter: "email-imap",
474
- originalId: "<m@x>",
475
- capturedAt: 0,
476
- payload: { ...env(1), folder: "INBOX" }, // no parsedBody
477
- };
478
- const batch = a.normalize(raw);
479
- expect(batch.events[0].content.text).toContain("alice1@example.com");
480
- expect(batch.events[0].content.text).toContain("subject:");
481
- });
482
-
483
- it("normalize HTML-only bodies are stripped to plain text", () => {
484
- const a = new EmailAdapter({
485
- account: { provider: "qq", email: "u@qq.com", authCode: "x" },
486
- sessionFactory: makeMockSession({}).factory,
487
- parser: async () => ({}),
488
- });
489
- const raw = {
490
- adapter: "email-imap",
491
- originalId: "<m@x>",
492
- capturedAt: 0,
493
- payload: {
494
- ...env(1),
495
- folder: "INBOX",
496
- parsedBody: {
497
- textBody: "",
498
- htmlBody: "<p>Hi <b>there</b>!</p><script>alert(1)</script>",
499
- attachments: [],
500
- },
501
- },
502
- };
503
- const batch = a.normalize(raw);
504
- expect(batch.events[0].content.text).toContain("Hi");
505
- expect(batch.events[0].content.text).toContain("there");
506
- expect(batch.events[0].content.text).not.toContain("<p>");
507
- expect(batch.events[0].content.text).not.toContain("alert(1)"); // script content stripped
508
- });
509
-
510
- it("normalize caps body at maxBodyChars + appends truncation marker", () => {
511
- const longText = "X".repeat(20_000);
512
- const a = new EmailAdapter({
513
- account: { provider: "qq", email: "u@qq.com", authCode: "x" },
514
- sessionFactory: makeMockSession({}).factory,
515
- parser: async () => ({}),
516
- maxBodyChars: 100,
517
- });
518
- const raw = {
519
- adapter: "email-imap",
520
- originalId: "<m@x>",
521
- capturedAt: 0,
522
- payload: {
523
- ...env(1),
524
- folder: "INBOX",
525
- parsedBody: { textBody: longText, htmlBody: "", attachments: [] },
526
- },
527
- };
528
- const batch = a.normalize(raw);
529
- expect(batch.events[0].content.text.length).toBeLessThan(longText.length);
530
- expect(batch.events[0].content.text).toMatch(/truncated/);
531
- });
532
-
533
- it("normalize captures attachment metadata in extra.attachments", () => {
534
- const a = new EmailAdapter({
535
- account: { provider: "qq", email: "u@qq.com", authCode: "x" },
536
- sessionFactory: makeMockSession({}).factory,
537
- parser: async () => ({}),
538
- });
539
- const raw = {
540
- adapter: "email-imap",
541
- originalId: "<m@x>",
542
- capturedAt: 0,
543
- payload: {
544
- ...env(1),
545
- folder: "INBOX",
546
- parsedBody: {
547
- textBody: "see attached",
548
- htmlBody: "",
549
- attachments: [
550
- { filename: "stmt.pdf", contentType: "application/pdf", contentDisposition: "attachment",
551
- size: 12345, sha256: "abc", isInline: false, isEncrypted: true },
552
- ],
553
- },
554
- },
555
- };
556
- const batch = a.normalize(raw);
557
- expect(batch.events[0].extra.attachments).toHaveLength(1);
558
- const a0 = batch.events[0].extra.attachments[0];
559
- expect(a0.filename).toBe("stmt.pdf");
560
- expect(a0.isEncrypted).toBe(true);
561
- expect(a0.sha256).toBe("abc");
562
- });
563
-
564
- it("sync degrades gracefully when parser throws (parseError captured)", async () => {
565
- const { factory } = makeMockSession({
566
- mailboxes: {
567
- INBOX: { uidValidity: 1, envelopes: [{ ...env(1), source: Buffer.from("garbage", "utf8") }] },
568
- },
569
- });
570
- const a = new EmailAdapter({
571
- account: { provider: "qq", email: "u@qq.com", authCode: "x", folders: ["INBOX"] },
572
- sessionFactory: factory,
573
- parser: async () => { throw new Error("malformed MIME"); },
574
- });
575
- const raws = [];
576
- for await (const r of a.sync()) raws.push(r);
577
- expect(raws).toHaveLength(1);
578
- expect(raws[0].payload.parsedBody).toBeDefined();
579
- expect(raws[0].payload.parsedBody.parseError).toContain("malformed MIME");
580
- // normalize should still produce a valid event (envelope fallback)
581
- const batch = a.normalize(raws[0]);
582
- expect(batch.events).toHaveLength(1);
583
- expect(batch.events[0].extra.parseError).toContain("malformed MIME");
584
- const v = validateBatch(batch);
585
- expect(v.valid).toBe(true);
586
- });
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
-
725
- describe("parseWatermark / formatWatermark", () => {
726
- it("parses well-formed strings", () => {
727
- expect(parseWatermark("42:100")).toEqual({ uidValidity: "42", lastUid: 100 });
728
- expect(parseWatermark("abc:0")).toEqual({ uidValidity: "abc", lastUid: 0 });
729
- });
730
-
731
- it("falls back to null/0 for malformed input", () => {
732
- expect(parseWatermark("")).toEqual({ uidValidity: null, lastUid: 0 });
733
- expect(parseWatermark("no-colon")).toEqual({ uidValidity: null, lastUid: 0 });
734
- expect(parseWatermark(null)).toEqual({ uidValidity: null, lastUid: 0 });
735
- });
736
-
737
- it("formats correctly", () => {
738
- expect(formatWatermark(42, 100)).toBe("42:100");
739
- expect(formatWatermark("abc", 0)).toBe("abc:0");
740
- expect(formatWatermark(null, 5)).toBe(":5");
741
- });
742
- });