@chainlesschain/personal-data-hub 0.2.2 → 0.2.4
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.
- package/__tests__/adapters/social-toutiao-kuaishou-scaffold.test.js +58 -16
- package/__tests__/analysis.test.js +1 -1
- package/__tests__/longtail-adapters.test.js +67 -16
- package/__tests__/messaging-qq-snapshot.test.js +294 -0
- package/__tests__/shopping-pinduoduo-snapshot.test.js +302 -0
- package/__tests__/shopping-snapshot.test.js +438 -0
- package/__tests__/social-adapters.test.js +28 -3
- package/__tests__/social-douyin-snapshot.test.js +253 -0
- package/__tests__/social-kuaishou-snapshot.test.js +309 -0
- package/__tests__/social-toutiao-snapshot.test.js +314 -0
- package/__tests__/social-weibo-snapshot.test.js +234 -0
- package/__tests__/social-xiaohongshu-snapshot.test.js +232 -0
- package/__tests__/travel-maps-snapshot.test.js +426 -0
- package/__tests__/vault-driver-error.test.js +74 -0
- package/lib/adapters/messaging-qq/index.js +498 -92
- package/lib/adapters/shopping-jd/index.js +228 -25
- package/lib/adapters/shopping-meituan/index.js +222 -26
- package/lib/adapters/shopping-pinduoduo/index.js +275 -0
- package/lib/adapters/social-douyin/index.js +454 -63
- package/lib/adapters/social-kuaishou/index.js +379 -127
- package/lib/adapters/social-toutiao/index.js +400 -130
- package/lib/adapters/social-weibo/index.js +393 -95
- package/lib/adapters/social-xiaohongshu/index.js +389 -49
- package/lib/adapters/travel-baidu-map/index.js +286 -26
- package/lib/adapters/travel-tencent-map/index.js +414 -0
- package/lib/index.js +5 -1
- package/lib/vault.js +60 -8
- package/package.json +2 -1
|
@@ -0,0 +1,302 @@
|
|
|
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
|
+
} = require("../lib/adapters/shopping-pinduoduo");
|
|
14
|
+
const { validateBatch } = require("../lib/batch");
|
|
15
|
+
|
|
16
|
+
// §2.4c v0.2 — Pinduoduo snapshot-only adapter. Pinduoduo's web API requires
|
|
17
|
+
// anti_token JS-VM signing (similar to 抖音 X-Bogus); cookie/api mode is
|
|
18
|
+
// deferred to v0.3 via a browser extension that exports order JSON.
|
|
19
|
+
|
|
20
|
+
function writeSnapshot(dir, snapshot) {
|
|
21
|
+
const p = path.join(dir, "shopping-pinduoduo.json");
|
|
22
|
+
fs.writeFileSync(p, JSON.stringify(snapshot), "utf-8");
|
|
23
|
+
return p;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function writeRaw(dir, fileName, content) {
|
|
27
|
+
const p = path.join(dir, fileName);
|
|
28
|
+
fs.writeFileSync(p, content, "utf-8");
|
|
29
|
+
return p;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
describe("PinduoduoAdapter snapshot mode", () => {
|
|
33
|
+
let tmpDir;
|
|
34
|
+
beforeEach(() => {
|
|
35
|
+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "pdd-snap-"));
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("exports SNAPSHOT_SCHEMA_VERSION = 1 and VALID_SNAPSHOT_KINDS = [order]", () => {
|
|
39
|
+
expect(SNAPSHOT_SCHEMA_VERSION).toBe(1);
|
|
40
|
+
expect(VALID_SNAPSHOT_KINDS).toEqual(["order"]);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("authenticate(inputPath) ok when readable", async () => {
|
|
44
|
+
const p = writeSnapshot(tmpDir, {
|
|
45
|
+
schemaVersion: 1,
|
|
46
|
+
snapshottedAt: Date.now(),
|
|
47
|
+
events: [],
|
|
48
|
+
});
|
|
49
|
+
const a = new PinduoduoAdapter();
|
|
50
|
+
const res = await a.authenticate({ inputPath: p });
|
|
51
|
+
expect(res.ok).toBe(true);
|
|
52
|
+
expect(res.mode).toBe("snapshot-file");
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("authenticate(inputPath) fails when path unreadable", async () => {
|
|
56
|
+
const a = new PinduoduoAdapter();
|
|
57
|
+
const res = await a.authenticate({ inputPath: path.join(tmpDir, "missing.json") });
|
|
58
|
+
expect(res.ok).toBe(false);
|
|
59
|
+
expect(res.reason).toBe("INPUT_PATH_UNREADABLE");
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("authenticate() with no inputPath returns NO_INPUT (no cookie mode in v0.2)", async () => {
|
|
63
|
+
const a = new PinduoduoAdapter({ account: { uid: "u1" } });
|
|
64
|
+
const res = await a.authenticate({});
|
|
65
|
+
expect(res.ok).toBe(false);
|
|
66
|
+
expect(res.reason).toBe("NO_INPUT");
|
|
67
|
+
// Honest hint about the anti_token gap
|
|
68
|
+
expect(res.message).toMatch(/snapshot mode/);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("sync() without inputPath throws with anti_token hint", async () => {
|
|
72
|
+
const a = new PinduoduoAdapter();
|
|
73
|
+
let threw = null;
|
|
74
|
+
try {
|
|
75
|
+
for await (const _r of a.sync({})) { /* drain */ }
|
|
76
|
+
} catch (err) {
|
|
77
|
+
threw = err;
|
|
78
|
+
}
|
|
79
|
+
expect(threw).toBeTruthy();
|
|
80
|
+
expect(String(threw.message)).toMatch(/anti_token/);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("rejects non-JSON inputPath with HTML-parsing-future hint", async () => {
|
|
84
|
+
const p = writeRaw(tmpDir, "orders.html", "<html><body>not json</body></html>");
|
|
85
|
+
const a = new PinduoduoAdapter();
|
|
86
|
+
let threw = null;
|
|
87
|
+
try {
|
|
88
|
+
for await (const _r of a.sync({ inputPath: p })) { /* drain */ }
|
|
89
|
+
} catch (err) {
|
|
90
|
+
threw = err;
|
|
91
|
+
}
|
|
92
|
+
expect(threw).toBeTruthy();
|
|
93
|
+
expect(String(threw.message)).toMatch(/snapshot must be JSON/);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("rejects schemaVersion mismatch", async () => {
|
|
97
|
+
const p = writeSnapshot(tmpDir, {
|
|
98
|
+
schemaVersion: 99,
|
|
99
|
+
snapshottedAt: Date.now(),
|
|
100
|
+
events: [],
|
|
101
|
+
});
|
|
102
|
+
const a = new PinduoduoAdapter();
|
|
103
|
+
let threw = null;
|
|
104
|
+
try {
|
|
105
|
+
for await (const _r of a.sync({ inputPath: p })) { /* drain */ }
|
|
106
|
+
} catch (err) {
|
|
107
|
+
threw = err;
|
|
108
|
+
}
|
|
109
|
+
expect(threw).toBeTruthy();
|
|
110
|
+
expect(String(threw.message)).toMatch(/schemaVersion mismatch/);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("empty events array yields nothing (no crash)", async () => {
|
|
114
|
+
const p = writeSnapshot(tmpDir, {
|
|
115
|
+
schemaVersion: 1,
|
|
116
|
+
snapshottedAt: Date.now(),
|
|
117
|
+
events: [],
|
|
118
|
+
});
|
|
119
|
+
const a = new PinduoduoAdapter();
|
|
120
|
+
const raws = [];
|
|
121
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
122
|
+
expect(raws.length).toBe(0);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("order event round-trips normalize cleanly", async () => {
|
|
126
|
+
const now = Date.now();
|
|
127
|
+
const p = writeSnapshot(tmpDir, {
|
|
128
|
+
schemaVersion: 1,
|
|
129
|
+
snapshottedAt: now,
|
|
130
|
+
vendor: "pinduoduo",
|
|
131
|
+
account: { uid: "u-alice", displayName: "alice" },
|
|
132
|
+
events: [
|
|
133
|
+
{
|
|
134
|
+
kind: "order",
|
|
135
|
+
id: "order-PDD-9001",
|
|
136
|
+
capturedAt: now - 1000,
|
|
137
|
+
orderId: "PDD-9001",
|
|
138
|
+
merchantName: "拼多多旗舰店",
|
|
139
|
+
placedAt: now - 86400_000,
|
|
140
|
+
paidAt: now - 86400_000 + 5_000,
|
|
141
|
+
status: "已发货",
|
|
142
|
+
items: [
|
|
143
|
+
{ name: "纸巾100抽", quantity: 5, unitPrice: 9.9, sku: "sku-001" },
|
|
144
|
+
{ name: "牙刷4支装", quantity: 1, unitPrice: 12.5 },
|
|
145
|
+
],
|
|
146
|
+
totalAmount: { value: 62.0, currency: "CNY" },
|
|
147
|
+
recipient: "张三",
|
|
148
|
+
shippingAddress: "上海市浦东新区...",
|
|
149
|
+
trackingNumber: "SF1234567890",
|
|
150
|
+
},
|
|
151
|
+
],
|
|
152
|
+
});
|
|
153
|
+
const a = new PinduoduoAdapter();
|
|
154
|
+
const raws = [];
|
|
155
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
156
|
+
expect(raws.length).toBe(1);
|
|
157
|
+
expect(raws[0].kind).toBe("order");
|
|
158
|
+
expect(raws[0].originalId).toMatch(/^pinduoduo:order:/);
|
|
159
|
+
expect(raws[0].originalId).toBe("pinduoduo:order:order-PDD-9001");
|
|
160
|
+
|
|
161
|
+
const batch = a.normalize(raws[0]);
|
|
162
|
+
expect(validateBatch(batch).valid).toBe(true);
|
|
163
|
+
// normalizeOrderRecord emits an Event for the placed order. Confirm the
|
|
164
|
+
// shape matches what the LocalVault audit row expects.
|
|
165
|
+
expect(batch.events.length).toBeGreaterThan(0);
|
|
166
|
+
// Items survive into the normalized record's extras / event content
|
|
167
|
+
expect(JSON.stringify(batch)).toContain("纸巾100抽");
|
|
168
|
+
// Recipient / address survive
|
|
169
|
+
expect(JSON.stringify(batch)).toContain("张三");
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("status mapping handles pinduoduo-typical strings", async () => {
|
|
173
|
+
const now = Date.now();
|
|
174
|
+
const cases = [
|
|
175
|
+
{ status: "已发货", expect: "shipped" },
|
|
176
|
+
{ status: "已收货", expect: "delivered" },
|
|
177
|
+
{ status: "已完成", expect: "delivered" },
|
|
178
|
+
{ status: "退款", expect: "refunded" },
|
|
179
|
+
{ status: "已关闭", expect: "cancelled" },
|
|
180
|
+
{ status: "待付款", expect: "placed" },
|
|
181
|
+
];
|
|
182
|
+
for (const c of cases) {
|
|
183
|
+
const p = writeSnapshot(tmpDir, {
|
|
184
|
+
schemaVersion: 1,
|
|
185
|
+
snapshottedAt: now,
|
|
186
|
+
events: [
|
|
187
|
+
{
|
|
188
|
+
kind: "order", id: `o-${c.status}`,
|
|
189
|
+
orderId: `o-${c.status}`, merchantName: "m",
|
|
190
|
+
placedAt: now, paidAt: now, status: c.status,
|
|
191
|
+
items: [{ name: "x", quantity: 1, unitPrice: 1 }],
|
|
192
|
+
totalAmount: { value: 1, currency: "CNY" },
|
|
193
|
+
},
|
|
194
|
+
],
|
|
195
|
+
});
|
|
196
|
+
const a = new PinduoduoAdapter();
|
|
197
|
+
const raws = [];
|
|
198
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
199
|
+
const batch = a.normalize(raws[0]);
|
|
200
|
+
// Find the order event and check status survives via extras (the
|
|
201
|
+
// shopping-base normalizer puts mapped status into the event)
|
|
202
|
+
const stringified = JSON.stringify(batch);
|
|
203
|
+
// Status maps to one of the canonical enum values. We can't easily
|
|
204
|
+
// get the field path without depending on shopping-base internals,
|
|
205
|
+
// so verify the canonical value appears somewhere in the batch.
|
|
206
|
+
expect(stringified).toContain(c.expect);
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it("respects per-kind include opt-out", async () => {
|
|
211
|
+
const now = Date.now();
|
|
212
|
+
const p = writeSnapshot(tmpDir, {
|
|
213
|
+
schemaVersion: 1,
|
|
214
|
+
snapshottedAt: now,
|
|
215
|
+
events: [
|
|
216
|
+
{ kind: "order", id: "o1", orderId: "o1", merchantName: "m", placedAt: now,
|
|
217
|
+
items: [], totalAmount: { value: 1, currency: "CNY" } },
|
|
218
|
+
],
|
|
219
|
+
});
|
|
220
|
+
const a = new PinduoduoAdapter();
|
|
221
|
+
const raws = [];
|
|
222
|
+
for await (const r of a.sync({ inputPath: p, include: { order: false } })) {
|
|
223
|
+
raws.push(r);
|
|
224
|
+
}
|
|
225
|
+
expect(raws.length).toBe(0);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it("respects opts.limit", async () => {
|
|
229
|
+
const now = Date.now();
|
|
230
|
+
const events = Array.from({ length: 5 }, (_, i) => ({
|
|
231
|
+
kind: "order", id: `o${i}`, orderId: `o${i}`, merchantName: "m",
|
|
232
|
+
placedAt: now - i * 1000,
|
|
233
|
+
items: [], totalAmount: { value: 1, currency: "CNY" },
|
|
234
|
+
}));
|
|
235
|
+
const p = writeSnapshot(tmpDir, { schemaVersion: 1, snapshottedAt: now, events });
|
|
236
|
+
const a = new PinduoduoAdapter();
|
|
237
|
+
const raws = [];
|
|
238
|
+
for await (const r of a.sync({ inputPath: p, limit: 2 })) raws.push(r);
|
|
239
|
+
expect(raws.length).toBe(2);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it("filters out unknown kinds (forward compat)", async () => {
|
|
243
|
+
const now = Date.now();
|
|
244
|
+
const p = writeSnapshot(tmpDir, {
|
|
245
|
+
schemaVersion: 1,
|
|
246
|
+
snapshottedAt: now,
|
|
247
|
+
events: [
|
|
248
|
+
{ kind: "order", id: "o1", orderId: "o1", merchantName: "m", placedAt: now,
|
|
249
|
+
items: [], totalAmount: { value: 1, currency: "CNY" } },
|
|
250
|
+
{ kind: "refund", id: "r1", orderId: "o1" }, // not yet supported
|
|
251
|
+
{ kind: "future-kind", id: "x" },
|
|
252
|
+
],
|
|
253
|
+
});
|
|
254
|
+
const a = new PinduoduoAdapter();
|
|
255
|
+
const raws = [];
|
|
256
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
257
|
+
expect(raws.length).toBe(1);
|
|
258
|
+
expect(raws[0].kind).toBe("order");
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
it("snapshottedAt fallback when event capturedAt missing", async () => {
|
|
262
|
+
const ts = 1700000000000;
|
|
263
|
+
const p = writeSnapshot(tmpDir, {
|
|
264
|
+
schemaVersion: 1,
|
|
265
|
+
snapshottedAt: ts,
|
|
266
|
+
events: [
|
|
267
|
+
{ kind: "order", id: "o1", orderId: "o1", merchantName: "m", items: [],
|
|
268
|
+
totalAmount: { value: 1, currency: "CNY" } },
|
|
269
|
+
],
|
|
270
|
+
});
|
|
271
|
+
const a = new PinduoduoAdapter();
|
|
272
|
+
const raws = [];
|
|
273
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
274
|
+
expect(raws[0].capturedAt).toBe(ts);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it("snapshotEventToRecord handles snake_case goods_name/goods_price fallback", async () => {
|
|
278
|
+
// Pinduoduo's internal field names use snake_case (goods_name, goods_price,
|
|
279
|
+
// goods_count); the normalizer falls back to those if camelCase is absent.
|
|
280
|
+
const now = Date.now();
|
|
281
|
+
const p = writeSnapshot(tmpDir, {
|
|
282
|
+
schemaVersion: 1,
|
|
283
|
+
snapshottedAt: now,
|
|
284
|
+
events: [
|
|
285
|
+
{
|
|
286
|
+
kind: "order", id: "o-snake",
|
|
287
|
+
orderId: "o-snake", merchantName: "店铺A",
|
|
288
|
+
placedAt: now, paidAt: now,
|
|
289
|
+
items: [
|
|
290
|
+
{ goods_name: "纸巾", goods_count: 3, goods_price: 5.5, sku_id: "sk1" },
|
|
291
|
+
],
|
|
292
|
+
totalAmount: { value: 16.5, currency: "CNY" },
|
|
293
|
+
},
|
|
294
|
+
],
|
|
295
|
+
});
|
|
296
|
+
const a = new PinduoduoAdapter();
|
|
297
|
+
const raws = [];
|
|
298
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
299
|
+
const batch = a.normalize(raws[0]);
|
|
300
|
+
expect(JSON.stringify(batch)).toContain("纸巾");
|
|
301
|
+
});
|
|
302
|
+
});
|