@chainlesschain/personal-data-hub 0.4.23 → 0.4.25

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 (39) hide show
  1. package/__tests__/adapters/bank-family.test.js +125 -0
  2. package/__tests__/adapters/car-mercedesme.test.js +74 -0
  3. package/__tests__/adapters/finance-dcep.test.js +74 -0
  4. package/__tests__/adapters/fitness-joyrun.test.js +82 -0
  5. package/__tests__/adapters/gov-12123.test.js +103 -0
  6. package/__tests__/adapters/gov-ixiamen.test.js +2 -2
  7. package/__tests__/adapters/music-qq.test.js +112 -0
  8. package/__tests__/adapters/reading-family.test.js +108 -0
  9. package/__tests__/adapters/travel-didi-consumer.test.js +66 -0
  10. package/__tests__/audio-ximalaya-snapshot.test.js +279 -0
  11. package/__tests__/fitness-keep-snapshot.test.js +224 -0
  12. package/__tests__/shopping-eleme-snapshot.test.js +454 -0
  13. package/__tests__/shopping-vipshop-snapshot.test.js +425 -0
  14. package/__tests__/shopping-xianyu-snapshot.test.js +451 -0
  15. package/__tests__/social-douban-snapshot.test.js +351 -0
  16. package/lib/adapter-guide.js +19 -1
  17. package/lib/adapters/_bank-base.js +405 -0
  18. package/lib/adapters/_reading-base.js +315 -0
  19. package/lib/adapters/audio-ximalaya/index.js +414 -0
  20. package/lib/adapters/bank-bankcomm/index.js +27 -0
  21. package/lib/adapters/bank-boc/index.js +26 -0
  22. package/lib/adapters/bank-cmbc/index.js +26 -0
  23. package/lib/adapters/bank-icbc/index.js +27 -0
  24. package/lib/adapters/car-mercedesme/index.js +225 -0
  25. package/lib/adapters/finance-dcep/index.js +302 -0
  26. package/lib/adapters/fitness-joyrun/index.js +295 -0
  27. package/lib/adapters/fitness-keep/index.js +343 -0
  28. package/lib/adapters/gov-12123/index.js +391 -0
  29. package/lib/adapters/gov-ixiamen/index.js +17 -10
  30. package/lib/adapters/music-qq/index.js +372 -0
  31. package/lib/adapters/reading-fanqie/index.js +61 -0
  32. package/lib/adapters/reading-qimao/index.js +61 -0
  33. package/lib/adapters/shopping-eleme/index.js +441 -0
  34. package/lib/adapters/shopping-vipshop/index.js +429 -0
  35. package/lib/adapters/shopping-xianyu/index.js +454 -0
  36. package/lib/adapters/social-douban/index.js +564 -0
  37. package/lib/adapters/travel-didi-consumer/index.js +148 -0
  38. package/lib/index.js +36 -0
  39. package/package.json +1 -1
