@chainlesschain/personal-data-hub 0.4.7 → 0.4.23
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/biz-tianyancha.test.js +159 -0
- package/__tests__/adapters/doc-baidu-netdisk.test.js +102 -0
- package/__tests__/adapters/doc-camscanner.test.js +147 -0
- package/__tests__/adapters/doc-platforms.test.js +177 -0
- package/__tests__/adapters/gov-ixiamen.test.js +150 -0
- package/__tests__/adapters/gov-tax.test.js +135 -0
- package/__tests__/adapters/health-meiyou.test.js +125 -0
- package/__tests__/adapters/music-kugou.test.js +187 -0
- package/__tests__/adapters/recruit-boss.test.js +180 -0
- package/__tests__/adapters/shopping-dianping.test.js +239 -0
- package/__tests__/adapters/social-csdn.test.js +175 -0
- package/__tests__/adapters/social-dongchedi.test.js +165 -0
- package/__tests__/adapters/social-zhihu.test.js +246 -0
- package/__tests__/adapters/travel-ctrip.test.js +175 -1
- package/__tests__/adapters/travel-didi.test.js +204 -0
- package/__tests__/adapters/travel-tongcheng.test.js +289 -0
- package/__tests__/adapters/video-platforms.test.js +152 -0
- package/__tests__/adapters/video-xigua.test.js +106 -0
- package/__tests__/adapters/wework-pc.test.js +124 -0
- package/lib/adapter-guide.js +25 -3
- package/lib/adapters/_document-base.js +370 -0
- package/lib/adapters/_video-base.js +331 -0
- package/lib/adapters/biz-tianyancha/index.js +348 -0
- package/lib/adapters/doc-baidu-netdisk/index.js +91 -0
- package/lib/adapters/doc-camscanner/index.js +102 -0
- package/lib/adapters/doc-tencent-docs/index.js +94 -0
- package/lib/adapters/doc-wps/index.js +77 -0
- package/lib/adapters/gov-ixiamen/index.js +380 -0
- package/lib/adapters/gov-tax/index.js +451 -0
- package/lib/adapters/health-meiyou/index.js +393 -0
- package/lib/adapters/music-kugou/index.js +418 -0
- package/lib/adapters/recruit-boss/index.js +442 -0
- package/lib/adapters/shopping-dianping/index.js +473 -0
- package/lib/adapters/social-csdn/index.js +444 -0
- package/lib/adapters/social-dongchedi/index.js +360 -0
- package/lib/adapters/social-zhihu/index.js +488 -0
- package/lib/adapters/travel-ctrip/index.js +255 -40
- package/lib/adapters/travel-didi/index.js +327 -0
- package/lib/adapters/travel-tongcheng/index.js +393 -0
- package/lib/adapters/video-iqiyi/index.js +75 -0
- package/lib/adapters/video-tencent/index.js +78 -0
- package/lib/adapters/video-xigua/index.js +68 -0
- package/lib/adapters/wework-pc/index.js +31 -0
- package/lib/index.js +40 -0
- package/package.json +1 -1
|
@@ -0,0 +1,150 @@
|
|
|
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 ix = require("../../lib/adapters/gov-ixiamen");
|
|
10
|
+
|
|
11
|
+
function writeTmp(content) {
|
|
12
|
+
const p = path.join(os.tmpdir(), `cc-ix-${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
|
+
|
|
22
|
+
const COOKIES = "XMGOV_SSO=abc; tgc=xyz";
|
|
23
|
+
|
|
24
|
+
describe("gov-ixiamen mappers", () => {
|
|
25
|
+
it("name/version/capabilities", () => {
|
|
26
|
+
expect(ix.NAME).toBe("gov-ixiamen");
|
|
27
|
+
expect(ix.VERSION).toBe("0.1.0");
|
|
28
|
+
});
|
|
29
|
+
it("inferCategory: explicit wins, else keyword, else fallback", () => {
|
|
30
|
+
expect(ix.inferCategory("随便", "医保")).toBe("医保");
|
|
31
|
+
expect(ix.inferCategory("城乡居民医保缴费")).toBe("医保");
|
|
32
|
+
expect(ix.inferCategory("住房公积金提取")).toBe("公积金");
|
|
33
|
+
expect(ix.inferCategory("机动车违章处理")).toBe("车驾管");
|
|
34
|
+
expect(ix.inferCategory("某个没见过的事项")).toBe("其他政务");
|
|
35
|
+
});
|
|
36
|
+
it("mapService maps gov-service fields; no id → null", () => {
|
|
37
|
+
const rec = ix.mapService({
|
|
38
|
+
service_id: "S1",
|
|
39
|
+
service_name: "养老保险待遇资格认证",
|
|
40
|
+
handle_time: 1716383000,
|
|
41
|
+
status: "已办结",
|
|
42
|
+
deptName: "厦门市社保中心",
|
|
43
|
+
});
|
|
44
|
+
expect(rec).toMatchObject({ serviceId: "S1", category: "社保", status: "已办结", dept: "厦门市社保中心" });
|
|
45
|
+
expect(rec.handledMs).toBe(1716383000000);
|
|
46
|
+
expect(ix.mapService({ service_name: "noid" })).toBe(null);
|
|
47
|
+
});
|
|
48
|
+
it("extractList tolerant", () => {
|
|
49
|
+
expect(ix.extractList({ list: [{ id: 1 }] })).toHaveLength(1);
|
|
50
|
+
expect(ix.extractList({ data: { records: [{ id: 1 }] } })).toHaveLength(1);
|
|
51
|
+
expect(ix.extractList({})).toEqual([]);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe("IXiamenAdapter (snapshot + cookie-api)", () => {
|
|
56
|
+
const SNAP = JSON.stringify({
|
|
57
|
+
schemaVersion: 1,
|
|
58
|
+
snapshottedAt: 1716383000000,
|
|
59
|
+
account: { userId: "u1", name: "张三" },
|
|
60
|
+
events: [
|
|
61
|
+
{
|
|
62
|
+
kind: "service",
|
|
63
|
+
id: "svc-S1",
|
|
64
|
+
serviceId: "S1",
|
|
65
|
+
serviceName: "住房公积金缴存明细查询",
|
|
66
|
+
handledTime: 1716383000,
|
|
67
|
+
status: "已办结",
|
|
68
|
+
dept: "厦门住房公积金中心",
|
|
69
|
+
},
|
|
70
|
+
],
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("snapshot sync + normalize → INTERACTION event + category topic", async () => {
|
|
74
|
+
const p = writeTmp(SNAP);
|
|
75
|
+
try {
|
|
76
|
+
const a = new ix.IXiamenAdapter();
|
|
77
|
+
expect((await a.authenticate({ inputPath: p })).mode).toBe("snapshot-file");
|
|
78
|
+
const items = await collect(a.sync({ inputPath: p }));
|
|
79
|
+
expect(items).toHaveLength(1);
|
|
80
|
+
expect(items[0].originalId).toBe("ixiamen:service:S1");
|
|
81
|
+
const batch = a.normalize(items[0]);
|
|
82
|
+
expect(batch.events[0].subtype).toBe("interaction");
|
|
83
|
+
expect(batch.events[0].content.title).toBe("办理: 住房公积金缴存明细查询");
|
|
84
|
+
expect(batch.events[0].extra.category).toBe("公积金");
|
|
85
|
+
expect(batch.topics[0].name).toBe("公积金");
|
|
86
|
+
expect(batch.topics[0].id).toBe("topic-ixiamen-cat-公积金");
|
|
87
|
+
expect(batch.events[0].extra.topicRef).toBe("topic-ixiamen-cat-公积金");
|
|
88
|
+
} finally {
|
|
89
|
+
fs.unlinkSync(p);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("dataDisclosure: high sensitivity + legalGate (gov real-name)", () => {
|
|
94
|
+
const a = new ix.IXiamenAdapter();
|
|
95
|
+
expect(a.dataDisclosure.sensitivity).toBe("high");
|
|
96
|
+
expect(a.dataDisclosure.legalGate).toBe(true);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("cookie-api: best-effort fetch + paginate + unverified flag", async () => {
|
|
100
|
+
const pages = [
|
|
101
|
+
{ list: [{ id: 7, name: "门诊报销", category: "医保", handledTime: 1716383000 }] },
|
|
102
|
+
{ list: [] },
|
|
103
|
+
];
|
|
104
|
+
const calls = [];
|
|
105
|
+
const a = new ix.IXiamenAdapter({
|
|
106
|
+
account: { cookies: COOKIES, userId: "u1" },
|
|
107
|
+
fetchFn: async ({ url, cookies, query, sign }) => {
|
|
108
|
+
calls.push({ url, cookies, page: query.page, sign });
|
|
109
|
+
return query.page === 1 ? pages[0] : pages[1];
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
const auth = await a.authenticate();
|
|
113
|
+
expect(auth).toMatchObject({ ok: true, mode: "cookie", unverified: true });
|
|
114
|
+
const items = await collect(a.sync({}));
|
|
115
|
+
expect(items).toHaveLength(1);
|
|
116
|
+
expect(items[0].originalId).toBe("ixiamen:service:7");
|
|
117
|
+
expect(calls[0].cookies).toBe(COOKIES);
|
|
118
|
+
expect(calls[0].sign).toBe(null);
|
|
119
|
+
const batch = a.normalize(items[0]);
|
|
120
|
+
expect(batch.events[0].extra.category).toBe("医保");
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("cookie-api: signProvider seam invoked + sinceWatermark stops early", async () => {
|
|
124
|
+
let seen = null;
|
|
125
|
+
const a = new ix.IXiamenAdapter({
|
|
126
|
+
account: { cookies: COOKIES },
|
|
127
|
+
signProvider: async ({ url, query }) => {
|
|
128
|
+
seen = { url, page: query.page };
|
|
129
|
+
return "gov-sig";
|
|
130
|
+
},
|
|
131
|
+
fetchFn: async () => ({
|
|
132
|
+
list: [
|
|
133
|
+
{ id: "new", name: "新事项", handledTime: 1716390000 },
|
|
134
|
+
{ id: "old", name: "旧事项", handledTime: 1700000000 },
|
|
135
|
+
],
|
|
136
|
+
}),
|
|
137
|
+
});
|
|
138
|
+
const items = await collect(a.sync({ sinceWatermark: 1716000000000 }));
|
|
139
|
+
expect(items).toHaveLength(1); // old one below watermark stops iteration
|
|
140
|
+
expect(items[0].originalId).toBe("ixiamen:service:new");
|
|
141
|
+
expect(seen.url).toContain("ixm.gov.cn");
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("default fetch throws; no input throws", async () => {
|
|
145
|
+
const a = new ix.IXiamenAdapter({ account: { cookies: COOKIES } });
|
|
146
|
+
await expect(collect(a.sync({}))).rejects.toThrow(/no fetchFn configured/);
|
|
147
|
+
const b = new ix.IXiamenAdapter();
|
|
148
|
+
await expect(collect(b.sync({}))).rejects.toThrow(/needs opts.inputPath/);
|
|
149
|
+
});
|
|
150
|
+
});
|
|
@@ -0,0 +1,135 @@
|
|
|
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 tx = require("../../lib/adapters/gov-tax");
|
|
10
|
+
|
|
11
|
+
function writeTmp(content) {
|
|
12
|
+
const p = path.join(os.tmpdir(), `cc-tax-${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
|
+
|
|
22
|
+
const COOKIES = "ETAX_SSO=abc; sid=xyz";
|
|
23
|
+
|
|
24
|
+
describe("gov-tax mappers", () => {
|
|
25
|
+
it("name/version", () => {
|
|
26
|
+
expect(tx.NAME).toBe("gov-tax");
|
|
27
|
+
expect(tx.VERSION).toBe("0.1.0");
|
|
28
|
+
});
|
|
29
|
+
it("periodToMs / toAmount", () => {
|
|
30
|
+
expect(tx.periodToMs("2025-03")).toBe(Date.parse("2025-03-01T00:00:00Z"));
|
|
31
|
+
expect(tx.periodToMs("202503")).toBe(Date.parse("2025-03-01T00:00:00Z"));
|
|
32
|
+
expect(tx.toAmount("¥1,234.56")).toBeCloseTo(1234.56, 2);
|
|
33
|
+
expect(tx.toAmount(2000)).toBe(2000);
|
|
34
|
+
expect(tx.toAmount("abc")).toBe(null);
|
|
35
|
+
});
|
|
36
|
+
it("mapIncome / mapDeclaration field aliases; no id → null", () => {
|
|
37
|
+
const inc = tx.mapIncome({ record_id: "I1", tax_period: "2025-03", income_type: "工资薪金", amount: "20,000", withheldTax: 1234.56, company: "某某公司", companyId: "9144" });
|
|
38
|
+
expect(inc).toMatchObject({ recordId: "I1", incomeType: "工资薪金", payerName: "某某公司", payerId: "9144" });
|
|
39
|
+
expect(inc.amount).toBe(20000);
|
|
40
|
+
expect(inc.withheld).toBeCloseTo(1234.56, 2);
|
|
41
|
+
expect(tx.mapIncome({ amount: 1 })).toBe(null);
|
|
42
|
+
const dec = tx.mapDeclaration({ id: "D1", tax_year: 2024, type: "综合所得年度汇算", status: "已退税", amount: -800 });
|
|
43
|
+
expect(dec).toMatchObject({ recordId: "D1", year: 2024, declType: "综合所得年度汇算", status: "已退税", settleAmount: -800 });
|
|
44
|
+
expect(tx.mapDeclaration({ year: 2024 })).toBe(null);
|
|
45
|
+
});
|
|
46
|
+
it("extractList tolerant", () => {
|
|
47
|
+
expect(tx.extractList({ list: [{ id: 1 }] })).toHaveLength(1);
|
|
48
|
+
expect(tx.extractList({ data: { records: [{ id: 1 }] } })).toHaveLength(1);
|
|
49
|
+
expect(tx.extractList({})).toEqual([]);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
describe("TaxAdapter (snapshot + cookie-api)", () => {
|
|
54
|
+
const SNAP = JSON.stringify({
|
|
55
|
+
schemaVersion: 1,
|
|
56
|
+
snapshottedAt: 1716383000000,
|
|
57
|
+
account: { userId: "u1" },
|
|
58
|
+
events: [
|
|
59
|
+
{ kind: "income", id: "inc-I1", recordId: "I1", period: "2025-03", incomeType: "工资薪金", amount: 20000, withheld: 1234.56, payerName: "某某科技有限公司", payerId: "9144ABC" },
|
|
60
|
+
{ kind: "declaration", id: "dec-D1", recordId: "D1", year: 2024, declType: "综合所得年度汇算", status: "已退税", settleAmount: -800, declaredAt: 1716383000 },
|
|
61
|
+
],
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("income → INCOME event + employer Person(MERCHANT)", async () => {
|
|
65
|
+
const p = writeTmp(SNAP);
|
|
66
|
+
try {
|
|
67
|
+
const a = new tx.TaxAdapter();
|
|
68
|
+
expect((await a.authenticate({ inputPath: p })).mode).toBe("snapshot-file");
|
|
69
|
+
const items = await collect(a.sync({ inputPath: p }));
|
|
70
|
+
expect(items).toHaveLength(2);
|
|
71
|
+
const inc = a.normalize(items[0]);
|
|
72
|
+
expect(inc.events[0].subtype).toBe("income");
|
|
73
|
+
expect(inc.events[0].extra.incomeType).toBe("工资薪金");
|
|
74
|
+
expect(inc.events[0].extra.amount).toBe(20000);
|
|
75
|
+
expect(inc.persons).toHaveLength(1);
|
|
76
|
+
expect(inc.persons[0].subtype).toBe("merchant");
|
|
77
|
+
expect(inc.persons[0].names[0]).toBe("某某科技有限公司");
|
|
78
|
+
expect(inc.events[0].extra.payerRef).toBe(inc.persons[0].id);
|
|
79
|
+
expect(items[0].originalId).toBe("tax:income:I1");
|
|
80
|
+
} finally {
|
|
81
|
+
fs.unlinkSync(p);
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("declaration → OTHER event with settleAmount", async () => {
|
|
86
|
+
const p = writeTmp(SNAP);
|
|
87
|
+
try {
|
|
88
|
+
const a = new tx.TaxAdapter();
|
|
89
|
+
const items = await collect(a.sync({ inputPath: p }));
|
|
90
|
+
const dec = a.normalize(items[1]);
|
|
91
|
+
expect(dec.events[0].subtype).toBe("other");
|
|
92
|
+
expect(dec.events[0].content.title).toContain("个税申报");
|
|
93
|
+
expect(dec.events[0].extra.settleAmount).toBe(-800);
|
|
94
|
+
expect(dec.events[0].extra.year).toBe(2024);
|
|
95
|
+
expect(items[1].originalId).toBe("tax:declaration:D1");
|
|
96
|
+
} finally {
|
|
97
|
+
fs.unlinkSync(p);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("dataDisclosure: high sensitivity + legalGate (financial/tax)", () => {
|
|
102
|
+
const a = new tx.TaxAdapter();
|
|
103
|
+
expect(a.dataDisclosure.sensitivity).toBe("high");
|
|
104
|
+
expect(a.dataDisclosure.legalGate).toBe(true);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("cookie-api: best-effort both kinds + unverified flag + sign seam", async () => {
|
|
108
|
+
let signed = 0;
|
|
109
|
+
const a = new tx.TaxAdapter({
|
|
110
|
+
account: { cookies: COOKIES, userId: "u1" },
|
|
111
|
+
signProvider: async () => {
|
|
112
|
+
signed += 1;
|
|
113
|
+
return "sig";
|
|
114
|
+
},
|
|
115
|
+
fetchFn: async ({ url, query }) => {
|
|
116
|
+
if (query.page > 1) return { list: [] };
|
|
117
|
+
if (url.includes("/income")) return { list: [{ recordId: "I9", period: "2025-01", incomeType: "劳务报酬", amount: 5000 }] };
|
|
118
|
+
return { list: [{ recordId: "D9", year: 2024, declType: "年度汇算", settleAmount: 300 }] };
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
const auth = await a.authenticate();
|
|
122
|
+
expect(auth).toMatchObject({ ok: true, mode: "cookie", unverified: true });
|
|
123
|
+
const items = await collect(a.sync({}));
|
|
124
|
+
expect(items).toHaveLength(2);
|
|
125
|
+
expect(items.map((i) => i.originalId).sort()).toEqual(["tax:declaration:D9", "tax:income:I9"]);
|
|
126
|
+
expect(signed).toBeGreaterThan(0);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("default fetch throws; no input throws", async () => {
|
|
130
|
+
const a = new tx.TaxAdapter({ account: { cookies: COOKIES } });
|
|
131
|
+
await expect(collect(a.sync({}))).rejects.toThrow(/no fetchFn configured/);
|
|
132
|
+
const b = new tx.TaxAdapter();
|
|
133
|
+
await expect(collect(b.sync({}))).rejects.toThrow(/needs opts.inputPath/);
|
|
134
|
+
});
|
|
135
|
+
});
|
|
@@ -0,0 +1,125 @@
|
|
|
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 my = require("../../lib/adapters/health-meiyou");
|
|
10
|
+
|
|
11
|
+
function writeTmp(content) {
|
|
12
|
+
const p = path.join(os.tmpdir(), `cc-my-${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
|
+
|
|
22
|
+
const COOKIES = "myclient_id=abc; sid=xyz";
|
|
23
|
+
|
|
24
|
+
describe("health-meiyou mappers", () => {
|
|
25
|
+
it("name/version", () => {
|
|
26
|
+
expect(my.NAME).toBe("health-meiyou");
|
|
27
|
+
expect(my.VERSION).toBe("0.1.0");
|
|
28
|
+
});
|
|
29
|
+
it("mapPeriod / mapRecord field aliases; no id → null", () => {
|
|
30
|
+
const p = my.mapPeriod({ record_id: "P1", start_date: 1716383000, end_date: 1716800000, cycle_length: 28, period_length: 5 });
|
|
31
|
+
expect(p).toMatchObject({ recordId: "P1", cycleLength: 28, periodLength: 5 });
|
|
32
|
+
expect(p.startMs).toBe(1716383000000);
|
|
33
|
+
expect(my.mapPeriod({})).toBe(null);
|
|
34
|
+
const r = my.mapRecord({ id: "R1", record_type: "mood", date: 1716383000, value: "开心", remark: "今天不错" });
|
|
35
|
+
expect(r).toMatchObject({ recordId: "R1", recordType: "mood", value: "开心", note: "今天不错" });
|
|
36
|
+
expect(my.mapRecord({ note: "noid" })).toBe(null);
|
|
37
|
+
});
|
|
38
|
+
it("extractList tolerant", () => {
|
|
39
|
+
expect(my.extractList({ list: [{ id: 1 }] })).toHaveLength(1);
|
|
40
|
+
expect(my.extractList({ data: { calendar: [{ id: 1 }] } })).toHaveLength(1);
|
|
41
|
+
expect(my.extractList({})).toEqual([]);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe("MeiyouAdapter (snapshot + cookie-api)", () => {
|
|
46
|
+
const SNAP = JSON.stringify({
|
|
47
|
+
schemaVersion: 1,
|
|
48
|
+
snapshottedAt: 1716383000000,
|
|
49
|
+
account: { userId: "u1" },
|
|
50
|
+
events: [
|
|
51
|
+
{ kind: "period", id: "p-P1", recordId: "P1", startDate: 1716383000, endDate: 1716800000, cycleLength: 28, periodLength: 5 },
|
|
52
|
+
{ kind: "record", id: "r-R1", recordId: "R1", recordType: "mood", date: 1716383000, value: "开心" },
|
|
53
|
+
],
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("snapshot sync + normalize → period + record events", async () => {
|
|
57
|
+
const p = writeTmp(SNAP);
|
|
58
|
+
try {
|
|
59
|
+
const a = new my.MeiyouAdapter();
|
|
60
|
+
expect((await a.authenticate({ inputPath: p })).mode).toBe("snapshot-file");
|
|
61
|
+
const items = await collect(a.sync({ inputPath: p }));
|
|
62
|
+
expect(items).toHaveLength(2);
|
|
63
|
+
const period = a.normalize(items[0]);
|
|
64
|
+
expect(period.events[0].subtype).toBe("other");
|
|
65
|
+
expect(period.events[0].content.title).toBe("经期记录");
|
|
66
|
+
expect(period.events[0].extra.cycleLength).toBe(28);
|
|
67
|
+
expect(items[0].originalId).toBe("meiyou:period:P1");
|
|
68
|
+
const record = a.normalize(items[1]);
|
|
69
|
+
expect(record.events[0].extra.recordType).toBe("mood");
|
|
70
|
+
expect(record.events[0].extra.value).toBe("开心");
|
|
71
|
+
expect(items[1].originalId).toBe("meiyou:record:R1");
|
|
72
|
+
} finally {
|
|
73
|
+
fs.unlinkSync(p);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("dataDisclosure: high sensitivity + legalGate (reproductive health)", () => {
|
|
78
|
+
const a = new my.MeiyouAdapter();
|
|
79
|
+
expect(a.dataDisclosure.sensitivity).toBe("high");
|
|
80
|
+
expect(a.dataDisclosure.legalGate).toBe(true);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("include filter can exclude a kind", async () => {
|
|
84
|
+
const p = writeTmp(SNAP);
|
|
85
|
+
try {
|
|
86
|
+
const a = new my.MeiyouAdapter();
|
|
87
|
+
const items = await collect(a.sync({ inputPath: p, include: { record: false } }));
|
|
88
|
+
expect(items).toHaveLength(1);
|
|
89
|
+
expect(items[0].kind).toBe("period");
|
|
90
|
+
} finally {
|
|
91
|
+
fs.unlinkSync(p);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("cookie-api: best-effort fetch both kinds + unverified flag + sign seam", async () => {
|
|
96
|
+
const calls = [];
|
|
97
|
+
let signed = 0;
|
|
98
|
+
const a = new my.MeiyouAdapter({
|
|
99
|
+
account: { cookies: COOKIES, userId: "u1" },
|
|
100
|
+
signProvider: async () => {
|
|
101
|
+
signed += 1;
|
|
102
|
+
return "sig";
|
|
103
|
+
},
|
|
104
|
+
fetchFn: async ({ url, query }) => {
|
|
105
|
+
calls.push({ url, page: query.page });
|
|
106
|
+
if (query.page > 1) return { list: [] };
|
|
107
|
+
if (url.includes("/period")) return { list: [{ recordId: "P9", startDate: 1716383000, cycleLength: 30 }] };
|
|
108
|
+
return { list: [{ recordId: "R9", recordType: "weight", date: 1716383000, value: 55 }] };
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
const auth = await a.authenticate();
|
|
112
|
+
expect(auth).toMatchObject({ ok: true, mode: "cookie", unverified: true });
|
|
113
|
+
const items = await collect(a.sync({}));
|
|
114
|
+
expect(items).toHaveLength(2);
|
|
115
|
+
expect(items.map((i) => i.originalId).sort()).toEqual(["meiyou:period:P9", "meiyou:record:R9"]);
|
|
116
|
+
expect(signed).toBeGreaterThan(0);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("default fetch throws; no input throws", async () => {
|
|
120
|
+
const a = new my.MeiyouAdapter({ account: { cookies: COOKIES } });
|
|
121
|
+
await expect(collect(a.sync({}))).rejects.toThrow(/no fetchFn configured/);
|
|
122
|
+
const b = new my.MeiyouAdapter();
|
|
123
|
+
await expect(collect(b.sync({}))).rejects.toThrow(/needs opts.inputPath/);
|
|
124
|
+
});
|
|
125
|
+
});
|
|
@@ -0,0 +1,187 @@
|
|
|
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
|
+
KugouMusicAdapter,
|
|
11
|
+
extractList,
|
|
12
|
+
songItemToRecord,
|
|
13
|
+
playlistItemToRecord,
|
|
14
|
+
NAME,
|
|
15
|
+
VERSION,
|
|
16
|
+
SNAPSHOT_SCHEMA_VERSION,
|
|
17
|
+
} = require("../../lib/adapters/music-kugou");
|
|
18
|
+
|
|
19
|
+
function writeTmp(content) {
|
|
20
|
+
const p = path.join(os.tmpdir(), `cc-kg-${crypto.randomUUID()}.json`);
|
|
21
|
+
fs.writeFileSync(p, content, "utf-8");
|
|
22
|
+
return p;
|
|
23
|
+
}
|
|
24
|
+
async function collect(gen) {
|
|
25
|
+
const out = [];
|
|
26
|
+
for await (const x of gen) out.push(x);
|
|
27
|
+
return out;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const COOKIES = "KuGoo=abc; kg_mid=xyz";
|
|
31
|
+
|
|
32
|
+
const SNAP = JSON.stringify({
|
|
33
|
+
schemaVersion: 1,
|
|
34
|
+
snapshottedAt: 1716383000000,
|
|
35
|
+
account: { userId: "u1" },
|
|
36
|
+
events: [
|
|
37
|
+
{ kind: "play", id: "p1", songId: "S1", song: "晴天", artist: "周杰伦", album: "叶惠美", playCount: 12, capturedAt: 1716300000000 },
|
|
38
|
+
{ kind: "favorite", id: "f1", songId: "S2", song: "稻香", artist: "周杰伦" },
|
|
39
|
+
{ kind: "playlist", id: "pl1", playlistId: "L1", name: "华语经典", trackCount: 88, creator: "我" },
|
|
40
|
+
],
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
describe("constants + item mappers", () => {
|
|
44
|
+
it("name/version", () => {
|
|
45
|
+
expect(NAME).toBe("music-kugou");
|
|
46
|
+
expect(VERSION).toBe("0.1.0");
|
|
47
|
+
expect(SNAPSHOT_SCHEMA_VERSION).toBe(1);
|
|
48
|
+
});
|
|
49
|
+
it("songItemToRecord: discrete fields + filename split fallback", () => {
|
|
50
|
+
const r1 = songItemToRecord({ hash: "H1", songname: "夜曲", singername: "周杰伦", album_name: "十一月的萧邦", addtime: 1716300000 });
|
|
51
|
+
expect(r1).toMatchObject({ id: "H1", song: "夜曲", artist: "周杰伦", album: "十一月的萧邦" });
|
|
52
|
+
expect(r1.occurredAt).toBe(1716300000000);
|
|
53
|
+
const r2 = songItemToRecord({ mixsongid: 9, filename: "林俊杰 - 江南" });
|
|
54
|
+
expect(r2).toMatchObject({ song: "江南", artist: "林俊杰" });
|
|
55
|
+
expect(songItemToRecord({ songname: "noid" })).toBe(null);
|
|
56
|
+
});
|
|
57
|
+
it("playlistItemToRecord", () => {
|
|
58
|
+
const r = playlistItemToRecord({ listid: "L9", name: "睡前", count: 30, nickname: "我" });
|
|
59
|
+
expect(r).toMatchObject({ id: "L9", playlistId: "L9", name: "睡前", trackCount: 30, creator: "我" });
|
|
60
|
+
});
|
|
61
|
+
it("extractList tolerant", () => {
|
|
62
|
+
expect(extractList({ list: [{ hash: 1 }] })).toHaveLength(1);
|
|
63
|
+
expect(extractList({ data: { info: [{ hash: 1 }] } })).toHaveLength(1);
|
|
64
|
+
expect(extractList({})).toEqual([]);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe("KugouMusicAdapter snapshot mode", () => {
|
|
69
|
+
it("authenticate validates inputPath", async () => {
|
|
70
|
+
const p = writeTmp(SNAP);
|
|
71
|
+
try {
|
|
72
|
+
const a = new KugouMusicAdapter();
|
|
73
|
+
expect((await a.authenticate({ inputPath: p })).mode).toBe("snapshot-file");
|
|
74
|
+
expect((await a.authenticate({ inputPath: path.join(os.tmpdir(), "no-kg.json") })).reason).toBe("INPUT_PATH_UNREADABLE");
|
|
75
|
+
} finally {
|
|
76
|
+
fs.unlinkSync(p);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("sync 3 kinds + normalize play→media+item / favorite→like / playlist→topic", async () => {
|
|
81
|
+
const p = writeTmp(SNAP);
|
|
82
|
+
try {
|
|
83
|
+
const a = new KugouMusicAdapter();
|
|
84
|
+
const items = await collect(a.sync({ inputPath: p }));
|
|
85
|
+
expect(items.map((x) => x.kind)).toEqual(["play", "favorite", "playlist"]);
|
|
86
|
+
|
|
87
|
+
const play = a.normalize(items[0]);
|
|
88
|
+
expect(play.events[0].subtype).toBe("media");
|
|
89
|
+
expect(play.events[0].content.title).toBe("听了: 晴天 - 周杰伦");
|
|
90
|
+
expect(play.items[0].subtype).toBe("media");
|
|
91
|
+
expect(play.items[0].extra.platform).toBe("kugou");
|
|
92
|
+
expect(play.events[0].extra.itemRef).toBe(play.items[0].id);
|
|
93
|
+
|
|
94
|
+
const fav = a.normalize(items[1]);
|
|
95
|
+
expect(fav.events[0].subtype).toBe("like");
|
|
96
|
+
expect(fav.events[0].content.title).toBe("收藏: 稻香 - 周杰伦");
|
|
97
|
+
|
|
98
|
+
const pl = a.normalize(items[2]);
|
|
99
|
+
expect(pl.topics[0].name).toBe("华语经典");
|
|
100
|
+
expect(pl.topics[0].extra.trackCount).toBe(88);
|
|
101
|
+
} finally {
|
|
102
|
+
fs.unlinkSync(p);
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("include + limit + schema mismatch + unknown kind", async () => {
|
|
107
|
+
const p = writeTmp(SNAP);
|
|
108
|
+
try {
|
|
109
|
+
const a = new KugouMusicAdapter();
|
|
110
|
+
expect((await collect(a.sync({ inputPath: p, include: { play: false, favorite: false } }))).map((x) => x.kind)).toEqual(["playlist"]);
|
|
111
|
+
expect(await collect(a.sync({ inputPath: p, limit: 1 }))).toHaveLength(1);
|
|
112
|
+
expect(() => a.normalize({ kind: "bogus", payload: {} })).toThrow(/unknown kind/);
|
|
113
|
+
} finally {
|
|
114
|
+
fs.unlinkSync(p);
|
|
115
|
+
}
|
|
116
|
+
const bad = writeTmp(JSON.stringify({ schemaVersion: 9, events: [] }));
|
|
117
|
+
try {
|
|
118
|
+
const a = new KugouMusicAdapter();
|
|
119
|
+
await expect(collect(a.sync({ inputPath: bad }))).rejects.toThrow(/schemaVersion mismatch/);
|
|
120
|
+
} finally {
|
|
121
|
+
fs.unlinkSync(bad);
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
describe("KugouMusicAdapter cookie-api mode", () => {
|
|
127
|
+
it("authenticate cookie mode (userId optional)", async () => {
|
|
128
|
+
const a = new KugouMusicAdapter({ account: { cookies: COOKIES } });
|
|
129
|
+
expect(await a.authenticate()).toEqual({ ok: true, account: null, mode: "cookie" });
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("sync fetches plays/favorites/playlists, normalizes", async () => {
|
|
133
|
+
const byUrl = (u) => (u.includes("listen") ? "play" : u.includes("favorite") ? "favorite" : "playlist");
|
|
134
|
+
const data = {
|
|
135
|
+
play: [{ hash: "H1", songname: "七里香", singername: "周杰伦", addtime: 1716300000 }],
|
|
136
|
+
favorite: [{ hash: "H2", filename: "陈奕迅 - 浮夸" }],
|
|
137
|
+
playlist: [{ listid: "L1", name: "粤语", count: 50 }],
|
|
138
|
+
};
|
|
139
|
+
const calls = [];
|
|
140
|
+
const a = new KugouMusicAdapter({
|
|
141
|
+
account: { cookies: COOKIES, userId: "u1" },
|
|
142
|
+
fetchFn: async ({ url, cookies, query, sign }) => {
|
|
143
|
+
const k = byUrl(url);
|
|
144
|
+
calls.push({ k, cookies, page: query.page, sign });
|
|
145
|
+
return { data: { list: query.page === 1 ? data[k] : [] } };
|
|
146
|
+
},
|
|
147
|
+
});
|
|
148
|
+
const items = await collect(a.sync({}));
|
|
149
|
+
expect(items.map((x) => x.kind).sort()).toEqual(["favorite", "play", "playlist"]);
|
|
150
|
+
expect(calls.every((c) => c.cookies === COOKIES && c.sign === null)).toBe(true);
|
|
151
|
+
const play = a.normalize(items.find((x) => x.kind === "play"));
|
|
152
|
+
expect(play.events[0].content.title).toBe("听了: 七里香 - 周杰伦");
|
|
153
|
+
const fav = a.normalize(items.find((x) => x.kind === "favorite"));
|
|
154
|
+
expect(fav.events[0].content.title).toBe("收藏: 浮夸 - 陈奕迅"); // filename split
|
|
155
|
+
const pl = a.normalize(items.find((x) => x.kind === "playlist"));
|
|
156
|
+
expect(pl.topics[0].name).toBe("粤语");
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("invokes signProvider", async () => {
|
|
160
|
+
const signCalls = [];
|
|
161
|
+
const a = new KugouMusicAdapter({
|
|
162
|
+
account: { cookies: COOKIES },
|
|
163
|
+
fetchFn: async ({ query }) => ({ list: query.page === 1 ? [{ hash: "H1", songname: "x" }] : [] }),
|
|
164
|
+
signProvider: async (ctx) => { signCalls.push(ctx); return "sig"; },
|
|
165
|
+
});
|
|
166
|
+
const items = await collect(a.sync({ include: { favorite: false, playlist: false } }));
|
|
167
|
+
expect(items.length).toBeGreaterThan(0);
|
|
168
|
+
expect(signCalls[0].cookies).toBe(COOKIES);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("limit + empty/login + default fetch + no input", async () => {
|
|
172
|
+
const a1 = new KugouMusicAdapter({
|
|
173
|
+
account: { cookies: COOKIES },
|
|
174
|
+
fetchFn: async ({ query }) => ({ list: query.page === 1 ? [{ hash: "H1", songname: "a" }, { hash: "H2", songname: "b" }] : [] }),
|
|
175
|
+
});
|
|
176
|
+
expect(await collect(a1.sync({ limit: 1 }))).toHaveLength(1);
|
|
177
|
+
|
|
178
|
+
const a2 = new KugouMusicAdapter({ account: { cookies: COOKIES }, fetchFn: async () => "<html>login</html>" });
|
|
179
|
+
expect(await collect(a2.sync({}))).toEqual([]);
|
|
180
|
+
|
|
181
|
+
const a3 = new KugouMusicAdapter({ account: { cookies: COOKIES } });
|
|
182
|
+
await expect(collect(a3.sync({}))).rejects.toThrow(/no fetchFn configured/);
|
|
183
|
+
|
|
184
|
+
const a4 = new KugouMusicAdapter();
|
|
185
|
+
await expect(collect(a4.sync({}))).rejects.toThrow(/needs opts.inputPath/);
|
|
186
|
+
});
|
|
187
|
+
});
|