@chainlesschain/personal-data-hub 0.3.6 → 0.3.8
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-kuaishou-adb-api-client.test.js +432 -0
- package/__tests__/adapters/social-kuaishou-adb-collector.test.js +276 -0
- package/__tests__/adapters/social-kuaishou-adb-cookies-extension.test.js +141 -0
- package/__tests__/adapters/social-kuaishou-adb-snapshot-builder.test.js +178 -0
- package/__tests__/adapters/social-toutiao-adb-api-client.test.js +537 -0
- package/__tests__/adapters/social-toutiao-adb-collector.test.js +285 -0
- package/__tests__/adapters/social-toutiao-adb-cookies-extension.test.js +163 -0
- package/__tests__/adapters/social-toutiao-adb-snapshot-builder.test.js +196 -0
- package/__tests__/adapters/social-xiaohongshu-adb-sign-provider-injection.test.js +351 -0
- package/__tests__/analysis.test.js +239 -14
- package/__tests__/query-parser.test.js +86 -0
- package/__tests__/vault.test.js +88 -0
- package/lib/adapters/ai-chat-history/health-checker.js +11 -0
- package/lib/adapters/social-kuaishou-adb/api-client.js +397 -0
- package/lib/adapters/social-kuaishou-adb/collector.js +196 -0
- package/lib/adapters/social-kuaishou-adb/cookies-extension.js +261 -0
- package/lib/adapters/social-kuaishou-adb/index.js +53 -0
- package/lib/adapters/social-kuaishou-adb/snapshot-builder.js +145 -0
- package/lib/adapters/social-toutiao-adb/api-client.js +377 -0
- package/lib/adapters/social-toutiao-adb/collector.js +200 -0
- package/lib/adapters/social-toutiao-adb/cookies-extension.js +266 -0
- package/lib/adapters/social-toutiao-adb/index.js +52 -0
- package/lib/adapters/social-toutiao-adb/snapshot-builder.js +148 -0
- package/lib/adapters/social-xiaohongshu-adb/api-client.js +36 -5
- package/lib/adapters/social-xiaohongshu-adb/collector.js +102 -51
- package/lib/analysis.js +154 -17
- package/lib/query-parser.js +93 -0
- package/lib/vault.js +64 -0
- package/package.json +5 -1
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { describe, it, expect } from "vitest";
|
|
4
|
+
|
|
5
|
+
const {
|
|
6
|
+
createKuaishouCookiesExtension,
|
|
7
|
+
KUAISHOU_COOKIES_REMOTE_PATH,
|
|
8
|
+
KUAISHOU_COOKIE_HOST_DOMAIN,
|
|
9
|
+
KUAISHOU_LOGIN_COOKIES,
|
|
10
|
+
assembleKuaishouCookieHeader,
|
|
11
|
+
_internals,
|
|
12
|
+
} = require("../../lib/adapters/social-kuaishou-adb/cookies-extension");
|
|
13
|
+
|
|
14
|
+
describe("constants", () => {
|
|
15
|
+
it("path points to com.smile.gifmaker WebView cookies", () => {
|
|
16
|
+
expect(KUAISHOU_COOKIES_REMOTE_PATH).toBe(
|
|
17
|
+
"/data/data/com.smile.gifmaker/app_webview/Default/Cookies",
|
|
18
|
+
);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("host domain is kuaishou.com", () => {
|
|
22
|
+
expect(KUAISHOU_COOKIE_HOST_DOMAIN).toBe("kuaishou.com");
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("login cookies enumerated (userId OR kuaishou.web.cp.api_ph)", () => {
|
|
26
|
+
expect([...KUAISHOU_LOGIN_COOKIES]).toEqual([
|
|
27
|
+
"userId",
|
|
28
|
+
"kuaishou.web.cp.api_ph",
|
|
29
|
+
]);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
describe("pickUidFromCookieMap", () => {
|
|
34
|
+
const make = (entries) =>
|
|
35
|
+
new Map(entries.map(([k, v]) => [k, { value: v }]));
|
|
36
|
+
|
|
37
|
+
it("prefers direct userId cookie", () => {
|
|
38
|
+
const map = make([
|
|
39
|
+
["userId", "12345"],
|
|
40
|
+
["kuaishou.web.cp.api_ph", encodeURIComponent('{"user_id":99999}')],
|
|
41
|
+
]);
|
|
42
|
+
expect(_internals.pickUidFromCookieMap(map)).toBe("12345");
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("falls back to api_ph nested user_id", () => {
|
|
46
|
+
const map = make([
|
|
47
|
+
["kuaishou.web.cp.api_ph", encodeURIComponent('{"user_id":"55555"}')],
|
|
48
|
+
]);
|
|
49
|
+
expect(_internals.pickUidFromCookieMap(map)).toBe("55555");
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("falls back to api_ph nested uid alternate", () => {
|
|
53
|
+
const map = make([
|
|
54
|
+
["kuaishou.web.cp.api_ph", encodeURIComponent('{"uid":"77777"}')],
|
|
55
|
+
]);
|
|
56
|
+
expect(_internals.pickUidFromCookieMap(map)).toBe("77777");
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("handles api_ph not URL-encoded (raw)", () => {
|
|
60
|
+
const map = make([["kuaishou.web.cp.api_ph", '{"user_id":"33333"}']]);
|
|
61
|
+
expect(_internals.pickUidFromCookieMap(map)).toBe("33333");
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("returns null for userId=0 (guest sentinel)", () => {
|
|
65
|
+
const map = make([["userId", "0"]]);
|
|
66
|
+
expect(_internals.pickUidFromCookieMap(map)).toBe(null);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("returns null when only anti-bot cookies present", () => {
|
|
70
|
+
const map = make([["did", "anonid"]]);
|
|
71
|
+
expect(_internals.pickUidFromCookieMap(map)).toBe(null);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
describe("assembleKuaishouCookieHeader", () => {
|
|
76
|
+
const mkCookie = (name, value, hostKey = ".kuaishou.com") => ({
|
|
77
|
+
name,
|
|
78
|
+
value,
|
|
79
|
+
hostKey,
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("builds header from direct userId cookie", () => {
|
|
83
|
+
const cookies = [
|
|
84
|
+
mkCookie("userId", "12345"),
|
|
85
|
+
mkCookie("did", "anon"),
|
|
86
|
+
];
|
|
87
|
+
const r = assembleKuaishouCookieHeader(cookies);
|
|
88
|
+
expect(r.header).toContain("userId=12345");
|
|
89
|
+
expect(r.uid).toBe("12345");
|
|
90
|
+
expect(r.missing).toEqual([]);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("builds header from api_ph alone (no direct userId)", () => {
|
|
94
|
+
const cookies = [
|
|
95
|
+
mkCookie(
|
|
96
|
+
"kuaishou.web.cp.api_ph",
|
|
97
|
+
encodeURIComponent('{"user_id":"55555","user_name":"Alice"}'),
|
|
98
|
+
),
|
|
99
|
+
];
|
|
100
|
+
const r = assembleKuaishouCookieHeader(cookies);
|
|
101
|
+
expect(r.header).toContain("kuaishou.web.cp.api_ph=");
|
|
102
|
+
expect(r.uid).toBe("55555");
|
|
103
|
+
expect(r.missing).toEqual([]);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("returns null header when no login cookie present", () => {
|
|
107
|
+
const cookies = [mkCookie("did", "anon"), mkCookie("ttwid", "x")];
|
|
108
|
+
const r = assembleKuaishouCookieHeader(cookies);
|
|
109
|
+
expect(r.header).toBe(null);
|
|
110
|
+
expect(r.uid).toBe(null);
|
|
111
|
+
expect(r.missing).toEqual(["userId", "kuaishou.web.cp.api_ph"]);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("dedupes by longest hostKey when same name appears twice", () => {
|
|
115
|
+
const cookies = [
|
|
116
|
+
mkCookie("userId", "subdomain-val", "www.kuaishou.com"),
|
|
117
|
+
mkCookie("userId", "wildcard-val", ".kuaishou.com"),
|
|
118
|
+
];
|
|
119
|
+
const r = assembleKuaishouCookieHeader(cookies);
|
|
120
|
+
expect(r.header).toContain("userId=subdomain-val");
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("throws TypeError on non-array input", () => {
|
|
124
|
+
expect(() => assembleKuaishouCookieHeader(null)).toThrow(TypeError);
|
|
125
|
+
expect(() => assembleKuaishouCookieHeader("x")).toThrow(TypeError);
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
describe("createKuaishouCookiesExtension", () => {
|
|
130
|
+
it("rejects when ctx missing required functions", async () => {
|
|
131
|
+
const ext = createKuaishouCookiesExtension();
|
|
132
|
+
await expect(ext({}, {})).rejects.toThrow(/ctx must provide/);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("rejects when adb is not a function", async () => {
|
|
136
|
+
const ext = createKuaishouCookiesExtension();
|
|
137
|
+
await expect(
|
|
138
|
+
ext({}, { adb: null, pickDevice: () => "serial" }),
|
|
139
|
+
).rejects.toThrow(/ctx must provide/);
|
|
140
|
+
});
|
|
141
|
+
});
|
|
@@ -0,0 +1,178 @@
|
|
|
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
|
+
|
|
8
|
+
const {
|
|
9
|
+
buildSnapshot,
|
|
10
|
+
writeSnapshotJson,
|
|
11
|
+
cleanupSnapshotJson,
|
|
12
|
+
SNAPSHOT_SCHEMA_VERSION,
|
|
13
|
+
} = require("../../lib/adapters/social-kuaishou-adb/snapshot-builder");
|
|
14
|
+
|
|
15
|
+
describe("SNAPSHOT_SCHEMA_VERSION", () => {
|
|
16
|
+
it("is 1 (matches existing social-kuaishou adapter)", () => {
|
|
17
|
+
expect(SNAPSHOT_SCHEMA_VERSION).toBe(1);
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
describe("buildSnapshot", () => {
|
|
22
|
+
it("throws on missing uid", () => {
|
|
23
|
+
expect(() => buildSnapshot({})).toThrow(/uid must be a non-empty string/);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("emits profile event from input.profile", () => {
|
|
27
|
+
const snap = buildSnapshot({
|
|
28
|
+
uid: "12345",
|
|
29
|
+
profile: {
|
|
30
|
+
nickname: "Alice",
|
|
31
|
+
kuaishouId: "alice_ks",
|
|
32
|
+
avatarUrl: "https://a/x.jpg",
|
|
33
|
+
sex: "F",
|
|
34
|
+
city: "Beijing",
|
|
35
|
+
},
|
|
36
|
+
snapshottedAt: 1716383021000,
|
|
37
|
+
});
|
|
38
|
+
expect(snap.schemaVersion).toBe(1);
|
|
39
|
+
expect(snap.account).toEqual({ uid: "12345", displayName: "" });
|
|
40
|
+
const prof = snap.events.find((e) => e.kind === "profile");
|
|
41
|
+
expect(prof).toMatchObject({
|
|
42
|
+
kind: "profile",
|
|
43
|
+
id: "profile-12345",
|
|
44
|
+
uid: "12345",
|
|
45
|
+
nickname: "Alice",
|
|
46
|
+
kuaishouId: "alice_ks",
|
|
47
|
+
city: "Beijing",
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("emits watch events with photo metadata", () => {
|
|
52
|
+
const snap = buildSnapshot({
|
|
53
|
+
uid: "1",
|
|
54
|
+
watch: [
|
|
55
|
+
{
|
|
56
|
+
photoId: "P1",
|
|
57
|
+
caption: "Funny vid",
|
|
58
|
+
duration: 30,
|
|
59
|
+
authorId: "A1",
|
|
60
|
+
authorName: "Auth1",
|
|
61
|
+
viewedAt: 1716000000000,
|
|
62
|
+
},
|
|
63
|
+
],
|
|
64
|
+
});
|
|
65
|
+
const w = snap.events.filter((e) => e.kind === "watch");
|
|
66
|
+
expect(w).toHaveLength(1);
|
|
67
|
+
expect(w[0]).toMatchObject({
|
|
68
|
+
kind: "watch",
|
|
69
|
+
id: "photo-P1",
|
|
70
|
+
capturedAt: 1716000000000,
|
|
71
|
+
photoId: "P1",
|
|
72
|
+
caption: "Funny vid",
|
|
73
|
+
duration: 30,
|
|
74
|
+
authorId: "A1",
|
|
75
|
+
authorName: "Auth1",
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("emits collect events with self as author", () => {
|
|
80
|
+
const snap = buildSnapshot({
|
|
81
|
+
uid: "12345",
|
|
82
|
+
displayName: "Alice",
|
|
83
|
+
collect: [{ photoId: "C1", caption: "My vid", postedAt: 1716100000000 }],
|
|
84
|
+
});
|
|
85
|
+
const c = snap.events.filter((e) => e.kind === "collect");
|
|
86
|
+
expect(c).toHaveLength(1);
|
|
87
|
+
expect(c[0]).toMatchObject({
|
|
88
|
+
kind: "collect",
|
|
89
|
+
id: "collect-C1",
|
|
90
|
+
capturedAt: 1716100000000,
|
|
91
|
+
photoId: "C1",
|
|
92
|
+
caption: "My vid",
|
|
93
|
+
authorId: "12345", // self
|
|
94
|
+
authorName: "Alice",
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("emits search events with keyword:ts id", () => {
|
|
99
|
+
const snap = buildSnapshot({
|
|
100
|
+
uid: "1",
|
|
101
|
+
search: [{ keyword: "AI", searchedAt: 1716200000000 }],
|
|
102
|
+
});
|
|
103
|
+
const s = snap.events.find((e) => e.kind === "search");
|
|
104
|
+
expect(s).toMatchObject({
|
|
105
|
+
kind: "search",
|
|
106
|
+
id: "search-AI:1716200000000",
|
|
107
|
+
capturedAt: 1716200000000,
|
|
108
|
+
keyword: "AI",
|
|
109
|
+
searchAt: 1716200000000,
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("ignores entries missing photoId / keyword", () => {
|
|
114
|
+
const snap = buildSnapshot({
|
|
115
|
+
uid: "1",
|
|
116
|
+
watch: [{ caption: "no photoId" }, { photoId: "OK" }],
|
|
117
|
+
collect: [{ caption: "no photoId" }],
|
|
118
|
+
search: [{ keyword: "" }, { keyword: "good" }],
|
|
119
|
+
});
|
|
120
|
+
expect(snap.events.filter((e) => e.kind === "watch")).toHaveLength(1);
|
|
121
|
+
expect(snap.events.filter((e) => e.kind === "collect")).toHaveLength(0);
|
|
122
|
+
expect(snap.events.filter((e) => e.kind === "search")).toHaveLength(1);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("falls back to snapshottedAt when event lacks timestamp", () => {
|
|
126
|
+
const snap = buildSnapshot({
|
|
127
|
+
uid: "1",
|
|
128
|
+
watch: [{ photoId: "P" }],
|
|
129
|
+
snapshottedAt: 1716000000000,
|
|
130
|
+
});
|
|
131
|
+
expect(snap.events[0].capturedAt).toBe(1716000000000);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("emits all 4 kinds at once", () => {
|
|
135
|
+
const snap = buildSnapshot({
|
|
136
|
+
uid: "12345",
|
|
137
|
+
profile: { nickname: "Alice" },
|
|
138
|
+
watch: [{ photoId: "W1" }],
|
|
139
|
+
collect: [{ photoId: "C1" }],
|
|
140
|
+
search: [{ keyword: "kw1", searchedAt: 1 }],
|
|
141
|
+
});
|
|
142
|
+
expect(snap.events).toHaveLength(4);
|
|
143
|
+
expect(snap.events.map((e) => e.kind).sort()).toEqual([
|
|
144
|
+
"collect",
|
|
145
|
+
"profile",
|
|
146
|
+
"search",
|
|
147
|
+
"watch",
|
|
148
|
+
]);
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
describe("writeSnapshotJson + cleanupSnapshotJson", () => {
|
|
153
|
+
it("writes valid JSON", () => {
|
|
154
|
+
const snap = buildSnapshot({ uid: "1", profile: { nickname: "A" } });
|
|
155
|
+
const full = writeSnapshotJson(snap);
|
|
156
|
+
try {
|
|
157
|
+
expect(fs.existsSync(full)).toBe(true);
|
|
158
|
+
const round = JSON.parse(fs.readFileSync(full, "utf-8"));
|
|
159
|
+
expect(round.account.uid).toBe("1");
|
|
160
|
+
} finally {
|
|
161
|
+
cleanupSnapshotJson(full);
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("rejects fileName with path separator", () => {
|
|
166
|
+
const snap = buildSnapshot({ uid: "1" });
|
|
167
|
+
expect(() =>
|
|
168
|
+
writeSnapshotJson(snap, { fileName: "../escape.json" }),
|
|
169
|
+
).toThrow(/basename, not a path/);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("cleanup tolerates missing file", () => {
|
|
173
|
+
expect(() => cleanupSnapshotJson(null)).not.toThrow();
|
|
174
|
+
expect(() =>
|
|
175
|
+
cleanupSnapshotJson(path.join(os.tmpdir(), "nonexistent-ks.json")),
|
|
176
|
+
).not.toThrow();
|
|
177
|
+
});
|
|
178
|
+
});
|