@chainlesschain/personal-data-hub 0.3.9 → 0.4.1
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/README.md +45 -25
- 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,149 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { describe, it, expect } from "vitest";
|
|
4
|
+
|
|
5
|
+
const { DingTalkPcAdapter } = require("../../lib/adapters/dingtalk-pc");
|
|
6
|
+
const { FeishuPcAdapter } = require("../../lib/adapters/feishu-pc");
|
|
7
|
+
const { readLocalImDb } = require("../../lib/adapters/_local-im-db-reader");
|
|
8
|
+
const { partitionBatch } = require("../../lib/batch");
|
|
9
|
+
|
|
10
|
+
// fake driver answering sqlite_master + table_info + SELECT * by table
|
|
11
|
+
function makeFakeDb(spec) {
|
|
12
|
+
class FakeStmt {
|
|
13
|
+
constructor(sql) {
|
|
14
|
+
this.sql = sql;
|
|
15
|
+
}
|
|
16
|
+
all() {
|
|
17
|
+
const s = this.sql;
|
|
18
|
+
if (/type='table'/.test(s)) return (spec.tables || []).map((n) => ({ name: n }));
|
|
19
|
+
const ti = s.match(/table_info\("(\w+)"\)/);
|
|
20
|
+
if (ti) return spec.cols[ti[1]] || [];
|
|
21
|
+
const fr = s.match(/FROM "(\w+)"/);
|
|
22
|
+
if (fr) return spec.rows[fr[1]] || [];
|
|
23
|
+
return [];
|
|
24
|
+
}
|
|
25
|
+
get() {
|
|
26
|
+
return { n: 1 };
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return class FakeDb {
|
|
30
|
+
// eslint-disable-next-line no-unused-vars
|
|
31
|
+
constructor(_p, _o) {}
|
|
32
|
+
prepare(sql) {
|
|
33
|
+
return new FakeStmt(sql);
|
|
34
|
+
}
|
|
35
|
+
pragma() {}
|
|
36
|
+
exec() {}
|
|
37
|
+
close() {}
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const SPEC = {
|
|
42
|
+
// msg_table matches + has time/content → ingested
|
|
43
|
+
// msg_meta matches pattern but no time/content → skipped (loud diagnostic)
|
|
44
|
+
// contact_meta doesn't match pattern → not scanned
|
|
45
|
+
// sqlite_sequence → filtered (sqlite_*)
|
|
46
|
+
tables: ["msg_table", "msg_meta", "contact_meta", "sqlite_sequence"],
|
|
47
|
+
cols: {
|
|
48
|
+
msg_table: [
|
|
49
|
+
{ name: "msgId" },
|
|
50
|
+
{ name: "createTime" },
|
|
51
|
+
{ name: "senderId" },
|
|
52
|
+
{ name: "conversationId" },
|
|
53
|
+
{ name: "content" },
|
|
54
|
+
],
|
|
55
|
+
msg_meta: [{ name: "uid" }, { name: "name" }],
|
|
56
|
+
contact_meta: [{ name: "uid" }, { name: "name" }],
|
|
57
|
+
},
|
|
58
|
+
rows: {
|
|
59
|
+
msg_table: [
|
|
60
|
+
{ msgId: "m1", createTime: 1700000000, senderId: "u1", conversationId: "c1", content: "开会通知" },
|
|
61
|
+
{ msgId: "m2", createTime: 1700000010, senderId: "u2", conversationId: "c1", content: "收到" },
|
|
62
|
+
],
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
// opaque schema — time resolves, but body is a BLOB → text null, raw kept
|
|
67
|
+
const OPAQUE_SPEC = {
|
|
68
|
+
tables: ["chat_msg"],
|
|
69
|
+
cols: { chat_msg: [{ name: "id" }, { name: "timestamp" }, { name: "body" }] },
|
|
70
|
+
rows: { chat_msg: [{ id: 7, timestamp: 1700000000, body: Buffer.from([1, 2]) }] },
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
function adapter(Cls, spec, { exists = true } = {}) {
|
|
74
|
+
const a = new Cls({ dbPath: "/fake.db" });
|
|
75
|
+
a._deps.fs = { existsSync: () => exists, accessSync: () => {}, constants: { R_OK: 4 } };
|
|
76
|
+
a._deps.dbDriverFactory = () => makeFakeDb(spec);
|
|
77
|
+
return a;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async function collect(iter) {
|
|
81
|
+
const out = [];
|
|
82
|
+
for await (const r of iter) out.push(r);
|
|
83
|
+
return out;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
describe("readLocalImDb (generic honest reader)", () => {
|
|
87
|
+
it("discovers message tables, skips metadata + sqlite_*", () => {
|
|
88
|
+
const { messages, diagnostic } = readLocalImDb("/x", { _databaseClass: makeFakeDb(SPEC) });
|
|
89
|
+
expect(diagnostic.messageTables).toEqual(["msg_table"]);
|
|
90
|
+
expect(diagnostic.skippedTables).toEqual(["msg_meta"]);
|
|
91
|
+
expect(messages).toHaveLength(2);
|
|
92
|
+
expect(messages[0].text).toBe("开会通知");
|
|
93
|
+
expect(messages[0].createdTimeMs).toBe(1700000000000);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("opaque body → text null but rawRow preserved + loud diagnostic", () => {
|
|
97
|
+
const { messages, diagnostic } = readLocalImDb("/x", { _databaseClass: makeFakeDb(OPAQUE_SPEC) });
|
|
98
|
+
expect(messages).toHaveLength(1);
|
|
99
|
+
expect(messages[0].text).toBeNull();
|
|
100
|
+
expect(messages[0].rawRow).toBeTruthy();
|
|
101
|
+
expect(diagnostic.textCount).toBe(0);
|
|
102
|
+
expect(diagnostic.messageTables).toEqual(["chat_msg"]);
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
describe.each([
|
|
107
|
+
["DingTalkPcAdapter", DingTalkPcAdapter, "dingtalk"],
|
|
108
|
+
["FeishuPcAdapter", FeishuPcAdapter, "feishu"],
|
|
109
|
+
])("%s (honest best-effort)", (_label, Cls, platform) => {
|
|
110
|
+
it("no-arg construct + DB_NOT_PULLED readiness + legalGate", async () => {
|
|
111
|
+
const a = new Cls();
|
|
112
|
+
expect(a.extractMode).toBe("device-pull");
|
|
113
|
+
expect(a.dataDisclosure.legalGate).toBe(true);
|
|
114
|
+
const r = await a.authenticate({ readinessOnly: true });
|
|
115
|
+
expect(r.reason).toBe("DB_NOT_PULLED");
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("reads messages → valid events, platform tag, raw preserved", async () => {
|
|
119
|
+
const a = adapter(Cls, SPEC);
|
|
120
|
+
const raws = await collect(a.sync({ dbPath: "/fake.db" }));
|
|
121
|
+
expect(raws).toHaveLength(2);
|
|
122
|
+
const merged = { events: [], persons: [], places: [], items: [], topics: [] };
|
|
123
|
+
for (const r of raws) {
|
|
124
|
+
const n = a.normalize(r);
|
|
125
|
+
for (const k of Object.keys(merged)) merged[k].push(...n[k]);
|
|
126
|
+
}
|
|
127
|
+
const { valid, invalidReasons } = partitionBatch(merged);
|
|
128
|
+
expect(invalidReasons).toHaveLength(0);
|
|
129
|
+
expect(valid.events).toHaveLength(2);
|
|
130
|
+
expect(valid.events[0].extra.platform).toBe(platform);
|
|
131
|
+
expect(valid.events[0].extra.textResolved).toBe(true);
|
|
132
|
+
expect(valid.events[0].extra.rawRow).toBeTruthy();
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("emits local-im-read progress diagnostic", async () => {
|
|
136
|
+
const a = adapter(Cls, SPEC);
|
|
137
|
+
const ev = [];
|
|
138
|
+
await collect(a.sync({ dbPath: "/fake.db", onProgress: (e) => ev.push(e) }));
|
|
139
|
+
const d = ev.find((e) => e.phase === "local-im-read");
|
|
140
|
+
expect(d.messageTables).toContain("msg_table");
|
|
141
|
+
expect(d.messageCount).toBe(2);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("missing db yields nothing; unknown kind throws", async () => {
|
|
145
|
+
const a = adapter(Cls, SPEC, { exists: false });
|
|
146
|
+
expect(await collect(a.sync({ dbPath: "/no.db" }))).toHaveLength(0);
|
|
147
|
+
expect(() => new Cls().normalize({ kind: "x", payload: { kind: "x" } })).toThrow(/unknown kind/);
|
|
148
|
+
});
|
|
149
|
+
});
|
|
@@ -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
|
+
});
|