@chainlesschain/personal-data-hub 0.4.28 → 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 (204) hide show
  1. package/README.md +13 -5
  2. package/lib/adapters/social-douyin-adb/usage-profile-reader.js +253 -0
  3. package/lib/adapters/social-douyin-adb/watch-history-reader.js +104 -31
  4. package/lib/adapters/social-toutiao-adb/article-reader.js +202 -0
  5. package/lib/analysis-skills/overview.js +24 -4
  6. package/lib/analysis-skills/spending.js +63 -2
  7. package/lib/analysis-skills/timeline.js +11 -6
  8. package/lib/prompt-builder.js +15 -1
  9. package/lib/query-parser.js +38 -8
  10. package/package.json +4 -1
  11. package/__tests__/adapter-guide.test.js +0 -47
  12. package/__tests__/adapter-spec.test.js +0 -78
  13. package/__tests__/adapters/ai-chat-cookie-capture-spec.test.js +0 -211
  14. package/__tests__/adapters/ai-chat-health-checker.test.js +0 -262
  15. package/__tests__/adapters/ai-chat-history.test.js +0 -396
  16. package/__tests__/adapters/ai-chat-http-client.test.js +0 -242
  17. package/__tests__/adapters/ai-chat-vendors.test.js +0 -874
  18. package/__tests__/adapters/alipay-bill-adapter.test.js +0 -538
  19. package/__tests__/adapters/apple-health.test.js +0 -95
  20. package/__tests__/adapters/bank-family.test.js +0 -125
  21. package/__tests__/adapters/biz-tianyancha.test.js +0 -159
  22. package/__tests__/adapters/browser-history-chrome.test.js +0 -377
  23. package/__tests__/adapters/browser-history-edge.test.js +0 -159
  24. package/__tests__/adapters/car-mercedesme.test.js +0 -74
  25. package/__tests__/adapters/doc-baidu-netdisk.test.js +0 -102
  26. package/__tests__/adapters/doc-camscanner.test.js +0 -147
  27. package/__tests__/adapters/doc-platforms.test.js +0 -177
  28. package/__tests__/adapters/edu-huawei-learning-live.test.js +0 -198
  29. package/__tests__/adapters/edu-zuoyebang-live.test.js +0 -226
  30. package/__tests__/adapters/email-adapter-snapshot.test.js +0 -237
  31. package/__tests__/adapters/email-adapter.test.js +0 -742
  32. package/__tests__/adapters/email-classifier.test.js +0 -347
  33. package/__tests__/adapters/email-imap-session.test.js +0 -334
  34. package/__tests__/adapters/email-parser.test.js +0 -244
  35. package/__tests__/adapters/email-pdf-extractor.test.js +0 -529
  36. package/__tests__/adapters/email-providers.test.js +0 -84
  37. package/__tests__/adapters/email-retry-progress.test.js +0 -294
  38. package/__tests__/adapters/email-templates.test.js +0 -822
  39. package/__tests__/adapters/family-23-collectors-scaffold.test.js +0 -182
  40. package/__tests__/adapters/finance-alipay-live.test.js +0 -258
  41. package/__tests__/adapters/finance-dcep.test.js +0 -74
  42. package/__tests__/adapters/fitness-joyrun.test.js +0 -82
  43. package/__tests__/adapters/game-genshin-live.test.js +0 -238
  44. package/__tests__/adapters/game-genshin-scaffold.test.js +0 -108
  45. package/__tests__/adapters/game-honor-of-kings-live.test.js +0 -230
  46. package/__tests__/adapters/git-activity.test.js +0 -222
  47. package/__tests__/adapters/gov-12123.test.js +0 -103
  48. package/__tests__/adapters/gov-ixiamen.test.js +0 -150
  49. package/__tests__/adapters/gov-tax.test.js +0 -135
  50. package/__tests__/adapters/health-meiyou.test.js +0 -125
  51. package/__tests__/adapters/local-files.test.js +0 -264
  52. package/__tests__/adapters/local-im-pc.test.js +0 -154
  53. package/__tests__/adapters/messaging-whatsapp.test.js +0 -289
  54. package/__tests__/adapters/music-kugou.test.js +0 -187
  55. package/__tests__/adapters/music-qq.test.js +0 -112
  56. package/__tests__/adapters/netease-music-live.test.js +0 -244
  57. package/__tests__/adapters/netease-music.test.js +0 -74
  58. package/__tests__/adapters/pc-local-discovery.test.js +0 -141
  59. package/__tests__/adapters/qq-pc-direct-read.test.js +0 -227
  60. package/__tests__/adapters/reading-family.test.js +0 -108
  61. package/__tests__/adapters/recruit-boss.test.js +0 -180
  62. package/__tests__/adapters/shell-history.test.js +0 -180
  63. package/__tests__/adapters/shopping-base.test.js +0 -179
  64. package/__tests__/adapters/shopping-dianping.test.js +0 -239
  65. package/__tests__/adapters/social-bilibili-adb-api-client.test.js +0 -721
  66. package/__tests__/adapters/social-bilibili-adb-chromium-cookies-reader.test.js +0 -346
  67. package/__tests__/adapters/social-bilibili-adb-collector.test.js +0 -284
  68. package/__tests__/adapters/social-bilibili-adb-cookies-extension.test.js +0 -343
  69. package/__tests__/adapters/social-bilibili-adb-snapshot-builder.test.js +0 -296
  70. package/__tests__/adapters/social-csdn.test.js +0 -175
  71. package/__tests__/adapters/social-dongchedi.test.js +0 -165
  72. package/__tests__/adapters/social-douyin-adb-aweme-detail.test.js +0 -165
  73. package/__tests__/adapters/social-douyin-adb-collector.test.js +0 -254
  74. package/__tests__/adapters/social-douyin-adb-db-extension.test.js +0 -114
  75. package/__tests__/adapters/social-douyin-adb-im-db-parser.test.js +0 -304
  76. package/__tests__/adapters/social-douyin-adb-snapshot-builder.test.js +0 -216
  77. package/__tests__/adapters/social-douyin-adb-watch-history.test.js +0 -192
  78. package/__tests__/adapters/social-kuaishou-adb-api-client.test.js +0 -496
  79. package/__tests__/adapters/social-kuaishou-adb-collector.test.js +0 -276
  80. package/__tests__/adapters/social-kuaishou-adb-cookies-extension.test.js +0 -152
  81. package/__tests__/adapters/social-kuaishou-adb-snapshot-builder.test.js +0 -178
  82. package/__tests__/adapters/social-toutiao-adb-account-reader.test.js +0 -135
  83. package/__tests__/adapters/social-toutiao-adb-api-client.test.js +0 -626
  84. package/__tests__/adapters/social-toutiao-adb-collector.test.js +0 -378
  85. package/__tests__/adapters/social-toutiao-adb-cookies-extension.test.js +0 -193
  86. package/__tests__/adapters/social-toutiao-adb-snapshot-builder.test.js +0 -196
  87. package/__tests__/adapters/social-toutiao-kuaishou-scaffold.test.js +0 -311
  88. package/__tests__/adapters/social-weibo-adb-api-client.test.js +0 -362
  89. package/__tests__/adapters/social-weibo-adb-collector.test.js +0 -201
  90. package/__tests__/adapters/social-weibo-adb-cookies-extension.test.js +0 -167
  91. package/__tests__/adapters/social-weibo-adb-snapshot-builder.test.js +0 -189
  92. package/__tests__/adapters/social-xiaohongshu-adb-api-client.test.js +0 -431
  93. package/__tests__/adapters/social-xiaohongshu-adb-collector.test.js +0 -207
  94. package/__tests__/adapters/social-xiaohongshu-adb-cookies-extension.test.js +0 -0
  95. package/__tests__/adapters/social-xiaohongshu-adb-sign-provider-injection.test.js +0 -351
  96. package/__tests__/adapters/social-xiaohongshu-adb-sign.test.js +0 -130
  97. package/__tests__/adapters/social-xiaohongshu-adb-snapshot-builder.test.js +0 -200
  98. package/__tests__/adapters/social-zhihu.test.js +0 -246
  99. package/__tests__/adapters/system-data-adapter.test.js +0 -443
  100. package/__tests__/adapters/system-data-android-ingest.test.js +0 -144
  101. package/__tests__/adapters/system-data-android.test.js +0 -519
  102. package/__tests__/adapters/system-data-disclosure.test.js +0 -153
  103. package/__tests__/adapters/travel-12306.test.js +0 -512
  104. package/__tests__/adapters/travel-amap.test.js +0 -219
  105. package/__tests__/adapters/travel-baidu-map.test.js +0 -305
  106. package/__tests__/adapters/travel-base.test.js +0 -205
  107. package/__tests__/adapters/travel-ctrip.test.js +0 -377
  108. package/__tests__/adapters/travel-didi-consumer.test.js +0 -66
  109. package/__tests__/adapters/travel-didi.test.js +0 -204
  110. package/__tests__/adapters/travel-tencent-map.test.js +0 -207
  111. package/__tests__/adapters/travel-tongcheng.test.js +0 -289
  112. package/__tests__/adapters/video-platforms.test.js +0 -152
  113. package/__tests__/adapters/video-xigua.test.js +0 -106
  114. package/__tests__/adapters/vscode.test.js +0 -299
  115. package/__tests__/adapters/wechat-bootstrap.test.js +0 -240
  116. package/__tests__/adapters/wechat-env-probe.test.js +0 -162
  117. package/__tests__/adapters/wechat-frida-agent.test.js +0 -322
  118. package/__tests__/adapters/wechat-frida-integration.test.js +0 -149
  119. package/__tests__/adapters/wechat-frida-key-provider.test.js +0 -188
  120. package/__tests__/adapters/wechat-md5-key-provider.test.js +0 -101
  121. package/__tests__/adapters/wechat-pc-direct-read.test.js +0 -365
  122. package/__tests__/adapters/wechat-pc-group-topic.test.js +0 -63
  123. package/__tests__/adapters/wechat-pc-v4-sidecar.test.js +0 -72
  124. package/__tests__/adapters/weread.test.js +0 -123
  125. package/__tests__/adapters/wework-pc.test.js +0 -124
  126. package/__tests__/adapters/win-recent.test.js +0 -192
  127. package/__tests__/analysis-skills.test.js +0 -679
  128. package/__tests__/analysis.test.js +0 -1845
  129. package/__tests__/audio-ximalaya-snapshot.test.js +0 -279
  130. package/__tests__/batch.test.js +0 -133
  131. package/__tests__/bridges-cc-kg.test.js +0 -231
  132. package/__tests__/bridges-cc-llm.test.js +0 -191
  133. package/__tests__/bridges-cc-rag.test.js +0 -162
  134. package/__tests__/categories.test.js +0 -92
  135. package/__tests__/e2e/ai-chat-cross-source-journey.test.js +0 -213
  136. package/__tests__/e2e/full-user-journey.test.js +0 -188
  137. package/__tests__/e2e/local-data-adapters-cli.e2e.test.js +0 -146
  138. package/__tests__/entity-resolver-ingest-hook.test.js +0 -177
  139. package/__tests__/entity-resolver-stages.test.js +0 -411
  140. package/__tests__/entity-resolver-vault.test.js +0 -249
  141. package/__tests__/entity-resolver.test.js +0 -526
  142. package/__tests__/fitness-keep-snapshot.test.js +0 -224
  143. package/__tests__/fixtures/entity-resolver-200-mock.json +0 -96
  144. package/__tests__/ids.test.js +0 -45
  145. package/__tests__/integration/ai-chat-history-registry.test.js +0 -228
  146. package/__tests__/integration/aichat-wizard-end-to-end.test.js +0 -282
  147. package/__tests__/integration/cross-adapter-pipelines.test.js +0 -396
  148. package/__tests__/integration/local-data-adapters-pipeline.test.js +0 -373
  149. package/__tests__/integration/social-bilibili-pipeline.test.js +0 -261
  150. package/__tests__/integration/wechat-bootstrap-end-to-end.test.js +0 -390
  151. package/__tests__/key-providers.test.js +0 -126
  152. package/__tests__/kg-derive.test.js +0 -219
  153. package/__tests__/llm-client.test.js +0 -122
  154. package/__tests__/longtail-adapters.test.js +0 -281
  155. package/__tests__/messaging-qq-snapshot.test.js +0 -294
  156. package/__tests__/mobile-extractor-encrypted.test.js +0 -460
  157. package/__tests__/mobile-extractor.test.js +0 -288
  158. package/__tests__/mock-adapter.test.js +0 -93
  159. package/__tests__/prompt-builder.test.js +0 -249
  160. package/__tests__/query-parser.test.js +0 -302
  161. package/__tests__/rag-derive.test.js +0 -169
  162. package/__tests__/registry-readiness.test.js +0 -292
  163. package/__tests__/registry.test.js +0 -420
  164. package/__tests__/salvage-ingest.test.js +0 -97
  165. package/__tests__/schemas.test.js +0 -331
  166. package/__tests__/shopping-adapters.test.js +0 -392
  167. package/__tests__/shopping-eleme-snapshot.test.js +0 -454
  168. package/__tests__/shopping-pinduoduo-snapshot.test.js +0 -484
  169. package/__tests__/shopping-snapshot.test.js +0 -438
  170. package/__tests__/shopping-vipshop-snapshot.test.js +0 -425
  171. package/__tests__/shopping-xianyu-snapshot.test.js +0 -451
  172. package/__tests__/sidecar-contacts-cross-validate.test.js +0 -186
  173. package/__tests__/sidecar-supervisor.test.js +0 -128
  174. package/__tests__/sign-providers.test.js +0 -62
  175. package/__tests__/social-adapters.test.js +0 -280
  176. package/__tests__/social-bilibili-snapshot.test.js +0 -278
  177. package/__tests__/social-douban-snapshot.test.js +0 -351
  178. package/__tests__/social-douyin-im-direct-read.test.js +0 -377
  179. package/__tests__/social-douyin-salvage-collector.test.js +0 -98
  180. package/__tests__/social-douyin-salvage-mapper.test.js +0 -90
  181. package/__tests__/social-douyin-snapshot.test.js +0 -256
  182. package/__tests__/social-kuaishou-snapshot.test.js +0 -362
  183. package/__tests__/social-toutiao-snapshot.test.js +0 -366
  184. package/__tests__/social-weibo-snapshot.test.js +0 -234
  185. package/__tests__/social-weibo-sqlite-device.test.js +0 -174
  186. package/__tests__/social-xiaohongshu-snapshot.test.js +0 -232
  187. package/__tests__/sqlite-leaf-salvage.test.js +0 -97
  188. package/__tests__/travel-adapters.test.js +0 -483
  189. package/__tests__/travel-maps-snapshot.test.js +0 -426
  190. package/__tests__/vault-driver-error.test.js +0 -74
  191. package/__tests__/vault-search-helpers.test.js +0 -104
  192. package/__tests__/vault-search.test.js +0 -423
  193. package/__tests__/vault.test.js +0 -767
  194. package/__tests__/wechat-adapter.test.js +0 -594
  195. package/__tests__/whatsapp-adapter.test.js +0 -138
  196. package/scripts/_make-fixture-all.js +0 -126
  197. package/scripts/_make-fixture-contacts.js +0 -84
  198. package/scripts/evaluate-entity-resolver.js +0 -213
  199. package/scripts/run-native-tests-sandbox.sh +0 -55
  200. package/scripts/smoke-phase-5-5.js +0 -196
  201. package/scripts/smoke-phase-5-7.js +0 -181
  202. package/scripts/smoke-system-data-contacts.js +0 -309
  203. package/scripts/smoke-system-data.js +0 -312
  204. package/vitest.config.js +0 -88
