@chainlesschain/personal-data-hub 0.4.29 → 0.4.31

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 (199) hide show
  1. package/lib/forensics/qq-nt-collect.js +190 -0
  2. package/lib/prompt-builder.js +15 -1
  3. package/package.json +8 -3
  4. package/__tests__/adapter-guide.test.js +0 -47
  5. package/__tests__/adapter-spec.test.js +0 -78
  6. package/__tests__/adapters/ai-chat-cookie-capture-spec.test.js +0 -211
  7. package/__tests__/adapters/ai-chat-health-checker.test.js +0 -262
  8. package/__tests__/adapters/ai-chat-history.test.js +0 -396
  9. package/__tests__/adapters/ai-chat-http-client.test.js +0 -242
  10. package/__tests__/adapters/ai-chat-vendors.test.js +0 -874
  11. package/__tests__/adapters/alipay-bill-adapter.test.js +0 -538
  12. package/__tests__/adapters/apple-health.test.js +0 -95
  13. package/__tests__/adapters/bank-family.test.js +0 -125
  14. package/__tests__/adapters/biz-tianyancha.test.js +0 -159
  15. package/__tests__/adapters/browser-history-chrome.test.js +0 -377
  16. package/__tests__/adapters/browser-history-edge.test.js +0 -159
  17. package/__tests__/adapters/car-mercedesme.test.js +0 -74
  18. package/__tests__/adapters/doc-baidu-netdisk.test.js +0 -102
  19. package/__tests__/adapters/doc-camscanner.test.js +0 -147
  20. package/__tests__/adapters/doc-platforms.test.js +0 -177
  21. package/__tests__/adapters/edu-huawei-learning-live.test.js +0 -198
  22. package/__tests__/adapters/edu-zuoyebang-live.test.js +0 -226
  23. package/__tests__/adapters/email-adapter-snapshot.test.js +0 -237
  24. package/__tests__/adapters/email-adapter.test.js +0 -742
  25. package/__tests__/adapters/email-classifier.test.js +0 -347
  26. package/__tests__/adapters/email-imap-session.test.js +0 -334
  27. package/__tests__/adapters/email-parser.test.js +0 -244
  28. package/__tests__/adapters/email-pdf-extractor.test.js +0 -529
  29. package/__tests__/adapters/email-providers.test.js +0 -84
  30. package/__tests__/adapters/email-retry-progress.test.js +0 -294
  31. package/__tests__/adapters/email-templates.test.js +0 -822
  32. package/__tests__/adapters/family-23-collectors-scaffold.test.js +0 -182
  33. package/__tests__/adapters/finance-alipay-live.test.js +0 -258
  34. package/__tests__/adapters/finance-dcep.test.js +0 -74
  35. package/__tests__/adapters/fitness-joyrun.test.js +0 -82
  36. package/__tests__/adapters/game-genshin-live.test.js +0 -238
  37. package/__tests__/adapters/game-genshin-scaffold.test.js +0 -108
  38. package/__tests__/adapters/game-honor-of-kings-live.test.js +0 -230
  39. package/__tests__/adapters/git-activity.test.js +0 -222
  40. package/__tests__/adapters/gov-12123.test.js +0 -103
  41. package/__tests__/adapters/gov-ixiamen.test.js +0 -150
  42. package/__tests__/adapters/gov-tax.test.js +0 -135
  43. package/__tests__/adapters/health-meiyou.test.js +0 -125
  44. package/__tests__/adapters/local-files.test.js +0 -264
  45. package/__tests__/adapters/local-im-pc.test.js +0 -154
  46. package/__tests__/adapters/messaging-whatsapp.test.js +0 -289
  47. package/__tests__/adapters/music-kugou.test.js +0 -187
  48. package/__tests__/adapters/music-qq.test.js +0 -112
  49. package/__tests__/adapters/netease-music-live.test.js +0 -244
  50. package/__tests__/adapters/netease-music.test.js +0 -74
  51. package/__tests__/adapters/pc-local-discovery.test.js +0 -141
  52. package/__tests__/adapters/qq-pc-direct-read.test.js +0 -227
  53. package/__tests__/adapters/reading-family.test.js +0 -108
  54. package/__tests__/adapters/recruit-boss.test.js +0 -180
  55. package/__tests__/adapters/shell-history.test.js +0 -180
  56. package/__tests__/adapters/shopping-base.test.js +0 -179
  57. package/__tests__/adapters/shopping-dianping.test.js +0 -239
  58. package/__tests__/adapters/social-bilibili-adb-api-client.test.js +0 -721
  59. package/__tests__/adapters/social-bilibili-adb-chromium-cookies-reader.test.js +0 -346
  60. package/__tests__/adapters/social-bilibili-adb-collector.test.js +0 -284
  61. package/__tests__/adapters/social-bilibili-adb-cookies-extension.test.js +0 -343
  62. package/__tests__/adapters/social-bilibili-adb-snapshot-builder.test.js +0 -296
  63. package/__tests__/adapters/social-csdn.test.js +0 -175
  64. package/__tests__/adapters/social-dongchedi.test.js +0 -165
  65. package/__tests__/adapters/social-douyin-adb-aweme-detail.test.js +0 -165
  66. package/__tests__/adapters/social-douyin-adb-collector.test.js +0 -254
  67. package/__tests__/adapters/social-douyin-adb-db-extension.test.js +0 -114
  68. package/__tests__/adapters/social-douyin-adb-im-db-parser.test.js +0 -304
  69. package/__tests__/adapters/social-douyin-adb-snapshot-builder.test.js +0 -216
  70. package/__tests__/adapters/social-douyin-adb-usage-profile.test.js +0 -229
  71. package/__tests__/adapters/social-douyin-adb-watch-history.test.js +0 -269
  72. package/__tests__/adapters/social-kuaishou-adb-api-client.test.js +0 -496
  73. package/__tests__/adapters/social-kuaishou-adb-collector.test.js +0 -276
  74. package/__tests__/adapters/social-kuaishou-adb-cookies-extension.test.js +0 -152
  75. package/__tests__/adapters/social-kuaishou-adb-snapshot-builder.test.js +0 -178
  76. package/__tests__/adapters/social-toutiao-adb-account-reader.test.js +0 -135
  77. package/__tests__/adapters/social-toutiao-adb-api-client.test.js +0 -626
  78. package/__tests__/adapters/social-toutiao-adb-article.test.js +0 -155
  79. package/__tests__/adapters/social-toutiao-adb-collector.test.js +0 -378
  80. package/__tests__/adapters/social-toutiao-adb-cookies-extension.test.js +0 -193
  81. package/__tests__/adapters/social-toutiao-adb-snapshot-builder.test.js +0 -196
  82. package/__tests__/adapters/social-toutiao-kuaishou-scaffold.test.js +0 -311
  83. package/__tests__/adapters/social-weibo-adb-api-client.test.js +0 -362
  84. package/__tests__/adapters/social-weibo-adb-collector.test.js +0 -201
  85. package/__tests__/adapters/social-weibo-adb-cookies-extension.test.js +0 -167
  86. package/__tests__/adapters/social-weibo-adb-snapshot-builder.test.js +0 -189
  87. package/__tests__/adapters/social-xiaohongshu-adb-api-client.test.js +0 -431
  88. package/__tests__/adapters/social-xiaohongshu-adb-collector.test.js +0 -207
  89. package/__tests__/adapters/social-xiaohongshu-adb-cookies-extension.test.js +0 -0
  90. package/__tests__/adapters/social-xiaohongshu-adb-sign-provider-injection.test.js +0 -351
  91. package/__tests__/adapters/social-xiaohongshu-adb-sign.test.js +0 -130
  92. package/__tests__/adapters/social-xiaohongshu-adb-snapshot-builder.test.js +0 -200
  93. package/__tests__/adapters/social-zhihu.test.js +0 -246
  94. package/__tests__/adapters/system-data-adapter.test.js +0 -443
  95. package/__tests__/adapters/system-data-android-ingest.test.js +0 -144
  96. package/__tests__/adapters/system-data-android.test.js +0 -519
  97. package/__tests__/adapters/system-data-disclosure.test.js +0 -153
  98. package/__tests__/adapters/travel-12306.test.js +0 -512
  99. package/__tests__/adapters/travel-amap.test.js +0 -219
  100. package/__tests__/adapters/travel-baidu-map.test.js +0 -305
  101. package/__tests__/adapters/travel-base.test.js +0 -205
  102. package/__tests__/adapters/travel-ctrip.test.js +0 -377
  103. package/__tests__/adapters/travel-didi-consumer.test.js +0 -66
  104. package/__tests__/adapters/travel-didi.test.js +0 -204
  105. package/__tests__/adapters/travel-tencent-map.test.js +0 -207
  106. package/__tests__/adapters/travel-tongcheng.test.js +0 -289
  107. package/__tests__/adapters/video-platforms.test.js +0 -152
  108. package/__tests__/adapters/video-xigua.test.js +0 -106
  109. package/__tests__/adapters/vscode.test.js +0 -299
  110. package/__tests__/adapters/wechat-bootstrap.test.js +0 -240
  111. package/__tests__/adapters/wechat-env-probe.test.js +0 -162
  112. package/__tests__/adapters/wechat-frida-agent.test.js +0 -322
  113. package/__tests__/adapters/wechat-frida-integration.test.js +0 -149
  114. package/__tests__/adapters/wechat-frida-key-provider.test.js +0 -188
  115. package/__tests__/adapters/wechat-md5-key-provider.test.js +0 -101
  116. package/__tests__/adapters/wechat-pc-direct-read.test.js +0 -365
  117. package/__tests__/adapters/wechat-pc-group-topic.test.js +0 -63
  118. package/__tests__/adapters/wechat-pc-v4-sidecar.test.js +0 -72
  119. package/__tests__/adapters/weread.test.js +0 -123
  120. package/__tests__/adapters/wework-pc.test.js +0 -124
  121. package/__tests__/adapters/win-recent.test.js +0 -192
  122. package/__tests__/analysis-skills.test.js +0 -754
  123. package/__tests__/analysis.test.js +0 -1845
  124. package/__tests__/audio-ximalaya-snapshot.test.js +0 -279
  125. package/__tests__/batch.test.js +0 -133
  126. package/__tests__/bridges-cc-kg.test.js +0 -231
  127. package/__tests__/bridges-cc-llm.test.js +0 -191
  128. package/__tests__/bridges-cc-rag.test.js +0 -162
  129. package/__tests__/categories.test.js +0 -92
  130. package/__tests__/e2e/ai-chat-cross-source-journey.test.js +0 -213
  131. package/__tests__/e2e/full-user-journey.test.js +0 -188
  132. package/__tests__/e2e/local-data-adapters-cli.e2e.test.js +0 -146
  133. package/__tests__/entity-resolver-ingest-hook.test.js +0 -177
  134. package/__tests__/entity-resolver-stages.test.js +0 -411
  135. package/__tests__/entity-resolver-vault.test.js +0 -249
  136. package/__tests__/entity-resolver.test.js +0 -526
  137. package/__tests__/fitness-keep-snapshot.test.js +0 -224
  138. package/__tests__/fixtures/entity-resolver-200-mock.json +0 -96
  139. package/__tests__/ids.test.js +0 -45
  140. package/__tests__/integration/ai-chat-history-registry.test.js +0 -228
  141. package/__tests__/integration/aichat-wizard-end-to-end.test.js +0 -282
  142. package/__tests__/integration/cross-adapter-pipelines.test.js +0 -396
  143. package/__tests__/integration/local-data-adapters-pipeline.test.js +0 -373
  144. package/__tests__/integration/social-bilibili-pipeline.test.js +0 -261
  145. package/__tests__/integration/wechat-bootstrap-end-to-end.test.js +0 -390
  146. package/__tests__/key-providers.test.js +0 -126
  147. package/__tests__/kg-derive.test.js +0 -219
  148. package/__tests__/llm-client.test.js +0 -122
  149. package/__tests__/longtail-adapters.test.js +0 -281
  150. package/__tests__/messaging-qq-snapshot.test.js +0 -294
  151. package/__tests__/mobile-extractor-encrypted.test.js +0 -460
  152. package/__tests__/mobile-extractor.test.js +0 -288
  153. package/__tests__/mock-adapter.test.js +0 -93
  154. package/__tests__/prompt-builder.test.js +0 -249
  155. package/__tests__/query-parser.test.js +0 -365
  156. package/__tests__/rag-derive.test.js +0 -169
  157. package/__tests__/registry-readiness.test.js +0 -292
  158. package/__tests__/registry.test.js +0 -420
  159. package/__tests__/salvage-ingest.test.js +0 -97
  160. package/__tests__/schemas.test.js +0 -331
  161. package/__tests__/shopping-adapters.test.js +0 -392
  162. package/__tests__/shopping-eleme-snapshot.test.js +0 -454
  163. package/__tests__/shopping-pinduoduo-snapshot.test.js +0 -484
  164. package/__tests__/shopping-snapshot.test.js +0 -438
  165. package/__tests__/shopping-vipshop-snapshot.test.js +0 -425
  166. package/__tests__/shopping-xianyu-snapshot.test.js +0 -451
  167. package/__tests__/sidecar-contacts-cross-validate.test.js +0 -186
  168. package/__tests__/sidecar-supervisor.test.js +0 -128
  169. package/__tests__/sign-providers.test.js +0 -62
  170. package/__tests__/social-adapters.test.js +0 -280
  171. package/__tests__/social-bilibili-snapshot.test.js +0 -278
  172. package/__tests__/social-douban-snapshot.test.js +0 -351
  173. package/__tests__/social-douyin-im-direct-read.test.js +0 -377
  174. package/__tests__/social-douyin-salvage-collector.test.js +0 -98
  175. package/__tests__/social-douyin-salvage-mapper.test.js +0 -90
  176. package/__tests__/social-douyin-snapshot.test.js +0 -256
  177. package/__tests__/social-kuaishou-snapshot.test.js +0 -362
  178. package/__tests__/social-toutiao-snapshot.test.js +0 -366
  179. package/__tests__/social-weibo-snapshot.test.js +0 -234
  180. package/__tests__/social-weibo-sqlite-device.test.js +0 -174
  181. package/__tests__/social-xiaohongshu-snapshot.test.js +0 -232
  182. package/__tests__/sqlite-leaf-salvage.test.js +0 -97
  183. package/__tests__/travel-adapters.test.js +0 -483
  184. package/__tests__/travel-maps-snapshot.test.js +0 -426
  185. package/__tests__/vault-driver-error.test.js +0 -74
  186. package/__tests__/vault-search-helpers.test.js +0 -104
  187. package/__tests__/vault-search.test.js +0 -423
  188. package/__tests__/vault.test.js +0 -767
  189. package/__tests__/wechat-adapter.test.js +0 -594
  190. package/__tests__/whatsapp-adapter.test.js +0 -138
  191. package/scripts/_make-fixture-all.js +0 -126
  192. package/scripts/_make-fixture-contacts.js +0 -84
  193. package/scripts/evaluate-entity-resolver.js +0 -213
  194. package/scripts/run-native-tests-sandbox.sh +0 -55
  195. package/scripts/smoke-phase-5-5.js +0 -196
  196. package/scripts/smoke-phase-5-7.js +0 -181
  197. package/scripts/smoke-system-data-contacts.js +0 -309
  198. package/scripts/smoke-system-data.js +0 -312
  199. package/vitest.config.js +0 -88
