Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions tests/strategy_switch_worker_validation.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,71 @@ try {
globalThis.fetch = originalFetch;
}

globalThis.fetch = async (url) => {
const requestUrl = String(url);
if (requestUrl.endsWith("/CLOUD_RUN_SERVICE_TARGETS_JSON")) {
return new Response("", { status: 404 });
}
if (requestUrl.endsWith("/SCHWAB_MIN_RESERVED_CASH_USD")) {
return new Response(JSON.stringify({ value: "150" }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
}
if (requestUrl.endsWith("/SCHWAB_RESERVED_CASH_RATIO")) {
return new Response(JSON.stringify({ value: "0.03" }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
}
if (requestUrl.endsWith("/INCOME_LAYER_START_USD")) {
return new Response(JSON.stringify({ value: "150000" }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
}
if (requestUrl.endsWith("/INCOME_LAYER_MAX_RATIO")) {
return new Response(JSON.stringify({ value: "0.95" }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
}
if (requestUrl.endsWith("/OPTION_OVERLAY_ENABLED")) {
return new Response(JSON.stringify({ value: "true" }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
}
if (requestUrl.endsWith("/RUNTIME_TARGET_ENABLED")) {
return new Response(JSON.stringify({ value: "TRUE" }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
}
if (requestUrl.endsWith("/DCA_MODE")) {
return new Response(JSON.stringify({ value: "smart" }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
}
if (requestUrl.endsWith("/DCA_BASE_INVESTMENT_USD")) {
return new Response(JSON.stringify({ value: "500" }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
}
return new Response("", { status: 404 });
};
try {
const currentStrategies = await __test.loadCurrentStrategies(
{ schwab: accountOptions.schwab },
{ RUNTIME_SETTINGS_DISPATCH_TOKEN: "test-token" },
);
assert.equal(currentStrategies.schwab.default.runtime_target_enabled, true);
} finally {
globalThis.fetch = originalFetch;
}

globalThis.fetch = async (url) => {
const requestUrl = String(url);
if (requestUrl.endsWith("/CLOUD_RUN_SERVICE_TARGETS_JSON")) {
Expand Down
37 changes: 29 additions & 8 deletions tests/test_cash_financing.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,13 @@ function restoreReserveAfterMarginDisabled(form) {

// --- 数据辅助函数 ---
function cleanOptionalBoolean(value) {
if (value === true || value === "true" || value === "1" || value === 1) return true;
if (value === false || value === "false" || value === "0" || value === 0) return false;
if (value === true || value === 1) return true;
if (value === false || value === 0) return false;
if (typeof value === "string") {
const text = value.trim().toLowerCase();
if (text === "true" || text === "1") return true;
if (text === "false" || text === "0") return false;
}
return null;
}

Expand Down Expand Up @@ -792,23 +797,39 @@ console.log("\n=== 9. syncRuntimeTargetForAccount (解析为具体值) ===\n");
assert(form.runtimeTargetMode === "disabled", "9b: runtime_target=false → 'disabled'");
}

// 9c: no entry → stays "current"
// 9c: runtime_target_enabled: TRUE → enabled
{
const state = { currentStrategies: makeCurrentStrategies("ibkr", { extra: { runtime_target_enabled: "TRUE" } }) };
const form = { runtimeTargetMode: "current", runtimeTargetTouched: false };
syncRuntimeTargetForAccount(state, form, "ibkr", makeAccount("preview"));
assert(form.runtimeTargetMode === "enabled", "9c: runtime_target='TRUE' → 'enabled'");
}

// 9d: runtime_target_enabled: FALSE → disabled
{
const state = { currentStrategies: makeCurrentStrategies("ibkr", { extra: { runtime_target_enabled: "FALSE" } }) };
const form = { runtimeTargetMode: "current", runtimeTargetTouched: false };
syncRuntimeTargetForAccount(state, form, "ibkr", makeAccount("preview"));
assert(form.runtimeTargetMode === "disabled", "9d: runtime_target='FALSE' → 'disabled'");
}

// 9e: no entry → stays "current"
{
const state = { currentStrategies: {} };
const form = { runtimeTargetMode: "current", runtimeTargetTouched: false };
syncRuntimeTargetForAccount(state, form, "ibkr", makeAccount("preview"));
assert(form.runtimeTargetMode === "current", "9c: no entry → 'current'");
}

// 9d: touched → skip
// 9f: touched → skip
{
const state = { currentStrategies: makeCurrentStrategies("ibkr", { extra: { runtime_target_enabled: false } }) };
const form = { runtimeTargetMode: "enabled", runtimeTargetTouched: true };
syncRuntimeTargetForAccount(state, form, "ibkr", makeAccount("preview"));
assert(form.runtimeTargetMode === "enabled", "9d: touched → not overwritten");
assert(form.runtimeTargetMode === "enabled", "9f: touched → not overwritten");
}

// 9e: summary/pending should not mark "current" as a disable change
// 9g: summary/pending should not mark "current" as a disable change
{
const state = { currentStrategies: makeCurrentStrategies("ibkr", { extra: { runtime_target_enabled: true } }) };
const pending = pendingRuntimeTarget(
Expand All @@ -817,8 +838,8 @@ console.log("\n=== 9. syncRuntimeTargetForAccount (解析为具体值) ===\n");
"ibkr",
makeAccount("preview"),
);
assert(pending.changed === false, "9e: current runtime target mode → unchanged");
assert(pending.inputs.runtime_target_enabled === true, "9e: current runtime target keeps enabled value");
assert(pending.changed === false, "9g: current runtime target mode → unchanged");
assert(pending.inputs.runtime_target_enabled === true, "9g: current runtime target keeps enabled value");
}

// ============================================================
Expand Down
11 changes: 8 additions & 3 deletions web/strategy-switch-console/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -1340,8 +1340,13 @@
}

function cleanOptionalBoolean(value) {
if (value === true || value === "true" || value === "1" || value === 1) return true;
if (value === false || value === "false" || value === "0" || value === 0) return false;
if (value === true || value === 1) return true;
if (value === false || value === 0) return false;
if (typeof value === "string") {
const text = value.trim().toLowerCase();
if (text === "true" || text === "1") return true;
if (text === "false" || text === "0") return false;
}
return null;
}

Expand Down Expand Up @@ -1410,7 +1415,7 @@
const form = state.forms[platform];
if (!form || form.runtimeTargetTouched) return;
const current = runtimeTargetEnabledForAccount(platform, selectedAccount(platform));
form.runtimeTargetMode = current ? "enabled" : "disabled";
form.runtimeTargetMode = current === false ? "disabled" : "enabled";
}

function syncReservePolicyForAccount(platform) {
Expand Down
2 changes: 1 addition & 1 deletion web/strategy-switch-console/app_js.js

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions web/strategy-switch-console/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2113,8 +2113,13 @@ function normalizeRuntimeExecutionMode(value, dryRunOnly) {
}

function cleanOptionalBoolean(value) {
if (value === true || value === "true" || value === "1" || value === 1) return true;
if (value === false || value === "false" || value === "0" || value === 0) return false;
if (value === true || value === 1) return true;
if (value === false || value === 0) return false;
if (typeof value === "string") {
const text = value.trim().toLowerCase();
if (text === "true" || text === "1") return true;
if (text === "false" || text === "0") return false;
}
return null;
}

Expand Down
Loading