@chainlesschain/personal-data-hub 0.2.1 → 0.2.2

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.
@@ -0,0 +1,278 @@
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
+ BilibiliAdapter,
11
+ SNAPSHOT_SCHEMA_VERSION,
12
+ VALID_KINDS,
13
+ } = require("../lib/adapters/social-bilibili");
14
+ const { validateBatch } = require("../lib/batch");
15
+
16
+ // A8 v0.1 (2026-05-22) — snapshot-mode tests, mirroring system-data-android.
17
+ //
18
+ // Why a separate file? `social-adapters.test.js` covers the legacy sqlite
19
+ // path (Phase 7.5 device-pull). Snapshot mode is a brand-new ingestion path
20
+ // driven by in-APK Android cc reading JSON from the phone's own WebView+OkHttp
21
+ // pipeline. Keeping tests separated makes it obvious which mode a regression
22
+ // belongs to.
23
+
24
+ function writeSnapshot(dir, snapshot) {
25
+ const p = path.join(dir, "social-bilibili.json");
26
+ fs.writeFileSync(p, JSON.stringify(snapshot), "utf-8");
27
+ return p;
28
+ }
29
+
30
+ describe("BilibiliAdapter snapshot mode", () => {
31
+ let tmpDir;
32
+ beforeEach(() => {
33
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "bili-snap-"));
34
+ });
35
+
36
+ it("exports SNAPSHOT_SCHEMA_VERSION = 1 + 4 VALID_KINDS", () => {
37
+ expect(SNAPSHOT_SCHEMA_VERSION).toBe(1);
38
+ expect(VALID_KINDS).toEqual(["history", "favourite", "dynamic", "follow"]);
39
+ });
40
+
41
+ it("authenticate(inputPath) ok when readable", async () => {
42
+ const p = writeSnapshot(tmpDir, {
43
+ schemaVersion: 1,
44
+ snapshottedAt: Date.now(),
45
+ events: [],
46
+ });
47
+ const a = new BilibiliAdapter();
48
+ const res = await a.authenticate({ inputPath: p });
49
+ expect(res.ok).toBe(true);
50
+ expect(res.mode).toBe("snapshot-file");
51
+ });
52
+
53
+ it("authenticate(inputPath) fails when path unreadable", async () => {
54
+ const a = new BilibiliAdapter();
55
+ const res = await a.authenticate({ inputPath: path.join(tmpDir, "missing.json") });
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 BilibiliAdapter();
62
+ const res = await a.authenticate({});
63
+ expect(res.ok).toBe(false);
64
+ expect(res.reason).toBe("NO_INPUT");
65
+ });
66
+
67
+ it("rejects schemaVersion mismatch", async () => {
68
+ const p = writeSnapshot(tmpDir, {
69
+ schemaVersion: 99,
70
+ snapshottedAt: Date.now(),
71
+ events: [],
72
+ });
73
+ const a = new BilibiliAdapter();
74
+ let threw = null;
75
+ try {
76
+ for await (const _r of a.sync({ inputPath: p })) { /* drain */ }
77
+ } catch (err) {
78
+ threw = err;
79
+ }
80
+ expect(threw).toBeTruthy();
81
+ expect(String(threw.message)).toMatch(/schemaVersion mismatch/);
82
+ });
83
+
84
+ it("empty events array yields nothing (no crash)", async () => {
85
+ const p = writeSnapshot(tmpDir, {
86
+ schemaVersion: 1,
87
+ snapshottedAt: Date.now(),
88
+ events: [],
89
+ });
90
+ const a = new BilibiliAdapter();
91
+ const raws = [];
92
+ for await (const r of a.sync({ inputPath: p })) raws.push(r);
93
+ expect(raws).toHaveLength(0);
94
+ });
95
+
96
+ it("yields all 4 kinds + normalize produces valid batches", async () => {
97
+ const p = writeSnapshot(tmpDir, {
98
+ schemaVersion: 1,
99
+ snapshottedAt: 1716000000000,
100
+ account: { uid: "12345", displayName: "alice" },
101
+ events: [
102
+ {
103
+ kind: "history",
104
+ id: "BV1abc",
105
+ capturedAt: 1715000000000,
106
+ title: "Rust 异步学习",
107
+ bvid: "BV1abc",
108
+ avid: 42,
109
+ duration: 600,
110
+ uploader: "技术UP主",
111
+ uploaderMid: 100,
112
+ part: "01 介绍",
113
+ },
114
+ {
115
+ kind: "favourite",
116
+ id: "fav-BV2def",
117
+ capturedAt: 1714000000000,
118
+ title: "前端架构",
119
+ bvid: "BV2def",
120
+ folderName: "学习",
121
+ uploader: "码农UP",
122
+ },
123
+ {
124
+ kind: "dynamic",
125
+ id: "dyn-99",
126
+ capturedAt: 1713000000000,
127
+ summary: "今天发了一个新视频",
128
+ dynamicType: "video",
129
+ rid: 99,
130
+ authorMid: 200,
131
+ authorName: "我关注的UP",
132
+ },
133
+ {
134
+ kind: "follow",
135
+ id: "follow-300",
136
+ capturedAt: 1712000000000,
137
+ mid: 300,
138
+ uname: "美食UP",
139
+ face: "https://i0.hdslb.com/...",
140
+ sign: "好吃的视频",
141
+ },
142
+ ],
143
+ });
144
+ const a = new BilibiliAdapter();
145
+ const raws = [];
146
+ for await (const r of a.sync({ inputPath: p })) raws.push(r);
147
+
148
+ expect(raws).toHaveLength(4);
149
+ expect(raws.map((r) => r.kind).sort()).toEqual([
150
+ "dynamic",
151
+ "favourite",
152
+ "follow",
153
+ "history",
154
+ ]);
155
+ // Stable originalId format
156
+ expect(raws.find((r) => r.kind === "history").originalId).toBe("bilibili:history:BV1abc");
157
+ expect(raws.find((r) => r.kind === "favourite").originalId).toBe("bilibili:favourite:fav-BV2def");
158
+ expect(raws.find((r) => r.kind === "dynamic").originalId).toBe("bilibili:dynamic:dyn-99");
159
+ expect(raws.find((r) => r.kind === "follow").originalId).toBe("bilibili:follow:follow-300");
160
+
161
+ // Normalize each + validate
162
+ for (const raw of raws) {
163
+ const batch = a.normalize(raw);
164
+ const v = validateBatch(batch);
165
+ expect(v.valid).toBe(true);
166
+
167
+ if (raw.kind === "history") {
168
+ expect(batch.events[0].subtype).toBe("browse");
169
+ expect(batch.events[0].extra.bvid).toBe("BV1abc");
170
+ expect(batch.events[0].extra.duration).toBe(600);
171
+ expect(batch.items).toHaveLength(1);
172
+ expect(batch.items[0].name).toBe("Rust 异步学习");
173
+ } else if (raw.kind === "favourite") {
174
+ expect(batch.events[0].subtype).toBe("like");
175
+ expect(batch.events[0].extra.folderName).toBe("学习");
176
+ expect(batch.items).toHaveLength(1);
177
+ } else if (raw.kind === "dynamic") {
178
+ expect(batch.events[0].subtype).toBe("browse");
179
+ expect(batch.events[0].extra.dynamicType).toBe("video");
180
+ expect(batch.events[0].extra.authorName).toBe("我关注的UP");
181
+ } else if (raw.kind === "follow") {
182
+ // Follow yields a person, not an event
183
+ expect(batch.events).toHaveLength(0);
184
+ expect(batch.persons).toHaveLength(1);
185
+ expect(batch.persons[0].names[0]).toBe("美食UP");
186
+ expect(batch.persons[0].identifiers["bilibili-mid"]).toEqual(["300"]);
187
+ }
188
+ }
189
+ });
190
+
191
+ it("per-kind include filter (e.g. include.follow=false drops follows)", async () => {
192
+ const p = writeSnapshot(tmpDir, {
193
+ schemaVersion: 1,
194
+ snapshottedAt: Date.now(),
195
+ events: [
196
+ { kind: "history", id: "h1", title: "x" },
197
+ { kind: "follow", id: "f1", mid: 1, uname: "u" },
198
+ ],
199
+ });
200
+ const a = new BilibiliAdapter();
201
+ const raws = [];
202
+ for await (const r of a.sync({ inputPath: p, include: { follow: false } })) {
203
+ raws.push(r);
204
+ }
205
+ expect(raws).toHaveLength(1);
206
+ expect(raws[0].kind).toBe("history");
207
+ });
208
+
209
+ it("limit caps emission", async () => {
210
+ const p = writeSnapshot(tmpDir, {
211
+ schemaVersion: 1,
212
+ snapshottedAt: Date.now(),
213
+ events: [
214
+ { kind: "history", id: "1", title: "a" },
215
+ { kind: "history", id: "2", title: "b" },
216
+ { kind: "history", id: "3", title: "c" },
217
+ ],
218
+ });
219
+ const a = new BilibiliAdapter();
220
+ const raws = [];
221
+ for await (const r of a.sync({ inputPath: p, limit: 2 })) raws.push(r);
222
+ expect(raws).toHaveLength(2);
223
+ });
224
+
225
+ it("skips unknown kinds (forward-compat with future event types)", async () => {
226
+ const p = writeSnapshot(tmpDir, {
227
+ schemaVersion: 1,
228
+ snapshottedAt: Date.now(),
229
+ events: [
230
+ { kind: "history", id: "1", title: "ok" },
231
+ { kind: "fancy-new-kind-from-future", id: "x", data: "?" },
232
+ { kind: "favourite", id: "f", title: "also ok" },
233
+ ],
234
+ });
235
+ const a = new BilibiliAdapter();
236
+ const raws = [];
237
+ for await (const r of a.sync({ inputPath: p })) raws.push(r);
238
+ expect(raws).toHaveLength(2);
239
+ expect(raws.map((r) => r.kind).sort()).toEqual(["favourite", "history"]);
240
+ });
241
+
242
+ it("uses fallback originalId when event.id absent (no crash, still ingestable)", async () => {
243
+ const p = writeSnapshot(tmpDir, {
244
+ schemaVersion: 1,
245
+ snapshottedAt: Date.now(),
246
+ events: [
247
+ // Missing id — adapter should derive from bvid/mid/rid or generate fallback
248
+ { kind: "history", bvid: "BV1xyz", title: "no-id" },
249
+ { kind: "follow", mid: 999, uname: "with-mid-no-id" },
250
+ { kind: "dynamic", summary: "no id no rid" },
251
+ ],
252
+ });
253
+ const a = new BilibiliAdapter();
254
+ const raws = [];
255
+ for await (const r of a.sync({ inputPath: p })) raws.push(r);
256
+ expect(raws).toHaveLength(3);
257
+ // history derives from bvid
258
+ expect(raws[0].originalId).toBe("bilibili:history:BV1xyz");
259
+ // follow derives from mid
260
+ expect(raws[1].originalId).toBe("bilibili:follow:999");
261
+ // dynamic with no id/bvid/mid/rid → fallback unknown- prefix
262
+ expect(raws[2].originalId).toMatch(/^bilibili:dynamic:unknown-/);
263
+ });
264
+
265
+ it("snapshot account propagates to payload (Path Y can re-attribute later)", async () => {
266
+ const p = writeSnapshot(tmpDir, {
267
+ schemaVersion: 1,
268
+ snapshottedAt: Date.now(),
269
+ account: { uid: "55555", displayName: "tester" },
270
+ events: [{ kind: "history", id: "1", title: "x" }],
271
+ });
272
+ const a = new BilibiliAdapter();
273
+ const raws = [];
274
+ for await (const r of a.sync({ inputPath: p })) raws.push(r);
275
+ expect(raws).toHaveLength(1);
276
+ expect(raws[0].payload.account.uid).toBe("55555");
277
+ });
278
+ });
@@ -331,6 +331,29 @@ describe("normalizeWeChatContact", () => {
331
331
  const b = normalizeWeChatContact({});
332
332
  expect(b.persons).toHaveLength(0);
333
333
  });
