@chainlesschain/personal-data-hub 0.4.18 → 0.4.24

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.
Files changed (53) hide show
  1. package/__tests__/adapters/bank-family.test.js +125 -0
  2. package/__tests__/adapters/biz-tianyancha.test.js +159 -0
  3. package/__tests__/adapters/car-mercedesme.test.js +74 -0
  4. package/__tests__/adapters/doc-camscanner.test.js +147 -0
  5. package/__tests__/adapters/finance-dcep.test.js +74 -0
  6. package/__tests__/adapters/fitness-joyrun.test.js +82 -0
  7. package/__tests__/adapters/gov-12123.test.js +103 -0
  8. package/__tests__/adapters/gov-ixiamen.test.js +150 -0
  9. package/__tests__/adapters/gov-tax.test.js +135 -0
  10. package/__tests__/adapters/health-meiyou.test.js +125 -0
  11. package/__tests__/adapters/music-qq.test.js +112 -0
  12. package/__tests__/adapters/reading-family.test.js +108 -0
  13. package/__tests__/adapters/social-dongchedi.test.js +165 -0
  14. package/__tests__/adapters/travel-didi-consumer.test.js +66 -0
  15. package/__tests__/adapters/video-xigua.test.js +106 -0
  16. package/__tests__/adapters/wework-pc.test.js +124 -0
  17. package/__tests__/audio-ximalaya-snapshot.test.js +279 -0
  18. package/__tests__/fitness-keep-snapshot.test.js +224 -0
  19. package/__tests__/shopping-eleme-snapshot.test.js +454 -0
  20. package/__tests__/shopping-vipshop-snapshot.test.js +425 -0
  21. package/__tests__/shopping-xianyu-snapshot.test.js +451 -0
  22. package/__tests__/social-douban-snapshot.test.js +351 -0
  23. package/lib/adapter-guide.js +31 -3
  24. package/lib/adapters/_bank-base.js +405 -0
  25. package/lib/adapters/_reading-base.js +315 -0
  26. package/lib/adapters/audio-ximalaya/index.js +414 -0
  27. package/lib/adapters/bank-bankcomm/index.js +27 -0
  28. package/lib/adapters/bank-boc/index.js +26 -0
  29. package/lib/adapters/bank-cmbc/index.js +26 -0
  30. package/lib/adapters/bank-icbc/index.js +27 -0
  31. package/lib/adapters/biz-tianyancha/index.js +348 -0
  32. package/lib/adapters/car-mercedesme/index.js +225 -0
  33. package/lib/adapters/doc-camscanner/index.js +102 -0
  34. package/lib/adapters/finance-dcep/index.js +302 -0
  35. package/lib/adapters/fitness-joyrun/index.js +295 -0
  36. package/lib/adapters/fitness-keep/index.js +343 -0
  37. package/lib/adapters/gov-12123/index.js +391 -0
  38. package/lib/adapters/gov-ixiamen/index.js +380 -0
  39. package/lib/adapters/gov-tax/index.js +451 -0
  40. package/lib/adapters/health-meiyou/index.js +393 -0
  41. package/lib/adapters/music-qq/index.js +372 -0
  42. package/lib/adapters/reading-fanqie/index.js +61 -0
  43. package/lib/adapters/reading-qimao/index.js +61 -0
  44. package/lib/adapters/shopping-eleme/index.js +441 -0
  45. package/lib/adapters/shopping-vipshop/index.js +429 -0
  46. package/lib/adapters/shopping-xianyu/index.js +454 -0
  47. package/lib/adapters/social-dongchedi/index.js +360 -0
  48. package/lib/adapters/social-douban/index.js +564 -0
  49. package/lib/adapters/travel-didi-consumer/index.js +148 -0
  50. package/lib/adapters/video-xigua/index.js +68 -0
  51. package/lib/adapters/wework-pc/index.js +31 -0
  52. package/lib/index.js +52 -0
  53. package/package.json +1 -1
