@chainlesschain/personal-data-hub 0.4.29 → 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 (198) hide show
  1. package/lib/prompt-builder.js +15 -1
  2. package/package.json +4 -1
  3. package/__tests__/adapter-guide.test.js +0 -47
  4. package/__tests__/adapter-spec.test.js +0 -78
  5. package/__tests__/adapters/ai-chat-cookie-capture-spec.test.js +0 -211
  6. package/__tests__/adapters/ai-chat-health-checker.test.js +0 -262
  7. package/__tests__/adapters/ai-chat-history.test.js +0 -396
  8. package/__tests__/adapters/ai-chat-http-client.test.js +0 -242
  9. package/__tests__/adapters/ai-chat-vendors.test.js +0 -874
  10. package/__tests__/adapters/alipay-bill-adapter.test.js +0 -538
  11. package/__tests__/adapters/apple-health.test.js +0 -95
  12. package/__tests__/adapters/bank-family.test.js +0 -125
  13. package/__tests__/adapters/biz-tianyancha.test.js +0 -159
  14. package/__tests__/adapters/browser-history-chrome.test.js +0 -377
  15. package/__tests__/adapters/browser-history-edge.test.js +0 -159
  16. package/__tests__/adapters/car-mercedesme.test.js +0 -74
  17. package/__tests__/adapters/doc-baidu-netdisk.test.js +0 -102
  18. package/__tests__/adapters/doc-camscanner.test.js +0 -147
  19. package/__tests__/adapters/doc-platforms.test.js +0 -177
  20. package/__tests__/adapters/edu-huawei-learning-live.test.js +0 -198
  21. package/__tests__/adapters/edu-zuoyebang-live.test.js +0 -226
  22. package/__tests__/adapters/email-adapter-snapshot.test.js +0 -237
  23. package/__tests__/adapters/email-adapter.test.js +0 -742
  24. package/__tests__/adapters/email-classifier.test.js +0 -347
  25. package/__tests__/adapters/email-imap-session.test.js +0 -334
  26. package/__tests__/adapters/email-parser.test.js +0 -244
  27. package/__tests__/adapters/email-pdf-extractor.test.js +0 -529
  28. package/__tests__/adapters/email-providers.test.js +0 -84
  29. package/__tests__/adapters/email-retry-progress.test.js +0 -294
  30. package/__tests__/adapters/email-templates.test.js +0 -822
  31. package/__tests__/adapters/family-23-collectors-scaffold.test.js +0 -182
  32. package/__tests__/adapters/finance-alipay-live.test.js +0 -258
  33. package/__tests__/adapters/finance-dcep.test.js +0 -74
  34. package/__tests__/adapters/fitness-joyrun.test.js +0 -82
  35. package/__tests__/adapters/game-genshin-live.test.js +0 -238
  36. package/__tests__/adapters/game-genshin-scaffold.test.js +0 -108
  37. package/__tests__/adapters/game-honor-of-kings-live.test.js +0 -230
  38. package/__tests__/adapters/git-activity.test.js +0 -222
  39. package/__tests__/adapters/gov-12123.test.js +0 -103
  40. package/__tests__/adapters/gov-ixiamen.test.js +0 -150
  41. package/__tests__/adapters/gov-tax.test.js +0 -135
  42. package/__tests__/adapters/health-meiyou.test.js +0 -125
  43. package/__tests__/adapters/local-files.test.js +0 -264
  44. package/__tests__/adapters/local-im-pc.test.js +0 -154
  45. package/__tests__/adapters/messaging-whatsapp.test.js +0 -289
  46. package/__tests__/adapters/music-kugou.test.js +0 -187
  47. package/__tests__/adapters/music-qq.test.js +0 -112
  48. package/__tests__/adapters/netease-music-live.test.js +0 -244
  49. package/__tests__/adapters/netease-music.test.js +0 -74
  50. package/__tests__/adapters/pc-local-discovery.test.js +0 -141
  51. package/__tests__/adapters/qq-pc-direct-read.test.js +0 -227
  52. package/__tests__/adapters/reading-family.test.js +0 -108
  53. package/__tests__/adapters/recruit-boss.test.js +0 -180
  54. package/__tests__/adapters/shell-history.test.js +0 -180
  55. package/__tests__/adapters/shopping-base.test.js +0 -179
  56. package/__tests__/adapters/shopping-dianping.test.js +0 -239
  57. package/__tests__/adapters/social-bilibili-adb-api-client.test.js +0 -721
  58. package/__tests__/adapters/social-bilibili-adb-chromium-cookies-reader.test.js +0 -346
  59. package/__tests__/adapters/social-bilibili-adb-collector.test.js +0 -284
  60. package/__tests__/adapters/social-bilibili-adb-cookies-extension.test.js +0 -343
  61. package/__tests__/adapters/social-bilibili-adb-snapshot-builder.test.js +0 -296
  62. package/__tests__/adapters/social-csdn.test.js +0 -175
  63. package/__tests__/adapters/social-dongchedi.test.js +0 -165
  64. package/__tests__/adapters/social-douyin-adb-aweme-detail.test.js +0 -165
  65. package/__tests__/adapters/social-douyin-adb-collector.test.js +0 -254
  66. package/__tests__/adapters/social-douyin-adb-db-extension.test.js +0 -114
  67. package/__tests__/adapters/social-douyin-adb-im-db-parser.test.js +0 -304
  68. package/__tests__/adapters/social-douyin-adb-snapshot-builder.test.js +0 -216
  69. package/__tests__/adapters/social-douyin-adb-usage-profile.test.js +0 -229
  70. package/__tests__/adapters/social-douyin-adb-watch-history.test.js +0 -269
  71. package/__tests__/adapters/social-kuaishou-adb-api-client.test.js +0 -496
  72. package/__tests__/adapters/social-kuaishou-adb-collector.test.js +0 -276
  73. package/__tests__/adapters/social-kuaishou-adb-cookies-extension.test.js +0 -152
  74. package/__tests__/adapters/social-kuaishou-adb-snapshot-builder.test.js +0 -178
  75. package/__tests__/adapters/social-toutiao-adb-account-reader.test.js +0 -135
  76. package/__tests__/adapters/social-toutiao-adb-api-client.test.js +0 -626
  77. package/__tests__/adapters/social-toutiao-adb-article.test.js +0 -155
  78. package/__tests__/adapters/social-toutiao-adb-collector.test.js +0 -378
  79. package/__tests__/adapters/social-toutiao-adb-cookies-extension.test.js +0 -193
  80. package/__tests__/adapters/social-toutiao-adb-snapshot-builder.test.js +0 -196
  81. package/__tests__/adapters/social-toutiao-kuaishou-scaffold.test.js +0 -311
  82. package/__tests__/adapters/social-weibo-adb-api-client.test.js +0 -362
  83. package/__tests__/adapters/social-weibo-adb-collector.test.js +0 -201
  84. package/__tests__/adapters/social-weibo-adb-cookies-extension.test.js +0 -167
  85. package/__tests__/adapters/social-weibo-adb-snapshot-builder.test.js +0 -189
  86. package/__tests__/adapters/social-xiaohongshu-adb-api-client.test.js +0 -431
  87. package/__tests__/adapters/social-xiaohongshu-adb-collector.test.js +0 -207
  88. package/__tests__/adapters/social-xiaohongshu-adb-cookies-extension.test.js +0 -0
  89. package/__tests__/adapters/social-xiaohongshu-adb-sign-provider-injection.test.js +0 -351
  90. package/__tests__/adapters/social-xiaohongshu-adb-sign.test.js +0 -130
  91. package/__tests__/adapters/social-xiaohongshu-adb-snapshot-builder.test.js +0 -200
  92. package/__tests__/adapters/social-zhihu.test.js +0 -246
  93. package/__tests__/adapters/system-data-adapter.test.js +0 -443
  94. package/__tests__/adapters/system-data-android-ingest.test.js +0 -144
  95. package/__tests__/adapters/system-data-android.test.js +0 -519
  96. package/__tests__/adapters/system-data-disclosure.test.js +0 -153
  97. package/__tests__/adapters/travel-12306.test.js +0 -512
  98. package/__tests__/adapters/travel-amap.test.js +0 -219
  99. package/__tests__/adapters/travel-baidu-map.test.js +0 -305
  100. package/__tests__/adapters/travel-base.test.js +0 -205
  101. package/__tests__/adapters/travel-ctrip.test.js +0 -377
  102. package/__tests__/adapters/travel-didi-consumer.test.js +0 -66
  103. package/__tests__/adapters/travel-didi.test.js +0 -204
  104. package/__tests__/adapters/travel-tencent-map.test.js +0 -207
  105. package/__tests__/adapters/travel-tongcheng.test.js +0 -289
  106. package/__tests__/adapters/video-platforms.test.js +0 -152
  107. package/__tests__/adapters/video-xigua.test.js +0 -106
  108. package/__tests__/adapters/vscode.test.js +0 -299
  109. package/__tests__/adapters/wechat-bootstrap.test.js +0 -240
  110. package/__tests__/adapters/wechat-env-probe.test.js +0 -162
  111. package/__tests__/adapters/wechat-frida-agent.test.js +0 -322
  112. package/__tests__/adapters/wechat-frida-integration.test.js +0 -149
  113. package/__tests__/adapters/wechat-frida-key-provider.test.js +0 -188
  114. package/__tests__/adapters/wechat-md5-key-provider.test.js +0 -101
  115. package/__tests__/adapters/wechat-pc-direct-read.test.js +0 -365
  116. package/__tests__/adapters/wechat-pc-group-topic.test.js +0 -63
  117. package/__tests__/adapters/wechat-pc-v4-sidecar.test.js +0 -72
  118. package/__tests__/adapters/weread.test.js +0 -123
  119. package/__tests__/adapters/wework-pc.test.js +0 -124
  120. package/__tests__/adapters/win-recent.test.js +0 -192
  121. package/__tests__/analysis-skills.test.js +0 -754
  122. package/__tests__/analysis.test.js +0 -1845
  123. package/__tests__/audio-ximalaya-snapshot.test.js +0 -279
  124. package/__tests__/batch.test.js +0 -133
  125. package/__tests__/bridges-cc-kg.test.js +0 -231
  126. package/__tests__/bridges-cc-llm.test.js +0 -191
  127. package/__tests__/bridges-cc-rag.test.js +0 -162
  128. package/__tests__/categories.test.js +0 -92
  129. package/__tests__/e2e/ai-chat-cross-source-journey.test.js +0 -213
  130. package/__tests__/e2e/full-user-journey.test.js +0 -188
  131. package/__tests__/e2e/local-data-adapters-cli.e2e.test.js +0 -146
  132. package/__tests__/entity-resolver-ingest-hook.test.js +0 -177
  133. package/__tests__/entity-resolver-stages.test.js +0 -411
  134. package/__tests__/entity-resolver-vault.test.js +0 -249
  135. package/__tests__/entity-resolver.test.js +0 -526
  136. package/__tests__/fitness-keep-snapshot.test.js +0 -224
  137. package/__tests__/fixtures/entity-resolver-200-mock.json +0 -96
  138. package/__tests__/ids.test.js +0 -45
  139. package/__tests__/integration/ai-chat-history-registry.test.js +0 -228
  140. package/__tests__/integration/aichat-wizard-end-to-end.test.js +0 -282
  141. package/__tests__/integration/cross-adapter-pipelines.test.js +0 -396
  142. package/__tests__/integration/local-data-adapters-pipeline.test.js +0 -373
  143. package/__tests__/integration/social-bilibili-pipeline.test.js +0 -261
  144. package/__tests__/integration/wechat-bootstrap-end-to-end.test.js +0 -390
  145. package/__tests__/key-providers.test.js +0 -126
  146. package/__tests__/kg-derive.test.js +0 -219
  147. package/__tests__/llm-client.test.js +0 -122
  148. package/__tests__/longtail-adapters.test.js +0 -281
  149. package/__tests__/messaging-qq-snapshot.test.js +0 -294
  150. package/__tests__/mobile-extractor-encrypted.test.js +0 -460
  151. package/__tests__/mobile-extractor.test.js +0 -288
  152. package/__tests__/mock-adapter.test.js +0 -93
  153. package/__tests__/prompt-builder.test.js +0 -249
  154. package/__tests__/query-parser.test.js +0 -365
  155. package/__tests__/rag-derive.test.js +0 -169
  156. package/__tests__/registry-readiness.test.js +0 -292
  157. package/__tests__/registry.test.js +0 -420
  158. package/__tests__/salvage-ingest.test.js +0 -97
  159. package/__tests__/schemas.test.js +0 -331
  160. package/__tests__/shopping-adapters.test.js +0 -392
  161. package/__tests__/shopping-eleme-snapshot.test.js +0 -454
  162. package/__tests__/shopping-pinduoduo-snapshot.test.js +0 -484
  163. package/__tests__/shopping-snapshot.test.js +0 -438
  164. package/__tests__/shopping-vipshop-snapshot.test.js +0 -425
  165. package/__tests__/shopping-xianyu-snapshot.test.js +0 -451
  166. package/__tests__/sidecar-contacts-cross-validate.test.js +0 -186
  167. package/__tests__/sidecar-supervisor.test.js +0 -128
  168. package/__tests__/sign-providers.test.js +0 -62
  169. package/__tests__/social-adapters.test.js +0 -280
  170. package/__tests__/social-bilibili-snapshot.test.js +0 -278
  171. package/__tests__/social-douban-snapshot.test.js +0 -351
  172. package/__tests__/social-douyin-im-direct-read.test.js +0 -377
  173. package/__tests__/social-douyin-salvage-collector.test.js +0 -98
  174. package/__tests__/social-douyin-salvage-mapper.test.js +0 -90
  175. package/__tests__/social-douyin-snapshot.test.js +0 -256
  176. package/__tests__/social-kuaishou-snapshot.test.js +0 -362
  177. package/__tests__/social-toutiao-snapshot.test.js +0 -366
  178. package/__tests__/social-weibo-snapshot.test.js +0 -234
  179. package/__tests__/social-weibo-sqlite-device.test.js +0 -174
  180. package/__tests__/social-xiaohongshu-snapshot.test.js +0 -232
  181. package/__tests__/sqlite-leaf-salvage.test.js +0 -97
  182. package/__tests__/travel-adapters.test.js +0 -483
  183. package/__tests__/travel-maps-snapshot.test.js +0 -426
  184. package/__tests__/vault-driver-error.test.js +0 -74
  185. package/__tests__/vault-search-helpers.test.js +0 -104
  186. package/__tests__/vault-search.test.js +0 -423
  187. package/__tests__/vault.test.js +0 -767
  188. package/__tests__/wechat-adapter.test.js +0 -594
  189. package/__tests__/whatsapp-adapter.test.js +0 -138
  190. package/scripts/_make-fixture-all.js +0 -126
  191. package/scripts/_make-fixture-contacts.js +0 -84
  192. package/scripts/evaluate-entity-resolver.js +0 -213
  193. package/scripts/run-native-tests-sandbox.sh +0 -55
  194. package/scripts/smoke-phase-5-5.js +0 -196
  195. package/scripts/smoke-phase-5-7.js +0 -181
  196. package/scripts/smoke-system-data-contacts.js +0 -309
  197. package/scripts/smoke-system-data.js +0 -312
  198. package/vitest.config.js +0 -88
