@chainlesschain/personal-data-hub 0.2.0 → 0.2.2
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/ai-chat-cookie-capture-spec.test.js +211 -0
- package/__tests__/adapters/ai-chat-health-checker.test.js +262 -0
- package/__tests__/adapters/ai-chat-history.test.js +8 -7
- package/__tests__/adapters/ai-chat-vendors.test.js +149 -8
- package/__tests__/adapters/social-toutiao-kuaishou-scaffold.test.js +269 -0
- package/__tests__/adapters/system-data-android-ingest.test.js +144 -0
- package/__tests__/adapters/system-data-android.test.js +387 -0
- package/__tests__/adapters/wechat-bootstrap.test.js +240 -0
- package/__tests__/adapters/wechat-env-probe.test.js +162 -0
- package/__tests__/adapters/wechat-frida-agent.test.js +322 -0
- package/__tests__/adapters/wechat-frida-integration.test.js +149 -0
- package/__tests__/adapters/wechat-frida-key-provider.test.js +188 -0
- package/__tests__/adapters/wechat-md5-key-provider.test.js +101 -0
- package/__tests__/analysis-skills.test.js +147 -0
- package/__tests__/analysis.test.js +329 -1
- package/__tests__/e2e/ai-chat-cross-source-journey.test.js +213 -0
- package/__tests__/e2e/full-user-journey.test.js +188 -0
- package/__tests__/integration/ai-chat-history-registry.test.js +228 -0
- package/__tests__/integration/aichat-wizard-end-to-end.test.js +282 -0
- package/__tests__/integration/cross-adapter-pipelines.test.js +396 -0
- package/__tests__/integration/social-bilibili-pipeline.test.js +261 -0
- package/__tests__/integration/wechat-bootstrap-end-to-end.test.js +390 -0
- package/__tests__/registry.test.js +4 -2
- package/__tests__/social-adapters.test.js +63 -14
- package/__tests__/social-bilibili-snapshot.test.js +278 -0
- package/__tests__/wechat-adapter.test.js +118 -0
- package/lib/adapters/ai-chat-history/ai-chat-adapter.js +55 -16
- package/lib/adapters/ai-chat-history/cookie-capture-spec.js +331 -0
- package/lib/adapters/ai-chat-history/health-checker.js +210 -0
- package/lib/adapters/ai-chat-history/schema-map.js +42 -5
- package/lib/adapters/ai-chat-history/vendor-spec.js +1 -0
- package/lib/adapters/ai-chat-history/vendors/doubao.js +255 -0
- package/lib/adapters/ai-chat-history/wizard-controller.js +473 -0
- package/lib/adapters/alipay-bill/alipay-bill-adapter.js +4 -0
- package/lib/adapters/social-bilibili/adapter.js +500 -0
- package/lib/adapters/social-bilibili/index.js +21 -169
- package/lib/adapters/social-kuaishou/index.js +237 -0
- package/lib/adapters/social-toutiao/index.js +236 -0
- package/lib/adapters/system-data-android/adapter.js +348 -0
- package/lib/adapters/system-data-android/index.js +76 -0
- package/lib/adapters/wechat/bootstrap.js +146 -0
- package/lib/adapters/wechat/content-parser.js +11 -2
- package/lib/adapters/wechat/db-reader.js +88 -10
- package/lib/adapters/wechat/env-probe.js +218 -0
- package/lib/adapters/wechat/frida-agent/loader.js +74 -0
- package/lib/adapters/wechat/frida-agent/wechat-key-hook.js +248 -0
- package/lib/adapters/wechat/index.js +9 -0
- package/lib/adapters/wechat/key-providers/frida-key-provider.js +252 -0
- package/lib/adapters/wechat/key-providers/index.js +22 -0
- package/lib/adapters/wechat/key-providers/key-provider-base.js +44 -0
- package/lib/adapters/wechat/key-providers/md5-key-provider.js +81 -0
- package/lib/adapters/wechat/normalize.js +12 -3
- package/lib/analysis-skills/spending.js +4 -1
- package/lib/analysis.js +191 -2
- package/lib/index.js +16 -0
- package/lib/prompt-builder.js +11 -1
- package/lib/query-parser.js +7 -1
- package/lib/vault.js +77 -0
- package/package.json +8 -1
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
|
4
|
+
import { mkdtempSync, writeFileSync, rmSync } from "node:fs";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { tmpdir } from "node:os";
|
|
7
|
+
|
|
8
|
+
const {
|
|
9
|
+
SystemDataAndroidAdapter,
|
|
10
|
+
SYSTEM_DATA_ANDROID_NAME,
|
|
11
|
+
SYSTEM_DATA_ANDROID_VERSION,
|
|
12
|
+
SNAPSHOT_SCHEMA_VERSION,
|
|
13
|
+
} = require("../../lib/adapters/system-data-android");
|
|
14
|
+
const { assertAdapter } = require("../../lib/adapter-spec");
|
|
15
|
+
const {
|
|
16
|
+
ENTITY_TYPES,
|
|
17
|
+
PERSON_SUBTYPES,
|
|
18
|
+
ITEM_SUBTYPES,
|
|
19
|
+
CAPTURED_BY,
|
|
20
|
+
} = require("../../lib/constants");
|
|
21
|
+
const { validatePerson, validateItem } = require("../../lib/schemas");
|
|
22
|
+
|
|
23
|
+
let tmpDir;
|
|
24
|
+
let snapshotPath;
|
|
25
|
+
|
|
26
|
+
function writeSnapshot(obj) {
|
|
27
|
+
writeFileSync(snapshotPath, JSON.stringify(obj), "utf-8");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
beforeEach(() => {
|
|
31
|
+
tmpDir = mkdtempSync(join(tmpdir(), "sda-test-"));
|
|
32
|
+
snapshotPath = join(tmpDir, "snapshot.json");
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
afterEach(() => {
|
|
36
|
+
rmSync(tmpDir, { recursive: true, force: true });
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
describe("SystemDataAndroidAdapter — contract", () => {
|
|
40
|
+
it("conforms to PersonalDataAdapter contract", () => {
|
|
41
|
+
const adapter = new SystemDataAndroidAdapter();
|
|
42
|
+
const r = assertAdapter(adapter);
|
|
43
|
+
expect(r).toEqual({ ok: true });
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("exposes stable name + version + capabilities", () => {
|
|
47
|
+
const adapter = new SystemDataAndroidAdapter();
|
|
48
|
+
expect(adapter.name).toBe(SYSTEM_DATA_ANDROID_NAME);
|
|
49
|
+
expect(adapter.version).toBe(SYSTEM_DATA_ANDROID_VERSION);
|
|
50
|
+
expect(adapter.extractMode).toBe("device-pull");
|
|
51
|
+
expect(adapter.dataDisclosure.sensitivity).toBe("medium");
|
|
52
|
+
expect(adapter.dataDisclosure.legalGate).toBe(false);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
describe("SystemDataAndroidAdapter.authenticate", () => {
|
|
57
|
+
it("fails when inputPath missing", async () => {
|
|
58
|
+
const adapter = new SystemDataAndroidAdapter();
|
|
59
|
+
const r = await adapter.authenticate({});
|
|
60
|
+
expect(r.ok).toBe(false);
|
|
61
|
+
expect(r.reason).toBe("INPUT_PATH_REQUIRED");
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("fails when inputPath does not exist", async () => {
|
|
65
|
+
const adapter = new SystemDataAndroidAdapter();
|
|
66
|
+
const r = await adapter.authenticate({ inputPath: join(tmpDir, "does-not-exist.json") });
|
|
67
|
+
expect(r.ok).toBe(false);
|
|
68
|
+
expect(r.reason).toBe("INPUT_PATH_UNREADABLE");
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("succeeds when inputPath is readable", async () => {
|
|
72
|
+
writeSnapshot({ schemaVersion: SNAPSHOT_SCHEMA_VERSION, contacts: [], apps: [] });
|
|
73
|
+
const adapter = new SystemDataAndroidAdapter();
|
|
74
|
+
const r = await adapter.authenticate({ inputPath: snapshotPath });
|
|
75
|
+
expect(r).toEqual({ ok: true, mode: "snapshot-file" });
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
describe("SystemDataAndroidAdapter.sync + normalize", () => {
|
|
80
|
+
it("rejects mismatched snapshot schemaVersion", async () => {
|
|
81
|
+
writeSnapshot({ schemaVersion: 99, contacts: [], apps: [] });
|
|
82
|
+
const adapter = new SystemDataAndroidAdapter();
|
|
83
|
+
await expect(async () => {
|
|
84
|
+
// eslint-disable-next-line no-unused-vars
|
|
85
|
+
for await (const _ of adapter.sync({ inputPath: snapshotPath })) {
|
|
86
|
+
// drain
|
|
87
|
+
}
|
|
88
|
+
}).rejects.toThrow(/schemaVersion mismatch/);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("yields contact + app raws and normalizes each into a valid entity", async () => {
|
|
92
|
+
writeSnapshot({
|
|
93
|
+
schemaVersion: SNAPSHOT_SCHEMA_VERSION,
|
|
94
|
+
snapshottedAt: 1_700_000_000_000,
|
|
95
|
+
contacts: [
|
|
96
|
+
{
|
|
97
|
+
lookupKey: "0r1-3A2B",
|
|
98
|
+
displayName: "妈妈",
|
|
99
|
+
phones: ["+8613800138000"],
|
|
100
|
+
emails: [],
|
|
101
|
+
starred: true,
|
|
102
|
+
organization: "家庭",
|
|
103
|
+
},
|
|
104
|
+
],
|
|
105
|
+
apps: [
|
|
106
|
+
{
|
|
107
|
+
packageName: "com.tencent.mm",
|
|
108
|
+
label: "微信",
|
|
109
|
+
versionName: "8.0.45",
|
|
110
|
+
versionCode: 2200,
|
|
111
|
+
firstInstallTime: 1_650_000_000_000,
|
|
112
|
+
lastUpdateTime: 1_700_000_000_000,
|
|
113
|
+
isSystem: false,
|
|
114
|
+
},
|
|
115
|
+
],
|
|
116
|
+
});
|
|
117
|
+
const adapter = new SystemDataAndroidAdapter();
|
|
118
|
+
|
|
119
|
+
// Every yielded raw must carry originalId so registry.putRawEvent's NOT
|
|
120
|
+
// NULL constraint doesn't surface as invalidCount=rawCount (real-device
|
|
121
|
+
// bug 2026-05-21). Capture all raws to assert structure first.
|
|
122
|
+
const rawsSeen = [];
|
|
123
|
+
for await (const r of adapter.sync({ inputPath: snapshotPath })) {
|
|
124
|
+
rawsSeen.push(r);
|
|
125
|
+
}
|
|
126
|
+
expect(rawsSeen[0].originalId).toBe("android-contact:0r1-3A2B");
|
|
127
|
+
expect(rawsSeen[1].originalId).toBe("android-app:com.tencent.mm");
|
|
128
|
+
|
|
129
|
+
const persons = [];
|
|
130
|
+
const items = [];
|
|
131
|
+
for await (const raw of adapter.sync({ inputPath: snapshotPath })) {
|
|
132
|
+
const batch = adapter.normalize(raw);
|
|
133
|
+
persons.push(...batch.persons);
|
|
134
|
+
items.push(...batch.items);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
expect(persons).toHaveLength(1);
|
|
138
|
+
const p = persons[0];
|
|
139
|
+
expect(p.id).toBe("person-android-0r1-3A2B");
|
|
140
|
+
expect(p.type).toBe(ENTITY_TYPES.PERSON);
|
|
141
|
+
expect(p.subtype).toBe(PERSON_SUBTYPES.CONTACT);
|
|
142
|
+
expect(p.names).toEqual(["妈妈"]);
|
|
143
|
+
expect(p.identifiers).toEqual({ phone: ["+8613800138000"] });
|
|
144
|
+
expect(p.extra).toEqual({ starred: true });
|
|
145
|
+
expect(p.relation).toBe("家庭");
|
|
146
|
+
expect(p.source.adapter).toBe(SYSTEM_DATA_ANDROID_NAME);
|
|
147
|
+
expect(p.source.capturedBy).toBe(CAPTURED_BY.API);
|
|
148
|
+
expect(p.source.capturedAt).toBe(1_700_000_000_000);
|
|
149
|
+
const pv = validatePerson(p);
|
|
150
|
+
expect(pv).toEqual({ valid: true, errors: [] });
|
|
151
|
+
|
|
152
|
+
expect(items).toHaveLength(1);
|
|
153
|
+
const i = items[0];
|
|
154
|
+
expect(i.id).toBe("item-android-app-com.tencent.mm");
|
|
155
|
+
expect(i.type).toBe(ENTITY_TYPES.ITEM);
|
|
156
|
+
expect(i.subtype).toBe(ITEM_SUBTYPES.OTHER);
|
|
157
|
+
expect(i.name).toBe("微信");
|
|
158
|
+
expect(i.category).toBe("user-app");
|
|
159
|
+
expect(i.extra.kind).toBe("installed_app");
|
|
160
|
+
expect(i.extra.packageName).toBe("com.tencent.mm");
|
|
161
|
+
expect(i.extra.versionCode).toBe(2200);
|
|
162
|
+
expect(i.extra.firstInstallTime).toBe(1_650_000_000_000);
|
|
163
|
+
expect(i.extra.isSystem).toBe(false);
|
|
164
|
+
const iv = validateItem(i);
|
|
165
|
+
expect(iv).toEqual({ valid: true, errors: [] });
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("dedupes by stable lookupKey — same key across syncs returns same id", async () => {
|
|
169
|
+
writeSnapshot({
|
|
170
|
+
schemaVersion: SNAPSHOT_SCHEMA_VERSION,
|
|
171
|
+
contacts: [{ lookupKey: "abc", displayName: "Alice", phones: [], emails: [] }],
|
|
172
|
+
apps: [],
|
|
173
|
+
});
|
|
174
|
+
const adapter = new SystemDataAndroidAdapter();
|
|
175
|
+
const ids = [];
|
|
176
|
+
for (let pass = 0; pass < 2; pass++) {
|
|
177
|
+
for await (const raw of adapter.sync({ inputPath: snapshotPath })) {
|
|
178
|
+
const batch = adapter.normalize(raw);
|
|
179
|
+
if (batch.persons[0]) ids.push(batch.persons[0].id);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
expect(ids).toEqual(["person-android-abc", "person-android-abc"]);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it("falls back to displayName when lookupKey missing", async () => {
|
|
186
|
+
writeSnapshot({
|
|
187
|
+
schemaVersion: SNAPSHOT_SCHEMA_VERSION,
|
|
188
|
+
contacts: [{ displayName: "Bob", phones: [], emails: [] }],
|
|
189
|
+
apps: [],
|
|
190
|
+
});
|
|
191
|
+
const adapter = new SystemDataAndroidAdapter();
|
|
192
|
+
let id;
|
|
193
|
+
for await (const raw of adapter.sync({ inputPath: snapshotPath })) {
|
|
194
|
+
id = adapter.normalize(raw).persons[0]?.id;
|
|
195
|
+
}
|
|
196
|
+
expect(id).toBe("person-android-Bob");
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("honors include filter — apps only", async () => {
|
|
200
|
+
writeSnapshot({
|
|
201
|
+
schemaVersion: SNAPSHOT_SCHEMA_VERSION,
|
|
202
|
+
contacts: [{ lookupKey: "k", displayName: "X", phones: [], emails: [] }],
|
|
203
|
+
apps: [
|
|
204
|
+
{ packageName: "com.x", label: "X", versionName: "1", versionCode: 1, isSystem: false },
|
|
205
|
+
],
|
|
206
|
+
});
|
|
207
|
+
const adapter = new SystemDataAndroidAdapter();
|
|
208
|
+
const kinds = [];
|
|
209
|
+
for await (const raw of adapter.sync({
|
|
210
|
+
inputPath: snapshotPath,
|
|
211
|
+
include: { contacts: false, apps: true },
|
|
212
|
+
})) {
|
|
213
|
+
kinds.push(raw.kind);
|
|
214
|
+
}
|
|
215
|
+
expect(kinds).toEqual(["app"]);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it("honors limit option", async () => {
|
|
219
|
+
writeSnapshot({
|
|
220
|
+
schemaVersion: SNAPSHOT_SCHEMA_VERSION,
|
|
221
|
+
contacts: [
|
|
222
|
+
{ lookupKey: "a", displayName: "A", phones: [], emails: [] },
|
|
223
|
+
{ lookupKey: "b", displayName: "B", phones: [], emails: [] },
|
|
224
|
+
{ lookupKey: "c", displayName: "C", phones: [], emails: [] },
|
|
225
|
+
],
|
|
226
|
+
apps: [],
|
|
227
|
+
});
|
|
228
|
+
const adapter = new SystemDataAndroidAdapter();
|
|
229
|
+
let count = 0;
|
|
230
|
+
for await (const _raw of adapter.sync({ inputPath: snapshotPath, limit: 2 })) {
|
|
231
|
+
count += 1;
|
|
232
|
+
}
|
|
233
|
+
expect(count).toBe(2);
|
|
234
|
+
});
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
describe("SystemDataAndroidAdapter — degenerate snapshots", () => {
|
|
238
|
+
it("anonymous contact gets placeholder name", () => {
|
|
239
|
+
const adapter = new SystemDataAndroidAdapter();
|
|
240
|
+
const batch = adapter.normalize({
|
|
241
|
+
kind: "contact",
|
|
242
|
+
capturedAt: 1_700_000_000_000,
|
|
243
|
+
payload: { lookupKey: "x" },
|
|
244
|
+
});
|
|
245
|
+
expect(batch.persons[0].names).toEqual(["(无名联系人)"]);
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it("system app gets category 'system-app'", () => {
|
|
249
|
+
const adapter = new SystemDataAndroidAdapter();
|
|
250
|
+
const batch = adapter.normalize({
|
|
251
|
+
kind: "app",
|
|
252
|
+
capturedAt: 1_700_000_000_000,
|
|
253
|
+
payload: { packageName: "com.android.settings", label: "Settings", isSystem: true },
|
|
254
|
+
});
|
|
255
|
+
expect(batch.items[0].category).toBe("system-app");
|
|
256
|
+
expect(batch.items[0].extra.isSystem).toBe(true);
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it("throws on unknown raw.kind", () => {
|
|
260
|
+
const adapter = new SystemDataAndroidAdapter();
|
|
261
|
+
expect(() =>
|
|
262
|
+
adapter.normalize({ kind: "mystery", capturedAt: 1, payload: {} })
|
|
263
|
+
).toThrow(/unknown raw.kind/);
|
|
264
|
+
});
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
// ─── Bridge-direct mode (A8 follow-up) ────────────────────────────────
|
|
268
|
+
//
|
|
269
|
+
// When `cc-android-bridge` is loaded inside in-APK cc, the adapter can pull
|
|
270
|
+
// directly from ContentResolver / PackageManager instead of waiting for the
|
|
271
|
+
// UI to write a snapshot file. These tests inject a mock bridge to exercise
|
|
272
|
+
// the new path without a real device.
|
|
273
|
+
|
|
274
|
+
describe("SystemDataAndroidAdapter — bridge-direct sync", () => {
|
|
275
|
+
function makeBridge({ contacts = [], apps = [], throws = null } = {}) {
|
|
276
|
+
return {
|
|
277
|
+
caps: () => ({ available: true, reason: "test" }),
|
|
278
|
+
invoke: async (method) => {
|
|
279
|
+
if (throws) throw throws;
|
|
280
|
+
if (method === "contacts.query") return { contacts };
|
|
281
|
+
if (method === "app.list") return { apps };
|
|
282
|
+
throw new Error("unexpected method " + method);
|
|
283
|
+
},
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
it("useBridge=true pulls contacts and apps via bridge.invoke", async () => {
|
|
288
|
+
const adapter = new SystemDataAndroidAdapter();
|
|
289
|
+
const bridge = makeBridge({
|
|
290
|
+
contacts: [
|
|
291
|
+
{ lookupKey: "ck-1", displayName: "妈妈", phones: ["13800000001"] },
|
|
292
|
+
{ lookupKey: "ck-2", displayName: "同事" },
|
|
293
|
+
],
|
|
294
|
+
apps: [
|
|
295
|
+
{ packageName: "com.tencent.mm", label: "微信", versionName: "8.0", isSystem: false },
|
|
296
|
+
],
|
|
297
|
+
});
|
|
298
|
+
adapter._deps.bridgeProvider = () => bridge;
|
|
299
|
+
|
|
300
|
+
const out = [];
|
|
301
|
+
for await (const r of adapter.sync({ useBridge: true })) out.push(r);
|
|
302
|
+
expect(out).toHaveLength(3);
|
|
303
|
+
expect(out[0].kind).toBe("contact");
|
|
304
|
+
expect(out[0].payload.displayName).toBe("妈妈");
|
|
305
|
+
expect(out[2].kind).toBe("app");
|
|
306
|
+
expect(out[2].payload.packageName).toBe("com.tencent.mm");
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
it("auto-engages bridge when inputPath omitted AND bridge.available", async () => {
|
|
310
|
+
const adapter = new SystemDataAndroidAdapter();
|
|
311
|
+
adapter._deps.bridgeProvider = () => makeBridge({
|
|
312
|
+
contacts: [{ lookupKey: "auto", displayName: "Auto" }],
|
|
313
|
+
});
|
|
314
|
+
const out = [];
|
|
315
|
+
for await (const r of adapter.sync({})) out.push(r);
|
|
316
|
+
expect(out).toHaveLength(1);
|
|
317
|
+
expect(out[0].payload.displayName).toBe("Auto");
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
it("respects include.contacts=false in bridge mode", async () => {
|
|
321
|
+
const adapter = new SystemDataAndroidAdapter();
|
|
322
|
+
adapter._deps.bridgeProvider = () => makeBridge({
|
|
323
|
+
contacts: [{ displayName: "should be skipped" }],
|
|
324
|
+
apps: [{ packageName: "com.kept" }],
|
|
325
|
+
});
|
|
326
|
+
const out = [];
|
|
327
|
+
for await (const r of adapter.sync({ useBridge: true, include: { contacts: false } })) {
|
|
328
|
+
out.push(r);
|
|
329
|
+
}
|
|
330
|
+
expect(out).toHaveLength(1);
|
|
331
|
+
expect(out[0].kind).toBe("app");
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
it("limit caps bridge yields", async () => {
|
|
335
|
+
const adapter = new SystemDataAndroidAdapter();
|
|
336
|
+
const many = Array.from({ length: 10 }, (_, i) => ({ displayName: `c${i}` }));
|
|
337
|
+
adapter._deps.bridgeProvider = () => makeBridge({ contacts: many });
|
|
338
|
+
const out = [];
|
|
339
|
+
for await (const r of adapter.sync({ useBridge: true, limit: 3 })) out.push(r);
|
|
340
|
+
expect(out).toHaveLength(3);
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
it("throws when useBridge=true but bridge missing", async () => {
|
|
344
|
+
const adapter = new SystemDataAndroidAdapter();
|
|
345
|
+
adapter._deps.bridgeProvider = () => null;
|
|
346
|
+
const it = adapter.sync({ useBridge: true });
|
|
347
|
+
await expect(it.next()).rejects.toThrow(/cc-android-bridge is not loaded/);
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
it("propagates bridge.invoke errors", async () => {
|
|
351
|
+
const adapter = new SystemDataAndroidAdapter();
|
|
352
|
+
adapter._deps.bridgeProvider = () =>
|
|
353
|
+
makeBridge({ throws: new Error("ANDROID_BRIDGE_NOT_AVAILABLE: not-on-android") });
|
|
354
|
+
const it = adapter.sync({ useBridge: true });
|
|
355
|
+
await expect(it.next()).rejects.toThrow(/ANDROID_BRIDGE_NOT_AVAILABLE/);
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
it("accepts bare array response from bridge (contacts as Array)", async () => {
|
|
359
|
+
const adapter = new SystemDataAndroidAdapter();
|
|
360
|
+
adapter._deps.bridgeProvider = () => ({
|
|
361
|
+
caps: () => ({ available: true }),
|
|
362
|
+
invoke: async (method) => {
|
|
363
|
+
if (method === "contacts.query") return [{ displayName: "Bare" }];
|
|
364
|
+
if (method === "app.list") return [];
|
|
365
|
+
return [];
|
|
366
|
+
},
|
|
367
|
+
});
|
|
368
|
+
const out = [];
|
|
369
|
+
for await (const r of adapter.sync({ useBridge: true })) out.push(r);
|
|
370
|
+
expect(out).toHaveLength(1);
|
|
371
|
+
expect(out[0].payload.displayName).toBe("Bare");
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
it("inputPath snapshot mode still works when bridgeProvider returns null", async () => {
|
|
375
|
+
writeSnapshot({
|
|
376
|
+
schemaVersion: SNAPSHOT_SCHEMA_VERSION,
|
|
377
|
+
snapshottedAt: 1_700_000_000_000,
|
|
378
|
+
contacts: [{ lookupKey: "snap", displayName: "From Snapshot" }],
|
|
379
|
+
apps: [],
|
|
380
|
+
});
|
|
381
|
+
const adapter = new SystemDataAndroidAdapter();
|
|
382
|
+
const out = [];
|
|
383
|
+
for await (const r of adapter.sync({ inputPath: snapshotPath })) out.push(r);
|
|
384
|
+
expect(out).toHaveLength(1);
|
|
385
|
+
expect(out[0].payload.displayName).toBe("From Snapshot");
|
|
386
|
+
});
|
|
387
|
+
});
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Phase 12.6.7 — bootstrapWechatAdapter unit tests.
|
|
3
|
+
*
|
|
4
|
+
* Validates the decision matrix that ties env-probe → KeyProvider →
|
|
5
|
+
* WechatAdapter. All exec calls are stubbed via `_probe` injection so
|
|
6
|
+
* tests stay fully hermetic (no adb, no Frida binding, no real device).
|
|
7
|
+
*/
|
|
8
|
+
"use strict";
|
|
9
|
+
|
|
10
|
+
import { describe, it, expect } from "vitest";
|
|
11
|
+
import path from "node:path";
|
|
12
|
+
import fs from "node:fs";
|
|
13
|
+
import os from "node:os";
|
|
14
|
+
import wechatModule from "../../lib/adapters/wechat/bootstrap.js";
|
|
15
|
+
const { bootstrapWechatAdapter } = wechatModule;
|
|
16
|
+
|
|
17
|
+
function mkProbe(overrides = {}) {
|
|
18
|
+
const base = {
|
|
19
|
+
ok: true,
|
|
20
|
+
suggestedKeyProvider: "md5",
|
|
21
|
+
reasons: ["WeChat 7.0.22 (< 8.0) — legacy MD5(IMEI+UIN) path supported"],
|
|
22
|
+
device: { reachable: true, serial: "SERIAL123", abi: "arm64-v8a" },
|
|
23
|
+
root: { detected: false, magiskInstalled: false },
|
|
24
|
+
frida: { serverRunning: false, port: null },
|
|
25
|
+
wechat: { installed: true, versionName: "7.0.22", majorVersion: 7 },
|
|
26
|
+
warnings: [],
|
|
27
|
+
};
|
|
28
|
+
return { ...base, ...overrides };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function mkWechatDataPath() {
|
|
32
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "pdh-wechat-bootstrap-"));
|
|
33
|
+
return dir;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
describe("bootstrapWechatAdapter — input validation", () => {
|
|
37
|
+
it("throws when account.uin missing (including bare opts)", async () => {
|
|
38
|
+
await expect(bootstrapWechatAdapter()).rejects.toThrow(/account\.uin/);
|
|
39
|
+
await expect(bootstrapWechatAdapter({})).rejects.toThrow(/account\.uin/);
|
|
40
|
+
await expect(
|
|
41
|
+
bootstrapWechatAdapter({ account: {} }),
|
|
42
|
+
).rejects.toThrow(/account\.uin/);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
describe("bootstrapWechatAdapter — md5 path", () => {
|
|
47
|
+
it("returns ok with MD5KeyProvider when probe suggests md5 + wechatDataPath provided", async () => {
|
|
48
|
+
const wechatDataPath = mkWechatDataPath();
|
|
49
|
+
const r = await bootstrapWechatAdapter({
|
|
50
|
+
account: { uin: "1234567890" },
|
|
51
|
+
wechatDataPath,
|
|
52
|
+
_probe: mkProbe(),
|
|
53
|
+
});
|
|
54
|
+
expect(r.ok).toBe(true);
|
|
55
|
+
expect(r.adapter).toBeDefined();
|
|
56
|
+
expect(r.adapter.account.uin).toBe("1234567890");
|
|
57
|
+
expect(r.keyProvider).toBeDefined();
|
|
58
|
+
expect(r.keyProvider.name).toBe("md5");
|
|
59
|
+
expect(r.probe.suggestedKeyProvider).toBe("md5");
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("returns MD5_NEEDS_WECHAT_DATA_PATH when md5 chosen but wechatDataPath missing", async () => {
|
|
63
|
+
const r = await bootstrapWechatAdapter({
|
|
64
|
+
account: { uin: "1234567890" },
|
|
65
|
+
_probe: mkProbe(),
|
|
66
|
+
});
|
|
67
|
+
expect(r.ok).toBe(false);
|
|
68
|
+
expect(r.reason).toBe("MD5_NEEDS_WECHAT_DATA_PATH");
|
|
69
|
+
expect(r.probe).toBeDefined();
|
|
70
|
+
expect(r.message).toMatch(/wechatDataPath/);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("propagates dbPath to the adapter constructor", async () => {
|
|
74
|
+
const wechatDataPath = mkWechatDataPath();
|
|
75
|
+
const dbPath = path.join(wechatDataPath, "MicroMsg.db");
|
|
76
|
+
fs.writeFileSync(dbPath, "stub", "utf-8");
|
|
77
|
+
const r = await bootstrapWechatAdapter({
|
|
78
|
+
account: { uin: "1234567890" },
|
|
79
|
+
dbPath,
|
|
80
|
+
wechatDataPath,
|
|
81
|
+
_probe: mkProbe(),
|
|
82
|
+
});
|
|
83
|
+
expect(r.ok).toBe(true);
|
|
84
|
+
expect(r.adapter._dbPath).toBe(dbPath);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
describe("bootstrapWechatAdapter — frida path", () => {
|
|
89
|
+
it("returns ok with FridaKeyProvider when probe suggests frida", async () => {
|
|
90
|
+
const r = await bootstrapWechatAdapter({
|
|
91
|
+
account: { uin: "wxid_abcdef" },
|
|
92
|
+
_probe: mkProbe({
|
|
93
|
+
suggestedKeyProvider: "frida",
|
|
94
|
+
wechat: { installed: true, versionName: "8.0.50", majorVersion: 8 },
|
|
95
|
+
root: { detected: true, magiskInstalled: true },
|
|
96
|
+
frida: { serverRunning: true, port: 27042 },
|
|
97
|
+
}),
|
|
98
|
+
});
|
|
99
|
+
expect(r.ok).toBe(true);
|
|
100
|
+
expect(r.keyProvider).toBeDefined();
|
|
101
|
+
expect(r.keyProvider.name).toBe("frida");
|
|
102
|
+
expect(r.adapter).toBeDefined();
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("forwards fridaOpts.deviceId / packageName / timeoutMs", async () => {
|
|
106
|
+
const r = await bootstrapWechatAdapter({
|
|
107
|
+
account: { uin: "wxid_abcdef" },
|
|
108
|
+
fridaOpts: {
|
|
109
|
+
deviceId: "EMULATOR_ID",
|
|
110
|
+
packageName: "com.tencent.mm.beta",
|
|
111
|
+
timeoutMs: 12_345,
|
|
112
|
+
},
|
|
113
|
+
_probe: mkProbe({
|
|
114
|
+
suggestedKeyProvider: "frida",
|
|
115
|
+
wechat: { installed: true, versionName: "8.0.50", majorVersion: 8 },
|
|
116
|
+
root: { detected: true, magiskInstalled: true },
|
|
117
|
+
frida: { serverRunning: true, port: 27042 },
|
|
118
|
+
}),
|
|
119
|
+
});
|
|
120
|
+
expect(r.ok).toBe(true);
|
|
121
|
+
expect(r.keyProvider._deviceId).toBe("EMULATOR_ID");
|
|
122
|
+
expect(r.keyProvider._packageName).toBe("com.tencent.mm.beta");
|
|
123
|
+
expect(r.keyProvider._timeoutMs).toBe(12_345);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("falls back to probe.device.serial when fridaOpts.deviceId not provided", async () => {
|
|
127
|
+
const r = await bootstrapWechatAdapter({
|
|
128
|
+
account: { uin: "wxid_abcdef" },
|
|
129
|
+
_probe: mkProbe({
|
|
130
|
+
suggestedKeyProvider: "frida",
|
|
131
|
+
device: { reachable: true, serial: "FROM_PROBE", abi: "arm64-v8a" },
|
|
132
|
+
wechat: { installed: true, versionName: "8.0.50", majorVersion: 8 },
|
|
133
|
+
root: { detected: true, magiskInstalled: true },
|
|
134
|
+
frida: { serverRunning: true, port: 27042 },
|
|
135
|
+
}),
|
|
136
|
+
});
|
|
137
|
+
expect(r.ok).toBe(true);
|
|
138
|
+
expect(r.keyProvider._deviceId).toBe("FROM_PROBE");
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
describe("bootstrapWechatAdapter — unsupported / unknown", () => {
|
|
143
|
+
it("returns ENV_UNSUPPORTED with probe reasons when nothing viable", async () => {
|
|
144
|
+
const r = await bootstrapWechatAdapter({
|
|
145
|
+
account: { uin: "1234567890" },
|
|
146
|
+
_probe: mkProbe({
|
|
147
|
+
ok: false,
|
|
148
|
+
suggestedKeyProvider: "unsupported",
|
|
149
|
+
reasons: ["WeChat 8.0.50 requires root for SQLCipher key extraction"],
|
|
150
|
+
}),
|
|
151
|
+
});
|
|
152
|
+
expect(r.ok).toBe(false);
|
|
153
|
+
expect(r.reason).toBe("ENV_UNSUPPORTED");
|
|
154
|
+
expect(r.message).toMatch(/root/);
|
|
155
|
+
expect(r.probe).toBeDefined();
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it("returns UNKNOWN_KEY_PROVIDER when override is not md5/frida", async () => {
|
|
159
|
+
const r = await bootstrapWechatAdapter({
|
|
160
|
+
account: { uin: "1234567890" },
|
|
161
|
+
keyProviderOverride: "magic-pony",
|
|
162
|
+
_probe: mkProbe(),
|
|
163
|
+
});
|
|
164
|
+
expect(r.ok).toBe(false);
|
|
165
|
+
expect(r.reason).toBe("UNKNOWN_KEY_PROVIDER");
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
describe("bootstrapWechatAdapter — override", () => {
|
|
170
|
+
it("keyProviderOverride='frida' picks frida even when probe suggests md5", async () => {
|
|
171
|
+
const r = await bootstrapWechatAdapter({
|
|
172
|
+
account: { uin: "wxid_force" },
|
|
173
|
+
keyProviderOverride: "frida",
|
|
174
|
+
_probe: mkProbe(), // suggests "md5"
|
|
175
|
+
});
|
|
176
|
+
expect(r.ok).toBe(true);
|
|
177
|
+
expect(r.keyProvider.name).toBe("frida");
|
|
178
|
+
expect(r.probe.suggestedKeyProvider).toBe("md5"); // probe transparent
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("keyProviderOverride='md5' picks md5 even when probe suggests frida", async () => {
|
|
182
|
+
const wechatDataPath = mkWechatDataPath();
|
|
183
|
+
const r = await bootstrapWechatAdapter({
|
|
184
|
+
account: { uin: "1234567890" },
|
|
185
|
+
wechatDataPath,
|
|
186
|
+
keyProviderOverride: "md5",
|
|
187
|
+
_probe: mkProbe({
|
|
188
|
+
suggestedKeyProvider: "frida",
|
|
189
|
+
wechat: { installed: true, versionName: "8.0.50", majorVersion: 8 },
|
|
190
|
+
root: { detected: true, magiskInstalled: true },
|
|
191
|
+
frida: { serverRunning: true, port: 27042 },
|
|
192
|
+
}),
|
|
193
|
+
});
|
|
194
|
+
expect(r.ok).toBe(true);
|
|
195
|
+
expect(r.keyProvider.name).toBe("md5");
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
describe("bootstrapWechatAdapter — adapter ctor errors", () => {
|
|
200
|
+
it("returns ADAPTER_CTOR_FAILED when WechatAdapter throws", async () => {
|
|
201
|
+
const wechatDataPath = mkWechatDataPath();
|
|
202
|
+
function Boom() {
|
|
203
|
+
throw new Error("ctor boom");
|
|
204
|
+
}
|
|
205
|
+
const r = await bootstrapWechatAdapter({
|
|
206
|
+
account: { uin: "1234567890" },
|
|
207
|
+
wechatDataPath,
|
|
208
|
+
_probe: mkProbe(),
|
|
209
|
+
_WechatAdapter: Boom,
|
|
210
|
+
});
|
|
211
|
+
expect(r.ok).toBe(false);
|
|
212
|
+
expect(r.reason).toBe("ADAPTER_CTOR_FAILED");
|
|
213
|
+
expect(r.message).toBe("ctor boom");
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
describe("bootstrapWechatAdapter — test seams", () => {
|
|
218
|
+
it("uses injected _md5Provider verbatim", async () => {
|
|
219
|
+
const stubProvider = { name: "md5", getKey: async () => "deadbeef" };
|
|
220
|
+
const r = await bootstrapWechatAdapter({
|
|
221
|
+
account: { uin: "1234567890" },
|
|
222
|
+
_probe: mkProbe(),
|
|
223
|
+
_md5Provider: stubProvider,
|
|
224
|
+
});
|
|
225
|
+
expect(r.ok).toBe(true);
|
|
226
|
+
expect(r.keyProvider).toBe(stubProvider);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it("uses injected _fridaProvider verbatim", async () => {
|
|
230
|
+
const stubProvider = { name: "frida", getKey: async () => "feedface".repeat(8) };
|
|
231
|
+
const r = await bootstrapWechatAdapter({
|
|
232
|
+
account: { uin: "wxid_abc" },
|
|
233
|
+
keyProviderOverride: "frida",
|
|
234
|
+
_probe: mkProbe(),
|
|
235
|
+
_fridaProvider: stubProvider,
|
|
236
|
+
});
|
|
237
|
+
expect(r.ok).toBe(true);
|
|
238
|
+
expect(r.keyProvider).toBe(stubProvider);
|
|
239
|
+
});
|
|
240
|
+
});
|