@chainlesschain/personal-data-hub 0.4.29 → 0.4.30

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.
Files changed (198) hide show
  1. package/lib/prompt-builder.js +15 -1
  2. package/package.json +4 -1
  3. package/__tests__/adapter-guide.test.js +0 -47
  4. package/__tests__/adapter-spec.test.js +0 -78
  5. package/__tests__/adapters/ai-chat-cookie-capture-spec.test.js +0 -211
  6. package/__tests__/adapters/ai-chat-health-checker.test.js +0 -262
  7. package/__tests__/adapters/ai-chat-history.test.js +0 -396
  8. package/__tests__/adapters/ai-chat-http-client.test.js +0 -242
  9. package/__tests__/adapters/ai-chat-vendors.test.js +0 -874
  10. package/__tests__/adapters/alipay-bill-adapter.test.js +0 -538
  11. package/__tests__/adapters/apple-health.test.js +0 -95
  12. package/__tests__/adapters/bank-family.test.js +0 -125
  13. package/__tests__/adapters/biz-tianyancha.test.js +0 -159
  14. package/__tests__/adapters/browser-history-chrome.test.js +0 -377
  15. package/__tests__/adapters/browser-history-edge.test.js +0 -159
  16. package/__tests__/adapters/car-mercedesme.test.js +0 -74
  17. package/__tests__/adapters/doc-baidu-netdisk.test.js +0 -102
  18. package/__tests__/adapters/doc-camscanner.test.js +0 -147
  19. package/__tests__/adapters/doc-platforms.test.js +0 -177
  20. package/__tests__/adapters/edu-huawei-learning-live.test.js +0 -198
  21. package/__tests__/adapters/edu-zuoyebang-live.test.js +0 -226
  22. package/__tests__/adapters/email-adapter-snapshot.test.js +0 -237
  23. package/__tests__/adapters/email-adapter.test.js +0 -742
  24. package/__tests__/adapters/email-classifier.test.js +0 -347
  25. package/__tests__/adapters/email-imap-session.test.js +0 -334
  26. package/__tests__/adapters/email-parser.test.js +0 -244
  27. package/__tests__/adapters/email-pdf-extractor.test.js +0 -529
  28. package/__tests__/adapters/email-providers.test.js +0 -84
  29. package/__tests__/adapters/email-retry-progress.test.js +0 -294
  30. package/__tests__/adapters/email-templates.test.js +0 -822
  31. package/__tests__/adapters/family-23-collectors-scaffold.test.js +0 -182
  32. package/__tests__/adapters/finance-alipay-live.test.js +0 -258
  33. package/__tests__/adapters/finance-dcep.test.js +0 -74
  34. package/__tests__/adapters/fitness-joyrun.test.js +0 -82
  35. package/__tests__/adapters/game-genshin-live.test.js +0 -238
  36. package/__tests__/adapters/game-genshin-scaffold.test.js +0 -108
  37. package/__tests__/adapters/game-honor-of-kings-live.test.js +0 -230
  38. package/__tests__/adapters/git-activity.test.js +0 -222
  39. package/__tests__/adapters/gov-12123.test.js +0 -103
  40. package/__tests__/adapters/gov-ixiamen.test.js +0 -150
  41. package/__tests__/adapters/gov-tax.test.js +0 -135
  42. package/__tests__/adapters/health-meiyou.test.js +0 -125
  43. package/__tests__/adapters/local-files.test.js +0 -264
  44. package/__tests__/adapters/local-im-pc.test.js +0 -154
  45. package/__tests__/adapters/messaging-whatsapp.test.js +0 -289
  46. package/__tests__/adapters/music-kugou.test.js +0 -187
  47. package/__tests__/adapters/music-qq.test.js +0 -112
  48. package/__tests__/adapters/netease-music-live.test.js +0 -244
  49. package/__tests__/adapters/netease-music.test.js +0 -74
  50. package/__tests__/adapters/pc-local-discovery.test.js +0 -141
  51. package/__tests__/adapters/qq-pc-direct-read.test.js +0 -227
  52. package/__tests__/adapters/reading-family.test.js +0 -108
  53. package/__tests__/adapters/recruit-boss.test.js +0 -180
  54. package/__tests__/adapters/shell-history.test.js +0 -180
  55. package/__tests__/adapters/shopping-base.test.js +0 -179
  56. package/__tests__/adapters/shopping-dianping.test.js +0 -239
  57. package/__tests__/adapters/social-bilibili-adb-api-client.test.js +0 -721
  58. package/__tests__/adapters/social-bilibili-adb-chromium-cookies-reader.test.js +0 -346
  59. package/__tests__/adapters/social-bilibili-adb-collector.test.js +0 -284
  60. package/__tests__/adapters/social-bilibili-adb-cookies-extension.test.js +0 -343
  61. package/__tests__/adapters/social-bilibili-adb-snapshot-builder.test.js +0 -296
  62. package/__tests__/adapters/social-csdn.test.js +0 -175
  63. package/__tests__/adapters/social-dongchedi.test.js +0 -165
  64. package/__tests__/adapters/social-douyin-adb-aweme-detail.test.js +0 -165
  65. package/__tests__/adapters/social-douyin-adb-collector.test.js +0 -254
  66. package/__tests__/adapters/social-douyin-adb-db-extension.test.js +0 -114
  67. package/__tests__/adapters/social-douyin-adb-im-db-parser.test.js +0 -304
  68. package/__tests__/adapters/social-douyin-adb-snapshot-builder.test.js +0 -216
  69. package/__tests__/adapters/social-douyin-adb-usage-profile.test.js +0 -229
  70. package/__tests__/adapters/social-douyin-adb-watch-history.test.js +0 -269
  71. package/__tests__/adapters/social-kuaishou-adb-api-client.test.js +0 -496
  72. package/__tests__/adapters/social-kuaishou-adb-collector.test.js +0 -276
  73. package/__tests__/adapters/social-kuaishou-adb-cookies-extension.test.js +0 -152
  74. package/__tests__/adapters/social-kuaishou-adb-snapshot-builder.test.js +0 -178
  75. package/__tests__/adapters/social-toutiao-adb-account-reader.test.js +0 -135
  76. package/__tests__/adapters/social-toutiao-adb-api-client.test.js +0 -626
  77. package/__tests__/adapters/social-toutiao-adb-article.test.js +0 -155
  78. package/__tests__/adapters/social-toutiao-adb-collector.test.js +0 -378
  79. package/__tests__/adapters/social-toutiao-adb-cookies-extension.test.js +0 -193
  80. package/__tests__/adapters/social-toutiao-adb-snapshot-builder.test.js +0 -196
  81. package/__tests__/adapters/social-toutiao-kuaishou-scaffold.test.js +0 -311
  82. package/__tests__/adapters/social-weibo-adb-api-client.test.js +0 -362
  83. package/__tests__/adapters/social-weibo-adb-collector.test.js +0 -201
  84. package/__tests__/adapters/social-weibo-adb-cookies-extension.test.js +0 -167
  85. package/__tests__/adapters/social-weibo-adb-snapshot-builder.test.js +0 -189
  86. package/__tests__/adapters/social-xiaohongshu-adb-api-client.test.js +0 -431
  87. package/__tests__/adapters/social-xiaohongshu-adb-collector.test.js +0 -207
  88. package/__tests__/adapters/social-xiaohongshu-adb-cookies-extension.test.js +0 -0
  89. package/__tests__/adapters/social-xiaohongshu-adb-sign-provider-injection.test.js +0 -351
  90. package/__tests__/adapters/social-xiaohongshu-adb-sign.test.js +0 -130
  91. package/__tests__/adapters/social-xiaohongshu-adb-snapshot-builder.test.js +0 -200
  92. package/__tests__/adapters/social-zhihu.test.js +0 -246
  93. package/__tests__/adapters/system-data-adapter.test.js +0 -443
  94. package/__tests__/adapters/system-data-android-ingest.test.js +0 -144
  95. package/__tests__/adapters/system-data-android.test.js +0 -519
  96. package/__tests__/adapters/system-data-disclosure.test.js +0 -153
  97. package/__tests__/adapters/travel-12306.test.js +0 -512
  98. package/__tests__/adapters/travel-amap.test.js +0 -219
  99. package/__tests__/adapters/travel-baidu-map.test.js +0 -305
  100. package/__tests__/adapters/travel-base.test.js +0 -205
  101. package/__tests__/adapters/travel-ctrip.test.js +0 -377
  102. package/__tests__/adapters/travel-didi-consumer.test.js +0 -66
  103. package/__tests__/adapters/travel-didi.test.js +0 -204
  104. package/__tests__/adapters/travel-tencent-map.test.js +0 -207
  105. package/__tests__/adapters/travel-tongcheng.test.js +0 -289
  106. package/__tests__/adapters/video-platforms.test.js +0 -152
  107. package/__tests__/adapters/video-xigua.test.js +0 -106
  108. package/__tests__/adapters/vscode.test.js +0 -299
  109. package/__tests__/adapters/wechat-bootstrap.test.js +0 -240
  110. package/__tests__/adapters/wechat-env-probe.test.js +0 -162
  111. package/__tests__/adapters/wechat-frida-agent.test.js +0 -322
  112. package/__tests__/adapters/wechat-frida-integration.test.js +0 -149
  113. package/__tests__/adapters/wechat-frida-key-provider.test.js +0 -188
  114. package/__tests__/adapters/wechat-md5-key-provider.test.js +0 -101
  115. package/__tests__/adapters/wechat-pc-direct-read.test.js +0 -365
  116. package/__tests__/adapters/wechat-pc-group-topic.test.js +0 -63
  117. package/__tests__/adapters/wechat-pc-v4-sidecar.test.js +0 -72
  118. package/__tests__/adapters/weread.test.js +0 -123
  119. package/__tests__/adapters/wework-pc.test.js +0 -124
  120. package/__tests__/adapters/win-recent.test.js +0 -192
  121. package/__tests__/analysis-skills.test.js +0 -754
  122. package/__tests__/analysis.test.js +0 -1845
  123. package/__tests__/audio-ximalaya-snapshot.test.js +0 -279
  124. package/__tests__/batch.test.js +0 -133
  125. package/__tests__/bridges-cc-kg.test.js +0 -231
  126. package/__tests__/bridges-cc-llm.test.js +0 -191
  127. package/__tests__/bridges-cc-rag.test.js +0 -162
  128. package/__tests__/categories.test.js +0 -92
  129. package/__tests__/e2e/ai-chat-cross-source-journey.test.js +0 -213
  130. package/__tests__/e2e/full-user-journey.test.js +0 -188
  131. package/__tests__/e2e/local-data-adapters-cli.e2e.test.js +0 -146
  132. package/__tests__/entity-resolver-ingest-hook.test.js +0 -177
  133. package/__tests__/entity-resolver-stages.test.js +0 -411
  134. package/__tests__/entity-resolver-vault.test.js +0 -249
  135. package/__tests__/entity-resolver.test.js +0 -526
  136. package/__tests__/fitness-keep-snapshot.test.js +0 -224
  137. package/__tests__/fixtures/entity-resolver-200-mock.json +0 -96
  138. package/__tests__/ids.test.js +0 -45
  139. package/__tests__/integration/ai-chat-history-registry.test.js +0 -228
  140. package/__tests__/integration/aichat-wizard-end-to-end.test.js +0 -282
  141. package/__tests__/integration/cross-adapter-pipelines.test.js +0 -396
  142. package/__tests__/integration/local-data-adapters-pipeline.test.js +0 -373
  143. package/__tests__/integration/social-bilibili-pipeline.test.js +0 -261
  144. package/__tests__/integration/wechat-bootstrap-end-to-end.test.js +0 -390
  145. package/__tests__/key-providers.test.js +0 -126
  146. package/__tests__/kg-derive.test.js +0 -219
  147. package/__tests__/llm-client.test.js +0 -122
  148. package/__tests__/longtail-adapters.test.js +0 -281
  149. package/__tests__/messaging-qq-snapshot.test.js +0 -294
  150. package/__tests__/mobile-extractor-encrypted.test.js +0 -460
  151. package/__tests__/mobile-extractor.test.js +0 -288
  152. package/__tests__/mock-adapter.test.js +0 -93
  153. package/__tests__/prompt-builder.test.js +0 -249
  154. package/__tests__/query-parser.test.js +0 -365
  155. package/__tests__/rag-derive.test.js +0 -169
  156. package/__tests__/registry-readiness.test.js +0 -292
  157. package/__tests__/registry.test.js +0 -420
  158. package/__tests__/salvage-ingest.test.js +0 -97
  159. package/__tests__/schemas.test.js +0 -331
  160. package/__tests__/shopping-adapters.test.js +0 -392
  161. package/__tests__/shopping-eleme-snapshot.test.js +0 -454
  162. package/__tests__/shopping-pinduoduo-snapshot.test.js +0 -484
  163. package/__tests__/shopping-snapshot.test.js +0 -438
  164. package/__tests__/shopping-vipshop-snapshot.test.js +0 -425
  165. package/__tests__/shopping-xianyu-snapshot.test.js +0 -451
  166. package/__tests__/sidecar-contacts-cross-validate.test.js +0 -186
  167. package/__tests__/sidecar-supervisor.test.js +0 -128
  168. package/__tests__/sign-providers.test.js +0 -62
  169. package/__tests__/social-adapters.test.js +0 -280
  170. package/__tests__/social-bilibili-snapshot.test.js +0 -278
  171. package/__tests__/social-douban-snapshot.test.js +0 -351
  172. package/__tests__/social-douyin-im-direct-read.test.js +0 -377
  173. package/__tests__/social-douyin-salvage-collector.test.js +0 -98
  174. package/__tests__/social-douyin-salvage-mapper.test.js +0 -90
  175. package/__tests__/social-douyin-snapshot.test.js +0 -256
  176. package/__tests__/social-kuaishou-snapshot.test.js +0 -362
  177. package/__tests__/social-toutiao-snapshot.test.js +0 -366
  178. package/__tests__/social-weibo-snapshot.test.js +0 -234
  179. package/__tests__/social-weibo-sqlite-device.test.js +0 -174
  180. package/__tests__/social-xiaohongshu-snapshot.test.js +0 -232
  181. package/__tests__/sqlite-leaf-salvage.test.js +0 -97
  182. package/__tests__/travel-adapters.test.js +0 -483
  183. package/__tests__/travel-maps-snapshot.test.js +0 -426
  184. package/__tests__/vault-driver-error.test.js +0 -74
  185. package/__tests__/vault-search-helpers.test.js +0 -104
  186. package/__tests__/vault-search.test.js +0 -423
  187. package/__tests__/vault.test.js +0 -767
  188. package/__tests__/wechat-adapter.test.js +0 -594
  189. package/__tests__/whatsapp-adapter.test.js +0 -138
  190. package/scripts/_make-fixture-all.js +0 -126
  191. package/scripts/_make-fixture-contacts.js +0 -84
  192. package/scripts/evaluate-entity-resolver.js +0 -213
  193. package/scripts/run-native-tests-sandbox.sh +0 -55
  194. package/scripts/smoke-phase-5-5.js +0 -196
  195. package/scripts/smoke-phase-5-7.js +0 -181
  196. package/scripts/smoke-system-data-contacts.js +0 -309
  197. package/scripts/smoke-system-data.js +0 -312
  198. package/vitest.config.js +0 -88
