@chainlesschain/personal-data-hub 0.4.28 → 0.4.30

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 (204) hide show
  1. package/README.md +13 -5
  2. package/lib/adapters/social-douyin-adb/usage-profile-reader.js +253 -0
  3. package/lib/adapters/social-douyin-adb/watch-history-reader.js +104 -31
  4. package/lib/adapters/social-toutiao-adb/article-reader.js +202 -0
  5. package/lib/analysis-skills/overview.js +24 -4
  6. package/lib/analysis-skills/spending.js +63 -2
  7. package/lib/analysis-skills/timeline.js +11 -6
  8. package/lib/prompt-builder.js +15 -1
  9. package/lib/query-parser.js +38 -8
  10. package/package.json +4 -1
  11. package/__tests__/adapter-guide.test.js +0 -47
  12. package/__tests__/adapter-spec.test.js +0 -78
  13. package/__tests__/adapters/ai-chat-cookie-capture-spec.test.js +0 -211
  14. package/__tests__/adapters/ai-chat-health-checker.test.js +0 -262
  15. package/__tests__/adapters/ai-chat-history.test.js +0 -396
  16. package/__tests__/adapters/ai-chat-http-client.test.js +0 -242
  17. package/__tests__/adapters/ai-chat-vendors.test.js +0 -874
  18. package/__tests__/adapters/alipay-bill-adapter.test.js +0 -538
  19. package/__tests__/adapters/apple-health.test.js +0 -95
  20. package/__tests__/adapters/bank-family.test.js +0 -125
  21. package/__tests__/adapters/biz-tianyancha.test.js +0 -159
  22. package/__tests__/adapters/browser-history-chrome.test.js +0 -377
  23. package/__tests__/adapters/browser-history-edge.test.js +0 -159
  24. package/__tests__/adapters/car-mercedesme.test.js +0 -74
  25. package/__tests__/adapters/doc-baidu-netdisk.test.js +0 -102
  26. package/__tests__/adapters/doc-camscanner.test.js +0 -147
  27. package/__tests__/adapters/doc-platforms.test.js +0 -177
  28. package/__tests__/adapters/edu-huawei-learning-live.test.js +0 -198
  29. package/__tests__/adapters/edu-zuoyebang-live.test.js +0 -226
  30. package/__tests__/adapters/email-adapter-snapshot.test.js +0 -237
  31. package/__tests__/adapters/email-adapter.test.js +0 -742
  32. package/__tests__/adapters/email-classifier.test.js +0 -347
  33. package/__tests__/adapters/email-imap-session.test.js +0 -334
  34. package/__tests__/adapters/email-parser.test.js +0 -244
  35. package/__tests__/adapters/email-pdf-extractor.test.js +0 -529
  36. package/__tests__/adapters/email-providers.test.js +0 -84
  37. package/__tests__/adapters/email-retry-progress.test.js +0 -294
  38. package/__tests__/adapters/email-templates.test.js +0 -822
  39. package/__tests__/adapters/family-23-collectors-scaffold.test.js +0 -182
  40. package/__tests__/adapters/finance-alipay-live.test.js +0 -258
  41. package/__tests__/adapters/finance-dcep.test.js +0 -74
  42. package/__tests__/adapters/fitness-joyrun.test.js +0 -82
  43. package/__tests__/adapters/game-genshin-live.test.js +0 -238
  44. package/__tests__/adapters/game-genshin-scaffold.test.js +0 -108
  45. package/__tests__/adapters/game-honor-of-kings-live.test.js +0 -230
  46. package/__tests__/adapters/git-activity.test.js +0 -222
  47. package/__tests__/adapters/gov-12123.test.js +0 -103
  48. package/__tests__/adapters/gov-ixiamen.test.js +0 -150
  49. package/__tests__/adapters/gov-tax.test.js +0 -135
  50. package/__tests__/adapters/health-meiyou.test.js +0 -125
  51. package/__tests__/adapters/local-files.test.js +0 -264
  52. package/__tests__/adapters/local-im-pc.test.js +0 -154
  53. package/__tests__/adapters/messaging-whatsapp.test.js +0 -289
  54. package/__tests__/adapters/music-kugou.test.js +0 -187
  55. package/__tests__/adapters/music-qq.test.js +0 -112
  56. package/__tests__/adapters/netease-music-live.test.js +0 -244
  57. package/__tests__/adapters/netease-music.test.js +0 -74
  58. package/__tests__/adapters/pc-local-discovery.test.js +0 -141
  59. package/__tests__/adapters/qq-pc-direct-read.test.js +0 -227
  60. package/__tests__/adapters/reading-family.test.js +0 -108
  61. package/__tests__/adapters/recruit-boss.test.js +0 -180
  62. package/__tests__/adapters/shell-history.test.js +0 -180
  63. package/__tests__/adapters/shopping-base.test.js +0 -179
  64. package/__tests__/adapters/shopping-dianping.test.js +0 -239
  65. package/__tests__/adapters/social-bilibili-adb-api-client.test.js +0 -721
  66. package/__tests__/adapters/social-bilibili-adb-chromium-cookies-reader.test.js +0 -346
  67. package/__tests__/adapters/social-bilibili-adb-collector.test.js +0 -284
  68. package/__tests__/adapters/social-bilibili-adb-cookies-extension.test.js +0 -343
  69. package/__tests__/adapters/social-bilibili-adb-snapshot-builder.test.js +0 -296
  70. package/__tests__/adapters/social-csdn.test.js +0 -175
  71. package/__tests__/adapters/social-dongchedi.test.js +0 -165
  72. package/__tests__/adapters/social-douyin-adb-aweme-detail.test.js +0 -165
  73. package/__tests__/adapters/social-douyin-adb-collector.test.js +0 -254
  74. package/__tests__/adapters/social-douyin-adb-db-extension.test.js +0 -114
  75. package/__tests__/adapters/social-douyin-adb-im-db-parser.test.js +0 -304
  76. package/__tests__/adapters/social-douyin-adb-snapshot-builder.test.js +0 -216
  77. package/__tests__/adapters/social-douyin-adb-watch-history.test.js +0 -192
  78. package/__tests__/adapters/social-kuaishou-adb-api-client.test.js +0 -496
  79. package/__tests__/adapters/social-kuaishou-adb-collector.test.js +0 -276
  80. package/__tests__/adapters/social-kuaishou-adb-cookies-extension.test.js +0 -152
  81. package/__tests__/adapters/social-kuaishou-adb-snapshot-builder.test.js +0 -178
  82. package/__tests__/adapters/social-toutiao-adb-account-reader.test.js +0 -135
  83. package/__tests__/adapters/social-toutiao-adb-api-client.test.js +0 -626
  84. package/__tests__/adapters/social-toutiao-adb-collector.test.js +0 -378
  85. package/__tests__/adapters/social-toutiao-adb-cookies-extension.test.js +0 -193
  86. package/__tests__/adapters/social-toutiao-adb-snapshot-builder.test.js +0 -196
  87. package/__tests__/adapters/social-toutiao-kuaishou-scaffold.test.js +0 -311
  88. package/__tests__/adapters/social-weibo-adb-api-client.test.js +0 -362
  89. package/__tests__/adapters/social-weibo-adb-collector.test.js +0 -201
  90. package/__tests__/adapters/social-weibo-adb-cookies-extension.test.js +0 -167
  91. package/__tests__/adapters/social-weibo-adb-snapshot-builder.test.js +0 -189
  92. package/__tests__/adapters/social-xiaohongshu-adb-api-client.test.js +0 -431
  93. package/__tests__/adapters/social-xiaohongshu-adb-collector.test.js +0 -207
  94. package/__tests__/adapters/social-xiaohongshu-adb-cookies-extension.test.js +0 -0
  95. package/__tests__/adapters/social-xiaohongshu-adb-sign-provider-injection.test.js +0 -351
  96. package/__tests__/adapters/social-xiaohongshu-adb-sign.test.js +0 -130
  97. package/__tests__/adapters/social-xiaohongshu-adb-snapshot-builder.test.js +0 -200
  98. package/__tests__/adapters/social-zhihu.test.js +0 -246
  99. package/__tests__/adapters/system-data-adapter.test.js +0 -443
  100. package/__tests__/adapters/system-data-android-ingest.test.js +0 -144
  101. package/__tests__/adapters/system-data-android.test.js +0 -519
  102. package/__tests__/adapters/system-data-disclosure.test.js +0 -153
  103. package/__tests__/adapters/travel-12306.test.js +0 -512
  104. package/__tests__/adapters/travel-amap.test.js +0 -219
  105. package/__tests__/adapters/travel-baidu-map.test.js +0 -305
  106. package/__tests__/adapters/travel-base.test.js +0 -205
  107. package/__tests__/adapters/travel-ctrip.test.js +0 -377
  108. package/__tests__/adapters/travel-didi-consumer.test.js +0 -66
  109. package/__tests__/adapters/travel-didi.test.js +0 -204
  110. package/__tests__/adapters/travel-tencent-map.test.js +0 -207
  111. package/__tests__/adapters/travel-tongcheng.test.js +0 -289
  112. package/__tests__/adapters/video-platforms.test.js +0 -152
  113. package/__tests__/adapters/video-xigua.test.js +0 -106
  114. package/__tests__/adapters/vscode.test.js +0 -299
  115. package/__tests__/adapters/wechat-bootstrap.test.js +0 -240
  116. package/__tests__/adapters/wechat-env-probe.test.js +0 -162
  117. package/__tests__/adapters/wechat-frida-agent.test.js +0 -322
  118. package/__tests__/adapters/wechat-frida-integration.test.js +0 -149
  119. package/__tests__/adapters/wechat-frida-key-provider.test.js +0 -188
  120. package/__tests__/adapters/wechat-md5-key-provider.test.js +0 -101
  121. package/__tests__/adapters/wechat-pc-direct-read.test.js +0 -365
  122. package/__tests__/adapters/wechat-pc-group-topic.test.js +0 -63
  123. package/__tests__/adapters/wechat-pc-v4-sidecar.test.js +0 -72
  124. package/__tests__/adapters/weread.test.js +0 -123
  125. package/__tests__/adapters/wework-pc.test.js +0 -124
  126. package/__tests__/adapters/win-recent.test.js +0 -192
  127. package/__tests__/analysis-skills.test.js +0 -679
  128. package/__tests__/analysis.test.js +0 -1845
  129. package/__tests__/audio-ximalaya-snapshot.test.js +0 -279
  130. package/__tests__/batch.test.js +0 -133
  131. package/__tests__/bridges-cc-kg.test.js +0 -231
  132. package/__tests__/bridges-cc-llm.test.js +0 -191
  133. package/__tests__/bridges-cc-rag.test.js +0 -162
  134. package/__tests__/categories.test.js +0 -92
  135. package/__tests__/e2e/ai-chat-cross-source-journey.test.js +0 -213
  136. package/__tests__/e2e/full-user-journey.test.js +0 -188
  137. package/__tests__/e2e/local-data-adapters-cli.e2e.test.js +0 -146
  138. package/__tests__/entity-resolver-ingest-hook.test.js +0 -177
  139. package/__tests__/entity-resolver-stages.test.js +0 -411
  140. package/__tests__/entity-resolver-vault.test.js +0 -249
  141. package/__tests__/entity-resolver.test.js +0 -526
  142. package/__tests__/fitness-keep-snapshot.test.js +0 -224
  143. package/__tests__/fixtures/entity-resolver-200-mock.json +0 -96
  144. package/__tests__/ids.test.js +0 -45
  145. package/__tests__/integration/ai-chat-history-registry.test.js +0 -228
  146. package/__tests__/integration/aichat-wizard-end-to-end.test.js +0 -282
  147. package/__tests__/integration/cross-adapter-pipelines.test.js +0 -396
  148. package/__tests__/integration/local-data-adapters-pipeline.test.js +0 -373
  149. package/__tests__/integration/social-bilibili-pipeline.test.js +0 -261
  150. package/__tests__/integration/wechat-bootstrap-end-to-end.test.js +0 -390
  151. package/__tests__/key-providers.test.js +0 -126
  152. package/__tests__/kg-derive.test.js +0 -219
  153. package/__tests__/llm-client.test.js +0 -122
  154. package/__tests__/longtail-adapters.test.js +0 -281
  155. package/__tests__/messaging-qq-snapshot.test.js +0 -294
  156. package/__tests__/mobile-extractor-encrypted.test.js +0 -460
  157. package/__tests__/mobile-extractor.test.js +0 -288
  158. package/__tests__/mock-adapter.test.js +0 -93
  159. package/__tests__/prompt-builder.test.js +0 -249
  160. package/__tests__/query-parser.test.js +0 -302
  161. package/__tests__/rag-derive.test.js +0 -169
  162. package/__tests__/registry-readiness.test.js +0 -292
  163. package/__tests__/registry.test.js +0 -420
  164. package/__tests__/salvage-ingest.test.js +0 -97
  165. package/__tests__/schemas.test.js +0 -331
  166. package/__tests__/shopping-adapters.test.js +0 -392
  167. package/__tests__/shopping-eleme-snapshot.test.js +0 -454
  168. package/__tests__/shopping-pinduoduo-snapshot.test.js +0 -484
  169. package/__tests__/shopping-snapshot.test.js +0 -438
  170. package/__tests__/shopping-vipshop-snapshot.test.js +0 -425
  171. package/__tests__/shopping-xianyu-snapshot.test.js +0 -451
  172. package/__tests__/sidecar-contacts-cross-validate.test.js +0 -186
  173. package/__tests__/sidecar-supervisor.test.js +0 -128
  174. package/__tests__/sign-providers.test.js +0 -62
  175. package/__tests__/social-adapters.test.js +0 -280
  176. package/__tests__/social-bilibili-snapshot.test.js +0 -278
  177. package/__tests__/social-douban-snapshot.test.js +0 -351
  178. package/__tests__/social-douyin-im-direct-read.test.js +0 -377
  179. package/__tests__/social-douyin-salvage-collector.test.js +0 -98
  180. package/__tests__/social-douyin-salvage-mapper.test.js +0 -90
  181. package/__tests__/social-douyin-snapshot.test.js +0 -256
  182. package/__tests__/social-kuaishou-snapshot.test.js +0 -362
  183. package/__tests__/social-toutiao-snapshot.test.js +0 -366
  184. package/__tests__/social-weibo-snapshot.test.js +0 -234
  185. package/__tests__/social-weibo-sqlite-device.test.js +0 -174
  186. package/__tests__/social-xiaohongshu-snapshot.test.js +0 -232
  187. package/__tests__/sqlite-leaf-salvage.test.js +0 -97
  188. package/__tests__/travel-adapters.test.js +0 -483
  189. package/__tests__/travel-maps-snapshot.test.js +0 -426
  190. package/__tests__/vault-driver-error.test.js +0 -74
  191. package/__tests__/vault-search-helpers.test.js +0 -104
  192. package/__tests__/vault-search.test.js +0 -423
  193. package/__tests__/vault.test.js +0 -767
  194. package/__tests__/wechat-adapter.test.js +0 -594
  195. package/__tests__/whatsapp-adapter.test.js +0 -138
  196. package/scripts/_make-fixture-all.js +0 -126
  197. package/scripts/_make-fixture-contacts.js +0 -84
  198. package/scripts/evaluate-entity-resolver.js +0 -213
  199. package/scripts/run-native-tests-sandbox.sh +0 -55
  200. package/scripts/smoke-phase-5-5.js +0 -196
  201. package/scripts/smoke-phase-5-7.js +0 -181
  202. package/scripts/smoke-system-data-contacts.js +0 -309
  203. package/scripts/smoke-system-data.js +0 -312
  204. package/vitest.config.js +0 -88
