@chainlesschain/personal-data-hub 0.4.29 → 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 (198) hide show
  1. package/lib/prompt-builder.js +15 -1
  2. package/package.json +4 -1
  3. package/__tests__/adapter-guide.test.js +0 -47
  4. package/__tests__/adapter-spec.test.js +0 -78
  5. package/__tests__/adapters/ai-chat-cookie-capture-spec.test.js +0 -211
  6. package/__tests__/adapters/ai-chat-health-checker.test.js +0 -262
  7. package/__tests__/adapters/ai-chat-history.test.js +0 -396
  8. package/__tests__/adapters/ai-chat-http-client.test.js +0 -242
  9. package/__tests__/adapters/ai-chat-vendors.test.js +0 -874
  10. package/__tests__/adapters/alipay-bill-adapter.test.js +0 -538
  11. package/__tests__/adapters/apple-health.test.js +0 -95
  12. package/__tests__/adapters/bank-family.test.js +0 -125
  13. package/__tests__/adapters/biz-tianyancha.test.js +0 -159
  14. package/__tests__/adapters/browser-history-chrome.test.js +0 -377
  15. package/__tests__/adapters/browser-history-edge.test.js +0 -159
  16. package/__tests__/adapters/car-mercedesme.test.js +0 -74
  17. package/__tests__/adapters/doc-baidu-netdisk.test.js +0 -102
  18. package/__tests__/adapters/doc-camscanner.test.js +0 -147
  19. package/__tests__/adapters/doc-platforms.test.js +0 -177
  20. package/__tests__/adapters/edu-huawei-learning-live.test.js +0 -198
  21. package/__tests__/adapters/edu-zuoyebang-live.test.js +0 -226
  22. package/__tests__/adapters/email-adapter-snapshot.test.js +0 -237
  23. package/__tests__/adapters/email-adapter.test.js +0 -742
  24. package/__tests__/adapters/email-classifier.test.js +0 -347
  25. package/__tests__/adapters/email-imap-session.test.js +0 -334
  26. package/__tests__/adapters/email-parser.test.js +0 -244
  27. package/__tests__/adapters/email-pdf-extractor.test.js +0 -529
  28. package/__tests__/adapters/email-providers.test.js +0 -84
  29. package/__tests__/adapters/email-retry-progress.test.js +0 -294
  30. package/__tests__/adapters/email-templates.test.js +0 -822
  31. package/__tests__/adapters/family-23-collectors-scaffold.test.js +0 -182
  32. package/__tests__/adapters/finance-alipay-live.test.js +0 -258
  33. package/__tests__/adapters/finance-dcep.test.js +0 -74
  34. package/__tests__/adapters/fitness-joyrun.test.js +0 -82
  35. package/__tests__/adapters/game-genshin-live.test.js +0 -238
  36. package/__tests__/adapters/game-genshin-scaffold.test.js +0 -108
  37. package/__tests__/adapters/game-honor-of-kings-live.test.js +0 -230
  38. package/__tests__/adapters/git-activity.test.js +0 -222
  39. package/__tests__/adapters/gov-12123.test.js +0 -103
  40. package/__tests__/adapters/gov-ixiamen.test.js +0 -150
  41. package/__tests__/adapters/gov-tax.test.js +0 -135
  42. package/__tests__/adapters/health-meiyou.test.js +0 -125
  43. package/__tests__/adapters/local-files.test.js +0 -264
  44. package/__tests__/adapters/local-im-pc.test.js +0 -154
  45. package/__tests__/adapters/messaging-whatsapp.test.js +0 -289
  46. package/__tests__/adapters/music-kugou.test.js +0 -187
  47. package/__tests__/adapters/music-qq.test.js +0 -112
  48. package/__tests__/adapters/netease-music-live.test.js +0 -244
  49. package/__tests__/adapters/netease-music.test.js +0 -74
  50. package/__tests__/adapters/pc-local-discovery.test.js +0 -141
  51. package/__tests__/adapters/qq-pc-direct-read.test.js +0 -227
  52. package/__tests__/adapters/reading-family.test.js +0 -108
  53. package/__tests__/adapters/recruit-boss.test.js +0 -180
  54. package/__tests__/adapters/shell-history.test.js +0 -180
  55. package/__tests__/adapters/shopping-base.test.js +0 -179
  56. package/__tests__/adapters/shopping-dianping.test.js +0 -239
  57. package/__tests__/adapters/social-bilibili-adb-api-client.test.js +0 -721
  58. package/__tests__/adapters/social-bilibili-adb-chromium-cookies-reader.test.js +0 -346
  59. package/__tests__/adapters/social-bilibili-adb-collector.test.js +0 -284
  60. package/__tests__/adapters/social-bilibili-adb-cookies-extension.test.js +0 -343
  61. package/__tests__/adapters/social-bilibili-adb-snapshot-builder.test.js +0 -296
  62. package/__tests__/adapters/social-csdn.test.js +0 -175
  63. package/__tests__/adapters/social-dongchedi.test.js +0 -165
  64. package/__tests__/adapters/social-douyin-adb-aweme-detail.test.js +0 -165
  65. package/__tests__/adapters/social-douyin-adb-collector.test.js +0 -254
  66. package/__tests__/adapters/social-douyin-adb-db-extension.test.js +0 -114
  67. package/__tests__/adapters/social-douyin-adb-im-db-parser.test.js +0 -304
  68. package/__tests__/adapters/social-douyin-adb-snapshot-builder.test.js +0 -216
  69. package/__tests__/adapters/social-douyin-adb-usage-profile.test.js +0 -229
  70. package/__tests__/adapters/social-douyin-adb-watch-history.test.js +0 -269
  71. package/__tests__/adapters/social-kuaishou-adb-api-client.test.js +0 -496
  72. package/__tests__/adapters/social-kuaishou-adb-collector.test.js +0 -276
  73. package/__tests__/adapters/social-kuaishou-adb-cookies-extension.test.js +0 -152
  74. package/__tests__/adapters/social-kuaishou-adb-snapshot-builder.test.js +0 -178
  75. package/__tests__/adapters/social-toutiao-adb-account-reader.test.js +0 -135
  76. package/__tests__/adapters/social-toutiao-adb-api-client.test.js +0 -626
  77. package/__tests__/adapters/social-toutiao-adb-article.test.js +0 -155
  78. package/__tests__/adapters/social-toutiao-adb-collector.test.js +0 -378
  79. package/__tests__/adapters/social-toutiao-adb-cookies-extension.test.js +0 -193
  80. package/__tests__/adapters/social-toutiao-adb-snapshot-builder.test.js +0 -196
  81. package/__tests__/adapters/social-toutiao-kuaishou-scaffold.test.js +0 -311
  82. package/__tests__/adapters/social-weibo-adb-api-client.test.js +0 -362
  83. package/__tests__/adapters/social-weibo-adb-collector.test.js +0 -201
  84. package/__tests__/adapters/social-weibo-adb-cookies-extension.test.js +0 -167
  85. package/__tests__/adapters/social-weibo-adb-snapshot-builder.test.js +0 -189
  86. package/__tests__/adapters/social-xiaohongshu-adb-api-client.test.js +0 -431
  87. package/__tests__/adapters/social-xiaohongshu-adb-collector.test.js +0 -207
  88. package/__tests__/adapters/social-xiaohongshu-adb-cookies-extension.test.js +0 -0
  89. package/__tests__/adapters/social-xiaohongshu-adb-sign-provider-injection.test.js +0 -351
  90. package/__tests__/adapters/social-xiaohongshu-adb-sign.test.js +0 -130
  91. package/__tests__/adapters/social-xiaohongshu-adb-snapshot-builder.test.js +0 -200
  92. package/__tests__/adapters/social-zhihu.test.js +0 -246
  93. package/__tests__/adapters/system-data-adapter.test.js +0 -443
  94. package/__tests__/adapters/system-data-android-ingest.test.js +0 -144
  95. package/__tests__/adapters/system-data-android.test.js +0 -519
  96. package/__tests__/adapters/system-data-disclosure.test.js +0 -153
  97. package/__tests__/adapters/travel-12306.test.js +0 -512
  98. package/__tests__/adapters/travel-amap.test.js +0 -219
  99. package/__tests__/adapters/travel-baidu-map.test.js +0 -305
  100. package/__tests__/adapters/travel-base.test.js +0 -205
  101. package/__tests__/adapters/travel-ctrip.test.js +0 -377
  102. package/__tests__/adapters/travel-didi-consumer.test.js +0 -66
  103. package/__tests__/adapters/travel-didi.test.js +0 -204
  104. package/__tests__/adapters/travel-tencent-map.test.js +0 -207
  105. package/__tests__/adapters/travel-tongcheng.test.js +0 -289
  106. package/__tests__/adapters/video-platforms.test.js +0 -152
  107. package/__tests__/adapters/video-xigua.test.js +0 -106
  108. package/__tests__/adapters/vscode.test.js +0 -299
  109. package/__tests__/adapters/wechat-bootstrap.test.js +0 -240
  110. package/__tests__/adapters/wechat-env-probe.test.js +0 -162
  111. package/__tests__/adapters/wechat-frida-agent.test.js +0 -322
  112. package/__tests__/adapters/wechat-frida-integration.test.js +0 -149
  113. package/__tests__/adapters/wechat-frida-key-provider.test.js +0 -188
  114. package/__tests__/adapters/wechat-md5-key-provider.test.js +0 -101
  115. package/__tests__/adapters/wechat-pc-direct-read.test.js +0 -365
  116. package/__tests__/adapters/wechat-pc-group-topic.test.js +0 -63
  117. package/__tests__/adapters/wechat-pc-v4-sidecar.test.js +0 -72
  118. package/__tests__/adapters/weread.test.js +0 -123
  119. package/__tests__/adapters/wework-pc.test.js +0 -124
  120. package/__tests__/adapters/win-recent.test.js +0 -192
  121. package/__tests__/analysis-skills.test.js +0 -754
  122. package/__tests__/analysis.test.js +0 -1845
  123. package/__tests__/audio-ximalaya-snapshot.test.js +0 -279
  124. package/__tests__/batch.test.js +0 -133
  125. package/__tests__/bridges-cc-kg.test.js +0 -231
  126. package/__tests__/bridges-cc-llm.test.js +0 -191
  127. package/__tests__/bridges-cc-rag.test.js +0 -162
  128. package/__tests__/categories.test.js +0 -92
  129. package/__tests__/e2e/ai-chat-cross-source-journey.test.js +0 -213
  130. package/__tests__/e2e/full-user-journey.test.js +0 -188
  131. package/__tests__/e2e/local-data-adapters-cli.e2e.test.js +0 -146
  132. package/__tests__/entity-resolver-ingest-hook.test.js +0 -177
  133. package/__tests__/entity-resolver-stages.test.js +0 -411
  134. package/__tests__/entity-resolver-vault.test.js +0 -249
  135. package/__tests__/entity-resolver.test.js +0 -526
  136. package/__tests__/fitness-keep-snapshot.test.js +0 -224
  137. package/__tests__/fixtures/entity-resolver-200-mock.json +0 -96
  138. package/__tests__/ids.test.js +0 -45
  139. package/__tests__/integration/ai-chat-history-registry.test.js +0 -228
  140. package/__tests__/integration/aichat-wizard-end-to-end.test.js +0 -282
  141. package/__tests__/integration/cross-adapter-pipelines.test.js +0 -396
  142. package/__tests__/integration/local-data-adapters-pipeline.test.js +0 -373
  143. package/__tests__/integration/social-bilibili-pipeline.test.js +0 -261
  144. package/__tests__/integration/wechat-bootstrap-end-to-end.test.js +0 -390
  145. package/__tests__/key-providers.test.js +0 -126
  146. package/__tests__/kg-derive.test.js +0 -219
  147. package/__tests__/llm-client.test.js +0 -122
  148. package/__tests__/longtail-adapters.test.js +0 -281
  149. package/__tests__/messaging-qq-snapshot.test.js +0 -294
  150. package/__tests__/mobile-extractor-encrypted.test.js +0 -460
  151. package/__tests__/mobile-extractor.test.js +0 -288
  152. package/__tests__/mock-adapter.test.js +0 -93
  153. package/__tests__/prompt-builder.test.js +0 -249
  154. package/__tests__/query-parser.test.js +0 -365
  155. package/__tests__/rag-derive.test.js +0 -169
  156. package/__tests__/registry-readiness.test.js +0 -292
  157. package/__tests__/registry.test.js +0 -420
  158. package/__tests__/salvage-ingest.test.js +0 -97
  159. package/__tests__/schemas.test.js +0 -331
  160. package/__tests__/shopping-adapters.test.js +0 -392
  161. package/__tests__/shopping-eleme-snapshot.test.js +0 -454
  162. package/__tests__/shopping-pinduoduo-snapshot.test.js +0 -484
  163. package/__tests__/shopping-snapshot.test.js +0 -438
  164. package/__tests__/shopping-vipshop-snapshot.test.js +0 -425
  165. package/__tests__/shopping-xianyu-snapshot.test.js +0 -451
  166. package/__tests__/sidecar-contacts-cross-validate.test.js +0 -186
  167. package/__tests__/sidecar-supervisor.test.js +0 -128
  168. package/__tests__/sign-providers.test.js +0 -62
  169. package/__tests__/social-adapters.test.js +0 -280
  170. package/__tests__/social-bilibili-snapshot.test.js +0 -278
  171. package/__tests__/social-douban-snapshot.test.js +0 -351
  172. package/__tests__/social-douyin-im-direct-read.test.js +0 -377
  173. package/__tests__/social-douyin-salvage-collector.test.js +0 -98
  174. package/__tests__/social-douyin-salvage-mapper.test.js +0 -90
  175. package/__tests__/social-douyin-snapshot.test.js +0 -256
  176. package/__tests__/social-kuaishou-snapshot.test.js +0 -362
  177. package/__tests__/social-toutiao-snapshot.test.js +0 -366
  178. package/__tests__/social-weibo-snapshot.test.js +0 -234
  179. package/__tests__/social-weibo-sqlite-device.test.js +0 -174
  180. package/__tests__/social-xiaohongshu-snapshot.test.js +0 -232
  181. package/__tests__/sqlite-leaf-salvage.test.js +0 -97
  182. package/__tests__/travel-adapters.test.js +0 -483
  183. package/__tests__/travel-maps-snapshot.test.js +0 -426
  184. package/__tests__/vault-driver-error.test.js +0 -74
  185. package/__tests__/vault-search-helpers.test.js +0 -104
  186. package/__tests__/vault-search.test.js +0 -423
  187. package/__tests__/vault.test.js +0 -767
  188. package/__tests__/wechat-adapter.test.js +0 -594
  189. package/__tests__/whatsapp-adapter.test.js +0 -138
  190. package/scripts/_make-fixture-all.js +0 -126
  191. package/scripts/_make-fixture-contacts.js +0 -84
  192. package/scripts/evaluate-entity-resolver.js +0 -213
  193. package/scripts/run-native-tests-sandbox.sh +0 -55
  194. package/scripts/smoke-phase-5-5.js +0 -196
  195. package/scripts/smoke-phase-5-7.js +0 -181
  196. package/scripts/smoke-system-data-contacts.js +0 -309
  197. package/scripts/smoke-system-data.js +0 -312
  198. package/vitest.config.js +0 -88
