@chainlesschain/personal-data-hub 0.2.1 → 0.2.3
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__/adapters/wechat-frida-agent.test.js +132 -1
- package/__tests__/integration/social-bilibili-pipeline.test.js +261 -0
- package/__tests__/longtail-adapters.test.js +60 -14
- 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 +91 -17
- package/__tests__/social-bilibili-snapshot.test.js +278 -0
- 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/__tests__/wechat-adapter.test.js +118 -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-bilibili/adapter.js +500 -0
- package/lib/adapters/social-bilibili/index.js +21 -169
- 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/adapters/wechat/content-parser.js +11 -2
- package/lib/adapters/wechat/db-reader.js +88 -10
- package/lib/adapters/wechat/frida-agent/loader.js +7 -0
- package/lib/adapters/wechat/frida-agent/wechat-key-hook.js +140 -18
- package/lib/adapters/wechat/key-providers/frida-key-provider.js +8 -0
- package/lib/adapters/wechat/normalize.js +12 -3
- package/lib/index.js +5 -1
- package/lib/vault.js +60 -8
- package/package.json +2 -1
|
@@ -0,0 +1,294 @@
|
|
|
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
|
+
QQAdapter,
|
|
11
|
+
SNAPSHOT_SCHEMA_VERSION,
|
|
12
|
+
VALID_SNAPSHOT_KINDS,
|
|
13
|
+
xorDecrypt,
|
|
14
|
+
} = require("../lib/adapters/messaging-qq");
|
|
15
|
+
const { validateBatch } = require("../lib/batch");
|
|
16
|
+
|
|
17
|
+
// §Phase 13.5 v0.2 (2026-05-22) — snapshot-mode tests, mirror of
|
|
18
|
+
// social-weibo-snapshot.test.js.
|
|
19
|
+
//
|
|
20
|
+
// Snapshot mode is in-APK Android cc reading JSON written by QQLocalCollector
|
|
21
|
+
// (su cp /data/data/com.tencent.mobileqq/databases/<uin>.db + plain SQLite
|
|
22
|
+
// open + per-row XOR-with-IMEI decrypt of msgData). Sqlite/device-pull tests
|
|
23
|
+
// stay in longtail-adapters.test.js. Both paths share normalize().
|
|
24
|
+
|
|
25
|
+
function writeSnapshot(dir, snapshot) {
|
|
26
|
+
const p = path.join(dir, "messaging-qq.json");
|
|
27
|
+
fs.writeFileSync(p, JSON.stringify(snapshot), "utf-8");
|
|
28
|
+
return p;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
describe("QQAdapter snapshot mode", () => {
|
|
32
|
+
let tmpDir;
|
|
33
|
+
beforeEach(() => {
|
|
34
|
+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "qq-snap-"));
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("exports SNAPSHOT_SCHEMA_VERSION = 1 + 3 VALID_SNAPSHOT_KINDS", () => {
|
|
38
|
+
expect(SNAPSHOT_SCHEMA_VERSION).toBe(1);
|
|
39
|
+
expect(VALID_SNAPSHOT_KINDS).toEqual(["contact", "group", "message"]);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("authenticate(inputPath) ok when readable", async () => {
|
|
43
|
+
const p = writeSnapshot(tmpDir, {
|
|
44
|
+
schemaVersion: 1,
|
|
45
|
+
snapshottedAt: Date.now(),
|
|
46
|
+
events: [],
|
|
47
|
+
});
|
|
48
|
+
const a = new QQAdapter();
|
|
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 QQAdapter();
|
|
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 neither inputPath nor dbPath returns NO_INPUT", async () => {
|
|
62
|
+
const a = new QQAdapter();
|
|
63
|
+
const res = await a.authenticate({});
|
|
64
|
+
expect(res.ok).toBe(false);
|
|
65
|
+
expect(res.reason).toBe("NO_INPUT");
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("rejects schemaVersion mismatch", async () => {
|
|
69
|
+
const p = writeSnapshot(tmpDir, {
|
|
70
|
+
schemaVersion: 99,
|
|
71
|
+
snapshottedAt: Date.now(),
|
|
72
|
+
events: [],
|
|
73
|
+
});
|
|
74
|
+
const a = new QQAdapter();
|
|
75
|
+
let threw = null;
|
|
76
|
+
try {
|
|
77
|
+
for await (const _r of a.sync({ inputPath: p })) { /* drain */ }
|
|
78
|
+
} catch (err) {
|
|
79
|
+
threw = err;
|
|
80
|
+
}
|
|
81
|
+
expect(threw).toBeTruthy();
|
|
82
|
+
expect(String(threw.message)).toMatch(/schemaVersion mismatch/);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("empty events array yields nothing (no crash)", async () => {
|
|
86
|
+
const p = writeSnapshot(tmpDir, {
|
|
87
|
+
schemaVersion: 1,
|
|
88
|
+
snapshottedAt: Date.now(),
|
|
89
|
+
events: [],
|
|
90
|
+
});
|
|
91
|
+
const a = new QQAdapter();
|
|
92
|
+
const raws = [];
|
|
93
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
94
|
+
expect(raws.length).toBe(0);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("contact + group + message round-trip normalize cleanly", async () => {
|
|
98
|
+
const now = Date.now();
|
|
99
|
+
const p = writeSnapshot(tmpDir, {
|
|
100
|
+
schemaVersion: 1,
|
|
101
|
+
snapshottedAt: now,
|
|
102
|
+
account: { qq: "12345", displayName: "alice" },
|
|
103
|
+
events: [
|
|
104
|
+
{
|
|
105
|
+
kind: "contact",
|
|
106
|
+
id: "contact-99999",
|
|
107
|
+
capturedAt: now - 1000,
|
|
108
|
+
uin: "99999",
|
|
109
|
+
nickname: "好友A",
|
|
110
|
+
remark: "工作组",
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
kind: "group",
|
|
114
|
+
id: "group-88888",
|
|
115
|
+
capturedAt: now - 2000,
|
|
116
|
+
troopUin: "88888",
|
|
117
|
+
troopName: "测试群",
|
|
118
|
+
memberCount: 30,
|
|
119
|
+
ownerUin: "77777",
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
kind: "message",
|
|
123
|
+
id: "msg-m1",
|
|
124
|
+
capturedAt: now - 3000,
|
|
125
|
+
msgId: "m1",
|
|
126
|
+
msgType: -1000,
|
|
127
|
+
senderUin: "99999",
|
|
128
|
+
peerUin: "12345",
|
|
129
|
+
isGroup: false,
|
|
130
|
+
isSend: false,
|
|
131
|
+
text: "你好",
|
|
132
|
+
},
|
|
133
|
+
],
|
|
134
|
+
});
|
|
135
|
+
const a = new QQAdapter();
|
|
136
|
+
const raws = [];
|
|
137
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
138
|
+
expect(raws.length).toBe(3);
|
|
139
|
+
|
|
140
|
+
const kinds = raws.map((r) => r.kind);
|
|
141
|
+
expect(kinds).toEqual(["contact", "group", "message"]);
|
|
142
|
+
|
|
143
|
+
// Each originalId namespaced under qq:<kind>:<id>
|
|
144
|
+
expect(raws[0].originalId).toMatch(/^qq:contact:/);
|
|
145
|
+
expect(raws[1].originalId).toMatch(/^qq:group:/);
|
|
146
|
+
expect(raws[2].originalId).toMatch(/^qq:message:/);
|
|
147
|
+
|
|
148
|
+
// Normalize each + validate
|
|
149
|
+
for (const raw of raws) {
|
|
150
|
+
const batch = a.normalize(raw);
|
|
151
|
+
expect(validateBatch(batch).valid).toBe(true);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const contactBatch = a.normalize(raws[0]);
|
|
155
|
+
expect(contactBatch.events.length).toBe(0);
|
|
156
|
+
expect(contactBatch.persons.length).toBe(1);
|
|
157
|
+
// remark + nickname + uin all surface as names (priority order)
|
|
158
|
+
expect(contactBatch.persons[0].names).toEqual(["工作组", "好友A", "99999"]);
|
|
159
|
+
expect(contactBatch.persons[0].identifiers["qq-uin"]).toEqual(["99999"]);
|
|
160
|
+
|
|
161
|
+
const groupBatch = a.normalize(raws[1]);
|
|
162
|
+
expect(groupBatch.events.length).toBe(0);
|
|
163
|
+
expect(groupBatch.topics.length).toBe(1);
|
|
164
|
+
expect(groupBatch.topics[0].name).toBe("测试群");
|
|
165
|
+
expect(groupBatch.topics[0].extra.troopUin).toBe("88888");
|
|
166
|
+
expect(groupBatch.topics[0].extra.memberCount).toBe(30);
|
|
167
|
+
|
|
168
|
+
const msgBatch = a.normalize(raws[2]);
|
|
169
|
+
expect(msgBatch.events.length).toBe(1);
|
|
170
|
+
expect(msgBatch.events[0].subtype).toBe("message");
|
|
171
|
+
expect(msgBatch.events[0].extra.peerUin).toBe("12345");
|
|
172
|
+
expect(msgBatch.events[0].extra.senderUin).toBe("99999");
|
|
173
|
+
expect(msgBatch.events[0].extra.isGroup).toBe(false);
|
|
174
|
+
expect(msgBatch.events[0].extra.isSend).toBe(false);
|
|
175
|
+
expect(msgBatch.events[0].content.text).toBe("你好");
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it("respects per-kind include opt-out", async () => {
|
|
179
|
+
const now = Date.now();
|
|
180
|
+
const p = writeSnapshot(tmpDir, {
|
|
181
|
+
schemaVersion: 1,
|
|
182
|
+
snapshottedAt: now,
|
|
183
|
+
events: [
|
|
184
|
+
{ kind: "contact", id: "c1", capturedAt: now, uin: "100", nickname: "A" },
|
|
185
|
+
{ kind: "group", id: "g1", capturedAt: now, troopUin: "200", troopName: "G" },
|
|
186
|
+
{ kind: "message", id: "m1", capturedAt: now, msgId: "m1", text: "hi" },
|
|
187
|
+
],
|
|
188
|
+
});
|
|
189
|
+
const a = new QQAdapter();
|
|
190
|
+
const raws = [];
|
|
191
|
+
for await (const r of a.sync({ inputPath: p, include: { message: false } })) {
|
|
192
|
+
raws.push(r);
|
|
193
|
+
}
|
|
194
|
+
const kinds = raws.map((r) => r.kind);
|
|
195
|
+
expect(kinds).toEqual(["contact", "group"]);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it("respects opts.limit", async () => {
|
|
199
|
+
const now = Date.now();
|
|
200
|
+
const events = Array.from({ length: 5 }, (_, i) => ({
|
|
201
|
+
kind: "message",
|
|
202
|
+
id: `m${i}`,
|
|
203
|
+
capturedAt: now - i * 100,
|
|
204
|
+
msgId: `m${i}`,
|
|
205
|
+
text: `t${i}`,
|
|
206
|
+
}));
|
|
207
|
+
const p = writeSnapshot(tmpDir, { schemaVersion: 1, snapshottedAt: now, events });
|
|
208
|
+
const a = new QQAdapter();
|
|
209
|
+
const raws = [];
|
|
210
|
+
for await (const r of a.sync({ inputPath: p, limit: 2 })) raws.push(r);
|
|
211
|
+
expect(raws.length).toBe(2);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it("filters out unknown kinds (forward compat)", async () => {
|
|
215
|
+
const now = Date.now();
|
|
216
|
+
const p = writeSnapshot(tmpDir, {
|
|
217
|
+
schemaVersion: 1,
|
|
218
|
+
snapshottedAt: now,
|
|
219
|
+
events: [
|
|
220
|
+
{ kind: "contact", id: "c1", capturedAt: now, uin: "100", nickname: "A" },
|
|
221
|
+
{ kind: "future-kind", id: "x", capturedAt: now },
|
|
222
|
+
{ kind: "search", id: "s", capturedAt: now }, // not a QQ snapshot kind
|
|
223
|
+
],
|
|
224
|
+
});
|
|
225
|
+
const a = new QQAdapter();
|
|
226
|
+
const raws = [];
|
|
227
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
228
|
+
expect(raws.length).toBe(1);
|
|
229
|
+
expect(raws[0].kind).toBe("contact");
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it("snapshottedAt fallback when event capturedAt missing", async () => {
|
|
233
|
+
const ts = 1700000000000;
|
|
234
|
+
const p = writeSnapshot(tmpDir, {
|
|
235
|
+
schemaVersion: 1,
|
|
236
|
+
snapshottedAt: ts,
|
|
237
|
+
events: [
|
|
238
|
+
{ kind: "contact", id: "c1", uin: "100", nickname: "A" },
|
|
239
|
+
],
|
|
240
|
+
});
|
|
241
|
+
const a = new QQAdapter();
|
|
242
|
+
const raws = [];
|
|
243
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
244
|
+
expect(raws[0].capturedAt).toBe(ts);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it("normalize handles missing identifiers gracefully (forward compat)", () => {
|
|
248
|
+
const a = new QQAdapter();
|
|
249
|
+
// Contact with empty payload — uin/nickname/remark all absent
|
|
250
|
+
const raw = {
|
|
251
|
+
adapter: "messaging-qq",
|
|
252
|
+
kind: "contact",
|
|
253
|
+
originalId: "qq:contact:unknown",
|
|
254
|
+
capturedAt: Date.now(),
|
|
255
|
+
payload: { kind: "contact" },
|
|
256
|
+
};
|
|
257
|
+
const batch = a.normalize(raw);
|
|
258
|
+
expect(batch.persons.length).toBe(1);
|
|
259
|
+
expect(batch.persons[0].names).toEqual(["(unnamed)"]);
|
|
260
|
+
expect(validateBatch(batch).valid).toBe(true);
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
it("xorDecrypt: empty input or empty key returns empty string", () => {
|
|
264
|
+
expect(xorDecrypt(null, Buffer.from("123"))).toBe("");
|
|
265
|
+
expect(xorDecrypt(Buffer.from(""), Buffer.from("123"))).toBe("");
|
|
266
|
+
expect(xorDecrypt(Buffer.from("hi"), Buffer.from(""))).toBe("");
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
it("xorDecrypt: ASCII round-trip via IMEI XOR (sjqz qq.py parity)", () => {
|
|
270
|
+
// Algorithm: out[i] = data[i] XOR imeiBytes[i % imeiLen]. Encryption is
|
|
271
|
+
// its own inverse: XOR-decrypt the encrypted form ⇒ original plaintext.
|
|
272
|
+
const imei = "123456789012345";
|
|
273
|
+
const plaintext = "hello";
|
|
274
|
+
const imeiBytes = Buffer.from(imei, "utf-8");
|
|
275
|
+
const ptBytes = Buffer.from(plaintext, "utf-8");
|
|
276
|
+
const encrypted = Buffer.alloc(ptBytes.length);
|
|
277
|
+
for (let i = 0; i < ptBytes.length; i++) {
|
|
278
|
+
encrypted[i] = ptBytes[i] ^ imeiBytes[i % imeiBytes.length];
|
|
279
|
+
}
|
|
280
|
+
expect(xorDecrypt(encrypted, imeiBytes)).toBe(plaintext);
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
it("xorDecrypt: UTF-8 multibyte (Chinese) round-trip", () => {
|
|
284
|
+
const imei = "999000111222333";
|
|
285
|
+
const plaintext = "你好世界";
|
|
286
|
+
const imeiBytes = Buffer.from(imei, "utf-8");
|
|
287
|
+
const ptBytes = Buffer.from(plaintext, "utf-8");
|
|
288
|
+
const encrypted = Buffer.alloc(ptBytes.length);
|
|
289
|
+
for (let i = 0; i < ptBytes.length; i++) {
|
|
290
|
+
encrypted[i] = ptBytes[i] ^ imeiBytes[i % imeiBytes.length];
|
|
291
|
+
}
|
|
292
|
+
expect(xorDecrypt(encrypted, imeiBytes)).toBe(plaintext);
|
|
293
|
+
});
|
|
294
|
+
});
|
|
@@ -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
|
+
});
|