@chainlesschain/personal-data-hub 0.2.1 → 0.2.3
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__/adapters/wechat-frida-agent.test.js +132 -1
- package/__tests__/integration/social-bilibili-pipeline.test.js +261 -0
- package/__tests__/longtail-adapters.test.js +60 -14
- 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 +91 -17
- package/__tests__/social-bilibili-snapshot.test.js +278 -0
- 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/__tests__/wechat-adapter.test.js +118 -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-bilibili/adapter.js +500 -0
- package/lib/adapters/social-bilibili/index.js +21 -169
- 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/adapters/wechat/content-parser.js +11 -2
- package/lib/adapters/wechat/db-reader.js +88 -10
- package/lib/adapters/wechat/frida-agent/loader.js +7 -0
- package/lib/adapters/wechat/frida-agent/wechat-key-hook.js +140 -18
- package/lib/adapters/wechat/key-providers/frida-key-provider.js +8 -0
- package/lib/adapters/wechat/normalize.js +12 -3
- package/lib/index.js +5 -1
- package/lib/vault.js +60 -8
- package/package.json +2 -1
|
@@ -0,0 +1,309 @@
|
|
|
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
|
+
KuaishouAdapter,
|
|
11
|
+
SNAPSHOT_SCHEMA_VERSION,
|
|
12
|
+
VALID_SNAPSHOT_KINDS,
|
|
13
|
+
} = require("../lib/adapters/social-kuaishou");
|
|
14
|
+
const { validateBatch } = require("../lib/batch");
|
|
15
|
+
|
|
16
|
+
// §A8 v0.2 — Kuaishou snapshot mode tests, mirror of social-toutiao-snapshot.
|
|
17
|
+
// Snapshot mode reads JSON produced by the phone's KuaishouLocalCollector
|
|
18
|
+
// (root + SQLCipher decrypt of /data/data/com.smile.gifmaker/databases/).
|
|
19
|
+
|
|
20
|
+
function writeSnapshot(dir, snapshot) {
|
|
21
|
+
const p = path.join(dir, "social-kuaishou.json");
|
|
22
|
+
fs.writeFileSync(p, JSON.stringify(snapshot), "utf-8");
|
|
23
|
+
return p;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
describe("KuaishouAdapter snapshot mode", () => {
|
|
27
|
+
let tmpDir;
|
|
28
|
+
beforeEach(() => {
|
|
29
|
+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "kuaishou-snap-"));
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("exports SNAPSHOT_SCHEMA_VERSION = 1 + 3 VALID_SNAPSHOT_KINDS", () => {
|
|
33
|
+
expect(SNAPSHOT_SCHEMA_VERSION).toBe(1);
|
|
34
|
+
expect(VALID_SNAPSHOT_KINDS).toEqual(["watch", "collect", "search"]);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("authenticate(inputPath) ok when readable", async () => {
|
|
38
|
+
const p = writeSnapshot(tmpDir, {
|
|
39
|
+
schemaVersion: 1,
|
|
40
|
+
snapshottedAt: Date.now(),
|
|
41
|
+
events: [],
|
|
42
|
+
});
|
|
43
|
+
const a = new KuaishouAdapter();
|
|
44
|
+
const res = await a.authenticate({ inputPath: p });
|
|
45
|
+
expect(res.ok).toBe(true);
|
|
46
|
+
expect(res.mode).toBe("snapshot-file");
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("authenticate(inputPath) fails when path unreadable", async () => {
|
|
50
|
+
const a = new KuaishouAdapter();
|
|
51
|
+
const res = await a.authenticate({
|
|
52
|
+
inputPath: path.join(tmpDir, "missing.json"),
|
|
53
|
+
});
|
|
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 KuaishouAdapter();
|
|
60
|
+
const res = await a.authenticate({});
|
|
61
|
+
expect(res.ok).toBe(false);
|
|
62
|
+
expect(res.reason).toBe("NO_INPUT");
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("authenticate() sqlite mode without account.uid returns NO_ACCOUNT_UID", async () => {
|
|
66
|
+
const a = new KuaishouAdapter({ dbPath: "/no/such/path.db" });
|
|
67
|
+
const res = await a.authenticate({});
|
|
68
|
+
expect(res.ok).toBe(false);
|
|
69
|
+
expect(res.reason).toBe("NO_ACCOUNT_UID");
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("rejects schemaVersion mismatch", async () => {
|
|
73
|
+
const p = writeSnapshot(tmpDir, {
|
|
74
|
+
schemaVersion: 99,
|
|
75
|
+
snapshottedAt: Date.now(),
|
|
76
|
+
events: [],
|
|
77
|
+
});
|
|
78
|
+
const a = new KuaishouAdapter();
|
|
79
|
+
let threw = null;
|
|
80
|
+
try {
|
|
81
|
+
for await (const _r of a.sync({ inputPath: p })) {
|
|
82
|
+
/* drain */
|
|
83
|
+
}
|
|
84
|
+
} catch (err) {
|
|
85
|
+
threw = err;
|
|
86
|
+
}
|
|
87
|
+
expect(threw).toBeTruthy();
|
|
88
|
+
expect(String(threw.message)).toMatch(/schemaVersion mismatch/);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("empty events array yields nothing (no crash)", async () => {
|
|
92
|
+
const p = writeSnapshot(tmpDir, {
|
|
93
|
+
schemaVersion: 1,
|
|
94
|
+
snapshottedAt: Date.now(),
|
|
95
|
+
events: [],
|
|
96
|
+
});
|
|
97
|
+
const a = new KuaishouAdapter();
|
|
98
|
+
const raws = [];
|
|
99
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
100
|
+
expect(raws.length).toBe(0);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("watch event round-trips normalize cleanly (BROWSE subtype)", async () => {
|
|
104
|
+
const now = Date.now();
|
|
105
|
+
const p = writeSnapshot(tmpDir, {
|
|
106
|
+
schemaVersion: 1,
|
|
107
|
+
snapshottedAt: now,
|
|
108
|
+
account: { uid: "67890", displayName: "alice" },
|
|
109
|
+
events: [
|
|
110
|
+
{
|
|
111
|
+
kind: "watch",
|
|
112
|
+
id: "photo-p-1",
|
|
113
|
+
capturedAt: now - 1000,
|
|
114
|
+
photoId: "p-1",
|
|
115
|
+
caption: "搞笑视频",
|
|
116
|
+
duration: 30,
|
|
117
|
+
authorId: "u-author-1",
|
|
118
|
+
authorName: "UpA",
|
|
119
|
+
},
|
|
120
|
+
],
|
|
121
|
+
});
|
|
122
|
+
const a = new KuaishouAdapter();
|
|
123
|
+
const raws = [];
|
|
124
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
125
|
+
expect(raws.length).toBe(1);
|
|
126
|
+
expect(raws[0].kind).toBe("watch");
|
|
127
|
+
expect(raws[0].originalId).toMatch(/^kuaishou:watch:/);
|
|
128
|
+
|
|
129
|
+
const batch = a.normalize(raws[0]);
|
|
130
|
+
expect(validateBatch(batch).valid).toBe(true);
|
|
131
|
+
expect(batch.events[0].subtype).toBe("browse");
|
|
132
|
+
expect(batch.events[0].content.title).toBe("搞笑视频");
|
|
133
|
+
expect(batch.events[0].extra.photoId).toBe("p-1");
|
|
134
|
+
expect(batch.events[0].extra.duration).toBe(30);
|
|
135
|
+
expect(batch.events[0].extra.authorId).toBe("u-author-1");
|
|
136
|
+
expect(batch.events[0].extra.authorName).toBe("UpA");
|
|
137
|
+
expect(batch.events[0].extra.platform).toBe("kuaishou");
|
|
138
|
+
expect(batch.events[0].source.capturedBy).toBe("api");
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("collect event round-trips normalize cleanly (LIKE subtype)", async () => {
|
|
142
|
+
const now = Date.now();
|
|
143
|
+
const p = writeSnapshot(tmpDir, {
|
|
144
|
+
schemaVersion: 1,
|
|
145
|
+
snapshottedAt: now,
|
|
146
|
+
events: [
|
|
147
|
+
{
|
|
148
|
+
kind: "collect",
|
|
149
|
+
id: "collect-p-2",
|
|
150
|
+
capturedAt: now - 2000,
|
|
151
|
+
photoId: "p-2",
|
|
152
|
+
caption: "美食 vlog",
|
|
153
|
+
authorId: "u-author-2",
|
|
154
|
+
authorName: "FoodVlogger",
|
|
155
|
+
},
|
|
156
|
+
],
|
|
157
|
+
});
|
|
158
|
+
const a = new KuaishouAdapter();
|
|
159
|
+
const raws = [];
|
|
160
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
161
|
+
expect(raws.length).toBe(1);
|
|
162
|
+
|
|
163
|
+
const batch = a.normalize(raws[0]);
|
|
164
|
+
expect(validateBatch(batch).valid).toBe(true);
|
|
165
|
+
expect(batch.events[0].subtype).toBe("like");
|
|
166
|
+
expect(batch.events[0].content.title).toBe("美食 vlog");
|
|
167
|
+
expect(batch.events[0].extra.photoId).toBe("p-2");
|
|
168
|
+
expect(batch.events[0].extra.authorName).toBe("FoodVlogger");
|
|
169
|
+
expect(batch.events[0].source.capturedBy).toBe("api");
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("search event round-trips normalize cleanly (POST subtype, keyword in title)", async () => {
|
|
173
|
+
const now = Date.now();
|
|
174
|
+
const p = writeSnapshot(tmpDir, {
|
|
175
|
+
schemaVersion: 1,
|
|
176
|
+
snapshottedAt: now,
|
|
177
|
+
events: [
|
|
178
|
+
{
|
|
179
|
+
kind: "search",
|
|
180
|
+
id: "search-square-dance:1700002000",
|
|
181
|
+
capturedAt: now - 3000,
|
|
182
|
+
keyword: "广场舞",
|
|
183
|
+
},
|
|
184
|
+
],
|
|
185
|
+
});
|
|
186
|
+
const a = new KuaishouAdapter();
|
|
187
|
+
const raws = [];
|
|
188
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
189
|
+
expect(raws.length).toBe(1);
|
|
190
|
+
|
|
191
|
+
const batch = a.normalize(raws[0]);
|
|
192
|
+
expect(validateBatch(batch).valid).toBe(true);
|
|
193
|
+
expect(batch.events[0].subtype).toBe("post");
|
|
194
|
+
expect(batch.events[0].content.title).toBe("广场舞");
|
|
195
|
+
expect(batch.events[0].extra.kind).toBe("search");
|
|
196
|
+
expect(batch.events[0].extra.keyword).toBe("广场舞");
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("respects per-kind include opt-out", async () => {
|
|
200
|
+
const now = Date.now();
|
|
201
|
+
const p = writeSnapshot(tmpDir, {
|
|
202
|
+
schemaVersion: 1,
|
|
203
|
+
snapshottedAt: now,
|
|
204
|
+
events: [
|
|
205
|
+
{ kind: "watch", id: "w1", capturedAt: now, photoId: "p-1", caption: "c1" },
|
|
206
|
+
{ kind: "collect", id: "c1", capturedAt: now, photoId: "p-2", caption: "c2" },
|
|
207
|
+
{ kind: "search", id: "s1", capturedAt: now, keyword: "kw" },
|
|
208
|
+
],
|
|
209
|
+
});
|
|
210
|
+
const a = new KuaishouAdapter();
|
|
211
|
+
const raws = [];
|
|
212
|
+
for await (const r of a.sync({
|
|
213
|
+
inputPath: p,
|
|
214
|
+
include: { collect: false, search: false },
|
|
215
|
+
})) {
|
|
216
|
+
raws.push(r);
|
|
217
|
+
}
|
|
218
|
+
expect(raws.length).toBe(1);
|
|
219
|
+
expect(raws[0].kind).toBe("watch");
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it("respects opts.limit", async () => {
|
|
223
|
+
const now = Date.now();
|
|
224
|
+
const events = [];
|
|
225
|
+
for (let i = 0; i < 10; i++) {
|
|
226
|
+
events.push({
|
|
227
|
+
kind: "watch",
|
|
228
|
+
id: `w${i}`,
|
|
229
|
+
capturedAt: now - i * 1000,
|
|
230
|
+
photoId: `p-${i}`,
|
|
231
|
+
caption: `c${i}`,
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
const p = writeSnapshot(tmpDir, { schemaVersion: 1, snapshottedAt: now, events });
|
|
235
|
+
const a = new KuaishouAdapter();
|
|
236
|
+
const raws = [];
|
|
237
|
+
for await (const r of a.sync({ inputPath: p, limit: 5 })) raws.push(r);
|
|
238
|
+
expect(raws.length).toBe(5);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it("filters out unknown kinds (forward compat)", async () => {
|
|
242
|
+
const now = Date.now();
|
|
243
|
+
const p = writeSnapshot(tmpDir, {
|
|
244
|
+
schemaVersion: 1,
|
|
245
|
+
snapshottedAt: now,
|
|
246
|
+
events: [
|
|
247
|
+
{ kind: "watch", id: "w1", capturedAt: now, photoId: "p-1", caption: "c1" },
|
|
248
|
+
{ kind: "future-kind", id: "x", capturedAt: now },
|
|
249
|
+
{ kind: "live-watch", id: "lv-1", capturedAt: now }, // v0.3 hypothetical
|
|
250
|
+
],
|
|
251
|
+
});
|
|
252
|
+
const a = new KuaishouAdapter();
|
|
253
|
+
const raws = [];
|
|
254
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
255
|
+
expect(raws.length).toBe(1);
|
|
256
|
+
expect(raws[0].kind).toBe("watch");
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it("snapshottedAt fallback when event capturedAt missing", async () => {
|
|
260
|
+
const ts = 1700000000000;
|
|
261
|
+
const p = writeSnapshot(tmpDir, {
|
|
262
|
+
schemaVersion: 1,
|
|
263
|
+
snapshottedAt: ts,
|
|
264
|
+
events: [{ kind: "watch", id: "w1", photoId: "p-1", caption: "c1" }],
|
|
265
|
+
});
|
|
266
|
+
const a = new KuaishouAdapter();
|
|
267
|
+
const raws = [];
|
|
268
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
269
|
+
expect(raws[0].capturedAt).toBe(ts);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it("sqlite mode throws when account.uid missing at sync time", async () => {
|
|
273
|
+
const a = new KuaishouAdapter();
|
|
274
|
+
let threw = null;
|
|
275
|
+
try {
|
|
276
|
+
for await (const _r of a.sync({ dbPath: "/no/such/path.db" })) {
|
|
277
|
+
/* drain */
|
|
278
|
+
}
|
|
279
|
+
} catch (err) {
|
|
280
|
+
threw = err;
|
|
281
|
+
}
|
|
282
|
+
expect(threw).toBeTruthy();
|
|
283
|
+
expect(String(threw.message)).toMatch(/account\.uid required/);
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
it("sqlite mode gracefully exits when dbPath unreadable (with account.uid)", async () => {
|
|
287
|
+
const a = new KuaishouAdapter({
|
|
288
|
+
account: { uid: "u-2" },
|
|
289
|
+
dbPath: "/no/such/path.db",
|
|
290
|
+
});
|
|
291
|
+
const raws = [];
|
|
292
|
+
for await (const r of a.sync()) raws.push(r);
|
|
293
|
+
expect(raws).toEqual([]);
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
it("sync() with neither inputPath nor dbPath throws", async () => {
|
|
297
|
+
const a = new KuaishouAdapter();
|
|
298
|
+
let threw = null;
|
|
299
|
+
try {
|
|
300
|
+
for await (const _r of a.sync()) {
|
|
301
|
+
/* drain */
|
|
302
|
+
}
|
|
303
|
+
} catch (err) {
|
|
304
|
+
threw = err;
|
|
305
|
+
}
|
|
306
|
+
expect(threw).toBeTruthy();
|
|
307
|
+
expect(String(threw.message)).toMatch(/needs opts\.inputPath/);
|
|
308
|
+
});
|
|
309
|
+
});
|
|
@@ -0,0 +1,314 @@
|
|
|
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
|
+
ToutiaoAdapter,
|
|
11
|
+
SNAPSHOT_SCHEMA_VERSION,
|
|
12
|
+
VALID_SNAPSHOT_KINDS,
|
|
13
|
+
} = require("../lib/adapters/social-toutiao");
|
|
14
|
+
const { validateBatch } = require("../lib/batch");
|
|
15
|
+
|
|
16
|
+
// §A8 v0.2 — Toutiao snapshot mode tests, mirror of social-douyin-snapshot.
|
|
17
|
+
// Snapshot mode is the in-APK Android cc path: ToutiaoLocalCollector reads
|
|
18
|
+
// (root + SQLCipher decrypt) the on-device DB and writes a JSON snapshot;
|
|
19
|
+
// this adapter's snapshot mode ingests that JSON. v0.2 SURFACE = read /
|
|
20
|
+
// collection / search kinds.
|
|
21
|
+
|
|
22
|
+
function writeSnapshot(dir, snapshot) {
|
|
23
|
+
const p = path.join(dir, "social-toutiao.json");
|
|
24
|
+
fs.writeFileSync(p, JSON.stringify(snapshot), "utf-8");
|
|
25
|
+
return p;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
describe("ToutiaoAdapter snapshot mode", () => {
|
|
29
|
+
let tmpDir;
|
|
30
|
+
beforeEach(() => {
|
|
31
|
+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "toutiao-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(["read", "collection", "search"]);
|
|
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 ToutiaoAdapter();
|
|
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 ToutiaoAdapter();
|
|
53
|
+
const res = await a.authenticate({
|
|
54
|
+
inputPath: path.join(tmpDir, "missing.json"),
|
|
55
|
+
});
|
|
56
|
+
expect(res.ok).toBe(false);
|
|
57
|
+
expect(res.reason).toBe("INPUT_PATH_UNREADABLE");
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("authenticate() with neither inputPath nor dbPath returns NO_INPUT", async () => {
|
|
61
|
+
const a = new ToutiaoAdapter();
|
|
62
|
+
const res = await a.authenticate({});
|
|
63
|
+
expect(res.ok).toBe(false);
|
|
64
|
+
expect(res.reason).toBe("NO_INPUT");
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("authenticate() sqlite mode without account.uid returns NO_ACCOUNT_UID", async () => {
|
|
68
|
+
const a = new ToutiaoAdapter({ dbPath: "/no/such/path.db" });
|
|
69
|
+
const res = await a.authenticate({});
|
|
70
|
+
expect(res.ok).toBe(false);
|
|
71
|
+
expect(res.reason).toBe("NO_ACCOUNT_UID");
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("rejects schemaVersion mismatch", async () => {
|
|
75
|
+
const p = writeSnapshot(tmpDir, {
|
|
76
|
+
schemaVersion: 99,
|
|
77
|
+
snapshottedAt: Date.now(),
|
|
78
|
+
events: [],
|
|
79
|
+
});
|
|
80
|
+
const a = new ToutiaoAdapter();
|
|
81
|
+
let threw = null;
|
|
82
|
+
try {
|
|
83
|
+
for await (const _r of a.sync({ inputPath: p })) {
|
|
84
|
+
/* drain */
|
|
85
|
+
}
|
|
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, {
|
|
95
|
+
schemaVersion: 1,
|
|
96
|
+
snapshottedAt: Date.now(),
|
|
97
|
+
events: [],
|
|
98
|
+
});
|
|
99
|
+
const a = new ToutiaoAdapter();
|
|
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("read event round-trips normalize cleanly (BROWSE subtype)", async () => {
|
|
106
|
+
const now = Date.now();
|
|
107
|
+
const p = writeSnapshot(tmpDir, {
|
|
108
|
+
schemaVersion: 1,
|
|
109
|
+
snapshottedAt: now,
|
|
110
|
+
account: { uid: "12345", displayName: "alice" },
|
|
111
|
+
events: [
|
|
112
|
+
{
|
|
113
|
+
kind: "read",
|
|
114
|
+
id: "read-i-1",
|
|
115
|
+
capturedAt: now - 1000,
|
|
116
|
+
itemId: "i-1",
|
|
117
|
+
title: "5G 商用进展",
|
|
118
|
+
category: "tech",
|
|
119
|
+
author: "TechCN",
|
|
120
|
+
readDuration: 120,
|
|
121
|
+
source: "首页推荐",
|
|
122
|
+
},
|
|
123
|
+
],
|
|
124
|
+
});
|
|
125
|
+
const a = new ToutiaoAdapter();
|
|
126
|
+
const raws = [];
|
|
127
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
128
|
+
expect(raws.length).toBe(1);
|
|
129
|
+
expect(raws[0].kind).toBe("read");
|
|
130
|
+
expect(raws[0].originalId).toMatch(/^toutiao:read:/);
|
|
131
|
+
|
|
132
|
+
const batch = a.normalize(raws[0]);
|
|
133
|
+
expect(validateBatch(batch).valid).toBe(true);
|
|
134
|
+
expect(batch.events.length).toBe(1);
|
|
135
|
+
const ev = batch.events[0];
|
|
136
|
+
expect(ev.subtype).toBe("browse");
|
|
137
|
+
expect(ev.content.title).toBe("5G 商用进展");
|
|
138
|
+
expect(ev.extra.itemId).toBe("i-1");
|
|
139
|
+
expect(ev.extra.category).toBe("tech");
|
|
140
|
+
expect(ev.extra.author).toBe("TechCN");
|
|
141
|
+
expect(ev.extra.readDuration).toBe(120);
|
|
142
|
+
expect(ev.extra.platform).toBe("toutiao");
|
|
143
|
+
expect(ev.source.capturedBy).toBe("api");
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it("collection event round-trips normalize cleanly (LIKE subtype)", async () => {
|
|
147
|
+
const now = Date.now();
|
|
148
|
+
const p = writeSnapshot(tmpDir, {
|
|
149
|
+
schemaVersion: 1,
|
|
150
|
+
snapshottedAt: now,
|
|
151
|
+
events: [
|
|
152
|
+
{
|
|
153
|
+
kind: "collection",
|
|
154
|
+
id: "collect-i-2",
|
|
155
|
+
capturedAt: now - 2000,
|
|
156
|
+
itemId: "i-2",
|
|
157
|
+
title: "深度长文",
|
|
158
|
+
category: "investigation",
|
|
159
|
+
author: "FinanceCN",
|
|
160
|
+
},
|
|
161
|
+
],
|
|
162
|
+
});
|
|
163
|
+
const a = new ToutiaoAdapter();
|
|
164
|
+
const raws = [];
|
|
165
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
166
|
+
expect(raws.length).toBe(1);
|
|
167
|
+
|
|
168
|
+
const batch = a.normalize(raws[0]);
|
|
169
|
+
expect(validateBatch(batch).valid).toBe(true);
|
|
170
|
+
expect(batch.events[0].subtype).toBe("like");
|
|
171
|
+
expect(batch.events[0].content.title).toBe("深度长文");
|
|
172
|
+
expect(batch.events[0].extra.itemId).toBe("i-2");
|
|
173
|
+
expect(batch.events[0].extra.category).toBe("investigation");
|
|
174
|
+
expect(batch.events[0].source.capturedBy).toBe("api");
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("search event round-trips normalize cleanly (POST subtype, keyword in title)", async () => {
|
|
178
|
+
const now = Date.now();
|
|
179
|
+
const p = writeSnapshot(tmpDir, {
|
|
180
|
+
schemaVersion: 1,
|
|
181
|
+
snapshottedAt: now,
|
|
182
|
+
events: [
|
|
183
|
+
{
|
|
184
|
+
kind: "search",
|
|
185
|
+
id: "search-rust:1700002000",
|
|
186
|
+
capturedAt: now - 3000,
|
|
187
|
+
keyword: "Rust 语言",
|
|
188
|
+
},
|
|
189
|
+
],
|
|
190
|
+
});
|
|
191
|
+
const a = new ToutiaoAdapter();
|
|
192
|
+
const raws = [];
|
|
193
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
194
|
+
expect(raws.length).toBe(1);
|
|
195
|
+
|
|
196
|
+
const batch = a.normalize(raws[0]);
|
|
197
|
+
expect(validateBatch(batch).valid).toBe(true);
|
|
198
|
+
expect(batch.events[0].subtype).toBe("post");
|
|
199
|
+
expect(batch.events[0].content.title).toBe("Rust 语言");
|
|
200
|
+
expect(batch.events[0].extra.kind).toBe("search");
|
|
201
|
+
expect(batch.events[0].extra.keyword).toBe("Rust 语言");
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it("respects per-kind include opt-out", async () => {
|
|
205
|
+
const now = Date.now();
|
|
206
|
+
const p = writeSnapshot(tmpDir, {
|
|
207
|
+
schemaVersion: 1,
|
|
208
|
+
snapshottedAt: now,
|
|
209
|
+
events: [
|
|
210
|
+
{ kind: "read", id: "r1", capturedAt: now, itemId: "i-1", title: "t1" },
|
|
211
|
+
{ kind: "collection", id: "c1", capturedAt: now, itemId: "i-2", title: "t2" },
|
|
212
|
+
{ kind: "search", id: "s1", capturedAt: now, keyword: "kw" },
|
|
213
|
+
],
|
|
214
|
+
});
|
|
215
|
+
const a = new ToutiaoAdapter();
|
|
216
|
+
const raws = [];
|
|
217
|
+
for await (const r of a.sync({
|
|
218
|
+
inputPath: p,
|
|
219
|
+
include: { collection: false, search: false },
|
|
220
|
+
})) {
|
|
221
|
+
raws.push(r);
|
|
222
|
+
}
|
|
223
|
+
expect(raws.length).toBe(1);
|
|
224
|
+
expect(raws[0].kind).toBe("read");
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it("respects opts.limit", async () => {
|
|
228
|
+
const now = Date.now();
|
|
229
|
+
const events = [];
|
|
230
|
+
for (let i = 0; i < 10; i++) {
|
|
231
|
+
events.push({
|
|
232
|
+
kind: "read",
|
|
233
|
+
id: `r${i}`,
|
|
234
|
+
capturedAt: now - i * 1000,
|
|
235
|
+
itemId: `i-${i}`,
|
|
236
|
+
title: `t${i}`,
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
const p = writeSnapshot(tmpDir, { schemaVersion: 1, snapshottedAt: now, events });
|
|
240
|
+
const a = new ToutiaoAdapter();
|
|
241
|
+
const raws = [];
|
|
242
|
+
for await (const r of a.sync({ inputPath: p, limit: 3 })) raws.push(r);
|
|
243
|
+
expect(raws.length).toBe(3);
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it("filters out unknown kinds (forward compat)", async () => {
|
|
247
|
+
const now = Date.now();
|
|
248
|
+
const p = writeSnapshot(tmpDir, {
|
|
249
|
+
schemaVersion: 1,
|
|
250
|
+
snapshottedAt: now,
|
|
251
|
+
events: [
|
|
252
|
+
{ kind: "read", id: "r1", capturedAt: now, itemId: "i-1", title: "t1" },
|
|
253
|
+
{ kind: "future-kind", id: "x", capturedAt: now },
|
|
254
|
+
{ kind: "subscription", id: "sub-1", capturedAt: now }, // v0.3 hypothetical
|
|
255
|
+
],
|
|
256
|
+
});
|
|
257
|
+
const a = new ToutiaoAdapter();
|
|
258
|
+
const raws = [];
|
|
259
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
260
|
+
expect(raws.length).toBe(1);
|
|
261
|
+
expect(raws[0].kind).toBe("read");
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it("snapshottedAt fallback when event capturedAt missing", async () => {
|
|
265
|
+
const ts = 1700000000000;
|
|
266
|
+
const p = writeSnapshot(tmpDir, {
|
|
267
|
+
schemaVersion: 1,
|
|
268
|
+
snapshottedAt: ts,
|
|
269
|
+
events: [{ kind: "read", id: "r1", itemId: "i-1", title: "t1" }],
|
|
270
|
+
});
|
|
271
|
+
const a = new ToutiaoAdapter();
|
|
272
|
+
const raws = [];
|
|
273
|
+
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
274
|
+
expect(raws[0].capturedAt).toBe(ts);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it("sqlite mode throws when account.uid missing at sync time", async () => {
|
|
278
|
+
const a = new ToutiaoAdapter();
|
|
279
|
+
let threw = null;
|
|
280
|
+
try {
|
|
281
|
+
for await (const _r of a.sync({ dbPath: "/no/such/path.db" })) {
|
|
282
|
+
/* drain */
|
|
283
|
+
}
|
|
284
|
+
} catch (err) {
|
|
285
|
+
threw = err;
|
|
286
|
+
}
|
|
287
|
+
expect(threw).toBeTruthy();
|
|
288
|
+
expect(String(threw.message)).toMatch(/account\.uid required/);
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
it("sqlite mode gracefully exits when dbPath unreadable (with account.uid)", async () => {
|
|
292
|
+
const a = new ToutiaoAdapter({
|
|
293
|
+
account: { uid: "u-1" },
|
|
294
|
+
dbPath: "/no/such/path.db",
|
|
295
|
+
});
|
|
296
|
+
const raws = [];
|
|
297
|
+
for await (const r of a.sync()) raws.push(r);
|
|
298
|
+
expect(raws).toEqual([]);
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
it("sync() with neither inputPath nor dbPath throws", async () => {
|
|
302
|
+
const a = new ToutiaoAdapter();
|
|
303
|
+
let threw = null;
|
|
304
|
+
try {
|
|
305
|
+
for await (const _r of a.sync()) {
|
|
306
|
+
/* drain */
|
|
307
|
+
}
|
|
308
|
+
} catch (err) {
|
|
309
|
+
threw = err;
|
|
310
|
+
}
|
|
311
|
+
expect(threw).toBeTruthy();
|
|
312
|
+
expect(String(threw.message)).toMatch(/needs opts\.inputPath/);
|
|
313
|
+
});
|
|
314
|
+
});
|