@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,279 +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
- XimalayaAdapter,
11
- SNAPSHOT_SCHEMA_VERSION,
12
- VALID_KINDS,
13
- extractList,
14
- trackItemToRecord,
15
- albumItemToRecord,
16
- } = require("../lib/adapters/audio-ximalaya");
17
- const { assertAdapter } = require("../lib/adapter-spec");
18
- const { validateBatch } = require("../lib/batch");
19
-
20
- // 喜马拉雅 (Ximalaya) — 听书/播客; mirrors music-kugou's play/favorite/subscribe
21
- // shape. signing injected (signProvider). pure-Node.
22
-
23
- function writeSnapshot(dir, snapshot) {
24
- const p = path.join(dir, "audio-ximalaya.json");
25
- fs.writeFileSync(p, JSON.stringify(snapshot), "utf-8");
26
- return p;
27
- }
28
-
29
- describe("XimalayaAdapter snapshot mode", () => {
30
- let tmpDir;
31
- beforeEach(() => {
32
- tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "xmly-snap-"));
33
- });
34
-
35
- it("exports schema constants", () => {
36
- expect(SNAPSHOT_SCHEMA_VERSION).toBe(1);
37
- expect(VALID_KINDS).toEqual(["play", "favorite", "subscribe"]);
38
- });
39
-
40
- it("authenticate(inputPath) ok when readable", async () => {
41
- const p = writeSnapshot(tmpDir, { schemaVersion: 1, snapshottedAt: Date.now(), events: [] });
42
- const a = new XimalayaAdapter();
43
- const res = await a.authenticate({ inputPath: p });
44
- expect(res.ok).toBe(true);
45
- expect(res.mode).toBe("snapshot-file");
46
- });
47
-
48
- it("authenticate() no input → NO_INPUT", async () => {
49
- const a = new XimalayaAdapter();
50
- const res = await a.authenticate({});
51
- expect(res.ok).toBe(false);
52
- expect(res.reason).toBe("NO_INPUT");
53
- });
54
-
55
- it("sync() without input throws with signProvider hint", async () => {
56
- const a = new XimalayaAdapter();
57
- let threw = null;
58
- try {
59
- for await (const _r of a.sync({})) { /* drain */ }
60
- } catch (err) {
61
- threw = err;
62
- }
63
- expect(String(threw.message)).toMatch(/signProvider/);
64
- });
65
-
66
- it("rejects schemaVersion mismatch", async () => {
67
- const p = writeSnapshot(tmpDir, { schemaVersion: 9, snapshottedAt: Date.now(), events: [] });
68
- const a = new XimalayaAdapter();
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("play (收听节目) → MEDIA event + MEDIA item", async () => {
79
- const now = Date.now();
80
- const p = writeSnapshot(tmpDir, {
81
- schemaVersion: 1, snapshottedAt: now,
82
- account: { userId: "u1", name: "alice" },
83
- events: [
84
- { kind: "play", id: "t1", trackId: "98765", title: "第1集 三体广播剧",
85
- anchor: "729声工场", album: "三体", durationSec: 1800, capturedAt: now - 1000 },
86
- ],
87
- });
88
- const a = new XimalayaAdapter();
89
- const raws = [];
90
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
91
- expect(raws.length).toBe(1);
92
- expect(raws[0].originalId).toBe("ximalaya:play:t1");
93
- const batch = a.normalize(raws[0]);
94
- expect(validateBatch(batch).valid).toBe(true);
95
- expect(batch.events[0].subtype).toBe("media");
96
- expect(batch.events[0].content.title).toContain("收听: 第1集 三体广播剧 - 729声工场");
97
- expect(batch.items[0].subtype).toBe("media");
98
- expect(batch.items[0].name).toBe("第1集 三体广播剧 - 729声工场");
99
- expect(batch.events[0].extra.album).toBe("三体");
100
- });
101
-
102
- it("favorite → LIKE event", async () => {
103
- const now = Date.now();
104
- const p = writeSnapshot(tmpDir, {
105
- schemaVersion: 1, snapshottedAt: now,
106
- events: [{ kind: "favorite", id: "f1", trackId: "555", title: "收藏的播客", anchor: "主播X" }],
107
- });
108
- const a = new XimalayaAdapter();
109
- const raws = [];
110
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
111
- const batch = a.normalize(raws[0]);
112
- expect(validateBatch(batch).valid).toBe(true);
113
- expect(batch.events[0].subtype).toBe("like");
114
- expect(batch.events[0].content.title).toContain("收藏: 收藏的播客");
115
- });
116
-
117
- it("subscribe → TOPIC (album)", async () => {
118
- const now = Date.now();
119
- const p = writeSnapshot(tmpDir, {
120
- schemaVersion: 1, snapshottedAt: now,
121
- events: [{ kind: "subscribe", id: "s1", albumId: "30001", album: "得到·精英日课",
122
- trackCount: 365, anchor: "罗振宇" }],
123
- });
124
- const a = new XimalayaAdapter();
125
- const raws = [];
126
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
127
- const batch = a.normalize(raws[0]);
128
- expect(validateBatch(batch).valid).toBe(true);
129
- expect(batch.events.length).toBe(0);
130
- expect(batch.topics.length).toBe(1);
131
- expect(batch.topics[0].id).toBe("topic-ximalaya-album-30001");
132
- expect(batch.topics[0].name).toBe("得到·精英日课");
133
- expect(batch.topics[0].extra.trackCount).toBe(365);
134
- });
135
-
136
- it("respects include opt-out + limit", async () => {
137
- const now = Date.now();
138
- const events = [
139
- { kind: "play", id: "p1", trackId: "1", title: "a", capturedAt: now },
140
- { kind: "favorite", id: "f1", trackId: "2", title: "b" },
141
- { kind: "subscribe", id: "s1", albumId: "3", album: "c" },
142
- ];
143
- const p = writeSnapshot(tmpDir, { schemaVersion: 1, snapshottedAt: now, events });
144
- const a = new XimalayaAdapter();
145
- const raws = [];
146
- for await (const r of a.sync({ inputPath: p, include: { favorite: false, subscribe: false } })) raws.push(r);
147
- expect(raws.length).toBe(1);
148
- expect(raws[0].kind).toBe("play");
149
- });
150
-
151
- it("filters unknown kinds", async () => {
152
- const now = Date.now();
153
- const p = writeSnapshot(tmpDir, {
154
- schemaVersion: 1, snapshottedAt: now,
155
- events: [
156
- { kind: "play", id: "p1", trackId: "1", title: "a", capturedAt: now },
157
- { kind: "comment", id: "c1" },
158
- ],
159
- });
160
- const a = new XimalayaAdapter();
161
- const raws = [];
162
- for await (const r of a.sync({ inputPath: p })) raws.push(r);
163
- expect(raws.length).toBe(1);
164
- });
165
-
166
- it("advertises capabilities + passes assertAdapter", () => {
167
- const a = new XimalayaAdapter();
168
- expect(a.capabilities).toContain("sync:snapshot");
169
- expect(a.capabilities).toContain("sync:cookie-api");
170
- expect(assertAdapter(a).ok).toBe(true);
171
- });
172
- });
173
-
174
- describe("XimalayaAdapter cookie-api mode", () => {
175
- it("authenticate(cookie) ok with cookies (userId optional)", async () => {
176
- const a = new XimalayaAdapter({ account: { cookies: "1&_token=ok" } });
177
- const res = await a.authenticate();
178
- expect(res.ok).toBe(true);
179
- expect(res.mode).toBe("cookie");
180
- });
181
-
182
- it("fetches plays/favorites/subscribes and normalizes", async () => {
183
- const byUrl = (url) => {
184
- if (url.includes("/history")) {
185
- return { data: { list: [{ trackId: 98765, title: "第1集", nickname: "声工场", albumTitle: "三体", duration: 1800, startedAt: 1700000000000 }] } };
186
- }
187
- if (url.includes("/favorite")) {
188
- return { data: { tracks: [{ track_id: 555, track_title: "播客A", anchor_name: "主播X" }] } };
189
- }
190
- if (url.includes("/subscribe")) {
191
- return { data: { albums: [{ albumId: 30001, albumTitle: "精英日课", includeTrackCount: 365, nickname: "罗振宇" }] } };
192
- }
193
- return {};
194
- };
195
- const fetchFn = async (opts) => byUrl(opts.url);
196
- const a = new XimalayaAdapter({ account: { userId: "u1", cookies: "1&_token=ok" }, fetchFn });
197
- const raws = [];
198
- for await (const r of a.sync({})) raws.push(r);
199
- expect(raws.map((r) => r.kind).sort()).toEqual(["favorite", "play", "subscribe"]);
200
-
201
- const play = raws.find((r) => r.kind === "play");
202
- const pb = a.normalize(play);
203
- expect(validateBatch(pb).valid).toBe(true);
204
- expect(pb.events[0].content.title).toContain("收听: 第1集 - 声工场");
205
- expect(pb.items[0].name).toBe("第1集 - 声工场");
206
-
207
- const sub = raws.find((r) => r.kind === "subscribe");
208
- const sb = a.normalize(sub);
209
- expect(sb.topics[0].name).toBe("精英日课");
210
- expect(sb.topics[0].extra.trackCount).toBe(365);
211
- });
212
-
213
- it("invokes signProvider, passes sign to fetchFn", async () => {
214
- let seen = null;
215
- const signProvider = async () => "SIG";
216
- const fetchFn = async (opts) => {
217
- seen = opts.sign;
218
- return {};
219
- };
220
- const a = new XimalayaAdapter({ account: { cookies: "x=1" }, fetchFn, signProvider });
221
- for await (const _r of a.sync({ include: { favorite: false, subscribe: false } })) { /* drain */ }
222
- expect(seen).toBe("SIG");
223
- });
224
-
225
- it("paginates plays until short page", async () => {
226
- const all = Array.from({ length: 45 }, (_, i) => ({ trackId: i + 1, title: `t${i}`, startedAt: 1700000000000 }));
227
- const seenPages = [];
228
- const fetchFn = async (opts) => {
229
- if (!opts.url.includes("/history")) return {};
230
- const page = opts.query.page;
231
- seenPages.push(page);
232
- return { list: all.slice((page - 1) * 30, page * 30) };
233
- };
234
- const a = new XimalayaAdapter({ account: { cookies: "x=1" }, fetchFn });
235
- const raws = [];
236
- for await (const r of a.sync({ include: { favorite: false, subscribe: false } })) raws.push(r);
237
- expect(raws.length).toBe(45);
238
- expect(seenPages).toEqual([1, 2]);
239
- });
240
-
241
- it("extractList + item mappers tolerate shapes", () => {
242
- expect(extractList({ list: [1] })).toEqual([1]);
243
- expect(extractList({ data: { tracks: [2] } })).toEqual([2]);
244
- expect(extractList({ data: { albums: [3] } })).toEqual([3]);
245
- expect(extractList(null)).toEqual([]);
246
- expect(trackItemToRecord({ trackId: 7, title: "x" }).trackId).toBe("7");
247
- expect(trackItemToRecord({})).toBe(null);
248
- expect(albumItemToRecord({ albumId: 9, albumTitle: "A" }).album).toBe("A");
249
- expect(albumItemToRecord({})).toBe(null);
250
- });
251
-
252
- it("uses opts.*Url overrides", async () => {
253
- const seen = [];
254
- const fetchFn = async (opts) => {
255
- seen.push(opts.url);
256
- return {};
257
- };
258
- const a = new XimalayaAdapter({
259
- account: { cookies: "x=1" },
260
- fetchFn,
261
- playsUrl: "https://x/p",
262
- favoritesUrl: "https://x/f",
263
- subscribesUrl: "https://x/s",
264
- });
265
- for await (const _r of a.sync({})) { /* drain */ }
266
- expect(seen).toEqual(["https://x/p", "https://x/f", "https://x/s"]);
267
- });
268
-
269
- it("default fetchFn throws legible error", async () => {
270
- const a = new XimalayaAdapter({ account: { cookies: "x=1" } });
271
- let threw = null;
272
- try {
273
- for await (const _r of a.sync({})) { /* drain */ }
274
- } catch (err) {
275
- threw = err;
276
- }
277
- expect(String(threw.message)).toMatch(/no fetchFn configured/);
278
- });
279
- });
@@ -1,133 +0,0 @@
1
- "use strict";
2
-
3
- import { describe, it, expect } from "vitest";
4
-
5
- const { emptyBatch, mergeBatches, validateBatch, partitionBatch } = require("../lib/batch");
6
- const { newId } = require("../lib/ids");
7
-
8
- const source = () => ({
9
- adapter: "test",
10
- adapterVersion: "0.1.0",
11
- capturedAt: Date.now(),
12
- capturedBy: "api",
13
- });
14
-
15
- const goodEvent = () => ({
16
- id: newId(),
17
- type: "event",
18
- subtype: "order",
19
- occurredAt: Date.now(),
20
- ingestedAt: Date.now(),
21
- content: { title: "X" },
22
- source: source(),
23
- });
24
-
25
- const goodPerson = () => ({
26
- id: newId(),
27
- type: "person",
28
- subtype: "merchant",
29
- names: ["美团"],
30
- ingestedAt: Date.now(),
31
- source: source(),
32
- });
33
-
34
- const badEvent = () => ({
35
- // missing required occurredAt + source + content
36
- id: newId(),
37
- type: "event",
38
- subtype: "order",
39
- ingestedAt: Date.now(),
40
- });
41
-
42
- describe("emptyBatch", () => {
43
- it("returns shape with all 5 entity arrays", () => {
44
- const b = emptyBatch();
45
- expect(b).toEqual({ events: [], persons: [], places: [], items: [], topics: [] });
46
- });
47
- });
48
-
49
- describe("mergeBatches", () => {
50
- it("concatenates all 5 arrays", () => {
51
- const a = { ...emptyBatch(), events: [goodEvent()], persons: [goodPerson()] };
52
- const b = { ...emptyBatch(), events: [goodEvent()] };
53
- const merged = mergeBatches(a, b);
54
- expect(merged.events.length).toBe(2);
55
- expect(merged.persons.length).toBe(1);
56
- expect(merged.places.length).toBe(0);
57
- });
58
-
59
- it("tolerates missing arrays in either input", () => {
60
- const merged = mergeBatches({}, { persons: [goodPerson()] });
61
- expect(merged.persons.length).toBe(1);
62
- expect(merged.events.length).toBe(0);
63
- });
64
- });
65
-
66
- describe("validateBatch", () => {
67
- it("returns valid=true for an all-good batch", () => {
68
- const r = validateBatch({ events: [goodEvent(), goodEvent()], persons: [goodPerson()] });
69
- expect(r.valid).toBe(true);
70
- expect(r.entityCount).toBe(3);
71
- expect(r.errorCount).toBe(0);
72
- });
73
-
74
- it("collects all errors instead of throwing", () => {
75
- const r = validateBatch({ events: [goodEvent(), badEvent(), badEvent()] });
76
- expect(r.valid).toBe(false);
77
- expect(r.entityCount).toBe(3);
78
- expect(r.errorCount).toBe(2);
79
- expect(r.errors.every((e) => e.kind === "events")).toBe(true);
80
- expect(r.errors[0]).toHaveProperty("index");
81
- expect(r.errors[0].errors.length).toBeGreaterThan(0);
82
- });
83
-
84
- it("rejects non-array entity buckets", () => {
85
- const r = validateBatch({ events: "not-an-array" });
86
- expect(r.valid).toBe(false);
87
- expect(r.errors[0].errors[0]).toMatch(/array/);
88
- });
89
-
90
- it("rejects non-object batch", () => {
91
- expect(validateBatch(null).valid).toBe(false);
92
- expect(validateBatch("string").valid).toBe(false);
93
- });
94
-
95
- it("accepts batch with only some entity types populated", () => {
96
- const r = validateBatch({ persons: [goodPerson()] });
97
- expect(r.valid).toBe(true);
98
- expect(r.entityCount).toBe(1);
99
- });
100
- });
101
-
102
- describe("partitionBatch", () => {
103
- it("separates valid from invalid into distinct sub-batches", () => {
104
- const gE = goodEvent();
105
- const bE = badEvent();
106
- const gP = goodPerson();
107
-
108
- const { valid, invalid, invalidReasons } = partitionBatch({
109
- events: [gE, bE],
110
- persons: [gP],
111
- });
112
-
113
- expect(valid.events).toEqual([gE]);
114
- expect(valid.persons).toEqual([gP]);
115
- expect(invalid.events).toEqual([bE]);
116
- expect(invalid.persons).toEqual([]);
117
- expect(invalidReasons.length).toBe(1);
118
- expect(invalidReasons[0].kind).toBe("events");
119
- expect(invalidReasons[0].id).toBe(bE.id);
120
- });
121
-
122
- it("returns empty invalid sub-batch when all rows are valid", () => {
123
- const { valid, invalid, invalidReasons } = partitionBatch({
124
- events: [goodEvent()],
125
- persons: [goodPerson()],
126
- });
127
- expect(invalid.events).toEqual([]);
128
- expect(invalid.persons).toEqual([]);
129
- expect(invalidReasons).toEqual([]);
130
- expect(valid.events.length).toBe(1);
131
- expect(valid.persons.length).toBe(1);
132
- });
133
- });
@@ -1,231 +0,0 @@
1
- "use strict";
2
-
3
- import { describe, it, expect } from "vitest";
4
-
5
- const { CcKgSink, HUB_TO_CC_TYPE } = require("../lib/bridges/cc-kg-sink");
6
-
7
- // ─── Fake cc KG (mirrors the real addEntity/addRelation signature) ──────
8
-
9
- function makeFakeKg() {
10
- const entities = new Map();
11
- const relations = [];
12
- return {
13
- addEntity(_db, cfg) {
14
- if (!cfg || !cfg.id || !cfg.name || !cfg.type) {
15
- throw new Error("missing required fields");
16
- }
17
- if (entities.has(cfg.id)) {
18
- throw new Error(`Entity already exists: ${cfg.id}`);
19
- }
20
- entities.set(cfg.id, { ...cfg });
21
- return cfg;
22
- },
23
- addRelation(_db, cfg) {
24
- if (!entities.has(cfg.sourceId)) throw new Error(`source not found: ${cfg.sourceId}`);
25
- if (!entities.has(cfg.targetId)) throw new Error(`target not found: ${cfg.targetId}`);
26
- relations.push({ ...cfg });
27
- return cfg;
28
- },
29
- entities,
30
- relations,
31
- };
32
- }
33
-
34
- const t = (subject, predicate, opts) => {
35
- const out = { subject, predicate };
36
- if (opts && opts.object) out.object = opts.object;
37
- else if (opts && opts.literal !== undefined) out.literal = opts.literal;
38
- return out;
39
- };
40
-
41
- // ─── Tests ──────────────────────────────────────────────────────────────
42
-
43
- describe("CcKgSink construction", () => {
44
- it("requires addEntity + addRelation", () => {
45
- expect(() => new CcKgSink()).toThrow();
46
- expect(() => new CcKgSink({ addRelation: () => {} })).toThrow(/addEntity/);
47
- expect(() => new CcKgSink({ addEntity: () => {} })).toThrow(/addRelation/);
48
- });
49
- });
50
-
51
- describe("CcKgSink.write entity creation", () => {
52
- it("creates entities from rdf:type + has-name triples", async () => {
53
- const cc = makeFakeKg();
54
- const sink = new CcKgSink({ addEntity: cc.addEntity, addRelation: cc.addRelation });
55
- const r = await sink.write([
56
- t("p1", "rdf:type", { literal: "person" }),
57
- t("p1", "subtype", { literal: "contact" }),
58
- t("p1", "has-name", { literal: "妈妈" }),
59
- t("p1", "has-name", { literal: "陈某某" }),
60
- t("p1", "id:phone", { literal: "13800001111" }),
61
- ]);
62
- expect(r.entitiesUpserted).toBe(1);
63
- const e = cc.entities.get("p1");
64
- expect(e.type).toBe("Person");
65
- expect(e.name).toBe("妈妈"); // first name wins
66
- expect(e.properties.subtype).toBe("contact");
67
- expect(e.properties.hubKind).toBe("person");
68
- expect(e.properties.aliases).toEqual(["陈某某"]);
69
- expect(e.properties["id:phone"]).toBe("13800001111");
70
- });
71
-
72
- it("maps hub place → cc Concept with hubKind", async () => {
73
- const cc = makeFakeKg();
74
- const sink = new CcKgSink({ addEntity: cc.addEntity, addRelation: cc.addRelation });
75
- await sink.write([
76
- t("pl1", "rdf:type", { literal: "place" }),
77
- t("pl1", "has-name", { literal: "妈妈家" }),
78
- t("pl1", "located-at", { literal: "24.5,118.1" }),
79
- ]);
80
- const e = cc.entities.get("pl1");
81
- expect(e.type).toBe("Concept");
82
- expect(e.properties.hubKind).toBe("place");
83
- expect(e.properties["located-at"]).toBe("24.5,118.1");
84
- });
85
-
86
- it("maps hub item / topic → cc Concept", async () => {
87
- const cc = makeFakeKg();
88
- const sink = new CcKgSink({ addEntity: cc.addEntity, addRelation: cc.addRelation });
89
- await sink.write([
90
- t("i1", "rdf:type", { literal: "item" }),
91
- t("i1", "has-name", { literal: "蛋白粉" }),
92
- t("t1", "rdf:type", { literal: "topic" }),
93
- t("t1", "has-name", { literal: "母亲健康" }),
94
- ]);
95
- expect(cc.entities.get("i1").type).toBe("Concept");
96
- expect(cc.entities.get("i1").properties.hubKind).toBe("item");
97
- expect(cc.entities.get("t1").type).toBe("Concept");
98
- expect(cc.entities.get("t1").properties.hubKind).toBe("topic");
99
- });
100
-
101
- it("falls back to subject id when no has-name (uses Event subject as name)", async () => {
102
- const cc = makeFakeKg();
103
- const sink = new CcKgSink({ addEntity: cc.addEntity, addRelation: cc.addRelation });
104
- await sink.write([
105
- t("evt-x", "rdf:type", { literal: "event" }),
106
- t("evt-x", "subtype", { literal: "order" }),
107
- ]);
108
- expect(cc.entities.get("evt-x").name).toBe("evt-x");
109
- expect(cc.entities.get("evt-x").type).toBe("Event");
110
- });
111
-
112
- it("treats 'already exists' as upsert success (no error)", async () => {
113
- const cc = makeFakeKg();
114
- const sink = new CcKgSink({ addEntity: cc.addEntity, addRelation: cc.addRelation });
115
- await sink.write([
116
- t("p1", "rdf:type", { literal: "person" }),
117
- t("p1", "has-name", { literal: "alice" }),
118
- ]);
119
- // Second write of same subject — cc throws "already exists"
120
- const r = await sink.write([
121
- t("p1", "rdf:type", { literal: "person" }),
122
- t("p1", "has-name", { literal: "alice" }),
123
- ]);
124
- expect(r.entitiesUpserted).toBe(0);
125
- expect(r.errors.length).toBe(0); // already-exists treated as upsert hit
126
- });
127
-
128
- it("captures unknown predicates under __extra", async () => {
129
- const cc = makeFakeKg();
130
- const sink = new CcKgSink({ addEntity: cc.addEntity, addRelation: cc.addRelation });
131
- await sink.write([
132
- t("evt-x", "rdf:type", { literal: "event" }),
133
- t("evt-x", "weird-predicate", { literal: "foo" }),
134
- ]);
135
- expect(cc.entities.get("evt-x").properties.__extra).toEqual({ "weird-predicate": "foo" });
136
- });
137
-
138
- it("collects upstream errors (e.g. missing required field)", async () => {
139
- const cc = makeFakeKg();
140
- const sink = new CcKgSink({
141
- addEntity: () => { throw new Error("upstream DB exploded"); },
142
- addRelation: cc.addRelation,
143
- });
144
- const r = await sink.write([t("p1", "rdf:type", { literal: "person" })]);
145
- expect(r.entitiesUpserted).toBe(0);
146
- expect(r.errors.length).toBe(1);
147
- expect(r.errors[0].error).toContain("upstream DB exploded");
148
- });
149
- });
150
-
151
- describe("CcKgSink.write relation creation", () => {
152
- it("adds relations between created entities", async () => {
153
- const cc = makeFakeKg();
154
- const sink = new CcKgSink({ addEntity: cc.addEntity, addRelation: cc.addRelation });
155
- const r = await sink.write([
156
- t("evt-1", "rdf:type", { literal: "event" }),
157
- t("evt-1", "subtype", { literal: "payment" }),
158
- t("p1", "rdf:type", { literal: "person" }),
159
- t("p1", "has-name", { literal: "mom" }),
160
- t("evt-1", "by", { object: "p1" }),
161
- t("evt-1", "involves", { object: "p1" }),
162
- ]);
163
- expect(r.entitiesUpserted).toBe(2);
164
- expect(r.relationsAdded).toBe(2);
165
- expect(cc.relations).toContainEqual({ sourceId: "evt-1", targetId: "p1", relationType: "by" });
166
- expect(cc.relations).toContainEqual({ sourceId: "evt-1", targetId: "p1", relationType: "involves" });
167
- });
168
-
169
- it("skips relation when endpoint not in KG (no dangling refs)", async () => {
170
- const cc = makeFakeKg();
171
- const sink = new CcKgSink({ addEntity: cc.addEntity, addRelation: cc.addRelation });
172
- const r = await sink.write([
173
- t("evt-1", "rdf:type", { literal: "event" }),
174
- // p-missing was never declared with rdf:type
175
- t("evt-1", "by", { object: "p-missing" }),
176
- ]);
177
- expect(r.relationsAdded).toBe(0);
178
- expect(r.errors.length).toBe(1);
179
- expect(r.errors[0].error).toContain("endpoint not in KG");
180
- });
181
-
182
- it("rejects unknown predicates (defensive — don't poison cc KG)", async () => {
183
- const cc = makeFakeKg();
184
- const sink = new CcKgSink({ addEntity: cc.addEntity, addRelation: cc.addRelation });
185
- await sink.write([t("e", "rdf:type", { literal: "event" })]);
186
- await sink.write([t("p", "rdf:type", { literal: "person" })]);
187
- const r = await sink.write([t("e", "frobnicate", { object: "p" })]);
188
- expect(r.relationsAdded).toBe(0);
189
- expect(r.errors[0].error).toBe("unknown predicate");
190
- });
191
-
192
- it("tolerates duplicate-relation errors as success", async () => {
193
- const cc = makeFakeKg();
194
- const sink = new CcKgSink({
195
- addEntity: cc.addEntity,
196
- addRelation: () => { throw new Error("Relation already exists"); },
197
- });
198
- await sink.write([
199
- t("e", "rdf:type", { literal: "event" }),
200
- t("p", "rdf:type", { literal: "person" }),
201
- ]);
202
- const r = await sink.write([t("e", "by", { object: "p" })]);
203
- // Even though addRelation throws "already exists", sink treats as success.
204
- expect(r.errors.length).toBe(0);
205
- });
206
- });
207
-
208
- describe("CcKgSink edge cases", () => {
209
- it("returns zeros for empty input", async () => {
210
- const cc = makeFakeKg();
211
- const sink = new CcKgSink({ addEntity: cc.addEntity, addRelation: cc.addRelation });
212
- const r = await sink.write([]);
213
- expect(r).toEqual({ entitiesUpserted: 0, relationsAdded: 0, errors: [] });
214
- });
215
-
216
- it("ignores malformed triples (missing subject/predicate)", async () => {
217
- const cc = makeFakeKg();
218
- const sink = new CcKgSink({ addEntity: cc.addEntity, addRelation: cc.addRelation });
219
- const r = await sink.write([{ predicate: "rdf:type", literal: "person" }, null, undefined]);
220
- expect(r.entitiesUpserted).toBe(0);
221
- expect(cc.entities.size).toBe(0);
222
- });
223
-
224
- it("HUB_TO_CC_TYPE exposes the documented mapping", () => {
225
- expect(HUB_TO_CC_TYPE.person).toBe("Person");
226
- expect(HUB_TO_CC_TYPE.event).toBe("Event");
227
- expect(HUB_TO_CC_TYPE.place).toBe("Concept");
228
- expect(HUB_TO_CC_TYPE.item).toBe("Concept");
229
- expect(HUB_TO_CC_TYPE.topic).toBe("Concept");
230
- });
231
- });