@@ -1,278 +0,0 @@
1
- "use strict";
2
-
3
- import { describe, it, expect, beforeEach } from "vitest";
4
-
5
- const fs = require("node:fs");
6
- const path = require("node:path");
7
- const os = require("node:os");
8
-
9
- const {
10
- BilibiliAdapter,
11
- SNAPSHOT_SCHEMA_VERSION,
12
- VALID_KINDS,
13
- } = require("../lib/adapters/social-bilibili");
14
- const { validateBatch } = require("../lib/batch");
15
-
16
- // A8 v0.1 (2026-05-22) — snapshot-mode tests, mirroring system-data-android.
17
- //
18
- // Why a separate file? `social-adapters.test.js` covers the legacy sqlite
19
- // path (Phase 7.5 device-pull). Snapshot mode is a brand-new ingestion path
20
- // driven by in-APK Android cc reading JSON from the phone's own WebView+OkHttp
21
- // pipeline. Keeping tests separated makes it obvious which mode a regression
22
- // belongs to.
23
-
24
- function writeSnapshot(dir, snapshot) {
25
- const p = path.join(dir, "social-bilibili.json");
26
- fs.writeFileSync(p, JSON.stringify(snapshot), "utf-8");
27
- return p;
28
- }
29
-
30
- describe("BilibiliAdapter snapshot mode", () => {
31
- let tmpDir;
32
- beforeEach(() => {
33
- tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "bili-snap-"));
34
- });
35
-
36
- it("exports SNAPSHOT_SCHEMA_VERSION = 1 + 4 VALID_KINDS", () => {
37
- expect(SNAPSHOT_SCHEMA_VERSION).toBe(1);
38
- expect(VALID_KINDS).toEqual(["history", "favourite", "dynamic", "follow"]);
39
- });
40
-
41
- it("authenticate(inputPath) ok when readable", async () => {
42
- const p = writeSnapshot(tmpDir, {
43
- schemaVersion: 1,
44
- snapshottedAt: Date.now(),
45
- events: [],
46
- });
47
- const a = new BilibiliAdapter();
48
- const res = await a.authenticate({ inputPath: p });
49
- expect(res.ok).toBe(true);
50
- expect(res.mode).toBe("snapshot-file");
51
- });
52
-
53
- it("authenticate(inputPath) fails when path unreadable", async () => {
54
- const a = new BilibiliAdapter();
55
- const res = await a.authenticate({ inputPath: path.join(tmpDir, "missing.json") });
56
- expect(res.ok).toBe(false);
57
- expect(res.reason).toBe("INPUT_PATH_UNREADABLE");
58
- });
59
-
60
- it("authenticate() with neither inputPath nor dbPath returns NO_INPUT", async () => {
61
- const a = new BilibiliAdapter();
62
- const res = await a.authenticate({});
63
- expect(res.ok).toBe(false);
64
- expect(res.reason).toBe("NO_INPUT");
65
- });
66
-
67
- it("rejects schemaVersion mismatch", async () => {
68
- const p = writeSnapshot(tmpDir, {
69
- schemaVersion: 99,
70
- snapshottedAt: Date.now(),
71
- events: [],
72
- });
73
- const a = new BilibiliAdapter();
74
- let threw = null;
75
- try {
76
- for await (const _r of a.sync({ inputPath: p })) { /* drain */ }
77
- } catch (err) {
78
- threw = err;
79
- }
80
- expect(threw).toBeTruthy();
81
- expect(String(threw.message)).toMatch(/schemaVersion mismatch/);
82
- });
83
-
84
- it("empty events array yields nothing (no crash)", async () => {
85
- const p = writeSnapshot(tmpDir, {
86
- schemaVersion: 1,
87
- snapshottedAt: Date.now(),
88
- events: [],
89
- });
90
- const a = new BilibiliAdapter();
91
- const raws = [];
92
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
93
- expect(raws).toHaveLength(0);
94
- });
95
-
96
- it("yields all 4 kinds + normalize produces valid batches", async () => {
97
- const p = writeSnapshot(tmpDir, {
98
- schemaVersion: 1,
99
- snapshottedAt: 1716000000000,
100
- account: { uid: "12345", displayName: "alice" },
101
- events: [
102
- {
103
- kind: "history",
104
- id: "BV1abc",
105
- capturedAt: 1715000000000,
106
- title: "Rust 异步学习",
107
- bvid: "BV1abc",
108
- avid: 42,
109
- duration: 600,
110
- uploader: "技术UP主",
111
- uploaderMid: 100,
112
- part: "01 介绍",
113
- },
114
- {
115
- kind: "favourite",
116
- id: "fav-BV2def",
117
- capturedAt: 1714000000000,
118
- title: "前端架构",
119
- bvid: "BV2def",
120
- folderName: "学习",
121
- uploader: "码农UP",
122
- },
123
- {
124
- kind: "dynamic",
125
- id: "dyn-99",
126
- capturedAt: 1713000000000,
127
- summary: "今天发了一个新视频",
128
- dynamicType: "video",
129
- rid: 99,
130
- authorMid: 200,
131
- authorName: "我关注的UP",
132
- },
133
- {
134
- kind: "follow",
135
- id: "follow-300",
136
- capturedAt: 1712000000000,
137
- mid: 300,
138
- uname: "美食UP",
139
- face: "https://i0.hdslb.com/...",
140
- sign: "好吃的视频",
141
- },
142
- ],
143
- });
144
- const a = new BilibiliAdapter();
145
- const raws = [];
146
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
147
-
148
- expect(raws).toHaveLength(4);
149
- expect(raws.map((r) => r.kind).sort()).toEqual([
150
- "dynamic",
151
- "favourite",
152
- "follow",
153
- "history",
154
- ]);
155
- // Stable originalId format
156
- expect(raws.find((r) => r.kind === "history").originalId).toBe("bilibili:history:BV1abc");
157
- expect(raws.find((r) => r.kind === "favourite").originalId).toBe("bilibili:favourite:fav-BV2def");
158
- expect(raws.find((r) => r.kind === "dynamic").originalId).toBe("bilibili:dynamic:dyn-99");
159
- expect(raws.find((r) => r.kind === "follow").originalId).toBe("bilibili:follow:follow-300");
160
-
161
- // Normalize each + validate
162
- for (const raw of raws) {
163
- const batch = a.normalize(raw);
164
- const v = validateBatch(batch);
165
- expect(v.valid).toBe(true);
166
-
167
- if (raw.kind === "history") {
168
- expect(batch.events[0].subtype).toBe("browse");
169
- expect(batch.events[0].extra.bvid).toBe("BV1abc");
170
- expect(batch.events[0].extra.duration).toBe(600);
171
- expect(batch.items).toHaveLength(1);
172
- expect(batch.items[0].name).toBe("Rust 异步学习");
173
- } else if (raw.kind === "favourite") {
174
- expect(batch.events[0].subtype).toBe("like");
175
- expect(batch.events[0].extra.folderName).toBe("学习");
176
- expect(batch.items).toHaveLength(1);
177
- } else if (raw.kind === "dynamic") {
178
- expect(batch.events[0].subtype).toBe("browse");
179
- expect(batch.events[0].extra.dynamicType).toBe("video");
180
- expect(batch.events[0].extra.authorName).toBe("我关注的UP");
181
- } else if (raw.kind === "follow") {
182
- // Follow yields a person, not an event
183
- expect(batch.events).toHaveLength(0);
184
- expect(batch.persons).toHaveLength(1);
185
- expect(batch.persons[0].names[0]).toBe("美食UP");
186
- expect(batch.persons[0].identifiers["bilibili-mid"]).toEqual(["300"]);
187
- }
188
- }
189
- });
190
-
191
- it("per-kind include filter (e.g. include.follow=false drops follows)", async () => {
192
- const p = writeSnapshot(tmpDir, {
193
- schemaVersion: 1,
194
- snapshottedAt: Date.now(),
195
- events: [
196
- { kind: "history", id: "h1", title: "x" },
197
- { kind: "follow", id: "f1", mid: 1, uname: "u" },
198
- ],
199
- });
200
- const a = new BilibiliAdapter();
201
- const raws = [];
202
- for await (const r of a.sync({ inputPath: p, include: { follow: false } })) {
203
- raws.push(r);
204
- }
205
- expect(raws).toHaveLength(1);
206
- expect(raws[0].kind).toBe("history");
207
- });
208
-
209
- it("limit caps emission", async () => {
210
- const p = writeSnapshot(tmpDir, {
211
- schemaVersion: 1,
212
- snapshottedAt: Date.now(),
213
- events: [
214
- { kind: "history", id: "1", title: "a" },
215
- { kind: "history", id: "2", title: "b" },
216
- { kind: "history", id: "3", title: "c" },
217
- ],
218
- });
219
- const a = new BilibiliAdapter();
220
- const raws = [];
221
- for await (const r of a.sync({ inputPath: p, limit: 2 })) raws.push(r);
222
- expect(raws).toHaveLength(2);
223
- });
224
-
225
- it("skips unknown kinds (forward-compat with future event types)", async () => {
226
- const p = writeSnapshot(tmpDir, {
227
- schemaVersion: 1,
228
- snapshottedAt: Date.now(),
229
- events: [
230
- { kind: "history", id: "1", title: "ok" },
231
- { kind: "fancy-new-kind-from-future", id: "x", data: "?" },
232
- { kind: "favourite", id: "f", title: "also ok" },
233
- ],
234
- });
235
- const a = new BilibiliAdapter();
236
- const raws = [];
237
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
238
- expect(raws).toHaveLength(2);
239
- expect(raws.map((r) => r.kind).sort()).toEqual(["favourite", "history"]);
240
- });
241
-
242
- it("uses fallback originalId when event.id absent (no crash, still ingestable)", async () => {
243
- const p = writeSnapshot(tmpDir, {
244
- schemaVersion: 1,
245
- snapshottedAt: Date.now(),
246
- events: [
247
- // Missing id — adapter should derive from bvid/mid/rid or generate fallback
248
- { kind: "history", bvid: "BV1xyz", title: "no-id" },
249
- { kind: "follow", mid: 999, uname: "with-mid-no-id" },
250
- { kind: "dynamic", summary: "no id no rid" },
251
- ],
252
- });
253
- const a = new BilibiliAdapter();
254
- const raws = [];
255
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
256
- expect(raws).toHaveLength(3);
257
- // history derives from bvid
258
- expect(raws[0].originalId).toBe("bilibili:history:BV1xyz");
259
- // follow derives from mid
260
- expect(raws[1].originalId).toBe("bilibili:follow:999");
261
- // dynamic with no id/bvid/mid/rid → fallback unknown- prefix
262
- expect(raws[2].originalId).toMatch(/^bilibili:dynamic:unknown-/);
263
- });
264
-
265
- it("snapshot account propagates to payload (Path Y can re-attribute later)", async () => {
266
- const p = writeSnapshot(tmpDir, {
267
- schemaVersion: 1,
268
- snapshottedAt: Date.now(),
269
- account: { uid: "55555", displayName: "tester" },
270
- events: [{ kind: "history", id: "1", title: "x" }],
271
- });
272
- const a = new BilibiliAdapter();
273
- const raws = [];
274
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
275
- expect(raws).toHaveLength(1);
276
- expect(raws[0].payload.account.uid).toBe("55555");
277
- });
278
- });
@@ -1,351 +0,0 @@
1
- "use strict";
2
-
3
- import { describe, it, expect, beforeEach } from "vitest";
4
-
5
- const fs = require("node:fs");
6
- const path = require("node:path");
7
- const os = require("node:os");
8
-
9
- const {
10
- DoubanAdapter,
11
- SNAPSHOT_SCHEMA_VERSION,
12
- VALID_SNAPSHOT_KINDS,
13
- extractData,
14
- } = require("../lib/adapters/social-douban");
15
- const { assertAdapter } = require("../lib/adapter-spec");
16
- const { validateBatch } = require("../lib/batch");
17
-
18
- // 豆瓣 (Douban / Frodo) — 书影音 interest graph. Mirrors social-zhihu's two-mode
19
- // custom-normalize shape + video-base MEDIA event+item for marks. Frodo signing
20
- // is injected (signProvider) so the adapter stays pure-Node.
21
-
22
- function writeSnapshot(dir, snapshot) {
23
- const p = path.join(dir, "social-douban.json");
24
- fs.writeFileSync(p, JSON.stringify(snapshot), "utf-8");
25
- return p;
26
- }
27
-
28
- describe("DoubanAdapter snapshot mode", () => {
29
- let tmpDir;
30
- beforeEach(() => {
31
- tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "douban-snap-"));
32
- });
33
-
34
- it("exports schema constants", () => {
35
- expect(SNAPSHOT_SCHEMA_VERSION).toBe(1);
36
- expect(VALID_SNAPSHOT_KINDS).toEqual(["interest", "review", "follow"]);
37
- });
38
-
39
- it("authenticate(inputPath) ok when readable", async () => {
40
- const p = writeSnapshot(tmpDir, { schemaVersion: 1, snapshottedAt: Date.now(), events: [] });
41
- const a = new DoubanAdapter();
42
- const res = await a.authenticate({ inputPath: p });
43
- expect(res.ok).toBe(true);
44
- expect(res.mode).toBe("snapshot-file");
45
- });
46
-
47
- it("authenticate() no input → NO_INPUT", async () => {
48
- const a = new DoubanAdapter();
49
- const res = await a.authenticate({});
50
- expect(res.ok).toBe(false);
51
- expect(res.reason).toBe("NO_INPUT");
52
- });
53
-
54
- it("sync() without input throws with signProvider hint", async () => {
55
- const a = new DoubanAdapter();
56
- let threw = null;
57
- try {
58
- for await (const _r of a.sync({})) { /* drain */ }
59
- } catch (err) {
60
- threw = err;
61
- }
62
- expect(threw).toBeTruthy();
63
- expect(String(threw.message)).toMatch(/signProvider/);
64
- });
65
-
66
- it("rejects schemaVersion mismatch", async () => {
67
- const p = writeSnapshot(tmpDir, { schemaVersion: 99, snapshottedAt: Date.now(), events: [] });
68
- const a = new DoubanAdapter();
69
- let threw = null;
70
- try {
71
- for await (const _r of a.sync({ inputPath: p })) { /* drain */ }
72
- } catch (err) {
73
- threw = err;
74
- }
75
- expect(String(threw.message)).toMatch(/schemaVersion mismatch/);
76
- });
77
-
78
- it("interest (看过电影) → MEDIA event + MEDIA item, normalizes cleanly", async () => {
79
- const now = Date.now();
80
- const p = writeSnapshot(tmpDir, {
81
- schemaVersion: 1,
82
- snapshottedAt: now,
83
- account: { userId: "12345", name: "alice" },
84
- events: [
85
- {
86
- kind: "interest", id: "interest-m1", subjectId: "26266893",
87
- subjectType: "movie", title: "瞬息全宇宙", status: "done",
88
- myRating: 5, comment: "好看", createdTime: 1700000000, url: "https://movie.douban.com/subject/26266893/",
89
- },
90
- ],
91
- });
92
- const a = new DoubanAdapter();
93
- const raws = [];
94
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
95
- expect(raws.length).toBe(1);
96
- expect(raws[0].kind).toBe("interest");
97
- expect(raws[0].originalId).toBe("douban:interest:interest-m1");
98
-
99
- const batch = a.normalize(raws[0]);
100
- expect(validateBatch(batch).valid).toBe(true);
101
- expect(batch.events.length).toBe(1);
102
- expect(batch.items.length).toBe(1);
103
- expect(batch.events[0].subtype).toBe("media");
104
- expect(batch.events[0].content.title).toContain("看过电影: 瞬息全宇宙");
105
- expect(batch.events[0].extra.myRating).toBe(5);
106
- expect(batch.items[0].name).toBe("瞬息全宇宙");
107
- expect(JSON.stringify(batch)).toContain("好看");
108
- });
109
-
110
- it("interest status maps to verb (mark=想看, doing=在看, done=看过)", async () => {
111
- const now = Date.now();
112
- const cases = [
113
- { status: "mark", verb: "想看" },
114
- { status: "doing", verb: "在看" },
115
- { status: "done", verb: "看过" },
116
- ];
117
- for (const c of cases) {
118
- const p = writeSnapshot(tmpDir, {
119
- schemaVersion: 1, snapshottedAt: now,
120
- events: [
121
- { kind: "interest", id: `i-${c.status}`, subjectId: "1", subjectType: "book",
122
- title: "三体", status: c.status, createdTime: now },
123
- ],
124
- });
125
- const a = new DoubanAdapter();
126
- const raws = [];
127
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
128
- const batch = a.normalize(raws[0]);
129
- expect(batch.events[0].content.title).toContain(`${c.verb}图书: 三体`);
130
- }
131
- });
132
-
133
- it("review → POST event", async () => {
134
- const now = Date.now();
135
- const p = writeSnapshot(tmpDir, {
136
- schemaVersion: 1, snapshottedAt: now,
137
- events: [
138
- { kind: "review", id: "review-1", reviewId: "9001", title: "一篇影评",
139
- abstract: "<p>写得不错</p>", subjectTitle: "瞬息全宇宙", rating: 4, createdTime: now },
140
- ],
141
- });
142
- const a = new DoubanAdapter();
143
- const raws = [];
144
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
145
- const batch = a.normalize(raws[0]);
146
- expect(validateBatch(batch).valid).toBe(true);
147
- expect(batch.events[0].subtype).toBe("post");
148
- expect(batch.events[0].content.title).toBe("一篇影评");
149
- expect(batch.events[0].content.text).toBe("写得不错"); // html stripped
150
- expect(batch.events[0].extra.rating).toBe(4);
151
- });
152
-
153
- it("follow → CONTACT person with douban-id identifier", async () => {
154
- const now = Date.now();
155
- const p = writeSnapshot(tmpDir, {
156
- schemaVersion: 1, snapshottedAt: now,
157
- events: [
158
- { kind: "follow", id: "follow-u1", memberId: "67890", name: "豆友小张",
159
- url: "https://www.douban.com/people/67890/", capturedAt: now },
160
- ],
161
- });
162
- const a = new DoubanAdapter();
163
- const raws = [];
164
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
165
- const batch = a.normalize(raws[0]);
166
- expect(validateBatch(batch).valid).toBe(true);
167
- expect(batch.persons.length).toBe(1);
168
- expect(batch.persons[0].id).toBe("person-douban-67890");
169
- expect(batch.persons[0].names).toEqual(["豆友小张"]);
170
- expect(batch.persons[0].identifiers["douban-id"]).toEqual(["67890"]);
171
- });
172
-
173
- it("respects per-kind include opt-out + limit", async () => {
174
- const now = Date.now();
175
- const events = [
176
- { kind: "interest", id: "i1", subjectId: "1", subjectType: "movie", title: "a", status: "done", createdTime: now },
177
- { kind: "review", id: "r1", reviewId: "2", title: "b", createdTime: now },
178
- { kind: "follow", id: "f1", memberId: "3", name: "c", capturedAt: now },
179
- ];
180
- const p = writeSnapshot(tmpDir, { schemaVersion: 1, snapshottedAt: now, events });
181
- const a = new DoubanAdapter();
182
- const raws = [];
183
- for await (const r of a.sync({ inputPath: p, include: { review: false, follow: false } })) raws.push(r);
184
- expect(raws.length).toBe(1);
185
- expect(raws[0].kind).toBe("interest");
186
- });
187
-
188
- it("filters unknown kinds (forward compat)", async () => {
189
- const now = Date.now();
190
- const p = writeSnapshot(tmpDir, {
191
- schemaVersion: 1, snapshottedAt: now,
192
- events: [
193
- { kind: "interest", id: "i1", subjectId: "1", subjectType: "movie", title: "a", status: "done", createdTime: now },
194
- { kind: "status", id: "s1" },
195
- ],
196
- });
197
- const a = new DoubanAdapter();
198
- const raws = [];
199
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
200
- expect(raws.length).toBe(1);
201
- });
202
-
203
- it("advertises snapshot + cookie-api capabilities; passes assertAdapter", () => {
204
- const a = new DoubanAdapter();
205
- expect(a.capabilities).toContain("sync:snapshot");
206
- expect(a.capabilities).toContain("sync:cookie-api");
207
- expect(assertAdapter(a).ok).toBe(true);
208
- });
209
- });
210
-
211
- describe("DoubanAdapter cookie-api mode", () => {
212
- it("authenticate(cookie) requires account.userId", async () => {
213
- const a = new DoubanAdapter({ account: { cookies: "bid=ok" } });
214
- const res = await a.authenticate();
215
- expect(res.ok).toBe(false);
216
- expect(res.reason).toBe("NO_ACCOUNT_USER_ID");
217
- });
218
-
219
- it("authenticate(cookie) ok when userId + cookies present", async () => {
220
- const a = new DoubanAdapter({ account: { userId: "12345", cookies: "bid=ok" } });
221
- const res = await a.authenticate();
222
- expect(res.ok).toBe(true);
223
- expect(res.mode).toBe("cookie");
224
- expect(res.account).toBe("12345");
225
- });
226
-
227
- it("fetches interests/reviews/following across the plan and normalizes", async () => {
228
- const byUrl = (url) => {
229
- if (url.includes("/interests")) {
230
- return {
231
- interests: [
232
- { id: "int1", status: "done", create_time: "2024-01-02 10:00:00",
233
- rating: { value: 4 }, comment: "不错",
234
- subject: { id: "26266893", type: "movie", title: "瞬息全宇宙", url: "u" } },
235
- ],
236
- total: 1,
237
- };
238
- }
239
- if (url.includes("/reviews")) {
240
- return { reviews: [{ id: "rev1", title: "影评", abstract: "好", create_time: "2024-01-03 10:00:00", subject: { title: "瞬息全宇宙" } }], total: 1 };
241
- }
242
- if (url.includes("/following")) {
243
- return { users: [{ id: "u9", name: "豆友" }], total: 1 };
244
- }
245
- return { total: 0 };
246
- };
247
- const fetchFn = async (opts) => byUrl(opts.url);
248
- const a = new DoubanAdapter({ account: { userId: "12345", cookies: "bid=ok" }, fetchFn });
249
- const raws = [];
250
- for await (const r of a.sync({})) raws.push(r);
251
- expect(raws.map((r) => r.kind).sort()).toEqual(["follow", "interest", "review"]);
252
-
253
- const interest = raws.find((r) => r.kind === "interest");
254
- const ib = a.normalize(interest);
255
- expect(validateBatch(ib).valid).toBe(true);
256
- expect(ib.events[0].subtype).toBe("media");
257
- expect(ib.events[0].content.title).toContain("看过电影: 瞬息全宇宙");
258
- expect(ib.events[0].extra.myRating).toBe(4);
259
- expect(ib.items[0].name).toBe("瞬息全宇宙");
260
-
261
- const follow = raws.find((r) => r.kind === "follow");
262
- const fb = a.normalize(follow);
263
- expect(fb.persons[0].identifiers["douban-id"]).toEqual(["u9"]);
264
- });
265
-
266
- it("invokes signProvider and passes sign to fetchFn", async () => {
267
- let seenSign = null;
268
- const signProvider = async () => "SIG-1";
269
- const fetchFn = async (opts) => {
270
- seenSign = opts.sign;
271
- return { total: 0 };
272
- };
273
- const a = new DoubanAdapter({
274
- account: { userId: "12345", cookies: "bid=ok" },
275
- fetchFn,
276
- signProvider,
277
- });
278
- for await (const _r of a.sync({ include: { review: false, follow: false } })) { /* drain */ }
279
- expect(seenSign).toBe("SIG-1");
280
- });
281
-
282
- it("passes sign: null when no signProvider", async () => {
283
- let seen = "unset";
284
- const fetchFn = async (opts) => {
285
- seen = opts.sign;
286
- return { total: 0 };
287
- };
288
- const a = new DoubanAdapter({ account: { userId: "12345", cookies: "bid=ok" }, fetchFn });
289
- for await (const _r of a.sync({ include: { review: false, follow: false } })) { /* drain */ }
290
- expect(seen).toBe(null);
291
- });
292
-
293
- it("paginates interests with start cursor until total reached", async () => {
294
- const seenStarts = [];
295
- const all = Array.from({ length: 25 }, (_, i) => ({
296
- id: `int${i}`, status: "done", create_time: "2024-01-01 00:00:00",
297
- subject: { id: String(i), type: "book", title: `书${i}` },
298
- }));
299
- const fetchFn = async (opts) => {
300
- if (!opts.url.includes("/interests")) return { total: 0 };
301
- const start = opts.query.start;
302
- seenStarts.push(start);
303
- return { interests: all.slice(start, start + 20), total: all.length };
304
- };
305
- const a = new DoubanAdapter({ account: { userId: "12345", cookies: "bid=ok" }, fetchFn });
306
- const raws = [];
307
- for await (const r of a.sync({ include: { review: false, follow: false } })) raws.push(r);
308
- expect(raws.length).toBe(25);
309
- expect(seenStarts).toEqual([0, 20]);
310
- });
311
-
312
- it("extractData tolerates Frodo response shapes", () => {
313
- expect(extractData({ interests: [1] }, "interest")).toEqual([1]);
314
- expect(extractData({ reviews: [2] }, "review")).toEqual([2]);
315
- expect(extractData({ users: [3] }, "follow")).toEqual([3]);
316
- expect(extractData({ items: [4] })).toEqual([4]);
317
- expect(extractData([5])).toEqual([5]);
318
- expect(extractData({})).toEqual([]);
319
- expect(extractData(null)).toEqual([]);
320
- });
321
-
322
- it("uses opts.*Url overrides", async () => {
323
- const seen = [];
324
- const fetchFn = async (opts) => {
325
- seen.push(opts.url);
326
- return { total: 0 };
327
- };
328
- const a = new DoubanAdapter({
329
- account: { userId: "12345", cookies: "bid=ok" },
330
- fetchFn,
331
- interestsUrl: "https://x/i/{id}",
332
- reviewsUrl: "https://x/r/{id}",
333
- followingUrl: "https://x/f/{id}",
334
- });
335
- for await (const _r of a.sync({})) { /* drain */ }
336
- expect(seen).toContain("https://x/i/12345");
337
- expect(seen).toContain("https://x/r/12345");
338
- expect(seen).toContain("https://x/f/12345");
339
- });
340
-
341
- it("default fetchFn throws legible error in cookie mode without injection", async () => {
342
- const a = new DoubanAdapter({ account: { userId: "12345", cookies: "bid=ok" } });
343
- let threw = null;
344
- try {
345
- for await (const _r of a.sync({})) { /* drain */ }
346
- } catch (err) {
347
- threw = err;
348
- }
349
- expect(String(threw.message)).toMatch(/no fetchFn configured/);
350
- });
351
- });