Improve scan terminal freshness and AMSC overlay consistency

This commit is contained in:
2569718930@qq.com
2026-06-15 01:39:19 +08:00
parent 717786b1d8
commit c79e0f36cc
30 changed files with 924 additions and 48 deletions
@@ -82,6 +82,11 @@ export function runTests() {
/fetchCache:\s*"no-store"/,
"scan terminal proxy must not put failed or initializing payloads into the Next data cache",
);
assert.match(
scanTerminalProxySource,
/"since_snapshot_id"/,
"scan terminal proxy should forward incremental scan diff snapshot ids to the backend",
);
const scanTerminalClientSource = fs.readFileSync(
path.join(
@@ -108,6 +113,11 @@ export function runTests() {
/params\.set\("_v",\s*SCAN_TERMINAL_PAYLOAD_VERSION\)/,
"scan terminal client should vary the CDN cache key without changing backend scan filters",
);
assert.match(
scanTerminalClientSource,
/params\.set\("diff",\s*"true"\)/,
"scan terminal client should request incremental scan diffs when it has a previous snapshot",
);
const overviewProxySource = fs.readFileSync(
path.join(
@@ -36,7 +36,7 @@ async function flushMicrotasks() {
export async function runTests() {
assert(DASHBOARD_REFRESH_POLICY_MS.observation === 60_000, "observation layer should refresh every 60 seconds");
assert(DASHBOARD_REFRESH_POLICY_MS.scanRows === 5 * 60_000, "region/city rows should refresh every 5 minutes");
assert(DASHBOARD_REFRESH_POLICY_MS.scanRows === 2 * 60_000, "region/city rows should refresh every 2 minutes");
assert(DASHBOARD_REFRESH_POLICY_MS.marketOverview === 10 * 60_000, "market overview should refresh every 10 minutes");
assert(DASHBOARD_REFRESH_POLICY_MS.model === 30 * 60_000, "DEB and multi-model data should refresh every 30 minutes");
assert(DASHBOARD_REFRESH_POLICY_SEC.metar === 5 * 60, "METAR polling should be 5 minutes");
@@ -69,8 +69,9 @@ export async function runTests() {
querySource.includes("handleForegroundScanRefresh") &&
querySource.includes('document.visibilityState !== "visible"') &&
querySource.includes("SCAN_CACHE_TTL_MS") &&
querySource.includes("FOREGROUND_SCAN_REFRESH_AFTER_SUCCESS_MS = 60_000") &&
querySource.includes("fetchScanTerminal({ forceRefresh: false, showLoading: false })"),
"scan list should silently revalidate stale rows when a long-hidden browser tab returns to the foreground",
"scan list should silently revalidate rows when a browser tab returns to the foreground after 60 seconds",
);
assert(
chartLogicSource.includes("DASHBOARD_REFRESH_POLICY_MS.metar") &&
@@ -0,0 +1,156 @@
import {
__applyPatchToScanRowForTest,
__mergeScanTerminalIncrementalResponseForTest,
} from "@/components/dashboard/scan-terminal/use-scan-terminal-query";
import type { ScanTerminalResponse } from "@/lib/dashboard-types";
import type { CityPatch } from "@/hooks/use-sse-patches";
function assert(condition: unknown, message: string) {
if (!condition) throw new Error(message);
}
export function runTests() {
const patch: CityPatch = {
type: "city_observation_patch.v1",
city: "shenzhen",
revision: 42,
changes: {
temp: 30.2,
max_so_far: 30.4,
signed_gap: 1.4,
gap_to_target: -1.4,
touch_distance: 0,
current_reference: 30.4,
edge: 0.06,
edge_percent: 6,
obs_time: "2026-06-14T06:30:00Z",
},
};
const next = __applyPatchToScanRowForTest(
{
city: "shenzhen",
current_temp: 29.8,
current_max_so_far: 30.0,
signed_gap: 1.0,
gap_to_target: -1.0,
touch_distance: 0.2,
current_reference: 30.0,
edge: 0.04,
edge_percent: 4,
local_time: "old",
target_threshold: 29,
},
patch,
) as any;
assert(next.current_temp === 30.2, "SSE patch should update scan row current temperature");
assert(next.current_max_so_far === 30.4, "SSE patch should use backend max_so_far instead of only Math.max(row, temp)");
assert(next.signed_gap === 1.4, "SSE patch should update signed_gap when backend sends it");
assert(next.gap_to_target === -1.4, "SSE patch should update gap_to_target when backend sends it");
assert(next.touch_distance === 0, "SSE patch should update touch_distance when backend sends it");
assert(next.current_reference === 30.4, "SSE patch should update current_reference when backend sends it");
assert(next.edge === 0.06 && next.edge_percent === 6, "SSE patch should update edge fields when backend sends them");
assert(next.local_time === "2026-06-14T06:30:00Z", "SSE patch should update observation time");
assert(next.sse_revision === 42, "SSE patch should record the applied revision");
const derived = __applyPatchToScanRowForTest(
{
city: "shenzhen",
current_temp: 28,
current_max_so_far: 28,
target_threshold: 30,
target_label: "30+",
},
{
type: "city_observation_patch.v1",
city: "shenzhen",
revision: 43,
changes: {
temp: 30.5,
},
},
) as any;
assert(derived.current_max_so_far === 30.5, "SSE patch should still derive max_so_far from temp when backend omits it");
assert(derived.signed_gap === 0.5, "SSE patch should derive signed_gap from updated max and row threshold");
assert(derived.gap_to_target === -0.5, "SSE patch should derive gap_to_target from updated max and row threshold");
assert(derived.touch_distance === 0, "SSE patch should derive zero touch_distance once threshold is reached");
const previous: ScanTerminalResponse = {
generated_at: "2026-06-14T00:00:00Z",
snapshot_id: "scan-old",
status: "ready",
stale: false,
filters: {
scan_mode: "tradable",
min_price: 0.05,
max_price: 0.95,
min_edge_pct: 2,
min_liquidity: 500,
high_liquidity_only: false,
market_type: "maxtemp",
time_range: "today",
limit: 180,
},
summary: {
recommended_count: 2,
visible_count: 2,
candidate_total: 2,
tradable_market_count: 2,
total_volume: 1000,
},
top_signal: null,
rows: [
{ id: "row-1", rank: 1, city: "shenzhen", edge_percent: 4 },
{ id: "row-removed", rank: 2, city: "paris", edge_percent: 3 },
],
};
const notModified = __mergeScanTerminalIncrementalResponseForTest(previous, {
generated_at: "2026-06-14T00:01:00Z",
snapshot_id: "scan-old",
status: "not_modified",
stale: false,
filters: previous.filters,
summary: previous.summary,
top_signal: previous.top_signal,
rows: [],
diff: {
mode: "not_modified",
base_snapshot_id: "scan-old",
snapshot_id: "scan-old",
rows_changed: [],
removed_row_ids: [],
},
});
assert(notModified.rows.length === 2, "not_modified scan diff should keep the previous rows");
assert(notModified.status === "ready", "not_modified scan diff should normalize to a renderable ready payload");
const delta = __mergeScanTerminalIncrementalResponseForTest(previous, {
generated_at: "2026-06-14T00:02:00Z",
snapshot_id: "scan-new",
status: "ready",
stale: false,
filters: previous.filters,
summary: { ...previous.summary, recommended_count: 2 },
top_signal: { id: "row-added", city: "tokyo" } as any,
rows: [],
diff: {
mode: "row_delta",
base_snapshot_id: "scan-old",
snapshot_id: "scan-new",
rows_changed: [
{ id: "row-1", rank: 1, city: "shenzhen", edge_percent: 6 },
{ id: "row-added", rank: 2, city: "tokyo", edge_percent: 5 },
],
removed_row_ids: ["row-removed"],
},
});
assert(delta.snapshot_id === "scan-new", "row_delta scan diff should advance snapshot_id");
assert(delta.rows.length === 2, "row_delta scan diff should merge changed, added, and removed rows");
assert(delta.rows[0].id === "row-1" && delta.rows[0].edge_percent === 6, "row_delta scan diff should replace changed rows");
assert(delta.rows[1].id === "row-added", "row_delta scan diff should append new rows in rank order");
}