@@ -0,0 +1,103 @@
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 g = require("../../lib/adapters/gov-12123");
10
+
11
+ function writeTmp(content) {
12
+ const p = path.join(os.tmpdir(), `cc-12123-${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
+ const COOKIES = "GAB_SSO=abc";
22
+
23
+ describe("gov-12123", () => {
24
+ it("name + mappers (chinese-field aliases)", () => {
25
+ expect(g.NAME).toBe("gov-12123");
26
+ const v = g.mapViolation({ wzbh: "V1", wfsj: 1716383000, wfdz: "环岛路", wfxw: "超速50%以下", fkje: "200", wfjfs: "3" });
27
+ expect(v).toMatchObject({ violationId: "V1", location: "环岛路", reason: "超速50%以下" });
28
+ expect(v.fine).toBe(200);
29
+ expect(v.points).toBe(3);
30
+ expect(g.mapViolation({ wfxw: "x" })).toBe(null);
31
+ const l = g.mapLicense({ dabh: "L1", zt: "正常", ljjf: 3, yxqz: 1900000000 });
32
+ expect(l).toMatchObject({ licenseId: "L1", status: "正常", cumulativePoints: 3 });
33
+ expect(g.mapLicense({ zt: "x" })).toBe(null);
34
+ });
35
+ it("extractLicense wraps single object", () => {
36
+ expect(g.extractLicense({ data: { dabh: "L1" } })).toHaveLength(1);
37
+ expect(g.extractLicense({ licenseId: "L2" })).toHaveLength(1);
38
+ expect(g.extractLicense({ list: [{ dabh: "L3" }] })).toHaveLength(1);
39
+ });
40
+
41
+ it("snapshot → violation + license OTHER events", async () => {
42
+ const SNAP = JSON.stringify({
43
+ schemaVersion: 1,
44
+ snapshottedAt: 1716383000000,
45
+ account: { userId: "u1" },
46
+ events: [
47
+ { kind: "violation", id: "v-V1", violationId: "V1", time: 1716383000, location: "环岛路", reason: "超速", fine: 200, points: 3 },
48
+ { kind: "license", id: "l-L1", licenseId: "L1", status: "正常", cumulativePoints: 3, validUntil: 1900000000 },
49
+ ],
50
+ });
51
+ const p = writeTmp(SNAP);
52
+ try {
53
+ const a = new g.Tmri12123Adapter();
54
+ const items = await collect(a.sync({ inputPath: p }));
55
+ expect(items).toHaveLength(2);
56
+ const v = a.normalize(items[0]);
57
+ expect(v.events[0].subtype).toBe("other");
58
+ expect(v.events[0].content.title).toContain("交通违章");
59
+ expect(v.events[0].extra.fine).toBe(200);
60
+ expect(v.events[0].extra.points).toBe(3);
61
+ expect(items[0].originalId).toBe("12123:violation:V1");
62
+ const l = a.normalize(items[1]);
63
+ expect(l.events[0].content.title).toContain("驾驶证状态");
64
+ expect(l.events[0].extra.cumulativePoints).toBe(3);
65
+ expect(items[1].originalId).toBe("12123:license:L1");
66
+ } finally {
67
+ fs.unlinkSync(p);
68
+ }
69
+ });
70
+
71
+ it("cookie-api: paginated violations + single license + unverified", async () => {
72
+ const a = new g.Tmri12123Adapter({
73
+ account: { cookies: COOKIES, userId: "u1" },
74
+ fetchFn: async ({ url, query }) => {
75
+ if (url.includes("/violation")) return query.page > 1 ? { list: [] } : { list: [{ wzbh: "V9", wfsj: 1716383000, wfxw: "闯红灯", fkje: 200, wfjfs: 6 }] };
76
+ return { data: { dabh: "L9", zt: "正常", ljjf: 6 } };
77
+ },
78
+ });
79
+ expect(await a.authenticate()).toMatchObject({ ok: true, mode: "cookie", unverified: true });
80
+ const items = await collect(a.sync({}));
81
+ expect(items).toHaveLength(2);
82
+ expect(items.map((i) => i.originalId).sort()).toEqual(["12123:license:L9", "12123:violation:V9"]);
83
+ });
84
+
85
+ it("province base host (verified .122.gov.cn/app); override + default", () => {
86
+ // default province bj
87
+ const a = new g.Tmri12123Adapter({ province: "fj" });
88
+ expect(a.province).toBe("fj");
89
+ expect(a._urls.violation).toBe("https://fj.122.gov.cn/app/violation/list");
90
+ expect(a._urls.license).toBe("https://fj.122.gov.cn/app/license/info");
91
+ // bad province → default bj
92
+ expect(new g.Tmri12123Adapter({ province: "XX" })._urls.violation).toBe("https://bj.122.gov.cn/app/violation/list");
93
+ // explicit url override still wins
94
+ expect(new g.Tmri12123Adapter({ violationUrl: "https://x/y" })._urls.violation).toBe("https://x/y");
95
+ });
96
+
97
+ it("high sensitivity + legalGate; default fetch / no input throw", async () => {
98
+ expect(new g.Tmri12123Adapter().dataDisclosure.sensitivity).toBe("high");
99
+ expect(new g.Tmri12123Adapter().dataDisclosure.legalGate).toBe(true);
100
+ await expect(collect(new g.Tmri12123Adapter({ account: { cookies: COOKIES } }).sync({}))).rejects.toThrow(/no fetchFn/);
101
+ await expect(collect(new g.Tmri12123Adapter().sync({}))).rejects.toThrow(/needs opts.inputPath/);
102
+ });
103
+ });
@@ -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,112 @@
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 qm = require("../../lib/adapters/music-qq");
10
+
11
+ function writeTmp(content) {
12
+ const p = path.join(os.tmpdir(), `cc-qqm-${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
+ const COOKIES = "qqmusic_key=abc; uin=123";
22
+
23
+ describe("music-qq mappers", () => {
24
+ it("name/version", () => {
25
+ expect(qm.NAME).toBe("music-qq");
26
+ expect(qm.VERSION).toBe("0.1.0");
27
+ });
28
+ it("flattenSinger handles array / string / object", () => {
29
+ expect(qm.flattenSinger([{ name: "周杰伦" }, { name: "费玉清" }])).toBe("周杰伦/费玉清");
30
+ expect(qm.flattenSinger("林俊杰")).toBe("林俊杰");
31
+ expect(qm.flattenSinger({ name: "邓紫棋" })).toBe("邓紫棋");
32
+ expect(qm.flattenSinger(null)).toBe("");
33
+ });
34
+ it("songItemToRecord (QQ fields: songmid, singer array, album.name)", () => {
35
+ const r = qm.songItemToRecord({ songmid: "S1", songname: "晴天", singer: [{ name: "周杰伦" }], album: { name: "叶惠美" }, time: 1716383000 });
36
+ expect(r).toMatchObject({ songId: "S1", song: "晴天", artist: "周杰伦", album: "叶惠美" });
37
+ expect(r.occurredAt).toBe(1716383000000);
38
+ expect(qm.songItemToRecord({ songname: "x" })).toBe(null);
39
+ });
40
+ it("playlistItemToRecord (dissid/dissname/songnum)", () => {
41
+ const r = qm.playlistItemToRecord({ dissid: "P1", dissname: "我喜欢", songnum: 42, creator: { name: "我" } });
42
+ expect(r).toMatchObject({ playlistId: "P1", name: "我喜欢", trackCount: 42, creator: "我" });
43
+ expect(qm.playlistItemToRecord({ dissname: "x" })).toBe(null);
44
+ });
45
+ it("extractList tolerant (list / data.songlist)", () => {
46
+ expect(qm.extractList({ list: [{ songmid: 1 }] })).toHaveLength(1);
47
+ expect(qm.extractList({ data: { songlist: [{ songmid: 1 }] } })).toHaveLength(1);
48
+ expect(qm.extractList({})).toEqual([]);
49
+ });
50
+ });
51
+
52
+ describe("QQMusicAdapter (snapshot + cookie-api)", () => {
53
+ const SNAP = JSON.stringify({
54
+ schemaVersion: 1,
55
+ snapshottedAt: 1716383000000,
56
+ account: { userId: "u1" },
57
+ events: [
58
+ { kind: "play", id: "play-S1", songId: "S1", song: "晴天", artist: "周杰伦", album: "叶惠美" },
59
+ { kind: "favorite", id: "fav-S2", songId: "S2", song: "稻香", artist: "周杰伦" },
60
+ { kind: "playlist", id: "pl-P1", playlistId: "P1", name: "我喜欢", trackCount: 42 },
61
+ ],
62
+ });
63
+
64
+ it("snapshot → play(MEDIA)/favorite(LIKE) events + playlist topic", async () => {
65
+ const p = writeTmp(SNAP);
66
+ try {
67
+ const a = new qm.QQMusicAdapter();
68
+ expect((await a.authenticate({ inputPath: p })).mode).toBe("snapshot-file");
69
+ const items = await collect(a.sync({ inputPath: p }));
70
+ expect(items).toHaveLength(3);
71
+ const play = a.normalize(items[0]);
72
+ expect(play.events[0].subtype).toBe("media");
73
+ expect(play.events[0].content.title).toContain("听了: 晴天");
74
+ expect(play.items[0].subtype).toBe("media");
75
+ expect(play.items[0].extra.platform).toBe("qqmusic");
76
+ expect(items[0].originalId).toBe("qqmusic:play:play-S1"); // snapshot uses ev.id (mirrors kugou)
77
+ const fav = a.normalize(items[1]);
78
+ expect(fav.events[0].subtype).toBe("like");
79
+ const pl = a.normalize(items[2]);
80
+ expect(pl.topics[0].name).toBe("我喜欢");
81
+ expect(pl.topics[0].id).toBe("topic-qqmusic-playlist-P1");
82
+ } finally {
83
+ fs.unlinkSync(p);
84
+ }
85
+ });
86
+
87
+ it("cookie-api: fetch 3 kinds + sign seam", async () => {
88
+ let signed = 0;
89
+ const a = new qm.QQMusicAdapter({
90
+ account: { cookies: COOKIES, userId: "u1" },
91
+ signProvider: async () => { signed += 1; return "sig"; },
92
+ fetchFn: async ({ url, query }) => {
93
+ if (query.page > 1) return { list: [] };
94
+ if (url.includes("/listen")) return { list: [{ songmid: "A", songname: "歌1", singer: [{ name: "歌手" }], time: 1716383000 }] };
95
+ if (url.includes("/favorite")) return { data: { songlist: [{ songmid: "B", songname: "歌2", singer: "歌手2" }] } };
96
+ return { data: { list: [{ dissid: "C", dissname: "歌单", songnum: 5 }] } };
97
+ },
98
+ });
99
+ expect(await a.authenticate()).toEqual({ ok: true, account: "u1", mode: "cookie" });
100
+ const items = await collect(a.sync({}));
101
+ expect(items).toHaveLength(3);
102
+ expect(items.map((i) => i.originalId).sort()).toEqual(["qqmusic:favorite:B", "qqmusic:play:A", "qqmusic:playlist:C"]);
103
+ expect(signed).toBeGreaterThan(0);
104
+ });
105
+
106
+ it("low sensitivity (consumer music); default fetch / no input throw", async () => {
107
+ expect(new qm.QQMusicAdapter().dataDisclosure.sensitivity).toBe("low");
108
+ expect(new qm.QQMusicAdapter().dataDisclosure.legalGate).toBe(false);
109
+ await expect(collect(new qm.QQMusicAdapter({ account: { cookies: COOKIES } }).sync({}))).rejects.toThrow(/no fetchFn/);
110
+ await expect(collect(new qm.QQMusicAdapter().sync({}))).rejects.toThrow(/needs opts.inputPath/);
111
+ });
112
+ });