@chainlesschain/personal-data-hub 0.4.4 → 0.4.5
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/edu-huawei-learning-live.test.js +198 -0
- package/__tests__/adapters/edu-zuoyebang-live.test.js +226 -0
- package/__tests__/adapters/family-23-collectors-scaffold.test.js +5 -1
- package/__tests__/adapters/finance-alipay-live.test.js +258 -0
- package/__tests__/adapters/game-genshin-live.test.js +238 -0
- package/__tests__/adapters/game-genshin-scaffold.test.js +4 -3
- package/__tests__/adapters/game-honor-of-kings-live.test.js +230 -0
- package/__tests__/adapters/netease-music-live.test.js +244 -0
- package/__tests__/adapters/social-douyin-adb-aweme-detail.test.js +165 -0
- package/__tests__/adapters/social-douyin-adb-watch-history.test.js +192 -0
- package/__tests__/adapters/social-toutiao-adb-account-reader.test.js +135 -0
- package/__tests__/adapters/social-toutiao-adb-api-client.test.js +89 -0
- package/__tests__/adapters/social-toutiao-adb-collector.test.js +95 -2
- package/__tests__/adapters/social-toutiao-adb-cookies-extension.test.js +30 -0
- package/__tests__/adapters/social-xiaohongshu-adb-cookies-extension.test.js +0 -0
- package/lib/adapters/_live-json-helpers.js +50 -0
- package/lib/adapters/edu-huawei-learning/api-client.js +178 -5
- package/lib/adapters/edu-huawei-learning/index.js +83 -9
- package/lib/adapters/edu-zuoyebang/api-client.js +181 -6
- package/lib/adapters/edu-zuoyebang/index.js +83 -9
- package/lib/adapters/finance-alipay/api-client.js +268 -6
- package/lib/adapters/finance-alipay/index.js +85 -9
- package/lib/adapters/game-genshin/api-client.js +207 -6
- package/lib/adapters/game-genshin/index.js +90 -9
- package/lib/adapters/game-honor-of-kings/api-client.js +235 -12
- package/lib/adapters/game-honor-of-kings/index.js +80 -9
- package/lib/adapters/netease-music/api-client.js +284 -0
- package/lib/adapters/netease-music/index.js +85 -9
- package/lib/adapters/social-douyin/index.js +2 -0
- package/lib/adapters/social-douyin-adb/aweme-detail-client.js +119 -0
- package/lib/adapters/social-douyin-adb/collector.js +114 -0
- package/lib/adapters/social-douyin-adb/index.js +18 -1
- package/lib/adapters/social-douyin-adb/watch-history-reader.js +188 -0
- package/lib/adapters/social-toutiao-adb/account-reader.js +179 -0
- package/lib/adapters/social-toutiao-adb/api-client.js +41 -17
- package/lib/adapters/social-toutiao-adb/collector.js +55 -19
- package/lib/adapters/social-toutiao-adb/cookies-extension.js +21 -1
- package/lib/adapters/social-toutiao-adb/index.js +6 -0
- package/lib/adapters/social-xiaohongshu-adb/cookies-extension.js +19 -1
- package/lib/index.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FAMILY-23 v0.2 — edu-huawei-learning LIVE web fetcher tests.
|
|
3
|
+
* cookie (华为账号会话) → user-info + study-records → snapshot-shaped events +
|
|
4
|
+
* adapter live sync + defensive field extraction + error mapping.
|
|
5
|
+
* All network via injected fetch. Endpoint shapes are best-effort (see
|
|
6
|
+
* api-client header) — these tests pin the REQUEST CONSTRUCTION + PARSING
|
|
7
|
+
* contract, not the live server, which can't be verified here.
|
|
8
|
+
*/
|
|
9
|
+
"use strict";
|
|
10
|
+
|
|
11
|
+
import { describe, it, expect } from "vitest";
|
|
12
|
+
|
|
13
|
+
const { HuaweiLearningAdapter } = require("../../lib");
|
|
14
|
+
const {
|
|
15
|
+
HuaweiLearningApiClient,
|
|
16
|
+
} = require("../../lib/adapters/edu-huawei-learning/api-client");
|
|
17
|
+
const { validateBatch } = require("../../lib/batch");
|
|
18
|
+
|
|
19
|
+
const COOKIE = "accountId=555; CASTGC=tok-xyz";
|
|
20
|
+
|
|
21
|
+
function makeFetch(routes, calls) {
|
|
22
|
+
return async (url, init) => {
|
|
23
|
+
calls.push({ url, headers: (init && init.headers) || {} });
|
|
24
|
+
const route = routes.find((r) => url.includes(r.match));
|
|
25
|
+
if (!route) return { ok: false, status: 404, text: async () => "not mapped" };
|
|
26
|
+
return {
|
|
27
|
+
ok: route.status ? route.status >= 200 && route.status < 300 : true,
|
|
28
|
+
status: route.status || 200,
|
|
29
|
+
text: async () =>
|
|
30
|
+
typeof route.body === "string" ? route.body : JSON.stringify(route.body),
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async function collect(iter) {
|
|
36
|
+
const out = [];
|
|
37
|
+
for await (const r of iter) out.push(r);
|
|
38
|
+
return out;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
describe("HuaweiLearningApiClient session probe", () => {
|
|
42
|
+
it("hasSession is lenient: any k=v cookie passes, empty/garbage fails", () => {
|
|
43
|
+
const c = new HuaweiLearningApiClient();
|
|
44
|
+
expect(c.hasSession("CASTGC=tok")).toBe(true);
|
|
45
|
+
expect(c.hasSession(COOKIE)).toBe(true);
|
|
46
|
+
expect(c.hasSession("")).toBe(false);
|
|
47
|
+
expect(c.lastError.code).toBe(-7);
|
|
48
|
+
expect(c.hasSession("garbage")).toBe(false);
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
describe("HuaweiLearningApiClient.fetchSnapshot — live (mocked fetch)", () => {
|
|
53
|
+
it("carries cookie on each GET + parses user/records defensively", async () => {
|
|
54
|
+
const calls = [];
|
|
55
|
+
const fetch = makeFetch(
|
|
56
|
+
[
|
|
57
|
+
{
|
|
58
|
+
match: "/edu/api/user/v1/info",
|
|
59
|
+
body: { code: 0, data: { user: { accountId: 555, nickName: "小华" } } },
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
match: "/edu/api/study/v1/records",
|
|
63
|
+
body: {
|
|
64
|
+
code: 0,
|
|
65
|
+
data: {
|
|
66
|
+
records: [
|
|
67
|
+
{ recordId: "c1", courseName: "物理", studyDuration: 1500, startTime: 1700000000 },
|
|
68
|
+
{ id: "c2", title: "化学", duration: 600000, createTime: "1700003600" },
|
|
69
|
+
],
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
],
|
|
74
|
+
calls,
|
|
75
|
+
);
|
|
76
|
+
const client = new HuaweiLearningApiClient({ fetch });
|
|
77
|
+
const result = await client.fetchSnapshot(COOKIE);
|
|
78
|
+
expect(result.account).toEqual({ uid: "555", displayName: "小华" });
|
|
79
|
+
const profile = result.events.find((e) => e.kind === "profile");
|
|
80
|
+
expect(profile).toMatchObject({ uid: "555", nickname: "小华" });
|
|
81
|
+
const studies = result.events.filter((e) => e.kind === "study");
|
|
82
|
+
expect(studies).toHaveLength(2);
|
|
83
|
+
expect(studies[0]).toMatchObject({ id: "study-c1", course: "物理", durationMs: 1500000, startAt: 1700000000000 });
|
|
84
|
+
expect(studies[1]).toMatchObject({ id: "study-c2", course: "化学", durationMs: 600000, startAt: 1700003600000 });
|
|
85
|
+
for (const c of calls) {
|
|
86
|
+
expect(c.headers.Cookie).toBe(COOKIE);
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("handles alternate envelope (resultCode + result + list)", async () => {
|
|
91
|
+
const fetch = makeFetch(
|
|
92
|
+
[
|
|
93
|
+
{ match: "/edu/api/user/v1/info", body: { resultCode: 0, result: { userId: "999", displayName: "阿福" } } },
|
|
94
|
+
{ match: "/edu/api/study/v1/records", body: { resultCode: 0, result: { list: [{ logId: "z1", name: "数学", learnTime: 300 }] } } },
|
|
95
|
+
],
|
|
96
|
+
[],
|
|
97
|
+
);
|
|
98
|
+
const client = new HuaweiLearningApiClient({ fetch });
|
|
99
|
+
const result = await client.fetchSnapshot(COOKIE);
|
|
100
|
+
expect(result.account).toEqual({ uid: "999", displayName: "阿福" });
|
|
101
|
+
const study = result.events.find((e) => e.kind === "study");
|
|
102
|
+
expect(study).toMatchObject({ id: "study-z1", course: "数学", durationMs: 300000 });
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("include.study:false skips records; limit/offset land in the query", async () => {
|
|
106
|
+
const calls = [];
|
|
107
|
+
const fetch = makeFetch(
|
|
108
|
+
[
|
|
109
|
+
{ match: "/edu/api/user/v1/info", body: { code: 0, data: { accountId: 1, nickName: "x" } } },
|
|
110
|
+
{ match: "/edu/api/study/v1/records", body: { code: 0, data: { records: [] } } },
|
|
111
|
+
],
|
|
112
|
+
calls,
|
|
113
|
+
);
|
|
114
|
+
const client = new HuaweiLearningApiClient({ fetch });
|
|
115
|
+
await client.fetchSnapshot(COOKIE, { include: { study: false } });
|
|
116
|
+
expect(calls.some((c) => c.url.includes("/study/v1/records"))).toBe(false);
|
|
117
|
+
|
|
118
|
+
await client.fetchSnapshot(COOKIE, { limit: 7, offset: 3 });
|
|
119
|
+
const rec = calls.find((c) => c.url.includes("/study/v1/records"));
|
|
120
|
+
expect(rec.url).toContain("offset=3");
|
|
121
|
+
expect(rec.url).toContain("limit=7");
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("maps non-zero code to null + lastError", async () => {
|
|
125
|
+
const fetch = makeFetch(
|
|
126
|
+
[{ match: "/edu/api/user/v1/info", body: { code: 401, message: "会话过期" } }],
|
|
127
|
+
[],
|
|
128
|
+
);
|
|
129
|
+
const client = new HuaweiLearningApiClient({ fetch });
|
|
130
|
+
expect(await client.fetchSnapshot(COOKIE)).toBeNull();
|
|
131
|
+
expect(client.lastError.code).toBe(401);
|
|
132
|
+
expect(client.lastError.message).toContain("会话过期");
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("no session cookie → null + lastError -7 (no network)", async () => {
|
|
136
|
+
const calls = [];
|
|
137
|
+
const client = new HuaweiLearningApiClient({ fetch: makeFetch([], calls) });
|
|
138
|
+
expect(await client.fetchSnapshot("")).toBeNull();
|
|
139
|
+
expect(client.lastError.code).toBe(-7);
|
|
140
|
+
expect(calls).toHaveLength(0);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("HTTP non-2xx → null + lastError with status", async () => {
|
|
144
|
+
const fetch = makeFetch([{ match: "/edu/api/user/v1/info", status: 500, body: "err" }], []);
|
|
145
|
+
const client = new HuaweiLearningApiClient({ fetch });
|
|
146
|
+
expect(await client.fetchSnapshot(COOKIE)).toBeNull();
|
|
147
|
+
expect(client.lastError.code).toBe(500);
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
describe("HuaweiLearningAdapter — cookie (live) sync mode", () => {
|
|
152
|
+
it("authenticate accepts a session cookie; rejects empty", async () => {
|
|
153
|
+
const a = new HuaweiLearningAdapter();
|
|
154
|
+
expect((await a.authenticate({ cookie: COOKIE })).mode).toBe("cookie");
|
|
155
|
+
const bad = await a.authenticate({ cookie: "garbage" });
|
|
156
|
+
expect(bad.ok).toBe(false);
|
|
157
|
+
expect(bad.reason).toBe("INVALID_COOKIE");
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("version/capabilities reflect v0.2 live mode", () => {
|
|
161
|
+
const a = new HuaweiLearningAdapter();
|
|
162
|
+
expect(a.version).toBe("0.2.0");
|
|
163
|
+
expect(a.capabilities).toContain("sync:cookie");
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it("sync via cookie yields profile+study raws → valid normalized batch", async () => {
|
|
167
|
+
const fetch = makeFetch(
|
|
168
|
+
[
|
|
169
|
+
{ match: "/edu/api/user/v1/info", body: { code: 0, data: { accountId: 555, nickName: "小华" } } },
|
|
170
|
+
{ match: "/edu/api/study/v1/records", body: { code: 0, data: { records: [{ recordId: "c1", courseName: "物理", studyDuration: 1500, startTime: 1700000000 }] } } },
|
|
171
|
+
],
|
|
172
|
+
[],
|
|
173
|
+
);
|
|
174
|
+
const a = new HuaweiLearningAdapter();
|
|
175
|
+
const raws = await collect(a.sync({ cookie: COOKIE, fetch }));
|
|
176
|
+
expect(raws.map((r) => r.kind).sort()).toEqual(["profile", "study"]);
|
|
177
|
+
const profileRaw = raws.find((r) => r.kind === "profile");
|
|
178
|
+
expect(profileRaw.originalId).toBe("huaweilearning:profile:profile-555");
|
|
179
|
+
|
|
180
|
+
for (const raw of raws) {
|
|
181
|
+
expect(validateBatch(a.normalize(raw)).valid).toBe(true);
|
|
182
|
+
}
|
|
183
|
+
const profileBatch = a.normalize(profileRaw);
|
|
184
|
+
expect(profileBatch.persons[0].identifiers["huawei-learning-uid"]).toEqual(["555"]);
|
|
185
|
+
const studyBatch = a.normalize(raws.find((r) => r.kind === "study"));
|
|
186
|
+
expect(studyBatch.events[0].extra.course).toBe("物理");
|
|
187
|
+
expect(studyBatch.events[0].extra.durationMs).toBe(1500000);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it("sync via cookie throws (mapped lastError) on API error", async () => {
|
|
191
|
+
const fetch = makeFetch(
|
|
192
|
+
[{ match: "/edu/api/user/v1/info", body: { code: 401, message: "会话过期" } }],
|
|
193
|
+
[],
|
|
194
|
+
);
|
|
195
|
+
const a = new HuaweiLearningAdapter();
|
|
196
|
+
await expect(collect(a.sync({ cookie: COOKIE, fetch }))).rejects.toThrow(/会话过期|code 401/);
|
|
197
|
+
});
|
|
198
|
+
});
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FAMILY-23 v0.2 — edu-zuoyebang LIVE web fetcher tests.
|
|
3
|
+
* cookie (ZYBUSS 会话) → user-info + study-records → snapshot-shaped events +
|
|
4
|
+
* adapter live sync + defensive field extraction + error mapping.
|
|
5
|
+
* All network via injected fetch. Endpoint shapes are best-effort (see
|
|
6
|
+
* api-client header) — these tests pin the REQUEST CONSTRUCTION + PARSING
|
|
7
|
+
* contract, not the live server, which can't be verified here.
|
|
8
|
+
*/
|
|
9
|
+
"use strict";
|
|
10
|
+
|
|
11
|
+
import { describe, it, expect } from "vitest";
|
|
12
|
+
|
|
13
|
+
const { ZuoyebangAdapter } = require("../../lib");
|
|
14
|
+
const {
|
|
15
|
+
ZuoyebangApiClient,
|
|
16
|
+
} = require("../../lib/adapters/edu-zuoyebang/api-client");
|
|
17
|
+
const {
|
|
18
|
+
pick,
|
|
19
|
+
toDurationMs,
|
|
20
|
+
toEpochMs,
|
|
21
|
+
} = require("../../lib/adapters/_live-json-helpers");
|
|
22
|
+
const { validateBatch } = require("../../lib/batch");
|
|
23
|
+
|
|
24
|
+
const COOKIE = "ZYBUSS=opaquetoken123; other=1";
|
|
25
|
+
|
|
26
|
+
function makeFetch(routes, calls) {
|
|
27
|
+
return async (url, init) => {
|
|
28
|
+
calls.push({ url, headers: (init && init.headers) || {} });
|
|
29
|
+
const route = routes.find((r) => url.includes(r.match));
|
|
30
|
+
if (!route) return { ok: false, status: 404, text: async () => "not mapped" };
|
|
31
|
+
return {
|
|
32
|
+
ok: route.status ? route.status >= 200 && route.status < 300 : true,
|
|
33
|
+
status: route.status || 200,
|
|
34
|
+
text: async () =>
|
|
35
|
+
typeof route.body === "string" ? route.body : JSON.stringify(route.body),
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async function collect(iter) {
|
|
41
|
+
const out = [];
|
|
42
|
+
for await (const r of iter) out.push(r);
|
|
43
|
+
return out;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
describe("_live-json-helpers (shared field-extraction)", () => {
|
|
47
|
+
it("pick returns first present non-empty value", () => {
|
|
48
|
+
expect(pick({ a: "", b: "x" }, ["a", "b"])).toBe("x");
|
|
49
|
+
expect(pick({ a: 0 }, ["a"])).toBe(0); // 0 is present
|
|
50
|
+
expect(pick({}, ["a"], "fb")).toBe("fb");
|
|
51
|
+
expect(pick(null, ["a"], "fb")).toBe("fb");
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("toDurationMs disambiguates seconds vs ms; coerces numeric strings", () => {
|
|
55
|
+
expect(toDurationMs(1800)).toBe(1800000);
|
|
56
|
+
expect(toDurationMs(1800000)).toBe(1800000);
|
|
57
|
+
expect(toDurationMs("900")).toBe(900000);
|
|
58
|
+
expect(toDurationMs("nope")).toBe(0);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("toEpochMs coerces seconds/ms/date-string", () => {
|
|
62
|
+
expect(toEpochMs(1700000000)).toBe(1700000000000);
|
|
63
|
+
expect(toEpochMs("1700000000000")).toBe(1700000000000);
|
|
64
|
+
expect(toEpochMs("2023-11-14T22:13:20Z")).toBe(1700000000000);
|
|
65
|
+
expect(toEpochMs("bogus")).toBeNull();
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
describe("ZuoyebangApiClient session probe", () => {
|
|
70
|
+
it("hasSession accepts ZYBUSS-only cookie (uid comes from the API)", () => {
|
|
71
|
+
const c = new ZuoyebangApiClient();
|
|
72
|
+
expect(c.hasSession("ZYBUSS=opaque")).toBe(true);
|
|
73
|
+
expect(c.hasSession("uid=111")).toBe(true);
|
|
74
|
+
expect(c.hasSession("foo=bar")).toBe(false);
|
|
75
|
+
expect(c.lastError.code).toBe(-7);
|
|
76
|
+
expect(c.hasSession("")).toBe(false);
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
describe("ZuoyebangApiClient.fetchSnapshot — live (mocked fetch)", () => {
|
|
81
|
+
it("carries cookie on each GET + parses user/records defensively", async () => {
|
|
82
|
+
const calls = [];
|
|
83
|
+
const fetch = makeFetch(
|
|
84
|
+
[
|
|
85
|
+
{
|
|
86
|
+
match: "/session/pc/getuserinfo",
|
|
87
|
+
body: { errNo: 0, data: { user: { uid: 12345, uname: "小红", gradeName: "初二" } } },
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
match: "/study/pc/record/list",
|
|
91
|
+
body: {
|
|
92
|
+
errNo: 0,
|
|
93
|
+
data: {
|
|
94
|
+
list: [
|
|
95
|
+
{ recordId: "r1", subjectName: "数学", studyTime: 1200, startTime: 1700000000 },
|
|
96
|
+
{ logId: "r2", subject: "英语", duration: 900000, createTime: "1700003600" },
|
|
97
|
+
],
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
],
|
|
102
|
+
calls,
|
|
103
|
+
);
|
|
104
|
+
const client = new ZuoyebangApiClient({ fetch });
|
|
105
|
+
const result = await client.fetchSnapshot(COOKIE);
|
|
106
|
+
expect(result.account).toEqual({ uid: "12345", displayName: "小红" });
|
|
107
|
+
const profile = result.events.find((e) => e.kind === "profile");
|
|
108
|
+
expect(profile).toMatchObject({ uid: "12345", nickname: "小红", grade: "初二" });
|
|
109
|
+
const studies = result.events.filter((e) => e.kind === "study");
|
|
110
|
+
expect(studies).toHaveLength(2);
|
|
111
|
+
expect(studies[0]).toMatchObject({ id: "study-r1", subject: "数学", durationMs: 1200000, startAt: 1700000000000 });
|
|
112
|
+
expect(studies[1]).toMatchObject({ id: "study-r2", subject: "英语", durationMs: 900000, startAt: 1700003600000 });
|
|
113
|
+
for (const c of calls) {
|
|
114
|
+
expect(c.headers.Cookie).toBe(COOKIE);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("handles flat user body + alternate envelope key (errno)", async () => {
|
|
119
|
+
const calls = [];
|
|
120
|
+
const fetch = makeFetch(
|
|
121
|
+
[
|
|
122
|
+
{ match: "/session/pc/getuserinfo", body: { errno: 0, data: { userId: "888", nickName: "阿明" } } },
|
|
123
|
+
{ match: "/study/pc/record/list", body: { errno: 0, data: { records: [] } } },
|
|
124
|
+
],
|
|
125
|
+
calls,
|
|
126
|
+
);
|
|
127
|
+
const client = new ZuoyebangApiClient({ fetch });
|
|
128
|
+
const result = await client.fetchSnapshot(COOKIE);
|
|
129
|
+
expect(result.account).toEqual({ uid: "888", displayName: "阿明" });
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("include.study:false skips the records call; limit/offset map to rn/pn", async () => {
|
|
133
|
+
const calls = [];
|
|
134
|
+
const fetch = makeFetch(
|
|
135
|
+
[
|
|
136
|
+
{ match: "/session/pc/getuserinfo", body: { errNo: 0, data: { uid: 1, uname: "x" } } },
|
|
137
|
+
{ match: "/study/pc/record/list", body: { errNo: 0, data: { list: [] } } },
|
|
138
|
+
],
|
|
139
|
+
calls,
|
|
140
|
+
);
|
|
141
|
+
const client = new ZuoyebangApiClient({ fetch });
|
|
142
|
+
await client.fetchSnapshot(COOKIE, { include: { study: false } });
|
|
143
|
+
expect(calls.some((c) => c.url.includes("record/list"))).toBe(false);
|
|
144
|
+
|
|
145
|
+
await client.fetchSnapshot(COOKIE, { limit: 5, offset: 2 });
|
|
146
|
+
const rec = calls.find((c) => c.url.includes("record/list"));
|
|
147
|
+
expect(rec.url).toContain("pn=2");
|
|
148
|
+
expect(rec.url).toContain("rn=5");
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("maps non-zero errNo to null + lastError (e.g. session expired)", async () => {
|
|
152
|
+
const fetch = makeFetch(
|
|
153
|
+
[{ match: "/session/pc/getuserinfo", body: { errNo: 3, errstr: "未登录" } }],
|
|
154
|
+
[],
|
|
155
|
+
);
|
|
156
|
+
const client = new ZuoyebangApiClient({ fetch });
|
|
157
|
+
expect(await client.fetchSnapshot(COOKIE)).toBeNull();
|
|
158
|
+
expect(client.lastError.code).toBe(3);
|
|
159
|
+
expect(client.lastError.message).toContain("未登录");
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it("no session cookie → null + lastError -7 (no network)", async () => {
|
|
163
|
+
const calls = [];
|
|
164
|
+
const client = new ZuoyebangApiClient({ fetch: makeFetch([], calls) });
|
|
165
|
+
expect(await client.fetchSnapshot("foo=bar")).toBeNull();
|
|
166
|
+
expect(client.lastError.code).toBe(-7);
|
|
167
|
+
expect(calls).toHaveLength(0);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it("HTTP non-2xx → null + lastError with status", async () => {
|
|
171
|
+
const fetch = makeFetch([{ match: "/session/pc/getuserinfo", status: 503, body: "down" }], []);
|
|
172
|
+
const client = new ZuoyebangApiClient({ fetch });
|
|
173
|
+
expect(await client.fetchSnapshot(COOKIE)).toBeNull();
|
|
174
|
+
expect(client.lastError.code).toBe(503);
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
describe("ZuoyebangAdapter — cookie (live) sync mode", () => {
|
|
179
|
+
it("authenticate accepts ZYBUSS cookie; rejects sessionless", async () => {
|
|
180
|
+
const a = new ZuoyebangAdapter();
|
|
181
|
+
expect((await a.authenticate({ cookie: COOKIE })).mode).toBe("cookie");
|
|
182
|
+
const bad = await a.authenticate({ cookie: "foo=bar" });
|
|
183
|
+
expect(bad.ok).toBe(false);
|
|
184
|
+
expect(bad.reason).toBe("INVALID_COOKIE");
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("version/capabilities reflect v0.2 live mode", () => {
|
|
188
|
+
const a = new ZuoyebangAdapter();
|
|
189
|
+
expect(a.version).toBe("0.2.0");
|
|
190
|
+
expect(a.capabilities).toContain("sync:cookie");
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it("sync via cookie yields profile+study raws → valid normalized batch", async () => {
|
|
194
|
+
const fetch = makeFetch(
|
|
195
|
+
[
|
|
196
|
+
{ match: "/session/pc/getuserinfo", body: { errNo: 0, data: { user: { uid: 12345, uname: "小红", gradeName: "初二" } } } },
|
|
197
|
+
{ match: "/study/pc/record/list", body: { errNo: 0, data: { list: [{ recordId: "r1", subjectName: "数学", studyTime: 1200, startTime: 1700000000 }] } } },
|
|
198
|
+
],
|
|
199
|
+
[],
|
|
200
|
+
);
|
|
201
|
+
const a = new ZuoyebangAdapter();
|
|
202
|
+
const raws = await collect(a.sync({ cookie: COOKIE, fetch }));
|
|
203
|
+
expect(raws.map((r) => r.kind).sort()).toEqual(["profile", "study"]);
|
|
204
|
+
const profileRaw = raws.find((r) => r.kind === "profile");
|
|
205
|
+
expect(profileRaw.originalId).toBe("zuoyebang:profile:profile-12345");
|
|
206
|
+
|
|
207
|
+
for (const raw of raws) {
|
|
208
|
+
expect(validateBatch(a.normalize(raw)).valid).toBe(true);
|
|
209
|
+
}
|
|
210
|
+
const profileBatch = a.normalize(profileRaw);
|
|
211
|
+
expect(profileBatch.persons[0].identifiers["zuoyebang-uid"]).toEqual(["12345"]);
|
|
212
|
+
expect(profileBatch.persons[0].extra.grade).toBe("初二");
|
|
213
|
+
const studyBatch = a.normalize(raws.find((r) => r.kind === "study"));
|
|
214
|
+
expect(studyBatch.events[0].extra.subject).toBe("数学");
|
|
215
|
+
expect(studyBatch.events[0].extra.durationMs).toBe(1200000);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it("sync via cookie throws (mapped lastError) on API error", async () => {
|
|
219
|
+
const fetch = makeFetch(
|
|
220
|
+
[{ match: "/session/pc/getuserinfo", body: { errNo: 3, errstr: "未登录" } }],
|
|
221
|
+
[],
|
|
222
|
+
);
|
|
223
|
+
const a = new ZuoyebangAdapter();
|
|
224
|
+
await expect(collect(a.sync({ cookie: COOKIE, fetch }))).rejects.toThrow(/未登录|code 3/);
|
|
225
|
+
});
|
|
226
|
+
});
|
|
@@ -36,6 +36,7 @@ const PLATFORMS = [
|
|
|
36
36
|
label: "王者荣耀",
|
|
37
37
|
Adapter: lib.HonorOfKingsAdapter,
|
|
38
38
|
name: "game-honor-of-kings",
|
|
39
|
+
version: "0.2.0", // live 营地 fetcher landed (see game-honor-of-kings-live.test.js)
|
|
39
40
|
sensitivity: "medium",
|
|
40
41
|
eventKind: "play",
|
|
41
42
|
eventSubtype: "media",
|
|
@@ -46,6 +47,7 @@ const PLATFORMS = [
|
|
|
46
47
|
label: "作业帮",
|
|
47
48
|
Adapter: lib.ZuoyebangAdapter,
|
|
48
49
|
name: "edu-zuoyebang",
|
|
50
|
+
version: "0.2.0", // live web fetcher landed (see edu-zuoyebang-live.test.js)
|
|
49
51
|
sensitivity: "medium",
|
|
50
52
|
eventKind: "study",
|
|
51
53
|
eventSubtype: "other",
|
|
@@ -56,6 +58,7 @@ const PLATFORMS = [
|
|
|
56
58
|
label: "支付宝",
|
|
57
59
|
Adapter: lib.AlipayAdapter,
|
|
58
60
|
name: "finance-alipay",
|
|
61
|
+
version: "0.2.0", // live mobilegw fetcher landed (see finance-alipay-live.test.js)
|
|
59
62
|
sensitivity: "high",
|
|
60
63
|
eventKind: "order",
|
|
61
64
|
eventSubtype: "payment",
|
|
@@ -66,6 +69,7 @@ const PLATFORMS = [
|
|
|
66
69
|
label: "华为学习中心",
|
|
67
70
|
Adapter: lib.HuaweiLearningAdapter,
|
|
68
71
|
name: "edu-huawei-learning",
|
|
72
|
+
version: "0.2.0", // live web fetcher landed (see edu-huawei-learning-live.test.js)
|
|
69
73
|
sensitivity: "medium",
|
|
70
74
|
eventKind: "study",
|
|
71
75
|
eventSubtype: "other",
|
|
@@ -80,7 +84,7 @@ for (const P of PLATFORMS) {
|
|
|
80
84
|
const a = new P.Adapter();
|
|
81
85
|
expect(assertAdapter(a).ok).toBe(true);
|
|
82
86
|
expect(a.name).toBe(P.name);
|
|
83
|
-
expect(a.version).toBe("0.1.0");
|
|
87
|
+
expect(a.version).toBe(P.version || "0.1.0");
|
|
84
88
|
expect(a.dataDisclosure.sensitivity).toBe(P.sensitivity);
|
|
85
89
|
expect(a.capabilities).toContain("sync:snapshot");
|
|
86
90
|
});
|