@@ -0,0 +1,425 @@
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
+ VipshopAdapter,
11
+ SNAPSHOT_SCHEMA_VERSION,
12
+ VALID_SNAPSHOT_KINDS,
13
+ orderToRecord,
14
+ extractOrders,
15
+ } = require("../lib/adapters/shopping-vipshop");
16
+ const { assertAdapter } = require("../lib/adapter-spec");
17
+ const { validateBatch } = require("../lib/batch");
18
+
19
+ // 唯品会 (VIP.com) — flash-sale e-commerce; mirrors shopping-eleme (snapshot +
20
+ // cookie-api, 元 amounts). VIP signing is injected (signProvider) so the adapter
21
+ // stays pure-Node.
22
+
23
+ function writeSnapshot(dir, snapshot) {
24
+ const p = path.join(dir, "shopping-vipshop.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("VipshopAdapter snapshot mode", () => {
36
+ let tmpDir;
37
+ beforeEach(() => {
38
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "vip-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, { schemaVersion: 1, snapshottedAt: Date.now(), events: [] });
48
+ const a = new VipshopAdapter();
49
+ const res = await a.authenticate({ inputPath: p });
50
+ expect(res.ok).toBe(true);
51
+ expect(res.mode).toBe("snapshot-file");
52
+ });
53
+
54
+ it("authenticate(inputPath) fails when path unreadable", async () => {
55
+ const a = new VipshopAdapter();
56
+ const res = await a.authenticate({ inputPath: path.join(tmpDir, "missing.json") });
57
+ expect(res.ok).toBe(false);
58
+ expect(res.reason).toBe("INPUT_PATH_UNREADABLE");
59
+ });
60
+
61
+ it("authenticate() with no inputPath returns NO_INPUT", async () => {
62
+ const a = new VipshopAdapter();
63
+ const res = await a.authenticate({});
64
+ expect(res.ok).toBe(false);
65
+ expect(res.reason).toBe("NO_INPUT");
66
+ expect(res.message).toMatch(/snapshot mode/);
67
+ });
68
+
69
+ it("sync() without inputPath throws with signing hint", async () => {
70
+ const a = new VipshopAdapter();
71
+ let threw = null;
72
+ try {
73
+ for await (const _r of a.sync({})) { /* drain */ }
74
+ } catch (err) {
75
+ threw = err;
76
+ }
77
+ expect(threw).toBeTruthy();
78
+ expect(String(threw.message)).toMatch(/signProvider/);
79
+ });
80
+
81
+ it("rejects non-JSON inputPath", async () => {
82
+ const p = writeRaw(tmpDir, "orders.html", "<html>not json</html>");
83
+ const a = new VipshopAdapter();
84
+ let threw = null;
85
+ try {
86
+ for await (const _r of a.sync({ inputPath: p })) { /* drain */ }
87
+ } catch (err) {
88
+ threw = err;
89
+ }
90
+ expect(threw).toBeTruthy();
91
+ expect(String(threw.message)).toMatch(/snapshot must be JSON/);
92
+ });
93
+
94
+ it("rejects schemaVersion mismatch", async () => {
95
+ const p = writeSnapshot(tmpDir, { schemaVersion: 99, snapshottedAt: Date.now(), events: [] });
96
+ const a = new VipshopAdapter();
97
+ let threw = null;
98
+ try {
99
+ for await (const _r of a.sync({ inputPath: p })) { /* drain */ }
100
+ } catch (err) {
101
+ threw = err;
102
+ }
103
+ expect(threw).toBeTruthy();
104
+ expect(String(threw.message)).toMatch(/schemaVersion mismatch/);
105
+ });
106
+
107
+ it("empty events array yields nothing (no crash)", async () => {
108
+ const p = writeSnapshot(tmpDir, { schemaVersion: 1, snapshottedAt: Date.now(), events: [] });
109
+ const a = new VipshopAdapter();
110
+ const raws = [];
111
+ for await (const r of a.sync({ inputPath: p })) raws.push(r);
112
+ expect(raws.length).toBe(0);
113
+ });
114
+
115
+ it("order event round-trips normalize cleanly", async () => {
116
+ const now = Date.now();
117
+ const p = writeSnapshot(tmpDir, {
118
+ schemaVersion: 1,
119
+ snapshottedAt: now,
120
+ vendor: "vipshop",
121
+ account: { userId: "u-alice", displayName: "alice" },
122
+ events: [
123
+ {
124
+ kind: "order",
125
+ id: "order-VIP-9001",
126
+ capturedAt: now - 1000,
127
+ orderId: "VIP-9001",
128
+ merchantName: "雅诗兰黛官方旗舰",
129
+ placedAt: now - 86400_000,
130
+ paidAt: now - 86400_000 + 5_000,
131
+ status: "已收货",
132
+ items: [
133
+ { name: "小棕瓶精华 50ml", quantity: 1, unitPrice: 680.0 },
134
+ { name: "面膜 5片装", quantity: 2, unitPrice: 99.0 },
135
+ ],
136
+ totalAmount: { value: 878.0, currency: "CNY" },
137
+ recipient: "张三",
138
+ shippingAddress: "上海市浦东新区...",
139
+ },
140
+ ],
141
+ });
142
+ const a = new VipshopAdapter();
143
+ const raws = [];
144
+ for await (const r of a.sync({ inputPath: p })) raws.push(r);
145
+ expect(raws.length).toBe(1);
146
+ expect(raws[0].originalId).toBe("vipshop:order:order-VIP-9001");
147
+
148
+ const batch = a.normalize(raws[0]);
149
+ expect(validateBatch(batch).valid).toBe(true);
150
+ expect(batch.events.length).toBeGreaterThan(0);
151
+ expect(JSON.stringify(batch)).toContain("小棕瓶精华 50ml");
152
+ expect(JSON.stringify(batch)).toContain("张三");
153
+ expect(JSON.stringify(batch)).toContain("delivered");
154
+ });
155
+
156
+ it("status mapping handles vip-typical strings", async () => {
157
+ const now = Date.now();
158
+ const cases = [
159
+ { status: "已发货", expect: "shipped" },
160
+ { status: "待收货", expect: "shipped" },
161
+ { status: "已收货", expect: "delivered" },
162
+ { status: "交易成功", expect: "delivered" },
163
+ { status: "退款成功", expect: "refunded" },
164
+ { status: "已取消", expect: "cancelled" },
165
+ { status: "待付款", expect: "placed" },
166
+ ];
167
+ for (const c of cases) {
168
+ const p = writeSnapshot(tmpDir, {
169
+ schemaVersion: 1,
170
+ snapshottedAt: now,
171
+ events: [
172
+ { kind: "order", id: `o-${c.status}`, orderId: `o-${c.status}`, merchantName: "m",
173
+ placedAt: now, paidAt: now, status: c.status,
174
+ items: [{ name: "x", quantity: 1, unitPrice: 1 }],
175
+ totalAmount: { value: 1, currency: "CNY" } },
176
+ ],
177
+ });
178
+ const a = new VipshopAdapter();
179
+ const raws = [];
180
+ for await (const r of a.sync({ inputPath: p })) raws.push(r);
181
+ const batch = a.normalize(raws[0]);
182
+ expect(JSON.stringify(batch)).toContain(c.expect);
183
+ }
184
+ });
185
+
186
+ it("respects per-kind include opt-out", async () => {
187
+ const now = Date.now();
188
+ const p = writeSnapshot(tmpDir, {
189
+ schemaVersion: 1,
190
+ snapshottedAt: now,
191
+ events: [
192
+ { kind: "order", id: "o1", orderId: "o1", merchantName: "m", placedAt: now,
193
+ items: [], totalAmount: { value: 1, currency: "CNY" } },
194
+ ],
195
+ });
196
+ const a = new VipshopAdapter();
197
+ const raws = [];
198
+ for await (const r of a.sync({ inputPath: p, include: { order: false } })) raws.push(r);
199
+ expect(raws.length).toBe(0);
200
+ });
201
+
202
+ it("respects opts.limit", async () => {
203
+ const now = Date.now();
204
+ const events = Array.from({ length: 5 }, (_, i) => ({
205
+ kind: "order", id: `o${i}`, orderId: `o${i}`, merchantName: "m",
206
+ placedAt: now - i * 1000, items: [], totalAmount: { value: 1, currency: "CNY" },
207
+ }));
208
+ const p = writeSnapshot(tmpDir, { schemaVersion: 1, snapshottedAt: now, events });
209
+ const a = new VipshopAdapter();
210
+ const raws = [];
211
+ for await (const r of a.sync({ inputPath: p, limit: 2 })) raws.push(r);
212
+ expect(raws.length).toBe(2);
213
+ });
214
+
215
+ it("filters out unknown kinds (forward compat)", async () => {
216
+ const now = Date.now();
217
+ const p = writeSnapshot(tmpDir, {
218
+ schemaVersion: 1,
219
+ snapshottedAt: now,
220
+ events: [
221
+ { kind: "order", id: "o1", orderId: "o1", merchantName: "m", placedAt: now,
222
+ items: [], totalAmount: { value: 1, currency: "CNY" } },
223
+ { kind: "browse", id: "b1" },
224
+ { kind: "future-kind", id: "x" },
225
+ ],
226
+ });
227
+ const a = new VipshopAdapter();
228
+ const raws = [];
229
+ for await (const r of a.sync({ inputPath: p })) raws.push(r);
230
+ expect(raws.length).toBe(1);
231
+ expect(raws[0].kind).toBe("order");
232
+ });
233
+
234
+ it("snapshottedAt fallback when event capturedAt missing", async () => {
235
+ const ts = 1700000000000;
236
+ const p = writeSnapshot(tmpDir, {
237
+ schemaVersion: 1,
238
+ snapshottedAt: ts,
239
+ events: [
240
+ { kind: "order", id: "o1", orderId: "o1", merchantName: "m", items: [],
241
+ totalAmount: { value: 1, currency: "CNY" } },
242
+ ],
243
+ });
244
+ const a = new VipshopAdapter();
245
+ const raws = [];
246
+ for await (const r of a.sync({ inputPath: p })) raws.push(r);
247
+ expect(raws[0].capturedAt).toBe(ts);
248
+ });
249
+
250
+ it("advertises both snapshot and cookie-api capabilities", () => {
251
+ const a = new VipshopAdapter();
252
+ expect(a.capabilities).toContain("sync:snapshot");
253
+ expect(a.capabilities).toContain("sync:cookie-api");
254
+ expect(assertAdapter(a).ok).toBe(true);
255
+ });
256
+ });
257
+
258
+ describe("VipshopAdapter cookie-api mode", () => {
259
+ it("authenticate(cookie) ok when userId + cookies present", async () => {
260
+ const a = new VipshopAdapter({ account: { userId: "u-1", cookies: "vip_access_token=ok" } });
261
+ const res = await a.authenticate();
262
+ expect(res.ok).toBe(true);
263
+ expect(res.mode).toBe("cookie");
264
+ expect(res.account).toBe("u-1");
265
+ });
266
+
267
+ it("authenticate(cookie) requires account.userId", async () => {
268
+ const a = new VipshopAdapter({ account: { cookies: "vip_access_token=ok" } });
269
+ const res = await a.authenticate();
270
+ expect(res.ok).toBe(false);
271
+ expect(res.reason).toBe("NO_ACCOUNT_USER_ID");
272
+ });
273
+
274
+ it("sync yields normalized records from fetchFn fixture (元)", async () => {
275
+ const fetchFn = async () => ({
276
+ data: {
277
+ orders: [
278
+ {
279
+ order_sn: "VIP-COOKIE-1",
280
+ brand_name: "Nike 官方",
281
+ order_status_name: "已收货",
282
+ money: 499.0,
283
+ add_time: 1700000000, // sec
284
+ pay_time: 1700000010,
285
+ consignee: "李四",
286
+ address: "广州市天河区...",
287
+ goods_list: [
288
+ { goods_name: "运动鞋", goods_num: 1, vipshop_price: 499.0, size_id: "42" },
289
+ ],
290
+ },
291
+ ],
292
+ },
293
+ });
294
+ const a = new VipshopAdapter({ account: { userId: "u-1", cookies: "vip_access_token=ok" }, fetchFn });
295
+ const raws = [];
296
+ for await (const r of a.sync({ sinceWatermark: 0 })) raws.push(r);
297
+ expect(raws.length).toBe(1);
298
+ expect(raws[0].originalId).toBe("VIP-COOKIE-1");
299
+ expect(raws[0].payload.record.merchantName).toBe("Nike 官方");
300
+ expect(raws[0].payload.record.totalAmount.value).toBe(499.0);
301
+ expect(raws[0].payload.record.items[0].unitPrice).toBe(499.0);
302
+ expect(raws[0].payload.record.status).toBe("delivered");
303
+
304
+ const batch = a.normalize(raws[0]);
305
+ expect(validateBatch(batch).valid).toBe(true);
306
+ expect(JSON.stringify(batch)).toContain("运动鞋");
307
+ expect(JSON.stringify(batch)).toContain("李四");
308
+ });
309
+
310
+ it("invokes signProvider and passes sign to fetchFn", async () => {
311
+ let seenSign = null;
312
+ const signProvider = async () => "SIGN-XYZ";
313
+ const fetchFn = async (opts) => {
314
+ seenSign = opts.sign;
315
+ return { orders: [] };
316
+ };
317
+ const a = new VipshopAdapter({
318
+ account: { userId: "u-1", cookies: "vip_access_token=ok" },
319
+ fetchFn,
320
+ signProvider,
321
+ });
322
+ for await (const _r of a.sync({ sinceWatermark: 0 })) { /* drain */ }
323
+ expect(seenSign).toBe("SIGN-XYZ");
324
+ });
325
+
326
+ it("passes sign: null when no signProvider configured", async () => {
327
+ let seen = "unset";
328
+ const fetchFn = async (opts) => {
329
+ seen = opts.sign;
330
+ return { orders: [] };
331
+ };
332
+ const a = new VipshopAdapter({ account: { userId: "u-1", cookies: "vip_access_token=ok" }, fetchFn });
333
+ for await (const _r of a.sync({ sinceWatermark: 0 })) { /* drain */ }
334
+ expect(seen).toBe(null);
335
+ });
336
+
337
+ it("paginates and stops at sinceWatermark", async () => {
338
+ const pages = {
339
+ 1: [
340
+ { order_sn: "p1-a", brand_name: "m", add_time: 1700000000, money: 10, goods_list: [] },
341
+ { order_sn: "p1-b", brand_name: "m", add_time: 1699000000, money: 10, goods_list: [] },
342
+ ],
343
+ 2: [{ order_sn: "p2-a", brand_name: "m", add_time: 1698000000, money: 10, goods_list: [] }],
344
+ };
345
+ const seenPages = [];
346
+ const fetchFn = async (opts) => {
347
+ seenPages.push(opts.query.page);
348
+ return { orders: pages[opts.query.page] || [] };
349
+ };
350
+ const a = new VipshopAdapter({ account: { userId: "u-1", cookies: "vip_access_token=ok" }, fetchFn });
351
+ const raws = [];
352
+ for await (const r of a.sync({ sinceWatermark: 1699500000 * 1000, pageSize: 2 })) {
353
+ raws.push(r);
354
+ }
355
+ expect(raws.map((r) => r.originalId)).toEqual(["p1-a"]);
356
+ expect(seenPages).toEqual([1]);
357
+ });
358
+
359
+ it("respects per-kind include opt-out in cookie mode (no fetch)", async () => {
360
+ let called = false;
361
+ const fetchFn = async () => {
362
+ called = true;
363
+ return { orders: [] };
364
+ };
365
+ const a = new VipshopAdapter({ account: { userId: "u-1", cookies: "vip_access_token=ok" }, fetchFn });
366
+ const raws = [];
367
+ for await (const r of a.sync({ include: { order: false } })) raws.push(r);
368
+ expect(raws.length).toBe(0);
369
+ expect(called).toBe(false);
370
+ });
371
+
372
+ it("orderToRecord maps vip fields (元 amounts, brand as merchant)", () => {
373
+ const rec = orderToRecord({
374
+ order_sn: "VIP-9",
375
+ brand_name: "兰蔻",
376
+ order_status_name: "已完成",
377
+ money: 320.0,
378
+ add_time: 1700000000,
379
+ goods_list: [{ goods_name: "粉底液", goods_num: 1, vipshop_price: 320.0 }],
380
+ });
381
+ expect(rec.orderId).toBe("VIP-9");
382
+ expect(rec.merchantName).toBe("兰蔻");
383
+ expect(rec.status).toBe("delivered");
384
+ expect(rec.totalAmount.value).toBe(320.0);
385
+ expect(rec.items[0].name).toBe("粉底液");
386
+ expect(rec.extras.capturedBy).toBe("cookie-api");
387
+ });
388
+
389
+ it("extractOrders tolerates nested response shapes", () => {
390
+ expect(extractOrders({ orders: [1] })).toEqual([1]);
391
+ expect(extractOrders({ orderList: [2] })).toEqual([2]);
392
+ expect(extractOrders({ data: { orders: [3] } })).toEqual([3]);
393
+ expect(extractOrders({ data: { orderList: [4] } })).toEqual([4]);
394
+ expect(extractOrders([5])).toEqual([5]);
395
+ expect(extractOrders({})).toEqual([]);
396
+ expect(extractOrders(null)).toEqual([]);
397
+ });
398
+
399
+ it("uses opts.ordersUrl override when provided", async () => {
400
+ let seenUrl = null;
401
+ const fetchFn = async (opts) => {
402
+ seenUrl = opts.url;
403
+ return { orders: [] };
404
+ };
405
+ const a = new VipshopAdapter({
406
+ account: { userId: "u-1", cookies: "vip_access_token=ok" },
407
+ fetchFn,
408
+ ordersUrl: "https://custom.example/orders",
409
+ });
410
+ for await (const _r of a.sync({ sinceWatermark: 0 })) { /* drain */ }
411
+ expect(seenUrl).toBe("https://custom.example/orders");
412
+ });
413
+
414
+ it("default fetchFn throws a legible error when cookie mode used without injection", async () => {
415
+ const a = new VipshopAdapter({ account: { userId: "u-1", cookies: "vip_access_token=ok" } });
416
+ let threw = null;
417
+ try {
418
+ for await (const _r of a.sync({ sinceWatermark: 0 })) { /* drain */ }
419
+ } catch (err) {
420
+ threw = err;
421
+ }
422
+ expect(threw).toBeTruthy();
423
+ expect(String(threw.message)).toMatch(/no fetchFn configured/);
424
+ });
425
+ });