@@ -1,128 +0,0 @@
1
- "use strict";
2
-
3
- import { describe, it, expect, beforeEach, afterEach } from "vitest";
4
- import path from "node:path";
5
- import fs from "node:fs";
6
-
7
- const {
8
- SidecarSupervisor,
9
- SidecarTimeoutError,
10
- SidecarMethodError,
11
- SidecarNotRunningError,
12
- } = require("../lib/sidecar");
13
-
14
- const SIDECAR_ROOT = path.resolve(
15
- __dirname,
16
- "..",
17
- "..",
18
- "personal-data-hub-bridge",
19
- );
20
-
21
- /**
22
- * Spawn the forensics-bridge sidecar from this repo's sibling package.
23
- * Tests are skipped if Python is unavailable on PATH (CI matrix coverage
24
- * is the source of truth; local dev without Python should not be blocked).
25
- */
26
- function makeSupervisor() {
27
- return new SidecarSupervisor({
28
- command: process.env.FORENSICS_BRIDGE_PYTHON || "python",
29
- args: ["-u", "-m", "forensics_bridge.ipc_server"],
30
- cwd: SIDECAR_ROOT,
31
- healthCheckIntervalMs: 0, // disable for tests — manual control only
32
- env: { PYTHONPATH: SIDECAR_ROOT },
33
- });
34
- }
35
-
36
- let pythonAvailable = true;
37
- try {
38
- // Cheap synchronous probe — spawn fails if python is not on PATH.
39
- const { spawnSync } = require("node:child_process");
40
- const probe = spawnSync(
41
- process.env.FORENSICS_BRIDGE_PYTHON || "python",
42
- ["--version"],
43
- { stdio: "ignore" },
44
- );
45
- if (probe.status !== 0) pythonAvailable = false;
46
- } catch (_err) {
47
- pythonAvailable = false;
48
- }
49
-
50
- // In the FTS5 sandbox runner the relative ../../personal-data-hub-bridge
51
- // resolves outside the temp tree and the directory does not exist; without
52
- // this gate, spawn() returns ENOENT and the test fails for environment
53
- // reasons rather than code reasons. Keep both gates so the file is safe
54
- // to run in either layout.
55
- const sidecarRootAvailable = fs.existsSync(SIDECAR_ROOT);
56
-
57
- const itPy = pythonAvailable && sidecarRootAvailable ? it : it.skip;
58
-
59
- describe("SidecarSupervisor (forensics-bridge integration)", () => {
60
- let supervisor;
61
-
62
- beforeEach(() => {
63
- supervisor = makeSupervisor();
64
- });
65
-
66
- afterEach(async () => {
67
- if (supervisor) await supervisor.stop({ graceMs: 1500 });
68
- });
69
-
70
- itPy("starts the sidecar and round-trips sidecar.ping", async () => {
71
- await supervisor.start({ readyTimeoutMs: 8_000 });
72
- expect(supervisor.isRunning()).toBe(true);
73
-
74
- const ping = await supervisor.invoke("sidecar.ping", {}, { timeoutMs: 3_000 });
75
- expect(ping.version).toMatch(/^\d+\.\d+\.\d+/);
76
- expect(ping.pythonVersion).toMatch(/^3\./);
77
- });
78
-
79
- itPy("sidecar.capabilities exposes registered methods", async () => {
80
- await supervisor.start({ readyTimeoutMs: 8_000 });
81
-
82
- const caps = await supervisor.invoke("sidecar.capabilities");
83
- expect(caps.methods).toContain("sidecar.ping");
84
- expect(caps.methods).toContain("sidecar.capabilities");
85
- // Namespace registry grows as parsers/extractors land; Phase 4.5.2 brings
86
- // the system parser online.
87
- expect(caps.parsers).toContain("system");
88
- expect(caps.extractors).toContain("android");
89
- });
90
-
91
- itPy("METHOD_NOT_FOUND surfaces as a typed SidecarMethodError", async () => {
92
- await supervisor.start({ readyTimeoutMs: 8_000 });
93
-
94
- await expect(
95
- supervisor.invoke("definitely.not.a.real.method"),
96
- ).rejects.toMatchObject({
97
- name: "SidecarMethodError",
98
- code: "METHOD_NOT_FOUND",
99
- retryable: false,
100
- });
101
- });
102
-
103
- itPy("invoke rejects with SidecarNotRunningError when sidecar is stopped", async () => {
104
- await supervisor.start({ readyTimeoutMs: 8_000 });
105
- await supervisor.stop({ graceMs: 1500 });
106
- expect(supervisor.isRunning()).toBe(false);
107
-
108
- await expect(supervisor.invoke("sidecar.ping")).rejects.toMatchObject({
109
- name: "SidecarNotRunningError",
110
- code: "SIDECAR_NOT_RUNNING",
111
- });
112
- });
113
-
114
- itPy("two sequential invokes share one sidecar process", async () => {
115
- await supervisor.start({ readyTimeoutMs: 8_000 });
116
- const first = await supervisor.invoke("sidecar.ping");
117
- const second = await supervisor.invoke("sidecar.capabilities");
118
- expect(first.version).toBeDefined();
119
- expect(second.methods).toContain("sidecar.ping");
120
- });
121
-
122
- itPy("error class hierarchy is exported", () => {
123
- expect(typeof SidecarSupervisor).toBe("function");
124
- expect(typeof SidecarTimeoutError).toBe("function");
125
- expect(typeof SidecarMethodError).toBe("function");
126
- expect(typeof SidecarNotRunningError).toBe("function");
127
- });
128
- });
@@ -1,62 +0,0 @@
1
- "use strict";
2
-
3
- /**
4
- * Phase 6a — SignProvider abstract + NullSignProvider unit cover.
5
- */
6
-
7
- import { describe, it, expect } from "vitest";
8
-
9
- const {
10
- SignProvider,
11
- NullSignProvider,
12
- NULL_SIGN_PROVIDER,
13
- } = require("../lib/sign-providers");
14
-
15
- describe("SignProvider abstract base", () => {
16
- it("signUrl returns null by default", async () => {
17
- const p = new SignProvider();
18
- expect(await p.signUrl("https://example.com", "purpose")).toBe(null);
19
- });
20
-
21
- it("signedHeaders returns {} by default", async () => {
22
- const p = new SignProvider();
23
- const r = await p.signedHeaders("https://example.com", "purpose");
24
- expect(r).toEqual({});
25
- });
26
-
27
- it("shutdown is no-op (no throw)", async () => {
28
- const p = new SignProvider();
29
- await expect(p.shutdown()).resolves.toBeUndefined();
30
- });
31
- });
32
-
33
- describe("NullSignProvider", () => {
34
- it("extends SignProvider", () => {
35
- const p = new NullSignProvider();
36
- expect(p).toBeInstanceOf(SignProvider);
37
- });
38
-
39
- it("inherits stub signUrl / signedHeaders / shutdown", async () => {
40
- const p = new NullSignProvider();
41
- expect(await p.signUrl("x", "y")).toBe(null);
42
- expect(await p.signedHeaders("x", "y")).toEqual({});
43
- await expect(p.shutdown()).resolves.toBeUndefined();
44
- });
45
-
46
- it("NULL_SIGN_PROVIDER is a frozen singleton", () => {
47
- expect(NULL_SIGN_PROVIDER).toBeInstanceOf(NullSignProvider);
48
- expect(Object.isFrozen(NULL_SIGN_PROVIDER)).toBe(true);
49
- });
50
-
51
- it("api-client pattern: call signUrl + signedHeaders without crash", async () => {
52
- // Simulates api-client usage — signProvider.signUrl(url, purpose) ||
53
- // fall back to unsigned URL; spread signedHeaders into request headers.
54
- const provider = NULL_SIGN_PROVIDER;
55
- const url = "https://api.example.com/x";
56
- const signedUrl = (await provider.signUrl(url, "test")) || url;
57
- expect(signedUrl).toBe(url); // null → fall back to original
58
- const extraHeaders = await provider.signedHeaders(url, "test");
59
- const finalHeaders = { Cookie: "x=y", ...extraHeaders };
60
- expect(finalHeaders).toEqual({ Cookie: "x=y" }); // no extras from null provider
61
- });
62
- });
@@ -1,280 +0,0 @@
1
- "use strict";
2
-
3
- import { describe, it, expect } from "vitest";
4
-
5
- const fs = require("node:fs");
6
- const path = require("node:path");
7
- const os = require("node:os");
8
-
9
- const { BilibiliAdapter, WeiboAdapter } = require("../lib");
10
- const { assertAdapter } = require("../lib/adapter-spec");
11
- const { validateBatch } = require("../lib/batch");
12
-
13
- function makeMockDriver(scriptedRows) {
14
- return function () {
15
- return {
16
- prepare(sql) {
17
- return {
18
- all() {
19
- for (const [matchSubstr, rows] of scriptedRows) {
20
- if (sql.includes(matchSubstr)) return rows;
21
- }
22
- throw new Error("no such table");
23
- },
24
- };
25
- },
26
- close() {},
27
- };
28
- };
29
- }
30
-
31
- // ─── BilibiliAdapter ────────────────────────────────────────────────────
32
-
33
- describe("BilibiliAdapter", () => {
34
- it("contract conformance", () => {
35
- const a = new BilibiliAdapter({ account: { uid: "1234" } });
36
- expect(assertAdapter(a).ok).toBe(true);
37
- expect(a.extractMode).toBe("device-pull");
38
- });
39
-
40
- it("accepts stateless construction (snapshot mode added in A8)", () => {
41
- // Before A8: constructor required opts.account.uid. After A8 the adapter
42
- // is stateless when running snapshot mode (in-APK Android cc reads a JSON
43
- // produced by the phone). Sqlite mode still needs account.uid but the
44
- // check moved into _syncViaSqlite where it actually matters.
45
- expect(() => new BilibiliAdapter({})).not.toThrow();
46
- expect(() => new BilibiliAdapter({ account: {} })).not.toThrow();
47
- expect(() => new BilibiliAdapter()).not.toThrow();
48
- });
49
-
50
- it("sqlite mode rejects missing account.uid at sync time", async () => {
51
- const a = new BilibiliAdapter({ dbPath: "/tmp/bili.db" });
52
- // Path-existence check happens before account.uid validation, so we
53
- // exercise the guard via dbPath=null + account=null which falls to
54
- // "sync needs inputPath OR dbPath" first. Use a real-looking dbPath
55
- // with no account to surface the account.uid throw deterministically.
56
- const fs = require("node:fs");
57
- const path = require("node:path");
58
- const os = require("node:os");
59
- const dir = fs.mkdtempSync(path.join(os.tmpdir(), "bili-no-acct-"));
60
- const dbPath = path.join(dir, "bili.db");
61
- fs.writeFileSync(dbPath, "fake");
62
- try {
63
- const b = new BilibiliAdapter({
64
- dbPath,
65
- dbDriverFactory: () => () => ({
66
- prepare: () => ({ all: () => [] }),
67
- close() {},
68
- }),
69
- });
70
- let threw = null;
71
- try {
72
- for await (const _r of b.sync()) { /* drain */ }
73
- } catch (err) {
74
- threw = err;
75
- }
76
- expect(threw).toBeTruthy();
77
- expect(String(threw.message)).toMatch(/account\.uid/);
78
- } finally {
79
- fs.rmSync(dir, { recursive: true, force: true });
80
- }
81
- });
82
-
83
- it("sync yields history + favourite records via mocked driver", async () => {
84
- const dir = fs.mkdtempSync(path.join(os.tmpdir(), "bili-"));
85
- const dbPath = path.join(dir, "bili.db");
86
- fs.writeFileSync(dbPath, "fake");
87
- try {
88
- const mockDriver = makeMockDriver([
89
- ["FROM history", [
90
- { id: 1, bvid: "BV1abc", title: "趣味视频", view_at: 1700000000, uploader: "UpA" },
91
- { id: 2, bvid: "BV1xyz", title: "教程", view_at: 1700000010, uploader: "UpB", duration: 300 },
92
- ]],
93
- ["FROM bili_favourite", [
94
- { id: 1, bvid: "BV1fav", title: "收藏A", save_time: 1700001000, folder_name: "学习" },
95
- ]],
96
- ]);
97
- const a = new BilibiliAdapter({
98
- account: { uid: "1234" },
99
- dbPath,
100
- dbDriverFactory: () => mockDriver,
101
- });
102
- const raws = [];
103
- for await (const r of a.sync()) raws.push(r);
104
- expect(raws.length).toBe(3); // 2 history + 1 favourite
105
- const histories = raws.filter((r) => r.payload.kind === "history");
106
- const favs = raws.filter((r) => r.payload.kind === "favourite");
107
- expect(histories).toHaveLength(2);
108
- expect(favs).toHaveLength(1);
109
-
110
- // Normalize each
111
- for (const raw of raws) {
112
- const batch = a.normalize(raw);
113
- const v = validateBatch(batch);
114
- expect(v.valid).toBe(true);
115
- const subtype = batch.events[0].subtype;
116
- if (raw.payload.kind === "history") expect(subtype).toBe("browse");
117
- if (raw.payload.kind === "favourite") expect(subtype).toBe("like");
118
- }
119
- } finally {
120
- fs.rmSync(dir, { recursive: true, force: true });
121
- }
122
- });
123
-
124
- it("throws when neither inputPath nor dbPath provided (A8: surface config errors)", async () => {
125
- // Before A8: sync silently yielded 0 if dbPath missing — masked typos and
126
- // misconfigured callers. After A8 we throw so callers see the problem.
127
- const a = new BilibiliAdapter({ account: { uid: "1234" } });
128
- let threw = null;
129
- try {
130
- for await (const _r of a.sync()) { /* drain */ }
131
- } catch (err) {
132
- threw = err;
133
- }
134
- expect(threw).toBeTruthy();
135
- expect(String(threw.message)).toMatch(/inputPath|dbPath/);
136
- });
137
-
138
- it("normalize captures bvid/avid/uploader into extra (flat payload, A8 shape)", async () => {
139
- const a = new BilibiliAdapter({ account: { uid: "1234" } });
140
- const raw = {
141
- adapter: "social-bilibili",
142
- kind: "history",
143
- originalId: "bilibili:history:BV1abc",
144
- capturedAt: 1700000000000,
145
- payload: {
146
- kind: "history",
147
- title: "Test",
148
- bvid: "BV1abc",
149
- avid: "1234",
150
- uploader: "UpA",
151
- duration: 300,
152
- },
153
- };
154
- const batch = a.normalize(raw);
155
- expect(batch.events[0].extra.bvid).toBe("BV1abc");
156
- expect(batch.events[0].extra.avid).toBe("1234");
157
- expect(batch.events[0].extra.uploader).toBe("UpA");
158
- expect(batch.events[0].extra.duration).toBe(300);
159
- // A8: history also yields an item entity (video) for KG linkage
160
- expect(batch.items).toHaveLength(1);
161
- expect(batch.items[0].extra.bvid).toBe("BV1abc");
162
- });
163
- });
164
-
165
- // ─── WeiboAdapter ───────────────────────────────────────────────────────
166
-
167
- describe("WeiboAdapter", () => {
168
- it("contract conformance", () => {
169
- const a = new WeiboAdapter({ account: { uid: "1234" } });
170
- expect(assertAdapter(a).ok).toBe(true);
171
- expect(a.extractMode).toBe("device-pull");
172
- expect(a.capabilities).toContain("sync:snapshot");
173
- expect(a.capabilities).toContain("sync:sqlite");
174
- });
175
-
176
- it("snapshot mode constructs without account.uid (stateless)", () => {
177
- // §A8 v0.2: constructor loosened — snapshot mode pulls account from the
178
- // snapshot file. Sqlite mode still requires account.uid, checked at sync
179
- // time not construction.
180
- const a = new WeiboAdapter({});
181
- expect(assertAdapter(a).ok).toBe(true);
182
- expect(a.account).toBeNull();
183
- });
184
-
185
- it("sqlite mode throws at sync time when account.uid missing", async () => {
186
- const dir = fs.mkdtempSync(path.join(os.tmpdir(), "weibo-"));
187
- const dbPath = path.join(dir, "weibo.db");
188
- fs.writeFileSync(dbPath, "fake");
189
- try {
190
- const a = new WeiboAdapter({});
191
- let threw = null;
192
- try {
193
- for await (const _r of a.sync({ dbPath })) { /* drain */ }
194
- } catch (e) {
195
- threw = e;
196
- }
197
- expect(threw).not.toBeNull();
198
- expect(threw.message).toMatch(/account\.uid/);
199
- } finally {
200
- fs.rmSync(dir, { recursive: true, force: true });
201
- }
202
- });
203
-
204
- it("sync yields posts + search records via mocked driver", async () => {
205
- const dir = fs.mkdtempSync(path.join(os.tmpdir(), "weibo-"));
206
- const dbPath = path.join(dir, "weibo.db");
207
- fs.writeFileSync(dbPath, "fake");
208
- try {
209
- const mockDriver = makeMockDriver([
210
- ["FROM post", [
211
- { id: 1, mid: "M1", text: "今天天气真好", created_at: 1700000000, reposts_count: 5, comments_count: 3 },
212
- ]],
213
- ["FROM status", []],
214
- ["FROM search_history", [
215
- { id: 1, keyword: "iPhone", time: 1700001000 },
216
- { id: 2, keyword: "音乐", time: 1700001100 },
217
- ]],
218
- ]);
219
- const a = new WeiboAdapter({
220
- account: { uid: "1234" },
221
- dbPath,
222
- dbDriverFactory: () => mockDriver,
223
- });
224
- const raws = [];
225
- for await (const r of a.sync()) raws.push(r);
226
- expect(raws.length).toBe(3); // 1 post + 2 searches
227
-
228
- for (const raw of raws) {
229
- const batch = a.normalize(raw);
230
- const v = validateBatch(batch);
231
- expect(v.valid).toBe(true);
232
- const subtype = batch.events[0].subtype;
233
- if (raw.payload.kind === "post") expect(subtype).toBe("post");
234
- if (raw.payload.kind === "search") expect(subtype).toBe("interaction");
235
- }
236
- } finally {
237
- fs.rmSync(dir, { recursive: true, force: true });
238
- }
239
- });
240
-
241
- it("normalize captures post metrics", async () => {
242
- const a = new WeiboAdapter({ account: { uid: "1234" } });
243
- const raw = {
244
- adapter: "social-weibo",
245
- originalId: "post-M1",
246
- capturedAt: 1700000000000,
247
- payload: {
248
- kind: "post",
249
- row: {
250
- id: 1, mid: "M1", text: "测试",
251
- created_at: 1700000000,
252
- reposts_count: 5, comments_count: 3, attitudes_count: 10,
253
- location: "上海",
254
- },
255
- },
256
- };
257
- const batch = a.normalize(raw);
258
- expect(batch.events[0].extra.weiboMid).toBe("M1");
259
- expect(batch.events[0].extra.repostsCount).toBe(5);
260
- expect(batch.events[0].extra.commentsCount).toBe(3);
261
- expect(batch.events[0].extra.likesCount).toBe(10);
262
- expect(batch.events[0].extra.location).toBe("上海");
263
- });
264
-
265
- it("normalize falls back when text is empty", async () => {
266
- const a = new WeiboAdapter({ account: { uid: "1234" } });
267
- const raw = {
268
- adapter: "social-weibo",
269
- originalId: "post-x",
270
- capturedAt: Date.now(),
271
- payload: {
272
- kind: "post",
273
- row: { id: 1, mid: "X", text: "", created_at: Math.floor(Date.now() / 1000) },
274
- },
275
- };
276
- const batch = a.normalize(raw);
277
- expect(batch.events[0].content.title).toBe("(空)");
278
- expect(validateBatch(batch).valid).toBe(true);
279
- });
280
- });