334
+
335
+ // sjqz parity audit follow-up (post-Phase 12.6.10) — classify
336
+ // 公众号 / Official Accounts (gh_*) as merchant subtype so the Ask
337
+ // flow / EntityResolver can filter them out of human contacts.
338
+ it("gh_* username → subtype merchant (公众号 / Official Account)", () => {
339
+ const b = normalizeWeChatContact({
340
+ username: "gh_abc123def",
341
+ nickname: "某品牌官方",
342
+ type: 3,
343
+ });
344
+ expect(b.persons).toHaveLength(1);
345
+ expect(b.persons[0].subtype).toBe("merchant");
346
+ expect(b.persons[0].identifiers.wechatId).toBe("gh_abc123def");
347
+ });
348
+
349
+ it("regular wxid_* → subtype contact (default)", () => {
350
+ const b = normalizeWeChatContact({
351
+ username: "wxid_realfriend",
352
+ nickname: "好友",
353
+ type: 1,
354
+ });
355
+ expect(b.persons[0].subtype).toBe("contact");
356
+ });
334
357
  });
335
358
 
336
359
  // ─── WechatAdapter contract + sync flow ──────────────────────────────────
@@ -441,6 +464,101 @@ describe("WechatAdapter.sync with mocked DB reader", () => {
441
464
  }
442
465
  });
