@chainlesschain/personal-data-hub 0.4.7 → 0.4.23
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/biz-tianyancha.test.js +159 -0
- package/__tests__/adapters/doc-baidu-netdisk.test.js +102 -0
- package/__tests__/adapters/doc-camscanner.test.js +147 -0
- package/__tests__/adapters/doc-platforms.test.js +177 -0
- package/__tests__/adapters/gov-ixiamen.test.js +150 -0
- package/__tests__/adapters/gov-tax.test.js +135 -0
- package/__tests__/adapters/health-meiyou.test.js +125 -0
- package/__tests__/adapters/music-kugou.test.js +187 -0
- package/__tests__/adapters/recruit-boss.test.js +180 -0
- package/__tests__/adapters/shopping-dianping.test.js +239 -0
- package/__tests__/adapters/social-csdn.test.js +175 -0
- package/__tests__/adapters/social-dongchedi.test.js +165 -0
- package/__tests__/adapters/social-zhihu.test.js +246 -0
- package/__tests__/adapters/travel-ctrip.test.js +175 -1
- package/__tests__/adapters/travel-didi.test.js +204 -0
- package/__tests__/adapters/travel-tongcheng.test.js +289 -0
- package/__tests__/adapters/video-platforms.test.js +152 -0
- package/__tests__/adapters/video-xigua.test.js +106 -0
- package/__tests__/adapters/wework-pc.test.js +124 -0
- package/lib/adapter-guide.js +25 -3
- package/lib/adapters/_document-base.js +370 -0
- package/lib/adapters/_video-base.js +331 -0
- package/lib/adapters/biz-tianyancha/index.js +348 -0
- package/lib/adapters/doc-baidu-netdisk/index.js +91 -0
- package/lib/adapters/doc-camscanner/index.js +102 -0
- package/lib/adapters/doc-tencent-docs/index.js +94 -0
- package/lib/adapters/doc-wps/index.js +77 -0
- package/lib/adapters/gov-ixiamen/index.js +380 -0
- package/lib/adapters/gov-tax/index.js +451 -0
- package/lib/adapters/health-meiyou/index.js +393 -0
- package/lib/adapters/music-kugou/index.js +418 -0
- package/lib/adapters/recruit-boss/index.js +442 -0
- package/lib/adapters/shopping-dianping/index.js +473 -0
- package/lib/adapters/social-csdn/index.js +444 -0
- package/lib/adapters/social-dongchedi/index.js +360 -0
- package/lib/adapters/social-zhihu/index.js +488 -0
- package/lib/adapters/travel-ctrip/index.js +255 -40
- package/lib/adapters/travel-didi/index.js +327 -0
- package/lib/adapters/travel-tongcheng/index.js +393 -0
- package/lib/adapters/video-iqiyi/index.js +75 -0
- package/lib/adapters/video-tencent/index.js +78 -0
- package/lib/adapters/video-xigua/index.js +68 -0
- package/lib/adapters/wework-pc/index.js +31 -0
- package/lib/index.js +40 -0
- package/package.json +1 -1
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { describe, it, expect } from "vitest";
|
|
4
|
+
const fs = require("node:fs");
|
|
5
|
+
const path = require("node:path");
|
|
6
|
+
const os = require("node:os");
|
|
7
|
+
const crypto = require("node:crypto");
|
|
8
|
+
|
|
9
|
+
const {
|
|
10
|
+
DongchediAdapter,
|
|
11
|
+
extractData,
|
|
12
|
+
isEnd,
|
|
13
|
+
NAME,
|
|
14
|
+
VERSION,
|
|
15
|
+
SNAPSHOT_SCHEMA_VERSION,
|
|
16
|
+
} = require("../../lib/adapters/social-dongchedi");
|
|
17
|
+
|
|
18
|
+
function writeTmp(content) {
|
|
19
|
+
const p = path.join(os.tmpdir(), `cc-dcd-${crypto.randomUUID()}.json`);
|
|
20
|
+
fs.writeFileSync(p, content, "utf-8");
|
|
21
|
+
return p;
|
|
22
|
+
}
|
|
23
|
+
async function collect(gen) {
|
|
24
|
+
const out = [];
|
|
25
|
+
for await (const x of gen) out.push(x);
|
|
26
|
+
return out;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const COOKIES = "tt_webid=abc; sessionid=xyz";
|
|
30
|
+
|
|
31
|
+
const SNAP = JSON.stringify({
|
|
32
|
+
schemaVersion: 1,
|
|
33
|
+
snapshottedAt: 1716383000000,
|
|
34
|
+
account: { userId: "u1" },
|
|
35
|
+
events: [
|
|
36
|
+
{ kind: "favourite", id: "fav-1", itemId: "G1", title: "2026 新能源车横评", contentType: "article", url: "https://x/G1", capturedAt: 1716300000000 },
|
|
37
|
+
{ kind: "follow", id: "follow-S1", followId: "S1", name: "理想 L 系列", followType: "series", capturedAt: 1716320000000 },
|
|
38
|
+
],
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
describe("constants + helpers", () => {
|
|
42
|
+
it("name/version/schema", () => {
|
|
43
|
+
expect(NAME).toBe("social-dongchedi");
|
|
44
|
+
expect(VERSION).toBe("0.1.0");
|
|
45
|
+
expect(SNAPSHOT_SCHEMA_VERSION).toBe(1);
|
|
46
|
+
});
|
|
47
|
+
it("extractData tolerant", () => {
|
|
48
|
+
expect(extractData({ data: [{ id: 1 }] })).toHaveLength(1);
|
|
49
|
+
expect(extractData({ data: { favorite_list: [{ id: 1 }] } })).toHaveLength(1);
|
|
50
|
+
expect(extractData({ data: { follow_list: [{ id: 1 }] } })).toHaveLength(1);
|
|
51
|
+
expect(extractData({})).toEqual([]);
|
|
52
|
+
});
|
|
53
|
+
it("isEnd reads has_more", () => {
|
|
54
|
+
expect(isEnd({ data: { has_more: false } })).toBe(true);
|
|
55
|
+
expect(isEnd({ has_more: 0 })).toBe(true);
|
|
56
|
+
expect(isEnd({ data: { has_more: true } })).toBe(false);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe("DongchediAdapter snapshot mode", () => {
|
|
61
|
+
it("authenticate validates inputPath", async () => {
|
|
62
|
+
const p = writeTmp(SNAP);
|
|
63
|
+
try {
|
|
64
|
+
const a = new DongchediAdapter();
|
|
65
|
+
expect((await a.authenticate({ inputPath: p })).mode).toBe("snapshot-file");
|
|
66
|
+
expect((await a.authenticate({ inputPath: path.join(os.tmpdir(), "no-dcd.json") })).reason).toBe("INPUT_PATH_UNREADABLE");
|
|
67
|
+
} finally {
|
|
68
|
+
fs.unlinkSync(p);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("sync 2 kinds + normalize favourite→like / follow→person", async () => {
|
|
73
|
+
const p = writeTmp(SNAP);
|
|
74
|
+
try {
|
|
75
|
+
const a = new DongchediAdapter();
|
|
76
|
+
const items = await collect(a.sync({ inputPath: p }));
|
|
77
|
+
expect(items.map((x) => x.kind)).toEqual(["favourite", "follow"]);
|
|
78
|
+
|
|
79
|
+
const fav = a.normalize(items[0]);
|
|
80
|
+
expect(fav.events[0].subtype).toBe("like");
|
|
81
|
+
expect(fav.events[0].content.title).toBe("收藏: 2026 新能源车横评");
|
|
82
|
+
expect(fav.events[0].extra.contentType).toBe("article");
|
|
83
|
+
|
|
84
|
+
const fol = a.normalize(items[1]);
|
|
85
|
+
expect(fol.persons[0].subtype).toBe("contact");
|
|
86
|
+
expect(fol.persons[0].names).toEqual(["理想 L 系列"]);
|
|
87
|
+
expect(fol.persons[0].identifiers["dongchedi-id"]).toEqual(["S1"]);
|
|
88
|
+
expect(fol.persons[0].extra.followType).toBe("series");
|
|
89
|
+
} finally {
|
|
90
|
+
fs.unlinkSync(p);
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("include + limit + schema mismatch + unknown kind", async () => {
|
|
95
|
+
const p = writeTmp(SNAP);
|
|
96
|
+
try {
|
|
97
|
+
const a = new DongchediAdapter();
|
|
98
|
+
expect((await collect(a.sync({ inputPath: p, include: { favourite: false } }))).map((x) => x.kind)).toEqual(["follow"]);
|
|
99
|
+
expect(await collect(a.sync({ inputPath: p, limit: 1 }))).toHaveLength(1);
|
|
100
|
+
expect(() => a.normalize({ kind: "bogus", payload: {} })).toThrow(/unknown kind/);
|
|
101
|
+
} finally {
|
|
102
|
+
fs.unlinkSync(p);
|
|
103
|
+
}
|
|
104
|
+
const bad = writeTmp(JSON.stringify({ schemaVersion: 9, events: [] }));
|
|
105
|
+
try {
|
|
106
|
+
const a = new DongchediAdapter();
|
|
107
|
+
await expect(collect(a.sync({ inputPath: bad }))).rejects.toThrow(/schemaVersion mismatch/);
|
|
108
|
+
} finally {
|
|
109
|
+
fs.unlinkSync(bad);
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
describe("DongchediAdapter cookie-api mode", () => {
|
|
115
|
+
it("authenticate cookie (userId optional)", async () => {
|
|
116
|
+
const a = new DongchediAdapter({ account: { cookies: COOKIES } });
|
|
117
|
+
expect(await a.authenticate()).toEqual({ ok: true, account: null, mode: "cookie" });
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("sync fetches favourites + follows, normalizes", async () => {
|
|
121
|
+
const byUrl = (u) => (u.includes("favorite") ? "favourite" : "follow");
|
|
122
|
+
const data = {
|
|
123
|
+
favourite: [{ group_id: "G1", title: "试驾视频", content_type: "video", create_time: 1716300000 }],
|
|
124
|
+
follow: [{ series_id: "S9", series_name: "比亚迪汉", follow_time: 1716320000 }],
|
|
125
|
+
};
|
|
126
|
+
const calls = [];
|
|
127
|
+
const a = new DongchediAdapter({
|
|
128
|
+
account: { cookies: COOKIES, userId: "u1" },
|
|
129
|
+
fetchFn: async ({ url, cookies, query, sign }) => {
|
|
130
|
+
const k = byUrl(url);
|
|
131
|
+
calls.push({ k, cookies, offset: query.offset, sign });
|
|
132
|
+
return { data: { list: query.offset === 0 ? data[k] : [], has_more: false } };
|
|
133
|
+
},
|
|
134
|
+
});
|
|
135
|
+
const items = await collect(a.sync({}));
|
|
136
|
+
expect(items.map((x) => x.kind).sort()).toEqual(["favourite", "follow"]);
|
|
137
|
+
expect(calls.every((c) => c.cookies === COOKIES && c.sign === null)).toBe(true);
|
|
138
|
+
const fav = a.normalize(items.find((x) => x.kind === "favourite"));
|
|
139
|
+
expect(fav.events[0].content.title).toBe("收藏: 试驾视频");
|
|
140
|
+
const fol = a.normalize(items.find((x) => x.kind === "follow"));
|
|
141
|
+
expect(fol.persons[0].names).toEqual(["比亚迪汉"]);
|
|
142
|
+
expect(fol.persons[0].extra.followType).toBe("series");
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("invokes signProvider + limit + empty + default fetch + no input", async () => {
|
|
146
|
+
const signCalls = [];
|
|
147
|
+
const a = new DongchediAdapter({
|
|
148
|
+
account: { cookies: COOKIES },
|
|
149
|
+
fetchFn: async ({ query }) => ({ data: { list: query.offset === 0 ? [{ group_id: "G1", title: "x" }, { group_id: "G2", title: "y" }] : [], has_more: false } }),
|
|
150
|
+
signProvider: async (ctx) => { signCalls.push(ctx); return "x-bogus"; },
|
|
151
|
+
});
|
|
152
|
+
expect(await collect(a.sync({ limit: 1, include: { follow: false } }))).toHaveLength(1);
|
|
153
|
+
expect(signCalls.length).toBeGreaterThan(0);
|
|
154
|
+
expect(signCalls[0].cookies).toBe(COOKIES);
|
|
155
|
+
|
|
156
|
+
const a2 = new DongchediAdapter({ account: { cookies: COOKIES }, fetchFn: async () => "<html>login</html>" });
|
|
157
|
+
expect(await collect(a2.sync({}))).toEqual([]);
|
|
158
|
+
|
|
159
|
+
const a3 = new DongchediAdapter({ account: { cookies: COOKIES } });
|
|
160
|
+
await expect(collect(a3.sync({}))).rejects.toThrow(/no fetchFn configured/);
|
|
161
|
+
|
|
162
|
+
const a4 = new DongchediAdapter();
|
|
163
|
+
await expect(collect(a4.sync({}))).rejects.toThrow(/needs opts.inputPath/);
|
|
164
|
+
});
|
|
165
|
+
});
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { describe, it, expect } from "vitest";
|
|
4
|
+
const fs = require("node:fs");
|
|
5
|
+
const path = require("node:path");
|
|
6
|
+
const os = require("node:os");
|
|
7
|
+
const crypto = require("node:crypto");
|
|
8
|
+
|
|
9
|
+
const {
|
|
10
|
+
ZhihuAdapter,
|
|
11
|
+
extractData,
|
|
12
|
+
NAME,
|
|
13
|
+
VERSION,
|
|
14
|
+
SNAPSHOT_SCHEMA_VERSION,
|
|
15
|
+
} = require("../../lib/adapters/social-zhihu");
|
|
16
|
+
|
|
17
|
+
function writeTmp(content) {
|
|
18
|
+
const p = path.join(os.tmpdir(), `cc-zhihu-${crypto.randomUUID()}.json`);
|
|
19
|
+
fs.writeFileSync(p, content, "utf-8");
|
|
20
|
+
return p;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async function collect(gen) {
|
|
24
|
+
const out = [];
|
|
25
|
+
for await (const x of gen) out.push(x);
|
|
26
|
+
return out;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const COOKIES = "z_c0=abc; d_c0=xyz";
|
|
30
|
+
|
|
31
|
+
const SNAPSHOT = JSON.stringify({
|
|
32
|
+
schemaVersion: 1,
|
|
33
|
+
snapshottedAt: 1716383000000,
|
|
34
|
+
account: { urlToken: "alice", name: "Alice" },
|
|
35
|
+
events: [
|
|
36
|
+
{
|
|
37
|
+
kind: "answer",
|
|
38
|
+
id: "answer-101",
|
|
39
|
+
answerId: "101",
|
|
40
|
+
questionTitle: "如何评价 X?",
|
|
41
|
+
excerpt: "<p>我认为 X 很好</p>",
|
|
42
|
+
voteupCount: 42,
|
|
43
|
+
commentCount: 3,
|
|
44
|
+
createdTime: 1716300000,
|
|
45
|
+
url: "https://www.zhihu.com/answer/101",
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
kind: "favourite",
|
|
49
|
+
id: "fav-202",
|
|
50
|
+
itemId: "202",
|
|
51
|
+
title: "好文收藏",
|
|
52
|
+
collectionName: "技术",
|
|
53
|
+
capturedAt: 1716310000000,
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
kind: "follow",
|
|
57
|
+
id: "follow-bob",
|
|
58
|
+
memberToken: "bob",
|
|
59
|
+
name: "Bob",
|
|
60
|
+
headline: "工程师",
|
|
61
|
+
avatarUrl: "https://pic/bob.jpg",
|
|
62
|
+
capturedAt: 1716320000000,
|
|
63
|
+
},
|
|
64
|
+
],
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
describe("constants", () => {
|
|
68
|
+
it("exposes name/version/schema", () => {
|
|
69
|
+
expect(NAME).toBe("social-zhihu");
|
|
70
|
+
expect(VERSION).toBe("0.1.0");
|
|
71
|
+
expect(SNAPSHOT_SCHEMA_VERSION).toBe(1);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
describe("extractData", () => {
|
|
76
|
+
it("pulls data/items arrays; tolerant of bad shapes", () => {
|
|
77
|
+
expect(extractData({ data: [{ id: 1 }] })).toHaveLength(1);
|
|
78
|
+
expect(extractData({ items: [{ id: 1 }] })).toHaveLength(1);
|
|
79
|
+
expect(extractData({})).toEqual([]);
|
|
80
|
+
expect(extractData(null)).toEqual([]);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
describe("ZhihuAdapter snapshot mode", () => {
|
|
85
|
+
it("authenticate validates inputPath readability", async () => {
|
|
86
|
+
const p = writeTmp(SNAPSHOT);
|
|
87
|
+
try {
|
|
88
|
+
const a = new ZhihuAdapter();
|
|
89
|
+
expect((await a.authenticate({ inputPath: p })).mode).toBe("snapshot-file");
|
|
90
|
+
const bad = await a.authenticate({ inputPath: path.join(os.tmpdir(), "nope-z.json") });
|
|
91
|
+
expect(bad.ok).toBe(false);
|
|
92
|
+
expect(bad.reason).toBe("INPUT_PATH_UNREADABLE");
|
|
93
|
+
} finally {
|
|
94
|
+
fs.unlinkSync(p);
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("sync yields all 3 kinds + normalize answer→post, favourite→like, follow→person", async () => {
|
|
99
|
+
const p = writeTmp(SNAPSHOT);
|
|
100
|
+
try {
|
|
101
|
+
const a = new ZhihuAdapter();
|
|
102
|
+
const real = await collect(a.sync({ inputPath: p }));
|
|
103
|
+
expect(real).toHaveLength(3);
|
|
104
|
+
expect(real.map((x) => x.kind)).toEqual(["answer", "favourite", "follow"]);
|
|
105
|
+
|
|
106
|
+
const ans = a.normalize(real[0]);
|
|
107
|
+
expect(ans.events[0].subtype).toBe("post");
|
|
108
|
+
expect(ans.events[0].content.text).toBe("我认为 X 很好"); // html stripped
|
|
109
|
+
expect(ans.events[0].extra.voteupCount).toBe(42);
|
|
110
|
+
expect(ans.events[0].extra.questionTitle).toBe("如何评价 X?");
|
|
111
|
+
|
|
112
|
+
const fav = a.normalize(real[1]);
|
|
113
|
+
expect(fav.events[0].subtype).toBe("like");
|
|
114
|
+
expect(fav.events[0].content.title).toBe("好文收藏");
|
|
115
|
+
|
|
116
|
+
const fol = a.normalize(real[2]);
|
|
117
|
+
expect(fol.persons[0].subtype).toBe("contact");
|
|
118
|
+
expect(fol.persons[0].names).toEqual(["Bob"]);
|
|
119
|
+
expect(fol.persons[0].identifiers["zhihu-token"]).toEqual(["bob"]);
|
|
120
|
+
} finally {
|
|
121
|
+
fs.unlinkSync(p);
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("respects include filter + limit", async () => {
|
|
126
|
+
const p = writeTmp(SNAPSHOT);
|
|
127
|
+
try {
|
|
128
|
+
const a = new ZhihuAdapter();
|
|
129
|
+
const onlyFollow = await collect(a.sync({ inputPath: p, include: { answer: false, favourite: false } }));
|
|
130
|
+
expect(onlyFollow.map((x) => x.kind)).toEqual(["follow"]);
|
|
131
|
+
const limited = await collect(a.sync({ inputPath: p, limit: 1 }));
|
|
132
|
+
expect(limited).toHaveLength(1);
|
|
133
|
+
} finally {
|
|
134
|
+
fs.unlinkSync(p);
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("schemaVersion mismatch throws", async () => {
|
|
139
|
+
const p = writeTmp(JSON.stringify({ schemaVersion: 9, events: [] }));
|
|
140
|
+
try {
|
|
141
|
+
const a = new ZhihuAdapter();
|
|
142
|
+
await expect(collect(a.sync({ inputPath: p }))).rejects.toThrow(/schemaVersion mismatch/);
|
|
143
|
+
} finally {
|
|
144
|
+
fs.unlinkSync(p);
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it("normalize throws on unknown kind", () => {
|
|
149
|
+
const a = new ZhihuAdapter();
|
|
150
|
+
expect(() => a.normalize({ kind: "bogus", payload: {} })).toThrow(/unknown kind/);
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
describe("ZhihuAdapter cookie-api mode", () => {
|
|
155
|
+
it("authenticate cookie mode requires urlToken", async () => {
|
|
156
|
+
const noTok = new ZhihuAdapter({ account: { cookies: COOKIES } });
|
|
157
|
+
expect((await noTok.authenticate()).reason).toBe("NO_ACCOUNT_URL_TOKEN");
|
|
158
|
+
const ok = new ZhihuAdapter({ account: { cookies: COOKIES, urlToken: "alice" } });
|
|
159
|
+
expect(await ok.authenticate()).toEqual({ ok: true, account: "alice", mode: "cookie" });
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it("sync fetches answers/followees/collections, paginates, normalizes", async () => {
|
|
163
|
+
const byUrl = (url) => {
|
|
164
|
+
if (url.includes("/answers")) return "answers";
|
|
165
|
+
if (url.includes("/followees")) return "followees";
|
|
166
|
+
if (url.includes("/collections")) return "collections";
|
|
167
|
+
return "?";
|
|
168
|
+
};
|
|
169
|
+
const data = {
|
|
170
|
+
answers: [
|
|
171
|
+
{ id: "A1", question: { title: "Q1" }, excerpt: "ans1", voteup_count: 9, created_time: 1716300000 },
|
|
172
|
+
],
|
|
173
|
+
followees: [{ url_token: "bob", name: "Bob", headline: "eng" }],
|
|
174
|
+
collections: [{ id: "C1", title: "我的收藏", is_public: true }],
|
|
175
|
+
};
|
|
176
|
+
const calls = [];
|
|
177
|
+
const fetchFn = async ({ url, cookies, query, sign }) => {
|
|
178
|
+
const k = byUrl(url);
|
|
179
|
+
calls.push({ k, cookies, offset: query.offset, sign });
|
|
180
|
+
// single page each
|
|
181
|
+
return { data: query.offset === 0 ? data[k] : [], paging: { is_end: query.offset !== 0 } };
|
|
182
|
+
};
|
|
183
|
+
const a = new ZhihuAdapter({ account: { cookies: COOKIES, urlToken: "alice" }, fetchFn });
|
|
184
|
+
const items = await collect(a.sync({}));
|
|
185
|
+
expect(items.map((x) => x.kind).sort()).toEqual(["answer", "favourite", "follow"]);
|
|
186
|
+
expect(calls.every((c) => c.cookies === COOKIES)).toBe(true);
|
|
187
|
+
expect(calls.every((c) => c.sign === null)).toBe(true);
|
|
188
|
+
|
|
189
|
+
const ans = a.normalize(items.find((x) => x.kind === "answer"));
|
|
190
|
+
expect(ans.events[0].content.title).toBe("Q1");
|
|
191
|
+
expect(ans.events[0].extra.voteupCount).toBe(9);
|
|
192
|
+
const fol = a.normalize(items.find((x) => x.kind === "follow"));
|
|
193
|
+
expect(fol.persons[0].names).toEqual(["Bob"]);
|
|
194
|
+
expect(fol.persons[0].identifiers["zhihu-token"]).toEqual(["bob"]);
|
|
195
|
+
const fav = a.normalize(items.find((x) => x.kind === "favourite"));
|
|
196
|
+
expect(fav.events[0].content.title).toBe("我的收藏");
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("invokes signProvider when configured", async () => {
|
|
200
|
+
const signCalls = [];
|
|
201
|
+
const a = new ZhihuAdapter({
|
|
202
|
+
account: { cookies: COOKIES, urlToken: "alice" },
|
|
203
|
+
fetchFn: async ({ query }) =>
|
|
204
|
+
query.offset === 0 ? { data: [{ id: "A1", question: { title: "Q" }, excerpt: "x" }], paging: { is_end: true } } : { data: [], paging: { is_end: true } },
|
|
205
|
+
signProvider: async (ctx) => {
|
|
206
|
+
signCalls.push(ctx);
|
|
207
|
+
return "x-zse-96-value";
|
|
208
|
+
},
|
|
209
|
+
// only answers to keep it short
|
|
210
|
+
});
|
|
211
|
+
const items = await collect(a.sync({ include: { follow: false, favourite: false } }));
|
|
212
|
+
expect(items.length).toBeGreaterThan(0);
|
|
213
|
+
expect(signCalls.length).toBeGreaterThan(0);
|
|
214
|
+
expect(signCalls[0].cookies).toBe(COOKIES);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it("respects opts.limit across kinds", async () => {
|
|
218
|
+
const a = new ZhihuAdapter({
|
|
219
|
+
account: { cookies: COOKIES, urlToken: "alice" },
|
|
220
|
+
fetchFn: async ({ query }) =>
|
|
221
|
+
query.offset === 0
|
|
222
|
+
? { data: [{ id: "A1", question: { title: "Q" }, excerpt: "x" }, { id: "A2", question: { title: "Q2" }, excerpt: "y" }], paging: { is_end: true } }
|
|
223
|
+
: { data: [], paging: { is_end: true } },
|
|
224
|
+
});
|
|
225
|
+
const items = await collect(a.sync({ limit: 1 }));
|
|
226
|
+
expect(items).toHaveLength(1);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it("is_end stops pagination; empty data yields zero (no crash)", async () => {
|
|
230
|
+
const a = new ZhihuAdapter({
|
|
231
|
+
account: { cookies: COOKIES, urlToken: "alice" },
|
|
232
|
+
fetchFn: async () => "not-json-login-redirect",
|
|
233
|
+
});
|
|
234
|
+
expect(await collect(a.sync({}))).toEqual([]);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it("default fetch throws when no fetchFn", async () => {
|
|
238
|
+
const a = new ZhihuAdapter({ account: { cookies: COOKIES, urlToken: "alice" } });
|
|
239
|
+
await expect(collect(a.sync({}))).rejects.toThrow(/no fetchFn configured/);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it("sync with no input/cookie throws", async () => {
|
|
243
|
+
const a = new ZhihuAdapter();
|
|
244
|
+
await expect(collect(a.sync({}))).rejects.toThrow(/needs opts.inputPath/);
|
|
245
|
+
});
|
|
246
|
+
});
|
|
@@ -9,6 +9,8 @@ const crypto = require("node:crypto");
|
|
|
9
9
|
const {
|
|
10
10
|
CtripAdapter,
|
|
11
11
|
parseRecords,
|
|
12
|
+
orderToRecord,
|
|
13
|
+
extractOrders,
|
|
12
14
|
TYPE_MAP,
|
|
13
15
|
NAME,
|
|
14
16
|
VERSION,
|
|
@@ -43,7 +45,7 @@ const FLIGHT_ORDER = {
|
|
|
43
45
|
describe("constants + TYPE_MAP", () => {
|
|
44
46
|
it("exposes name/version", () => {
|
|
45
47
|
expect(NAME).toBe("travel-ctrip");
|
|
46
|
-
expect(VERSION).toBe("0.
|
|
48
|
+
expect(VERSION).toBe("0.7.0");
|
|
47
49
|
});
|
|
48
50
|
|
|
49
51
|
it("maps ctrip order types to vehicleType", () => {
|
|
@@ -201,3 +203,175 @@ describe("CtripAdapter", () => {
|
|
|
201
203
|
);
|
|
202
204
|
});
|
|
203
205
|
});
|
|
206
|
+
|
|
207
|
+
describe("orderToRecord web-API aliases (cookie-api mode)", () => {
|
|
208
|
+
it("maps web order field names (bizType / amount / orderDate / departCity)", () => {
|
|
209
|
+
const rec = orderToRecord(
|
|
210
|
+
{
|
|
211
|
+
orderId: "W1",
|
|
212
|
+
bizType: "Flight",
|
|
213
|
+
departCity: "广州",
|
|
214
|
+
arriveCity: "成都",
|
|
215
|
+
amount: "899.5",
|
|
216
|
+
departureDate: 1716383021000,
|
|
217
|
+
contactName: "王五",
|
|
218
|
+
orderDate: "2026-04-01",
|
|
219
|
+
},
|
|
220
|
+
{ capturedVia: "cookie-api" },
|
|
221
|
+
);
|
|
222
|
+
expect(rec).toMatchObject({
|
|
223
|
+
vendorId: "ctrip",
|
|
224
|
+
recordId: "W1",
|
|
225
|
+
vehicleType: "flight",
|
|
226
|
+
from: { city: "广州" },
|
|
227
|
+
to: { city: "成都" },
|
|
228
|
+
departureMs: 1716383021000,
|
|
229
|
+
traveler: "王五",
|
|
230
|
+
});
|
|
231
|
+
expect(rec.totalCost).toEqual({ value: 899.5, currency: "CNY" });
|
|
232
|
+
expect(rec.extras.capturedVia).toBe("cookie-api");
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it("file-import rows keep priority + no capturedVia by default", () => {
|
|
236
|
+
const rec = orderToRecord({ orderId: "F1", type: "hotel", price: "300" });
|
|
237
|
+
expect(rec.vehicleType).toBe("hotel");
|
|
238
|
+
expect(rec.totalCost).toEqual({ value: 300, currency: "CNY" });
|
|
239
|
+
expect(rec.extras.capturedVia).toBeUndefined();
|
|
240
|
+
});
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
describe("extractOrders", () => {
|
|
244
|
+
it("pulls the list from common Ctrip response shapes", () => {
|
|
245
|
+
expect(extractOrders({ orders: [{ orderId: "A" }] })).toHaveLength(1);
|
|
246
|
+
expect(extractOrders({ data: { orderList: [{ orderId: "B" }] } })).toHaveLength(1);
|
|
247
|
+
expect(extractOrders({ result: { list: [{ orderId: "C" }] } })).toHaveLength(1);
|
|
248
|
+
expect(extractOrders({})).toEqual([]);
|
|
249
|
+
expect(extractOrders(null)).toEqual([]);
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
describe("CtripAdapter cookie-api mode", () => {
|
|
254
|
+
const COOKIES = "_bfa=abc; cticket=xyz; UUID=u1";
|
|
255
|
+
|
|
256
|
+
it("authenticate returns cookie mode (account OPTIONAL)", async () => {
|
|
257
|
+
const a = new CtripAdapter({ account: { cookies: COOKIES } });
|
|
258
|
+
expect(await a.authenticate()).toEqual({
|
|
259
|
+
ok: true,
|
|
260
|
+
account: null,
|
|
261
|
+
mode: "cookie",
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
it("authenticate fails on empty cookies", async () => {
|
|
266
|
+
const a = new CtripAdapter({ account: { cookies: "" } });
|
|
267
|
+
// empty cookies → no cookieAuth → falls through to ready (not cookie mode)
|
|
268
|
+
expect((await a.authenticate()).mode).toBe("ready");
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
it("sync fetches, paginates, maps + normalizes end-to-end", async () => {
|
|
272
|
+
const pages = [
|
|
273
|
+
{
|
|
274
|
+
orderList: [
|
|
275
|
+
{
|
|
276
|
+
orderId: "CK1",
|
|
277
|
+
bizType: "flight",
|
|
278
|
+
departCity: "上海",
|
|
279
|
+
arriveCity: "北京",
|
|
280
|
+
amount: "1200",
|
|
281
|
+
orderDate: 1716383021000,
|
|
282
|
+
flightNumber: "MU500",
|
|
283
|
+
},
|
|
284
|
+
],
|
|
285
|
+
},
|
|
286
|
+
{ orderList: [] }, // page 2 empty → stop
|
|
287
|
+
];
|
|
288
|
+
const calls = [];
|
|
289
|
+
const fetchFn = async ({ url, cookies, query, sign }) => {
|
|
290
|
+
calls.push({ url, cookies, pageIndex: query.pageIndex, sign });
|
|
291
|
+
return pages[query.pageIndex - 1] || { orderList: [] };
|
|
292
|
+
};
|
|
293
|
+
const a = new CtripAdapter({
|
|
294
|
+
account: { cookies: COOKIES },
|
|
295
|
+
fetchFn,
|
|
296
|
+
// no signProvider → sign should be null (best-effort unsigned)
|
|
297
|
+
});
|
|
298
|
+
const items = await collect(a.sync({ sinceWatermark: 0 }));
|
|
299
|
+
expect(items).toHaveLength(1);
|
|
300
|
+
expect(items[0]).toMatchObject({ adapter: NAME, originalId: "CK1" });
|
|
301
|
+
expect(calls[0].cookies).toBe(COOKIES);
|
|
302
|
+
expect(calls[0].sign).toBe(null);
|
|
303
|
+
const batch = a.normalize(items[0]);
|
|
304
|
+
expect(batch.events[0].content.title).toBe("flight: 上海 → 北京");
|
|
305
|
+
expect(batch.events[0].content.amount).toMatchObject({ value: 1200 });
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
it("invokes signProvider when configured", async () => {
|
|
309
|
+
const fetchFn = async ({ query }) =>
|
|
310
|
+
query.pageIndex === 1 ? { orders: [{ orderId: "S1", type: "hotel" }] } : { orders: [] };
|
|
311
|
+
const signCalls = [];
|
|
312
|
+
const signProvider = async (ctx) => {
|
|
313
|
+
signCalls.push(ctx);
|
|
314
|
+
return "SIGNED-TOKEN";
|
|
315
|
+
};
|
|
316
|
+
const a = new CtripAdapter({ account: { cookies: COOKIES }, fetchFn, signProvider });
|
|
317
|
+
const items = await collect(a.sync({ sinceWatermark: 0 }));
|
|
318
|
+
expect(items).toHaveLength(1);
|
|
319
|
+
expect(signCalls).toHaveLength(1);
|
|
320
|
+
expect(signCalls[0].cookies).toBe(COOKIES);
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
it("stops at sinceWatermark (older orders dropped)", async () => {
|
|
324
|
+
const fetchFn = async () => ({
|
|
325
|
+
orderList: [
|
|
326
|
+
{ orderId: "NEW", type: "flight", orderDate: 2_000_000_000_000 },
|
|
327
|
+
{ orderId: "OLD", type: "flight", orderDate: 1_000_000_000_000 },
|
|
328
|
+
],
|
|
329
|
+
});
|
|
330
|
+
const a = new CtripAdapter({ account: { cookies: COOKIES }, fetchFn });
|
|
331
|
+
const items = await collect(a.sync({ sinceWatermark: 1_500_000_000_000, maxPages: 1 }));
|
|
332
|
+
expect(items.map((x) => x.originalId)).toEqual(["NEW"]);
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
it("respects opts.limit", async () => {
|
|
336
|
+
const fetchFn = async () => ({
|
|
337
|
+
orderList: [
|
|
338
|
+
{ orderId: "L1", type: "flight", orderDate: 2_000_000_000_000 },
|
|
339
|
+
{ orderId: "L2", type: "flight", orderDate: 2_000_000_000_001 },
|
|
340
|
+
],
|
|
341
|
+
});
|
|
342
|
+
const a = new CtripAdapter({ account: { cookies: COOKIES }, fetchFn });
|
|
343
|
+
const items = await collect(a.sync({ sinceWatermark: 0, limit: 1, maxPages: 1 }));
|
|
344
|
+
expect(items).toHaveLength(1);
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
it("empty/login-redirect response yields zero (no crash)", async () => {
|
|
348
|
+
const a = new CtripAdapter({
|
|
349
|
+
account: { cookies: COOKIES },
|
|
350
|
+
fetchFn: async () => "<html>login</html>",
|
|
351
|
+
});
|
|
352
|
+
expect(await collect(a.sync({ sinceWatermark: 0 }))).toEqual([]);
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
it("default fetch throws when no fetchFn (wiring bug)", async () => {
|
|
356
|
+
const a = new CtripAdapter({ account: { cookies: COOKIES } });
|
|
357
|
+
await expect(collect(a.sync({ sinceWatermark: 0 }))).rejects.toThrow(
|
|
358
|
+
/no fetchFn configured/,
|
|
359
|
+
);
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
it("snapshot/file path takes priority over cookie mode", async () => {
|
|
363
|
+
const p = writeTmp(JSON.stringify([FLIGHT_ORDER]));
|
|
364
|
+
try {
|
|
365
|
+
const a = new CtripAdapter({
|
|
366
|
+
account: { cookies: COOKIES },
|
|
367
|
+
fetchFn: async () => {
|
|
368
|
+
throw new Error("fetchFn should NOT be called in file mode");
|
|
369
|
+
},
|
|
370
|
+
});
|
|
371
|
+
const items = await collect(a.sync({ inputPath: p }));
|
|
372
|
+
expect(items.map((x) => x.originalId)).toEqual(["CT1"]);
|
|
373
|
+
} finally {
|
|
374
|
+
fs.unlinkSync(p);
|
|
375
|
+
}
|
|
376
|
+
});
|
|
377
|
+
});
|