@@ -1,144 +0,0 @@
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
- });
@@ -1,519 +0,0 @@
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
- EVENT_SUBTYPES,
20
- CAPTURED_BY,
21
- } = require("../../lib/constants");
22
- const { validatePerson, validateItem, validateEvent } = require("../../lib/schemas");
23
-
24
- let tmpDir;
25
- let snapshotPath;
26
-
27
- function writeSnapshot(obj) {
28
- writeFileSync(snapshotPath, JSON.stringify(obj), "utf-8");
29
- }
30
-
31
- beforeEach(() => {
32
- tmpDir = mkdtempSync(join(tmpdir(), "sda-test-"));
33
- snapshotPath = join(tmpDir, "snapshot.json");
34
- });
35
-
36
- afterEach(() => {
37
- rmSync(tmpDir, { recursive: true, force: true });
38
- });
39
-
40
- describe("SystemDataAndroidAdapter — contract", () => {
41
- it("conforms to PersonalDataAdapter contract", () => {
42
- const adapter = new SystemDataAndroidAdapter();
43
- const r = assertAdapter(adapter);
44
- expect(r).toEqual({ ok: true });
45
- });
46
-
47
- it("exposes stable name + version + capabilities", () => {
48
- const adapter = new SystemDataAndroidAdapter();
49
- expect(adapter.name).toBe(SYSTEM_DATA_ANDROID_NAME);
50
- expect(adapter.version).toBe(SYSTEM_DATA_ANDROID_VERSION);
51
- expect(adapter.extractMode).toBe("device-pull");
52
- // v0.2 bumped sensitivity medium → high when sms/call_log were added
53
- // (real address book + SMS body are firmly PII-grade).
54
- expect(adapter.dataDisclosure.sensitivity).toBe("high");
55
- expect(adapter.dataDisclosure.legalGate).toBe(false);
56
- });
57
- });
58
-
59
- describe("SystemDataAndroidAdapter.authenticate", () => {
60
- it("fails when inputPath missing", async () => {
61
- const adapter = new SystemDataAndroidAdapter();
62
- const r = await adapter.authenticate({});
63
- expect(r.ok).toBe(false);
64
- expect(r.reason).toBe("INPUT_PATH_REQUIRED");
65
- });
66
-
67
- it("fails when inputPath does not exist", async () => {
68
- const adapter = new SystemDataAndroidAdapter();
69
- const r = await adapter.authenticate({ inputPath: join(tmpDir, "does-not-exist.json") });
70
- expect(r.ok).toBe(false);
71
- expect(r.reason).toBe("INPUT_PATH_UNREADABLE");
72
- });
73
-
74
- it("succeeds when inputPath is readable", async () => {
75
- writeSnapshot({ schemaVersion: SNAPSHOT_SCHEMA_VERSION, contacts: [], apps: [] });
76
- const adapter = new SystemDataAndroidAdapter();
77
- const r = await adapter.authenticate({ inputPath: snapshotPath });
78
- expect(r).toEqual({ ok: true, mode: "snapshot-file" });
79
- });
80
- });
81
-
82
- describe("SystemDataAndroidAdapter.sync + normalize", () => {
83
- it("rejects mismatched snapshot schemaVersion", async () => {
84
- writeSnapshot({ schemaVersion: 99, contacts: [], apps: [] });
85
- const adapter = new SystemDataAndroidAdapter();
86
- await expect(async () => {
87
- // eslint-disable-next-line no-unused-vars
88
- for await (const _ of adapter.sync({ inputPath: snapshotPath })) {
89
- // drain
90
- }
91
- }).rejects.toThrow(/schemaVersion mismatch/);
92
- });
93
-
94
- it("yields contact + app raws and normalizes each into a valid entity", async () => {
95
- writeSnapshot({
96
- schemaVersion: SNAPSHOT_SCHEMA_VERSION,
97
- snapshottedAt: 1_700_000_000_000,
98
- contacts: [
99
- {
100
- lookupKey: "0r1-3A2B",
101
- displayName: "妈妈",
102
- phones: ["+8613800138000"],
103
- emails: [],
104
- starred: true,
105
- organization: "家庭",
106
- },
107
- ],
108
- apps: [
109
- {
110
- packageName: "com.tencent.mm",
111
- label: "微信",
112
- versionName: "8.0.45",
113
- versionCode: 2200,
114
- firstInstallTime: 1_650_000_000_000,
115
- lastUpdateTime: 1_700_000_000_000,
116
- isSystem: false,
117
- },
118
- ],
119
- });
120
- const adapter = new SystemDataAndroidAdapter();
121
-
122
- // Every yielded raw must carry originalId so registry.putRawEvent's NOT
123
- // NULL constraint doesn't surface as invalidCount=rawCount (real-device
124
- // bug 2026-05-21). Capture all raws to assert structure first.
125
- const rawsSeen = [];
126
- for await (const r of adapter.sync({ inputPath: snapshotPath })) {
127
- rawsSeen.push(r);
128
- }
129
- expect(rawsSeen[0].originalId).toBe("android-contact:0r1-3A2B");
130
- expect(rawsSeen[1].originalId).toBe("android-app:com.tencent.mm");
131
-
132
- const persons = [];
133
- const items = [];
134
- const events = [];
135
- for await (const raw of adapter.sync({ inputPath: snapshotPath })) {
136
- const batch = adapter.normalize(raw);
137
- persons.push(...batch.persons);
138
- items.push(...batch.items);
139
- events.push(...batch.events);
140
- }
141
-
142
- // v0.3.1 — one synthetic event per contact + per app so they show up
143
- // under the Vault Browser's `category=system` facet (events table).
144
- expect(events).toHaveLength(2);
145
- expect(events.map((e) => e.id)).toEqual([
146
- "event-android-contact-0r1-3A2B",
147
- "event-android-app-com.tencent.mm",
148
- ]);
149
- expect(events[0].subtype).toBe(EVENT_SUBTYPES.OTHER);
150
- expect(events[0].content.title).toBe("联系人:妈妈");
151
- expect(events[0].extra.kind).toBe("contact-snapshot");
152
- // v0.3.2 — contact event.extra carries identifying fields so detail
153
- // sheet doesn't have to join the persons table.
154
- expect(events[0].extra.phones).toEqual(["+8613800138000"]);
155
- expect(events[0].extra.organization).toBe("家庭");
156
- expect(events[0].extra.starred).toBe(true);
157
- expect(events[1].content.title).toBe("应用:微信");
158
- expect(events[1].extra.kind).toBe("app-snapshot");
159
- expect(events[1].extra.packageName).toBe("com.tencent.mm");
160
- // v0.3.2 — app event.extra carries version + install fields.
161
- expect(events[1].extra.versionName).toBe("8.0.45");
162
- expect(events[1].extra.versionCode).toBe(2200);
163
- expect(events[1].extra.firstInstallTime).toBe(1_650_000_000_000);
164
- expect(events[1].extra.isSystem).toBe(false);
165
- events.forEach((e) => {
166
- const ev = validateEvent(e);
167
- expect(ev).toEqual({ valid: true, errors: [] });
168
- });
169
-
170
- expect(persons).toHaveLength(1);
171
- const p = persons[0];
172
- expect(p.id).toBe("person-android-0r1-3A2B");
173
- expect(p.type).toBe(ENTITY_TYPES.PERSON);
174
- expect(p.subtype).toBe(PERSON_SUBTYPES.CONTACT);
175
- expect(p.names).toEqual(["妈妈"]);
176
- expect(p.identifiers).toEqual({ phone: ["+8613800138000"] });
177
- expect(p.extra).toEqual({ starred: true });
178
- expect(p.relation).toBe("家庭");
179
- expect(p.source.adapter).toBe(SYSTEM_DATA_ANDROID_NAME);
180
- expect(p.source.capturedBy).toBe(CAPTURED_BY.API);
181
- expect(p.source.capturedAt).toBe(1_700_000_000_000);
182
- const pv = validatePerson(p);
183
- expect(pv).toEqual({ valid: true, errors: [] });
184
-
185
- expect(items).toHaveLength(1);
186
- const i = items[0];
187
- expect(i.id).toBe("item-android-app-com.tencent.mm");
188
- expect(i.type).toBe(ENTITY_TYPES.ITEM);
189
- expect(i.subtype).toBe(ITEM_SUBTYPES.OTHER);
190
- expect(i.name).toBe("微信");
191
- expect(i.category).toBe("user-app");
192
- expect(i.extra.kind).toBe("installed_app");
193
- expect(i.extra.packageName).toBe("com.tencent.mm");
194
- expect(i.extra.versionCode).toBe(2200);
195
- expect(i.extra.firstInstallTime).toBe(1_650_000_000_000);
196
- expect(i.extra.isSystem).toBe(false);
197
- const iv = validateItem(i);
198
- expect(iv).toEqual({ valid: true, errors: [] });
199
- });
200
-
201
- it("dedupes by stable lookupKey — same key across syncs returns same id", async () => {
202
- writeSnapshot({
203
- schemaVersion: SNAPSHOT_SCHEMA_VERSION,
204
- contacts: [{ lookupKey: "abc", displayName: "Alice", phones: [], emails: [] }],
205
- apps: [],
206
- });
207
- const adapter = new SystemDataAndroidAdapter();
208
- const ids = [];
209
- for (let pass = 0; pass < 2; pass++) {
210
- for await (const raw of adapter.sync({ inputPath: snapshotPath })) {
211
- const batch = adapter.normalize(raw);
212
- if (batch.persons[0]) ids.push(batch.persons[0].id);
213
- }
214
- }
215
- expect(ids).toEqual(["person-android-abc", "person-android-abc"]);
216
- });
217
-
218
- it("falls back to displayName when lookupKey missing", async () => {
219
- writeSnapshot({
220
- schemaVersion: SNAPSHOT_SCHEMA_VERSION,
221
- contacts: [{ displayName: "Bob", phones: [], emails: [] }],
222
- apps: [],
223
- });
224
- const adapter = new SystemDataAndroidAdapter();
225
- let id;
226
- for await (const raw of adapter.sync({ inputPath: snapshotPath })) {
227
- id = adapter.normalize(raw).persons[0]?.id;
228
- }
229
- expect(id).toBe("person-android-Bob");
230
- });
231
-
232
- it("honors include filter — apps only", async () => {
233
- writeSnapshot({
234
- schemaVersion: SNAPSHOT_SCHEMA_VERSION,
235
- contacts: [{ lookupKey: "k", displayName: "X", phones: [], emails: [] }],
236
- apps: [
237
- { packageName: "com.x", label: "X", versionName: "1", versionCode: 1, isSystem: false },
238
- ],
239
- });
240
- const adapter = new SystemDataAndroidAdapter();
241
- const kinds = [];
242
- for await (const raw of adapter.sync({
243
- inputPath: snapshotPath,
244
- include: { contacts: false, apps: true },
245
- })) {
246
- kinds.push(raw.kind);
247
- }
248
- expect(kinds).toEqual(["app"]);
249
- });
250
-
251
- it("honors limit option", async () => {
252
- writeSnapshot({
253
- schemaVersion: SNAPSHOT_SCHEMA_VERSION,
254
- contacts: [
255
- { lookupKey: "a", displayName: "A", phones: [], emails: [] },
256
- { lookupKey: "b", displayName: "B", phones: [], emails: [] },
257
- { lookupKey: "c", displayName: "C", phones: [], emails: [] },
258
- ],
259
- apps: [],
260
- });
261
- const adapter = new SystemDataAndroidAdapter();
262
- let count = 0;
263
- for await (const _raw of adapter.sync({ inputPath: snapshotPath, limit: 2 })) {
264
- count += 1;
265
- }
266
- expect(count).toBe(2);
267
- });
268
- });
269
-
270
- describe("SystemDataAndroidAdapter — degenerate snapshots", () => {
271
- it("anonymous contact gets placeholder name", () => {
272
- const adapter = new SystemDataAndroidAdapter();
273
- const batch = adapter.normalize({
274
- kind: "contact",
275
- capturedAt: 1_700_000_000_000,
276
- payload: { lookupKey: "x" },
277
- });
278
- expect(batch.persons[0].names).toEqual(["(无名联系人)"]);
279
- });
280
-
281
- it("system app gets category 'system-app'", () => {
282
- const adapter = new SystemDataAndroidAdapter();
283
- const batch = adapter.normalize({
284
- kind: "app",
285
- capturedAt: 1_700_000_000_000,
286
- payload: { packageName: "com.android.settings", label: "Settings", isSystem: true },
287
- });
288
- expect(batch.items[0].category).toBe("system-app");
289
- expect(batch.items[0].extra.isSystem).toBe(true);
290
- });
291
-
292
- it("throws on unknown raw.kind", () => {
293
- const adapter = new SystemDataAndroidAdapter();
294
- expect(() =>
295
- adapter.normalize({ kind: "mystery", capturedAt: 1, payload: {} })
296
- ).toThrow(/unknown raw.kind/);
297
- });
298
- });
299
-
300
- // ─── Bridge-direct mode (A8 follow-up) ────────────────────────────────
301
- //
302
- // When `cc-android-bridge` is loaded inside in-APK cc, the adapter can pull
303
- // directly from ContentResolver / PackageManager instead of waiting for the
304
- // UI to write a snapshot file. These tests inject a mock bridge to exercise
305
- // the new path without a real device.
306
-
307
- describe("SystemDataAndroidAdapter — bridge-direct sync", () => {
308
- // Helper accepts every kind the adapter may invoke today (v0.3.0 added
309
- // sms / call / media). Unspecified kinds return empty so existing tests
310
- // only need to opt-in to the kinds they assert against.
311
- function makeBridge({
312
- contacts = [],
313
- apps = [],
314
- sms = [],
315
- calls = [],
316
- media = {},
317
- throws = null,
318
- } = {}) {
319
- return {
320
- caps: () => ({ available: true, reason: "test" }),
321
- invoke: async (method, params) => {
322
- if (throws) throw throws;
323
- if (method === "contacts.query") return { contacts };
324
- if (method === "app.list") return { apps };
325
- if (method === "sms.query") return { sms };
326
- if (method === "call.query") return { calls };
327
- if (method === "media.list") {
328
- const cat = params && params.category;
329
- return { files: media[cat] || [] };
330
- }
331
- throw new Error("unexpected method " + method);
332
- },
333
- };
334
- }
335
-
336
- it("useBridge=true pulls contacts and apps via bridge.invoke", async () => {
337
- const adapter = new SystemDataAndroidAdapter();
338
- const bridge = makeBridge({
339
- contacts: [
340
- { lookupKey: "ck-1", displayName: "妈妈", phones: ["13800000001"] },
341
- { lookupKey: "ck-2", displayName: "同事" },
342
- ],
343
- apps: [
344
- { packageName: "com.tencent.mm", label: "微信", versionName: "8.0", isSystem: false },
345
- ],
346
- });
347
- adapter._deps.bridgeProvider = () => bridge;
348
-
349
- const out = [];
350
- for await (const r of adapter.sync({ useBridge: true })) out.push(r);
351
- expect(out).toHaveLength(3);
352
- expect(out[0].kind).toBe("contact");
353
- expect(out[0].payload.displayName).toBe("妈妈");
354
- expect(out[2].kind).toBe("app");
355
- expect(out[2].payload.packageName).toBe("com.tencent.mm");
356
- });
357
-
358
- it("auto-engages bridge when inputPath omitted AND bridge.available", async () => {
359
- const adapter = new SystemDataAndroidAdapter();
360
- adapter._deps.bridgeProvider = () => makeBridge({
361
- contacts: [{ lookupKey: "auto", displayName: "Auto" }],
362
- });
363
- const out = [];
364
- for await (const r of adapter.sync({})) out.push(r);
365
- expect(out).toHaveLength(1);
366
- expect(out[0].payload.displayName).toBe("Auto");
367
- });
368
-
369
- it("respects include.contacts=false in bridge mode", async () => {
370
- const adapter = new SystemDataAndroidAdapter();
371
- adapter._deps.bridgeProvider = () => makeBridge({
372
- contacts: [{ displayName: "should be skipped" }],
373
- apps: [{ packageName: "com.kept" }],
374
- });
375
- const out = [];
376
- for await (const r of adapter.sync({ useBridge: true, include: { contacts: false } })) {
377
- out.push(r);
378
- }
379
- expect(out).toHaveLength(1);
380
- expect(out[0].kind).toBe("app");
381
- });
382
-
383
- it("limit caps bridge yields", async () => {
384
- const adapter = new SystemDataAndroidAdapter();
385
- const many = Array.from({ length: 10 }, (_, i) => ({ displayName: `c${i}` }));
386
- adapter._deps.bridgeProvider = () => makeBridge({ contacts: many });
387
- const out = [];
388
- for await (const r of adapter.sync({ useBridge: true, limit: 3 })) out.push(r);
389
- expect(out).toHaveLength(3);
390
- });
391
-
392
- it("throws when useBridge=true but bridge missing", async () => {
393
- const adapter = new SystemDataAndroidAdapter();
394
- adapter._deps.bridgeProvider = () => null;
395
- const it = adapter.sync({ useBridge: true });
396
- await expect(it.next()).rejects.toThrow(/cc-android-bridge is not loaded/);
397
- });
398
-
399
- it("propagates bridge.invoke errors", async () => {
400
- const adapter = new SystemDataAndroidAdapter();
401
- adapter._deps.bridgeProvider = () =>
402
- makeBridge({ throws: new Error("ANDROID_BRIDGE_NOT_AVAILABLE: not-on-android") });
403
- const it = adapter.sync({ useBridge: true });
404
- await expect(it.next()).rejects.toThrow(/ANDROID_BRIDGE_NOT_AVAILABLE/);
405
- });
406
-
407
- it("accepts bare array response from bridge (contacts as Array)", async () => {
408
- const adapter = new SystemDataAndroidAdapter();
409
- adapter._deps.bridgeProvider = () => ({
410
- caps: () => ({ available: true }),
411
- invoke: async (method) => {
412
- if (method === "contacts.query") return [{ displayName: "Bare" }];
413
- if (method === "app.list") return [];
414
- return [];
415
- },
416
- });
417
- const out = [];
418
- for await (const r of adapter.sync({ useBridge: true })) out.push(r);
419
- expect(out).toHaveLength(1);
420
- expect(out[0].payload.displayName).toBe("Bare");
421
- });
422
-
423
- it("inputPath snapshot mode still works when bridgeProvider returns null", async () => {
424
- writeSnapshot({
425
- schemaVersion: SNAPSHOT_SCHEMA_VERSION,
426
- snapshottedAt: 1_700_000_000_000,
427
- contacts: [{ lookupKey: "snap", displayName: "From Snapshot" }],
428
- apps: [],
429
- });
430
- const adapter = new SystemDataAndroidAdapter();
431
- const out = [];
432
- for await (const r of adapter.sync({ inputPath: snapshotPath })) out.push(r);
433
- expect(out).toHaveLength(1);
434
- expect(out[0].payload.displayName).toBe("From Snapshot");
435
- });
436
-
437
- // v0.2 — sms/call bridge methods. Validate yields + originalId stability.
438
- it("v0.2 bridge yields sms + call rows when present", async () => {
439
- const adapter = new SystemDataAndroidAdapter();
440
- adapter._deps.bridgeProvider = () =>
441
- makeBridge({
442
- sms: [{ id: 7, address: "10086", body: "余额: ¥10", date: 1_700_000_000_000, type: 1 }],
443
- calls: [{ id: 9, number: "13900000000", duration: 12, date: 1_700_000_001_000, type: 2 }],
444
- });
445
- const out = [];
446
- for await (const r of adapter.sync({ useBridge: true })) out.push(r);
447
- expect(out.map((r) => r.kind)).toEqual(["sms", "call"]);
448
- expect(out[0].originalId).toBe("android-sms:7");
449
- expect(out[1].originalId).toBe("android-call:9");
450
- });
451
-
452
- it("v0.2 include.sms=false / include.calls=false honoured per-kind", async () => {
453
- const adapter = new SystemDataAndroidAdapter();
454
- adapter._deps.bridgeProvider = () =>
455
- makeBridge({
456
- sms: [{ id: 1, address: "x", body: "y", date: 1, type: 1 }],
457
- calls: [{ id: 2, number: "x", duration: 0, date: 1, type: 1 }],
458
- });
459
- const noSms = [];
460
- for await (const r of adapter.sync({ useBridge: true, include: { sms: false } })) noSms.push(r);
461
- expect(noSms.map((r) => r.kind)).toEqual(["call"]);
462
- const noCalls = [];
463
- for await (const r of adapter.sync({ useBridge: true, include: { calls: false } })) noCalls.push(r);
464
- expect(noCalls.map((r) => r.kind)).toEqual(["sms"]);
465
- });
466
-
467
- // v0.3 — media files across 5 /sdcard categories.
468
- it("v0.3 bridge yields media-file per category with stable originalId", async () => {
469
- const adapter = new SystemDataAndroidAdapter();
470
- adapter._deps.bridgeProvider = () =>
471
- makeBridge({
472
- media: {
473
- photos: [{ path: "/sdcard/DCIM/Camera/img.jpg", size: 1234, mtimeMs: 1, ext: "jpg", category: "photos" }],
474
- videos: [{ path: "/sdcard/Movies/m.mp4", size: 999, mtimeMs: 2, ext: "mp4", category: "videos" }],
475
- },
476
- });
477
- const out = [];
478
- for await (const r of adapter.sync({ useBridge: true })) out.push(r);
479
- expect(out.map((r) => r.kind)).toEqual(["media-file", "media-file"]);
480
- expect(out[0].originalId).toBe("android-media:/sdcard/DCIM/Camera/img.jpg");
481
- expect(out[1].originalId).toBe("android-media:/sdcard/Movies/m.mp4");
482
- });
483
-
484
- it("v0.3 include.media=false disables ALL media categories", async () => {
485
- const adapter = new SystemDataAndroidAdapter();
486
- adapter._deps.bridgeProvider = () =>
487
- makeBridge({
488
- media: {
489
- photos: [{ path: "/p.jpg", category: "photos" }],
490
- videos: [{ path: "/v.mp4", category: "videos" }],
491
- },
492
- });
493
- const out = [];
494
- for await (const r of adapter.sync({ useBridge: true, include: { media: false } })) {
495
- out.push(r);
496
- }
497
- expect(out).toHaveLength(0);
498
- });
499
-
500
- it("v0.3 include.media.photos=false skips just photos, keeps videos", async () => {
501
- const adapter = new SystemDataAndroidAdapter();
502
- adapter._deps.bridgeProvider = () =>
503
- makeBridge({
504
- media: {
505
- photos: [{ path: "/p.jpg", category: "photos" }],
506
- videos: [{ path: "/v.mp4", category: "videos" }],
507
- },
508
- });
509
- const out = [];
510
- for await (const r of adapter.sync({
511
- useBridge: true,
512
- include: { media: { photos: false } },
513
- })) {
514
- out.push(r);
515
- }
516
- expect(out).toHaveLength(1);
517
- expect(out[0].payload.category).toBe("videos");
518
- });
519
- });