443
466
 
467
+ // sjqz parity audit follow-up — fetchContacts must exclude
468
+ // @stranger and fake_* by default (vault pollution prevention).
469
+ it("fetchContacts excludes @stranger and fake_* by default", async () => {
470
+ // Pure DI smoke — capture the SQL passed to .prepare() to verify the
471
+ // junk filter is in the query. We mock just enough of better-sqlite3's
472
+ // shape: db.prepare(sql) → { all(limit) → rows }, exec, pragma.
473
+ const seenSql = [];
474
+ const fakeDriver = function Database(_path, _opts) {
475
+ return {
476
+ pragma: () => undefined,
477
+ exec: () => undefined,
478
+ prepare(sql) {
479
+ seenSql.push(sql);
480
+ return {
481
+ all: () => {
482
+ if (sql.startsWith("PRAGMA table_info")) {
483
+ return [
484
+ { name: "username" },
485
+ { name: "alias" },
486
+ { name: "nickname" },
487
+ { name: "conRemark" },
488
+ { name: "type" },
489
+ ];
490
+ }
491
+ if (sql.startsWith("SELECT count")) return [{ n: 5 }];
492
+ if (sql.includes("FROM rcontact")) return [];
493
+ return [];
494
+ },
495
+ get: () => ({ n: 5 }),
496
+ };
497
+ },
498
+ close: () => undefined,
499
+ };
500
+ };
501
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "wechat-junkfilt-"));
502
+ const dbPath = path.join(dir, "EnMicroMsg.db");
503
+ fs.writeFileSync(dbPath, "fake");
504
+ try {
505
+ const reader = new WeChatDBReader({
506
+ dbPath,
507
+ keyProvider: { getKey: async () => "0".repeat(64) },
508
+ driver: fakeDriver,
509
+ });
510
+ await reader.open();
511
+ reader.fetchContacts({ limit: 100 });
512
+ const contactsSql = seenSql.find((s) => s.includes("FROM rcontact"));
513
+ expect(contactsSql).toBeDefined();
514
+ expect(contactsSql).toMatch(/NOT LIKE '%@stranger'/);
515
+ expect(contactsSql).toMatch(/NOT LIKE 'fake_%'/);
516
+ } finally {
517
+ fs.rmSync(dir, { recursive: true, force: true });
518
+ }
519
+ });
520
+
521
+ it("fetchContacts with includeJunk:true drops the filter (forensic mode)", async () => {
522
+ const seenSql = [];
523
+ const fakeDriver = function Database() {
524
+ return {
525
+ pragma: () => undefined,
526
+ exec: () => undefined,
527
+ prepare(sql) {
528
+ seenSql.push(sql);
529
+ return {
530
+ all: () => {
531
+ if (sql.startsWith("PRAGMA table_info")) {
532
+ return ["username", "alias", "nickname", "conRemark", "type"].map((name) => ({ name }));
533
+ }
534
+ if (sql.startsWith("SELECT count")) return [{ n: 5 }];
535
+ return [];
536
+ },
537
+ get: () => ({ n: 5 }),
538
+ };
539
+ },
540
+ close: () => undefined,
541
+ };
542
+ };
543
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "wechat-incljunk-"));
544
+ const dbPath = path.join(dir, "EnMicroMsg.db");
545
+ fs.writeFileSync(dbPath, "fake");
546
+ try {
547
+ const reader = new WeChatDBReader({
548
+ dbPath,
549
+ keyProvider: { getKey: async () => "0".repeat(64) },
550
+ driver: fakeDriver,
551
+ });
552
+ await reader.open();
553
+ reader.fetchContacts({ limit: 100, includeJunk: true });
554
+ const contactsSql = seenSql.find((s) => s.includes("FROM rcontact"));
555
+ expect(contactsSql).toBeDefined();
556
+ expect(contactsSql).not.toMatch(/NOT LIKE/);
557
+ } finally {
558
+ fs.rmSync(dir, { recursive: true, force: true });
559
+ }
560
+ });
561
+
444
562
  it("idle no-op when DB path missing", async () => {
445
563
  const a = new WechatAdapter({
446
564
  account: { uin: "self123" },