@chainlesschain/personal-data-hub 0.4.23 → 0.4.24
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/bank-family.test.js +125 -0
- package/__tests__/adapters/car-mercedesme.test.js +74 -0
- package/__tests__/adapters/finance-dcep.test.js +74 -0
- package/__tests__/adapters/fitness-joyrun.test.js +82 -0
- package/__tests__/adapters/gov-12123.test.js +103 -0
- package/__tests__/adapters/music-qq.test.js +112 -0
- package/__tests__/adapters/reading-family.test.js +108 -0
- package/__tests__/adapters/travel-didi-consumer.test.js +66 -0
- package/__tests__/audio-ximalaya-snapshot.test.js +279 -0
- package/__tests__/fitness-keep-snapshot.test.js +224 -0
- package/__tests__/shopping-eleme-snapshot.test.js +454 -0
- package/__tests__/shopping-vipshop-snapshot.test.js +425 -0
- package/__tests__/shopping-xianyu-snapshot.test.js +451 -0
- package/__tests__/social-douban-snapshot.test.js +351 -0
- package/lib/adapter-guide.js +19 -1
- package/lib/adapters/_bank-base.js +405 -0
- package/lib/adapters/_reading-base.js +315 -0
- package/lib/adapters/audio-ximalaya/index.js +414 -0
- package/lib/adapters/bank-bankcomm/index.js +27 -0
- package/lib/adapters/bank-boc/index.js +26 -0
- package/lib/adapters/bank-cmbc/index.js +26 -0
- package/lib/adapters/bank-icbc/index.js +27 -0
- package/lib/adapters/car-mercedesme/index.js +225 -0
- package/lib/adapters/finance-dcep/index.js +302 -0
- package/lib/adapters/fitness-joyrun/index.js +295 -0
- package/lib/adapters/fitness-keep/index.js +343 -0
- package/lib/adapters/gov-12123/index.js +391 -0
- package/lib/adapters/music-qq/index.js +372 -0
- package/lib/adapters/reading-fanqie/index.js +61 -0
- package/lib/adapters/reading-qimao/index.js +61 -0
- package/lib/adapters/shopping-eleme/index.js +441 -0
- package/lib/adapters/shopping-vipshop/index.js +429 -0
- package/lib/adapters/shopping-xianyu/index.js +454 -0
- package/lib/adapters/social-douban/index.js +564 -0
- package/lib/adapters/travel-didi-consumer/index.js +148 -0
- package/lib/index.js +36 -0
- package/package.json +1 -1
|
@@ -0,0 +1,451 @@
|
|
|
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
|
+
XianyuAdapter,
|
|
11
|
+
SNAPSHOT_SCHEMA_VERSION,
|
|
12
|
+
VALID_SNAPSHOT_KINDS,
|
|
13
|
+
orderToRecord,
|
|
14
|
+
extractOrders,
|
|
15
|
+
normSide,
|
|
16
|
+
} = require("../lib/adapters/shopping-xianyu");
|
|
17
|
+
const { assertAdapter } = require("../lib/adapter-spec");
|
|
18
|
+
const { validateBatch } = require("../lib/batch");
|
|
19
|
+
|
|
20
|
+
// 闲鱼 (Xianyu / goofish) — Alibaba C2C 二手交易; mirrors shopping-eleme with a
|
|
21
|
+
// two-sided buy/sell `side`. mtop signing is injected (signProvider) so the
|
|
22
|
+
// adapter stays pure-Node.
|
|
23
|
+
|
|
24
|
+
function writeSnapshot(dir, snapshot) {
|
|
25
|
+
const p = path.join(dir, "shopping-xianyu.json");
|
|
26
|
+
fs.writeFileSync(p, JSON.stringify(snapshot), "utf-8");
|
|
27
|
+
return p;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function writeRaw(dir, fileName, content) {
|
|
31
|
+
const p = path.join(dir, fileName);
|
|
32
|
+
fs.writeFileSync(p, content, "utf-8");
|
|
33
|
+
return p;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
describe("XianyuAdapter snapshot mode", () => {
|
|
37
|
+
let tmpDir;
|
|
38
|
+
beforeEach(() => {
|
|
39
|
+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "xianyu-snap-"));
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("exports SNAPSHOT_SCHEMA_VERSION = 1 and VALID_SNAPSHOT_KINDS = [order]", () => {
|
|
43
|
+
expect(SNAPSHOT_SCHEMA_VERSION).toBe(1);
|
|
44
|
+
expect(VALID_SNAPSHOT_KINDS).toEqual(["order"]);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("authenticate(inputPath) ok when readable", async () => {
|
|
48
|
+
const p = writeSnapshot(tmpDir, { schemaVersion: 1, snapshottedAt: Date.now(), events: [] });
|
|
49
|
+
const a = new XianyuAdapter();
|
|
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 XianyuAdapter();
|
|
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", async () => {
|
|
63
|
+
const a = new XianyuAdapter();
|
|
64
|
+
const res = await a.authenticate({});
|
|
65
|
+
expect(res.ok).toBe(false);
|
|
66
|
+
expect(res.reason).toBe("NO_INPUT");
|
|
67
|
+
expect(res.message).toMatch(/snapshot mode/);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("sync() without inputPath throws with signing hint", async () => {
|
|
71
|
+
const a = new XianyuAdapter();
|
|
72
|
+
let threw = null;
|
|
73
|
+
try {
|
|
74
|
+
for await (const _r of a.sync({})) { /* drain */ }
|
|
75
|
+
} catch (err) {
|
|
76
|
+
threw = err;
|
|
77
|
+
}
|
|
78
|
+
expect(threw).toBeTruthy();
|
|
79
|
+
expect(String(threw.message)).toMatch(/signProvider/);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("rejects non-JSON inputPath", async () => {
|
|
83
|
+
const p = writeRaw(tmpDir, "orders.html", "<html>not json</html>");
|
|
84
|
+
const a = new XianyuAdapter();
|
|
85
|
+
let threw = null;
|
|
86
|
+
try {
|
|
87
|
+
for await (const _r of a.sync({ inputPath: p })) { /* drain */ }
|
|
88
|
+
} catch (err) {
|
|
89
|
+
threw = err;
|
|
90
|
+
}
|
|
91
|
+
expect(threw).toBeTruthy();
|
|
92
|
+
expect(String(threw.message)).toMatch(/snapshot must be JSON/);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("rejects schemaVersion mismatch", async () => {
|
|
96
|
+
const p = writeSnapshot(tmpDir, { schemaVersion: 99, snapshottedAt: Date.now(), events: [] });
|
|
97
|
+
const a = new XianyuAdapter();
|
|
98
|
+
let threw = null;
|
|
99
|
+
try {
|
|
100
|
+
for await (const _r of a.sync({ inputPath: p })) { /* drain */ }
|
|
101
|
+
} catch (err) {
|
|
102
|
+
threw = err;
|
|
103
|
+
}
|
|
104
|
+
expect(threw).toBeTruthy();
|
|
105
|
+
expect(String(threw.message)).toMatch(/schemaVersion mismatch/);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("empty events array yields nothing (no crash)", async () => {
|
|
109
|
+
const p = writeSnapshot(tmpDir, { schemaVersion: 1, snapshottedAt: Date.now(), events: [] });
|
|
110
|
+
const a = new XianyuAdapter();
|
|
111
|
+
const raws = [];
|
|
112
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
113
|
+
expect(raws.length).toBe(0);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("buy order round-trips normalize cleanly (counterparty = seller)", async () => {
|
|
117
|
+
const now = Date.now();
|
|
118
|
+
const p = writeSnapshot(tmpDir, {
|
|
119
|
+
schemaVersion: 1,
|
|
120
|
+
snapshottedAt: now,
|
|
121
|
+
vendor: "xianyu",
|
|
122
|
+
account: { userId: "u-alice", displayName: "alice" },
|
|
123
|
+
events: [
|
|
124
|
+
{
|
|
125
|
+
kind: "order",
|
|
126
|
+
id: "order-XY-9001",
|
|
127
|
+
capturedAt: now - 1000,
|
|
128
|
+
orderId: "XY-9001",
|
|
129
|
+
side: "buy",
|
|
130
|
+
title: "二手 iPhone 13 128G",
|
|
131
|
+
counterparty: "卖家阿强",
|
|
132
|
+
placedAt: now - 86400_000,
|
|
133
|
+
paidAt: now - 86400_000 + 5_000,
|
|
134
|
+
status: "交易成功",
|
|
135
|
+
totalAmount: { value: 2800.0, currency: "CNY" },
|
|
136
|
+
recipient: "张三",
|
|
137
|
+
shippingAddress: "上海市浦东新区...",
|
|
138
|
+
},
|
|
139
|
+
],
|
|
140
|
+
});
|
|
141
|
+
const a = new XianyuAdapter();
|
|
142
|
+
const raws = [];
|
|
143
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
144
|
+
expect(raws.length).toBe(1);
|
|
145
|
+
expect(raws[0].originalId).toBe("xianyu:order:order-XY-9001");
|
|
146
|
+
|
|
147
|
+
const batch = a.normalize(raws[0]);
|
|
148
|
+
expect(validateBatch(batch).valid).toBe(true);
|
|
149
|
+
expect(batch.events.length).toBeGreaterThan(0);
|
|
150
|
+
expect(JSON.stringify(batch)).toContain("二手 iPhone 13 128G");
|
|
151
|
+
expect(JSON.stringify(batch)).toContain("卖家阿强");
|
|
152
|
+
expect(JSON.stringify(batch)).toContain("delivered");
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("sell order maps side=sell into the record", async () => {
|
|
156
|
+
const now = Date.now();
|
|
157
|
+
const p = writeSnapshot(tmpDir, {
|
|
158
|
+
schemaVersion: 1,
|
|
159
|
+
snapshottedAt: now,
|
|
160
|
+
events: [
|
|
161
|
+
{
|
|
162
|
+
kind: "order", id: "XY-SELL-1", orderId: "XY-SELL-1", side: "sell",
|
|
163
|
+
title: "出闲置耳机", counterparty: "买家小李",
|
|
164
|
+
placedAt: now, paidAt: now, status: "已完成",
|
|
165
|
+
totalAmount: { value: 120, currency: "CNY" },
|
|
166
|
+
},
|
|
167
|
+
],
|
|
168
|
+
});
|
|
169
|
+
const a = new XianyuAdapter();
|
|
170
|
+
const raws = [];
|
|
171
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
172
|
+
const batch = a.normalize(raws[0]);
|
|
173
|
+
expect(validateBatch(batch).valid).toBe(true);
|
|
174
|
+
expect(JSON.stringify(batch)).toContain("sell");
|
|
175
|
+
expect(JSON.stringify(batch)).toContain("买家小李");
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it("status mapping handles xianyu-typical strings", async () => {
|
|
179
|
+
const now = Date.now();
|
|
180
|
+
const cases = [
|
|
181
|
+
{ status: "已发货", expect: "shipped" },
|
|
182
|
+
{ status: "待收货", expect: "shipped" },
|
|
183
|
+
{ status: "交易成功", expect: "delivered" },
|
|
184
|
+
{ status: "已完成", expect: "delivered" },
|
|
185
|
+
{ status: "退款成功", expect: "refunded" },
|
|
186
|
+
{ status: "已关闭", expect: "cancelled" },
|
|
187
|
+
{ status: "等待付款", expect: "placed" },
|
|
188
|
+
];
|
|
189
|
+
for (const c of cases) {
|
|
190
|
+
const p = writeSnapshot(tmpDir, {
|
|
191
|
+
schemaVersion: 1,
|
|
192
|
+
snapshottedAt: now,
|
|
193
|
+
events: [
|
|
194
|
+
{ kind: "order", id: `o-${c.status}`, orderId: `o-${c.status}`, side: "buy",
|
|
195
|
+
title: "x", counterparty: "c", placedAt: now, paidAt: now, status: c.status,
|
|
196
|
+
totalAmount: { value: 1, currency: "CNY" } },
|
|
197
|
+
],
|
|
198
|
+
});
|
|
199
|
+
const a = new XianyuAdapter();
|
|
200
|
+
const raws = [];
|
|
201
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
202
|
+
const batch = a.normalize(raws[0]);
|
|
203
|
+
expect(JSON.stringify(batch)).toContain(c.expect);
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it("respects per-kind include opt-out", async () => {
|
|
208
|
+
const now = Date.now();
|
|
209
|
+
const p = writeSnapshot(tmpDir, {
|
|
210
|
+
schemaVersion: 1,
|
|
211
|
+
snapshottedAt: now,
|
|
212
|
+
events: [
|
|
213
|
+
{ kind: "order", id: "o1", orderId: "o1", side: "buy", title: "t", placedAt: now,
|
|
214
|
+
totalAmount: { value: 1, currency: "CNY" } },
|
|
215
|
+
],
|
|
216
|
+
});
|
|
217
|
+
const a = new XianyuAdapter();
|
|
218
|
+
const raws = [];
|
|
219
|
+
for await (const r of a.sync({ inputPath: p, include: { order: false } })) raws.push(r);
|
|
220
|
+
expect(raws.length).toBe(0);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it("respects opts.limit", async () => {
|
|
224
|
+
const now = Date.now();
|
|
225
|
+
const events = Array.from({ length: 5 }, (_, i) => ({
|
|
226
|
+
kind: "order", id: `o${i}`, orderId: `o${i}`, side: "buy", title: "t",
|
|
227
|
+
placedAt: now - i * 1000, totalAmount: { value: 1, currency: "CNY" },
|
|
228
|
+
}));
|
|
229
|
+
const p = writeSnapshot(tmpDir, { schemaVersion: 1, snapshottedAt: now, events });
|
|
230
|
+
const a = new XianyuAdapter();
|
|
231
|
+
const raws = [];
|
|
232
|
+
for await (const r of a.sync({ inputPath: p, limit: 2 })) raws.push(r);
|
|
233
|
+
expect(raws.length).toBe(2);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it("filters out unknown kinds (forward compat)", async () => {
|
|
237
|
+
const now = Date.now();
|
|
238
|
+
const p = writeSnapshot(tmpDir, {
|
|
239
|
+
schemaVersion: 1,
|
|
240
|
+
snapshottedAt: now,
|
|
241
|
+
events: [
|
|
242
|
+
{ kind: "order", id: "o1", orderId: "o1", side: "buy", title: "t", placedAt: now,
|
|
243
|
+
totalAmount: { value: 1, currency: "CNY" } },
|
|
244
|
+
{ kind: "message", id: "m1" },
|
|
245
|
+
{ kind: "future-kind", id: "x" },
|
|
246
|
+
],
|
|
247
|
+
});
|
|
248
|
+
const a = new XianyuAdapter();
|
|
249
|
+
const raws = [];
|
|
250
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
251
|
+
expect(raws.length).toBe(1);
|
|
252
|
+
expect(raws[0].kind).toBe("order");
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it("snapshottedAt fallback when event capturedAt missing", async () => {
|
|
256
|
+
const ts = 1700000000000;
|
|
257
|
+
const p = writeSnapshot(tmpDir, {
|
|
258
|
+
schemaVersion: 1,
|
|
259
|
+
snapshottedAt: ts,
|
|
260
|
+
events: [
|
|
261
|
+
{ kind: "order", id: "o1", orderId: "o1", side: "buy", title: "t",
|
|
262
|
+
totalAmount: { value: 1, currency: "CNY" } },
|
|
263
|
+
],
|
|
264
|
+
});
|
|
265
|
+
const a = new XianyuAdapter();
|
|
266
|
+
const raws = [];
|
|
267
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
268
|
+
expect(raws[0].capturedAt).toBe(ts);
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
it("advertises both snapshot and cookie-api capabilities", () => {
|
|
272
|
+
const a = new XianyuAdapter();
|
|
273
|
+
expect(a.capabilities).toContain("sync:snapshot");
|
|
274
|
+
expect(a.capabilities).toContain("sync:cookie-api");
|
|
275
|
+
expect(assertAdapter(a).ok).toBe(true);
|
|
276
|
+
});
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
describe("XianyuAdapter cookie-api mode", () => {
|
|
280
|
+
it("authenticate(cookie) ok when userId + cookies present", async () => {
|
|
281
|
+
const a = new XianyuAdapter({ account: { userId: "u-1", cookies: "_m_h5_tk=ok" } });
|
|
282
|
+
const res = await a.authenticate();
|
|
283
|
+
expect(res.ok).toBe(true);
|
|
284
|
+
expect(res.mode).toBe("cookie");
|
|
285
|
+
expect(res.account).toBe("u-1");
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
it("authenticate(cookie) requires account.userId", async () => {
|
|
289
|
+
const a = new XianyuAdapter({ account: { cookies: "_m_h5_tk=ok" } });
|
|
290
|
+
const res = await a.authenticate();
|
|
291
|
+
expect(res.ok).toBe(false);
|
|
292
|
+
expect(res.reason).toBe("NO_ACCOUNT_USER_ID");
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it("sync yields normalized buy+sell records from fetchFn fixture", async () => {
|
|
296
|
+
const byTab = {
|
|
297
|
+
buy: [
|
|
298
|
+
{ order_id: "XY-BUY-1", title: "二手相机", seller_nick: "卖家A", status_text: "交易成功",
|
|
299
|
+
total_amount: 1500, order_time: 1700000000, pay_time: 1700000010,
|
|
300
|
+
receiver: "李四", address: "广州..." },
|
|
301
|
+
],
|
|
302
|
+
sell: [
|
|
303
|
+
{ order_id: "XY-SELL-1", title: "出旧书", buyer_nick: "买家B", status_text: "交易成功",
|
|
304
|
+
total_amount: 30, order_time: 1700000500 },
|
|
305
|
+
],
|
|
306
|
+
};
|
|
307
|
+
const fetchFn = async (opts) => ({ orders: byTab[opts.query.tab] || [] });
|
|
308
|
+
const a = new XianyuAdapter({ account: { userId: "u-1", cookies: "_m_h5_tk=ok" }, fetchFn });
|
|
309
|
+
const raws = [];
|
|
310
|
+
for await (const r of a.sync({ sinceWatermark: 0 })) raws.push(r);
|
|
311
|
+
expect(raws.map((r) => r.originalId).sort()).toEqual(["XY-BUY-1", "XY-SELL-1"]);
|
|
312
|
+
const buy = raws.find((r) => r.originalId === "XY-BUY-1");
|
|
313
|
+
expect(buy.payload.record.extras.side).toBe("buy");
|
|
314
|
+
expect(buy.payload.record.merchantName).toBe("卖家A");
|
|
315
|
+
expect(buy.payload.record.totalAmount.value).toBe(1500);
|
|
316
|
+
expect(buy.payload.record.status).toBe("delivered");
|
|
317
|
+
const sell = raws.find((r) => r.originalId === "XY-SELL-1");
|
|
318
|
+
expect(sell.payload.record.extras.side).toBe("sell");
|
|
319
|
+
expect(sell.payload.record.merchantName).toBe("买家B");
|
|
320
|
+
|
|
321
|
+
const batch = a.normalize(buy);
|
|
322
|
+
expect(validateBatch(batch).valid).toBe(true);
|
|
323
|
+
expect(JSON.stringify(batch)).toContain("二手相机");
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
it("invokes signProvider and passes sign to fetchFn", async () => {
|
|
327
|
+
let seenSign = null;
|
|
328
|
+
const signProvider = async () => "SIGN-XYZ";
|
|
329
|
+
const fetchFn = async (opts) => {
|
|
330
|
+
seenSign = opts.sign;
|
|
331
|
+
return { orders: [] };
|
|
332
|
+
};
|
|
333
|
+
const a = new XianyuAdapter({
|
|
334
|
+
account: { userId: "u-1", cookies: "_m_h5_tk=ok" },
|
|
335
|
+
fetchFn,
|
|
336
|
+
signProvider,
|
|
337
|
+
});
|
|
338
|
+
for await (const _r of a.sync({ sinceWatermark: 0, sides: ["buy"] })) { /* drain */ }
|
|
339
|
+
expect(seenSign).toBe("SIGN-XYZ");
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
it("passes sign: null when no signProvider configured", async () => {
|
|
343
|
+
let seen = "unset";
|
|
344
|
+
const fetchFn = async (opts) => {
|
|
345
|
+
seen = opts.sign;
|
|
346
|
+
return { orders: [] };
|
|
347
|
+
};
|
|
348
|
+
const a = new XianyuAdapter({ account: { userId: "u-1", cookies: "_m_h5_tk=ok" }, fetchFn });
|
|
349
|
+
for await (const _r of a.sync({ sinceWatermark: 0, sides: ["buy"] })) { /* drain */ }
|
|
350
|
+
expect(seen).toBe(null);
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
it("paginates and stops at sinceWatermark within one side", async () => {
|
|
354
|
+
const pages = {
|
|
355
|
+
1: [
|
|
356
|
+
{ order_id: "p1-a", title: "t", order_time: 1700000000, total_amount: 10 },
|
|
357
|
+
{ order_id: "p1-b", title: "t", order_time: 1699000000, total_amount: 10 },
|
|
358
|
+
],
|
|
359
|
+
2: [{ order_id: "p2-a", title: "t", order_time: 1698000000, total_amount: 10 }],
|
|
360
|
+
};
|
|
361
|
+
const seenPages = [];
|
|
362
|
+
const fetchFn = async (opts) => {
|
|
363
|
+
seenPages.push(opts.query.pageNumber);
|
|
364
|
+
return { orders: pages[opts.query.pageNumber] || [] };
|
|
365
|
+
};
|
|
366
|
+
const a = new XianyuAdapter({ account: { userId: "u-1", cookies: "_m_h5_tk=ok" }, fetchFn });
|
|
367
|
+
const raws = [];
|
|
368
|
+
for await (const r of a.sync({ sinceWatermark: 1699500000 * 1000, pageSize: 2, sides: ["buy"] })) {
|
|
369
|
+
raws.push(r);
|
|
370
|
+
}
|
|
371
|
+
expect(raws.map((r) => r.originalId)).toEqual(["p1-a"]);
|
|
372
|
+
expect(seenPages).toEqual([1]);
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
it("respects per-kind include opt-out in cookie mode (no fetch)", async () => {
|
|
376
|
+
let called = false;
|
|
377
|
+
const fetchFn = async () => {
|
|
378
|
+
called = true;
|
|
379
|
+
return { orders: [] };
|
|
380
|
+
};
|
|
381
|
+
const a = new XianyuAdapter({ account: { userId: "u-1", cookies: "_m_h5_tk=ok" }, fetchFn });
|
|
382
|
+
const raws = [];
|
|
383
|
+
for await (const r of a.sync({ include: { order: false } })) raws.push(r);
|
|
384
|
+
expect(raws.length).toBe(0);
|
|
385
|
+
expect(called).toBe(false);
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
it("normSide canonicalizes buy/sell tokens", () => {
|
|
389
|
+
expect(normSide("buy")).toBe("buy");
|
|
390
|
+
expect(normSide("sell")).toBe("sell");
|
|
391
|
+
expect(normSide("卖出")).toBe("sell");
|
|
392
|
+
expect(normSide("我买到的")).toBe("buy");
|
|
393
|
+
expect(normSide(null, { bizType: "sold" })).toBe("sell");
|
|
394
|
+
expect(normSide(undefined, {})).toBe("buy"); // default
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
it("orderToRecord maps xianyu buy fields (counterparty=seller)", () => {
|
|
398
|
+
const rec = orderToRecord({
|
|
399
|
+
order_id: "XY-9",
|
|
400
|
+
title: "二手键盘",
|
|
401
|
+
seller_nick: "卖家C",
|
|
402
|
+
status_text: "已完成",
|
|
403
|
+
total_amount: 88.0,
|
|
404
|
+
order_time: 1700000000,
|
|
405
|
+
}, "buy");
|
|
406
|
+
expect(rec.orderId).toBe("XY-9");
|
|
407
|
+
expect(rec.merchantName).toBe("卖家C");
|
|
408
|
+
expect(rec.status).toBe("delivered");
|
|
409
|
+
expect(rec.totalAmount.value).toBe(88.0);
|
|
410
|
+
expect(rec.items[0].name).toBe("二手键盘");
|
|
411
|
+
expect(rec.extras.side).toBe("buy");
|
|
412
|
+
expect(rec.extras.capturedBy).toBe("cookie-api");
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
it("extractOrders tolerates nested mtop response shapes", () => {
|
|
416
|
+
expect(extractOrders({ orders: [1] })).toEqual([1]);
|
|
417
|
+
expect(extractOrders({ order_list: [2] })).toEqual([2]);
|
|
418
|
+
expect(extractOrders({ data: { orderList: [3] } })).toEqual([3]);
|
|
419
|
+
expect(extractOrders({ data: { list: [4] } })).toEqual([4]);
|
|
420
|
+
expect(extractOrders([5])).toEqual([5]);
|
|
421
|
+
expect(extractOrders({})).toEqual([]);
|
|
422
|
+
expect(extractOrders(null)).toEqual([]);
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
it("uses opts.ordersUrl override when provided", async () => {
|
|
426
|
+
let seenUrl = null;
|
|
427
|
+
const fetchFn = async (opts) => {
|
|
428
|
+
seenUrl = opts.url;
|
|
429
|
+
return { orders: [] };
|
|
430
|
+
};
|
|
431
|
+
const a = new XianyuAdapter({
|
|
432
|
+
account: { userId: "u-1", cookies: "_m_h5_tk=ok" },
|
|
433
|
+
fetchFn,
|
|
434
|
+
ordersUrl: "https://custom.example/orders",
|
|
435
|
+
});
|
|
436
|
+
for await (const _r of a.sync({ sinceWatermark: 0, sides: ["buy"] })) { /* drain */ }
|
|
437
|
+
expect(seenUrl).toBe("https://custom.example/orders");
|
|
438
|
+
});
|
|
439
|
+
|
|
440
|
+
it("default fetchFn throws a legible error when cookie mode used without injection", async () => {
|
|
441
|
+
const a = new XianyuAdapter({ account: { userId: "u-1", cookies: "_m_h5_tk=ok" } });
|
|
442
|
+
let threw = null;
|
|
443
|
+
try {
|
|
444
|
+
for await (const _r of a.sync({ sinceWatermark: 0 })) { /* drain */ }
|
|
445
|
+
} catch (err) {
|
|
446
|
+
threw = err;
|
|
447
|
+
}
|
|
448
|
+
expect(threw).toBeTruthy();
|
|
449
|
+
expect(String(threw.message)).toMatch(/no fetchFn configured/);
|
|
450
|
+
});
|
|
451
|
+
});
|