@chainlesschain/personal-data-hub 0.4.28 → 0.4.30
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/README.md +13 -5
- package/lib/adapters/social-douyin-adb/usage-profile-reader.js +253 -0
- package/lib/adapters/social-douyin-adb/watch-history-reader.js +104 -31
- package/lib/adapters/social-toutiao-adb/article-reader.js +202 -0
- package/lib/analysis-skills/overview.js +24 -4
- package/lib/analysis-skills/spending.js +63 -2
- package/lib/analysis-skills/timeline.js +11 -6
- package/lib/prompt-builder.js +15 -1
- package/lib/query-parser.js +38 -8
- package/package.json +4 -1
- package/__tests__/adapter-guide.test.js +0 -47
- package/__tests__/adapter-spec.test.js +0 -78
- package/__tests__/adapters/ai-chat-cookie-capture-spec.test.js +0 -211
- package/__tests__/adapters/ai-chat-health-checker.test.js +0 -262
- package/__tests__/adapters/ai-chat-history.test.js +0 -396
- package/__tests__/adapters/ai-chat-http-client.test.js +0 -242
- package/__tests__/adapters/ai-chat-vendors.test.js +0 -874
- package/__tests__/adapters/alipay-bill-adapter.test.js +0 -538
- package/__tests__/adapters/apple-health.test.js +0 -95
- package/__tests__/adapters/bank-family.test.js +0 -125
- package/__tests__/adapters/biz-tianyancha.test.js +0 -159
- package/__tests__/adapters/browser-history-chrome.test.js +0 -377
- package/__tests__/adapters/browser-history-edge.test.js +0 -159
- package/__tests__/adapters/car-mercedesme.test.js +0 -74
- package/__tests__/adapters/doc-baidu-netdisk.test.js +0 -102
- package/__tests__/adapters/doc-camscanner.test.js +0 -147
- package/__tests__/adapters/doc-platforms.test.js +0 -177
- package/__tests__/adapters/edu-huawei-learning-live.test.js +0 -198
- package/__tests__/adapters/edu-zuoyebang-live.test.js +0 -226
- package/__tests__/adapters/email-adapter-snapshot.test.js +0 -237
- package/__tests__/adapters/email-adapter.test.js +0 -742
- package/__tests__/adapters/email-classifier.test.js +0 -347
- package/__tests__/adapters/email-imap-session.test.js +0 -334
- package/__tests__/adapters/email-parser.test.js +0 -244
- package/__tests__/adapters/email-pdf-extractor.test.js +0 -529
- package/__tests__/adapters/email-providers.test.js +0 -84
- package/__tests__/adapters/email-retry-progress.test.js +0 -294
- package/__tests__/adapters/email-templates.test.js +0 -822
- package/__tests__/adapters/family-23-collectors-scaffold.test.js +0 -182
- package/__tests__/adapters/finance-alipay-live.test.js +0 -258
- package/__tests__/adapters/finance-dcep.test.js +0 -74
- package/__tests__/adapters/fitness-joyrun.test.js +0 -82
- package/__tests__/adapters/game-genshin-live.test.js +0 -238
- package/__tests__/adapters/game-genshin-scaffold.test.js +0 -108
- package/__tests__/adapters/game-honor-of-kings-live.test.js +0 -230
- package/__tests__/adapters/git-activity.test.js +0 -222
- package/__tests__/adapters/gov-12123.test.js +0 -103
- package/__tests__/adapters/gov-ixiamen.test.js +0 -150
- package/__tests__/adapters/gov-tax.test.js +0 -135
- package/__tests__/adapters/health-meiyou.test.js +0 -125
- package/__tests__/adapters/local-files.test.js +0 -264
- package/__tests__/adapters/local-im-pc.test.js +0 -154
- package/__tests__/adapters/messaging-whatsapp.test.js +0 -289
- package/__tests__/adapters/music-kugou.test.js +0 -187
- package/__tests__/adapters/music-qq.test.js +0 -112
- package/__tests__/adapters/netease-music-live.test.js +0 -244
- package/__tests__/adapters/netease-music.test.js +0 -74
- package/__tests__/adapters/pc-local-discovery.test.js +0 -141
- package/__tests__/adapters/qq-pc-direct-read.test.js +0 -227
- package/__tests__/adapters/reading-family.test.js +0 -108
- package/__tests__/adapters/recruit-boss.test.js +0 -180
- package/__tests__/adapters/shell-history.test.js +0 -180
- package/__tests__/adapters/shopping-base.test.js +0 -179
- package/__tests__/adapters/shopping-dianping.test.js +0 -239
- package/__tests__/adapters/social-bilibili-adb-api-client.test.js +0 -721
- package/__tests__/adapters/social-bilibili-adb-chromium-cookies-reader.test.js +0 -346
- package/__tests__/adapters/social-bilibili-adb-collector.test.js +0 -284
- package/__tests__/adapters/social-bilibili-adb-cookies-extension.test.js +0 -343
- package/__tests__/adapters/social-bilibili-adb-snapshot-builder.test.js +0 -296
- package/__tests__/adapters/social-csdn.test.js +0 -175
- package/__tests__/adapters/social-dongchedi.test.js +0 -165
- package/__tests__/adapters/social-douyin-adb-aweme-detail.test.js +0 -165
- package/__tests__/adapters/social-douyin-adb-collector.test.js +0 -254
- package/__tests__/adapters/social-douyin-adb-db-extension.test.js +0 -114
- package/__tests__/adapters/social-douyin-adb-im-db-parser.test.js +0 -304
- package/__tests__/adapters/social-douyin-adb-snapshot-builder.test.js +0 -216
- package/__tests__/adapters/social-douyin-adb-watch-history.test.js +0 -192
- package/__tests__/adapters/social-kuaishou-adb-api-client.test.js +0 -496
- package/__tests__/adapters/social-kuaishou-adb-collector.test.js +0 -276
- package/__tests__/adapters/social-kuaishou-adb-cookies-extension.test.js +0 -152
- package/__tests__/adapters/social-kuaishou-adb-snapshot-builder.test.js +0 -178
- package/__tests__/adapters/social-toutiao-adb-account-reader.test.js +0 -135
- package/__tests__/adapters/social-toutiao-adb-api-client.test.js +0 -626
- package/__tests__/adapters/social-toutiao-adb-collector.test.js +0 -378
- package/__tests__/adapters/social-toutiao-adb-cookies-extension.test.js +0 -193
- package/__tests__/adapters/social-toutiao-adb-snapshot-builder.test.js +0 -196
- package/__tests__/adapters/social-toutiao-kuaishou-scaffold.test.js +0 -311
- package/__tests__/adapters/social-weibo-adb-api-client.test.js +0 -362
- package/__tests__/adapters/social-weibo-adb-collector.test.js +0 -201
- package/__tests__/adapters/social-weibo-adb-cookies-extension.test.js +0 -167
- package/__tests__/adapters/social-weibo-adb-snapshot-builder.test.js +0 -189
- package/__tests__/adapters/social-xiaohongshu-adb-api-client.test.js +0 -431
- package/__tests__/adapters/social-xiaohongshu-adb-collector.test.js +0 -207
- package/__tests__/adapters/social-xiaohongshu-adb-cookies-extension.test.js +0 -0
- package/__tests__/adapters/social-xiaohongshu-adb-sign-provider-injection.test.js +0 -351
- package/__tests__/adapters/social-xiaohongshu-adb-sign.test.js +0 -130
- package/__tests__/adapters/social-xiaohongshu-adb-snapshot-builder.test.js +0 -200
- package/__tests__/adapters/social-zhihu.test.js +0 -246
- package/__tests__/adapters/system-data-adapter.test.js +0 -443
- package/__tests__/adapters/system-data-android-ingest.test.js +0 -144
- package/__tests__/adapters/system-data-android.test.js +0 -519
- package/__tests__/adapters/system-data-disclosure.test.js +0 -153
- package/__tests__/adapters/travel-12306.test.js +0 -512
- package/__tests__/adapters/travel-amap.test.js +0 -219
- package/__tests__/adapters/travel-baidu-map.test.js +0 -305
- package/__tests__/adapters/travel-base.test.js +0 -205
- package/__tests__/adapters/travel-ctrip.test.js +0 -377
- package/__tests__/adapters/travel-didi-consumer.test.js +0 -66
- package/__tests__/adapters/travel-didi.test.js +0 -204
- package/__tests__/adapters/travel-tencent-map.test.js +0 -207
- package/__tests__/adapters/travel-tongcheng.test.js +0 -289
- package/__tests__/adapters/video-platforms.test.js +0 -152
- package/__tests__/adapters/video-xigua.test.js +0 -106
- package/__tests__/adapters/vscode.test.js +0 -299
- package/__tests__/adapters/wechat-bootstrap.test.js +0 -240
- package/__tests__/adapters/wechat-env-probe.test.js +0 -162
- package/__tests__/adapters/wechat-frida-agent.test.js +0 -322
- package/__tests__/adapters/wechat-frida-integration.test.js +0 -149
- package/__tests__/adapters/wechat-frida-key-provider.test.js +0 -188
- package/__tests__/adapters/wechat-md5-key-provider.test.js +0 -101
- package/__tests__/adapters/wechat-pc-direct-read.test.js +0 -365
- package/__tests__/adapters/wechat-pc-group-topic.test.js +0 -63
- package/__tests__/adapters/wechat-pc-v4-sidecar.test.js +0 -72
- package/__tests__/adapters/weread.test.js +0 -123
- package/__tests__/adapters/wework-pc.test.js +0 -124
- package/__tests__/adapters/win-recent.test.js +0 -192
- package/__tests__/analysis-skills.test.js +0 -679
- package/__tests__/analysis.test.js +0 -1845
- package/__tests__/audio-ximalaya-snapshot.test.js +0 -279
- package/__tests__/batch.test.js +0 -133
- package/__tests__/bridges-cc-kg.test.js +0 -231
- package/__tests__/bridges-cc-llm.test.js +0 -191
- package/__tests__/bridges-cc-rag.test.js +0 -162
- package/__tests__/categories.test.js +0 -92
- package/__tests__/e2e/ai-chat-cross-source-journey.test.js +0 -213
- package/__tests__/e2e/full-user-journey.test.js +0 -188
- package/__tests__/e2e/local-data-adapters-cli.e2e.test.js +0 -146
- package/__tests__/entity-resolver-ingest-hook.test.js +0 -177
- package/__tests__/entity-resolver-stages.test.js +0 -411
- package/__tests__/entity-resolver-vault.test.js +0 -249
- package/__tests__/entity-resolver.test.js +0 -526
- package/__tests__/fitness-keep-snapshot.test.js +0 -224
- package/__tests__/fixtures/entity-resolver-200-mock.json +0 -96
- package/__tests__/ids.test.js +0 -45
- package/__tests__/integration/ai-chat-history-registry.test.js +0 -228
- package/__tests__/integration/aichat-wizard-end-to-end.test.js +0 -282
- package/__tests__/integration/cross-adapter-pipelines.test.js +0 -396
- package/__tests__/integration/local-data-adapters-pipeline.test.js +0 -373
- package/__tests__/integration/social-bilibili-pipeline.test.js +0 -261
- package/__tests__/integration/wechat-bootstrap-end-to-end.test.js +0 -390
- package/__tests__/key-providers.test.js +0 -126
- package/__tests__/kg-derive.test.js +0 -219
- package/__tests__/llm-client.test.js +0 -122
- package/__tests__/longtail-adapters.test.js +0 -281
- package/__tests__/messaging-qq-snapshot.test.js +0 -294
- package/__tests__/mobile-extractor-encrypted.test.js +0 -460
- package/__tests__/mobile-extractor.test.js +0 -288
- package/__tests__/mock-adapter.test.js +0 -93
- package/__tests__/prompt-builder.test.js +0 -249
- package/__tests__/query-parser.test.js +0 -302
- package/__tests__/rag-derive.test.js +0 -169
- package/__tests__/registry-readiness.test.js +0 -292
- package/__tests__/registry.test.js +0 -420
- package/__tests__/salvage-ingest.test.js +0 -97
- package/__tests__/schemas.test.js +0 -331
- package/__tests__/shopping-adapters.test.js +0 -392
- package/__tests__/shopping-eleme-snapshot.test.js +0 -454
- package/__tests__/shopping-pinduoduo-snapshot.test.js +0 -484
- package/__tests__/shopping-snapshot.test.js +0 -438
- package/__tests__/shopping-vipshop-snapshot.test.js +0 -425
- package/__tests__/shopping-xianyu-snapshot.test.js +0 -451
- package/__tests__/sidecar-contacts-cross-validate.test.js +0 -186
- package/__tests__/sidecar-supervisor.test.js +0 -128
- package/__tests__/sign-providers.test.js +0 -62
- package/__tests__/social-adapters.test.js +0 -280
- package/__tests__/social-bilibili-snapshot.test.js +0 -278
- package/__tests__/social-douban-snapshot.test.js +0 -351
- package/__tests__/social-douyin-im-direct-read.test.js +0 -377
- package/__tests__/social-douyin-salvage-collector.test.js +0 -98
- package/__tests__/social-douyin-salvage-mapper.test.js +0 -90
- package/__tests__/social-douyin-snapshot.test.js +0 -256
- package/__tests__/social-kuaishou-snapshot.test.js +0 -362
- package/__tests__/social-toutiao-snapshot.test.js +0 -366
- package/__tests__/social-weibo-snapshot.test.js +0 -234
- package/__tests__/social-weibo-sqlite-device.test.js +0 -174
- package/__tests__/social-xiaohongshu-snapshot.test.js +0 -232
- package/__tests__/sqlite-leaf-salvage.test.js +0 -97
- package/__tests__/travel-adapters.test.js +0 -483
- package/__tests__/travel-maps-snapshot.test.js +0 -426
- package/__tests__/vault-driver-error.test.js +0 -74
- package/__tests__/vault-search-helpers.test.js +0 -104
- package/__tests__/vault-search.test.js +0 -423
- package/__tests__/vault.test.js +0 -767
- package/__tests__/wechat-adapter.test.js +0 -594
- package/__tests__/whatsapp-adapter.test.js +0 -138
- package/scripts/_make-fixture-all.js +0 -126
- package/scripts/_make-fixture-contacts.js +0 -84
- package/scripts/evaluate-entity-resolver.js +0 -213
- package/scripts/run-native-tests-sandbox.sh +0 -55
- package/scripts/smoke-phase-5-5.js +0 -196
- package/scripts/smoke-phase-5-7.js +0 -181
- package/scripts/smoke-system-data-contacts.js +0 -309
- package/scripts/smoke-system-data.js +0 -312
- package/vitest.config.js +0 -88
|
@@ -1,454 +0,0 @@
|
|
|
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
|
-
ElemeAdapter,
|
|
11
|
-
SNAPSHOT_SCHEMA_VERSION,
|
|
12
|
-
VALID_SNAPSHOT_KINDS,
|
|
13
|
-
orderToRecord,
|
|
14
|
-
extractOrders,
|
|
15
|
-
} = require("../lib/adapters/shopping-eleme");
|
|
16
|
-
const { assertAdapter } = require("../lib/adapter-spec");
|
|
17
|
-
const { validateBatch } = require("../lib/batch");
|
|
18
|
-
|
|
19
|
-
// 饿了么 (Ele.me) — Alibaba-owned #2 food-delivery; mirrors shopping-meituan
|
|
20
|
-
// (the sibling 外卖 platform) with a snapshot + cookie-api dual path. The mtop
|
|
21
|
-
// signing is injected (signProvider) so the adapter stays pure-Node.
|
|
22
|
-
|
|
23
|
-
function writeSnapshot(dir, snapshot) {
|
|
24
|
-
const p = path.join(dir, "shopping-eleme.json");
|
|
25
|
-
fs.writeFileSync(p, JSON.stringify(snapshot), "utf-8");
|
|
26
|
-
return p;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function writeRaw(dir, fileName, content) {
|
|
30
|
-
const p = path.join(dir, fileName);
|
|
31
|
-
fs.writeFileSync(p, content, "utf-8");
|
|
32
|
-
return p;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
describe("ElemeAdapter snapshot mode", () => {
|
|
36
|
-
let tmpDir;
|
|
37
|
-
beforeEach(() => {
|
|
38
|
-
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "eleme-snap-"));
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it("exports SNAPSHOT_SCHEMA_VERSION = 1 and VALID_SNAPSHOT_KINDS = [order]", () => {
|
|
42
|
-
expect(SNAPSHOT_SCHEMA_VERSION).toBe(1);
|
|
43
|
-
expect(VALID_SNAPSHOT_KINDS).toEqual(["order"]);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it("authenticate(inputPath) ok when readable", async () => {
|
|
47
|
-
const p = writeSnapshot(tmpDir, {
|
|
48
|
-
schemaVersion: 1,
|
|
49
|
-
snapshottedAt: Date.now(),
|
|
50
|
-
events: [],
|
|
51
|
-
});
|
|
52
|
-
const a = new ElemeAdapter();
|
|
53
|
-
const res = await a.authenticate({ inputPath: p });
|
|
54
|
-
expect(res.ok).toBe(true);
|
|
55
|
-
expect(res.mode).toBe("snapshot-file");
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
it("authenticate(inputPath) fails when path unreadable", async () => {
|
|
59
|
-
const a = new ElemeAdapter();
|
|
60
|
-
const res = await a.authenticate({ inputPath: path.join(tmpDir, "missing.json") });
|
|
61
|
-
expect(res.ok).toBe(false);
|
|
62
|
-
expect(res.reason).toBe("INPUT_PATH_UNREADABLE");
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
it("authenticate() with no inputPath returns NO_INPUT", async () => {
|
|
66
|
-
const a = new ElemeAdapter();
|
|
67
|
-
const res = await a.authenticate({});
|
|
68
|
-
expect(res.ok).toBe(false);
|
|
69
|
-
expect(res.reason).toBe("NO_INPUT");
|
|
70
|
-
expect(res.message).toMatch(/snapshot mode/);
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it("sync() without inputPath throws with signing hint", async () => {
|
|
74
|
-
const a = new ElemeAdapter();
|
|
75
|
-
let threw = null;
|
|
76
|
-
try {
|
|
77
|
-
for await (const _r of a.sync({})) { /* drain */ }
|
|
78
|
-
} catch (err) {
|
|
79
|
-
threw = err;
|
|
80
|
-
}
|
|
81
|
-
expect(threw).toBeTruthy();
|
|
82
|
-
expect(String(threw.message)).toMatch(/signProvider/);
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
it("rejects non-JSON inputPath", async () => {
|
|
86
|
-
const p = writeRaw(tmpDir, "orders.html", "<html><body>not json</body></html>");
|
|
87
|
-
const a = new ElemeAdapter();
|
|
88
|
-
let threw = null;
|
|
89
|
-
try {
|
|
90
|
-
for await (const _r of a.sync({ inputPath: p })) { /* drain */ }
|
|
91
|
-
} catch (err) {
|
|
92
|
-
threw = err;
|
|
93
|
-
}
|
|
94
|
-
expect(threw).toBeTruthy();
|
|
95
|
-
expect(String(threw.message)).toMatch(/snapshot must be JSON/);
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
it("rejects schemaVersion mismatch", async () => {
|
|
99
|
-
const p = writeSnapshot(tmpDir, {
|
|
100
|
-
schemaVersion: 99,
|
|
101
|
-
snapshottedAt: Date.now(),
|
|
102
|
-
events: [],
|
|
103
|
-
});
|
|
104
|
-
const a = new ElemeAdapter();
|
|
105
|
-
let threw = null;
|
|
106
|
-
try {
|
|
107
|
-
for await (const _r of a.sync({ inputPath: p })) { /* drain */ }
|
|
108
|
-
} catch (err) {
|
|
109
|
-
threw = err;
|
|
110
|
-
}
|
|
111
|
-
expect(threw).toBeTruthy();
|
|
112
|
-
expect(String(threw.message)).toMatch(/schemaVersion mismatch/);
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
it("empty events array yields nothing (no crash)", async () => {
|
|
116
|
-
const p = writeSnapshot(tmpDir, {
|
|
117
|
-
schemaVersion: 1,
|
|
118
|
-
snapshottedAt: Date.now(),
|
|
119
|
-
events: [],
|
|
120
|
-
});
|
|
121
|
-
const a = new ElemeAdapter();
|
|
122
|
-
const raws = [];
|
|
123
|
-
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
124
|
-
expect(raws.length).toBe(0);
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
it("order event round-trips normalize cleanly", async () => {
|
|
128
|
-
const now = Date.now();
|
|
129
|
-
const p = writeSnapshot(tmpDir, {
|
|
130
|
-
schemaVersion: 1,
|
|
131
|
-
snapshottedAt: now,
|
|
132
|
-
vendor: "eleme",
|
|
133
|
-
account: { userId: "u-alice", displayName: "alice" },
|
|
134
|
-
events: [
|
|
135
|
-
{
|
|
136
|
-
kind: "order",
|
|
137
|
-
id: "order-ELM-9001",
|
|
138
|
-
capturedAt: now - 1000,
|
|
139
|
-
orderId: "ELM-9001",
|
|
140
|
-
merchantName: "沙县小吃(人民路店)",
|
|
141
|
-
placedAt: now - 86400_000,
|
|
142
|
-
paidAt: now - 86400_000 + 5_000,
|
|
143
|
-
status: "已送达",
|
|
144
|
-
items: [
|
|
145
|
-
{ name: "蒸饺", quantity: 2, unitPrice: 8.0 },
|
|
146
|
-
{ name: "拌面", quantity: 1, unitPrice: 7.5 },
|
|
147
|
-
],
|
|
148
|
-
totalAmount: { value: 23.5, currency: "CNY" },
|
|
149
|
-
recipient: "张三",
|
|
150
|
-
shippingAddress: "上海市浦东新区...",
|
|
151
|
-
},
|
|
152
|
-
],
|
|
153
|
-
});
|
|
154
|
-
const a = new ElemeAdapter();
|
|
155
|
-
const raws = [];
|
|
156
|
-
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
157
|
-
expect(raws.length).toBe(1);
|
|
158
|
-
expect(raws[0].kind).toBe("order");
|
|
159
|
-
expect(raws[0].originalId).toBe("eleme:order:order-ELM-9001");
|
|
160
|
-
|
|
161
|
-
const batch = a.normalize(raws[0]);
|
|
162
|
-
expect(validateBatch(batch).valid).toBe(true);
|
|
163
|
-
expect(batch.events.length).toBeGreaterThan(0);
|
|
164
|
-
expect(JSON.stringify(batch)).toContain("蒸饺");
|
|
165
|
-
expect(JSON.stringify(batch)).toContain("张三");
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
it("status mapping handles ele.me-typical strings", async () => {
|
|
169
|
-
const now = Date.now();
|
|
170
|
-
const cases = [
|
|
171
|
-
{ status: "配送中", expect: "shipped" },
|
|
172
|
-
{ status: "已送达", expect: "delivered" },
|
|
173
|
-
{ status: "已完成", expect: "delivered" },
|
|
174
|
-
{ status: "退款成功", expect: "refunded" },
|
|
175
|
-
{ status: "已取消", expect: "cancelled" },
|
|
176
|
-
{ status: "等待商家接单", expect: "placed" },
|
|
177
|
-
];
|
|
178
|
-
for (const c of cases) {
|
|
179
|
-
const p = writeSnapshot(tmpDir, {
|
|
180
|
-
schemaVersion: 1,
|
|
181
|
-
snapshottedAt: now,
|
|
182
|
-
events: [
|
|
183
|
-
{
|
|
184
|
-
kind: "order", id: `o-${c.status}`,
|
|
185
|
-
orderId: `o-${c.status}`, merchantName: "m",
|
|
186
|
-
placedAt: now, paidAt: now, status: c.status,
|
|
187
|
-
items: [{ name: "x", quantity: 1, unitPrice: 1 }],
|
|
188
|
-
totalAmount: { value: 1, currency: "CNY" },
|
|
189
|
-
},
|
|
190
|
-
],
|
|
191
|
-
});
|
|
192
|
-
const a = new ElemeAdapter();
|
|
193
|
-
const raws = [];
|
|
194
|
-
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
195
|
-
const batch = a.normalize(raws[0]);
|
|
196
|
-
expect(JSON.stringify(batch)).toContain(c.expect);
|
|
197
|
-
}
|
|
198
|
-
});
|
|
199
|
-
|
|
200
|
-
it("respects per-kind include opt-out", async () => {
|
|
201
|
-
const now = Date.now();
|
|
202
|
-
const p = writeSnapshot(tmpDir, {
|
|
203
|
-
schemaVersion: 1,
|
|
204
|
-
snapshottedAt: now,
|
|
205
|
-
events: [
|
|
206
|
-
{ kind: "order", id: "o1", orderId: "o1", merchantName: "m", placedAt: now,
|
|
207
|
-
items: [], totalAmount: { value: 1, currency: "CNY" } },
|
|
208
|
-
],
|
|
209
|
-
});
|
|
210
|
-
const a = new ElemeAdapter();
|
|
211
|
-
const raws = [];
|
|
212
|
-
for await (const r of a.sync({ inputPath: p, include: { order: false } })) {
|
|
213
|
-
raws.push(r);
|
|
214
|
-
}
|
|
215
|
-
expect(raws.length).toBe(0);
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
it("respects opts.limit", async () => {
|
|
219
|
-
const now = Date.now();
|
|
220
|
-
const events = Array.from({ length: 5 }, (_, i) => ({
|
|
221
|
-
kind: "order", id: `o${i}`, orderId: `o${i}`, merchantName: "m",
|
|
222
|
-
placedAt: now - i * 1000,
|
|
223
|
-
items: [], totalAmount: { value: 1, currency: "CNY" },
|
|
224
|
-
}));
|
|
225
|
-
const p = writeSnapshot(tmpDir, { schemaVersion: 1, snapshottedAt: now, events });
|
|
226
|
-
const a = new ElemeAdapter();
|
|
227
|
-
const raws = [];
|
|
228
|
-
for await (const r of a.sync({ inputPath: p, limit: 2 })) raws.push(r);
|
|
229
|
-
expect(raws.length).toBe(2);
|
|
230
|
-
});
|
|
231
|
-
|
|
232
|
-
it("filters out unknown kinds (forward compat)", async () => {
|
|
233
|
-
const now = Date.now();
|
|
234
|
-
const p = writeSnapshot(tmpDir, {
|
|
235
|
-
schemaVersion: 1,
|
|
236
|
-
snapshottedAt: now,
|
|
237
|
-
events: [
|
|
238
|
-
{ kind: "order", id: "o1", orderId: "o1", merchantName: "m", placedAt: now,
|
|
239
|
-
items: [], totalAmount: { value: 1, currency: "CNY" } },
|
|
240
|
-
{ kind: "refund", id: "r1", orderId: "o1" },
|
|
241
|
-
{ kind: "future-kind", id: "x" },
|
|
242
|
-
],
|
|
243
|
-
});
|
|
244
|
-
const a = new ElemeAdapter();
|
|
245
|
-
const raws = [];
|
|
246
|
-
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
247
|
-
expect(raws.length).toBe(1);
|
|
248
|
-
expect(raws[0].kind).toBe("order");
|
|
249
|
-
});
|
|
250
|
-
|
|
251
|
-
it("snapshottedAt fallback when event capturedAt missing", async () => {
|
|
252
|
-
const ts = 1700000000000;
|
|
253
|
-
const p = writeSnapshot(tmpDir, {
|
|
254
|
-
schemaVersion: 1,
|
|
255
|
-
snapshottedAt: ts,
|
|
256
|
-
events: [
|
|
257
|
-
{ kind: "order", id: "o1", orderId: "o1", merchantName: "m", items: [],
|
|
258
|
-
totalAmount: { value: 1, currency: "CNY" } },
|
|
259
|
-
],
|
|
260
|
-
});
|
|
261
|
-
const a = new ElemeAdapter();
|
|
262
|
-
const raws = [];
|
|
263
|
-
for await (const r of a.sync({ inputPath: p })) raws.push(r);
|
|
264
|
-
expect(raws[0].capturedAt).toBe(ts);
|
|
265
|
-
});
|
|
266
|
-
|
|
267
|
-
it("advertises both snapshot and cookie-api capabilities", () => {
|
|
268
|
-
const a = new ElemeAdapter();
|
|
269
|
-
expect(a.capabilities).toContain("sync:snapshot");
|
|
270
|
-
expect(a.capabilities).toContain("sync:cookie-api");
|
|
271
|
-
expect(assertAdapter(a).ok).toBe(true);
|
|
272
|
-
});
|
|
273
|
-
});
|
|
274
|
-
|
|
275
|
-
describe("ElemeAdapter cookie-api mode", () => {
|
|
276
|
-
it("authenticate(cookie) ok when userId + cookies present", async () => {
|
|
277
|
-
const a = new ElemeAdapter({ account: { userId: "u-1", cookies: "USERID=ok" } });
|
|
278
|
-
const res = await a.authenticate();
|
|
279
|
-
expect(res.ok).toBe(true);
|
|
280
|
-
expect(res.mode).toBe("cookie");
|
|
281
|
-
expect(res.account).toBe("u-1");
|
|
282
|
-
});
|
|
283
|
-
|
|
284
|
-
it("authenticate(cookie) requires account.userId", async () => {
|
|
285
|
-
const a = new ElemeAdapter({ account: { cookies: "USERID=ok" } });
|
|
286
|
-
const res = await a.authenticate();
|
|
287
|
-
expect(res.ok).toBe(false);
|
|
288
|
-
expect(res.reason).toBe("NO_ACCOUNT_USER_ID");
|
|
289
|
-
});
|
|
290
|
-
|
|
291
|
-
it("sync yields normalized records from fetchFn fixture (元)", async () => {
|
|
292
|
-
const fetchFn = async () => ({
|
|
293
|
-
orders: [
|
|
294
|
-
{
|
|
295
|
-
order_id: "ELM-COOKIE-1",
|
|
296
|
-
restaurant_name: "兰州拉面",
|
|
297
|
-
status_bar_text: "已送达",
|
|
298
|
-
total_amount: 28.5,
|
|
299
|
-
order_time: 1700000000, // sec
|
|
300
|
-
pay_time: 1700000010,
|
|
301
|
-
consignee: "李四",
|
|
302
|
-
address: "广州市天河区...",
|
|
303
|
-
basket: [
|
|
304
|
-
{ name: "牛肉拉面", quantity: 2, price: 14.25 },
|
|
305
|
-
],
|
|
306
|
-
},
|
|
307
|
-
],
|
|
308
|
-
});
|
|
309
|
-
const a = new ElemeAdapter({
|
|
310
|
-
account: { userId: "u-1", cookies: "USERID=ok" },
|
|
311
|
-
fetchFn,
|
|
312
|
-
});
|
|
313
|
-
const raws = [];
|
|
314
|
-
for await (const r of a.sync({ sinceWatermark: 0 })) raws.push(r);
|
|
315
|
-
expect(raws.length).toBe(1);
|
|
316
|
-
expect(raws[0].originalId).toBe("ELM-COOKIE-1");
|
|
317
|
-
expect(raws[0].payload.record.totalAmount.value).toBe(28.5);
|
|
318
|
-
expect(raws[0].payload.record.items[0].unitPrice).toBe(14.25);
|
|
319
|
-
expect(raws[0].payload.record.status).toBe("delivered");
|
|
320
|
-
|
|
321
|
-
const batch = a.normalize(raws[0]);
|
|
322
|
-
expect(validateBatch(batch).valid).toBe(true);
|
|
323
|
-
expect(JSON.stringify(batch)).toContain("牛肉拉面");
|
|
324
|
-
expect(JSON.stringify(batch)).toContain("李四");
|
|
325
|
-
});
|
|
326
|
-
|
|
327
|
-
it("invokes signProvider and passes sign to fetchFn", async () => {
|
|
328
|
-
let seenSign = null;
|
|
329
|
-
const signProvider = async () => "SIGN-XYZ";
|
|
330
|
-
const fetchFn = async (opts) => {
|
|
331
|
-
seenSign = opts.sign;
|
|
332
|
-
return { orders: [] };
|
|
333
|
-
};
|
|
334
|
-
const a = new ElemeAdapter({
|
|
335
|
-
account: { userId: "u-1", cookies: "USERID=ok" },
|
|
336
|
-
fetchFn,
|
|
337
|
-
signProvider,
|
|
338
|
-
});
|
|
339
|
-
for await (const _r of a.sync({ sinceWatermark: 0 })) { /* drain */ }
|
|
340
|
-
expect(seenSign).toBe("SIGN-XYZ");
|
|
341
|
-
});
|
|
342
|
-
|
|
343
|
-
it("passes sign: null when no signProvider configured", async () => {
|
|
344
|
-
let seen = "unset";
|
|
345
|
-
const fetchFn = async (opts) => {
|
|
346
|
-
seen = opts.sign;
|
|
347
|
-
return { orders: [] };
|
|
348
|
-
};
|
|
349
|
-
const a = new ElemeAdapter({
|
|
350
|
-
account: { userId: "u-1", cookies: "USERID=ok" },
|
|
351
|
-
fetchFn,
|
|
352
|
-
});
|
|
353
|
-
for await (const _r of a.sync({ sinceWatermark: 0 })) { /* drain */ }
|
|
354
|
-
expect(seen).toBe(null);
|
|
355
|
-
});
|
|
356
|
-
|
|
357
|
-
it("paginates with offset and stops at sinceWatermark", async () => {
|
|
358
|
-
const pages = {
|
|
359
|
-
0: [
|
|
360
|
-
{ order_id: "p1-a", restaurant_name: "m", order_time: 1700000000, total_amount: 10, basket: [] },
|
|
361
|
-
{ order_id: "p1-b", restaurant_name: "m", order_time: 1699000000, total_amount: 10, basket: [] },
|
|
362
|
-
],
|
|
363
|
-
2: [
|
|
364
|
-
{ order_id: "p2-a", restaurant_name: "m", order_time: 1698000000, total_amount: 10, basket: [] },
|
|
365
|
-
],
|
|
366
|
-
};
|
|
367
|
-
const seenOffsets = [];
|
|
368
|
-
const fetchFn = async (opts) => {
|
|
369
|
-
seenOffsets.push(opts.query.offset);
|
|
370
|
-
return { orders: pages[opts.query.offset] || [] };
|
|
371
|
-
};
|
|
372
|
-
const a = new ElemeAdapter({
|
|
373
|
-
account: { userId: "u-1", cookies: "USERID=ok" },
|
|
374
|
-
fetchFn,
|
|
375
|
-
});
|
|
376
|
-
const raws = [];
|
|
377
|
-
for await (const r of a.sync({ sinceWatermark: 1699500000 * 1000, pageSize: 2 })) {
|
|
378
|
-
raws.push(r);
|
|
379
|
-
}
|
|
380
|
-
expect(raws.map((r) => r.originalId)).toEqual(["p1-a"]);
|
|
381
|
-
// stopped inside page 1 (offset 0) — never fetched offset 2
|
|
382
|
-
expect(seenOffsets).toEqual([0]);
|
|
383
|
-
});
|
|
384
|
-
|
|
385
|
-
it("respects per-kind include opt-out in cookie mode (no fetch)", async () => {
|
|
386
|
-
let called = false;
|
|
387
|
-
const fetchFn = async () => {
|
|
388
|
-
called = true;
|
|
389
|
-
return { orders: [] };
|
|
390
|
-
};
|
|
391
|
-
const a = new ElemeAdapter({
|
|
392
|
-
account: { userId: "u-1", cookies: "USERID=ok" },
|
|
393
|
-
fetchFn,
|
|
394
|
-
});
|
|
395
|
-
const raws = [];
|
|
396
|
-
for await (const r of a.sync({ include: { order: false } })) raws.push(r);
|
|
397
|
-
expect(raws.length).toBe(0);
|
|
398
|
-
expect(called).toBe(false);
|
|
399
|
-
});
|
|
400
|
-
|
|
401
|
-
it("orderToRecord maps ele.me fields (元 amounts)", () => {
|
|
402
|
-
const rec = orderToRecord({
|
|
403
|
-
order_id: "ELM-9",
|
|
404
|
-
restaurant_name: "黄焖鸡米饭",
|
|
405
|
-
status_bar_text: "已完成",
|
|
406
|
-
total_amount: 18.0,
|
|
407
|
-
order_time: 1700000000,
|
|
408
|
-
basket: [{ name: "黄焖鸡", quantity: 1, price: 18.0 }],
|
|
409
|
-
});
|
|
410
|
-
expect(rec.orderId).toBe("ELM-9");
|
|
411
|
-
expect(rec.merchantName).toBe("黄焖鸡米饭");
|
|
412
|
-
expect(rec.status).toBe("delivered");
|
|
413
|
-
expect(rec.totalAmount.value).toBe(18.0);
|
|
414
|
-
expect(rec.items[0].name).toBe("黄焖鸡");
|
|
415
|
-
expect(rec.extras.capturedBy).toBe("cookie-api");
|
|
416
|
-
});
|
|
417
|
-
|
|
418
|
-
it("extractOrders tolerates nested response shapes", () => {
|
|
419
|
-
expect(extractOrders({ orders: [1] })).toEqual([1]);
|
|
420
|
-
expect(extractOrders({ order_list: [2] })).toEqual([2]);
|
|
421
|
-
expect(extractOrders({ list: [3] })).toEqual([3]);
|
|
422
|
-
expect(extractOrders({ data: { orders: [4] } })).toEqual([4]);
|
|
423
|
-
expect(extractOrders([5])).toEqual([5]);
|
|
424
|
-
expect(extractOrders({})).toEqual([]);
|
|
425
|
-
expect(extractOrders(null)).toEqual([]);
|
|
426
|
-
});
|
|
427
|
-
|
|
428
|
-
it("uses opts.ordersUrl override when provided", async () => {
|
|
429
|
-
let seenUrl = null;
|
|
430
|
-
const fetchFn = async (opts) => {
|
|
431
|
-
seenUrl = opts.url;
|
|
432
|
-
return { orders: [] };
|
|
433
|
-
};
|
|
434
|
-
const a = new ElemeAdapter({
|
|
435
|
-
account: { userId: "u-1", cookies: "USERID=ok" },
|
|
436
|
-
fetchFn,
|
|
437
|
-
ordersUrl: "https://custom.example/orders",
|
|
438
|
-
});
|
|
439
|
-
for await (const _r of a.sync({ sinceWatermark: 0 })) { /* drain */ }
|
|
440
|
-
expect(seenUrl).toBe("https://custom.example/orders");
|
|
441
|
-
});
|
|
442
|
-
|
|
443
|
-
it("default fetchFn throws a legible error when cookie mode used without injection", async () => {
|
|
444
|
-
const a = new ElemeAdapter({ account: { userId: "u-1", cookies: "USERID=ok" } });
|
|
445
|
-
let threw = null;
|
|
446
|
-
try {
|
|
447
|
-
for await (const _r of a.sync({ sinceWatermark: 0 })) { /* drain */ }
|
|
448
|
-
} catch (err) {
|
|
449
|
-
threw = err;
|
|
450
|
-
}
|
|
451
|
-
expect(threw).toBeTruthy();
|
|
452
|
-
expect(String(threw.message)).toMatch(/no fetchFn configured/);
|
|
453
|
-
});
|
|
454
|
-
});
|