@chainlesschain/personal-data-hub 0.3.9 → 0.4.0
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/apple-health.test.js +95 -0
- package/__tests__/adapters/email-templates.test.js +123 -0
- package/__tests__/adapters/family-23-collectors-scaffold.test.js +178 -0
- package/__tests__/adapters/game-genshin-scaffold.test.js +107 -0
- package/__tests__/adapters/git-activity.test.js +7 -1
- package/__tests__/adapters/local-im-pc.test.js +149 -0
- package/__tests__/adapters/netease-music.test.js +74 -0
- package/__tests__/adapters/qq-pc-direct-read.test.js +186 -0
- package/__tests__/adapters/system-data-adapter.test.js +4 -1
- package/__tests__/adapters/wechat-pc-direct-read.test.js +207 -0
- package/__tests__/adapters/weread.test.js +123 -0
- package/__tests__/analysis.test.js +120 -15
- package/__tests__/mobile-extractor-encrypted.test.js +460 -0
- package/__tests__/prompt-builder.test.js +25 -0
- package/__tests__/registry-readiness.test.js +233 -0
- package/__tests__/social-douyin-im-direct-read.test.js +311 -0
- package/__tests__/social-douyin-snapshot.test.js +5 -2
- package/__tests__/vault.test.js +99 -0
- package/lib/adapter-guide.js +520 -0
- package/lib/adapter-readiness.js +257 -0
- package/lib/adapters/_local-im-db-reader.js +218 -0
- package/lib/adapters/_local-im-pc-adapter.js +162 -0
- package/lib/adapters/apple-health/index.js +329 -0
- package/lib/adapters/dingtalk-pc/index.js +29 -0
- package/lib/adapters/edu-huawei-learning/api-client.js +47 -0
- package/lib/adapters/edu-huawei-learning/index.js +255 -0
- package/lib/adapters/edu-zuoyebang/api-client.js +48 -0
- package/lib/adapters/edu-zuoyebang/index.js +259 -0
- package/lib/adapters/email-imap/email-adapter.js +16 -0
- package/lib/adapters/email-imap/templates/bill.js +174 -18
- package/lib/adapters/feishu-pc/index.js +29 -0
- package/lib/adapters/finance-alipay/api-client.js +48 -0
- package/lib/adapters/finance-alipay/index.js +257 -0
- package/lib/adapters/game-genshin/api-client.js +59 -0
- package/lib/adapters/game-genshin/index.js +274 -0
- package/lib/adapters/game-honor-of-kings/api-client.js +54 -0
- package/lib/adapters/game-honor-of-kings/index.js +259 -0
- package/lib/adapters/netease-music/index.js +227 -0
- package/lib/adapters/qq-pc/index.js +200 -0
- package/lib/adapters/qq-pc/nt-db-reader.js +210 -0
- package/lib/adapters/social-douyin/index.js +194 -1
- package/lib/adapters/wechat/wechat-adapter.js +7 -1
- package/lib/adapters/wechat-pc/index.js +335 -0
- package/lib/adapters/wechat-pc/pc-db-reader.js +327 -0
- package/lib/adapters/weread/api-client.js +128 -0
- package/lib/adapters/weread/index.js +337 -0
- package/lib/analysis.js +65 -0
- package/lib/index.js +39 -0
- package/lib/mobile-extractor/bplist.js +233 -0
- package/lib/mobile-extractor/ios-backup-crypto.js +315 -0
- package/lib/mobile-extractor/ios.js +131 -16
- package/lib/prompt-builder.js +11 -1
- package/lib/registry.js +170 -0
- package/lib/vault.js +105 -0
- package/package.json +1 -1
- package/scripts/run-native-tests-sandbox.sh +2 -0
- package/vitest.config.js +79 -1
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { describe, it, expect } from "vitest";
|
|
4
|
+
|
|
5
|
+
const { NeteaseMusicAdapter } = require("../../lib/adapters/netease-music");
|
|
6
|
+
const { partitionBatch } = require("../../lib/batch");
|
|
7
|
+
|
|
8
|
+
const SNAPSHOT = {
|
|
9
|
+
schemaVersion: 1,
|
|
10
|
+
snapshottedAt: 1700000000000,
|
|
11
|
+
account: { uid: "42", nickname: "me" },
|
|
12
|
+
events: [
|
|
13
|
+
{ kind: "play", id: "p1", capturedAt: 1700000001000, song: "晴天", artist: "周杰伦", album: "叶惠美", songId: "186016", playCount: 50 },
|
|
14
|
+
{ kind: "favorite", id: "f1", capturedAt: 1700000002000, song: "稻香", artist: "周杰伦", songId: "186001" },
|
|
15
|
+
{ kind: "playlist", id: "pl1", capturedAt: 1700000003000, name: "我喜欢的音乐", playlistId: "999", trackCount: 200, creator: "me" },
|
|
16
|
+
{ kind: "bogus", id: "x" },
|
|
17
|
+
],
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
function adapter(snap = SNAPSHOT, { exists = true } = {}) {
|
|
21
|
+
const a = new NeteaseMusicAdapter();
|
|
22
|
+
a._deps.fs = {
|
|
23
|
+
existsSync: () => exists,
|
|
24
|
+
readFileSync: () => JSON.stringify(snap),
|
|
25
|
+
accessSync: () => {},
|
|
26
|
+
constants: { R_OK: 4 },
|
|
27
|
+
};
|
|
28
|
+
return a;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async function collect(iter) {
|
|
32
|
+
const out = [];
|
|
33
|
+
for await (const r of iter) out.push(r);
|
|
34
|
+
return out;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
describe("NeteaseMusicAdapter", () => {
|
|
38
|
+
it("readinessOnly → NO_INPUT (snapshot)", async () => {
|
|
39
|
+
const r = await new NeteaseMusicAdapter().authenticate({ readinessOnly: true });
|
|
40
|
+
expect(r.reason).toBe("NO_INPUT");
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("ingests play/favorite/playlist, skips unknown kinds", async () => {
|
|
44
|
+
const raws = await collect(adapter().sync({ inputPath: "/x" }));
|
|
45
|
+
expect(raws.map((r) => r.kind)).toEqual(["play", "favorite", "playlist"]);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("normalizes to valid batch (events + items + topic)", async () => {
|
|
49
|
+
const a = adapter();
|
|
50
|
+
const raws = await collect(a.sync({ inputPath: "/x" }));
|
|
51
|
+
const merged = { events: [], persons: [], places: [], items: [], topics: [] };
|
|
52
|
+
for (const r of raws) {
|
|
53
|
+
const n = a.normalize(r);
|
|
54
|
+
for (const k of Object.keys(merged)) merged[k].push(...n[k]);
|
|
55
|
+
}
|
|
56
|
+
const { valid, invalidReasons } = partitionBatch(merged);
|
|
57
|
+
expect(invalidReasons).toHaveLength(0);
|
|
58
|
+
expect(valid.events).toHaveLength(2); // play + favorite
|
|
59
|
+
expect(valid.items).toHaveLength(2); // two songs
|
|
60
|
+
expect(valid.topics).toHaveLength(1); // playlist
|
|
61
|
+
const play = valid.events.find((e) => e.subtype === "media");
|
|
62
|
+
expect(play.content.title).toContain("晴天");
|
|
63
|
+
expect(valid.topics[0].name).toBe("我喜欢的音乐");
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("schemaVersion mismatch throws", async () => {
|
|
67
|
+
const a = adapter({ schemaVersion: 99, events: [] });
|
|
68
|
+
await expect(collect(a.sync({ inputPath: "/x" }))).rejects.toThrow(/schemaVersion/);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("missing file yields nothing", async () => {
|
|
72
|
+
expect(await collect(adapter(SNAPSHOT, { exists: false }).sync({ inputPath: "/x" }))).toHaveLength(0);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { describe, it, expect } from "vitest";
|
|
4
|
+
|
|
5
|
+
const { QQPcAdapter } = require("../../lib/adapters/qq-pc");
|
|
6
|
+
const { partitionBatch } = require("../../lib/batch");
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* QQ NT (PC desktop) local-direct-read — 本地直读样板 (ported from wechat-pc).
|
|
10
|
+
*
|
|
11
|
+
* No native SQLite: fake driver via `_deps.dbDriverFactory`. The point of
|
|
12
|
+
* these tests is the PLUMBING + honest defensiveness (resolve columns,
|
|
13
|
+
* preserve raw row, loud diagnostic, best-effort text) — not protobuf
|
|
14
|
+
* decoding, which is real-device tuning.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
function makeFakeDb(spec) {
|
|
18
|
+
class FakeStmt {
|
|
19
|
+
constructor(sql) {
|
|
20
|
+
this.sql = sql;
|
|
21
|
+
}
|
|
22
|
+
all() {
|
|
23
|
+
const s = this.sql;
|
|
24
|
+
const m = s.match(/PRAGMA table_info\((\w+)\)/);
|
|
25
|
+
if (m) return spec.cols[m[1]] || [];
|
|
26
|
+
const f = s.match(/FROM (\w+)/);
|
|
27
|
+
if (f) return spec.rows[f[1]] || [];
|
|
28
|
+
return [];
|
|
29
|
+
}
|
|
30
|
+
get() {
|
|
31
|
+
return { n: 1 };
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return class FakeDb {
|
|
35
|
+
// eslint-disable-next-line no-unused-vars
|
|
36
|
+
constructor(_path, _opts) {}
|
|
37
|
+
prepare(sql) {
|
|
38
|
+
return new FakeStmt(sql);
|
|
39
|
+
}
|
|
40
|
+
pragma() {}
|
|
41
|
+
exec() {}
|
|
42
|
+
close() {}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// readable-name schema (decrypted/re-exported db) — text resolves cleanly
|
|
47
|
+
const READABLE_SPEC = {
|
|
48
|
+
cols: {
|
|
49
|
+
c2c_msg_table: [
|
|
50
|
+
{ name: "msgId" },
|
|
51
|
+
{ name: "msgTime" },
|
|
52
|
+
{ name: "msgType" },
|
|
53
|
+
{ name: "senderUin" },
|
|
54
|
+
{ name: "peerUin" },
|
|
55
|
+
{ name: "content" },
|
|
56
|
+
],
|
|
57
|
+
group_msg_table: [
|
|
58
|
+
{ name: "msgId" },
|
|
59
|
+
{ name: "msgTime" },
|
|
60
|
+
{ name: "senderUin" },
|
|
61
|
+
{ name: "peerUin" },
|
|
62
|
+
{ name: "content" },
|
|
63
|
+
],
|
|
64
|
+
},
|
|
65
|
+
rows: {
|
|
66
|
+
c2c_msg_table: [
|
|
67
|
+
{ msgId: "c1", msgTime: 1700000000, msgType: 1, senderUin: "111", peerUin: "222", content: "hi there" },
|
|
68
|
+
],
|
|
69
|
+
group_msg_table: [
|
|
70
|
+
{ msgId: "g1", msgTime: 1700000100, senderUin: "333", peerUin: "9001", content: "群里大家好" },
|
|
71
|
+
],
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
// obfuscated numeric schema + BLOB content — text is null but raw preserved
|
|
76
|
+
const NUMERIC_SPEC = {
|
|
77
|
+
cols: {
|
|
78
|
+
c2c_msg_table: [
|
|
79
|
+
{ name: "40001" }, // msgId
|
|
80
|
+
{ name: "40050" }, // time
|
|
81
|
+
{ name: "40011" }, // type
|
|
82
|
+
{ name: "40033" }, // sender
|
|
83
|
+
{ name: "40021" }, // peer
|
|
84
|
+
{ name: "40800" }, // content (blob)
|
|
85
|
+
],
|
|
86
|
+
},
|
|
87
|
+
rows: {
|
|
88
|
+
c2c_msg_table: [
|
|
89
|
+
{ "40001": 9001, "40050": 1700000000, "40011": 1, "40033": 111, "40021": 222, "40800": Buffer.from([1, 2, 3]) },
|
|
90
|
+
],
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
function freshAdapter(spec, { fsOverride } = {}) {
|
|
95
|
+
const a = new QQPcAdapter({ dbPath: "/fake/nt_msg.db" });
|
|
96
|
+
a._deps.fs = fsOverride || { existsSync: () => true, accessSync: () => {}, constants: { R_OK: 4 } };
|
|
97
|
+
a._deps.dbDriverFactory = () => makeFakeDb(spec);
|
|
98
|
+
return a;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async function collect(iter) {
|
|
102
|
+
const out = [];
|
|
103
|
+
for await (const r of iter) out.push(r);
|
|
104
|
+
return out;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
describe("QQPcAdapter — readiness + construction", () => {
|
|
108
|
+
it("no-arg construct + DB_NOT_PULLED readiness", async () => {
|
|
109
|
+
const a = new QQPcAdapter();
|
|
110
|
+
expect(a.name).toBe("qq-pc");
|
|
111
|
+
expect(a.dataDisclosure.legalGate).toBe(true);
|
|
112
|
+
const r = await a.authenticate({ readinessOnly: true });
|
|
113
|
+
expect(r.reason).toBe("DB_NOT_PULLED");
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
describe("QQPcAdapter — nt_msg.db (readable schema)", () => {
|
|
118
|
+
it("reads c2c + group messages → valid events, 0 invalid", async () => {
|
|
119
|
+
const a = freshAdapter(READABLE_SPEC);
|
|
120
|
+
const raws = await collect(a.sync({ dbPath: "/fake/nt_msg.db" }));
|
|
121
|
+
expect(raws).toHaveLength(2);
|
|
122
|
+
expect(raws.every((r) => r.kind === "message")).toBe(true);
|
|
123
|
+
const merged = { events: [], persons: [], places: [], items: [], topics: [] };
|
|
124
|
+
for (const r of raws) {
|
|
125
|
+
const n = a.normalize(r);
|
|
126
|
+
for (const k of Object.keys(merged)) merged[k].push(...n[k]);
|
|
127
|
+
}
|
|
128
|
+
const { valid, invalidReasons } = partitionBatch(merged);
|
|
129
|
+
expect(invalidReasons).toHaveLength(0);
|
|
130
|
+
expect(valid.events).toHaveLength(2);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("resolves text + flags group + preserves timestamp", async () => {
|
|
134
|
+
const a = freshAdapter(READABLE_SPEC);
|
|
135
|
+
const raws = await collect(a.sync({ dbPath: "/fake/nt_msg.db" }));
|
|
136
|
+
const group = raws.find((r) => r.payload.isGroup);
|
|
137
|
+
expect(group.payload.text).toBe("群里大家好");
|
|
138
|
+
expect(group.payload.createdTimeMs).toBe(1700000100000);
|
|
139
|
+
const ev = a.normalize(group).events[0];
|
|
140
|
+
expect(ev.extra.isGroup).toBe(true);
|
|
141
|
+
expect(ev.extra.textResolved).toBe(true);
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
describe("QQPcAdapter — nt_msg.db (numeric/obfuscated + BLOB body)", () => {
|
|
146
|
+
it("still ingests, text null, raw row preserved, loud diagnostic", async () => {
|
|
147
|
+
const a = freshAdapter(NUMERIC_SPEC);
|
|
148
|
+
const events = [];
|
|
149
|
+
const raws = await collect(
|
|
150
|
+
a.sync({ dbPath: "/fake/nt_msg.db", onProgress: (e) => events.push(e) }),
|
|
151
|
+
);
|
|
152
|
+
expect(raws).toHaveLength(1);
|
|
153
|
+
const ev = a.normalize(raws[0]).events[0];
|
|
154
|
+
// No silent drop: it's a valid event even with unresolved protobuf text.
|
|
155
|
+
const { valid, invalidReasons } = partitionBatch({
|
|
156
|
+
events: [ev], persons: [], places: [], items: [], topics: [],
|
|
157
|
+
});
|
|
158
|
+
expect(invalidReasons).toHaveLength(0);
|
|
159
|
+
expect(valid.events).toHaveLength(1);
|
|
160
|
+
expect(ev.extra.textResolved).toBe(false);
|
|
161
|
+
expect(ev.extra.rawRow).toBeTruthy(); // nothing lost
|
|
162
|
+
// diagnostic tells the user what resolved
|
|
163
|
+
const diag = events.find((e) => e.phase === "qq-nt-read");
|
|
164
|
+
expect(diag.hadC2cTable).toBe(true);
|
|
165
|
+
expect(diag.messageCount).toBe(1);
|
|
166
|
+
expect(diag.resolvedColumns.c2c_msg_table.time).toBe("40050");
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
describe("QQPcAdapter — edge cases", () => {
|
|
171
|
+
it("respects limit", async () => {
|
|
172
|
+
const a = freshAdapter(READABLE_SPEC);
|
|
173
|
+
const capped = await collect(a.sync({ dbPath: "/fake/nt_msg.db", limit: 1 }));
|
|
174
|
+
expect(capped).toHaveLength(1);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("missing db yields nothing", async () => {
|
|
178
|
+
const a = freshAdapter(READABLE_SPEC, { fsOverride: { existsSync: () => false } });
|
|
179
|
+
expect(await collect(a.sync({ dbPath: "/nope.db" }))).toHaveLength(0);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("unknown normalize kind throws", () => {
|
|
183
|
+
const a = new QQPcAdapter();
|
|
184
|
+
expect(() => a.normalize({ kind: "x", payload: { kind: "x" } })).toThrow(/unknown kind/);
|
|
185
|
+
});
|
|
186
|
+
});
|
|
@@ -350,7 +350,10 @@ describe("SystemDataAdapter.sync ADB pull flow", () => {
|
|
|
350
350
|
});
|
|
351
351
|
const adapter = new SystemDataAdapter({ supervisor: sup });
|
|
352
352
|
|
|
353
|
-
|
|
353
|
+
// No scratchDir → adapter defaults to fs.mkdtempSync(os.tmpdir()). Don't pass
|
|
354
|
+
// an absolute "/scratch": it mkdir's at FS root, which is EACCES on Linux CI
|
|
355
|
+
// (passed on Windows where /scratch maps to a creatable drive-relative path).
|
|
356
|
+
const iter = adapter.sync({ serial: "redmi" });
|
|
354
357
|
for await (const _ of iter) { /* drain */ }
|
|
355
358
|
|
|
356
359
|
expect(pullCalls).toEqual([
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { describe, it, expect } from "vitest";
|
|
4
|
+
|
|
5
|
+
const { WeChatPcAdapter } = require("../../lib/adapters/wechat-pc");
|
|
6
|
+
const { partitionBatch } = require("../../lib/batch");
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* WeChat **PC desktop** local-direct-read (本地直读样板, ported from Douyin).
|
|
10
|
+
*
|
|
11
|
+
* No native SQLite: a fake Database driver is injected via
|
|
12
|
+
* `_deps.dbDriverFactory` (pc-db-reader accepts it as `_databaseClass`).
|
|
13
|
+
* Covers the message/contact normalize + the MSG / Contact table read +
|
|
14
|
+
* group-message sender-prefix parsing + key-vs-plaintext routing.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
// Fake better-sqlite3-style driver answering pc-db-reader's PRAGMA + SELECTs.
|
|
18
|
+
function makeFakeDb(spec) {
|
|
19
|
+
class FakeStmt {
|
|
20
|
+
constructor(sql) {
|
|
21
|
+
this.sql = sql;
|
|
22
|
+
}
|
|
23
|
+
all() {
|
|
24
|
+
const s = this.sql;
|
|
25
|
+
if (/PRAGMA table_info\(MSG\)/.test(s)) return spec.msgCols || [];
|
|
26
|
+
if (/FROM MSG/.test(s)) return spec.msgRows || [];
|
|
27
|
+
if (/PRAGMA table_info\(Contact\)/.test(s)) return spec.contactCols || [];
|
|
28
|
+
if (/FROM Contact/.test(s)) return spec.contactRows || [];
|
|
29
|
+
return [];
|
|
30
|
+
}
|
|
31
|
+
get() {
|
|
32
|
+
return { n: 1 }; // sqlite_master probe
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return class FakeDb {
|
|
36
|
+
// eslint-disable-next-line no-unused-vars
|
|
37
|
+
constructor(_path, _opts) {}
|
|
38
|
+
prepare(sql) {
|
|
39
|
+
return new FakeStmt(sql);
|
|
40
|
+
}
|
|
41
|
+
pragma() {}
|
|
42
|
+
exec() {}
|
|
43
|
+
close() {}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const MSG_SPEC = {
|
|
48
|
+
msgCols: [
|
|
49
|
+
{ name: "MsgSvrID" },
|
|
50
|
+
{ name: "StrTalker" },
|
|
51
|
+
{ name: "IsSender" },
|
|
52
|
+
{ name: "CreateTime" },
|
|
53
|
+
{ name: "Type" },
|
|
54
|
+
{ name: "StrContent" },
|
|
55
|
+
],
|
|
56
|
+
msgRows: [
|
|
57
|
+
{ msgSvrId: "700001", talker: "wxid_bob", isSend: 0, createTime: 1700000000, type: 1, content: "你好啊" },
|
|
58
|
+
{ msgSvrId: "700002", talker: "wxid_bob", isSend: 1, createTime: 1700000010, type: 1, content: "在的" },
|
|
59
|
+
{ msgSvrId: "700003", talker: "room1@chatroom", isSend: 0, createTime: 1700000020, type: 1, content: "wxid_carol:\n大家好" },
|
|
60
|
+
],
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const CONTACT_SPEC = {
|
|
64
|
+
contactCols: [
|
|
65
|
+
{ name: "UserName" },
|
|
66
|
+
{ name: "Alias" },
|
|
67
|
+
{ name: "NickName" },
|
|
68
|
+
{ name: "Remark" },
|
|
69
|
+
{ name: "Type" },
|
|
70
|
+
],
|
|
71
|
+
contactRows: [
|
|
72
|
+
{ wxid: "wxid_bob", alias: "bob123", nickname: "Bob", remark: "老鲍", type: 3 },
|
|
73
|
+
{ wxid: "gh_official01", alias: null, nickname: "某公众号", remark: null, type: 3 },
|
|
74
|
+
{ wxid: "room1@chatroom", alias: null, nickname: "群", remark: null, type: 2 }, // filtered
|
|
75
|
+
],
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
function freshAdapter(spec, { fsOverride } = {}) {
|
|
79
|
+
const a = new WeChatPcAdapter({ dbPath: "/fake/MSG0.db" });
|
|
80
|
+
a._deps.fs = fsOverride || { existsSync: () => true, accessSync: () => {}, constants: { R_OK: 4 } };
|
|
81
|
+
a._deps.dbDriverFactory = () => makeFakeDb(spec);
|
|
82
|
+
return a;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async function collect(iter) {
|
|
86
|
+
const out = [];
|
|
87
|
+
for await (const r of iter) out.push(r);
|
|
88
|
+
return out;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
describe("WeChatPcAdapter — readiness + construction", () => {
|
|
92
|
+
it("constructs no-arg and reports DB_NOT_PULLED via readinessOnly", async () => {
|
|
93
|
+
const a = new WeChatPcAdapter();
|
|
94
|
+
expect(a.name).toBe("wechat-pc");
|
|
95
|
+
expect(a.extractMode).toBe("device-pull");
|
|
96
|
+
expect(a.dataDisclosure.legalGate).toBe(true);
|
|
97
|
+
const r = await a.authenticate({ readinessOnly: true });
|
|
98
|
+
expect(r.ok).toBe(false);
|
|
99
|
+
expect(r.reason).toBe("DB_NOT_PULLED");
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("readinessOnly with a configured dbPath reports configured (no DB open)", async () => {
|
|
103
|
+
const a = new WeChatPcAdapter({ dbPath: "/some/MSG0.db" });
|
|
104
|
+
const r = await a.authenticate({ readinessOnly: true });
|
|
105
|
+
expect(r.ok).toBe(true);
|
|
106
|
+
expect(r.mode).toBe("configured");
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
describe("WeChatPcAdapter — MSG*.db messages", () => {
|
|
111
|
+
it("reads MSG rows → message raws with stable ids", async () => {
|
|
112
|
+
const a = freshAdapter(MSG_SPEC);
|
|
113
|
+
const raws = await collect(a.sync({ dbPath: "/fake/MSG0.db" }));
|
|
114
|
+
expect(raws.map((r) => r.kind)).toEqual(["message", "message", "message"]);
|
|
115
|
+
expect(raws.map((r) => r.originalId)).toEqual([
|
|
116
|
+
"wechat-pc:message:700001",
|
|
117
|
+
"wechat-pc:message:700002",
|
|
118
|
+
"wechat-pc:message:700003",
|
|
119
|
+
]);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("messages normalize to valid events (+ contact persons + group topic), 0 invalid", async () => {
|
|
123
|
+
const a = freshAdapter(MSG_SPEC);
|
|
124
|
+
const raws = await collect(a.sync({ dbPath: "/fake/MSG0.db" }));
|
|
125
|
+
const merged = { events: [], persons: [], places: [], items: [], topics: [] };
|
|
126
|
+
for (const r of raws) {
|
|
127
|
+
const n = a.normalize(r);
|
|
128
|
+
for (const k of Object.keys(merged)) merged[k].push(...n[k]);
|
|
129
|
+
}
|
|
130
|
+
const { valid, invalidReasons } = partitionBatch(merged);
|
|
131
|
+
expect(invalidReasons).toHaveLength(0);
|
|
132
|
+
expect(valid.events).toHaveLength(3);
|
|
133
|
+
// bob (1-on-1) + carol (group sender)
|
|
134
|
+
const personIds = valid.persons.map((p) => p.id).sort();
|
|
135
|
+
expect(personIds).toContain("person-wechat-wxid_bob");
|
|
136
|
+
expect(personIds).toContain("person-wechat-wxid_carol");
|
|
137
|
+
// the chatroom becomes a topic
|
|
138
|
+
expect(valid.topics.map((t) => t.id)).toContain("topic-wechat-group-room1@chatroom");
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("strips the group-message sender prefix from the text", async () => {
|
|
142
|
+
const a = freshAdapter(MSG_SPEC);
|
|
143
|
+
const raws = await collect(a.sync({ dbPath: "/fake/MSG0.db" }));
|
|
144
|
+
const groupRaw = raws.find((r) => r.payload.talker === "room1@chatroom");
|
|
145
|
+
expect(groupRaw.payload.senderWxid).toBe("wxid_carol");
|
|
146
|
+
expect(groupRaw.payload.text).toBe("大家好");
|
|
147
|
+
const ev = a.normalize(groupRaw).events[0];
|
|
148
|
+
expect(ev.content.text).toBe("大家好");
|
|
149
|
+
expect(ev.extra.isGroup).toBe(true);
|
|
150
|
+
expect(ev.actor).toBe("person-wechat-wxid_carol");
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("CreateTime (seconds) normalizes to ms", async () => {
|
|
154
|
+
const a = freshAdapter(MSG_SPEC);
|
|
155
|
+
const raws = await collect(a.sync({ dbPath: "/fake/MSG0.db" }));
|
|
156
|
+
expect(raws[0].payload.createdTimeMs).toBe(1700000000000);
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
describe("WeChatPcAdapter — MicroMsg.db contacts", () => {
|
|
161
|
+
it("reads Contact rows → contact persons; skips @chatroom; gh_ → merchant", async () => {
|
|
162
|
+
const a = freshAdapter(CONTACT_SPEC);
|
|
163
|
+
const raws = await collect(a.sync({ dbPath: "/fake/MicroMsg.db" }));
|
|
164
|
+
expect(raws.map((r) => r.kind)).toEqual(["contact", "contact"]); // chatroom filtered
|
|
165
|
+
const persons = raws.map((r) => a.normalize(r).persons[0]);
|
|
166
|
+
const bob = persons.find((p) => p.id === "person-wechat-wxid_bob");
|
|
167
|
+
expect(bob.subtype).toBe("contact");
|
|
168
|
+
expect(bob.names[0]).toBe("老鲍"); // remark wins
|
|
169
|
+
expect(bob.identifiers.wechatId).toBe("wxid_bob");
|
|
170
|
+
const gh = persons.find((p) => p.id === "person-wechat-gh_official01");
|
|
171
|
+
expect(gh.subtype).toBe("merchant");
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
describe("WeChatPcAdapter — options + edge cases", () => {
|
|
176
|
+
it("respects include={message:false} and limit", async () => {
|
|
177
|
+
const a = freshAdapter(MSG_SPEC);
|
|
178
|
+
const none = await collect(a.sync({ dbPath: "/fake/MSG0.db", include: { message: false } }));
|
|
179
|
+
expect(none).toHaveLength(0);
|
|
180
|
+
const capped = await collect(a.sync({ dbPath: "/fake/MSG0.db", limit: 2 }));
|
|
181
|
+
expect(capped).toHaveLength(2);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it("emits a pc-db-read progress event with the diagnostic", async () => {
|
|
185
|
+
const a = freshAdapter(MSG_SPEC);
|
|
186
|
+
const events = [];
|
|
187
|
+
await collect(a.sync({ dbPath: "/fake/MSG0.db", onProgress: (e) => events.push(e) }));
|
|
188
|
+
const parsed = events.find((e) => e.phase === "pc-db-read");
|
|
189
|
+
expect(parsed).toBeTruthy();
|
|
190
|
+
expect(parsed.hadMsgTable).toBe(true);
|
|
191
|
+
expect(parsed.messageCount).toBe(3);
|
|
192
|
+
expect(parsed.mode).toBe("plaintext"); // no key supplied
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it("missing db file yields nothing (no throw)", async () => {
|
|
196
|
+
const a = freshAdapter(MSG_SPEC, { fsOverride: { existsSync: () => false } });
|
|
197
|
+
const raws = await collect(a.sync({ dbPath: "/nope/MSG0.db" }));
|
|
198
|
+
expect(raws).toHaveLength(0);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it("unknown normalize kind throws", () => {
|
|
202
|
+
const a = new WeChatPcAdapter();
|
|
203
|
+
expect(() => a.normalize({ kind: "weird", payload: { kind: "weird" } })).toThrow(
|
|
204
|
+
/unknown kind/,
|
|
205
|
+
);
|
|
206
|
+
});
|
|
207
|
+
});
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { describe, it, expect } from "vitest";
|
|
4
|
+
|
|
5
|
+
const { WeReadAdapter } = require("../../lib/adapters/weread");
|
|
6
|
+
const { WeReadApiClient } = require("../../lib/adapters/weread/api-client");
|
|
7
|
+
const { partitionBatch } = require("../../lib/batch");
|
|
8
|
+
|
|
9
|
+
// ── stub fetch returning canned WeRead JSON by URL ──────────────────────
|
|
10
|
+
function makeFetch(routes) {
|
|
11
|
+
return async (url) => {
|
|
12
|
+
for (const [pat, body] of routes) {
|
|
13
|
+
if (url.includes(pat)) {
|
|
14
|
+
return {
|
|
15
|
+
ok: true,
|
|
16
|
+
status: 200,
|
|
17
|
+
headers: { get: () => null },
|
|
18
|
+
json: async () => body,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return { ok: true, status: 200, headers: { get: () => null }, json: async () => ({}) };
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const ROUTES = [
|
|
27
|
+
["/user/notebooks", { books: [{ bookId: "b1", book: { title: "人类简史", author: "赫拉利", cover: "c" }, noteCount: 2, reviewCount: 1 }] }],
|
|
28
|
+
["/book/bookmarklist", { updated: [{ bookmarkId: "m1", bookId: "b1", markText: "认知革命", chapterTitle: "第一章", createTime: 1700000000 }] }],
|
|
29
|
+
["/review/list", { reviews: [{ review: { reviewId: "r1", bookId: "b1", content: "很有启发", chapterTitle: "第一章", createTime: 1700000100 } }] }],
|
|
30
|
+
];
|
|
31
|
+
|
|
32
|
+
async function collect(iter) {
|
|
33
|
+
const out = [];
|
|
34
|
+
for await (const r of iter) out.push(r);
|
|
35
|
+
return out;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
describe("WeReadApiClient (cookie HTTP, stub fetch)", () => {
|
|
39
|
+
it("parses notebooks / bookmarks / reviews defensively", async () => {
|
|
40
|
+
const c = new WeReadApiClient({ cookie: "wr_skey=x", fetch: makeFetch(ROUTES) });
|
|
41
|
+
const books = await c.getNotebooks();
|
|
42
|
+
expect(books).toHaveLength(1);
|
|
43
|
+
expect(books[0].title).toBe("人类简史");
|
|
44
|
+
const marks = await c.getBookmarks("b1");
|
|
45
|
+
expect(marks[0].markText).toBe("认知革命");
|
|
46
|
+
const reviews = await c.getReviews("b1");
|
|
47
|
+
expect(reviews[0].content).toBe("很有启发");
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("requires a cookie", () => {
|
|
51
|
+
expect(() => new WeReadApiClient({})).toThrow(/cookie/);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("degrades a failing endpoint to empty (no throw)", async () => {
|
|
55
|
+
const c = new WeReadApiClient({
|
|
56
|
+
cookie: "x",
|
|
57
|
+
fetch: async () => { throw new Error("network down"); },
|
|
58
|
+
});
|
|
59
|
+
expect(await c.getNotebooks()).toEqual([]);
|
|
60
|
+
expect(c.lastErrorCode).toBeTruthy();
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
describe("WeReadAdapter — cookie mode", () => {
|
|
65
|
+
it("readinessOnly without cookie → INVALID_COOKIE (credential)", async () => {
|
|
66
|
+
const r = await new WeReadAdapter().authenticate({ readinessOnly: true });
|
|
67
|
+
expect(r.reason).toBe("INVALID_COOKIE");
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("readinessOnly with cookie → configured", async () => {
|
|
71
|
+
const r = await new WeReadAdapter({ cookie: "x" }).authenticate({ readinessOnly: true });
|
|
72
|
+
expect(r.ok).toBe(true);
|
|
73
|
+
expect(r.mode).toBe("configured");
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("fetches book + highlight + review and normalizes to a valid batch", async () => {
|
|
77
|
+
const a = new WeReadAdapter();
|
|
78
|
+
const raws = await collect(a.sync({ cookie: "wr_skey=x", fetch: makeFetch(ROUTES) }));
|
|
79
|
+
expect(raws.map((r) => r.kind)).toEqual(["book", "highlight", "review"]);
|
|
80
|
+
const merged = { events: [], persons: [], places: [], items: [], topics: [] };
|
|
81
|
+
for (const r of raws) {
|
|
82
|
+
const n = a.normalize(r);
|
|
83
|
+
for (const k of Object.keys(merged)) merged[k].push(...n[k]);
|
|
84
|
+
}
|
|
85
|
+
const { valid, invalidReasons } = partitionBatch(merged);
|
|
86
|
+
expect(invalidReasons).toHaveLength(0);
|
|
87
|
+
expect(valid.events).toHaveLength(3); // book(browse) + highlight(other) + review(post)
|
|
88
|
+
expect(valid.items).toHaveLength(1); // the book
|
|
89
|
+
expect(valid.events.find((e) => e.subtype === "browse").content.title).toContain("人类简史");
|
|
90
|
+
expect(valid.events.find((e) => e.subtype === "post").content.text).toBe("很有启发");
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("includeNotes:false yields only book events", async () => {
|
|
94
|
+
const a = new WeReadAdapter();
|
|
95
|
+
const raws = await collect(a.sync({ cookie: "x", fetch: makeFetch(ROUTES), includeNotes: false }));
|
|
96
|
+
expect(raws.map((r) => r.kind)).toEqual(["book"]);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
describe("WeReadAdapter — snapshot mode", () => {
|
|
101
|
+
const SNAP = {
|
|
102
|
+
schemaVersion: 1,
|
|
103
|
+
snapshottedAt: 1700000000000,
|
|
104
|
+
events: [
|
|
105
|
+
{ kind: "book", id: "b1", bookId: "b1", title: "三体", author: "刘慈欣" },
|
|
106
|
+
{ kind: "highlight", id: "m1", bookId: "b1", bookTitle: "三体", markText: "不要回答", createTime: 1700000001 },
|
|
107
|
+
],
|
|
108
|
+
};
|
|
109
|
+
function snapAdapter(snap = SNAP, { exists = true } = {}) {
|
|
110
|
+
const a = new WeReadAdapter();
|
|
111
|
+
a._deps.fs = { existsSync: () => exists, readFileSync: () => JSON.stringify(snap), accessSync: () => {}, constants: { R_OK: 4 } };
|
|
112
|
+
return a;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
it("ingests snapshot events", async () => {
|
|
116
|
+
const raws = await collect(snapAdapter().sync({ inputPath: "/x" }));
|
|
117
|
+
expect(raws.map((r) => r.kind)).toEqual(["book", "highlight"]);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("schemaVersion mismatch throws", async () => {
|
|
121
|
+
await expect(collect(snapAdapter({ schemaVersion: 9, events: [] }).sync({ inputPath: "/x" }))).rejects.toThrow(/schemaVersion/);
|
|
122
|
+
});
|
|
123
|
+
});
|