@@ -1,205 +0,0 @@
1
- "use strict";
2
-
3
- import { describe, it, expect } from "vitest";
4
-
5
- const {
6
- normalizeTravelRecord,
7
- parseChineseDateTime,
8
- placeIdFor,
9
- slug,
10
- } = require("../../lib/adapters/travel-base");
11
-
12
- describe("normalizeTravelRecord", () => {
13
- const fullRecord = {
14
- vendorId: "12306",
15
- recordId: "E123456789",
16
- vehicleType: "train",
17
- from: { station: "上海虹桥", city: "上海" },
18
- to: { station: "北京南", city: "北京" },
19
- departureMs: 1716383021000,
20
- arrivalMs: 1716401021000,
21
- carrier: "12306",
22
- vehicleNumber: "G35",
23
- totalCost: { value: 553.5, currency: "CNY" },
24
- traveler: "张三",
25
- confirmationCode: "E123456789",
26
- bookedAt: 1716000000000,
27
- extras: { seat: "二等座" },
28
- };
29
-
30
- it("throws without rec / recordId", () => {
31
- expect(() => normalizeTravelRecord(null)).toThrow(/rec required/);
32
- expect(() => normalizeTravelRecord({})).toThrow(/recordId required/);
33
- });
34
-
35
- it("emits 1 trip event with title, amount, vehicle metadata", () => {
36
- const batch = normalizeTravelRecord(fullRecord, {
37
- adapterName: "travel-12306",
38
- adapterVersion: "0.6.0",
39
- });
40
- expect(batch.events).toHaveLength(1);
41
- const ev = batch.events[0];
42
- expect(ev.type).toBe("event");
43
- expect(ev.subtype).toBe("trip");
44
- expect(ev.occurredAt).toBe(1716383021000); // departureMs wins
45
- expect(ev.content.title).toBe("train: 上海虹桥 → 北京南");
46
- expect(ev.content.amount).toEqual({
47
- value: 553.5,
48
- currency: "CNY",
49
- direction: "out",
50
- });
51
- expect(ev.source).toMatchObject({
52
- adapter: "travel-12306",
53
- adapterVersion: "0.6.0",
54
- originalId: "E123456789",
55
- });
56
- expect(ev.extra).toMatchObject({
57
- vehicleType: "train",
58
- vendorId: "12306",
59
- from: "上海虹桥",
60
- to: "北京南",
61
- vehicleNumber: "G35",
62
- confirmationCode: "E123456789",
63
- arrivalMs: 1716401021000,
64
- bookedAt: 1716000000000,
65
- });
66
- });
67
-
68
- it("emits from/to places with stable dedup-able ids", () => {
69
- const batch = normalizeTravelRecord(fullRecord, {
70
- adapterName: "travel-12306",
71
- });
72
- expect(batch.places).toHaveLength(2);
73
- expect(batch.places[0].name).toBe("上海虹桥");
74
- expect(batch.places[0].type).toBe("place");
75
- // same station on another trip → same place id (cross-trip dedup)
76
- const again = normalizeTravelRecord(
77
- { ...fullRecord, recordId: "OTHER" },
78
- { adapterName: "travel-12306" },
79
- );
80
- expect(again.places[0].id).toBe(batch.places[0].id);
81
- });
82
-
83
- it("emits carrier as merchant person + traveler as contact person", () => {
84
- const batch = normalizeTravelRecord(fullRecord, {
85
- adapterName: "travel-12306",
86
- });
87
- const merchant = batch.persons.find((p) => p.subtype === "merchant");
88
- const contact = batch.persons.find((p) => p.subtype === "contact");
89
- expect(merchant.names).toEqual(["12306"]);
90
- expect(contact.names).toEqual(["张三"]);
91
- // traveler becomes the actor; self + traveler + carrier participate
92
- const ev = batch.events[0];
93
- expect(ev.actor).toBe(contact.id);
94
- expect(ev.participants).toEqual(["person-self", contact.id, merchant.id]);
95
- });
96
-
97
- it("suppresses traveler person when traveler is self", () => {
98
- const batch = normalizeTravelRecord(fullRecord, {
99
- adapterName: "travel-12306",
100
- selfName: "张三",
101
- });
102
- expect(batch.persons.find((p) => p.subtype === "contact")).toBeUndefined();
103
- expect(batch.events[0].actor).toBe("person-self");
104
- });
105
-
106
- it("falls back occurredAt: departureMs → bookedAt → now", () => {
107
- const noDeparture = normalizeTravelRecord({
108
- recordId: "R1",
109
- bookedAt: 1716000000000,
110
- });
111
- expect(noDeparture.events[0].occurredAt).toBe(1716000000000);
112
- const before = Date.now();
113
- const bare = normalizeTravelRecord({ recordId: "R2" });
114
- expect(bare.events[0].occurredAt).toBeGreaterThanOrEqual(before);
115
- });
116
-
117
- it("builds partial titles when from/to missing", () => {
118
- const toOnly = normalizeTravelRecord({
119
- recordId: "R1",
120
- vehicleType: "visit",
121
- to: { name: "咖啡店" },
122
- });
123
- expect(toOnly.events[0].content.title).toBe("visit: → 咖啡店");
124
- const neither = normalizeTravelRecord({
125
- recordId: "R2",
126
- carrier: "高德地图",
127
- });
128
- expect(neither.events[0].content.title).toBe("trip: 高德地图");
129
- });
130
-
131
- it("omits amount when totalCost.value not finite", () => {
132
- const batch = normalizeTravelRecord({
133
- recordId: "R1",
134
- totalCost: { value: NaN },
135
- });
136
- expect(batch.events[0].content.amount).toBeUndefined();
137
- });
138
- });
139
-
140
- describe("parseChineseDateTime", () => {
141
- it("parses YYYY-MM-DD HH:MM:SS (local time)", () => {
142
- expect(parseChineseDateTime("2026-04-15 14:30:00")).toBe(
143
- new Date(2026, 3, 15, 14, 30, 0).getTime(),
144
- );
145
- });
146
-
147
- it("parses YYYY/MM/DD HH:MM", () => {
148
- expect(parseChineseDateTime("2026/4/15 14:30")).toBe(
149
- new Date(2026, 3, 15, 14, 30).getTime(),
150
- );
151
- });
152
-
153
- it("parses 2026年4月15日 14:30", () => {
154
- expect(parseChineseDateTime("2026年4月15日 14:30")).toBe(
155
- new Date(2026, 3, 15, 14, 30).getTime(),
156
- );
157
- });
158
-
159
- it("parses 2026年4月15日 (date only)", () => {
160
- expect(parseChineseDateTime("2026年4月15日")).toBe(
161
- new Date(2026, 3, 15).getTime(),
162
- );
163
- });
164
-
165
- it("parses ISO-with-Z via the first regex AS LOCAL TIME (documented quirk)", () => {
166
- // The YYYY-MM-DD[T ]HH:MM regex matches before the Date.parse fallback
167
- // and ignores the Z suffix — Chinese app dumps are local-time anyway.
168
- expect(parseChineseDateTime("2026-04-15T06:30:00.000Z")).toBe(
169
- new Date(2026, 3, 15, 6, 30, 0).getTime(),
170
- );
171
- });
172
-
173
- it("falls back to Date.parse for formats no regex matches", () => {
174
- expect(parseChineseDateTime("Apr 15, 2026")).toBe(
175
- Date.parse("Apr 15, 2026"),
176
- );
177
- });
178
-
179
- it("returns null for garbage / empty / non-string", () => {
180
- expect(parseChineseDateTime("not a date")).toBe(null);
181
- expect(parseChineseDateTime("")).toBe(null);
182
- expect(parseChineseDateTime(42)).toBe(null);
183
- });
184
- });
185
-
186
- describe("placeIdFor + slug", () => {
187
- it("keys by station first, lowercased + slugged", () => {
188
- expect(placeIdFor({ station: "北京南", city: "北京" }, "travel-12306")).toBe(
189
- "place-travel-12306-北京南",
190
- );
191
- expect(placeIdFor({ city: "Shanghai HongQiao" }, "a")).toBe(
192
- "place-a-shanghai-hongqiao",
193
- );
194
- });
195
-
196
- it("returns null for empty place", () => {
197
- expect(placeIdFor(null, "a")).toBe(null);
198
- });
199
-
200
- it("slug strips punctuation, keeps CJK + word chars + dash", () => {
201
- expect(slug("高德地图")).toBe("高德地图");
202
- expect(slug("Air China! (CA)")).toBe("air-china-ca");
203
- expect(slug("")).toBe("");
204
- });
205
- });
@@ -1,377 +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
- CtripAdapter,
11
- parseRecords,
12
- orderToRecord,
13
- extractOrders,
14
- TYPE_MAP,
15
- NAME,
16
- VERSION,
17
- } = require("../../lib/adapters/travel-ctrip");
18
-
19
- function writeTmp(content) {
20
- const p = path.join(os.tmpdir(), `cc-ctrip-test-${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: "CT1",
33
- type: "flight",
34
- fromCity: "上海",
35
- toCity: "北京",
36
- departureTime: 1716383021000,
37
- arrivalTime: 1716390000000,
38
- airline: "国航",
39
- flightNumber: "CA1234",
40
- price: "1280.00",
41
- passengerName: "张三",
42
- pnr: "ABC123",
43
- };
44
-
45
- describe("constants + TYPE_MAP", () => {
46
- it("exposes name/version", () => {
47
- expect(NAME).toBe("travel-ctrip");
48
- expect(VERSION).toBe("0.7.0");
49
- });
50
-
51
- it("maps ctrip order types to vehicleType", () => {
52
- expect(TYPE_MAP.flight).toBe("flight");
53
- expect(TYPE_MAP.airline).toBe("flight");
54
- expect(TYPE_MAP.hotel).toBe("hotel");
55
- expect(TYPE_MAP.train).toBe("train");
56
- expect(TYPE_MAP.cruise).toBe("cruise");
57
- });
58
- });
59
-
60
- describe("parseRecords", () => {
61
- it("parses flight order with aliases (airline → carrier, pnr → confirmation)", () => {
62
- const recs = parseRecords(JSON.stringify([FLIGHT_ORDER]));
63
- expect(recs).toHaveLength(1);
64
- expect(recs[0]).toMatchObject({
65
- vendorId: "ctrip",
66
- recordId: "CT1",
67
- vehicleType: "flight",
68
- from: { city: "上海" },
69
- to: { city: "北京" },
70
- departureMs: 1716383021000,
71
- carrier: "国航",
72
- vehicleNumber: "CA1234",
73
- traveler: "张三",
74
- confirmationCode: "ABC123",
75
- });
76
- expect(recs[0].totalCost).toEqual({ value: 1280, currency: "CNY" });
77
- });
78
-
79
- it("hotel order: hotelCity → to, hotelName → carrier, checkIn/Out → times", () => {
80
- const recs = parseRecords(
81
- JSON.stringify([
82
- {
83
- id: "H1",
84
- type: "hotel",
85
- hotelCity: "杭州",
86
- hotelName: "西湖宾馆",
87
- checkIn: "2026年4月15日",
88
- checkOut: "2026年4月17日",
89
- guestName: "李四",
90
- nights: 2,
91
- },
92
- ]),
93
- );
94
- expect(recs[0]).toMatchObject({
95
- recordId: "H1",
96
- vehicleType: "hotel",
97
- to: { city: "杭州" },
98
- carrier: "西湖宾馆",
99
- traveler: "李四",
100
- });
101
- expect(recs[0].from).toBe(null);
102
- expect(recs[0].departureMs).toBe(new Date(2026, 3, 15).getTime());
103
- expect(recs[0].extras.nights).toBe(2);
104
- });
105
-
106
- it("unknown type falls back to trip + default carrier 携程", () => {
107
- const recs = parseRecords(JSON.stringify([{ order_no: "X1" }]));
108
- expect(recs[0].vehicleType).toBe("trip");
109
- expect(recs[0].carrier).toBe("携程");
110
- });
111
-
112
- it("accepts {orders:[...]} envelope + JSONL fallback, drops id-less rows", () => {
113
- expect(
114
- parseRecords(JSON.stringify({ orders: [FLIGHT_ORDER] })),
115
- ).toHaveLength(1);
116
- const jsonl = `${JSON.stringify(FLIGHT_ORDER)}\ngarbage line\n${JSON.stringify({ ...FLIGHT_ORDER, orderId: "CT2" })}`;
117
- expect(parseRecords(jsonl).map((r) => r.recordId)).toEqual(["CT1", "CT2"]);
118
- expect(parseRecords(JSON.stringify([{ type: "flight" }]))).toHaveLength(0);
119
- });
120
- });
121
-
122
- describe("CtripAdapter", () => {
123
- it("authenticate ok in ready mode without account (v0.6 optional)", async () => {
124
- const a = new CtripAdapter();
125
- expect(await a.authenticate({})).toEqual({
126
- ok: true,
127
- account: null,
128
- mode: "ready",
129
- });
130
- });
131
-
132
- it("authenticate validates inputPath readability", async () => {
133
- const p = writeTmp("[]");
134
- try {
135
- const a = new CtripAdapter();
136
- expect((await a.authenticate({ inputPath: p })).mode).toBe(
137
- "snapshot-file",
138
- );
139
- const bad = await a.authenticate({
140
- inputPath: path.join(os.tmpdir(), "nonexistent-ctrip.json"),
141
- });
142
- expect(bad.ok).toBe(false);
143
- expect(bad.reason).toBe("INPUT_PATH_UNREADABLE");
144
- } finally {
145
- fs.unlinkSync(p);
146
- }
147
- });
148
-
149
- it("sync yields orders from file (inputPath alias) + normalize end-to-end", async () => {
150
- const p = writeTmp(JSON.stringify([FLIGHT_ORDER]));
151
- try {
152
- const a = new CtripAdapter();
153
- const items = await collect(a.sync({ inputPath: p }));
154
- expect(items).toHaveLength(1);
155
- expect(items[0]).toMatchObject({ adapter: NAME, originalId: "CT1" });
156
- const batch = a.normalize(items[0]);
157
- const ev = batch.events[0];
158
- expect(ev.subtype).toBe("trip");
159
- expect(ev.content.title).toBe("flight: 上海 → 北京");
160
- expect(ev.content.amount).toEqual({
161
- value: 1280,
162
- currency: "CNY",
163
- direction: "out",
164
- });
165
- expect(
166
- batch.persons.find((x) => x.subtype === "merchant").names,
167
- ).toEqual(["国航"]);
168
- } finally {
169
- fs.unlinkSync(p);
170
- }
171
- });
172
-
173
- it("sync returns silently when no path / missing file", async () => {
174
- const a = new CtripAdapter();
175
- expect(await collect(a.sync({}))).toEqual([]);
176
- expect(
177
- await collect(
178
- a.sync({ inputPath: path.join(os.tmpdir(), "nope-ctrip.json") }),
179
- ),
180
- ).toEqual([]);
181
- });
182
-
183
- it("sync throws on broken {-line; pure garbage degrades to empty (JSONL filter)", async () => {
184
- const broken = writeTmp("{broken json line");
185
- const garbage = writeTmp("not json at all");
186
- try {
187
- const a = new CtripAdapter();
188
- await expect(collect(a.sync({ inputPath: broken }))).rejects.toThrow(
189
- /parse failed/,
190
- );
191
- // no line starts with "{" → JSONL filter yields [] rather than throwing
192
- expect(await collect(a.sync({ inputPath: garbage }))).toEqual([]);
193
- } finally {
194
- fs.unlinkSync(broken);
195
- fs.unlinkSync(garbage);
196
- }
197
- });
198
-
199
- it("normalize throws on missing record", () => {
200
- const a = new CtripAdapter();
201
- expect(() => a.normalize({ payload: {} })).toThrow(
202
- /payload\.record missing/,
203
- );
204
- });
205
- });
206
-
207
- describe("orderToRecord web-API aliases (cookie-api mode)", () => {
208
- it("maps web order field names (bizType / amount / orderDate / departCity)", () => {
209
- const rec = orderToRecord(
210
- {
211
- orderId: "W1",
212
- bizType: "Flight",
213
- departCity: "广州",
214
- arriveCity: "成都",
215
- amount: "899.5",
216
- departureDate: 1716383021000,
217
- contactName: "王五",
218
- orderDate: "2026-04-01",
219
- },
220
- { capturedVia: "cookie-api" },
221
- );
222
- expect(rec).toMatchObject({
223
- vendorId: "ctrip",
224
- recordId: "W1",
225
- vehicleType: "flight",
226
- from: { city: "广州" },
227
- to: { city: "成都" },
228
- departureMs: 1716383021000,
229
- traveler: "王五",
230
- });
231
- expect(rec.totalCost).toEqual({ value: 899.5, currency: "CNY" });
232
- expect(rec.extras.capturedVia).toBe("cookie-api");
233
- });
234
-
235
- it("file-import rows keep priority + no capturedVia by default", () => {
236
- const rec = orderToRecord({ orderId: "F1", type: "hotel", price: "300" });
237
- expect(rec.vehicleType).toBe("hotel");
238
- expect(rec.totalCost).toEqual({ value: 300, currency: "CNY" });
239
- expect(rec.extras.capturedVia).toBeUndefined();
240
- });
241
- });
242
-
243
- describe("extractOrders", () => {
244
- it("pulls the list from common Ctrip response shapes", () => {
245
- expect(extractOrders({ orders: [{ orderId: "A" }] })).toHaveLength(1);
246
- expect(extractOrders({ data: { orderList: [{ orderId: "B" }] } })).toHaveLength(1);
247
- expect(extractOrders({ result: { list: [{ orderId: "C" }] } })).toHaveLength(1);
248
- expect(extractOrders({})).toEqual([]);
249
- expect(extractOrders(null)).toEqual([]);
250
- });
251
- });
252
-
253
- describe("CtripAdapter cookie-api mode", () => {
254
- const COOKIES = "_bfa=abc; cticket=xyz; UUID=u1";
255
-
256
- it("authenticate returns cookie mode (account OPTIONAL)", async () => {
257
- const a = new CtripAdapter({ account: { cookies: COOKIES } });
258
- expect(await a.authenticate()).toEqual({
259
- ok: true,
260
- account: null,
261
- mode: "cookie",
262
- });
263
- });
264
-
265
- it("authenticate fails on empty cookies", async () => {
266
- const a = new CtripAdapter({ account: { cookies: "" } });
267
- // empty cookies → no cookieAuth → falls through to ready (not cookie mode)
268
- expect((await a.authenticate()).mode).toBe("ready");
269
- });
270
-
271
- it("sync fetches, paginates, maps + normalizes end-to-end", async () => {
272
- const pages = [
273
- {
274
- orderList: [
275
- {
276
- orderId: "CK1",
277
- bizType: "flight",
278
- departCity: "上海",
279
- arriveCity: "北京",
280
- amount: "1200",
281
- orderDate: 1716383021000,
282
- flightNumber: "MU500",
283
- },
284
- ],
285
- },
286
- { orderList: [] }, // page 2 empty → stop
287
- ];
288
- const calls = [];
289
- const fetchFn = async ({ url, cookies, query, sign }) => {
290
- calls.push({ url, cookies, pageIndex: query.pageIndex, sign });
291
- return pages[query.pageIndex - 1] || { orderList: [] };
292
- };
293
- const a = new CtripAdapter({
294
- account: { cookies: COOKIES },
295
- fetchFn,
296
- // no signProvider → sign should be null (best-effort unsigned)
297
- });
298
- const items = await collect(a.sync({ sinceWatermark: 0 }));
299
- expect(items).toHaveLength(1);
300
- expect(items[0]).toMatchObject({ adapter: NAME, originalId: "CK1" });
301
- expect(calls[0].cookies).toBe(COOKIES);
302
- expect(calls[0].sign).toBe(null);
303
- const batch = a.normalize(items[0]);
304
- expect(batch.events[0].content.title).toBe("flight: 上海 → 北京");
305
- expect(batch.events[0].content.amount).toMatchObject({ value: 1200 });
306
- });
307
-
308
- it("invokes signProvider when configured", async () => {
309
- const fetchFn = async ({ query }) =>
310
- query.pageIndex === 1 ? { orders: [{ orderId: "S1", type: "hotel" }] } : { orders: [] };
311
- const signCalls = [];
312
- const signProvider = async (ctx) => {
313
- signCalls.push(ctx);
314
- return "SIGNED-TOKEN";
315
- };
316
- const a = new CtripAdapter({ account: { cookies: COOKIES }, fetchFn, signProvider });
317
- const items = await collect(a.sync({ sinceWatermark: 0 }));
318
- expect(items).toHaveLength(1);
319
- expect(signCalls).toHaveLength(1);
320
- expect(signCalls[0].cookies).toBe(COOKIES);
321
- });
322
-
323
- it("stops at sinceWatermark (older orders dropped)", async () => {
324
- const fetchFn = async () => ({
325
- orderList: [
326
- { orderId: "NEW", type: "flight", orderDate: 2_000_000_000_000 },
327
- { orderId: "OLD", type: "flight", orderDate: 1_000_000_000_000 },
328
- ],
329
- });
330
- const a = new CtripAdapter({ account: { cookies: COOKIES }, fetchFn });
331
- const items = await collect(a.sync({ sinceWatermark: 1_500_000_000_000, maxPages: 1 }));
332
- expect(items.map((x) => x.originalId)).toEqual(["NEW"]);
333
- });
334
-
335
- it("respects opts.limit", async () => {
336
- const fetchFn = async () => ({
337
- orderList: [
338
- { orderId: "L1", type: "flight", orderDate: 2_000_000_000_000 },
339
- { orderId: "L2", type: "flight", orderDate: 2_000_000_000_001 },
340
- ],
341
- });
342
- const a = new CtripAdapter({ account: { cookies: COOKIES }, fetchFn });
343
- const items = await collect(a.sync({ sinceWatermark: 0, limit: 1, maxPages: 1 }));
344
- expect(items).toHaveLength(1);
345
- });
346
-
347
- it("empty/login-redirect response yields zero (no crash)", async () => {
348
- const a = new CtripAdapter({
349
- account: { cookies: COOKIES },
350
- fetchFn: async () => "<html>login</html>",
351
- });
352
- expect(await collect(a.sync({ sinceWatermark: 0 }))).toEqual([]);
353
- });
354
-
355
- it("default fetch throws when no fetchFn (wiring bug)", async () => {
356
- const a = new CtripAdapter({ account: { cookies: COOKIES } });
357
- await expect(collect(a.sync({ sinceWatermark: 0 }))).rejects.toThrow(
358
- /no fetchFn configured/,
359
- );
360
- });
361
-
362
- it("snapshot/file path takes priority over cookie mode", async () => {
363
- const p = writeTmp(JSON.stringify([FLIGHT_ORDER]));
364
- try {
365
- const a = new CtripAdapter({
366
- account: { cookies: COOKIES },
367
- fetchFn: async () => {
368
- throw new Error("fetchFn should NOT be called in file mode");
369
- },
370
- });
371
- const items = await collect(a.sync({ inputPath: p }));
372
- expect(items.map((x) => x.originalId)).toEqual(["CT1"]);
373
- } finally {
374
- fs.unlinkSync(p);
375
- }
376
- });
377
- });
@@ -1,66 +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 dc = require("../../lib/adapters/travel-didi-consumer");
10
-
11
- function writeTmp(content) {
12
- const p = path.join(os.tmpdir(), `cc-didic-${crypto.randomUUID()}.json`);
13
- fs.writeFileSync(p, content, "utf-8");
14
- return p;
15
- }
16
- async function collect(gen) {
17
- const out = [];
18
- for await (const x of gen) out.push(x);
19
- return out;
20
- }
21
- const COOKIES = "didi_token=abc";
22
-
23
- describe("travel-didi-consumer", () => {
24
- it("name distinct from enterprise", () => {
25
- expect(dc.NAME).toBe("travel-didi-consumer");
26
- expect(new dc.DidiConsumerAdapter().name).toBe("travel-didi-consumer");
27
- });
28
-
29
- it("snapshot ride → car travel event (via travel-base normalize)", async () => {
30
- const SNAP = JSON.stringify([
31
- { orderId: "O1", fromAddress: "家", toAddress: "公司", departTime: 1716383000, arriveTime: 1716385000, fare: 2350, productName: "快车" },
32
- ]);
33
- const p = writeTmp(SNAP);
34
- try {
35
- const a = new dc.DidiConsumerAdapter();
36
- expect((await a.authenticate({ inputPath: p })).mode).toBe("snapshot-file");
37
- const items = await collect(a.sync({ inputPath: p }));
38
- expect(items).toHaveLength(1);
39
- expect(items[0].originalId).toBe("O1");
40
- const b = a.normalize(items[0]);
41
- expect(b.events.length).toBeGreaterThan(0);
42
- expect(b.source ? true : b.events[0].source.adapter).toBe("travel-didi-consumer");
43
- } finally {
44
- fs.unlinkSync(p);
45
- }
46
- });
47
-
48
- it("cookie-api: unverified + sign seam + paginate", async () => {
49
- let signed = 0;
50
- const a = new dc.DidiConsumerAdapter({
51
- account: { cookies: COOKIES, phone: "1" },
52
- signProvider: async () => { signed += 1; return "sig"; },
53
- fetchFn: async ({ query }) => (query.pageIndex > 1 ? { data: { list: [] } } : { data: { list: [{ orderId: "O9", fromAddress: "A", toAddress: "B", departTime: Date.now() }] } }),
54
- });
55
- expect(await a.authenticate()).toMatchObject({ ok: true, mode: "cookie", unverified: true });
56
- const items = await collect(a.sync({}));
57
- expect(items).toHaveLength(1);
58
- expect(items[0].originalId).toBe("O9");
59
- expect(signed).toBeGreaterThan(0);
60
- });
61
-
62
- it("medium sensitivity; default fetch throws", async () => {
63
- expect(new dc.DidiConsumerAdapter().dataDisclosure.sensitivity).toBe("medium");
64
- await expect(collect(new dc.DidiConsumerAdapter({ account: { cookies: COOKIES } }).sync({}))).rejects.toThrow(/no fetchFn/);
65
- });
66
- });