@chainlesschain/personal-data-hub 0.2.2 → 0.2.4
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.
- package/__tests__/adapters/social-toutiao-kuaishou-scaffold.test.js +58 -16
- package/__tests__/analysis.test.js +1 -1
- package/__tests__/longtail-adapters.test.js +67 -16
- package/__tests__/messaging-qq-snapshot.test.js +294 -0
- package/__tests__/shopping-pinduoduo-snapshot.test.js +302 -0
- package/__tests__/shopping-snapshot.test.js +438 -0
- package/__tests__/social-adapters.test.js +28 -3
- package/__tests__/social-douyin-snapshot.test.js +253 -0
- package/__tests__/social-kuaishou-snapshot.test.js +309 -0
- package/__tests__/social-toutiao-snapshot.test.js +314 -0
- package/__tests__/social-weibo-snapshot.test.js +234 -0
- package/__tests__/social-xiaohongshu-snapshot.test.js +232 -0
- package/__tests__/travel-maps-snapshot.test.js +426 -0
- package/__tests__/vault-driver-error.test.js +74 -0
- package/lib/adapters/messaging-qq/index.js +498 -92
- package/lib/adapters/shopping-jd/index.js +228 -25
- package/lib/adapters/shopping-meituan/index.js +222 -26
- package/lib/adapters/shopping-pinduoduo/index.js +275 -0
- package/lib/adapters/social-douyin/index.js +454 -63
- package/lib/adapters/social-kuaishou/index.js +379 -127
- package/lib/adapters/social-toutiao/index.js +400 -130
- package/lib/adapters/social-weibo/index.js +393 -95
- package/lib/adapters/social-xiaohongshu/index.js +389 -49
- package/lib/adapters/travel-baidu-map/index.js +286 -26
- package/lib/adapters/travel-tencent-map/index.js +414 -0
- package/lib/index.js +5 -1
- package/lib/vault.js +60 -8
- package/package.json +2 -1
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { describe, it, expect, beforeEach } from "vitest";
|
|
4
|
+
|
|
5
|
+
const fs = require("node:fs");
|
|
6
|
+
const path = require("node:path");
|
|
7
|
+
const os = require("node:os");
|
|
8
|
+
|
|
9
|
+
const {
|
|
10
|
+
XiaohongshuAdapter,
|
|
11
|
+
SNAPSHOT_SCHEMA_VERSION,
|
|
12
|
+
VALID_SNAPSHOT_KINDS,
|
|
13
|
+
} = require("../lib/adapters/social-xiaohongshu");
|
|
14
|
+
const { validateBatch } = require("../lib/batch");
|
|
15
|
+
|
|
16
|
+
// §A8 v0.2 — snapshot-mode tests, mirror of social-weibo-snapshot.test.js.
|
|
17
|
+
//
|
|
18
|
+
// Snapshot mode is in-APK Android cc reading JSON written by XhsLocalCollector
|
|
19
|
+
// (WebView + OkHttp + X-S signed requests). Sqlite/device-pull tests stay in
|
|
20
|
+
// longtail-adapters.test.js (legacy Phase 13.4 path).
|
|
21
|
+
|
|
22
|
+
function writeSnapshot(dir, snapshot) {
|
|
23
|
+
const p = path.join(dir, "social-xiaohongshu.json");
|
|
24
|
+
fs.writeFileSync(p, JSON.stringify(snapshot), "utf-8");
|
|
25
|
+
return p;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
describe("XiaohongshuAdapter snapshot mode", () => {
|
|
29
|
+
let tmpDir;
|
|
30
|
+
beforeEach(() => {
|
|
31
|
+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "xhs-snap-"));
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("exports SNAPSHOT_SCHEMA_VERSION = 1 + 3 VALID_SNAPSHOT_KINDS", () => {
|
|
35
|
+
expect(SNAPSHOT_SCHEMA_VERSION).toBe(1);
|
|
36
|
+
expect(VALID_SNAPSHOT_KINDS).toEqual(["note", "liked", "follow"]);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("authenticate(inputPath) ok when readable", async () => {
|
|
40
|
+
const p = writeSnapshot(tmpDir, {
|
|
41
|
+
schemaVersion: 1,
|
|
42
|
+
snapshottedAt: Date.now(),
|
|
43
|
+
events: [],
|
|
44
|
+
});
|
|
45
|
+
const a = new XiaohongshuAdapter();
|
|
46
|
+
const res = await a.authenticate({ inputPath: p });
|
|
47
|
+
expect(res.ok).toBe(true);
|
|
48
|
+
expect(res.mode).toBe("snapshot-file");
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("authenticate(inputPath) fails when path unreadable", async () => {
|
|
52
|
+
const a = new XiaohongshuAdapter();
|
|
53
|
+
const res = await a.authenticate({ inputPath: path.join(tmpDir, "missing.json") });
|
|
54
|
+
expect(res.ok).toBe(false);
|
|
55
|
+
expect(res.reason).toBe("INPUT_PATH_UNREADABLE");
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("authenticate() with neither inputPath nor dbPath returns NO_INPUT", async () => {
|
|
59
|
+
const a = new XiaohongshuAdapter();
|
|
60
|
+
const res = await a.authenticate({});
|
|
61
|
+
expect(res.ok).toBe(false);
|
|
62
|
+
expect(res.reason).toBe("NO_INPUT");
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("rejects schemaVersion mismatch", async () => {
|
|
66
|
+
const p = writeSnapshot(tmpDir, {
|
|
67
|
+
schemaVersion: 99,
|
|
68
|
+
snapshottedAt: Date.now(),
|
|
69
|
+
events: [],
|
|
70
|
+
});
|
|
71
|
+
const a = new XiaohongshuAdapter();
|
|
72
|
+
let threw = null;
|
|
73
|
+
try {
|
|
74
|
+
for await (const _r of a.sync({ inputPath: p })) { /* drain */ }
|
|
75
|
+
} catch (err) {
|
|
76
|
+
threw = err;
|
|
77
|
+
}
|
|
78
|
+
expect(threw).toBeTruthy();
|
|
79
|
+
expect(String(threw.message)).toMatch(/schemaVersion mismatch/);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("empty events array yields nothing (no crash)", async () => {
|
|
83
|
+
const p = writeSnapshot(tmpDir, {
|
|
84
|
+
schemaVersion: 1,
|
|
85
|
+
snapshottedAt: Date.now(),
|
|
86
|
+
events: [],
|
|
87
|
+
});
|
|
88
|
+
const a = new XiaohongshuAdapter();
|
|
89
|
+
const raws = [];
|
|
90
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
91
|
+
expect(raws.length).toBe(0);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("note + liked + follow round-trip normalize cleanly", async () => {
|
|
95
|
+
const now = Date.now();
|
|
96
|
+
const p = writeSnapshot(tmpDir, {
|
|
97
|
+
schemaVersion: 1,
|
|
98
|
+
snapshottedAt: now,
|
|
99
|
+
account: { uid: "5e8c8f7e000000000abcdef0", numericUid: "12345", displayName: "alice" },
|
|
100
|
+
events: [
|
|
101
|
+
{
|
|
102
|
+
kind: "note",
|
|
103
|
+
id: "note-N1",
|
|
104
|
+
capturedAt: now - 1000,
|
|
105
|
+
title: "今日穿搭",
|
|
106
|
+
noteId: "N1",
|
|
107
|
+
desc: "夏日清凉",
|
|
108
|
+
type: "normal",
|
|
109
|
+
likedCount: 100,
|
|
110
|
+
collectedCount: 30,
|
|
111
|
+
commentCount: 15,
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
kind: "liked",
|
|
115
|
+
id: "liked-N2",
|
|
116
|
+
capturedAt: now - 2000,
|
|
117
|
+
title: "好喜欢的菜谱",
|
|
118
|
+
noteId: "N2",
|
|
119
|
+
authorNickname: "美食家",
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
kind: "follow",
|
|
123
|
+
id: "follow-USR99",
|
|
124
|
+
capturedAt: now - 3000,
|
|
125
|
+
userId: "USR99",
|
|
126
|
+
nickname: "carol",
|
|
127
|
+
image: "https://example.com/c.jpg",
|
|
128
|
+
},
|
|
129
|
+
],
|
|
130
|
+
});
|
|
131
|
+
const a = new XiaohongshuAdapter();
|
|
132
|
+
const raws = [];
|
|
133
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
134
|
+
expect(raws.length).toBe(3);
|
|
135
|
+
|
|
136
|
+
const kinds = raws.map((r) => r.kind);
|
|
137
|
+
expect(kinds).toEqual(["note", "liked", "follow"]);
|
|
138
|
+
|
|
139
|
+
expect(raws[0].originalId).toMatch(/^xiaohongshu:note:/);
|
|
140
|
+
expect(raws[1].originalId).toMatch(/^xiaohongshu:liked:/);
|
|
141
|
+
expect(raws[2].originalId).toMatch(/^xiaohongshu:follow:/);
|
|
142
|
+
|
|
143
|
+
for (const raw of raws) {
|
|
144
|
+
const batch = a.normalize(raw);
|
|
145
|
+
expect(validateBatch(batch).valid).toBe(true);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const noteBatch = a.normalize(raws[0]);
|
|
149
|
+
expect(noteBatch.events[0].subtype).toBe("post");
|
|
150
|
+
expect(noteBatch.events[0].extra.noteId).toBe("N1");
|
|
151
|
+
expect(noteBatch.events[0].extra.likedCount).toBe(100);
|
|
152
|
+
expect(noteBatch.events[0].extra.collectedCount).toBe(30);
|
|
153
|
+
expect(noteBatch.events[0].extra.commentCount).toBe(15);
|
|
154
|
+
expect(noteBatch.events[0].extra.type).toBe("normal");
|
|
155
|
+
expect(noteBatch.events[0].source.capturedBy).toBe("api");
|
|
156
|
+
|
|
157
|
+
const likedBatch = a.normalize(raws[1]);
|
|
158
|
+
expect(likedBatch.events[0].subtype).toBe("like");
|
|
159
|
+
expect(likedBatch.events[0].extra.authorNickname).toBe("美食家");
|
|
160
|
+
|
|
161
|
+
const followBatch = a.normalize(raws[2]);
|
|
162
|
+
expect(followBatch.events.length).toBe(0);
|
|
163
|
+
expect(followBatch.persons.length).toBe(1);
|
|
164
|
+
expect(followBatch.persons[0].names).toEqual(["carol"]);
|
|
165
|
+
expect(followBatch.persons[0].identifiers["xiaohongshu-uid"]).toEqual(["USR99"]);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("respects per-kind include opt-out", async () => {
|
|
169
|
+
const now = Date.now();
|
|
170
|
+
const p = writeSnapshot(tmpDir, {
|
|
171
|
+
schemaVersion: 1,
|
|
172
|
+
snapshottedAt: now,
|
|
173
|
+
events: [
|
|
174
|
+
{ kind: "note", id: "n1", capturedAt: now, title: "t", noteId: "N1" },
|
|
175
|
+
{ kind: "liked", id: "l1", capturedAt: now, title: "l", noteId: "N2" },
|
|
176
|
+
{ kind: "follow", id: "fl1", capturedAt: now, userId: "U1", nickname: "x" },
|
|
177
|
+
],
|
|
178
|
+
});
|
|
179
|
+
const a = new XiaohongshuAdapter();
|
|
180
|
+
const raws = [];
|
|
181
|
+
for await (const r of a.sync({ inputPath: p, include: { liked: false } })) {
|
|
182
|
+
raws.push(r);
|
|
183
|
+
}
|
|
184
|
+
const kinds = raws.map((r) => r.kind);
|
|
185
|
+
expect(kinds).toEqual(["note", "follow"]);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it("respects opts.limit", async () => {
|
|
189
|
+
const now = Date.now();
|
|
190
|
+
const events = Array.from({ length: 5 }, (_, i) => ({
|
|
191
|
+
kind: "note", id: `n${i}`, capturedAt: now - i * 100, title: `t${i}`, noteId: `N${i}`,
|
|
192
|
+
}));
|
|
193
|
+
const p = writeSnapshot(tmpDir, { schemaVersion: 1, snapshottedAt: now, events });
|
|
194
|
+
const a = new XiaohongshuAdapter();
|
|
195
|
+
const raws = [];
|
|
196
|
+
for await (const r of a.sync({ inputPath: p, limit: 2 })) raws.push(r);
|
|
197
|
+
expect(raws.length).toBe(2);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it("filters out unknown kinds (forward compat)", async () => {
|
|
201
|
+
const now = Date.now();
|
|
202
|
+
const p = writeSnapshot(tmpDir, {
|
|
203
|
+
schemaVersion: 1,
|
|
204
|
+
snapshottedAt: now,
|
|
205
|
+
events: [
|
|
206
|
+
{ kind: "note", id: "n1", capturedAt: now, title: "ok", noteId: "N1" },
|
|
207
|
+
{ kind: "future-kind", id: "x", capturedAt: now },
|
|
208
|
+
{ kind: "history", id: "h", capturedAt: now }, // sqlite-only
|
|
209
|
+
],
|
|
210
|
+
});
|
|
211
|
+
const a = new XiaohongshuAdapter();
|
|
212
|
+
const raws = [];
|
|
213
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
214
|
+
expect(raws.length).toBe(1);
|
|
215
|
+
expect(raws[0].kind).toBe("note");
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it("snapshottedAt fallback when event capturedAt missing", async () => {
|
|
219
|
+
const ts = 1700000000000;
|
|
220
|
+
const p = writeSnapshot(tmpDir, {
|
|
221
|
+
schemaVersion: 1,
|
|
222
|
+
snapshottedAt: ts,
|
|
223
|
+
events: [
|
|
224
|
+
{ kind: "note", id: "n1", title: "no time", noteId: "N1" },
|
|
225
|
+
],
|
|
226
|
+
});
|
|
227
|
+
const a = new XiaohongshuAdapter();
|
|
228
|
+
const raws = [];
|
|
229
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
230
|
+
expect(raws[0].capturedAt).toBe(ts);
|
|
231
|
+
});
|
|
232
|
+
});
|
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { describe, it, expect, beforeEach } from "vitest";
|
|
4
|
+
|
|
5
|
+
const fs = require("node:fs");
|
|
6
|
+
const path = require("node:path");
|
|
7
|
+
const os = require("node:os");
|
|
8
|
+
|
|
9
|
+
const {
|
|
10
|
+
BaiduMapAdapter,
|
|
11
|
+
SNAPSHOT_SCHEMA_VERSION: BAIDU_VERSION,
|
|
12
|
+
} = require("../lib/adapters/travel-baidu-map");
|
|
13
|
+
const {
|
|
14
|
+
TencentMapAdapter,
|
|
15
|
+
SNAPSHOT_SCHEMA_VERSION: TENCENT_VERSION,
|
|
16
|
+
} = require("../lib/adapters/travel-tencent-map");
|
|
17
|
+
const { validateBatch } = require("../lib/batch");
|
|
18
|
+
|
|
19
|
+
// §2.5b 地图三联 v0.2 — snapshot-mode tests, mirror of social-weibo-snapshot
|
|
20
|
+
// & social-bilibili-snapshot patterns.
|
|
21
|
+
//
|
|
22
|
+
// Snapshot mode is in-APK Android cc reading JSON written by the
|
|
23
|
+
// {Baidu,Tencent}MapLocalCollector (WebView cookie scrape). Sqlite/device-pull
|
|
24
|
+
// tests remain in travel-adapters.test.js. Three kinds: favourite / search /
|
|
25
|
+
// route — all 3 reuse normalizeTravelRecord via on-the-fly TravelRecord build.
|
|
26
|
+
|
|
27
|
+
function writeSnapshot(dir, fileName, snapshot) {
|
|
28
|
+
const p = path.join(dir, fileName);
|
|
29
|
+
fs.writeFileSync(p, JSON.stringify(snapshot), "utf-8");
|
|
30
|
+
return p;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
describe("BaiduMapAdapter snapshot mode", () => {
|
|
34
|
+
let tmpDir;
|
|
35
|
+
beforeEach(() => {
|
|
36
|
+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "baidu-map-snap-"));
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("exports SNAPSHOT_SCHEMA_VERSION = 1", () => {
|
|
40
|
+
expect(BAIDU_VERSION).toBe(1);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("authenticate(inputPath) ok when readable + mode=snapshot-file", async () => {
|
|
44
|
+
const p = writeSnapshot(tmpDir, "travel-baidu-map.json", {
|
|
45
|
+
schemaVersion: 1,
|
|
46
|
+
snapshottedAt: Date.now(),
|
|
47
|
+
events: [],
|
|
48
|
+
});
|
|
49
|
+
const a = new BaiduMapAdapter();
|
|
50
|
+
const res = await a.authenticate({ inputPath: p });
|
|
51
|
+
expect(res.ok).toBe(true);
|
|
52
|
+
expect(res.mode).toBe("snapshot-file");
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("authenticate(inputPath) fails when path unreadable", async () => {
|
|
56
|
+
const a = new BaiduMapAdapter();
|
|
57
|
+
const res = await a.authenticate({ inputPath: path.join(tmpDir, "missing.json") });
|
|
58
|
+
expect(res.ok).toBe(false);
|
|
59
|
+
expect(res.reason).toBe("INPUT_PATH_UNREADABLE");
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("authenticate() with neither inputPath nor dbPath returns NO_INPUT", async () => {
|
|
63
|
+
const a = new BaiduMapAdapter();
|
|
64
|
+
const res = await a.authenticate({});
|
|
65
|
+
expect(res.ok).toBe(false);
|
|
66
|
+
expect(res.reason).toBe("NO_INPUT");
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("authenticate(dbPath) without account.deviceId returns NO_ACCOUNT_DEVICE_ID", async () => {
|
|
70
|
+
const a = new BaiduMapAdapter({ dbPath: path.join(tmpDir, "fake.db") });
|
|
71
|
+
const res = await a.authenticate({});
|
|
72
|
+
expect(res.ok).toBe(false);
|
|
73
|
+
expect(res.reason).toBe("NO_ACCOUNT_DEVICE_ID");
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("rejects schemaVersion mismatch", async () => {
|
|
77
|
+
const p = writeSnapshot(tmpDir, "travel-baidu-map.json", {
|
|
78
|
+
schemaVersion: 99,
|
|
79
|
+
snapshottedAt: Date.now(),
|
|
80
|
+
events: [],
|
|
81
|
+
});
|
|
82
|
+
const a = new BaiduMapAdapter();
|
|
83
|
+
let threw = null;
|
|
84
|
+
try {
|
|
85
|
+
for await (const _r of a.sync({ inputPath: p })) { /* drain */ }
|
|
86
|
+
} catch (err) {
|
|
87
|
+
threw = err;
|
|
88
|
+
}
|
|
89
|
+
expect(threw).toBeTruthy();
|
|
90
|
+
expect(String(threw.message)).toMatch(/schemaVersion mismatch/);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("empty events array yields nothing (no crash)", async () => {
|
|
94
|
+
const p = writeSnapshot(tmpDir, "travel-baidu-map.json", {
|
|
95
|
+
schemaVersion: 1,
|
|
96
|
+
snapshottedAt: Date.now(),
|
|
97
|
+
events: [],
|
|
98
|
+
});
|
|
99
|
+
const a = new BaiduMapAdapter();
|
|
100
|
+
const raws = [];
|
|
101
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
102
|
+
expect(raws.length).toBe(0);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("favourite + search + route round-trip normalize cleanly", async () => {
|
|
106
|
+
const now = Date.now();
|
|
107
|
+
const p = writeSnapshot(tmpDir, "travel-baidu-map.json", {
|
|
108
|
+
schemaVersion: 1,
|
|
109
|
+
snapshottedAt: now,
|
|
110
|
+
vendor: "baidu-map",
|
|
111
|
+
account: { uid: "12345", displayName: "alice" },
|
|
112
|
+
events: [
|
|
113
|
+
{
|
|
114
|
+
kind: "favourite",
|
|
115
|
+
id: "fav-R1",
|
|
116
|
+
capturedAt: now - 1000,
|
|
117
|
+
name: "公司",
|
|
118
|
+
address: "厦门市象屿路 93 号",
|
|
119
|
+
lat: 24.4781,
|
|
120
|
+
lng: 118.0894,
|
|
121
|
+
category: "company",
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
kind: "search",
|
|
125
|
+
id: "search-S1",
|
|
126
|
+
capturedAt: now - 2000,
|
|
127
|
+
query: "鼓浪屿",
|
|
128
|
+
city: "厦门",
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
kind: "route",
|
|
132
|
+
id: "route-RT1",
|
|
133
|
+
capturedAt: now - 3000,
|
|
134
|
+
from: { name: "厦门北站", lat: 24.6, lng: 118.0 },
|
|
135
|
+
to: { name: "厦门大学", lat: 24.43, lng: 118.09 },
|
|
136
|
+
mode: "drive",
|
|
137
|
+
},
|
|
138
|
+
],
|
|
139
|
+
});
|
|
140
|
+
const a = new BaiduMapAdapter();
|
|
141
|
+
const raws = [];
|
|
142
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
143
|
+
expect(raws.length).toBe(3);
|
|
144
|
+
|
|
145
|
+
const kinds = raws.map((r) => r.kind);
|
|
146
|
+
expect(kinds).toEqual(["favourite", "search", "route"]);
|
|
147
|
+
|
|
148
|
+
// Each originalId namespaced under baidu-map:<kind>:<id>
|
|
149
|
+
expect(raws[0].originalId).toMatch(/^baidu-map:favourite:/);
|
|
150
|
+
expect(raws[1].originalId).toMatch(/^baidu-map:search:/);
|
|
151
|
+
expect(raws[2].originalId).toMatch(/^baidu-map:route:/);
|
|
152
|
+
|
|
153
|
+
// Normalize each + validate
|
|
154
|
+
for (const raw of raws) {
|
|
155
|
+
const batch = a.normalize(raw);
|
|
156
|
+
expect(validateBatch(batch).valid).toBe(true);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Favourite: visit-type trip event + 1 place (to) + carrier merchant
|
|
160
|
+
const favBatch = a.normalize(raws[0]);
|
|
161
|
+
expect(favBatch.events.length).toBe(1);
|
|
162
|
+
expect(favBatch.events[0].subtype).toBe("trip");
|
|
163
|
+
// The to-place name flows through as the trip's destination
|
|
164
|
+
// (Place uses singular `name`; aliases array for redundant labels)
|
|
165
|
+
const favToPlace = favBatch.places.find(
|
|
166
|
+
(pl) => pl.name === "公司" || (pl.aliases && pl.aliases.includes("公司")),
|
|
167
|
+
);
|
|
168
|
+
expect(favToPlace).toBeTruthy();
|
|
169
|
+
|
|
170
|
+
// Route: from/to places (2) + carrier merchant
|
|
171
|
+
const routeBatch = a.normalize(raws[2]);
|
|
172
|
+
expect(routeBatch.places.length).toBeGreaterThanOrEqual(2);
|
|
173
|
+
|
|
174
|
+
// Carrier always "百度地图"
|
|
175
|
+
const allBatches = raws.map((r) => a.normalize(r));
|
|
176
|
+
const carriers = allBatches.flatMap((b) =>
|
|
177
|
+
b.persons.filter((p) => p.names.includes("百度地图")),
|
|
178
|
+
);
|
|
179
|
+
expect(carriers.length).toBe(3);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("respects per-kind include opt-out", async () => {
|
|
183
|
+
const now = Date.now();
|
|
184
|
+
const p = writeSnapshot(tmpDir, "travel-baidu-map.json", {
|
|
185
|
+
schemaVersion: 1,
|
|
186
|
+
snapshottedAt: now,
|
|
187
|
+
events: [
|
|
188
|
+
{ kind: "favourite", id: "f1", capturedAt: now, name: "家", lat: 24, lng: 118 },
|
|
189
|
+
{ kind: "search", id: "s1", capturedAt: now, query: "餐厅" },
|
|
190
|
+
{ kind: "route", id: "r1", capturedAt: now, from: { name: "A" }, to: { name: "B" }, mode: "walk" },
|
|
191
|
+
],
|
|
192
|
+
});
|
|
193
|
+
const a = new BaiduMapAdapter();
|
|
194
|
+
const raws = [];
|
|
195
|
+
for await (const r of a.sync({ inputPath: p, include: { search: false } })) {
|
|
196
|
+
raws.push(r);
|
|
197
|
+
}
|
|
198
|
+
const kinds = raws.map((r) => r.kind);
|
|
199
|
+
expect(kinds).toEqual(["favourite", "route"]);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it("respects opts.limit", async () => {
|
|
203
|
+
const now = Date.now();
|
|
204
|
+
const events = Array.from({ length: 5 }, (_, i) => ({
|
|
205
|
+
kind: "favourite",
|
|
206
|
+
id: `f${i}`,
|
|
207
|
+
capturedAt: now - i * 100,
|
|
208
|
+
name: `place-${i}`,
|
|
209
|
+
lat: 24,
|
|
210
|
+
lng: 118,
|
|
211
|
+
}));
|
|
212
|
+
const p = writeSnapshot(tmpDir, "travel-baidu-map.json", {
|
|
213
|
+
schemaVersion: 1,
|
|
214
|
+
snapshottedAt: now,
|
|
215
|
+
events,
|
|
216
|
+
});
|
|
217
|
+
const a = new BaiduMapAdapter();
|
|
218
|
+
const raws = [];
|
|
219
|
+
for await (const r of a.sync({ inputPath: p, limit: 2 })) raws.push(r);
|
|
220
|
+
expect(raws.length).toBe(2);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it("filters out unknown kinds (forward compat)", async () => {
|
|
224
|
+
const now = Date.now();
|
|
225
|
+
const p = writeSnapshot(tmpDir, "travel-baidu-map.json", {
|
|
226
|
+
schemaVersion: 1,
|
|
227
|
+
snapshottedAt: now,
|
|
228
|
+
events: [
|
|
229
|
+
{ kind: "favourite", id: "f1", capturedAt: now, name: "ok", lat: 1, lng: 2 },
|
|
230
|
+
{ kind: "future-kind", id: "x", capturedAt: now },
|
|
231
|
+
{ kind: "trip-summary", id: "ts", capturedAt: now }, // hypothetical future
|
|
232
|
+
],
|
|
233
|
+
});
|
|
234
|
+
const a = new BaiduMapAdapter();
|
|
235
|
+
const raws = [];
|
|
236
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
237
|
+
expect(raws.length).toBe(1);
|
|
238
|
+
expect(raws[0].kind).toBe("favourite");
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it("snapshottedAt fallback when event capturedAt missing", async () => {
|
|
242
|
+
const ts = 1700000000000;
|
|
243
|
+
const p = writeSnapshot(tmpDir, "travel-baidu-map.json", {
|
|
244
|
+
schemaVersion: 1,
|
|
245
|
+
snapshottedAt: ts,
|
|
246
|
+
events: [
|
|
247
|
+
{ kind: "favourite", id: "f1", name: "no time", lat: 1, lng: 2 },
|
|
248
|
+
],
|
|
249
|
+
});
|
|
250
|
+
const a = new BaiduMapAdapter();
|
|
251
|
+
const raws = [];
|
|
252
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
253
|
+
expect(raws[0].capturedAt).toBe(ts);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
it("sync() without inputPath OR dbPath throws", async () => {
|
|
257
|
+
const a = new BaiduMapAdapter();
|
|
258
|
+
let threw = null;
|
|
259
|
+
try {
|
|
260
|
+
for await (const _r of a.sync({})) { /* drain */ }
|
|
261
|
+
} catch (err) {
|
|
262
|
+
threw = err;
|
|
263
|
+
}
|
|
264
|
+
expect(threw).toBeTruthy();
|
|
265
|
+
expect(String(threw.message)).toMatch(/needs opts.inputPath/);
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
it("snapshot mode does NOT require account.deviceId at construction", () => {
|
|
269
|
+
// The whole point of snapshot mode: stateless adapter, account from snapshot
|
|
270
|
+
expect(() => new BaiduMapAdapter()).not.toThrow();
|
|
271
|
+
expect(() => new BaiduMapAdapter({})).not.toThrow();
|
|
272
|
+
});
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
describe("TencentMapAdapter snapshot mode", () => {
|
|
276
|
+
let tmpDir;
|
|
277
|
+
beforeEach(() => {
|
|
278
|
+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "tencent-map-snap-"));
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it("exports SNAPSHOT_SCHEMA_VERSION = 1", () => {
|
|
282
|
+
expect(TENCENT_VERSION).toBe(1);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it("constructor allows missing account (stateless snapshot mode)", () => {
|
|
286
|
+
expect(() => new TencentMapAdapter()).not.toThrow();
|
|
287
|
+
expect(() => new TencentMapAdapter({})).not.toThrow();
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it("authenticate(inputPath) ok", async () => {
|
|
291
|
+
const p = writeSnapshot(tmpDir, "travel-tencent-map.json", {
|
|
292
|
+
schemaVersion: 1,
|
|
293
|
+
snapshottedAt: Date.now(),
|
|
294
|
+
events: [],
|
|
295
|
+
});
|
|
296
|
+
const a = new TencentMapAdapter();
|
|
297
|
+
const res = await a.authenticate({ inputPath: p });
|
|
298
|
+
expect(res.ok).toBe(true);
|
|
299
|
+
expect(res.mode).toBe("snapshot-file");
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
it("authenticate(dbPath) without deviceId returns NO_ACCOUNT_DEVICE_ID", async () => {
|
|
303
|
+
const a = new TencentMapAdapter({ dbPath: "/tmp/fake.db" });
|
|
304
|
+
const res = await a.authenticate({});
|
|
305
|
+
expect(res.ok).toBe(false);
|
|
306
|
+
expect(res.reason).toBe("NO_ACCOUNT_DEVICE_ID");
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
it("rejects schemaVersion mismatch", async () => {
|
|
310
|
+
const p = writeSnapshot(tmpDir, "travel-tencent-map.json", {
|
|
311
|
+
schemaVersion: 99,
|
|
312
|
+
snapshottedAt: Date.now(),
|
|
313
|
+
events: [],
|
|
314
|
+
});
|
|
315
|
+
const a = new TencentMapAdapter();
|
|
316
|
+
let threw = null;
|
|
317
|
+
try {
|
|
318
|
+
for await (const _r of a.sync({ inputPath: p })) { /* drain */ }
|
|
319
|
+
} catch (err) {
|
|
320
|
+
threw = err;
|
|
321
|
+
}
|
|
322
|
+
expect(threw).toBeTruthy();
|
|
323
|
+
expect(String(threw.message)).toMatch(/schemaVersion mismatch/);
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
it("favourite + route normalize cleanly with carrier=腾讯地图", async () => {
|
|
327
|
+
const now = Date.now();
|
|
328
|
+
const p = writeSnapshot(tmpDir, "travel-tencent-map.json", {
|
|
329
|
+
schemaVersion: 1,
|
|
330
|
+
snapshottedAt: now,
|
|
331
|
+
vendor: "tencent-map",
|
|
332
|
+
account: { uid: "qq-12345", displayName: "alice" },
|
|
333
|
+
events: [
|
|
334
|
+
{
|
|
335
|
+
kind: "favourite",
|
|
336
|
+
id: "fav-T1",
|
|
337
|
+
capturedAt: now - 1000,
|
|
338
|
+
name: "公司",
|
|
339
|
+
address: "广州市天河区珠江新城",
|
|
340
|
+
lat: 23.12,
|
|
341
|
+
lng: 113.32,
|
|
342
|
+
category: "company",
|
|
343
|
+
},
|
|
344
|
+
{
|
|
345
|
+
kind: "route",
|
|
346
|
+
id: "route-T1",
|
|
347
|
+
capturedAt: now - 2000,
|
|
348
|
+
from: { name: "广州南站", lat: 22.99, lng: 113.27 },
|
|
349
|
+
to: { name: "天河客运站", lat: 23.16, lng: 113.36 },
|
|
350
|
+
mode: "transit",
|
|
351
|
+
},
|
|
352
|
+
],
|
|
353
|
+
});
|
|
354
|
+
const a = new TencentMapAdapter();
|
|
355
|
+
const raws = [];
|
|
356
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
357
|
+
expect(raws.length).toBe(2);
|
|
358
|
+
|
|
359
|
+
expect(raws[0].originalId).toMatch(/^tencent-map:favourite:/);
|
|
360
|
+
expect(raws[1].originalId).toMatch(/^tencent-map:route:/);
|
|
361
|
+
|
|
362
|
+
for (const raw of raws) {
|
|
363
|
+
const batch = a.normalize(raw);
|
|
364
|
+
expect(validateBatch(batch).valid).toBe(true);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// Carrier must be 腾讯地图 (not 百度地图 or 高德地图 — paranoid catch
|
|
368
|
+
// copy-paste regression)
|
|
369
|
+
const allBatches = raws.map((r) => a.normalize(r));
|
|
370
|
+
for (const b of allBatches) {
|
|
371
|
+
const tencentCarrier = b.persons.find((p) => p.names.includes("腾讯地图"));
|
|
372
|
+
expect(tencentCarrier).toBeTruthy();
|
|
373
|
+
expect(b.persons.find((p) => p.names.includes("百度地图"))).toBeFalsy();
|
|
374
|
+
expect(b.persons.find((p) => p.names.includes("高德地图"))).toBeFalsy();
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// Route mode "transit" maps to bus
|
|
378
|
+
const routeBatch = a.normalize(raws[1]);
|
|
379
|
+
const tripEvent = routeBatch.events[0];
|
|
380
|
+
expect(tripEvent.extra.vehicleType).toBe("bus");
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
it("empty events array yields nothing", async () => {
|
|
384
|
+
const p = writeSnapshot(tmpDir, "travel-tencent-map.json", {
|
|
385
|
+
schemaVersion: 1,
|
|
386
|
+
snapshottedAt: Date.now(),
|
|
387
|
+
events: [],
|
|
388
|
+
});
|
|
389
|
+
const a = new TencentMapAdapter();
|
|
390
|
+
const raws = [];
|
|
391
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
392
|
+
expect(raws.length).toBe(0);
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
it("filters unknown kinds", async () => {
|
|
396
|
+
const now = Date.now();
|
|
397
|
+
const p = writeSnapshot(tmpDir, "travel-tencent-map.json", {
|
|
398
|
+
schemaVersion: 1,
|
|
399
|
+
snapshottedAt: now,
|
|
400
|
+
events: [
|
|
401
|
+
{ kind: "favourite", id: "f1", capturedAt: now, name: "ok", lat: 1, lng: 2 },
|
|
402
|
+
{ kind: "speculative-future", id: "x", capturedAt: now },
|
|
403
|
+
],
|
|
404
|
+
});
|
|
405
|
+
const a = new TencentMapAdapter();
|
|
406
|
+
const raws = [];
|
|
407
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
408
|
+
expect(raws.length).toBe(1);
|
|
409
|
+
expect(raws[0].kind).toBe("favourite");
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
it("snapshottedAt fallback when event capturedAt missing", async () => {
|
|
413
|
+
const ts = 1700000000000;
|
|
414
|
+
const p = writeSnapshot(tmpDir, "travel-tencent-map.json", {
|
|
415
|
+
schemaVersion: 1,
|
|
416
|
+
snapshottedAt: ts,
|
|
417
|
+
events: [
|
|
418
|
+
{ kind: "favourite", id: "f1", name: "no time", lat: 1, lng: 2 },
|
|
419
|
+
],
|
|
420
|
+
});
|
|
421
|
+
const a = new TencentMapAdapter();
|
|
422
|
+
const raws = [];
|
|
423
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
424
|
+
expect(raws[0].capturedAt).toBe(ts);
|
|
425
|
+
});
|
|
426
|
+
});
|