@@ -1,454 +0,0 @@
1
- "use strict";
2
-
3
- import { describe, it, expect, beforeEach } from "vitest";
4
-
5
- const fs = require("node:fs");
6
- const path = require("node:path");
7
- const os = require("node:os");
8
-
9
- const {
10
- ElemeAdapter,
11
- SNAPSHOT_SCHEMA_VERSION,
12
- VALID_SNAPSHOT_KINDS,
13
- orderToRecord,
14
- extractOrders,
15
- } = require("../lib/adapters/shopping-eleme");
16
- const { assertAdapter } = require("../lib/adapter-spec");
17
- const { validateBatch } = require("../lib/batch");
18
-
19
- // 饿了么 (Ele.me) — Alibaba-owned #2 food-delivery; mirrors shopping-meituan
20
- // (the sibling 外卖 platform) with a snapshot + cookie-api dual path. The mtop
21
- // signing is injected (signProvider) so the adapter stays pure-Node.
22
-
23
- function writeSnapshot(dir, snapshot) {
24
- const p = path.join(dir, "shopping-eleme.json");
25
- fs.writeFileSync(p, JSON.stringify(snapshot), "utf-8");
26
- return p;
27
- }
28
-
29
- function writeRaw(dir, fileName, content) {
30
- const p = path.join(dir, fileName);
31
- fs.writeFileSync(p, content, "utf-8");
32
- return p;
33
- }
34
-
35
- describe("ElemeAdapter snapshot mode", () => {
36
- let tmpDir;
37
- beforeEach(() => {
38
- tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "eleme-snap-"));
39
- });
40
-
41
- it("exports SNAPSHOT_SCHEMA_VERSION = 1 and VALID_SNAPSHOT_KINDS = [order]", () => {
42
- expect(SNAPSHOT_SCHEMA_VERSION).toBe(1);
43
- expect(VALID_SNAPSHOT_KINDS).toEqual(["order"]);
44
- });
45
-
46
- it("authenticate(inputPath) ok when readable", async () => {
47
- const p = writeSnapshot(tmpDir, {
48
- schemaVersion: 1,
49
- snapshottedAt: Date.now(),
50
- events: [],
51
- });
52
- const a = new ElemeAdapter();
53
- const res = await a.authenticate({ inputPath: p });
54
- expect(res.ok).toBe(true);
55
- expect(res.mode).toBe("snapshot-file");
56
- });
57
-
58
- it("authenticate(inputPath) fails when path unreadable", async () => {
59
- const a = new ElemeAdapter();
60
- const res = await a.authenticate({ inputPath: path.join(tmpDir, "missing.json") });
61
- expect(res.ok).toBe(false);
62
- expect(res.reason).toBe("INPUT_PATH_UNREADABLE");
63
- });
64
-
65
- it("authenticate() with no inputPath returns NO_INPUT", async () => {
66
- const a = new ElemeAdapter();
67
- const res = await a.authenticate({});
68
- expect(res.ok).toBe(false);
69
- expect(res.reason).toBe("NO_INPUT");
70
- expect(res.message).toMatch(/snapshot mode/);
71
- });
72
-
73
- it("sync() without inputPath throws with signing hint", async () => {
74
- const a = new ElemeAdapter();
75
- let threw = null;
76
- try {
77
- for await (const _r of a.sync({})) { /* drain */ }
78
- } catch (err) {
79
- threw = err;
80
- }
81
- expect(threw).toBeTruthy();
82
- expect(String(threw.message)).toMatch(/signProvider/);
83
- });
84
-
85
- it("rejects non-JSON inputPath", async () => {
86
- const p = writeRaw(tmpDir, "orders.html", "<html><body>not json</body></html>");
87
- const a = new ElemeAdapter();
88
- let threw = null;
89
- try {
90
- for await (const _r of a.sync({ inputPath: p })) { /* drain */ }
91
- } catch (err) {
92
- threw = err;
93
- }
94
- expect(threw).toBeTruthy();
95
- expect(String(threw.message)).toMatch(/snapshot must be JSON/);
96
- });
97
-
98
- it("rejects schemaVersion mismatch", async () => {
99
- const p = writeSnapshot(tmpDir, {
100
- schemaVersion: 99,
101
- snapshottedAt: Date.now(),
102
- events: [],
103
- });
104
- const a = new ElemeAdapter();
105
- let threw = null;
106
- try {
107
- for await (const _r of a.sync({ inputPath: p })) { /* drain */ }
108
- } catch (err) {
109
- threw = err;
110
- }
111
- expect(threw).toBeTruthy();
112
- expect(String(threw.message)).toMatch(/schemaVersion mismatch/);
113
- });
114
-
115
- it("empty events array yields nothing (no crash)", async () => {
116
- const p = writeSnapshot(tmpDir, {
117
- schemaVersion: 1,
118
- snapshottedAt: Date.now(),
119
- events: [],
120
- });
121
- const a = new ElemeAdapter();
122
- const raws = [];
123
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
124
- expect(raws.length).toBe(0);
125
- });
126
-
127
- it("order event round-trips normalize cleanly", async () => {
128
- const now = Date.now();
129
- const p = writeSnapshot(tmpDir, {
130
- schemaVersion: 1,
131
- snapshottedAt: now,
132
- vendor: "eleme",
133
- account: { userId: "u-alice", displayName: "alice" },
134
- events: [
135
- {
136
- kind: "order",
137
- id: "order-ELM-9001",
138
- capturedAt: now - 1000,
139
- orderId: "ELM-9001",
140
- merchantName: "沙县小吃(人民路店)",
141
- placedAt: now - 86400_000,
142
- paidAt: now - 86400_000 + 5_000,
143
- status: "已送达",
144
- items: [
145
- { name: "蒸饺", quantity: 2, unitPrice: 8.0 },
146
- { name: "拌面", quantity: 1, unitPrice: 7.5 },
147
- ],
148
- totalAmount: { value: 23.5, currency: "CNY" },
149
- recipient: "张三",
150
- shippingAddress: "上海市浦东新区...",
151
- },
152
- ],
153
- });
154
- const a = new ElemeAdapter();
155
- const raws = [];
156
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
157
- expect(raws.length).toBe(1);
158
- expect(raws[0].kind).toBe("order");
159
- expect(raws[0].originalId).toBe("eleme:order:order-ELM-9001");
160
-
161
- const batch = a.normalize(raws[0]);
162
- expect(validateBatch(batch).valid).toBe(true);
163
- expect(batch.events.length).toBeGreaterThan(0);
164
- expect(JSON.stringify(batch)).toContain("蒸饺");
165
- expect(JSON.stringify(batch)).toContain("张三");
166
- });
167
-
168
- it("status mapping handles ele.me-typical strings", async () => {
169
- const now = Date.now();
170
- const cases = [
171
- { status: "配送中", expect: "shipped" },
172
- { status: "已送达", expect: "delivered" },
173
- { status: "已完成", expect: "delivered" },
174
- { status: "退款成功", expect: "refunded" },
175
- { status: "已取消", expect: "cancelled" },
176
- { status: "等待商家接单", expect: "placed" },
177
- ];
178
- for (const c of cases) {
179
- const p = writeSnapshot(tmpDir, {
180
- schemaVersion: 1,
181
- snapshottedAt: now,
182
- events: [
183
- {
184
- kind: "order", id: `o-${c.status}`,
185
- orderId: `o-${c.status}`, merchantName: "m",
186
- placedAt: now, paidAt: now, status: c.status,
187
- items: [{ name: "x", quantity: 1, unitPrice: 1 }],
188
- totalAmount: { value: 1, currency: "CNY" },
189
- },
190
- ],
191
- });
192
- const a = new ElemeAdapter();
193
- const raws = [];
194
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
195
- const batch = a.normalize(raws[0]);
196
- expect(JSON.stringify(batch)).toContain(c.expect);
197
- }
198
- });
199
-
200
- it("respects per-kind include opt-out", async () => {
201
- const now = Date.now();
202
- const p = writeSnapshot(tmpDir, {
203
- schemaVersion: 1,
204
- snapshottedAt: now,
205
- events: [
206
- { kind: "order", id: "o1", orderId: "o1", merchantName: "m", placedAt: now,
207
- items: [], totalAmount: { value: 1, currency: "CNY" } },
208
- ],
209
- });
210
- const a = new ElemeAdapter();
211
- const raws = [];
212
- for await (const r of a.sync({ inputPath: p, include: { order: false } })) {
213
- raws.push(r);
214
- }
215
- expect(raws.length).toBe(0);
216
- });
217
-
218
- it("respects opts.limit", async () => {
219
- const now = Date.now();
220
- const events = Array.from({ length: 5 }, (_, i) => ({
221
- kind: "order", id: `o${i}`, orderId: `o${i}`, merchantName: "m",
222
- placedAt: now - i * 1000,
223
- items: [], totalAmount: { value: 1, currency: "CNY" },
224
- }));
225
- const p = writeSnapshot(tmpDir, { schemaVersion: 1, snapshottedAt: now, events });
226
- const a = new ElemeAdapter();
227
- const raws = [];
228
- for await (const r of a.sync({ inputPath: p, limit: 2 })) raws.push(r);
229
- expect(raws.length).toBe(2);
230
- });
231
-
232
- it("filters out unknown kinds (forward compat)", async () => {
233
- const now = Date.now();
234
- const p = writeSnapshot(tmpDir, {
235
- schemaVersion: 1,
236
- snapshottedAt: now,
237
- events: [
238
- { kind: "order", id: "o1", orderId: "o1", merchantName: "m", placedAt: now,
239
- items: [], totalAmount: { value: 1, currency: "CNY" } },
240
- { kind: "refund", id: "r1", orderId: "o1" },
241
- { kind: "future-kind", id: "x" },
242
- ],
243
- });
244
- const a = new ElemeAdapter();
245
- const raws = [];
246
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
247
- expect(raws.length).toBe(1);
248
- expect(raws[0].kind).toBe("order");
249
- });
250
-
251
- it("snapshottedAt fallback when event capturedAt missing", async () => {
252
- const ts = 1700000000000;
253
- const p = writeSnapshot(tmpDir, {
254
- schemaVersion: 1,
255
- snapshottedAt: ts,
256
- events: [
257
- { kind: "order", id: "o1", orderId: "o1", merchantName: "m", items: [],
258
- totalAmount: { value: 1, currency: "CNY" } },
259
- ],
260
- });
261
- const a = new ElemeAdapter();
262
- const raws = [];
263
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
264
- expect(raws[0].capturedAt).toBe(ts);
265
- });
266
-
267
- it("advertises both snapshot and cookie-api capabilities", () => {
268
- const a = new ElemeAdapter();
269
- expect(a.capabilities).toContain("sync:snapshot");
270
- expect(a.capabilities).toContain("sync:cookie-api");
271
- expect(assertAdapter(a).ok).toBe(true);
272
- });
273
- });
274
-
275
- describe("ElemeAdapter cookie-api mode", () => {
276
- it("authenticate(cookie) ok when userId + cookies present", async () => {
277
- const a = new ElemeAdapter({ account: { userId: "u-1", cookies: "USERID=ok" } });
278
- const res = await a.authenticate();
279
- expect(res.ok).toBe(true);
280
- expect(res.mode).toBe("cookie");
281
- expect(res.account).toBe("u-1");
282
- });
283
-
284
- it("authenticate(cookie) requires account.userId", async () => {
285
- const a = new ElemeAdapter({ account: { cookies: "USERID=ok" } });
286
- const res = await a.authenticate();
287
- expect(res.ok).toBe(false);
288
- expect(res.reason).toBe("NO_ACCOUNT_USER_ID");
289
- });
290
-
291
- it("sync yields normalized records from fetchFn fixture (元)", async () => {
292
- const fetchFn = async () => ({
293
- orders: [
294
- {
295
- order_id: "ELM-COOKIE-1",
296
- restaurant_name: "兰州拉面",
297
- status_bar_text: "已送达",
298
- total_amount: 28.5,
299
- order_time: 1700000000, // sec
300
- pay_time: 1700000010,
301
- consignee: "李四",
302
- address: "广州市天河区...",
303
- basket: [
304
- { name: "牛肉拉面", quantity: 2, price: 14.25 },
305
- ],
306
- },
307
- ],
308
- });
309
- const a = new ElemeAdapter({
310
- account: { userId: "u-1", cookies: "USERID=ok" },
311
- fetchFn,
312
- });
313
- const raws = [];
314
- for await (const r of a.sync({ sinceWatermark: 0 })) raws.push(r);
315
- expect(raws.length).toBe(1);
316
- expect(raws[0].originalId).toBe("ELM-COOKIE-1");
317
- expect(raws[0].payload.record.totalAmount.value).toBe(28.5);
318
- expect(raws[0].payload.record.items[0].unitPrice).toBe(14.25);
319
- expect(raws[0].payload.record.status).toBe("delivered");
320
-
321
- const batch = a.normalize(raws[0]);
322
- expect(validateBatch(batch).valid).toBe(true);
323
- expect(JSON.stringify(batch)).toContain("牛肉拉面");
324
- expect(JSON.stringify(batch)).toContain("李四");
325
- });
326
-
327
- it("invokes signProvider and passes sign to fetchFn", async () => {
328
- let seenSign = null;
329
- const signProvider = async () => "SIGN-XYZ";
330
- const fetchFn = async (opts) => {
331
- seenSign = opts.sign;
332
- return { orders: [] };
333
- };
334
- const a = new ElemeAdapter({
335
- account: { userId: "u-1", cookies: "USERID=ok" },
336
- fetchFn,
337
- signProvider,
338
- });
339
- for await (const _r of a.sync({ sinceWatermark: 0 })) { /* drain */ }
340
- expect(seenSign).toBe("SIGN-XYZ");
341
- });
342
-
343
- it("passes sign: null when no signProvider configured", async () => {
344
- let seen = "unset";
345
- const fetchFn = async (opts) => {
346
- seen = opts.sign;
347
- return { orders: [] };
348
- };
349
- const a = new ElemeAdapter({
350
- account: { userId: "u-1", cookies: "USERID=ok" },
351
- fetchFn,
352
- });
353
- for await (const _r of a.sync({ sinceWatermark: 0 })) { /* drain */ }
354
- expect(seen).toBe(null);
355
- });
356
-
357
- it("paginates with offset and stops at sinceWatermark", async () => {
358
- const pages = {
359
- 0: [
360
- { order_id: "p1-a", restaurant_name: "m", order_time: 1700000000, total_amount: 10, basket: [] },
361
- { order_id: "p1-b", restaurant_name: "m", order_time: 1699000000, total_amount: 10, basket: [] },
362
- ],
363
- 2: [
364
- { order_id: "p2-a", restaurant_name: "m", order_time: 1698000000, total_amount: 10, basket: [] },
365
- ],
366
- };
367
- const seenOffsets = [];
368
- const fetchFn = async (opts) => {
369
- seenOffsets.push(opts.query.offset);
370
- return { orders: pages[opts.query.offset] || [] };
371
- };
372
- const a = new ElemeAdapter({
373
- account: { userId: "u-1", cookies: "USERID=ok" },
374
- fetchFn,
375
- });
376
- const raws = [];
377
- for await (const r of a.sync({ sinceWatermark: 1699500000 * 1000, pageSize: 2 })) {
378
- raws.push(r);
379
- }
380
- expect(raws.map((r) => r.originalId)).toEqual(["p1-a"]);
381
- // stopped inside page 1 (offset 0) — never fetched offset 2
382
- expect(seenOffsets).toEqual([0]);
383
- });
384
-
385
- it("respects per-kind include opt-out in cookie mode (no fetch)", async () => {
386
- let called = false;
387
- const fetchFn = async () => {
388
- called = true;
389
- return { orders: [] };
390
- };
391
- const a = new ElemeAdapter({
392
- account: { userId: "u-1", cookies: "USERID=ok" },
393
- fetchFn,
394
- });
395
- const raws = [];
396
- for await (const r of a.sync({ include: { order: false } })) raws.push(r);
397
- expect(raws.length).toBe(0);
398
- expect(called).toBe(false);
399
- });
400
-
401
- it("orderToRecord maps ele.me fields (元 amounts)", () => {
402
- const rec = orderToRecord({
403
- order_id: "ELM-9",
404
- restaurant_name: "黄焖鸡米饭",
405
- status_bar_text: "已完成",
406
- total_amount: 18.0,
407
- order_time: 1700000000,
408
- basket: [{ name: "黄焖鸡", quantity: 1, price: 18.0 }],
409
- });
410
- expect(rec.orderId).toBe("ELM-9");
411
- expect(rec.merchantName).toBe("黄焖鸡米饭");
412
- expect(rec.status).toBe("delivered");
413
- expect(rec.totalAmount.value).toBe(18.0);
414
- expect(rec.items[0].name).toBe("黄焖鸡");
415
- expect(rec.extras.capturedBy).toBe("cookie-api");
416
- });
417
-
418
- it("extractOrders tolerates nested response shapes", () => {
419
- expect(extractOrders({ orders: [1] })).toEqual([1]);
420
- expect(extractOrders({ order_list: [2] })).toEqual([2]);
421
- expect(extractOrders({ list: [3] })).toEqual([3]);
422
- expect(extractOrders({ data: { orders: [4] } })).toEqual([4]);
423
- expect(extractOrders([5])).toEqual([5]);
424
- expect(extractOrders({})).toEqual([]);
425
- expect(extractOrders(null)).toEqual([]);
426
- });
427
-
428
- it("uses opts.ordersUrl override when provided", async () => {
429
- let seenUrl = null;
430
- const fetchFn = async (opts) => {
431
- seenUrl = opts.url;
432
- return { orders: [] };
433
- };
434
- const a = new ElemeAdapter({
435
- account: { userId: "u-1", cookies: "USERID=ok" },
436
- fetchFn,
437
- ordersUrl: "https://custom.example/orders",
438
- });
439
- for await (const _r of a.sync({ sinceWatermark: 0 })) { /* drain */ }
440
- expect(seenUrl).toBe("https://custom.example/orders");
441
- });
442
-
443
- it("default fetchFn throws a legible error when cookie mode used without injection", async () => {
444
- const a = new ElemeAdapter({ account: { userId: "u-1", cookies: "USERID=ok" } });
445
- let threw = null;
446
- try {
447
- for await (const _r of a.sync({ sinceWatermark: 0 })) { /* drain */ }
448
- } catch (err) {
449
- threw = err;
450
- }
451
- expect(threw).toBeTruthy();
452
- expect(String(threw.message)).toMatch(/no fetchFn configured/);
453
- });
454
- });