@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,219 +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
- const crypto = require("node:crypto");
8
-
9
- const { AmapAdapter, NAME, VERSION } = require("../../lib/adapters/travel-amap");
10
-
11
- function writeTmpDb() {
12
- // sync() only checks fs.existsSync before handing the path to the
13
- // injected driver — content is irrelevant for the fake.
14
- const p = path.join(os.tmpdir(), `cc-amap-test-${crypto.randomUUID()}.db`);
15
- fs.writeFileSync(p, "fake");
16
- return p;
17
- }
18
-
19
- async function collect(gen) {
20
- const out = [];
21
- for await (const x of gen) out.push(x);
22
- return out;
23
- }
24
-
25
- /**
26
- * Fake better-sqlite3 driver. `tables` maps a SQL substring (table name)
27
- * to rows; unmatched prepare() throws like sqlite does on missing tables
28
- * so trySelect's fallback chain is exercised.
29
- */
30
- function makeFakeDriverFactory(tables, log = {}) {
31
- return () =>
32
- class FakeDb {
33
- constructor(dbPath, opts) {
34
- log.opened = { dbPath, opts };
35
- }
36
- prepare(sql) {
37
- for (const [needle, rows] of Object.entries(tables)) {
38
- if (sql.includes(needle)) return { all: () => rows };
39
- }
40
- throw new Error(`no such table in: ${sql}`);
41
- }
42
- close() {
43
- log.closed = true;
44
- }
45
- };
46
- }
47
-
48
- const ROUTE_ROW = {
49
- id: 7,
50
- mode: "drive",
51
- from_name: "家",
52
- to_name: "公司",
53
- from_lat: 31.23,
54
- from_lng: 121.47,
55
- to_lat: 31.2,
56
- to_lng: 121.44,
57
- time: 1716383021, // seconds — must be upgraded to ms
58
- };
59
-
60
- const SEARCH_ROW = {
61
- id: 9,
62
- keyword: "咖啡店",
63
- city: "上海",
64
- lat: 31.22,
65
- lng: 121.45,
66
- time: 1716383021000, // already ms
67
- };
68
-
69
- describe("constants", () => {
70
- it("exposes name/version", () => {
71
- expect(NAME).toBe("travel-amap");
72
- expect(VERSION).toBe("0.6.0");
73
- });
74
- });
75
-
76
- describe("authenticate", () => {
77
- it("ready mode when no db path", async () => {
78
- const a = new AmapAdapter();
79
- expect(await a.authenticate({})).toEqual({
80
- ok: true,
81
- account: null,
82
- mode: "ready",
83
- });
84
- });
85
-
86
- it("snapshot-file mode when dbPath exists", async () => {
87
- const p = writeTmpDb();
88
- try {
89
- const a = new AmapAdapter({ account: { deviceId: "DEV1" } });
90
- expect(await a.authenticate({ dbPath: p })).toEqual({
91
- ok: true,
92
- account: "DEV1",
93
- mode: "snapshot-file",
94
- });
95
- } finally {
96
- fs.unlinkSync(p);
97
- }
98
- });
99
- });
100
-
101
- describe("sync — fake sqlite driver", () => {
102
- it("yields route + search records, opens readonly, closes db", async () => {
103
- const p = writeTmpDb();
104
- const log = {};
105
- try {
106
- const a = new AmapAdapter({
107
- dbPath: p,
108
- dbDriverFactory: makeFakeDriverFactory(
109
- { history_route: [ROUTE_ROW], history_search: [SEARCH_ROW] },
110
- log,
111
- ),
112
- });
113
- const items = await collect(a.sync({}));
114
- expect(items).toHaveLength(2);
115
-
116
- const route = items.find((i) => i.payload.kind === "route");
117
- expect(route.originalId).toBe("route-7");
118
- expect(route.payload.record).toMatchObject({
119
- vendorId: "amap",
120
- vehicleType: "car", // mode=drive
121
- carrier: "高德地图",
122
- departureMs: 1716383021 * 1000, // seconds upgraded to ms
123
- });
124
- expect(route.payload.record.from.name).toBe("家");
125
- expect(route.payload.record.to.name).toBe("公司");
126
-
127
- const search = items.find((i) => i.payload.kind === "search");
128
- expect(search.originalId).toBe("search-9");
129
- expect(search.payload.record).toMatchObject({
130
- vehicleType: "visit",
131
- departureMs: 1716383021000, // ms passthrough
132
- });
133
- expect(search.payload.record.to).toMatchObject({
134
- name: "咖啡店",
135
- city: "上海",
136
- });
137
-
138
- expect(log.opened.opts).toEqual({ readonly: true });
139
- expect(log.closed).toBe(true);
140
- } finally {
141
- fs.unlinkSync(p);
142
- }
143
- });
144
-
145
- it("falls back to legacy ROUTE_HISTORY table name", async () => {
146
- const p = writeTmpDb();
147
- try {
148
- const a = new AmapAdapter({
149
- dbPath: p,
150
- dbDriverFactory: makeFakeDriverFactory({
151
- ROUTE_HISTORY: [ROUTE_ROW],
152
- history_search: [],
153
- }),
154
- });
155
- const items = await collect(a.sync({}));
156
- expect(items).toHaveLength(1);
157
- expect(items[0].payload.kind).toBe("route");
158
- } finally {
159
- fs.unlinkSync(p);
160
- }
161
- });
162
-
163
- it("skips rows without any id, tolerates missing tables entirely", async () => {
164
- const p = writeTmpDb();
165
- try {
166
- const a = new AmapAdapter({
167
- dbPath: p,
168
- dbDriverFactory: makeFakeDriverFactory({
169
- history_route: [{ mode: "walk" }], // no id → dropped
170
- }),
171
- });
172
- expect(await collect(a.sync({}))).toEqual([]);
173
- } finally {
174
- fs.unlinkSync(p);
175
- }
176
- });
177
-
178
- it("returns silently when db file missing", async () => {
179
- const a = new AmapAdapter({
180
- dbPath: path.join(os.tmpdir(), "nonexistent-amap.db"),
181
- dbDriverFactory: makeFakeDriverFactory({}),
182
- });
183
- expect(await collect(a.sync({}))).toEqual([]);
184
- });
185
- });
186
-
187
- describe("normalize", () => {
188
- it("route record → trip event with place coordinates", async () => {
189
- const p = writeTmpDb();
190
- try {
191
- const a = new AmapAdapter({
192
- dbPath: p,
193
- dbDriverFactory: makeFakeDriverFactory({
194
- history_route: [ROUTE_ROW],
195
- history_search: [],
196
- }),
197
- });
198
- const [item] = await collect(a.sync({}));
199
- const batch = a.normalize(item);
200
- const ev = batch.events[0];
201
- expect(ev.subtype).toBe("trip");
202
- expect(ev.content.title).toBe("car: 家 → 公司");
203
- expect(ev.source.adapter).toBe(NAME);
204
- expect(batch.places).toHaveLength(2);
205
- expect(batch.places[0].coordinates).toEqual({ lat: 31.23, lng: 121.47 });
206
- const merchant = batch.persons.find((x) => x.subtype === "merchant");
207
- expect(merchant.names).toEqual(["高德地图"]);
208
- } finally {
209
- fs.unlinkSync(p);
210
- }
211
- });
212
-
213
- it("throws on missing record", () => {
214
- const a = new AmapAdapter();
215
- expect(() => a.normalize({ payload: {} })).toThrow(
216
- /payload\.record missing/,
217
- );
218
- });
219
- });
@@ -1,305 +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
- const crypto = require("node:crypto");
8
-
9
- const {
10
- BaiduMapAdapter,
11
- NAME,
12
- VERSION,
13
- SNAPSHOT_SCHEMA_VERSION,
14
- } = require("../../lib/adapters/travel-baidu-map");
15
-
16
- function writeTmp(content, ext = "json") {
17
- const p = path.join(
18
- os.tmpdir(),
19
- `cc-baidumap-test-${crypto.randomUUID()}.${ext}`,
20
- );
21
- fs.writeFileSync(p, content, "utf-8");
22
- return p;
23
- }
24
-
25
- async function collect(gen) {
26
- const out = [];
27
- for await (const x of gen) out.push(x);
28
- return out;
29
- }
30
-
31
- function makeFakeDriverFactory(tables, log = {}) {
32
- return () =>
33
- class FakeDb {
34
- constructor(dbPath, opts) {
35
- log.opened = { dbPath, opts };
36
- }
37
- prepare(sql) {
38
- for (const [needle, rows] of Object.entries(tables)) {
39
- if (sql.includes(needle)) return { all: () => rows };
40
- }
41
- throw new Error(`no such table in: ${sql}`);
42
- }
43
- close() {
44
- log.closed = true;
45
- }
46
- };
47
- }
48
-
49
- const SNAPSHOT = {
50
- schemaVersion: SNAPSHOT_SCHEMA_VERSION,
51
- snapshottedAt: 1716383021000,
52
- vendor: "baidu-map",
53
- account: { uid: "U1", displayName: "Alice" },
54
- events: [
55
- {
56
- kind: "favourite",
57
- id: "fav-1",
58
- capturedAt: 1716383021000,
59
- name: "家",
60
- address: "幸福路1号",
61
- lat: 31.23,
62
- lng: 121.47,
63
- category: "home",
64
- },
65
- {
66
- kind: "search",
67
- id: "search-2",
68
- time: 1716383021, // seconds — parseTime must upgrade to ms
69
- query: "咖啡店",
70
- city: "上海",
71
- },
72
- {
73
- kind: "route",
74
- id: "route-3",
75
- capturedAt: 1716383021000,
76
- from: { name: "家", lat: 31.23, lng: 121.47 },
77
- to: { name: "公司", lat: 31.2, lng: 121.44 },
78
- mode: "drive",
79
- },
80
- { kind: "alien", id: "x" },
81
- ],
82
- };
83
-
84
- describe("constants", () => {
85
- it("exposes name/version/schema", () => {
86
- expect(NAME).toBe("travel-baidu-map");
87
- expect(VERSION).toBe("0.6.0");
88
- expect(SNAPSHOT_SCHEMA_VERSION).toBe(1);
89
- });
90
- });
91
-
92
- describe("authenticate", () => {
93
- it("snapshot mode ok / unreadable", async () => {
94
- const p = writeTmp("{}");
95
- try {
96
- const a = new BaiduMapAdapter();
97
- expect(await a.authenticate({ inputPath: p })).toEqual({
98
- ok: true,
99
- mode: "snapshot-file",
100
- });
101
- const bad = await a.authenticate({
102
- inputPath: path.join(os.tmpdir(), "nonexistent-baidu.json"),
103
- });
104
- expect(bad.reason).toBe("INPUT_PATH_UNREADABLE");
105
- } finally {
106
- fs.unlinkSync(p);
107
- }
108
- });
109
-
110
- it("sqlite mode requires account.deviceId; NO_INPUT otherwise", async () => {
111
- const noAcc = new BaiduMapAdapter({ dbPath: "x.db" });
112
- expect((await noAcc.authenticate({})).reason).toBe("NO_ACCOUNT_DEVICE_ID");
113
- const withAcc = new BaiduMapAdapter({
114
- dbPath: "x.db",
115
- account: { deviceId: "DEV1" },
116
- });
117
- expect(await withAcc.authenticate({})).toEqual({
118
- ok: true,
119
- account: "DEV1",
120
- mode: "sqlite",
121
- });
122
- expect((await new BaiduMapAdapter().authenticate({})).reason).toBe(
123
- "NO_INPUT",
124
- );
125
- });
126
- });
127
-
128
- describe("sync — snapshot mode", () => {
129
- it("yields 3 kinds with prefixed originalId + account attached, skips unknown kind", async () => {
130
- const p = writeTmp(JSON.stringify(SNAPSHOT));
131
- try {
132
- const a = new BaiduMapAdapter();
133
- const items = await collect(a.sync({ inputPath: p }));
134
- expect(items).toHaveLength(3);
135
- expect(items.map((i) => i.originalId)).toEqual([
136
- "baidu-map:favourite:fav-1",
137
- "baidu-map:search:search-2",
138
- "baidu-map:route:route-3",
139
- ]);
140
- // every payload carries the snapshot account for bookkeeping
141
- for (const i of items) {
142
- expect(i.payload.account).toEqual({ uid: "U1", displayName: "Alice" });
143
- }
144
- // ev.time in seconds upgraded to ms
145
- expect(items[1].capturedAt).toBe(1716383021 * 1000);
146
- } finally {
147
- fs.unlinkSync(p);
148
- }
149
- });
150
-
151
- it("throws on schemaVersion mismatch; honors include gate + limit", async () => {
152
- const bad = writeTmp(JSON.stringify({ schemaVersion: 9, events: [] }));
153
- const good = writeTmp(JSON.stringify(SNAPSHOT));
154
- try {
155
- const a = new BaiduMapAdapter();
156
- await expect(collect(a.sync({ inputPath: bad }))).rejects.toThrow(
157
- /schemaVersion mismatch/,
158
- );
159
- expect(
160
- await collect(a.sync({ inputPath: good, include: { search: false } })),
161
- ).toHaveLength(2);
162
- expect(
163
- await collect(a.sync({ inputPath: good, limit: 1 })),
164
- ).toHaveLength(1);
165
- } finally {
166
- fs.unlinkSync(bad);
167
- fs.unlinkSync(good);
168
- }
169
- });
170
- });
171
-
172
- describe("sync — sqlite mode (fake driver)", () => {
173
- const ROUTE_ROW = {
174
- _id: 5,
175
- type: "transit",
176
- start_name: "人民广场",
177
- end_name: "虹桥火车站",
178
- start_lat: 31.23,
179
- start_lng: 121.47,
180
- time: 1716383021, // seconds
181
- };
182
- const SEARCH_ROW = { _id: 6, key: "火锅", city: "成都", time: "1716383021" };
183
-
184
- it("requires account.deviceId", async () => {
185
- const a = new BaiduMapAdapter({ dbPath: "x.db" });
186
- await expect(collect(a.sync({}))).rejects.toThrow(
187
- /account\.deviceId required/,
188
- );
189
- });
190
-
191
- it("yields route (transit→bus) + search records, closes db", async () => {
192
- const p = writeTmp("fake", "db");
193
- const log = {};
194
- try {
195
- const a = new BaiduMapAdapter({
196
- dbPath: p,
197
- account: { deviceId: "DEV1" },
198
- dbDriverFactory: makeFakeDriverFactory(
199
- { route_history: [ROUTE_ROW], search_history: [SEARCH_ROW] },
200
- log,
201
- ),
202
- });
203
- const items = await collect(a.sync({}));
204
- expect(items).toHaveLength(2);
205
- expect(items[0].payload.record).toMatchObject({
206
- vendorId: "baidumap",
207
- recordId: "route-5",
208
- vehicleType: "bus",
209
- carrier: "百度地图",
210
- departureMs: 1716383021 * 1000,
211
- });
212
- expect(items[1].payload.record).toMatchObject({
213
- recordId: "search-6",
214
- vehicleType: "visit",
215
- departureMs: 1716383021 * 1000, // string seconds upgraded
216
- });
217
- expect(items[1].payload.record.to).toMatchObject({
218
- name: "火锅",
219
- city: "成都",
220
- });
221
- expect(log.opened.opts).toEqual({ readonly: true });
222
- expect(log.closed).toBe(true);
223
- } finally {
224
- fs.unlinkSync(p);
225
- }
226
- });
227
-
228
- it("falls back to bd_route_history legacy table; missing db file silent", async () => {
229
- const p = writeTmp("fake", "db");
230
- try {
231
- const a = new BaiduMapAdapter({
232
- dbPath: p,
233
- account: { deviceId: "DEV1" },
234
- dbDriverFactory: makeFakeDriverFactory({
235
- bd_route_history: [ROUTE_ROW],
236
- search_history: [],
237
- }),
238
- });
239
- expect(await collect(a.sync({}))).toHaveLength(1);
240
-
241
- const gone = new BaiduMapAdapter({
242
- dbPath: path.join(os.tmpdir(), "nonexistent-baidu.db"),
243
- account: { deviceId: "DEV1" },
244
- dbDriverFactory: makeFakeDriverFactory({}),
245
- });
246
- expect(await collect(gone.sync({}))).toEqual([]);
247
- } finally {
248
- fs.unlinkSync(p);
249
- }
250
- });
251
- });
252
-
253
- describe("normalize", () => {
254
- it("snapshot favourite → visit trip titled by place name", async () => {
255
- const p = writeTmp(JSON.stringify(SNAPSHOT));
256
- try {
257
- const a = new BaiduMapAdapter();
258
- const [fav, search, route] = await collect(a.sync({ inputPath: p }));
259
-
260
- const favBatch = a.normalize(fav);
261
- expect(favBatch.events[0].subtype).toBe("trip");
262
- expect(favBatch.events[0].content.title).toBe("visit: → 家");
263
- expect(favBatch.events[0].extra.vendorExtras.category).toBe("home");
264
- expect(favBatch.places[0].coordinates).toEqual({
265
- lat: 31.23,
266
- lng: 121.47,
267
- });
268
-
269
- // search has city → city wins over name in the title
270
- expect(a.normalize(search).events[0].content.title).toBe("visit: → 上海");
271
-
272
- const routeBatch = a.normalize(route);
273
- expect(routeBatch.events[0].content.title).toBe("car: 家 → 公司");
274
- expect(routeBatch.places).toHaveLength(2);
275
- const merchant = routeBatch.persons.find((x) => x.subtype === "merchant");
276
- expect(merchant.names).toEqual(["百度地图"]);
277
- } finally {
278
- fs.unlinkSync(p);
279
- }
280
- });
281
-
282
- it("sqlite-mode record payload passes through travel-base", () => {
283
- const a = new BaiduMapAdapter();
284
- const batch = a.normalize({
285
- payload: {
286
- record: {
287
- vendorId: "baidumap",
288
- recordId: "route-9",
289
- vehicleType: "car",
290
- from: { name: "A" },
291
- to: { name: "B" },
292
- carrier: "百度地图",
293
- },
294
- kind: "route",
295
- },
296
- });
297
- expect(batch.events[0].content.title).toBe("car: A → B");
298
- });
299
-
300
- it("throws on missing payload", () => {
301
- expect(() => new BaiduMapAdapter().normalize(null)).toThrow(
302
- /payload missing/,
303
- );
304
- });
305
- });