@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,196 +0,0 @@
1
- "use strict";
2
-
3
- import { describe, it, expect } from "vitest";
4
- const fs = require("node:fs");
5
- const path = require("node:path");
6
- const os = require("node:os");
7
-
8
- const {
9
- buildSnapshot,
10
- writeSnapshotJson,
11
- cleanupSnapshotJson,
12
- SNAPSHOT_SCHEMA_VERSION,
13
- } = require("../../lib/adapters/social-toutiao-adb/snapshot-builder");
14
-
15
- describe("SNAPSHOT_SCHEMA_VERSION", () => {
16
- it("is 1 (matches existing social-toutiao adapter)", () => {
17
- expect(SNAPSHOT_SCHEMA_VERSION).toBe(1);
18
- });
19
- });
20
-
21
- describe("buildSnapshot", () => {
22
- it("throws on missing uid", () => {
23
- expect(() => buildSnapshot({})).toThrow(/uid must be a non-empty string/);
24
- });
25
-
26
- it("emits profile event when input.profile given", () => {
27
- const snap = buildSnapshot({
28
- uid: "12345",
29
- profile: {
30
- nickname: "Alice",
31
- avatarUrl: "https://a/x.jpg",
32
- mobile: "138****",
33
- followingCount: 10,
34
- followerCount: 99,
35
- mediaId: "5678",
36
- },
37
- snapshottedAt: 1716383021000,
38
- });
39
- expect(snap.schemaVersion).toBe(1);
40
- expect(snap.account).toEqual({ uid: "12345", displayName: "" });
41
- const profileEvents = snap.events.filter((e) => e.kind === "profile");
42
- expect(profileEvents).toHaveLength(1);
43
- expect(profileEvents[0]).toMatchObject({
44
- kind: "profile",
45
- id: "profile-12345",
46
- uid: "12345",
47
- nickname: "Alice",
48
- followingCount: 10,
49
- followerCount: 99,
50
- mediaId: "5678",
51
- });
52
- });
53
-
54
- it("skips profile event when input.profile missing", () => {
55
- const snap = buildSnapshot({ uid: "12345" });
56
- expect(snap.events.filter((e) => e.kind === "profile")).toHaveLength(0);
57
- });
58
-
59
- it("emits read events from feed input", () => {
60
- const snap = buildSnapshot({
61
- uid: "12345",
62
- feed: [
63
- {
64
- itemId: "G1",
65
- title: "Read 1",
66
- category: "tech",
67
- author: "AuthorA",
68
- publishedAt: 1716000000000,
69
- readDuration: 60,
70
- source: "source-a",
71
- },
72
- ],
73
- });
74
- const reads = snap.events.filter((e) => e.kind === "read");
75
- expect(reads).toHaveLength(1);
76
- expect(reads[0]).toMatchObject({
77
- kind: "read",
78
- id: "read-G1",
79
- capturedAt: 1716000000000,
80
- itemId: "G1",
81
- title: "Read 1",
82
- category: "tech",
83
- author: "AuthorA",
84
- readDuration: 60,
85
- source: "source-a",
86
- });
87
- });
88
-
89
- it("emits collection events", () => {
90
- const snap = buildSnapshot({
91
- uid: "12345",
92
- collection: [
93
- {
94
- itemId: "C1",
95
- title: "Saved",
96
- category: "news",
97
- author: "AuthorB",
98
- savedAt: 1716100000000,
99
- },
100
- ],
101
- });
102
- const cols = snap.events.filter((e) => e.kind === "collection");
103
- expect(cols).toHaveLength(1);
104
- expect(cols[0]).toMatchObject({
105
- kind: "collection",
106
- id: "collect-C1",
107
- capturedAt: 1716100000000,
108
- itemId: "C1",
109
- title: "Saved",
110
- category: "news",
111
- author: "AuthorB",
112
- });
113
- });
114
-
115
- it("emits search events with keyword as id base", () => {
116
- const snap = buildSnapshot({
117
- uid: "12345",
118
- search: [{ keyword: "AI", searchedAt: 1716200000000 }],
119
- });
120
- const searches = snap.events.filter((e) => e.kind === "search");
121
- expect(searches).toHaveLength(1);
122
- expect(searches[0]).toMatchObject({
123
- kind: "search",
124
- id: "search-AI:1716200000000",
125
- capturedAt: 1716200000000,
126
- keyword: "AI",
127
- searchAt: 1716200000000,
128
- });
129
- });
130
-
131
- it("ignores search entries with empty keyword", () => {
132
- const snap = buildSnapshot({
133
- uid: "1",
134
- search: [{ keyword: "", searchedAt: 1 }, { keyword: "ok", searchedAt: 2 }],
135
- });
136
- const searches = snap.events.filter((e) => e.kind === "search");
137
- expect(searches).toHaveLength(1);
138
- expect(searches[0].keyword).toBe("ok");
139
- });
140
-
141
- it("falls back to snapshottedAt when event lacks timestamp", () => {
142
- const snap = buildSnapshot({
143
- uid: "1",
144
- feed: [{ itemId: "G", title: "T" }],
145
- snapshottedAt: 1716000000000,
146
- });
147
- expect(snap.events[0].capturedAt).toBe(1716000000000);
148
- });
149
-
150
- it("emits multiple kinds at once with stable id formats", () => {
151
- const snap = buildSnapshot({
152
- uid: "12345",
153
- profile: { nickname: "Alice" },
154
- feed: [{ itemId: "G1", title: "T1" }],
155
- collection: [{ itemId: "C1", title: "T2" }],
156
- search: [{ keyword: "kw1", searchedAt: 1 }],
157
- });
158
- expect(snap.events).toHaveLength(4);
159
- const ids = snap.events.map((e) => e.id);
160
- expect(ids).toContain("profile-12345");
161
- expect(ids).toContain("read-G1");
162
- expect(ids).toContain("collect-C1");
163
- expect(ids).toContain("search-kw1:1");
164
- });
165
- });
166
-
167
- describe("writeSnapshotJson + cleanupSnapshotJson", () => {
168
- it("writes valid JSON to staging dir", () => {
169
- const snap = buildSnapshot({ uid: "1", profile: { nickname: "A" } });
170
- const fullPath = writeSnapshotJson(snap);
171
- try {
172
- expect(fs.existsSync(fullPath)).toBe(true);
173
- const round = JSON.parse(fs.readFileSync(fullPath, "utf-8"));
174
- expect(round.account.uid).toBe("1");
175
- } finally {
176
- cleanupSnapshotJson(fullPath);
177
- }
178
- });
179
-
180
- it("rejects fileName with path separator", () => {
181
- const snap = buildSnapshot({ uid: "1" });
182
- expect(() =>
183
- writeSnapshotJson(snap, { fileName: "../escape.json" }),
184
- ).toThrow(/basename, not a path/);
185
- expect(() =>
186
- writeSnapshotJson(snap, { fileName: "sub/dir.json" }),
187
- ).toThrow(/basename, not a path/);
188
- });
189
-
190
- it("cleanupSnapshotJson tolerates missing file", () => {
191
- expect(() => cleanupSnapshotJson(null)).not.toThrow();
192
- expect(() =>
193
- cleanupSnapshotJson(path.join(os.tmpdir(), "nonexistent-x-y-z.json")),
194
- ).not.toThrow();
195
- });
196
- });
@@ -1,311 +0,0 @@
1
- /**
2
- * §A8 v0.2 — Toutiao 今日头条 + Kuaishou 快手 sqlite-mode tests.
3
- *
4
- * Originally Phase 13.8+13.9 v0.1 scaffold tests; promoted in §A8 v0.2 to
5
- * cover the dual-mode (snapshot + sqlite) adapter. Snapshot-mode coverage
6
- * lives in `../social-{toutiao,kuaishou}-snapshot.test.js`; this file
7
- * focuses on the legacy sqlite/device-pull path that desktop wiring still
8
- * uses for PCs running AndroidExtractor.
9
- *
10
- * - Adapter contract conformance (assertAdapter ok)
11
- * - sync() yields raw rows per `kind` from mocked SQLite driver
12
- * - normalize() produces valid UnifiedSchema events with correct subtype
13
- * - Account validation lazy-checked at sync() time (v0.2 changed:
14
- * account.uid is now OPTIONAL at construction so snapshot-mode-only
15
- * callers can omit it).
16
- */
17
-
18
- "use strict";
19
-
20
- import { describe, it, expect } from "vitest";
21
-
22
- const fs = require("node:fs");
23
- const path = require("node:path");
24
- const os = require("node:os");
25
-
26
- const { ToutiaoAdapter, KuaishouAdapter } = require("../../lib");
27
- const { assertAdapter } = require("../../lib/adapter-spec");
28
- const { validateBatch } = require("../../lib/batch");
29
-
30
- function makeMockDriver(scriptedRows) {
31
- return function () {
32
- return {
33
- prepare(sql) {
34
- return {
35
- all() {
36
- for (const [matchSubstr, rows] of scriptedRows) {
37
- if (sql.includes(matchSubstr)) return rows;
38
- }
39
- throw new Error("no such table");
40
- },
41
- };
42
- },
43
- close() {},
44
- };
45
- };
46
- }
47
-
48
- function withFakeDb(fn) {
49
- const dir = fs.mkdtempSync(path.join(os.tmpdir(), "pdh-scaffold-"));
50
- const dbPath = path.join(dir, "fake.db");
51
- fs.writeFileSync(dbPath, "fake");
52
- return fn(dbPath);
53
- }
54
-
55
- // ─── ToutiaoAdapter ─────────────────────────────────────────────────────
56
-
57
- describe("ToutiaoAdapter — §A8 v0.2 sqlite mode", () => {
58
- it("contract conformance + sensitivity high (news reading reveals political/medical interest)", () => {
59
- const a = new ToutiaoAdapter({ account: { uid: "u-1" } });
60
- expect(assertAdapter(a).ok).toBe(true);
61
- expect(a.name).toBe("social-toutiao");
62
- expect(a.extractMode).toBe("device-pull");
63
- expect(a.dataDisclosure.sensitivity).toBe("high");
64
- // v0.2 dual-mode capabilities — adapter accepts both snapshot and sqlite.
65
- expect(a.capabilities).toContain("sync:snapshot");
66
- expect(a.capabilities).toContain("sync:sqlite");
67
- });
68
-
69
- it("v0.2: account OPTIONAL at construction (snapshot mode is stateless)", () => {
70
- // Used to throw in v0.1 — now legal. Sqlite-mode sync() will lazy-throw.
71
- expect(() => new ToutiaoAdapter()).not.toThrow();
72
- expect(() => new ToutiaoAdapter({})).not.toThrow();
73
- expect(() => new ToutiaoAdapter({ account: {} })).not.toThrow();
74
- });
75
-
76
- it("sqlite mode lazy-throws when account.uid missing at sync time", async () => {
77
- const a = new ToutiaoAdapter({ dbPath: "/no/such/path.db" });
78
- let threw = null;
79
- try {
80
- for await (const _r of a.sync()) {
81
- /* drain */
82
- }
83
- } catch (err) {
84
- threw = err;
85
- }
86
- expect(threw).toBeTruthy();
87
- expect(String(threw.message)).toMatch(/account\.uid required/);
88
- });
89
-
90
- it("sync yields read + collection + search raws via mocked driver", async () => {
91
- await withFakeDb(async (dbPath) => {
92
- const driver = makeMockDriver([
93
- [
94
- "FROM read_history",
95
- [
96
- { id: 1, item_id: "i-1", title: "新闻 A", read_time: 1700000000, category: "tech" },
97
- { id: 2, item_id: "i-2", title: "新闻 B", read_time: 1700000010, category: "finance" },
98
- ],
99
- ],
100
- [
101
- "FROM collection_article",
102
- [{ id: 1, item_id: "i-3", article_title: "深度长文", save_time: 1700001000 }],
103
- ],
104
- [
105
- "FROM search_history",
106
- [{ id: 1, keyword: "Rust 语言", search_time: 1700002000 }],
107
- ],
108
- ]);
109
- const a = new ToutiaoAdapter({
110
- account: { uid: "u-1" },
111
- dbPath,
112
- dbDriverFactory: () => driver,
113
- });
114
- const raws = [];
115
- for await (const r of a.sync()) raws.push(r);
116
- expect(raws.length).toBe(4);
117
- expect(raws.filter((r) => r.payload.kind === "read")).toHaveLength(2);
118
- expect(raws.filter((r) => r.payload.kind === "collection")).toHaveLength(1);
119
- expect(raws.filter((r) => r.payload.kind === "search")).toHaveLength(1);
120
- });
121
- });
122
-
123
- it("normalize maps read → browse / collection → like / search → post (all subtypes valid)", async () => {
124
- const a = new ToutiaoAdapter({ account: { uid: "u-1" } });
125
- const samples = [
126
- {
127
- kind: "read",
128
- row: { id: 1, item_id: "i-1", title: "T1", read_time: 1700000000, category: "tech" },
129
- expectedSubtype: "browse",
130
- },
131
- {
132
- kind: "collection",
133
- row: { id: 1, item_id: "i-2", article_title: "T2", save_time: 1700001000 },
134
- expectedSubtype: "like",
135
- },
136
- {
137
- kind: "search",
138
- row: { id: 1, keyword: "Rust", search_time: 1700002000 },
139
- expectedSubtype: "post",
140
- },
141
- ];
142
- for (const s of samples) {
143
- const batch = a.normalize({
144
- adapter: "social-toutiao",
145
- originalId: `${s.kind}-${s.row.id}`,
146
- capturedAt: Date.now(),
147
- payload: { row: s.row, kind: s.kind },
148
- });
149
- const v = validateBatch(batch);
150
- expect(v.valid).toBe(true);
151
- expect(batch.events[0].subtype).toBe(s.expectedSubtype);
152
- expect(batch.events[0].source.adapter).toBe("social-toutiao");
153
- }
154
- });
155
-
156
- it("normalize throws on missing payload row + no snapshot fields (validator-friendly)", () => {
157
- const a = new ToutiaoAdapter({ account: { uid: "u-1" } });
158
- // v0.2: row-missing check moved into per-kind normalizers (snapshot
159
- // payloads have no `row` but carry fields directly).
160
- expect(() => a.normalize({ payload: { kind: "read" } })).toThrow(/row missing/);
161
- });
162
-
163
- it("search keyword preserved verbatim in content.title + extra.keyword", () => {
164
- const a = new ToutiaoAdapter({ account: { uid: "u-1" } });
165
- const batch = a.normalize({
166
- adapter: "social-toutiao",
167
- originalId: "search-1",
168
- capturedAt: 1700002000_000,
169
- payload: { row: { id: 1, keyword: "新冠 后遗症", search_time: 1700002000 }, kind: "search" },
170
- });
171
- expect(batch.events[0].content.title).toBe("新冠 后遗症");
172
- expect(batch.events[0].extra.kind).toBe("search");
173
- expect(batch.events[0].extra.keyword).toBe("新冠 后遗症");
174
- });
175
-
176
- it("sync gracefully exits when dbPath missing", async () => {
177
- const a = new ToutiaoAdapter({ account: { uid: "u-1" }, dbPath: "/no/such/path.db" });
178
- const raws = [];
179
- for await (const r of a.sync()) raws.push(r);
180
- expect(raws).toEqual([]);
181
- });
182
- });
183
-
184
- // ─── KuaishouAdapter ────────────────────────────────────────────────────
185
-
186
- describe("KuaishouAdapter — §A8 v0.2 sqlite mode", () => {
187
- it("contract conformance + sensitivity medium (entertainment preference)", () => {
188
- const a = new KuaishouAdapter({ account: { uid: "u-2" } });
189
- expect(assertAdapter(a).ok).toBe(true);
190
- expect(a.name).toBe("social-kuaishou");
191
- expect(a.extractMode).toBe("device-pull");
192
- expect(a.dataDisclosure.sensitivity).toBe("medium");
193
- expect(a.capabilities).toContain("sync:snapshot");
194
- expect(a.capabilities).toContain("sync:sqlite");
195
- });
196
-
197
- it("v0.2: account OPTIONAL at construction (snapshot mode is stateless)", () => {
198
- expect(() => new KuaishouAdapter()).not.toThrow();
199
- expect(() => new KuaishouAdapter({})).not.toThrow();
200
- expect(() => new KuaishouAdapter({ account: {} })).not.toThrow();
201
- });
202
-
203
- it("sqlite mode lazy-throws when account.uid missing at sync time", async () => {
204
- const a = new KuaishouAdapter({ dbPath: "/no/such/path.db" });
205
- let threw = null;
206
- try {
207
- for await (const _r of a.sync()) {
208
- /* drain */
209
- }
210
- } catch (err) {
211
- threw = err;
212
- }
213
- expect(threw).toBeTruthy();
214
- expect(String(threw.message)).toMatch(/account\.uid required/);
215
- });
216
-
217
- it("sync yields watch + collect + search raws via mocked driver", async () => {
218
- await withFakeDb(async (dbPath) => {
219
- const driver = makeMockDriver([
220
- [
221
- "FROM photo_history",
222
- [
223
- {
224
- id: 1,
225
- photo_id: "p-1",
226
- caption: "搞笑视频",
227
- view_time: 1700000000,
228
- duration: 30,
229
- author_name: "UpA",
230
- },
231
- ],
232
- ],
233
- [
234
- "FROM user_collect",
235
- [{ id: 1, photo_id: "p-2", caption: "美食 vlog", collect_time: 1700001000 }],
236
- ],
237
- [
238
- "FROM search_record",
239
- [{ id: 1, keyword: "广场舞", search_time: 1700002000 }],
240
- ],
241
- ]);
242
- const a = new KuaishouAdapter({
243
- account: { uid: "u-2" },
244
- dbPath,
245
- dbDriverFactory: () => driver,
246
- });
247
- const raws = [];
248
- for await (const r of a.sync()) raws.push(r);
249
- expect(raws.length).toBe(3);
250
- expect(raws.filter((r) => r.payload.kind === "watch")).toHaveLength(1);
251
- expect(raws.filter((r) => r.payload.kind === "collect")).toHaveLength(1);
252
- expect(raws.filter((r) => r.payload.kind === "search")).toHaveLength(1);
253
- });
254
- });
255
-
256
- it("normalize maps watch → browse / collect → like / search → post (all subtypes valid)", () => {
257
- const a = new KuaishouAdapter({ account: { uid: "u-2" } });
258
- const samples = [
259
- {
260
- kind: "watch",
261
- row: { id: 1, photo_id: "p-1", caption: "C1", view_time: 1700000000, duration: 30 },
262
- expectedSubtype: "browse",
263
- },
264
- {
265
- kind: "collect",
266
- row: { id: 1, photo_id: "p-2", caption: "C2", collect_time: 1700001000 },
267
- expectedSubtype: "like",
268
- },
269
- {
270
- kind: "search",
271
- row: { id: 1, keyword: "广场舞", search_time: 1700002000 },
272
- expectedSubtype: "post",
273
- },
274
- ];
275
- for (const s of samples) {
276
- const batch = a.normalize({
277
- adapter: "social-kuaishou",
278
- originalId: `${s.kind}-${s.row.id}`,
279
- capturedAt: Date.now(),
280
- payload: { row: s.row, kind: s.kind },
281
- });
282
- const v = validateBatch(batch);
283
- expect(v.valid).toBe(true);
284
- expect(batch.events[0].subtype).toBe(s.expectedSubtype);
285
- expect(batch.events[0].source.adapter).toBe("social-kuaishou");
286
- }
287
- });
288
-
289
- it("watch event extra carries photoId + duration + authorName", () => {
290
- const a = new KuaishouAdapter({ account: { uid: "u-2" } });
291
- const batch = a.normalize({
292
- adapter: "social-kuaishou",
293
- originalId: "watch-1",
294
- capturedAt: 1700000000_000,
295
- payload: {
296
- row: {
297
- id: 1,
298
- photo_id: "p-1",
299
- caption: "美食",
300
- view_time: 1700000000,
301
- duration: 60,
302
- author_name: "FoodVlogger",
303
- },
304
- kind: "watch",
305
- },
306
- });
307
- expect(batch.events[0].extra.photoId).toBe("p-1");
308
- expect(batch.events[0].extra.duration).toBe(60);
309
- expect(batch.events[0].extra.authorName).toBe("FoodVlogger");
310
- });
311
- });