@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,366 +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
- ToutiaoAdapter,
11
- SNAPSHOT_SCHEMA_VERSION,
12
- VALID_SNAPSHOT_KINDS,
13
- } = require("../lib/adapters/social-toutiao");
14
- const { validateBatch } = require("../lib/batch");
15
-
16
- // §A8 v0.2 — Toutiao snapshot mode tests, mirror of social-douyin-snapshot.
17
- // Snapshot mode is the in-APK Android cc path: ToutiaoLocalCollector reads
18
- // (root + SQLCipher decrypt) the on-device DB and writes a JSON snapshot;
19
- // this adapter's snapshot mode ingests that JSON. v0.2 SURFACE = read /
20
- // collection / search kinds.
21
-
22
- function writeSnapshot(dir, snapshot) {
23
- const p = path.join(dir, "social-toutiao.json");
24
- fs.writeFileSync(p, JSON.stringify(snapshot), "utf-8");
25
- return p;
26
- }
27
-
28
- describe("ToutiaoAdapter snapshot mode", () => {
29
- let tmpDir;
30
- beforeEach(() => {
31
- tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "toutiao-snap-"));
32
- });
33
-
34
- it("exports SNAPSHOT_SCHEMA_VERSION = 1 + 4 VALID_SNAPSHOT_KINDS (v0.2.1 adds profile)", () => {
35
- expect(SNAPSHOT_SCHEMA_VERSION).toBe(1);
36
- expect(VALID_SNAPSHOT_KINDS).toEqual([
37
- "profile",
38
- "read",
39
- "collection",
40
- "search",
41
- ]);
42
- });
43
-
44
- it("authenticate(inputPath) ok when readable", async () => {
45
- const p = writeSnapshot(tmpDir, {
46
- schemaVersion: 1,
47
- snapshottedAt: Date.now(),
48
- events: [],
49
- });
50
- const a = new ToutiaoAdapter();
51
- const res = await a.authenticate({ inputPath: p });
52
- expect(res.ok).toBe(true);
53
- expect(res.mode).toBe("snapshot-file");
54
- });
55
-
56
- it("authenticate(inputPath) fails when path unreadable", async () => {
57
- const a = new ToutiaoAdapter();
58
- const res = await a.authenticate({
59
- inputPath: path.join(tmpDir, "missing.json"),
60
- });
61
- expect(res.ok).toBe(false);
62
- expect(res.reason).toBe("INPUT_PATH_UNREADABLE");
63
- });
64
-
65
- it("authenticate() with neither inputPath nor dbPath returns NO_INPUT", async () => {
66
- const a = new ToutiaoAdapter();
67
- const res = await a.authenticate({});
68
- expect(res.ok).toBe(false);
69
- expect(res.reason).toBe("NO_INPUT");
70
- });
71
-
72
- it("authenticate() sqlite mode without account.uid returns NO_ACCOUNT_UID", async () => {
73
- const a = new ToutiaoAdapter({ dbPath: "/no/such/path.db" });
74
- const res = await a.authenticate({});
75
- expect(res.ok).toBe(false);
76
- expect(res.reason).toBe("NO_ACCOUNT_UID");
77
- });
78
-
79
- it("rejects schemaVersion mismatch", async () => {
80
- const p = writeSnapshot(tmpDir, {
81
- schemaVersion: 99,
82
- snapshottedAt: Date.now(),
83
- events: [],
84
- });
85
- const a = new ToutiaoAdapter();
86
- let threw = null;
87
- try {
88
- for await (const _r of a.sync({ inputPath: p })) {
89
- /* drain */
90
- }
91
- } catch (err) {
92
- threw = err;
93
- }
94
- expect(threw).toBeTruthy();
95
- expect(String(threw.message)).toMatch(/schemaVersion mismatch/);
96
- });
97
-
98
- it("empty events array yields nothing (no crash)", async () => {
99
- const p = writeSnapshot(tmpDir, {
100
- schemaVersion: 1,
101
- snapshottedAt: Date.now(),
102
- events: [],
103
- });
104
- const a = new ToutiaoAdapter();
105
- const raws = [];
106
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
107
- expect(raws.length).toBe(0);
108
- });
109
-
110
- it("v0.2 profile event normalizes to person-self with toutiao-uid identifier", async () => {
111
- const now = Date.now();
112
- const p = writeSnapshot(tmpDir, {
113
- schemaVersion: 1,
114
- snapshottedAt: now,
115
- account: { uid: "99999", displayName: "alice" },
116
- events: [
117
- {
118
- kind: "profile",
119
- id: "profile-99999",
120
- capturedAt: now - 500,
121
- uid: "99999",
122
- nickname: "alice",
123
- avatarUrl: "https://p.toutiao.com/u/alice.jpg",
124
- description: "hi there",
125
- followingCount: 12,
126
- followerCount: 34,
127
- mediaId: "media-1",
128
- },
129
- ],
130
- });
131
- const a = new ToutiaoAdapter();
132
- const raws = [];
133
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
134
- expect(raws.length).toBe(1);
135
- expect(raws[0].kind).toBe("profile");
136
- expect(raws[0].originalId).toMatch(/^toutiao:profile:/);
137
-
138
- const batch = a.normalize(raws[0]);
139
- expect(validateBatch(batch).valid).toBe(true);
140
- // KIND_PROFILE produces a person record (not an event)
141
- expect(batch.events.length).toBe(0);
142
- expect(batch.persons.length).toBe(1);
143
- const person = batch.persons[0];
144
- expect(person.id).toBe("person-toutiao-99999");
145
- expect(person.subtype).toBe("self");
146
- expect(person.names).toEqual(["alice"]);
147
- expect(person.identifiers["toutiao-uid"]).toEqual(["99999"]);
148
- expect(person.identifiers["toutiao-media-id"]).toEqual(["media-1"]);
149
- expect(person.extra.platform).toBe("toutiao");
150
- expect(person.extra.avatarUrl).toBe("https://p.toutiao.com/u/alice.jpg");
151
- expect(person.extra.description).toBe("hi there");
152
- expect(person.extra.followingCount).toBe(12);
153
- expect(person.extra.followerCount).toBe(34);
154
- expect(person.source.capturedBy).toBe("api");
155
- });
156
-
157
- it("read event round-trips normalize cleanly (BROWSE subtype)", async () => {
158
- const now = Date.now();
159
- const p = writeSnapshot(tmpDir, {
160
- schemaVersion: 1,
161
- snapshottedAt: now,
162
- account: { uid: "12345", displayName: "alice" },
163
- events: [
164
- {
165
- kind: "read",
166
- id: "read-i-1",
167
- capturedAt: now - 1000,
168
- itemId: "i-1",
169
- title: "5G 商用进展",
170
- category: "tech",
171
- author: "TechCN",
172
- readDuration: 120,
173
- source: "首页推荐",
174
- },
175
- ],
176
- });
177
- const a = new ToutiaoAdapter();
178
- const raws = [];
179
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
180
- expect(raws.length).toBe(1);
181
- expect(raws[0].kind).toBe("read");
182
- expect(raws[0].originalId).toMatch(/^toutiao:read:/);
183
-
184
- const batch = a.normalize(raws[0]);
185
- expect(validateBatch(batch).valid).toBe(true);
186
- expect(batch.events.length).toBe(1);
187
- const ev = batch.events[0];
188
- expect(ev.subtype).toBe("browse");
189
- expect(ev.content.title).toBe("5G 商用进展");
190
- expect(ev.extra.itemId).toBe("i-1");
191
- expect(ev.extra.category).toBe("tech");
192
- expect(ev.extra.author).toBe("TechCN");
193
- expect(ev.extra.readDuration).toBe(120);
194
- expect(ev.extra.platform).toBe("toutiao");
195
- expect(ev.source.capturedBy).toBe("api");
196
- });
197
-
198
- it("collection event round-trips normalize cleanly (LIKE subtype)", async () => {
199
- const now = Date.now();
200
- const p = writeSnapshot(tmpDir, {
201
- schemaVersion: 1,
202
- snapshottedAt: now,
203
- events: [
204
- {
205
- kind: "collection",
206
- id: "collect-i-2",
207
- capturedAt: now - 2000,
208
- itemId: "i-2",
209
- title: "深度长文",
210
- category: "investigation",
211
- author: "FinanceCN",
212
- },
213
- ],
214
- });
215
- const a = new ToutiaoAdapter();
216
- const raws = [];
217
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
218
- expect(raws.length).toBe(1);
219
-
220
- const batch = a.normalize(raws[0]);
221
- expect(validateBatch(batch).valid).toBe(true);
222
- expect(batch.events[0].subtype).toBe("like");
223
- expect(batch.events[0].content.title).toBe("深度长文");
224
- expect(batch.events[0].extra.itemId).toBe("i-2");
225
- expect(batch.events[0].extra.category).toBe("investigation");
226
- expect(batch.events[0].source.capturedBy).toBe("api");
227
- });
228
-
229
- it("search event round-trips normalize cleanly (POST subtype, keyword in title)", async () => {
230
- const now = Date.now();
231
- const p = writeSnapshot(tmpDir, {
232
- schemaVersion: 1,
233
- snapshottedAt: now,
234
- events: [
235
- {
236
- kind: "search",
237
- id: "search-rust:1700002000",
238
- capturedAt: now - 3000,
239
- keyword: "Rust 语言",
240
- },
241
- ],
242
- });
243
- const a = new ToutiaoAdapter();
244
- const raws = [];
245
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
246
- expect(raws.length).toBe(1);
247
-
248
- const batch = a.normalize(raws[0]);
249
- expect(validateBatch(batch).valid).toBe(true);
250
- expect(batch.events[0].subtype).toBe("post");
251
- expect(batch.events[0].content.title).toBe("Rust 语言");
252
- expect(batch.events[0].extra.kind).toBe("search");
253
- expect(batch.events[0].extra.keyword).toBe("Rust 语言");
254
- });
255
-
256
- it("respects per-kind include opt-out", async () => {
257
- const now = Date.now();
258
- const p = writeSnapshot(tmpDir, {
259
- schemaVersion: 1,
260
- snapshottedAt: now,
261
- events: [
262
- { kind: "read", id: "r1", capturedAt: now, itemId: "i-1", title: "t1" },
263
- { kind: "collection", id: "c1", capturedAt: now, itemId: "i-2", title: "t2" },
264
- { kind: "search", id: "s1", capturedAt: now, keyword: "kw" },
265
- ],
266
- });
267
- const a = new ToutiaoAdapter();
268
- const raws = [];
269
- for await (const r of a.sync({
270
- inputPath: p,
271
- include: { collection: false, search: false },
272
- })) {
273
- raws.push(r);
274
- }
275
- expect(raws.length).toBe(1);
276
- expect(raws[0].kind).toBe("read");
277
- });
278
-
279
- it("respects opts.limit", async () => {
280
- const now = Date.now();
281
- const events = [];
282
- for (let i = 0; i < 10; i++) {
283
- events.push({
284
- kind: "read",
285
- id: `r${i}`,
286
- capturedAt: now - i * 1000,
287
- itemId: `i-${i}`,
288
- title: `t${i}`,
289
- });
290
- }
291
- const p = writeSnapshot(tmpDir, { schemaVersion: 1, snapshottedAt: now, events });
292
- const a = new ToutiaoAdapter();
293
- const raws = [];
294
- for await (const r of a.sync({ inputPath: p, limit: 3 })) raws.push(r);
295
- expect(raws.length).toBe(3);
296
- });
297
-
298
- it("filters out unknown kinds (forward compat)", async () => {
299
- const now = Date.now();
300
- const p = writeSnapshot(tmpDir, {
301
- schemaVersion: 1,
302
- snapshottedAt: now,
303
- events: [
304
- { kind: "read", id: "r1", capturedAt: now, itemId: "i-1", title: "t1" },
305
- { kind: "future-kind", id: "x", capturedAt: now },
306
- { kind: "subscription", id: "sub-1", capturedAt: now }, // v0.3 hypothetical
307
- ],
308
- });
309
- const a = new ToutiaoAdapter();
310
- const raws = [];
311
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
312
- expect(raws.length).toBe(1);
313
- expect(raws[0].kind).toBe("read");
314
- });
315
-
316
- it("snapshottedAt fallback when event capturedAt missing", async () => {
317
- const ts = 1700000000000;
318
- const p = writeSnapshot(tmpDir, {
319
- schemaVersion: 1,
320
- snapshottedAt: ts,
321
- events: [{ kind: "read", id: "r1", itemId: "i-1", title: "t1" }],
322
- });
323
- const a = new ToutiaoAdapter();
324
- const raws = [];
325
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
326
- expect(raws[0].capturedAt).toBe(ts);
327
- });
328
-
329
- it("sqlite mode throws when account.uid missing at sync time", async () => {
330
- const a = new ToutiaoAdapter();
331
- let threw = null;
332
- try {
333
- for await (const _r of a.sync({ dbPath: "/no/such/path.db" })) {
334
- /* drain */
335
- }
336
- } catch (err) {
337
- threw = err;
338
- }
339
- expect(threw).toBeTruthy();
340
- expect(String(threw.message)).toMatch(/account\.uid required/);
341
- });
342
-
343
- it("sqlite mode gracefully exits when dbPath unreadable (with account.uid)", async () => {
344
- const a = new ToutiaoAdapter({
345
- account: { uid: "u-1" },
346
- dbPath: "/no/such/path.db",
347
- });
348
- const raws = [];
349
- for await (const r of a.sync()) raws.push(r);
350
- expect(raws).toEqual([]);
351
- });
352
-
353
- it("sync() with neither inputPath nor dbPath throws", async () => {
354
- const a = new ToutiaoAdapter();
355
- let threw = null;
356
- try {
357
- for await (const _r of a.sync()) {
358
- /* drain */
359
- }
360
- } catch (err) {
361
- threw = err;
362
- }
363
- expect(threw).toBeTruthy();
364
- expect(String(threw.message)).toMatch(/needs opts\.inputPath/);
365
- });
366
- });
@@ -1,234 +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
- WeiboAdapter,
11
- SNAPSHOT_SCHEMA_VERSION,
12
- VALID_SNAPSHOT_KINDS,
13
- } = require("../lib/adapters/social-weibo");
14
- const { validateBatch } = require("../lib/batch");
15
-
16
- // §A8 v0.2 — snapshot-mode tests, mirror of social-bilibili-snapshot.test.js.
17
- //
18
- // Snapshot mode is in-APK Android cc reading JSON written by WeiboLocalCollector
19
- // (WebView + OkHttp). Sqlite/device-pull tests stay in social-adapters.test.js.
20
-
21
- function writeSnapshot(dir, snapshot) {
22
- const p = path.join(dir, "social-weibo.json");
23
- fs.writeFileSync(p, JSON.stringify(snapshot), "utf-8");
24
- return p;
25
- }
26
-
27
- describe("WeiboAdapter snapshot mode", () => {
28
- let tmpDir;
29
- beforeEach(() => {
30
- tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "weibo-snap-"));
31
- });
32
-
33
- it("exports SNAPSHOT_SCHEMA_VERSION = 1 + 3 VALID_SNAPSHOT_KINDS", () => {
34
- expect(SNAPSHOT_SCHEMA_VERSION).toBe(1);
35
- expect(VALID_SNAPSHOT_KINDS).toEqual(["post", "favourite", "follow"]);
36
- });
37
-
38
- it("authenticate(inputPath) ok when readable", async () => {
39
- const p = writeSnapshot(tmpDir, {
40
- schemaVersion: 1,
41
- snapshottedAt: Date.now(),
42
- events: [],
43
- });
44
- const a = new WeiboAdapter();
45
- const res = await a.authenticate({ inputPath: p });
46
- expect(res.ok).toBe(true);
47
- expect(res.mode).toBe("snapshot-file");
48
- });
49
-
50
- it("authenticate(inputPath) fails when path unreadable", async () => {
51
- const a = new WeiboAdapter();
52
- const res = await a.authenticate({ inputPath: path.join(tmpDir, "missing.json") });
53
- expect(res.ok).toBe(false);
54
- expect(res.reason).toBe("INPUT_PATH_UNREADABLE");
55
- });
56
-
57
- it("authenticate() with neither inputPath nor dbPath returns NO_INPUT", async () => {
58
- const a = new WeiboAdapter();
59
- const res = await a.authenticate({});
60
- expect(res.ok).toBe(false);
61
- expect(res.reason).toBe("NO_INPUT");
62
- });
63
-
64
- it("rejects schemaVersion mismatch", async () => {
65
- const p = writeSnapshot(tmpDir, {
66
- schemaVersion: 99,
67
- snapshottedAt: Date.now(),
68
- events: [],
69
- });
70
- const a = new WeiboAdapter();
71
- let threw = null;
72
- try {
73
- for await (const _r of a.sync({ inputPath: p })) { /* drain */ }
74
- } catch (err) {
75
- threw = err;
76
- }
77
- expect(threw).toBeTruthy();
78
- expect(String(threw.message)).toMatch(/schemaVersion mismatch/);
79
- });
80
-
81
- it("empty events array yields nothing (no crash)", async () => {
82
- const p = writeSnapshot(tmpDir, {
83
- schemaVersion: 1,
84
- snapshottedAt: Date.now(),
85
- events: [],
86
- });
87
- const a = new WeiboAdapter();
88
- const raws = [];
89
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
90
- expect(raws.length).toBe(0);
91
- });
92
-
93
- it("post + favourite + follow round-trip normalize cleanly", async () => {
94
- const now = Date.now();
95
- const p = writeSnapshot(tmpDir, {
96
- schemaVersion: 1,
97
- snapshottedAt: now,
98
- account: { uid: "12345", displayName: "alice" },
99
- events: [
100
- {
101
- kind: "post",
102
- id: "post-M1",
103
- capturedAt: now - 1000,
104
- text: "今天天气真好",
105
- mid: "M1",
106
- source: "iPhone",
107
- repostsCount: 5,
108
- commentsCount: 3,
109
- likesCount: 10,
110
- picCount: 1,
111
- },
112
- {
113
- kind: "favourite",
114
- id: "fav-M2",
115
- capturedAt: now - 2000,
116
- text: "收藏的微博",
117
- mid: "M2",
118
- authorScreenName: "bob",
119
- },
120
- {
121
- kind: "follow",
122
- id: "follow-99",
123
- capturedAt: now - 3000,
124
- uid: 99,
125
- screenName: "carol",
126
- description: "hello",
127
- avatarUrl: "https://example.com/c.jpg",
128
- },
129
- ],
130
- });
131
- const a = new WeiboAdapter();
132
- const raws = [];
133
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
134
- expect(raws.length).toBe(3);
135
-
136
- const kinds = raws.map((r) => r.kind);
137
- expect(kinds).toEqual(["post", "favourite", "follow"]);
138
-
139
- // Each originalId namespaced under weibo:<kind>:<id>
140
- expect(raws[0].originalId).toMatch(/^weibo:post:/);
141
- expect(raws[1].originalId).toMatch(/^weibo:favourite:/);
142
- expect(raws[2].originalId).toMatch(/^weibo:follow:/);
143
-
144
- // Normalize each + validate
145
- for (const raw of raws) {
146
- const batch = a.normalize(raw);
147
- expect(validateBatch(batch).valid).toBe(true);
148
- }
149
-
150
- const postBatch = a.normalize(raws[0]);
151
- expect(postBatch.events[0].subtype).toBe("post");
152
- expect(postBatch.events[0].extra.weiboMid).toBe("M1");
153
- expect(postBatch.events[0].extra.repostsCount).toBe(5);
154
- expect(postBatch.events[0].extra.commentsCount).toBe(3);
155
- expect(postBatch.events[0].extra.likesCount).toBe(10);
156
- expect(postBatch.events[0].extra.picCount).toBe(1);
157
- expect(postBatch.events[0].source.capturedBy).toBe("api");
158
-
159
- const favBatch = a.normalize(raws[1]);
160
- expect(favBatch.events[0].subtype).toBe("like");
161
- expect(favBatch.events[0].extra.authorScreenName).toBe("bob");
162
-
163
- const followBatch = a.normalize(raws[2]);
164
- expect(followBatch.events.length).toBe(0);
165
- expect(followBatch.persons.length).toBe(1);
166
- expect(followBatch.persons[0].names).toEqual(["carol"]);
167
- expect(followBatch.persons[0].identifiers["weibo-uid"]).toEqual(["99"]);
168
- });
169
-
170
- it("respects per-kind include opt-out", async () => {
171
- const now = Date.now();
172
- const p = writeSnapshot(tmpDir, {
173
- schemaVersion: 1,
174
- snapshottedAt: now,
175
- events: [
176
- { kind: "post", id: "p1", capturedAt: now, text: "p1", mid: "M1" },
177
- { kind: "favourite", id: "f1", capturedAt: now, text: "f1", mid: "M2" },
178
- { kind: "follow", id: "fl1", capturedAt: now, uid: 99, screenName: "x" },
179
- ],
180
- });
181
- const a = new WeiboAdapter();
182
- const raws = [];
183
- for await (const r of a.sync({ inputPath: p, include: { favourite: false } })) {
184
- raws.push(r);
185
- }
186
- const kinds = raws.map((r) => r.kind);
187
- expect(kinds).toEqual(["post", "follow"]);
188
- });
189
-
190
- it("respects opts.limit", async () => {
191
- const now = Date.now();
192
- const events = Array.from({ length: 5 }, (_, i) => ({
193
- kind: "post", id: `p${i}`, capturedAt: now - i * 100, text: `t${i}`, mid: `M${i}`,
194
- }));
195
- const p = writeSnapshot(tmpDir, { schemaVersion: 1, snapshottedAt: now, events });
196
- const a = new WeiboAdapter();
197
- const raws = [];
198
- for await (const r of a.sync({ inputPath: p, limit: 2 })) raws.push(r);
199
- expect(raws.length).toBe(2);
200
- });
201
-
202
- it("filters out unknown kinds (forward compat)", async () => {
203
- const now = Date.now();
204
- const p = writeSnapshot(tmpDir, {
205
- schemaVersion: 1,
206
- snapshottedAt: now,
207
- events: [
208
- { kind: "post", id: "p1", capturedAt: now, text: "ok", mid: "M1" },
209
- { kind: "future-kind", id: "x", capturedAt: now },
210
- { kind: "search", id: "s", capturedAt: now }, // search is sqlite-only
211
- ],
212
- });
213
- const a = new WeiboAdapter();
214
- const raws = [];
215
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
216
- expect(raws.length).toBe(1);
217
- expect(raws[0].kind).toBe("post");
218
- });
219
-
220
- it("snapshottedAt fallback when event capturedAt missing", async () => {
221
- const ts = 1700000000000;
222
- const p = writeSnapshot(tmpDir, {
223
- schemaVersion: 1,
224
- snapshottedAt: ts,
225
- events: [
226
- { kind: "post", id: "p1", text: "no time", mid: "M1" },
227
- ],
228
- });
229
- const a = new WeiboAdapter();
230
- const raws = [];
231
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
232
- expect(raws[0].capturedAt).toBe(ts);
233
- });
234
- });