@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,204 +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
- DidiAdapter,
11
- parseRecords,
12
- orderToRecord,
13
- extractOrders,
14
- parseFareYuan,
15
- NAME,
16
- VERSION,
17
- } = require("../../lib/adapters/travel-didi");
18
-
19
- function writeTmp(content) {
20
- const p = path.join(os.tmpdir(), `cc-didi-${crypto.randomUUID()}.json`);
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
- const COOKIES = "es_token=abc; ticket=xyz";
32
-
33
- const RIDE = {
34
- orderId: "DD1",
35
- fromAddress: "公司",
36
- toAddress: "机场",
37
- departTime: 1716383021000,
38
- arriveTime: 1716386021000,
39
- fare: "58.50",
40
- productName: "专车",
41
- passengerName: "张三",
42
- };
43
-
44
- describe("constants + parseFareYuan", () => {
45
- it("exposes name/version", () => {
46
- expect(NAME).toBe("travel-didi");
47
- expect(VERSION).toBe("0.1.0");
48
- });
49
- it("parseFareYuan: 元 decimal kept; large integer treated as 分", () => {
50
- expect(parseFareYuan("58.50")).toBe(58.5);
51
- expect(parseFareYuan(58.5)).toBe(58.5);
52
- expect(parseFareYuan(5850)).toBe(58.5); // 分
53
- expect(parseFareYuan(58)).toBe(58); // small int → 元
54
- expect(parseFareYuan("x")).toBe(0);
55
- });
56
- });
57
-
58
- describe("orderToRecord", () => {
59
- it("maps a ride → car TravelRecord (from/to name, fare, product)", () => {
60
- const rec = orderToRecord(RIDE, { capturedVia: "cookie-api" });
61
- expect(rec).toMatchObject({
62
- vendorId: "didi",
63
- recordId: "DD1",
64
- vehicleType: "car",
65
- from: { name: "公司" },
66
- to: { name: "机场" },
67
- departureMs: 1716383021000,
68
- arrivalMs: 1716386021000,
69
- carrier: "滴滴",
70
- traveler: "张三",
71
- });
72
- expect(rec.totalCost).toEqual({ value: 58.5, currency: "CNY" });
73
- expect(rec.extras.productType).toBe("专车");
74
- expect(rec.extras.capturedVia).toBe("cookie-api");
75
- });
76
- it("snake_case + seconds-epoch aliases; drops id-less", () => {
77
- const rec = orderToRecord({ oid: "DD2", from_address: "A", to_address: "B", depart_time: 1716383021, total_fee: 5850 });
78
- expect(rec.recordId).toBe("DD2");
79
- expect(rec.departureMs).toBe(1716383021000); // seconds → ms
80
- expect(rec.totalCost.value).toBe(58.5);
81
- expect(orderToRecord({ from_address: "A" })).toBe(null);
82
- });
83
- it("extractOrders tolerant of shapes", () => {
84
- expect(extractOrders({ orders: [{ orderId: 1 }] })).toHaveLength(1);
85
- expect(extractOrders({ data: { list: [{ orderId: 1 }] } })).toHaveLength(1);
86
- expect(extractOrders({})).toEqual([]);
87
- });
88
- });
89
-
90
- describe("DidiAdapter file/snapshot mode", () => {
91
- it("authenticate ready without account; validates inputPath", async () => {
92
- const a = new DidiAdapter();
93
- expect(await a.authenticate({})).toEqual({ ok: true, account: null, mode: "ready" });
94
- const p = writeTmp("[]");
95
- try {
96
- expect((await a.authenticate({ inputPath: p })).mode).toBe("snapshot-file");
97
- expect((await a.authenticate({ inputPath: path.join(os.tmpdir(), "nope-dd.json") })).reason).toBe("INPUT_PATH_UNREADABLE");
98
- } finally {
99
- fs.unlinkSync(p);
100
- }
101
- });
102
-
103
- it("sync yields ride + normalize end-to-end (car trip)", async () => {
104
- const p = writeTmp(JSON.stringify([RIDE]));
105
- try {
106
- const a = new DidiAdapter();
107
- const items = await collect(a.sync({ inputPath: p }));
108
- expect(items).toHaveLength(1);
109
- expect(items[0]).toMatchObject({ adapter: NAME, originalId: "DD1" });
110
- const batch = a.normalize(items[0]);
111
- expect(batch.events[0].content.title).toBe("car: 公司 → 机场");
112
- expect(batch.events[0].content.amount).toEqual({ value: 58.5, currency: "CNY", direction: "out" });
113
- expect(batch.persons.find((x) => x.subtype === "merchant").names).toEqual(["滴滴"]);
114
- } finally {
115
- fs.unlinkSync(p);
116
- }
117
- });
118
-
119
- it("parseRecords JSONL + {orders} envelope; sync silent when no path", async () => {
120
- expect(parseRecords(JSON.stringify({ orders: [RIDE] }))).toHaveLength(1);
121
- const jsonl = `${JSON.stringify(RIDE)}\njunk\n${JSON.stringify({ ...RIDE, orderId: "DD9" })}`;
122
- expect(parseRecords(jsonl).map((r) => r.recordId)).toEqual(["DD1", "DD9"]);
123
- const a = new DidiAdapter();
124
- expect(await collect(a.sync({}))).toEqual([]);
125
- });
126
-
127
- it("normalize throws on missing record", () => {
128
- const a = new DidiAdapter();
129
- expect(() => a.normalize({ payload: {} })).toThrow(/payload\.record missing/);
130
- });
131
- });
132
-
133
- describe("DidiAdapter cookie-api mode", () => {
134
- it("authenticate cookie mode (account optional)", async () => {
135
- const a = new DidiAdapter({ account: { cookies: COOKIES } });
136
- expect(await a.authenticate()).toEqual({ ok: true, account: null, mode: "cookie" });
137
- const ready = new DidiAdapter({ account: { cookies: "" } });
138
- expect((await ready.authenticate()).mode).toBe("ready");
139
- });
140
-
141
- it("sync fetches, paginates, maps + normalizes", async () => {
142
- const pages = [
143
- { data: { list: [{ orderId: "CK1", fromAddress: "家", toAddress: "公司", departTime: 1716383021, fare: 2300 }] } },
144
- { data: { list: [] } },
145
- ];
146
- const calls = [];
147
- const fetchFn = async ({ url, cookies, query, sign }) => {
148
- calls.push({ url, cookies, pageIndex: query.pageIndex, sign });
149
- return pages[query.pageIndex - 1] || { data: { list: [] } };
150
- };
151
- const a = new DidiAdapter({ account: { cookies: COOKIES }, fetchFn });
152
- const items = await collect(a.sync({ sinceWatermark: 0 }));
153
- expect(items).toHaveLength(1);
154
- expect(items[0].originalId).toBe("CK1");
155
- expect(calls[0].cookies).toBe(COOKIES);
156
- expect(calls[0].sign).toBe(null);
157
- const batch = a.normalize(items[0]);
158
- expect(batch.events[0].content.title).toBe("car: 家 → 公司");
159
- expect(batch.events[0].content.amount.value).toBe(23); // 2300 分 → 23 元
160
- });
161
-
162
- it("invokes signProvider when configured", async () => {
163
- const signCalls = [];
164
- const a = new DidiAdapter({
165
- account: { cookies: COOKIES },
166
- fetchFn: async ({ query }) => (query.pageIndex === 1 ? { orders: [{ orderId: "S1" }] } : { orders: [] }),
167
- signProvider: async (ctx) => { signCalls.push(ctx); return "DD-SIG"; },
168
- });
169
- const items = await collect(a.sync({ sinceWatermark: 0 }));
170
- expect(items).toHaveLength(1);
171
- expect(signCalls.length).toBeGreaterThan(0);
172
- expect(signCalls[0].cookies).toBe(COOKIES);
173
- });
174
-
175
- it("sinceWatermark + limit + empty/login response + default fetch throws", async () => {
176
- const a1 = new DidiAdapter({
177
- account: { cookies: COOKIES },
178
- fetchFn: async () => ({ orders: [
179
- { orderId: "NEW", departTime: 2_000_000_000_000 },
180
- { orderId: "OLD", departTime: 1_000_000_000_000 },
181
- ] }),
182
- });
183
- expect((await collect(a1.sync({ sinceWatermark: 1_500_000_000_000, maxPages: 1 }))).map((x) => x.originalId)).toEqual(["NEW"]);
184
-
185
- const a2 = new DidiAdapter({ account: { cookies: COOKIES }, fetchFn: async () => "<html>login</html>" });
186
- expect(await collect(a2.sync({ sinceWatermark: 0 }))).toEqual([]);
187
-
188
- const a3 = new DidiAdapter({ account: { cookies: COOKIES } });
189
- await expect(collect(a3.sync({ sinceWatermark: 0 }))).rejects.toThrow(/no fetchFn configured/);
190
- });
191
-
192
- it("file path takes priority over cookie mode", async () => {
193
- const p = writeTmp(JSON.stringify([RIDE]));
194
- try {
195
- const a = new DidiAdapter({
196
- account: { cookies: COOKIES },
197
- fetchFn: async () => { throw new Error("must not fetch in file mode"); },
198
- });
199
- expect((await collect(a.sync({ inputPath: p }))).map((x) => x.originalId)).toEqual(["DD1"]);
200
- } finally {
201
- fs.unlinkSync(p);
202
- }
203
- });
204
- });
@@ -1,207 +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
- TencentMapAdapter,
11
- NAME,
12
- VERSION,
13
- SNAPSHOT_SCHEMA_VERSION,
14
- } = require("../../lib/adapters/travel-tencent-map");
15
-
16
- function writeTmp(content, ext = "json") {
17
- const p = path.join(
18
- os.tmpdir(),
19
- `cc-tencentmap-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: "tencent-map",
53
- account: { uid: "U1" },
54
- events: [
55
- {
56
- kind: "favourite",
57
- id: "fav-1",
58
- capturedAt: 1716383021000,
59
- name: "公司",
60
- lat: 31.2,
61
- lng: 121.44,
62
- category: "company",
63
- },
64
- {
65
- kind: "route",
66
- id: "route-2",
67
- capturedAt: 1716383021000,
68
- from: { name: "公司" },
69
- to: { name: "体育馆" },
70
- mode: "bike",
71
- },
72
- ],
73
- };
74
-
75
- describe("constants", () => {
76
- it("exposes name/version/schema", () => {
77
- expect(NAME).toBe("travel-tencent-map");
78
- expect(VERSION).toBe("0.2.0");
79
- expect(SNAPSHOT_SCHEMA_VERSION).toBe(1);
80
- });
81
- });
82
-
83
- describe("authenticate", () => {
84
- it("mirrors baidu: snapshot / sqlite-needs-deviceId / NO_INPUT", async () => {
85
- const p = writeTmp("{}");
86
- try {
87
- const a = new TencentMapAdapter();
88
- expect((await a.authenticate({ inputPath: p })).mode).toBe(
89
- "snapshot-file",
90
- );
91
- expect(
92
- (
93
- await new TencentMapAdapter({ dbPath: "x.db" }).authenticate({})
94
- ).reason,
95
- ).toBe("NO_ACCOUNT_DEVICE_ID");
96
- expect((await a.authenticate({})).reason).toBe("NO_INPUT");
97
- } finally {
98
- fs.unlinkSync(p);
99
- }
100
- });
101
- });
102
-
103
- describe("sync — snapshot mode", () => {
104
- it("yields events with tencent-map: prefixed originalId", async () => {
105
- const p = writeTmp(JSON.stringify(SNAPSHOT));
106
- try {
107
- const a = new TencentMapAdapter();
108
- const items = await collect(a.sync({ inputPath: p }));
109
- expect(items.map((i) => i.originalId)).toEqual([
110
- "tencent-map:favourite:fav-1",
111
- "tencent-map:route:route-2",
112
- ]);
113
- expect(items[0].payload.account).toEqual({ uid: "U1" });
114
- } finally {
115
- fs.unlinkSync(p);
116
- }
117
- });
118
-
119
- it("throws on schemaVersion mismatch", async () => {
120
- const p = writeTmp(JSON.stringify({ schemaVersion: 2, events: [] }));
121
- try {
122
- await expect(
123
- collect(new TencentMapAdapter().sync({ inputPath: p })),
124
- ).rejects.toThrow(/schemaVersion mismatch/);
125
- } finally {
126
- fs.unlinkSync(p);
127
- }
128
- });
129
- });
130
-
131
- describe("sync — sqlite mode (fake driver)", () => {
132
- it("yields rows + falls back to tencent_* legacy tables (keyword alias)", async () => {
133
- const p = writeTmp("fake", "db");
134
- const log = {};
135
- try {
136
- const a = new TencentMapAdapter({
137
- dbPath: p,
138
- account: { deviceId: "DEV1" },
139
- dbDriverFactory: makeFakeDriverFactory(
140
- {
141
- tencent_route_history: [
142
- {
143
- _id: 5,
144
- mode: "walk",
145
- start_name: "家",
146
- end_name: "菜场",
147
- time: 1716383021,
148
- },
149
- ],
150
- tencent_search_history: [
151
- { _id: 6, keyword: "奶茶", city: "深圳", time: 1716383021000 },
152
- ],
153
- },
154
- log,
155
- ),
156
- });
157
- const items = await collect(a.sync({}));
158
- expect(items).toHaveLength(2);
159
- expect(items[0].payload.record).toMatchObject({
160
- vendorId: "tencentmap",
161
- recordId: "route-5",
162
- vehicleType: "walk",
163
- carrier: "腾讯地图",
164
- departureMs: 1716383021 * 1000,
165
- });
166
- // searchRowToRecord accepts the tencent `keyword` alias
167
- expect(items[1].payload.record.to).toMatchObject({
168
- name: "奶茶",
169
- city: "深圳",
170
- });
171
- expect(log.closed).toBe(true);
172
- } finally {
173
- fs.unlinkSync(p);
174
- }
175
- });
176
-
177
- it("requires account.deviceId at sync time", async () => {
178
- const a = new TencentMapAdapter({ dbPath: "x.db" });
179
- await expect(collect(a.sync({}))).rejects.toThrow(
180
- /account\.deviceId required/,
181
- );
182
- });
183
- });
184
-
185
- describe("normalize", () => {
186
- it("snapshot route → bike trip titled by place names", async () => {
187
- const p = writeTmp(JSON.stringify(SNAPSHOT));
188
- try {
189
- const a = new TencentMapAdapter();
190
- const [fav, route] = await collect(a.sync({ inputPath: p }));
191
- expect(a.normalize(fav).events[0].content.title).toBe("visit: → 公司");
192
- const batch = a.normalize(route);
193
- expect(batch.events[0].content.title).toBe("bike: 公司 → 体育馆");
194
- expect(
195
- batch.persons.find((x) => x.subtype === "merchant").names,
196
- ).toEqual(["腾讯地图"]);
197
- } finally {
198
- fs.unlinkSync(p);
199
- }
200
- });
201
-
202
- it("throws on missing payload", () => {
203
- expect(() => new TencentMapAdapter().normalize(null)).toThrow(
204
- /payload missing/,
205
- );
206
- });
207
- });
@@ -1,289 +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
- TongchengAdapter,
11
- parseRecords,
12
- orderToRecord,
13
- extractOrders,
14
- TYPE_MAP,
15
- NAME,
16
- VERSION,
17
- } = require("../../lib/adapters/travel-tongcheng");
18
-
19
- function writeTmp(content) {
20
- const p = path.join(os.tmpdir(), `cc-tc-${crypto.randomUUID()}.json`);
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
- const FLIGHT_ORDER = {
32
- orderId: "TC1",
33
- projectType: "flight",
34
- departureCity: "上海",
35
- arrivalCity: "北京",
36
- departureDate: 1716383021000,
37
- airlineName: "东方航空",
38
- flightNumber: "MU5101",
39
- amount: "1180.00",
40
- passengerName: "张三",
41
- };
42
-
43
- const COOKIES = "clientId=abc; tc_token=xyz; memberId=m1";
44
-
45
- describe("constants + TYPE_MAP", () => {
46
- it("exposes name/version", () => {
47
- expect(NAME).toBe("travel-tongcheng");
48
- expect(VERSION).toBe("0.1.0");
49
- });
50
-
51
- it("maps tongcheng order types (incl. Chinese + scenery→attraction)", () => {
52
- expect(TYPE_MAP.flight).toBe("flight");
53
- expect(TYPE_MAP["机票"]).toBe("flight");
54
- expect(TYPE_MAP["酒店"]).toBe("hotel");
55
- expect(TYPE_MAP["火车票"]).toBe("train");
56
- expect(TYPE_MAP["门票"]).toBe("attraction");
57
- expect(TYPE_MAP["汽车票"]).toBe("bus");
58
- });
59
- });
60
-
61
- describe("parseRecords / orderToRecord", () => {
62
- it("parses flight order (projectType/amount/departureCity aliases)", () => {
63
- const recs = parseRecords(JSON.stringify([FLIGHT_ORDER]));
64
- expect(recs).toHaveLength(1);
65
- expect(recs[0]).toMatchObject({
66
- vendorId: "tongcheng",
67
- recordId: "TC1",
68
- vehicleType: "flight",
69
- from: { city: "上海" },
70
- to: { city: "北京" },
71
- departureMs: 1716383021000,
72
- carrier: "东方航空",
73
- vehicleNumber: "MU5101",
74
- traveler: "张三",
75
- });
76
- expect(recs[0].totalCost).toEqual({ value: 1180, currency: "CNY" });
77
- });
78
-
79
- it("hotel order: hotelCity→to, hotelName→carrier, checkIn→time", () => {
80
- const recs = parseRecords(
81
- JSON.stringify([
82
- {
83
- orderId: "H1",
84
- projectType: "hotel",
85
- hotelCity: "杭州",
86
- hotelName: "西湖国宾馆",
87
- checkIn: "2026年5月1日",
88
- linkName: "李四",
89
- nights: 2,
90
- },
91
- ]),
92
- );
93
- expect(recs[0]).toMatchObject({
94
- recordId: "H1",
95
- vehicleType: "hotel",
96
- to: { city: "杭州" },
97
- carrier: "西湖国宾馆",
98
- traveler: "李四",
99
- });
100
- expect(recs[0].from).toBe(null);
101
- expect(recs[0].extras.nights).toBe(2);
102
- });
103
-
104
- it("scenery/门票 order maps to attraction; sceneryName→carrier/to", () => {
105
- const rec = orderToRecord({ orderId: "S1", projectType: "门票", sceneryName: "黄山风景区", amount: 230 });
106
- expect(rec.vehicleType).toBe("attraction");
107
- expect(rec.carrier).toBe("黄山风景区");
108
- expect(rec.totalCost.value).toBe(230);
109
- });
110
-
111
- it("unknown type → trip + default carrier 同程; drops id-less rows", () => {
112
- expect(orderToRecord({ order_no: "X1" }).vehicleType).toBe("trip");
113
- expect(orderToRecord({ order_no: "X1" }).carrier).toBe("同程");
114
- expect(orderToRecord({ projectType: "flight" })).toBe(null);
115
- });
116
-
117
- it("accepts {orders:[...]} envelope + JSONL fallback", () => {
118
- expect(parseRecords(JSON.stringify({ orders: [FLIGHT_ORDER] }))).toHaveLength(1);
119
- const jsonl = `${JSON.stringify(FLIGHT_ORDER)}\ngarbage\n${JSON.stringify({ ...FLIGHT_ORDER, orderId: "TC2" })}`;
120
- expect(parseRecords(jsonl).map((r) => r.recordId)).toEqual(["TC1", "TC2"]);
121
- });
122
- });
123
-
124
- describe("extractOrders", () => {
125
- it("pulls list from common shapes", () => {
126
- expect(extractOrders({ orders: [{ orderId: "A" }] })).toHaveLength(1);
127
- expect(extractOrders({ data: { orderList: [{ orderId: "B" }] } })).toHaveLength(1);
128
- expect(extractOrders({ data: { records: [{ orderId: "C" }] } })).toHaveLength(1);
129
- expect(extractOrders({})).toEqual([]);
130
- expect(extractOrders(null)).toEqual([]);
131
- });
132
- });
133
-
134
- describe("TongchengAdapter file/snapshot mode", () => {
135
- it("authenticate ready mode without account", async () => {
136
- const a = new TongchengAdapter();
137
- expect(await a.authenticate({})).toEqual({ ok: true, account: null, mode: "ready" });
138
- });
139
-
140
- it("authenticate validates inputPath readability", async () => {
141
- const p = writeTmp("[]");
142
- try {
143
- const a = new TongchengAdapter();
144
- expect((await a.authenticate({ inputPath: p })).mode).toBe("snapshot-file");
145
- const bad = await a.authenticate({ inputPath: path.join(os.tmpdir(), "nope-tc.json") });
146
- expect(bad.ok).toBe(false);
147
- expect(bad.reason).toBe("INPUT_PATH_UNREADABLE");
148
- } finally {
149
- fs.unlinkSync(p);
150
- }
151
- });
152
-
153
- it("sync yields orders from file + normalize end-to-end", async () => {
154
- const p = writeTmp(JSON.stringify([FLIGHT_ORDER]));
155
- try {
156
- const a = new TongchengAdapter();
157
- const items = await collect(a.sync({ inputPath: p }));
158
- expect(items).toHaveLength(1);
159
- expect(items[0]).toMatchObject({ adapter: NAME, originalId: "TC1" });
160
- const batch = a.normalize(items[0]);
161
- expect(batch.events[0].content.title).toBe("flight: 上海 → 北京");
162
- expect(batch.events[0].content.amount).toEqual({ value: 1180, currency: "CNY", direction: "out" });
163
- expect(batch.persons.find((x) => x.subtype === "merchant").names).toEqual(["东方航空"]);
164
- } finally {
165
- fs.unlinkSync(p);
166
- }
167
- });
168
-
169
- it("sync returns silently when no path / missing file", async () => {
170
- const a = new TongchengAdapter();
171
- expect(await collect(a.sync({}))).toEqual([]);
172
- expect(await collect(a.sync({ inputPath: path.join(os.tmpdir(), "nope-tc2.json") }))).toEqual([]);
173
- });
174
-
175
- it("normalize throws on missing record", () => {
176
- const a = new TongchengAdapter();
177
- expect(() => a.normalize({ payload: {} })).toThrow(/payload\.record missing/);
178
- });
179
- });
180
-
181
- describe("TongchengAdapter cookie-api mode", () => {
182
- it("authenticate cookie mode (account OPTIONAL)", async () => {
183
- const a = new TongchengAdapter({ account: { cookies: COOKIES } });
184
- expect(await a.authenticate()).toEqual({ ok: true, account: null, mode: "cookie" });
185
- });
186
-
187
- it("empty cookies → ready (not cookie mode)", async () => {
188
- const a = new TongchengAdapter({ account: { cookies: "" } });
189
- expect((await a.authenticate()).mode).toBe("ready");
190
- });
191
-
192
- it("sync fetches, paginates, maps + normalizes", async () => {
193
- const pages = [
194
- {
195
- orderList: [
196
- { orderId: "CK1", projectType: "train", departureCity: "广州", arrivalCity: "深圳", amount: "75", orderDate: 1716383021000, trainNo: "G6501" },
197
- ],
198
- },
199
- { orderList: [] },
200
- ];
201
- const calls = [];
202
- const fetchFn = async ({ url, cookies, query, sign }) => {
203
- calls.push({ url, cookies, pageIndex: query.pageIndex, sign });
204
- return pages[query.pageIndex - 1] || { orderList: [] };
205
- };
206
- const a = new TongchengAdapter({ account: { cookies: COOKIES }, fetchFn });
207
- const items = await collect(a.sync({ sinceWatermark: 0 }));
208
- expect(items).toHaveLength(1);
209
- expect(items[0]).toMatchObject({ adapter: NAME, originalId: "CK1" });
210
- expect(calls[0].cookies).toBe(COOKIES);
211
- expect(calls[0].sign).toBe(null);
212
- const batch = a.normalize(items[0]);
213
- expect(batch.events[0].content.title).toBe("train: 广州 → 深圳");
214
- });
215
-
216
- it("invokes signProvider when configured", async () => {
217
- const signCalls = [];
218
- const a = new TongchengAdapter({
219
- account: { cookies: COOKIES },
220
- fetchFn: async ({ query }) =>
221
- query.pageIndex === 1 ? { orders: [{ orderId: "S1", projectType: "hotel" }] } : { orders: [] },
222
- signProvider: async (ctx) => {
223
- signCalls.push(ctx);
224
- return "TC-SIGN";
225
- },
226
- });
227
- const items = await collect(a.sync({ sinceWatermark: 0 }));
228
- expect(items).toHaveLength(1);
229
- expect(signCalls).toHaveLength(1);
230
- expect(signCalls[0].cookies).toBe(COOKIES);
231
- });
232
-
233
- it("stops at sinceWatermark", async () => {
234
- const a = new TongchengAdapter({
235
- account: { cookies: COOKIES },
236
- fetchFn: async () => ({
237
- orderList: [
238
- { orderId: "NEW", projectType: "flight", orderDate: 2_000_000_000_000 },
239
- { orderId: "OLD", projectType: "flight", orderDate: 1_000_000_000_000 },
240
- ],
241
- }),
242
- });
243
- const items = await collect(a.sync({ sinceWatermark: 1_500_000_000_000, maxPages: 1 }));
244
- expect(items.map((x) => x.originalId)).toEqual(["NEW"]);
245
- });
246
-
247
- it("respects opts.limit", async () => {
248
- const a = new TongchengAdapter({
249
- account: { cookies: COOKIES },
250
- fetchFn: async () => ({
251
- orderList: [
252
- { orderId: "L1", projectType: "flight", orderDate: 2_000_000_000_000 },
253
- { orderId: "L2", projectType: "flight", orderDate: 2_000_000_000_001 },
254
- ],
255
- }),
256
- });
257
- const items = await collect(a.sync({ sinceWatermark: 0, limit: 1, maxPages: 1 }));
258
- expect(items).toHaveLength(1);
259
- });
260
-
261
- it("empty/login-redirect response yields zero (no crash)", async () => {
262
- const a = new TongchengAdapter({
263
- account: { cookies: COOKIES },
264
- fetchFn: async () => "<html>login</html>",
265
- });
266
- expect(await collect(a.sync({ sinceWatermark: 0 }))).toEqual([]);
267
- });
268
-
269
- it("default fetch throws when no fetchFn", async () => {
270
- const a = new TongchengAdapter({ account: { cookies: COOKIES } });
271
- await expect(collect(a.sync({ sinceWatermark: 0 }))).rejects.toThrow(/no fetchFn configured/);
272
- });
273
-
274
- it("file path takes priority over cookie mode", async () => {
275
- const p = writeTmp(JSON.stringify([FLIGHT_ORDER]));
276
- try {
277
- const a = new TongchengAdapter({
278
- account: { cookies: COOKIES },
279
- fetchFn: async () => {
280
- throw new Error("fetchFn should NOT be called in file mode");
281
- },
282
- });
283
- const items = await collect(a.sync({ inputPath: p }));
284
- expect(items.map((x) => x.originalId)).toEqual(["TC1"]);
285
- } finally {
286
- fs.unlinkSync(p);
287
- }
288
- });
289
- });