@@ -1,484 +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
- PinduoduoAdapter,
11
- SNAPSHOT_SCHEMA_VERSION,
12
- VALID_SNAPSHOT_KINDS,
13
- orderToRecord,
14
- extractOrders,
15
- } = require("../lib/adapters/shopping-pinduoduo");
16
- const { assertAdapter } = require("../lib/adapter-spec");
17
- const { validateBatch } = require("../lib/batch");
18
-
19
- // §2.4c v0.2 — Pinduoduo snapshot-only adapter. Pinduoduo's web API requires
20
- // anti_token JS-VM signing (similar to 抖音 X-Bogus); cookie/api mode is
21
- // deferred to v0.3 via a browser extension that exports order JSON.
22
-
23
- function writeSnapshot(dir, snapshot) {
24
- const p = path.join(dir, "shopping-pinduoduo.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("PinduoduoAdapter snapshot mode", () => {
36
- let tmpDir;
37
- beforeEach(() => {
38
- tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "pdd-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 PinduoduoAdapter();
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 PinduoduoAdapter();
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 (no cookie mode in v0.2)", async () => {
66
- const a = new PinduoduoAdapter({ account: { uid: "u1" } });
67
- const res = await a.authenticate({});
68
- expect(res.ok).toBe(false);
69
- expect(res.reason).toBe("NO_INPUT");
70
- // Honest hint about the anti_token gap
71
- expect(res.message).toMatch(/snapshot mode/);
72
- });
73
-
74
- it("sync() without inputPath throws with anti_token hint", async () => {
75
- const a = new PinduoduoAdapter();
76
- let threw = null;
77
- try {
78
- for await (const _r of a.sync({})) { /* drain */ }
79
- } catch (err) {
80
- threw = err;
81
- }
82
- expect(threw).toBeTruthy();
83
- expect(String(threw.message)).toMatch(/anti_token/);
84
- });
85
-
86
- it("rejects non-JSON inputPath with HTML-parsing-future hint", async () => {
87
- const p = writeRaw(tmpDir, "orders.html", "<html><body>not json</body></html>");
88
- const a = new PinduoduoAdapter();
89
- let threw = null;
90
- try {
91
- for await (const _r of a.sync({ inputPath: p })) { /* drain */ }
92
- } catch (err) {
93
- threw = err;
94
- }
95
- expect(threw).toBeTruthy();
96
- expect(String(threw.message)).toMatch(/snapshot must be JSON/);
97
- });
98
-
99
- it("rejects schemaVersion mismatch", async () => {
100
- const p = writeSnapshot(tmpDir, {
101
- schemaVersion: 99,
102
- snapshottedAt: Date.now(),
103
- events: [],
104
- });
105
- const a = new PinduoduoAdapter();
106
- let threw = null;
107
- try {
108
- for await (const _r of a.sync({ inputPath: p })) { /* drain */ }
109
- } catch (err) {
110
- threw = err;
111
- }
112
- expect(threw).toBeTruthy();
113
- expect(String(threw.message)).toMatch(/schemaVersion mismatch/);
114
- });
115
-
116
- it("empty events array yields nothing (no crash)", async () => {
117
- const p = writeSnapshot(tmpDir, {
118
- schemaVersion: 1,
119
- snapshottedAt: Date.now(),
120
- events: [],
121
- });
122
- const a = new PinduoduoAdapter();
123
- const raws = [];
124
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
125
- expect(raws.length).toBe(0);
126
- });
127
-
128
- it("order event round-trips normalize cleanly", async () => {
129
- const now = Date.now();
130
- const p = writeSnapshot(tmpDir, {
131
- schemaVersion: 1,
132
- snapshottedAt: now,
133
- vendor: "pinduoduo",
134
- account: { uid: "u-alice", displayName: "alice" },
135
- events: [
136
- {
137
- kind: "order",
138
- id: "order-PDD-9001",
139
- capturedAt: now - 1000,
140
- orderId: "PDD-9001",
141
- merchantName: "拼多多旗舰店",
142
- placedAt: now - 86400_000,
143
- paidAt: now - 86400_000 + 5_000,
144
- status: "已发货",
145
- items: [
146
- { name: "纸巾100抽", quantity: 5, unitPrice: 9.9, sku: "sku-001" },
147
- { name: "牙刷4支装", quantity: 1, unitPrice: 12.5 },
148
- ],
149
- totalAmount: { value: 62.0, currency: "CNY" },
150
- recipient: "张三",
151
- shippingAddress: "上海市浦东新区...",
152
- trackingNumber: "SF1234567890",
153
- },
154
- ],
155
- });
156
- const a = new PinduoduoAdapter();
157
- const raws = [];
158
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
159
- expect(raws.length).toBe(1);
160
- expect(raws[0].kind).toBe("order");
161
- expect(raws[0].originalId).toMatch(/^pinduoduo:order:/);
162
- expect(raws[0].originalId).toBe("pinduoduo:order:order-PDD-9001");
163
-
164
- const batch = a.normalize(raws[0]);
165
- expect(validateBatch(batch).valid).toBe(true);
166
- // normalizeOrderRecord emits an Event for the placed order. Confirm the
167
- // shape matches what the LocalVault audit row expects.
168
- expect(batch.events.length).toBeGreaterThan(0);
169
- // Items survive into the normalized record's extras / event content
170
- expect(JSON.stringify(batch)).toContain("纸巾100抽");
171
- // Recipient / address survive
172
- expect(JSON.stringify(batch)).toContain("张三");
173
- });
174
-
175
- it("status mapping handles pinduoduo-typical strings", async () => {
176
- const now = Date.now();
177
- const cases = [
178
- { status: "已发货", expect: "shipped" },
179
- { status: "已收货", expect: "delivered" },
180
- { status: "已完成", expect: "delivered" },
181
- { status: "退款", expect: "refunded" },
182
- { status: "已关闭", expect: "cancelled" },
183
- { status: "待付款", expect: "placed" },
184
- ];
185
- for (const c of cases) {
186
- const p = writeSnapshot(tmpDir, {
187
- schemaVersion: 1,
188
- snapshottedAt: now,
189
- events: [
190
- {
191
- kind: "order", id: `o-${c.status}`,
192
- orderId: `o-${c.status}`, merchantName: "m",
193
- placedAt: now, paidAt: now, status: c.status,
194
- items: [{ name: "x", quantity: 1, unitPrice: 1 }],
195
- totalAmount: { value: 1, currency: "CNY" },
196
- },
197
- ],
198
- });
199
- const a = new PinduoduoAdapter();
200
- const raws = [];
201
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
202
- const batch = a.normalize(raws[0]);
203
- // Find the order event and check status survives via extras (the
204
- // shopping-base normalizer puts mapped status into the event)
205
- const stringified = JSON.stringify(batch);
206
- // Status maps to one of the canonical enum values. We can't easily
207
- // get the field path without depending on shopping-base internals,
208
- // so verify the canonical value appears somewhere in the batch.
209
- expect(stringified).toContain(c.expect);
210
- }
211
- });
212
-
213
- it("respects per-kind include opt-out", async () => {
214
- const now = Date.now();
215
- const p = writeSnapshot(tmpDir, {
216
- schemaVersion: 1,
217
- snapshottedAt: now,
218
- events: [
219
- { kind: "order", id: "o1", orderId: "o1", merchantName: "m", placedAt: now,
220
- items: [], totalAmount: { value: 1, currency: "CNY" } },
221
- ],
222
- });
223
- const a = new PinduoduoAdapter();
224
- const raws = [];
225
- for await (const r of a.sync({ inputPath: p, include: { order: false } })) {
226
- raws.push(r);
227
- }
228
- expect(raws.length).toBe(0);
229
- });
230
-
231
- it("respects opts.limit", async () => {
232
- const now = Date.now();
233
- const events = Array.from({ length: 5 }, (_, i) => ({
234
- kind: "order", id: `o${i}`, orderId: `o${i}`, merchantName: "m",
235
- placedAt: now - i * 1000,
236
- items: [], totalAmount: { value: 1, currency: "CNY" },
237
- }));
238
- const p = writeSnapshot(tmpDir, { schemaVersion: 1, snapshottedAt: now, events });
239
- const a = new PinduoduoAdapter();
240
- const raws = [];
241
- for await (const r of a.sync({ inputPath: p, limit: 2 })) raws.push(r);
242
- expect(raws.length).toBe(2);
243
- });
244
-
245
- it("filters out unknown kinds (forward compat)", async () => {
246
- const now = Date.now();
247
- const p = writeSnapshot(tmpDir, {
248
- schemaVersion: 1,
249
- snapshottedAt: now,
250
- events: [
251
- { kind: "order", id: "o1", orderId: "o1", merchantName: "m", placedAt: now,
252
- items: [], totalAmount: { value: 1, currency: "CNY" } },
253
- { kind: "refund", id: "r1", orderId: "o1" }, // not yet supported
254
- { kind: "future-kind", id: "x" },
255
- ],
256
- });
257
- const a = new PinduoduoAdapter();
258
- const raws = [];
259
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
260
- expect(raws.length).toBe(1);
261
- expect(raws[0].kind).toBe("order");
262
- });
263
-
264
- it("snapshottedAt fallback when event capturedAt missing", async () => {
265
- const ts = 1700000000000;
266
- const p = writeSnapshot(tmpDir, {
267
- schemaVersion: 1,
268
- snapshottedAt: ts,
269
- events: [
270
- { kind: "order", id: "o1", orderId: "o1", merchantName: "m", items: [],
271
- totalAmount: { value: 1, currency: "CNY" } },
272
- ],
273
- });
274
- const a = new PinduoduoAdapter();
275
- const raws = [];
276
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
277
- expect(raws[0].capturedAt).toBe(ts);
278
- });
279
-
280
- it("advertises both snapshot and cookie-api capabilities", () => {
281
- const a = new PinduoduoAdapter();
282
- expect(a.capabilities).toContain("sync:snapshot");
283
- expect(a.capabilities).toContain("sync:cookie-api");
284
- expect(assertAdapter(a).ok).toBe(true);
285
- });
286
-
287
- it("snapshotEventToRecord handles snake_case goods_name/goods_price fallback", async () => {
288
- // Pinduoduo's internal field names use snake_case (goods_name, goods_price,
289
- // goods_count); the normalizer falls back to those if camelCase is absent.
290
- const now = Date.now();
291
- const p = writeSnapshot(tmpDir, {
292
- schemaVersion: 1,
293
- snapshottedAt: now,
294
- events: [
295
- {
296
- kind: "order", id: "o-snake",
297
- orderId: "o-snake", merchantName: "店铺A",
298
- placedAt: now, paidAt: now,
299
- items: [
300
- { goods_name: "纸巾", goods_count: 3, goods_price: 5.5, sku_id: "sk1" },
301
- ],
302
- totalAmount: { value: 16.5, currency: "CNY" },
303
- },
304
- ],
305
- });
306
- const a = new PinduoduoAdapter();
307
- const raws = [];
308
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
309
- const batch = a.normalize(raws[0]);
310
- expect(JSON.stringify(batch)).toContain("纸巾");
311
- });
312
- });
313
-
314
- // §2.4c v0.3 — Pinduoduo cookie-api mode. The actual HTTP + anti_token signing
315
- // is injected (fetchFn / signProvider) so the adapter stays pure-Node.
316
- describe("PinduoduoAdapter cookie-api mode", () => {
317
- it("authenticate(cookie) ok when uid + cookies present", async () => {
318
- const a = new PinduoduoAdapter({ account: { uid: "u-1", cookies: "PDDAccessToken=ok" } });
319
- const res = await a.authenticate();
320
- expect(res.ok).toBe(true);
321
- expect(res.mode).toBe("cookie");
322
- expect(res.account).toBe("u-1");
323
- });
324
-
325
- it("authenticate(cookie) requires account.uid", async () => {
326
- const a = new PinduoduoAdapter({ account: { cookies: "PDDAccessToken=ok" } });
327
- const res = await a.authenticate();
328
- expect(res.ok).toBe(false);
329
- expect(res.reason).toBe("NO_ACCOUNT_UID");
330
- });
331
-
332
- it("sync yields normalized records from fetchFn fixture (cents → 元)", async () => {
333
- const fetchFn = async () => ({
334
- orders: [
335
- {
336
- order_sn: "PDD-COOKIE-1",
337
- mall_name: "拼多多旗舰店",
338
- order_status_prompt: "已发货",
339
- order_amount: 6200, // 分 → 62.00 元
340
- order_time: 1700000000, // sec
341
- pay_time: 1700000010,
342
- receive_name: "李四",
343
- address: "广州市天河区...",
344
- goods_list: [
345
- { goods_name: "纸巾100抽", goods_number: 5, goods_price: 990, sku_id: "sk1" },
346
- ],
347
- },
348
- ],
349
- });
350
- const a = new PinduoduoAdapter({
351
- account: { uid: "u-1", cookies: "PDDAccessToken=ok" },
352
- fetchFn,
353
- });
354
- const raws = [];
355
- for await (const r of a.sync({ sinceWatermark: 0 })) raws.push(r);
356
- expect(raws.length).toBe(1);
357
- expect(raws[0].originalId).toBe("PDD-COOKIE-1");
358
- expect(raws[0].payload.record.totalAmount.value).toBe(62);
359
- expect(raws[0].payload.record.items[0].unitPrice).toBe(9.9);
360
- expect(raws[0].payload.record.status).toBe("shipped");
361
-
362
- const batch = a.normalize(raws[0]);
363
- expect(validateBatch(batch).valid).toBe(true);
364
- expect(JSON.stringify(batch)).toContain("纸巾100抽");
365
- expect(JSON.stringify(batch)).toContain("李四");
366
- });
367
-
368
- it("invokes signProvider and passes antiToken to fetchFn", async () => {
369
- let seenAntiToken = null;
370
- const signProvider = async () => "ANTI-TOKEN-XYZ";
371
- const fetchFn = async (opts) => {
372
- seenAntiToken = opts.antiToken;
373
- return { orders: [] };
374
- };
375
- const a = new PinduoduoAdapter({
376
- account: { uid: "u-1", cookies: "PDDAccessToken=ok" },
377
- fetchFn,
378
- signProvider,
379
- });
380
- for await (const _r of a.sync({ sinceWatermark: 0 })) { /* drain */ }
381
- expect(seenAntiToken).toBe("ANTI-TOKEN-XYZ");
382
- });
383
-
384
- it("passes antiToken: null when no signProvider configured", async () => {
385
- let seen = "unset";
386
- const fetchFn = async (opts) => {
387
- seen = opts.antiToken;
388
- return { orders: [] };
389
- };
390
- const a = new PinduoduoAdapter({
391
- account: { uid: "u-1", cookies: "PDDAccessToken=ok" },
392
- fetchFn,
393
- });
394
- for await (const _r of a.sync({ sinceWatermark: 0 })) { /* drain */ }
395
- expect(seen).toBe(null);
396
- });
397
-
398
- it("paginates and stops at sinceWatermark", async () => {
399
- const pages = {
400
- 1: [
401
- { order_sn: "p1-a", mall_name: "m", order_time: 1700000000, order_amount: 100,
402
- goods_list: [] },
403
- { order_sn: "p1-b", mall_name: "m", order_time: 1699000000, order_amount: 100,
404
- goods_list: [] },
405
- ],
406
- 2: [
407
- { order_sn: "p2-a", mall_name: "m", order_time: 1698000000, order_amount: 100,
408
- goods_list: [] },
409
- ],
410
- };
411
- const seenPages = [];
412
- const fetchFn = async (opts) => {
413
- seenPages.push(opts.query.pageNumber);
414
- return { orders: pages[opts.query.pageNumber] || [] };
415
- };
416
- const a = new PinduoduoAdapter({
417
- account: { uid: "u-1", cookies: "PDDAccessToken=ok" },
418
- fetchFn,
419
- });
420
- const raws = [];
421
- // watermark between 1699000000s and 1700000000s → stops on the older order
422
- for await (const r of a.sync({ sinceWatermark: 1699500000 * 1000, pageSize: 2 })) {
423
- raws.push(r);
424
- }
425
- expect(raws.map((r) => r.originalId)).toEqual(["p1-a"]);
426
- // stopped inside page 1 — never fetched page 2
427
- expect(seenPages).toEqual([1]);
428
- });
429
-
430
- it("respects per-kind include opt-out in cookie mode", async () => {
431
- let called = false;
432
- const fetchFn = async () => {
433
- called = true;
434
- return { orders: [] };
435
- };
436
- const a = new PinduoduoAdapter({
437
- account: { uid: "u-1", cookies: "PDDAccessToken=ok" },
438
- fetchFn,
439
- });
440
- const raws = [];
441
- for await (const r of a.sync({ include: { order: false } })) raws.push(r);
442
- expect(raws.length).toBe(0);
443
- expect(called).toBe(false);
444
- });
445
-
446
- it("orderToRecord maps snake_case PDD fields with cents conversion", () => {
447
- const rec = orderToRecord({
448
- order_sn: "PDD-9",
449
- mall_name: "店铺B",
450
- order_status: 4, // numeric → 已完成 → delivered
451
- order_amount: 1650,
452
- order_time: 1700000000,
453
- goods_list: [{ goods_name: "牙刷", goods_number: 2, goods_price: 825 }],
454
- });
455
- expect(rec.orderId).toBe("PDD-9");
456
- expect(rec.merchantName).toBe("店铺B");
457
- expect(rec.status).toBe("delivered");
458
- expect(rec.totalAmount.value).toBe(16.5);
459
- expect(rec.items[0].unitPrice).toBe(8.25);
460
- expect(rec.extras.capturedBy).toBe("cookie-api");
461
- });
462
-
463
- it("extractOrders tolerates nested response shapes", () => {
464
- expect(extractOrders({ orders: [1] })).toEqual([1]);
465
- expect(extractOrders({ order_list: [2] })).toEqual([2]);
466
- expect(extractOrders({ list: [3] })).toEqual([3]);
467
- expect(extractOrders({ result: { order_list: [4] } })).toEqual([4]);
468
- expect(extractOrders({ result: { list: [5] } })).toEqual([5]);
469
- expect(extractOrders({})).toEqual([]);
470
- expect(extractOrders(null)).toEqual([]);
471
- });
472
-
473
- it("default fetchFn throws a legible error when cookie mode used without injection", async () => {
474
- const a = new PinduoduoAdapter({ account: { uid: "u-1", cookies: "PDDAccessToken=ok" } });
475
- let threw = null;
476
- try {
477
- for await (const _r of a.sync({ sinceWatermark: 0 })) { /* drain */ }
478
- } catch (err) {
479
- threw = err;
480
- }
481
- expect(threw).toBeTruthy();
482
- expect(String(threw.message)).toMatch(/no fetchFn configured/);
483
- });
484
- });