@chainlesschain/personal-data-hub 0.1.0 → 0.2.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/__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 +396 -0
- package/__tests__/adapters/ai-chat-http-client.test.js +242 -0
- package/__tests__/adapters/ai-chat-vendors.test.js +874 -0
- package/__tests__/adapters/alipay-bill-adapter.test.js +538 -0
- package/__tests__/adapters/email-adapter.test.js +138 -1
- package/__tests__/adapters/email-classifier.test.js +347 -0
- package/__tests__/adapters/email-pdf-extractor.test.js +529 -0
- package/__tests__/adapters/email-retry-progress.test.js +294 -0
- package/__tests__/adapters/email-templates.test.js +699 -0
- package/__tests__/adapters/social-toutiao-kuaishou-scaffold.test.js +269 -0
- package/__tests__/adapters/system-data-adapter.test.js +440 -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/system-data-disclosure.test.js +153 -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 +191 -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 +556 -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__/entity-resolver-ingest-hook.test.js +177 -0
- package/__tests__/entity-resolver-stages.test.js +411 -0
- package/__tests__/entity-resolver-vault.test.js +246 -0
- package/__tests__/entity-resolver.test.js +526 -0
- package/__tests__/fixtures/entity-resolver-200-mock.json +96 -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/wechat-bootstrap-end-to-end.test.js +390 -0
- package/__tests__/longtail-adapters.test.js +217 -0
- package/__tests__/mobile-extractor.test.js +288 -0
- package/__tests__/registry.test.js +4 -2
- package/__tests__/shopping-adapters.test.js +296 -0
- package/__tests__/sidecar-contacts-cross-validate.test.js +163 -0
- package/__tests__/sidecar-supervisor.test.js +120 -0
- package/__tests__/social-adapters.test.js +206 -0
- package/__tests__/travel-adapters.test.js +325 -0
- package/__tests__/vault.test.js +3 -3
- package/__tests__/wechat-adapter.test.js +476 -0
- package/__tests__/whatsapp-adapter.test.js +135 -0
- package/lib/adapter-spec.js +12 -0
- package/lib/adapters/_python-sidecar-base.js +207 -0
- package/lib/adapters/ai-chat-history/ai-chat-adapter.js +374 -0
- package/lib/adapters/ai-chat-history/cookie-auth.js +109 -0
- 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/http-client.js +211 -0
- package/lib/adapters/ai-chat-history/index.js +28 -0
- package/lib/adapters/ai-chat-history/schema-map.js +258 -0
- package/lib/adapters/ai-chat-history/vendor-spec.js +86 -0
- package/lib/adapters/ai-chat-history/vendors/coze.js +179 -0
- package/lib/adapters/ai-chat-history/vendors/deepseek.js +199 -0
- package/lib/adapters/ai-chat-history/vendors/doubao.js +255 -0
- package/lib/adapters/ai-chat-history/vendors/dreamina.js +174 -0
- package/lib/adapters/ai-chat-history/vendors/hunyuan.js +176 -0
- package/lib/adapters/ai-chat-history/vendors/kimi.js +182 -0
- package/lib/adapters/ai-chat-history/vendors/qianfan.js +160 -0
- package/lib/adapters/ai-chat-history/vendors/tongyi.js +193 -0
- package/lib/adapters/ai-chat-history/vendors/zhipu.js +202 -0
- package/lib/adapters/ai-chat-history/wizard-controller.js +473 -0
- package/lib/adapters/alipay-bill/alipay-bill-adapter.js +311 -0
- package/lib/adapters/alipay-bill/counterparty.js +129 -0
- package/lib/adapters/alipay-bill/csv-parser.js +217 -0
- package/lib/adapters/alipay-bill/index.js +41 -0
- package/lib/adapters/alipay-bill/zip-decryptor.js +111 -0
- package/lib/adapters/email-imap/classifier.js +495 -0
- package/lib/adapters/email-imap/email-adapter.js +419 -8
- package/lib/adapters/email-imap/index.js +42 -0
- package/lib/adapters/email-imap/pdf-extractor.js +192 -0
- package/lib/adapters/email-imap/templates/bill.js +232 -0
- package/lib/adapters/email-imap/templates/government.js +120 -0
- package/lib/adapters/email-imap/templates/index.js +78 -0
- package/lib/adapters/email-imap/templates/order.js +186 -0
- package/lib/adapters/email-imap/templates/other.js +114 -0
- package/lib/adapters/email-imap/templates/register.js +113 -0
- package/lib/adapters/email-imap/templates/travel.js +157 -0
- package/lib/adapters/email-imap/templates/utils.js +275 -0
- package/lib/adapters/email-imap/transactions.js +234 -0
- package/lib/adapters/messaging-qq/index.js +158 -0
- package/lib/adapters/messaging-telegram/index.js +142 -0
- package/lib/adapters/messaging-whatsapp/index.js +189 -0
- package/lib/adapters/shopping-base/index.js +208 -0
- package/lib/adapters/shopping-jd/index.js +150 -0
- package/lib/adapters/shopping-meituan/index.js +154 -0
- package/lib/adapters/shopping-taobao/index.js +176 -0
- package/lib/adapters/social-bilibili/index.js +171 -0
- package/lib/adapters/social-douyin/index.js +116 -0
- package/lib/adapters/social-kuaishou/index.js +237 -0
- package/lib/adapters/social-toutiao/index.js +236 -0
- package/lib/adapters/social-weibo/index.js +164 -0
- package/lib/adapters/social-xiaohongshu/index.js +96 -0
- package/lib/adapters/system-data/disclosure.js +166 -0
- package/lib/adapters/system-data/index.js +34 -0
- package/lib/adapters/system-data/system-data-adapter.js +344 -0
- package/lib/adapters/system-data-android/adapter.js +348 -0
- package/lib/adapters/system-data-android/index.js +76 -0
- package/lib/adapters/travel-12306/index.js +151 -0
- package/lib/adapters/travel-amap/index.js +164 -0
- package/lib/adapters/travel-baidu-map/index.js +162 -0
- package/lib/adapters/travel-base/index.js +240 -0
- package/lib/adapters/travel-ctrip/index.js +151 -0
- package/lib/adapters/wechat/bootstrap.js +146 -0
- package/lib/adapters/wechat/content-parser.js +326 -0
- package/lib/adapters/wechat/db-reader.js +209 -0
- package/lib/adapters/wechat/env-probe.js +218 -0
- package/lib/adapters/wechat/frida-agent/loader.js +67 -0
- package/lib/adapters/wechat/frida-agent/wechat-key-hook.js +126 -0
- package/lib/adapters/wechat/index.js +37 -0
- package/lib/adapters/wechat/key-extractor.js +158 -0
- package/lib/adapters/wechat/key-providers/frida-key-provider.js +244 -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 +220 -0
- package/lib/adapters/wechat/wechat-adapter.js +205 -0
- package/lib/analysis-skills/base.js +113 -0
- package/lib/analysis-skills/footprint.js +167 -0
- package/lib/analysis-skills/index.js +58 -0
- package/lib/analysis-skills/interests.js +161 -0
- package/lib/analysis-skills/relations.js +226 -0
- package/lib/analysis-skills/spending.js +219 -0
- package/lib/analysis-skills/timeline.js +167 -0
- package/lib/analysis.js +191 -2
- package/lib/entity-resolver/embedding-stage.js +198 -0
- package/lib/entity-resolver/entity-resolver.js +384 -0
- package/lib/entity-resolver/index.js +42 -0
- package/lib/entity-resolver/llm-stage.js +191 -0
- package/lib/entity-resolver/rule-stage.js +208 -0
- package/lib/entity-resolver/worker.js +149 -0
- package/lib/index.js +131 -0
- package/lib/migrations.js +73 -0
- package/lib/mobile-extractor/android.js +193 -0
- package/lib/mobile-extractor/index.js +9 -0
- package/lib/mobile-extractor/ios.js +223 -0
- package/lib/prompt-builder.js +11 -1
- package/lib/query-parser.js +7 -1
- package/lib/registry.js +42 -0
- package/lib/sidecar/index.js +15 -0
- package/lib/sidecar/supervisor.js +359 -0
- package/lib/vault.js +343 -0
- package/package.json +36 -3
- package/scripts/_make-fixture-all.js +126 -0
- package/scripts/_make-fixture-contacts.js +84 -0
- package/scripts/evaluate-entity-resolver.js +213 -0
- package/scripts/smoke-phase-5-5.js +196 -0
- package/scripts/smoke-phase-5-7.js +181 -0
- package/scripts/smoke-system-data-contacts.js +309 -0
- package/scripts/smoke-system-data.js +312 -0
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Unit tests for SystemDataAdapter (Phase 4.5.5).
|
|
5
|
+
*
|
|
6
|
+
* Uses a hand-rolled FakeSupervisor — we don't spawn Python here. The supervisor
|
|
7
|
+
* boundary is verified separately by the integration tests in
|
|
8
|
+
* `sidecar-supervisor.test.js` + `sidecar-contacts-cross-validate.test.js`.
|
|
9
|
+
* Here we want fast, deterministic, full-coverage exercise of the adapter's
|
|
10
|
+
* orchestration logic (include flags / dataPaths fallback / progress events /
|
|
11
|
+
* authenticate gating / etc).
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { describe, it, expect, beforeEach } from "vitest";
|
|
15
|
+
|
|
16
|
+
const {
|
|
17
|
+
SystemDataAdapter,
|
|
18
|
+
SYSTEM_DATA_ADAPTER_NAME,
|
|
19
|
+
SYSTEM_DATA_ADAPTER_VERSION,
|
|
20
|
+
DEFAULT_INCLUDE,
|
|
21
|
+
} = require("../../lib/adapters/system-data");
|
|
22
|
+
const { assertAdapter } = require("../../lib/adapter-spec");
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Minimal supervisor stub: pluggable per-method handlers + recorded calls.
|
|
26
|
+
*/
|
|
27
|
+
class FakeSupervisor {
|
|
28
|
+
constructor(handlers = {}) {
|
|
29
|
+
this.handlers = handlers;
|
|
30
|
+
this.calls = [];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async invoke(method, params = {}, opts = {}) {
|
|
34
|
+
this.calls.push({ method, params, opts });
|
|
35
|
+
const handler = this.handlers[method];
|
|
36
|
+
if (!handler) throw Object.assign(new Error(`unhandled method: ${method}`), { code: "TEST_NO_HANDLER" });
|
|
37
|
+
return await handler(params, opts);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Make a chunk-emitting handler whose `onChunk` is fed by `chunks`. */
|
|
42
|
+
function chunkingHandler(chunks, result) {
|
|
43
|
+
return async (_params, opts) => {
|
|
44
|
+
for (const c of chunks) {
|
|
45
|
+
if (opts && typeof opts.onChunk === "function") opts.onChunk(c);
|
|
46
|
+
}
|
|
47
|
+
return result;
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function personPayload(rawContactId, name, phones = []) {
|
|
52
|
+
return {
|
|
53
|
+
id: `person:system:android:${rawContactId}`,
|
|
54
|
+
type: "person",
|
|
55
|
+
subtype: "contact",
|
|
56
|
+
names: [name],
|
|
57
|
+
identifiers: phones.length ? { phone: phones } : undefined,
|
|
58
|
+
ingestedAt: 1_700_000_000_000,
|
|
59
|
+
confidence: 1.0,
|
|
60
|
+
source: {
|
|
61
|
+
adapter: "system-data",
|
|
62
|
+
adapterVersion: "0.1.0",
|
|
63
|
+
originalId: String(rawContactId),
|
|
64
|
+
capturedAt: 1_700_000_000_000,
|
|
65
|
+
capturedBy: "sqlite",
|
|
66
|
+
},
|
|
67
|
+
extra: { starred: false },
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function callEventPayload(callId, occurredAt = 1_700_000_000_000) {
|
|
72
|
+
return {
|
|
73
|
+
id: `event:system:call:${callId}`,
|
|
74
|
+
type: "event",
|
|
75
|
+
subtype: "call",
|
|
76
|
+
occurredAt,
|
|
77
|
+
actor: "person:self",
|
|
78
|
+
participants: ["person:system:android:1"],
|
|
79
|
+
content: {},
|
|
80
|
+
ingestedAt: occurredAt,
|
|
81
|
+
confidence: 1.0,
|
|
82
|
+
source: {
|
|
83
|
+
adapter: "system-data",
|
|
84
|
+
adapterVersion: "0.1.0",
|
|
85
|
+
originalId: String(callId),
|
|
86
|
+
capturedAt: occurredAt,
|
|
87
|
+
capturedBy: "sqlite",
|
|
88
|
+
},
|
|
89
|
+
extra: { callType: "outgoing", callTypeCode: 2, isRead: true, rawNumber: "13800001111" },
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function placePayload(name) {
|
|
94
|
+
return {
|
|
95
|
+
id: `place:wifi:${name.toLowerCase()}`,
|
|
96
|
+
type: "place",
|
|
97
|
+
name,
|
|
98
|
+
category: "wifi",
|
|
99
|
+
aliases: [],
|
|
100
|
+
ingestedAt: 1_700_000_000_000,
|
|
101
|
+
confidence: 0.95,
|
|
102
|
+
source: {
|
|
103
|
+
adapter: "system-data",
|
|
104
|
+
adapterVersion: "0.1.0",
|
|
105
|
+
originalId: name,
|
|
106
|
+
capturedAt: 1_700_000_000_000,
|
|
107
|
+
capturedBy: "manual",
|
|
108
|
+
},
|
|
109
|
+
extra: { securityType: "WPA/WPA2", hidden: false, passwordStored: true },
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// ---------------------------------------------------------------------------
|
|
114
|
+
// Suite
|
|
115
|
+
// ---------------------------------------------------------------------------
|
|
116
|
+
|
|
117
|
+
describe("SystemDataAdapter contract", () => {
|
|
118
|
+
it("conforms to PersonalDataAdapter contract", () => {
|
|
119
|
+
const adapter = new SystemDataAdapter({ supervisor: new FakeSupervisor() });
|
|
120
|
+
const result = assertAdapter(adapter);
|
|
121
|
+
expect(result).toEqual({ ok: true });
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("exposes constants and version", () => {
|
|
125
|
+
expect(SYSTEM_DATA_ADAPTER_NAME).toBe("system-data");
|
|
126
|
+
expect(SYSTEM_DATA_ADAPTER_VERSION).toMatch(/^\d+\.\d+\.\d+/);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("declares correct default include flags (SMS opt-out)", () => {
|
|
130
|
+
expect(DEFAULT_INCLUDE).toEqual({
|
|
131
|
+
contacts: true,
|
|
132
|
+
calllog: true,
|
|
133
|
+
sms: false,
|
|
134
|
+
wifi: true,
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("dataDisclosure is high-sensitivity + legalGate ON", () => {
|
|
139
|
+
const adapter = new SystemDataAdapter({ supervisor: new FakeSupervisor() });
|
|
140
|
+
expect(adapter.dataDisclosure.sensitivity).toBe("high");
|
|
141
|
+
expect(adapter.dataDisclosure.legalGate).toBe(true);
|
|
142
|
+
expect(adapter.dataDisclosure.notice).toMatch(/不向任何服务器上传/);
|
|
143
|
+
expect(adapter.dataDisclosure.fields.some((f) => f.startsWith("sms:"))).toBe(true);
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
describe("SystemDataAdapter.authenticate", () => {
|
|
148
|
+
it("returns offline mode when dataPaths are provided", async () => {
|
|
149
|
+
const sup = new FakeSupervisor({
|
|
150
|
+
"sidecar.ping": async () => ({ version: "0.1.0", pythonVersion: "3.12" }),
|
|
151
|
+
});
|
|
152
|
+
const adapter = new SystemDataAdapter({ supervisor: sup });
|
|
153
|
+
|
|
154
|
+
const out = await adapter.authenticate({ dataPaths: { contacts: "/tmp/c.db" } });
|
|
155
|
+
expect(out).toEqual({ ok: true, mode: "offline", sidecarVersion: "0.1.0" });
|
|
156
|
+
expect(sup.calls.map((c) => c.method)).toEqual(["sidecar.ping"]);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("fails when sidecar is down", async () => {
|
|
160
|
+
const sup = new FakeSupervisor({
|
|
161
|
+
"sidecar.ping": async () => {
|
|
162
|
+
throw Object.assign(new Error("sidecar gone"), {
|
|
163
|
+
code: "SIDECAR_NOT_RUNNING",
|
|
164
|
+
});
|
|
165
|
+
},
|
|
166
|
+
});
|
|
167
|
+
const adapter = new SystemDataAdapter({ supervisor: sup });
|
|
168
|
+
await expect(adapter.authenticate({ dataPaths: { contacts: "/x" } })).rejects.toThrow();
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("device mode lists devices and accepts any authorized one", async () => {
|
|
172
|
+
const sup = new FakeSupervisor({
|
|
173
|
+
"sidecar.ping": async () => ({ version: "0.1.0", pythonVersion: "3.12" }),
|
|
174
|
+
"android.list_devices": async () => ({
|
|
175
|
+
devices: [
|
|
176
|
+
{ serial: "abc", state: "device", model: "Redmi" },
|
|
177
|
+
{ serial: "xyz", state: "unauthorized" },
|
|
178
|
+
],
|
|
179
|
+
}),
|
|
180
|
+
});
|
|
181
|
+
const adapter = new SystemDataAdapter({ supervisor: sup });
|
|
182
|
+
|
|
183
|
+
const out = await adapter.authenticate({});
|
|
184
|
+
expect(out.ok).toBe(true);
|
|
185
|
+
expect(out.mode).toBe("device");
|
|
186
|
+
expect(out.devices).toHaveLength(1);
|
|
187
|
+
expect(out.devices[0].serial).toBe("abc");
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it("device mode fails when no authorized device matches", async () => {
|
|
191
|
+
const sup = new FakeSupervisor({
|
|
192
|
+
"sidecar.ping": async () => ({ version: "0.1.0", pythonVersion: "3.12" }),
|
|
193
|
+
"android.list_devices": async () => ({
|
|
194
|
+
devices: [{ serial: "xyz", state: "unauthorized" }],
|
|
195
|
+
}),
|
|
196
|
+
});
|
|
197
|
+
const adapter = new SystemDataAdapter({ supervisor: sup });
|
|
198
|
+
const out = await adapter.authenticate({});
|
|
199
|
+
expect(out.ok).toBe(false);
|
|
200
|
+
expect(out.reason).toMatch(/no authorized/i);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("device mode targets a specific serial when provided", async () => {
|
|
204
|
+
const sup = new FakeSupervisor({
|
|
205
|
+
"sidecar.ping": async () => ({ version: "0.1.0", pythonVersion: "3.12" }),
|
|
206
|
+
"android.list_devices": async () => ({
|
|
207
|
+
devices: [
|
|
208
|
+
{ serial: "abc", state: "device" },
|
|
209
|
+
{ serial: "xyz", state: "device" },
|
|
210
|
+
],
|
|
211
|
+
}),
|
|
212
|
+
});
|
|
213
|
+
const adapter = new SystemDataAdapter({ supervisor: sup });
|
|
214
|
+
const out = await adapter.authenticate({ serial: "xyz" });
|
|
215
|
+
expect(out.ok).toBe(true);
|
|
216
|
+
expect(out.devices).toEqual([{ serial: "xyz", state: "device" }]);
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
describe("SystemDataAdapter.sync via dataPaths (no ADB)", () => {
|
|
221
|
+
let sup;
|
|
222
|
+
let adapter;
|
|
223
|
+
|
|
224
|
+
beforeEach(() => {
|
|
225
|
+
sup = new FakeSupervisor({
|
|
226
|
+
"system.parse_contacts": chunkingHandler(
|
|
227
|
+
[{ events: [], persons: [personPayload(1, "妈妈", ["138"])], places: [], items: [] }],
|
|
228
|
+
{ status: "ok", totalPersons: 1, watermark: null, stats: { with_phone: 1, with_email: 0, starred: 0 } },
|
|
229
|
+
),
|
|
230
|
+
"system.parse_calllog": chunkingHandler(
|
|
231
|
+
[{
|
|
232
|
+
events: [callEventPayload(101)],
|
|
233
|
+
persons: [personPayload("unknown:139", "139")],
|
|
234
|
+
places: [],
|
|
235
|
+
items: [],
|
|
236
|
+
}],
|
|
237
|
+
{ status: "ok", totalEvents: 1, totalPersonsCreated: 1, watermark: null, stats: {} },
|
|
238
|
+
),
|
|
239
|
+
"system.parse_wifi": chunkingHandler(
|
|
240
|
+
[{ events: [], persons: [], places: [placePayload("HomeWiFi")], items: [] }],
|
|
241
|
+
{ status: "ok", totalPlaces: 1, watermark: null, stats: {} },
|
|
242
|
+
),
|
|
243
|
+
});
|
|
244
|
+
adapter = new SystemDataAdapter({ supervisor: sup });
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it("yields raws in entityType-tagged envelope shape", async () => {
|
|
248
|
+
const raws = [];
|
|
249
|
+
for await (const raw of adapter.sync({
|
|
250
|
+
dataPaths: {
|
|
251
|
+
contacts: "/tmp/contacts2.db",
|
|
252
|
+
calllog: "/tmp/calls.db",
|
|
253
|
+
wifi: "/tmp/wifi-dir",
|
|
254
|
+
},
|
|
255
|
+
})) {
|
|
256
|
+
raws.push(raw);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// 1 person (contacts) + 1 event + 1 unknown person (calllog) + 1 place
|
|
260
|
+
expect(raws).toHaveLength(4);
|
|
261
|
+
const types = raws.map((r) => r.entityType);
|
|
262
|
+
expect(types.sort()).toEqual(["event", "person", "person", "place"]);
|
|
263
|
+
|
|
264
|
+
// Envelope shape
|
|
265
|
+
for (const r of raws) {
|
|
266
|
+
expect(typeof r.originalId).toBe("string");
|
|
267
|
+
expect(typeof r.capturedAt).toBe("number");
|
|
268
|
+
expect(r.payload && r.payload.type).toBe(r.entityType);
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it("normalize() places one entity into its correct bucket", () => {
|
|
273
|
+
const raw = {
|
|
274
|
+
entityType: "person",
|
|
275
|
+
originalId: "1",
|
|
276
|
+
capturedAt: 1,
|
|
277
|
+
payload: personPayload(1, "X"),
|
|
278
|
+
};
|
|
279
|
+
const batch = adapter.normalize(raw);
|
|
280
|
+
expect(batch).toEqual({
|
|
281
|
+
events: [],
|
|
282
|
+
persons: [raw.payload],
|
|
283
|
+
places: [],
|
|
284
|
+
items: [],
|
|
285
|
+
topics: [],
|
|
286
|
+
});
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
it("normalize() returns empty buckets for unknown entityType", () => {
|
|
290
|
+
const batch = adapter.normalize({ entityType: "???", payload: {} });
|
|
291
|
+
expect(batch.events).toEqual([]);
|
|
292
|
+
expect(batch.persons).toEqual([]);
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it("respects include flag — disabling SMS skips parse_sms entirely", async () => {
|
|
296
|
+
// sms enabled in this run; we have no parse_sms handler, so enabling
|
|
297
|
+
// it would crash. Default has it off — sync should succeed.
|
|
298
|
+
const raws = [];
|
|
299
|
+
for await (const raw of adapter.sync({
|
|
300
|
+
dataPaths: {
|
|
301
|
+
contacts: "/tmp/c.db",
|
|
302
|
+
calllog: "/tmp/cl.db",
|
|
303
|
+
wifi: "/tmp/wifi",
|
|
304
|
+
},
|
|
305
|
+
})) {
|
|
306
|
+
raws.push(raw);
|
|
307
|
+
}
|
|
308
|
+
expect(sup.calls.find((c) => c.method === "system.parse_sms")).toBeUndefined();
|
|
309
|
+
expect(raws.length).toBeGreaterThan(0);
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
it("explicitly disabling every source yields zero raws and zero sidecar calls", async () => {
|
|
313
|
+
const raws = [];
|
|
314
|
+
for await (const raw of adapter.sync({
|
|
315
|
+
include: { contacts: false, calllog: false, sms: false, wifi: false },
|
|
316
|
+
dataPaths: {},
|
|
317
|
+
})) {
|
|
318
|
+
raws.push(raw);
|
|
319
|
+
}
|
|
320
|
+
expect(raws).toEqual([]);
|
|
321
|
+
expect(sup.calls).toEqual([]);
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
it("forwards onProgress events from adapter scope", async () => {
|
|
325
|
+
const events = [];
|
|
326
|
+
const iter = adapter.sync({
|
|
327
|
+
dataPaths: { contacts: "/c", calllog: "/cl", wifi: "/w" },
|
|
328
|
+
onProgress: (msg) => events.push(msg),
|
|
329
|
+
});
|
|
330
|
+
for await (const _ of iter) { /* drain */ }
|
|
331
|
+
|
|
332
|
+
const phases = events.map((e) => `${e.source}:${e.phase}`);
|
|
333
|
+
expect(phases).toContain("contacts:parsing");
|
|
334
|
+
expect(phases).toContain("calllog:parsing");
|
|
335
|
+
expect(phases).toContain("wifi:parsing");
|
|
336
|
+
});
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
describe("SystemDataAdapter.sync ADB pull flow", () => {
|
|
340
|
+
it("invokes android.pull_file per enabled source when no dataPaths given", async () => {
|
|
341
|
+
const pullCalls = [];
|
|
342
|
+
const sup = new FakeSupervisor({
|
|
343
|
+
"android.pull_file": async (params) => {
|
|
344
|
+
pullCalls.push(params.remote_path);
|
|
345
|
+
return { remote: params.remote_path, local: `/scratch/${pullCalls.length}.db`, bytes: 100 };
|
|
346
|
+
},
|
|
347
|
+
"system.parse_contacts": chunkingHandler([], { status: "ok", totalPersons: 0, watermark: null, stats: {} }),
|
|
348
|
+
"system.parse_calllog": chunkingHandler([], { status: "ok", totalEvents: 0, totalPersonsCreated: 0, watermark: null, stats: {} }),
|
|
349
|
+
"system.parse_wifi": chunkingHandler([], { status: "ok", totalPlaces: 0, watermark: null, stats: {} }),
|
|
350
|
+
});
|
|
351
|
+
const adapter = new SystemDataAdapter({ supervisor: sup });
|
|
352
|
+
|
|
353
|
+
const iter = adapter.sync({ serial: "redmi", scratchDir: "/scratch" });
|
|
354
|
+
for await (const _ of iter) { /* drain */ }
|
|
355
|
+
|
|
356
|
+
expect(pullCalls).toEqual([
|
|
357
|
+
"/data/data/com.android.providers.contacts/databases/contacts2.db",
|
|
358
|
+
"/data/data/com.android.providers.contacts/databases/calllog.db",
|
|
359
|
+
"/data/misc/wifi/",
|
|
360
|
+
]);
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
it("sdcard mode uses /sdcard/Download/ paths", async () => {
|
|
364
|
+
const pullCalls = [];
|
|
365
|
+
const sup = new FakeSupervisor({
|
|
366
|
+
"android.pull_file": async (params) => {
|
|
367
|
+
pullCalls.push(params.remote_path);
|
|
368
|
+
return { remote: params.remote_path, local: `/scratch/${pullCalls.length}`, bytes: 100 };
|
|
369
|
+
},
|
|
370
|
+
"system.parse_contacts": chunkingHandler([], { status: "ok", totalPersons: 0, watermark: null, stats: {} }),
|
|
371
|
+
"system.parse_calllog": chunkingHandler([], { status: "ok", totalEvents: 0, totalPersonsCreated: 0, watermark: null, stats: {} }),
|
|
372
|
+
"system.parse_wifi": chunkingHandler([], { status: "ok", totalPlaces: 0, watermark: null, stats: {} }),
|
|
373
|
+
});
|
|
374
|
+
const adapter = new SystemDataAdapter({ supervisor: sup });
|
|
375
|
+
|
|
376
|
+
const iter = adapter.sync({ serial: "redmi", extractMode: "sdcard" });
|
|
377
|
+
for await (const _ of iter) { /* drain */ }
|
|
378
|
+
|
|
379
|
+
for (const p of pullCalls) expect(p).toMatch(/^\/sdcard\/Download\//);
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
it("throws when a source is enabled but neither serial nor dataPaths present", async () => {
|
|
383
|
+
const sup = new FakeSupervisor();
|
|
384
|
+
const adapter = new SystemDataAdapter({ supervisor: sup });
|
|
385
|
+
const iter = adapter.sync({ include: { contacts: true, calllog: false, sms: false, wifi: false } });
|
|
386
|
+
await expect((async () => {
|
|
387
|
+
for await (const _ of iter) { /* */ }
|
|
388
|
+
})()).rejects.toThrow(/no serial.*dataPaths/i);
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
it("skips wifi gracefully when pull_file fails with EXTRACT_PERMISSION_DENIED", async () => {
|
|
392
|
+
const sup = new FakeSupervisor({
|
|
393
|
+
"android.pull_file": async (params) => {
|
|
394
|
+
if (params.remote_path.startsWith("/data/misc/wifi")) {
|
|
395
|
+
throw Object.assign(new Error("denied"), {
|
|
396
|
+
code: "EXTRACT_PERMISSION_DENIED",
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
return { remote: params.remote_path, local: `/scratch/x`, bytes: 1 };
|
|
400
|
+
},
|
|
401
|
+
"system.parse_contacts": chunkingHandler([], { status: "ok", totalPersons: 0, watermark: null, stats: {} }),
|
|
402
|
+
"system.parse_calllog": chunkingHandler([], { status: "ok", totalEvents: 0, totalPersonsCreated: 0, watermark: null, stats: {} }),
|
|
403
|
+
});
|
|
404
|
+
const adapter = new SystemDataAdapter({ supervisor: sup });
|
|
405
|
+
const events = [];
|
|
406
|
+
const iter = adapter.sync({
|
|
407
|
+
serial: "redmi",
|
|
408
|
+
include: { contacts: true, calllog: true, sms: false, wifi: true },
|
|
409
|
+
onProgress: (msg) => events.push(msg),
|
|
410
|
+
});
|
|
411
|
+
for await (const _ of iter) { /* drain */ }
|
|
412
|
+
|
|
413
|
+
const wifiSkipped = events.find((e) => e.source === "wifi" && e.phase === "skipped");
|
|
414
|
+
expect(wifiSkipped).toBeDefined();
|
|
415
|
+
expect(wifiSkipped.reason).toBe("EXTRACT_PERMISSION_DENIED");
|
|
416
|
+
});
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
describe("SystemDataAdapter.healthCheck", () => {
|
|
420
|
+
it("returns ok when sidecar pings", async () => {
|
|
421
|
+
const sup = new FakeSupervisor({
|
|
422
|
+
"sidecar.ping": async () => ({ version: "0.1.0" }),
|
|
423
|
+
});
|
|
424
|
+
const adapter = new SystemDataAdapter({ supervisor: sup });
|
|
425
|
+
const r = await adapter.healthCheck();
|
|
426
|
+
expect(r).toEqual({ ok: true, version: "0.1.0" });
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
it("returns not-ok with reason when ping fails", async () => {
|
|
430
|
+
const sup = new FakeSupervisor({
|
|
431
|
+
"sidecar.ping": async () => {
|
|
432
|
+
throw Object.assign(new Error("dead"), { code: "TIMEOUT" });
|
|
433
|
+
},
|
|
434
|
+
});
|
|
435
|
+
const adapter = new SystemDataAdapter({ supervisor: sup });
|
|
436
|
+
const r = await adapter.healthCheck();
|
|
437
|
+
expect(r.ok).toBe(false);
|
|
438
|
+
expect(r.reason).toMatch(/TIMEOUT/);
|
|
439
|
+
});
|
|
440
|
+
});
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
|
|
4
|
+
import { mkdtempSync, rmSync, existsSync, readFileSync } from "node:fs";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { tmpdir } from "node:os";
|
|
7
|
+
|
|
8
|
+
const {
|
|
9
|
+
SystemDataAndroidAdapter,
|
|
10
|
+
ingestSystemDataAndroidSnapshot,
|
|
11
|
+
SNAPSHOT_SCHEMA_VERSION,
|
|
12
|
+
} = require("../../lib/adapters/system-data-android");
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Tests for the Path C ingest helper — staging-file write + syncAdapter call
|
|
16
|
+
* + cleanup. Uses a fake hub.registry that proxies to a real
|
|
17
|
+
* SystemDataAndroidAdapter so we exercise both halves of the pipeline (the
|
|
18
|
+
* adapter's _syncViaSnapshot does fs.readFileSync of the staging path we
|
|
19
|
+
* wrote, then the helper unlinks it after).
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
let tmpHubDir;
|
|
23
|
+
let hub;
|
|
24
|
+
|
|
25
|
+
beforeEach(() => {
|
|
26
|
+
tmpHubDir = mkdtempSync(join(tmpdir(), "pdh-ingest-"));
|
|
27
|
+
const adapter = new SystemDataAndroidAdapter();
|
|
28
|
+
hub = {
|
|
29
|
+
hubDir: tmpHubDir,
|
|
30
|
+
registry: {
|
|
31
|
+
syncAdapter: vi.fn(async (name, opts) => {
|
|
32
|
+
// Mimic what the real hub does: run the adapter's sync against the
|
|
33
|
+
// staging path the helper wrote.
|
|
34
|
+
const out = { adapter: name, ingested: 0, partitions: {} };
|
|
35
|
+
for await (const raw of adapter.sync(opts)) {
|
|
36
|
+
out.ingested += 1;
|
|
37
|
+
if (raw.kind === "contact")
|
|
38
|
+
out.partitions.contacts = { ingested: (out.partitions.contacts?.ingested || 0) + 1 };
|
|
39
|
+
if (raw.kind === "app")
|
|
40
|
+
out.partitions.apps = { ingested: (out.partitions.apps?.ingested || 0) + 1 };
|
|
41
|
+
}
|
|
42
|
+
return out;
|
|
43
|
+
}),
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
afterEach(() => {
|
|
49
|
+
rmSync(tmpHubDir, { recursive: true, force: true });
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
describe("ingestSystemDataAndroidSnapshot", () => {
|
|
53
|
+
it("writes staging file, runs syncAdapter, cleans up", async () => {
|
|
54
|
+
const snapshot = {
|
|
55
|
+
schemaVersion: SNAPSHOT_SCHEMA_VERSION,
|
|
56
|
+
snapshottedAt: 1_700_000_000_000,
|
|
57
|
+
contacts: [
|
|
58
|
+
{ lookupKey: "ck-1", displayName: "妈妈", phones: ["13800000001"] },
|
|
59
|
+
],
|
|
60
|
+
apps: [{ packageName: "com.tencent.mm", label: "微信" }],
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const report = await ingestSystemDataAndroidSnapshot(hub, snapshot);
|
|
64
|
+
|
|
65
|
+
expect(report.adapter).toBe("system-data-android");
|
|
66
|
+
expect(report.ingested).toBe(2); // 1 contact + 1 app
|
|
67
|
+
|
|
68
|
+
// syncAdapter was called with an inputPath under <hubDir>/staging/
|
|
69
|
+
expect(hub.registry.syncAdapter).toHaveBeenCalledTimes(1);
|
|
70
|
+
const [name, opts] = hub.registry.syncAdapter.mock.calls[0];
|
|
71
|
+
expect(name).toBe("system-data-android");
|
|
72
|
+
expect(opts.inputPath).toContain(join(tmpHubDir, "staging"));
|
|
73
|
+
expect(opts.inputPath).toMatch(/system-data-android-\d+.+\.json$/);
|
|
74
|
+
|
|
75
|
+
// Staging file was cleaned up afterwards
|
|
76
|
+
expect(existsSync(opts.inputPath)).toBe(false);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("rejects snapshot with mismatched schemaVersion", async () => {
|
|
80
|
+
await expect(
|
|
81
|
+
ingestSystemDataAndroidSnapshot(hub, {
|
|
82
|
+
schemaVersion: 99,
|
|
83
|
+
snapshottedAt: 0,
|
|
84
|
+
contacts: [],
|
|
85
|
+
apps: [],
|
|
86
|
+
}),
|
|
87
|
+
).rejects.toThrow(/schemaVersion 99/);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("rejects missing hub.hubDir", async () => {
|
|
91
|
+
await expect(
|
|
92
|
+
ingestSystemDataAndroidSnapshot(
|
|
93
|
+
{ registry: hub.registry },
|
|
94
|
+
{ schemaVersion: SNAPSHOT_SCHEMA_VERSION, snapshottedAt: 0, contacts: [], apps: [] },
|
|
95
|
+
),
|
|
96
|
+
).rejects.toThrow(/hubDir/);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("rejects missing snapshot payload", async () => {
|
|
100
|
+
await expect(
|
|
101
|
+
ingestSystemDataAndroidSnapshot(hub, null),
|
|
102
|
+
).rejects.toThrow(/snapshot payload required/);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("cleans up staging file even when syncAdapter throws", async () => {
|
|
106
|
+
hub.registry.syncAdapter = vi.fn(async () => {
|
|
107
|
+
throw new Error("simulated sync failure");
|
|
108
|
+
});
|
|
109
|
+
let leaked = null;
|
|
110
|
+
try {
|
|
111
|
+
await ingestSystemDataAndroidSnapshot(hub, {
|
|
112
|
+
schemaVersion: SNAPSHOT_SCHEMA_VERSION,
|
|
113
|
+
snapshottedAt: 0,
|
|
114
|
+
contacts: [{ lookupKey: "x", displayName: "X" }],
|
|
115
|
+
apps: [],
|
|
116
|
+
});
|
|
117
|
+
} catch (_e) {
|
|
118
|
+
// expected
|
|
119
|
+
}
|
|
120
|
+
leaked = hub.registry.syncAdapter.mock.calls[0]?.[1]?.inputPath;
|
|
121
|
+
expect(leaked).toBeTruthy();
|
|
122
|
+
expect(existsSync(leaked)).toBe(false);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("staging file contains the snapshot JSON we passed in", async () => {
|
|
126
|
+
// Capture path before syncAdapter unlinks it
|
|
127
|
+
let captured = null;
|
|
128
|
+
hub.registry.syncAdapter = vi.fn(async (_name, opts) => {
|
|
129
|
+
captured = readFileSync(opts.inputPath, "utf-8");
|
|
130
|
+
return { adapter: "system-data-android", ingested: 0 };
|
|
131
|
+
});
|
|
132
|
+
const snapshot = {
|
|
133
|
+
schemaVersion: SNAPSHOT_SCHEMA_VERSION,
|
|
134
|
+
snapshottedAt: 1_700_000_000_000,
|
|
135
|
+
contacts: [{ lookupKey: "ck-test", displayName: "Test" }],
|
|
136
|
+
apps: [],
|
|
137
|
+
};
|
|
138
|
+
await ingestSystemDataAndroidSnapshot(hub, snapshot);
|
|
139
|
+
expect(captured).toBeTruthy();
|
|
140
|
+
const parsed = JSON.parse(captured);
|
|
141
|
+
expect(parsed.schemaVersion).toBe(SNAPSHOT_SCHEMA_VERSION);
|
|
142
|
+
expect(parsed.contacts[0].lookupKey).toBe("ck-test");
|
|
143
|
+
});
|
|
144
|
+
});
|