@chainlesschain/personal-data-hub 0.4.3 → 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/messaging-whatsapp.test.js +289 -0
- package/__tests__/adapters/netease-music-live.test.js +244 -0
- package/__tests__/adapters/shopping-base.test.js +179 -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-kuaishou-adb-api-client.test.js +64 -0
- package/__tests__/adapters/social-kuaishou-adb-cookies-extension.test.js +11 -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-api-client.test.js +431 -0
- package/__tests__/adapters/social-xiaohongshu-adb-cookies-extension.test.js +0 -0
- package/__tests__/adapters/social-xiaohongshu-adb-snapshot-builder.test.js +200 -0
- package/__tests__/adapters/travel-12306.test.js +279 -0
- package/__tests__/adapters/travel-amap.test.js +219 -0
- package/__tests__/adapters/travel-baidu-map.test.js +305 -0
- package/__tests__/adapters/travel-base.test.js +205 -0
- package/__tests__/adapters/travel-ctrip.test.js +203 -0
- package/__tests__/adapters/travel-tencent-map.test.js +207 -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-kuaishou/index.js +7 -2
- package/lib/adapters/social-kuaishou-adb/api-client.js +38 -18
- package/lib/adapters/social-kuaishou-adb/cookies-extension.js +16 -15
- package/lib/adapters/social-toutiao/index.js +8 -4
- 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/adapters/travel-base/index.js +9 -2
- package/lib/index.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FAMILY-23 v0.2 — game-honor-of-kings LIVE 营地 (Camp) fetcher tests.
|
|
3
|
+
* credential (accessToken+openid+role) → profile + battle-list → snapshot-shaped
|
|
4
|
+
* events + adapter live sync + defensive field extraction + error mapping.
|
|
5
|
+
* All network via injected fetch. Endpoint shapes are best-effort (see api-client
|
|
6
|
+
* header) — these tests pin the REQUEST CONSTRUCTION + PARSING contract, not the
|
|
7
|
+
* live server, which can't be verified here.
|
|
8
|
+
*/
|
|
9
|
+
"use strict";
|
|
10
|
+
|
|
11
|
+
import { describe, it, expect } from "vitest";
|
|
12
|
+
|
|
13
|
+
const { HonorOfKingsAdapter } = require("../../lib");
|
|
14
|
+
const {
|
|
15
|
+
HonorOfKingsApiClient,
|
|
16
|
+
pick,
|
|
17
|
+
toDurationMs,
|
|
18
|
+
toEpochMs,
|
|
19
|
+
} = require("../../lib/adapters/game-honor-of-kings/api-client");
|
|
20
|
+
const { validateBatch } = require("../../lib/batch");
|
|
21
|
+
|
|
22
|
+
const CRED = {
|
|
23
|
+
accessToken: "tok-abc",
|
|
24
|
+
openid: "OPENID12345678",
|
|
25
|
+
acctype: "qc",
|
|
26
|
+
areaId: "1",
|
|
27
|
+
roleId: "900000001",
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
function makeFetch(routes, calls) {
|
|
31
|
+
return async (url, init) => {
|
|
32
|
+
let parsedBody = null;
|
|
33
|
+
try {
|
|
34
|
+
parsedBody = init && init.body ? JSON.parse(init.body) : null;
|
|
35
|
+
} catch (_e) {
|
|
36
|
+
parsedBody = init && init.body;
|
|
37
|
+
}
|
|
38
|
+
calls.push({ url, body: parsedBody, headers: (init && init.headers) || {} });
|
|
39
|
+
const route = routes.find((r) => url.includes(r.match));
|
|
40
|
+
if (!route) return { ok: false, status: 404, text: async () => "not mapped" };
|
|
41
|
+
return {
|
|
42
|
+
ok: route.status ? route.status >= 200 && route.status < 300 : true,
|
|
43
|
+
status: route.status || 200,
|
|
44
|
+
text: async () =>
|
|
45
|
+
typeof route.body === "string" ? route.body : JSON.stringify(route.body),
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async function collect(iter) {
|
|
51
|
+
const out = [];
|
|
52
|
+
for await (const r of iter) out.push(r);
|
|
53
|
+
return out;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
describe("honor-of-kings field-extraction helpers", () => {
|
|
57
|
+
it("pick returns first present non-empty value", () => {
|
|
58
|
+
expect(pick({ a: "", b: "x" }, ["a", "b"])).toBe("x");
|
|
59
|
+
expect(pick({ a: 0 }, ["a"])).toBe(0); // 0 is present
|
|
60
|
+
expect(pick({}, ["a"], "fb")).toBe("fb");
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("toDurationMs treats <1e7 as seconds, else ms; coerces numeric strings", () => {
|
|
64
|
+
expect(toDurationMs(1800)).toBe(1800000); // 30 min in seconds
|
|
65
|
+
expect(toDurationMs(1800000)).toBe(1800000); // already ms
|
|
66
|
+
expect(toDurationMs("900")).toBe(900000);
|
|
67
|
+
expect(toDurationMs(0)).toBe(0);
|
|
68
|
+
expect(toDurationMs("nope")).toBe(0);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("toEpochMs coerces seconds/ms/date-string", () => {
|
|
72
|
+
expect(toEpochMs(1700000000)).toBe(1700000000000);
|
|
73
|
+
expect(toEpochMs(1700000000000)).toBe(1700000000000);
|
|
74
|
+
expect(toEpochMs("2023-11-14T22:13:20Z")).toBe(1700000000000);
|
|
75
|
+
expect(toEpochMs("bogus")).toBeNull();
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
describe("HonorOfKingsApiClient.fetchSnapshot — live (mocked fetch)", () => {
|
|
80
|
+
it("merges auth fields into each request + parses profile/battles defensively", async () => {
|
|
81
|
+
const calls = [];
|
|
82
|
+
const fetch = makeFetch(
|
|
83
|
+
[
|
|
84
|
+
{
|
|
85
|
+
match: "/play/profildetail",
|
|
86
|
+
body: {
|
|
87
|
+
returnCode: 0,
|
|
88
|
+
data: { role: { roleId: "900000001", roleName: "小明", level: 30, rankName: "钻石", logo: "u.png" } },
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
match: "/game/getbattlelist",
|
|
93
|
+
body: {
|
|
94
|
+
returnCode: 0,
|
|
95
|
+
data: {
|
|
96
|
+
list: [
|
|
97
|
+
{ gameSeq: "g1", startTime: 1700000000, gametime: 900, mapName: "排位赛" },
|
|
98
|
+
{ gameSeq: "g2", startTime: "1700003600", usedTime: 1200, modeName: "巅峰赛" },
|
|
99
|
+
],
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
],
|
|
104
|
+
calls,
|
|
105
|
+
);
|
|
106
|
+
const client = new HonorOfKingsApiClient({ fetch });
|
|
107
|
+
const result = await client.fetchSnapshot(CRED);
|
|
108
|
+
expect(result.account).toEqual({ uid: "900000001", displayName: "小明" });
|
|
109
|
+
const profile = result.events.find((e) => e.kind === "profile");
|
|
110
|
+
expect(profile).toMatchObject({ uid: "900000001", nickname: "小明", level: 30, rank: "钻石", avatarUrl: "u.png" });
|
|
111
|
+
const plays = result.events.filter((e) => e.kind === "play");
|
|
112
|
+
expect(plays).toHaveLength(2);
|
|
113
|
+
expect(plays[0]).toMatchObject({ durationMs: 900000, mode: "排位赛", startAt: 1700000000000 });
|
|
114
|
+
expect(plays[1]).toMatchObject({ durationMs: 1200000, mode: "巅峰赛", startAt: 1700003600000 });
|
|
115
|
+
// every request carried the OAuth auth fields.
|
|
116
|
+
for (const c of calls) {
|
|
117
|
+
expect(c.body.accessToken).toBe("tok-abc");
|
|
118
|
+
expect(c.body.openid).toBe("OPENID12345678");
|
|
119
|
+
expect(c.body.acctype).toBe("qc");
|
|
120
|
+
expect(c.headers["Content-Type"]).toContain("application/json");
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("handles alternate envelope/field names (result + roleInfo + battleList)", async () => {
|
|
125
|
+
const calls = [];
|
|
126
|
+
const fetch = makeFetch(
|
|
127
|
+
[
|
|
128
|
+
{
|
|
129
|
+
match: "/play/profildetail",
|
|
130
|
+
body: { result: 0, data: { roleInfo: { uid: "777", nickName: "阿强", roleLevel: 15, gradeName: "黄金" } } },
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
match: "/game/getbattlelist",
|
|
134
|
+
body: { result: 0, data: { battleList: [{ gameId: "b9", battleTime: 1700000000, gameDuration: 600, gameName: "5v5" }] } },
|
|
135
|
+
},
|
|
136
|
+
],
|
|
137
|
+
calls,
|
|
138
|
+
);
|
|
139
|
+
const client = new HonorOfKingsApiClient({ fetch });
|
|
140
|
+
const result = await client.fetchSnapshot(CRED);
|
|
141
|
+
expect(result.account.displayName).toBe("阿强");
|
|
142
|
+
const profile = result.events.find((e) => e.kind === "profile");
|
|
143
|
+
expect(profile).toMatchObject({ uid: "777", level: 15, rank: "黄金" });
|
|
144
|
+
const play = result.events.find((e) => e.kind === "play");
|
|
145
|
+
expect(play).toMatchObject({ durationMs: 600000, mode: "5v5" });
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it("include.play:false skips the battle-list call", async () => {
|
|
149
|
+
const calls = [];
|
|
150
|
+
const fetch = makeFetch(
|
|
151
|
+
[{ match: "/play/profildetail", body: { returnCode: 0, data: { role: { roleId: "1", roleName: "x" } } } }],
|
|
152
|
+
calls,
|
|
153
|
+
);
|
|
154
|
+
const client = new HonorOfKingsApiClient({ fetch });
|
|
155
|
+
const result = await client.fetchSnapshot(CRED, { include: { play: false } });
|
|
156
|
+
expect(calls.some((c) => c.url.includes("getbattlelist"))).toBe(false);
|
|
157
|
+
expect(result.events.every((e) => e.kind !== "play")).toBe(true);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("maps non-zero returnCode to null + lastError (e.g. token expired)", async () => {
|
|
161
|
+
const calls = [];
|
|
162
|
+
const fetch = makeFetch([{ match: "/play/profildetail", body: { returnCode: 1001, returnMsg: "登录态失效" } }], calls);
|
|
163
|
+
const client = new HonorOfKingsApiClient({ fetch });
|
|
164
|
+
expect(await client.fetchSnapshot(CRED)).toBeNull();
|
|
165
|
+
expect(client.lastError.code).toBe(1001);
|
|
166
|
+
expect(client.lastError.message).toContain("登录态失效");
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it("missing accessToken/openid → null + lastError -1 (no network)", async () => {
|
|
170
|
+
const calls = [];
|
|
171
|
+
const client = new HonorOfKingsApiClient({ fetch: makeFetch([], calls) });
|
|
172
|
+
expect(await client.fetchSnapshot({ openid: "x" })).toBeNull();
|
|
173
|
+
expect(client.lastError.code).toBe(-1);
|
|
174
|
+
expect(calls).toHaveLength(0);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("HTTP non-2xx → null + lastError with status", async () => {
|
|
178
|
+
const calls = [];
|
|
179
|
+
const fetch = makeFetch([{ match: "/play/profildetail", status: 500, body: "err" }], calls);
|
|
180
|
+
const client = new HonorOfKingsApiClient({ fetch });
|
|
181
|
+
expect(await client.fetchSnapshot(CRED)).toBeNull();
|
|
182
|
+
expect(client.lastError.code).toBe(500);
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
describe("HonorOfKingsAdapter — credential (live) sync mode", () => {
|
|
187
|
+
it("authenticate accepts a credential bundle; rejects incomplete", async () => {
|
|
188
|
+
const a = new HonorOfKingsAdapter();
|
|
189
|
+
expect((await a.authenticate({ credential: CRED })).mode).toBe("camp-token");
|
|
190
|
+
expect((await a.authenticate({ credential: { openid: "x" } })).ok).toBe(false);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it("version/capabilities reflect v0.2 live mode", () => {
|
|
194
|
+
const a = new HonorOfKingsAdapter();
|
|
195
|
+
expect(a.version).toBe("0.2.0");
|
|
196
|
+
expect(a.capabilities).toContain("sync:camp-token");
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("sync via credential yields profile+play raws → valid normalized batch", async () => {
|
|
200
|
+
const calls = [];
|
|
201
|
+
const fetch = makeFetch(
|
|
202
|
+
[
|
|
203
|
+
{ match: "/play/profildetail", body: { returnCode: 0, data: { role: { roleId: "900000001", roleName: "小明", level: 30, rankName: "钻石" } } } },
|
|
204
|
+
{ match: "/game/getbattlelist", body: { returnCode: 0, data: { list: [{ gameSeq: "g1", startTime: 1700000000, gametime: 1800, mapName: "排位赛" }] } } },
|
|
205
|
+
],
|
|
206
|
+
calls,
|
|
207
|
+
);
|
|
208
|
+
const a = new HonorOfKingsAdapter();
|
|
209
|
+
const raws = await collect(a.sync({ credential: CRED, fetch }));
|
|
210
|
+
expect(raws.map((r) => r.kind).sort()).toEqual(["play", "profile"]);
|
|
211
|
+
const profileRaw = raws.find((r) => r.kind === "profile");
|
|
212
|
+
expect(profileRaw.originalId).toBe("hok:profile:profile-900000001");
|
|
213
|
+
|
|
214
|
+
for (const raw of raws) {
|
|
215
|
+
expect(validateBatch(a.normalize(raw)).valid).toBe(true);
|
|
216
|
+
}
|
|
217
|
+
const profileBatch = a.normalize(profileRaw);
|
|
218
|
+
expect(profileBatch.persons[0].identifiers["hok-uid"]).toEqual(["900000001"]);
|
|
219
|
+
expect(profileBatch.persons[0].extra.rank).toBe("钻石");
|
|
220
|
+
const playBatch = a.normalize(raws.find((r) => r.kind === "play"));
|
|
221
|
+
expect(playBatch.events[0].extra.durationMs).toBe(1800000);
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it("sync via credential throws (mapped lastError) on API error", async () => {
|
|
225
|
+
const calls = [];
|
|
226
|
+
const fetch = makeFetch([{ match: "/play/profildetail", body: { returnCode: 1001, returnMsg: "登录态失效" } }], calls);
|
|
227
|
+
const a = new HonorOfKingsAdapter();
|
|
228
|
+
await expect(collect(a.sync({ credential: CRED, fetch }))).rejects.toThrow(/登录态失效|code 1001/);
|
|
229
|
+
});
|
|
230
|
+
});
|
|
@@ -0,0 +1,289 @@
|
|
|
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
|
+
WhatsAppAdapter,
|
|
11
|
+
NAME,
|
|
12
|
+
VERSION,
|
|
13
|
+
} = require("../../lib/adapters/messaging-whatsapp");
|
|
14
|
+
|
|
15
|
+
function writeTmpDb() {
|
|
16
|
+
const p = path.join(
|
|
17
|
+
os.tmpdir(),
|
|
18
|
+
`cc-whatsapp-test-${crypto.randomUUID()}.db`,
|
|
19
|
+
);
|
|
20
|
+
fs.writeFileSync(p, "fake");
|
|
21
|
+
return p;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async function collect(gen) {
|
|
25
|
+
const out = [];
|
|
26
|
+
for await (const x of gen) out.push(x);
|
|
27
|
+
return out;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function makeFakeDriverFactory(tables, log = {}) {
|
|
31
|
+
return () =>
|
|
32
|
+
class FakeDb {
|
|
33
|
+
constructor(dbPath, opts) {
|
|
34
|
+
log.opened = { dbPath, opts };
|
|
35
|
+
}
|
|
36
|
+
prepare(sql) {
|
|
37
|
+
for (const [needle, rows] of Object.entries(tables)) {
|
|
38
|
+
if (sql.includes(needle)) return { all: () => rows };
|
|
39
|
+
}
|
|
40
|
+
throw new Error(`no such table in: ${sql}`);
|
|
41
|
+
}
|
|
42
|
+
close() {
|
|
43
|
+
log.closed = true;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
describe("constants", () => {
|
|
49
|
+
it("exposes name/version + high sensitivity & legal gate", () => {
|
|
50
|
+
expect(NAME).toBe("messaging-whatsapp");
|
|
51
|
+
expect(VERSION).toBe("0.6.0");
|
|
52
|
+
const a = new WhatsAppAdapter();
|
|
53
|
+
expect(a.dataDisclosure.sensitivity).toBe("high");
|
|
54
|
+
expect(a.dataDisclosure.legalGate).toBe(true);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
describe("authenticate", () => {
|
|
59
|
+
it("fails DB_NOT_PULLED without a real db file", async () => {
|
|
60
|
+
const a = new WhatsAppAdapter();
|
|
61
|
+
const r = await a.authenticate({});
|
|
62
|
+
expect(r.ok).toBe(false);
|
|
63
|
+
expect(r.reason).toBe("DB_NOT_PULLED");
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("ok when dbPath exists (inputPath alias too)", async () => {
|
|
67
|
+
const p = writeTmpDb();
|
|
68
|
+
try {
|
|
69
|
+
const a = new WhatsAppAdapter({ account: { phone: "8613800138000" } });
|
|
70
|
+
expect(await a.authenticate({ inputPath: p })).toEqual({
|
|
71
|
+
ok: true,
|
|
72
|
+
account: "8613800138000",
|
|
73
|
+
mode: "snapshot-file",
|
|
74
|
+
});
|
|
75
|
+
} finally {
|
|
76
|
+
fs.unlinkSync(p);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
describe("sync — fake sqlite driver", () => {
|
|
82
|
+
const JID_ROW = {
|
|
83
|
+
_id: 1,
|
|
84
|
+
user: "8613800138000",
|
|
85
|
+
raw_string: "8613800138000@s.whatsapp.net",
|
|
86
|
+
display_name: "Alice",
|
|
87
|
+
};
|
|
88
|
+
const CHAT_ROW = { _id: 2, subject: "Family group" };
|
|
89
|
+
const MSG_ROW = {
|
|
90
|
+
_id: 3,
|
|
91
|
+
key_remote_jid: "8613800138000@s.whatsapp.net",
|
|
92
|
+
from_me: 1,
|
|
93
|
+
text_data: "hello",
|
|
94
|
+
timestamp: 1716383021, // seconds
|
|
95
|
+
};
|
|
96
|
+
const CALL_ROW = {
|
|
97
|
+
_id: 4,
|
|
98
|
+
jid_row_id: 1,
|
|
99
|
+
from_me: 0,
|
|
100
|
+
video_call: 1,
|
|
101
|
+
duration: 65,
|
|
102
|
+
timestamp: 1716383021000,
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
it("yields contact/chat/message/call rows with kind tags, closes db", async () => {
|
|
106
|
+
const p = writeTmpDb();
|
|
107
|
+
const log = {};
|
|
108
|
+
try {
|
|
109
|
+
const a = new WhatsAppAdapter({
|
|
110
|
+
dbPath: p,
|
|
111
|
+
dbDriverFactory: makeFakeDriverFactory(
|
|
112
|
+
{
|
|
113
|
+
"FROM jid": [JID_ROW],
|
|
114
|
+
"FROM chat": [CHAT_ROW],
|
|
115
|
+
"FROM message ": [MSG_ROW],
|
|
116
|
+
"FROM call_log": [CALL_ROW],
|
|
117
|
+
},
|
|
118
|
+
log,
|
|
119
|
+
),
|
|
120
|
+
});
|
|
121
|
+
const items = await collect(a.sync({}));
|
|
122
|
+
expect(items.map((i) => i.originalId)).toEqual([
|
|
123
|
+
"jid-1",
|
|
124
|
+
"chat-2",
|
|
125
|
+
"msg-3",
|
|
126
|
+
"call-4",
|
|
127
|
+
]);
|
|
128
|
+
expect(items.map((i) => i.payload.kind)).toEqual([
|
|
129
|
+
"contact",
|
|
130
|
+
"chat",
|
|
131
|
+
"message",
|
|
132
|
+
"call",
|
|
133
|
+
]);
|
|
134
|
+
// message timestamp seconds → ms
|
|
135
|
+
expect(items[2].capturedAt).toBe(1716383021 * 1000);
|
|
136
|
+
expect(log.opened.opts).toEqual({ readonly: true });
|
|
137
|
+
expect(log.closed).toBe(true);
|
|
138
|
+
} finally {
|
|
139
|
+
fs.unlinkSync(p);
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("falls back to legacy `messages` table when `message` missing", async () => {
|
|
144
|
+
const p = writeTmpDb();
|
|
145
|
+
try {
|
|
146
|
+
const a = new WhatsAppAdapter({
|
|
147
|
+
dbPath: p,
|
|
148
|
+
dbDriverFactory: makeFakeDriverFactory({
|
|
149
|
+
"FROM jid": [],
|
|
150
|
+
"FROM chat": [],
|
|
151
|
+
"FROM messages ": [MSG_ROW],
|
|
152
|
+
"FROM call_log": [],
|
|
153
|
+
}),
|
|
154
|
+
});
|
|
155
|
+
const items = await collect(a.sync({}));
|
|
156
|
+
expect(items).toHaveLength(1);
|
|
157
|
+
expect(items[0].payload.kind).toBe("message");
|
|
158
|
+
} finally {
|
|
159
|
+
fs.unlinkSync(p);
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it("returns silently when db file missing", async () => {
|
|
164
|
+
const a = new WhatsAppAdapter({
|
|
165
|
+
dbPath: path.join(os.tmpdir(), "nonexistent-wa.db"),
|
|
166
|
+
dbDriverFactory: makeFakeDriverFactory({}),
|
|
167
|
+
});
|
|
168
|
+
expect(await collect(a.sync({}))).toEqual([]);
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
describe("normalize", () => {
|
|
173
|
+
const a = new WhatsAppAdapter();
|
|
174
|
+
|
|
175
|
+
it("personal jid → contact person with digits-only phone identifier", () => {
|
|
176
|
+
const batch = a.normalize({
|
|
177
|
+
originalId: "jid-1",
|
|
178
|
+
payload: {
|
|
179
|
+
kind: "contact",
|
|
180
|
+
row: {
|
|
181
|
+
_id: 1,
|
|
182
|
+
user: "+86 138-0013-8000",
|
|
183
|
+
raw_string: "8613800138000@s.whatsapp.net",
|
|
184
|
+
display_name: "Alice",
|
|
185
|
+
},
|
|
186
|
+
},
|
|
187
|
+
});
|
|
188
|
+
const person = batch.persons[0];
|
|
189
|
+
expect(person.subtype).toBe("contact");
|
|
190
|
+
expect(person.names).toEqual(["Alice", "+86 138-0013-8000"]);
|
|
191
|
+
expect(person.identifiers).toEqual({ phone: ["8613800138000"] });
|
|
192
|
+
expect(batch.topics).toEqual([]);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it("group jid (@g.us) → topic, not person", () => {
|
|
196
|
+
const batch = a.normalize({
|
|
197
|
+
originalId: "jid-9",
|
|
198
|
+
payload: {
|
|
199
|
+
kind: "contact",
|
|
200
|
+
row: {
|
|
201
|
+
_id: 9,
|
|
202
|
+
raw_string: "1234-5678@g.us",
|
|
203
|
+
display_name: "Family group",
|
|
204
|
+
},
|
|
205
|
+
},
|
|
206
|
+
});
|
|
207
|
+
expect(batch.persons).toEqual([]);
|
|
208
|
+
expect(batch.topics[0]).toMatchObject({
|
|
209
|
+
id: "topic-whatsapp-1234-5678@g.us",
|
|
210
|
+
name: "Family group",
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it("chat row → topic named by subject", () => {
|
|
215
|
+
const batch = a.normalize({
|
|
216
|
+
originalId: "chat-2",
|
|
217
|
+
payload: { kind: "chat", row: { _id: 2, subject: "Work chat" } },
|
|
218
|
+
});
|
|
219
|
+
expect(batch.topics[0]).toMatchObject({
|
|
220
|
+
id: "topic-whatsapp-chat-2",
|
|
221
|
+
name: "Work chat",
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it("outgoing message → message event from person-self, title truncated to 80", () => {
|
|
226
|
+
const longText = "x".repeat(200);
|
|
227
|
+
const batch = a.normalize({
|
|
228
|
+
originalId: "msg-3",
|
|
229
|
+
payload: {
|
|
230
|
+
kind: "message",
|
|
231
|
+
row: {
|
|
232
|
+
_id: 3,
|
|
233
|
+
key_remote_jid: "8613800138000@s.whatsapp.net",
|
|
234
|
+
from_me: 1,
|
|
235
|
+
text_data: longText,
|
|
236
|
+
timestamp: 1716383021000,
|
|
237
|
+
},
|
|
238
|
+
},
|
|
239
|
+
});
|
|
240
|
+
const ev = batch.events[0];
|
|
241
|
+
expect(ev.subtype).toBe("message");
|
|
242
|
+
expect(ev.actor).toBe("person-self");
|
|
243
|
+
expect(ev.occurredAt).toBe(1716383021000);
|
|
244
|
+
expect(ev.content.title).toHaveLength(80);
|
|
245
|
+
expect(ev.content.text).toHaveLength(200);
|
|
246
|
+
expect(ev.extra.isOutgoing).toBe(true);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it("incoming empty message → '(空)' title, actor from jid", () => {
|
|
250
|
+
const batch = a.normalize({
|
|
251
|
+
originalId: "msg-4",
|
|
252
|
+
payload: {
|
|
253
|
+
kind: "message",
|
|
254
|
+
row: {
|
|
255
|
+
_id: 4,
|
|
256
|
+
key_remote_jid: "86139@s.whatsapp.net",
|
|
257
|
+
from_me: 0,
|
|
258
|
+
timestamp: 1716383021000,
|
|
259
|
+
},
|
|
260
|
+
},
|
|
261
|
+
});
|
|
262
|
+
const ev = batch.events[0];
|
|
263
|
+
expect(ev.content.title).toBe("(空)");
|
|
264
|
+
expect(ev.actor).toBe("person-whatsapp-86139@s.whatsapp.net");
|
|
265
|
+
expect(ev.extra.isOutgoing).toBe(false);
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
it("incoming video call → call event with duration/isVideo extras", () => {
|
|
269
|
+
const batch = a.normalize({
|
|
270
|
+
originalId: "call-5",
|
|
271
|
+
payload: {
|
|
272
|
+
kind: "call",
|
|
273
|
+
row: {
|
|
274
|
+
_id: 5,
|
|
275
|
+
jid_row_id: 1,
|
|
276
|
+
from_me: 0,
|
|
277
|
+
video_call: 1,
|
|
278
|
+
duration: 65,
|
|
279
|
+
timestamp: 1716383021000,
|
|
280
|
+
},
|
|
281
|
+
},
|
|
282
|
+
});
|
|
283
|
+
const ev = batch.events[0];
|
|
284
|
+
expect(ev.subtype).toBe("call");
|
|
285
|
+
expect(ev.content.title).toBe("WhatsApp call (video)");
|
|
286
|
+
expect(ev.actor).toBe("person-whatsapp-1");
|
|
287
|
+
expect(ev.extra).toMatchObject({ duration: 65, isVideo: true, fromMe: false });
|
|
288
|
+
});
|
|
289
|
+
});
|