Skip to content

Commit 700b64e

Browse files
Pigbibicodex
andcommitted
Fix longbridge account key mapping for runtime target
Co-Authored-By: Codex <noreply@openai.com>
1 parent 3f7e50b commit 700b64e

3 files changed

Lines changed: 83 additions & 8 deletions

File tree

tests/test_cash_financing.js

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,28 @@ function makeCurrentStrategies(platform, overrides = {}) {
159159
function currentEntryForAccount(state, platform, account) {
160160
const byPlatform = state.currentStrategies[platform] || {};
161161
const normalizeAccountLookupKey = (value) => String(value || "").trim().toLowerCase();
162+
const collectAccountLookupCandidates = (keys) => {
163+
const candidates = new Set();
164+
for (const rawKey of keys) {
165+
const key = normalizeAccountLookupKey(rawKey);
166+
if (!key) continue;
167+
168+
candidates.add(key);
169+
170+
const compact = key.replace(/[^a-z0-9]+/g, "");
171+
if (compact) candidates.add(compact);
172+
173+
const parts = key.split(/[^a-z0-9]+/).filter(Boolean);
174+
for (const part of parts) candidates.add(part);
175+
if (parts.length > 1) {
176+
candidates.add(parts[parts.length - 1]);
177+
}
178+
}
179+
return [...candidates];
180+
};
162181
const keys = [account?.key, account?.target_name, account?.label].filter(Boolean).map(String);
163182

164-
const candidates = [...new Set(keys.map(normalizeAccountLookupKey).filter(Boolean))];
183+
const candidates = new Set(collectAccountLookupCandidates(keys));
165184

166185
for (const key of keys) {
167186
const entry = byPlatform[key];
@@ -175,7 +194,9 @@ function currentEntryForAccount(state, platform, account) {
175194

176195
for (const [rawKey, entry] of Object.entries(byPlatform)) {
177196
if (!entry || typeof entry !== "object") continue;
178-
if (candidates.includes(normalizeAccountLookupKey(rawKey))) {
197+
const rawCandidates = collectAccountLookupCandidates([rawKey]);
198+
const hasMatch = rawCandidates.some((candidate) => candidates.has(candidate));
199+
if (hasMatch) {
179200
if (!entry.strategy_profile) {
180201
return {
181202
...entry,
@@ -1056,6 +1077,40 @@ console.log("\n=== 11. currentEntryForAccount 映射健壮性 ===\n");
10561077
assert(entry?.strategy_profile === "soxl_soxx_trend_income", "11e: existing strategy_profile should be preserved");
10571078
}
10581079

1080+
// 11f: 账号 key 带平台前缀时能命中(longbridge sg -> sg)
1081+
{
1082+
const state = {
1083+
currentStrategies: {
1084+
longbridge: {
1085+
sg: { runtime_target_enabled: true },
1086+
},
1087+
},
1088+
};
1089+
const account = {
1090+
key: "longbridge sg",
1091+
label: "LongBridge SG",
1092+
};
1093+
const entry = currentEntryForAccount(state, "longbridge", account);
1094+
assert(entry && entry.runtime_target_enabled === true, "11f: platform-prefixed key should fallback-match by token");
1095+
}
1096+
1097+
// 11g: 账号 key 带分隔符时能命中(longbridge-sg / LB|SG)
1098+
{
1099+
const state = {
1100+
currentStrategies: {
1101+
longbridge: {
1102+
sg: { runtime_target_enabled: true },
1103+
},
1104+
},
1105+
};
1106+
const account = {
1107+
key: "longbridge-sg",
1108+
label: "LB|SG",
1109+
};
1110+
const entry = currentEntryForAccount(state, "longbridge", account);
1111+
assert(entry && entry.runtime_target_enabled === true, "11g: separator variants should fallback-match");
1112+
}
1113+
10591114
// ============================================================
10601115
// 12. syncOptionOverlayForAccount (解析为具体值)
10611116
// ============================================================

web/strategy-switch-console/app.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,11 +1236,29 @@
12361236
return String(value || "").trim().toLowerCase();
12371237
}
12381238

1239+
function collectAccountLookupCandidates(keys) {
1240+
const candidates = new Set();
1241+
for (const rawKey of keys) {
1242+
const key = normalizeAccountLookupKey(rawKey);
1243+
if (!key) continue;
1244+
1245+
candidates.add(key);
1246+
1247+
const compact = key.replace(/[^a-z0-9]+/g, "");
1248+
if (compact) candidates.add(compact);
1249+
1250+
const parts = key.split(/[^a-z0-9]+/).filter(Boolean);
1251+
for (const part of parts) candidates.add(part);
1252+
if (parts.length > 1) {
1253+
candidates.add(parts[parts.length - 1]);
1254+
}
1255+
}
1256+
return [...candidates];
1257+
}
1258+
12391259
function resolveCurrentEntryByKey(byPlatform, keys) {
1240-
const candidates = [...new Set(keys
1241-
.map(normalizeAccountLookupKey)
1242-
.filter(Boolean))];
1243-
if (!candidates.length) return null;
1260+
const candidates = new Set(collectAccountLookupCandidates(keys));
1261+
if (!candidates.size) return null;
12441262

12451263
for (const key of keys) {
12461264
const entry = byPlatform[key];
@@ -1249,7 +1267,9 @@
12491267

12501268
for (const [rawKey, entry] of Object.entries(byPlatform)) {
12511269
if (!currentEntryHasState(entry)) continue;
1252-
if (candidates.includes(normalizeAccountLookupKey(rawKey))) return entry;
1270+
const rawCandidates = collectAccountLookupCandidates([rawKey]);
1271+
const hasMatch = rawCandidates.some((candidate) => candidates.has(candidate));
1272+
if (hasMatch) return entry;
12531273
}
12541274

12551275
return null;

web/strategy-switch-console/app_js.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)