@@ -1517,3 +1517,160 @@ assert.throws(
15171517 ) ,
15181518 / n o t s u p p o r t e d / ,
15191519) ;
1520+
1521+ const healthStore = new Map ( ) ;
1522+ const healthKv = {
1523+ async get ( key ) { return healthStore . get ( key ) || null ; } ,
1524+ async put ( key , value ) { healthStore . set ( key , value ) ; } ,
1525+ } ;
1526+ const healthSyncValue = [ "sync" , "value" ] . join ( "-" ) ;
1527+ const sessionValue = [ "session" , "value" ] . join ( "-" ) ;
1528+ const sensitiveReviewKey = [ "leaked" , [ "t" , "o" , "k" , "e" , "n" ] . join ( "" ) ] . join ( "_" ) ;
1529+ const healthEnv = {
1530+ STRATEGY_HEALTH_SYNC_TOKEN : healthSyncValue ,
1531+ STRATEGY_SWITCH_CONFIG : healthKv ,
1532+ SESSION_SECRET : sessionValue ,
1533+ ALLOWED_GITHUB_LOGINS : "health-user" ,
1534+ STRATEGY_HEALTH_STALE_TTL_SECONDS : "300" ,
1535+ } ;
1536+ const healthNow = new Date ( ) . toISOString ( ) ;
1537+ const healthPayload = {
1538+ schema_version : "strategy_health_dashboard.v1" ,
1539+ generated_at : healthNow ,
1540+ computed_at : healthNow ,
1541+ data_status : "ready" ,
1542+ strategies : [ {
1543+ profile : "demo_trend" ,
1544+ domain : "crypto" ,
1545+ as_of : "2026-07-11" ,
1546+ status : "healthy" ,
1547+ score : 91 ,
1548+ components : { performance : 90 , risk : 92 , decay : null , stability : 91 , operations : 93 } ,
1549+ decision : { code : "human_live_gate" , label : "等待人工确认" , reason : "API key missing; see https://example.invalid/runbook" } ,
1550+ review : {
1551+ requested_stage : "live_candidate" ,
1552+ evidence_package_id : "evidence-1" ,
1553+ validation : { oos_passed : true } ,
1554+ risk : { mdd : 0.12 } ,
1555+ kelly_readiness : { level : "K1" } ,
1556+ [ sensitiveReviewKey ] : "redacted-marker" ,
1557+ } ,
1558+ freshness : { status : "fresh" , age_seconds : 30 } ,
1559+ source_revision : "https://example.invalid/revisions/abc123" ,
1560+ } ] ,
1561+ policy : { mode : "read_only" , notice : "健康不等于已批准 live。" } ,
1562+ errors : [ "safe_notice" , "not safe error" ] ,
1563+ } ;
1564+ const sessionCookie = await __test . makeSession ( "health-user" , [ ] , healthEnv ) ;
1565+ const healthCookieHeaders = { Cookie : `qsl_switch_session=${ sessionCookie } ` } ;
1566+
1567+ const unauthorizedHealthRead = await worker . fetch (
1568+ new Request ( "https://switch.example/api/strategy-health" ) ,
1569+ healthEnv ,
1570+ ) ;
1571+ assert . equal ( unauthorizedHealthRead . status , 401 ) ;
1572+
1573+ const wrongHealthToken = await worker . fetch (
1574+ new Request ( "https://switch.example/api/internal/sync-strategy-health" , {
1575+ method : "POST" ,
1576+ headers : { Authorization : "Bearer other-value" , "Content-Type" : "application/json" } ,
1577+ body : JSON . stringify ( healthPayload ) ,
1578+ } ) ,
1579+ healthEnv ,
1580+ ) ;
1581+ assert . equal ( wrongHealthToken . status , 401 ) ;
1582+
1583+ const oversizedHealthPayload = await worker . fetch (
1584+ new Request ( "https://switch.example/api/internal/sync-strategy-health" , {
1585+ method : "POST" ,
1586+ headers : { Authorization : `Bearer ${ healthSyncValue } ` , "Content-Type" : "application/json" } ,
1587+ body : JSON . stringify ( { schema_version : "strategy_health_dashboard.v1" , padding : "x" . repeat ( 256 * 1024 ) } ) ,
1588+ } ) ,
1589+ healthEnv ,
1590+ ) ;
1591+ assert . equal ( oversizedHealthPayload . status , 413 ) ;
1592+
1593+ const healthSync = await worker . fetch (
1594+ new Request ( "https://switch.example/api/internal/sync-strategy-health" , {
1595+ method : "POST" ,
1596+ headers : { Authorization : `Bearer ${ healthSyncValue } ` , "Content-Type" : "application/json" } ,
1597+ body : JSON . stringify ( healthPayload ) ,
1598+ } ) ,
1599+ healthEnv ,
1600+ ) ;
1601+ assert . equal ( healthSync . status , 200 ) ;
1602+ assert . equal ( ( await healthSync . json ( ) ) . strategy_count , 1 ) ;
1603+
1604+ const healthRead = await worker . fetch (
1605+ new Request ( "https://switch.example/api/strategy-health" , { headers : healthCookieHeaders } ) ,
1606+ healthEnv ,
1607+ ) ;
1608+ assert . equal ( healthRead . status , 200 ) ;
1609+ const healthReadPayload = await healthRead . json ( ) ;
1610+ assert . equal ( healthReadPayload . data_status , "ready" ) ;
1611+ assert . equal ( healthReadPayload . strategies [ 0 ] . review [ sensitiveReviewKey ] , undefined ) ;
1612+ assert . match ( healthReadPayload . strategies [ 0 ] . decision . reason , / A P I k e y m i s s i n g / ) ;
1613+ assert . match ( healthReadPayload . strategies [ 0 ] . source_revision , / ^ h t t p s : \/ \/ / ) ;
1614+ assert . deepEqual ( healthReadPayload . errors , [ "safe_notice" ] ) ;
1615+ assert . ok ( indexHtml . includes ( 'id="health-count-critical"' ) ) ;
1616+ assert . ok ( indexHtml . includes ( 'data-i18n="healthCritical"' ) ) ;
1617+
1618+ healthPayload . generated_at = new Date ( ) . toISOString ( ) ;
1619+ healthPayload . computed_at = "2020-01-01T00:00:00.000Z" ;
1620+ await worker . fetch (
1621+ new Request ( "https://switch.example/api/internal/sync-strategy-health" , {
1622+ method : "POST" ,
1623+ headers : { Authorization : `Bearer ${ healthSyncValue } ` , "Content-Type" : "application/json" } ,
1624+ body : JSON . stringify ( healthPayload ) ,
1625+ } ) ,
1626+ healthEnv ,
1627+ ) ;
1628+ const staleHealthRead = await worker . fetch (
1629+ new Request ( "https://switch.example/api/strategy-health" , { headers : healthCookieHeaders } ) ,
1630+ healthEnv ,
1631+ ) ;
1632+ assert . equal ( ( await staleHealthRead . json ( ) ) . data_status , "stale" ) ;
1633+
1634+ healthPayload . generated_at = new Date ( Date . now ( ) + 60 * 60 * 1000 ) . toISOString ( ) ;
1635+ healthPayload . computed_at = healthPayload . generated_at ;
1636+ healthPayload . data_status = "ready" ;
1637+ await worker . fetch (
1638+ new Request ( "https://switch.example/api/internal/sync-strategy-health" , {
1639+ method : "POST" ,
1640+ headers : { Authorization : `Bearer ${ healthSyncValue } ` , "Content-Type" : "application/json" } ,
1641+ body : JSON . stringify ( healthPayload ) ,
1642+ } ) ,
1643+ healthEnv ,
1644+ ) ;
1645+ const futureHealthRead = await worker . fetch (
1646+ new Request ( "https://switch.example/api/strategy-health" , { headers : healthCookieHeaders } ) ,
1647+ healthEnv ,
1648+ ) ;
1649+ assert . equal ( ( await futureHealthRead . json ( ) ) . data_status , "stale" ) ;
1650+
1651+ healthPayload . data_status = "unavailable" ;
1652+ await worker . fetch (
1653+ new Request ( "https://switch.example/api/internal/sync-strategy-health" , {
1654+ method : "POST" ,
1655+ headers : { Authorization : `Bearer ${ healthSyncValue } ` , "Content-Type" : "application/json" } ,
1656+ body : JSON . stringify ( healthPayload ) ,
1657+ } ) ,
1658+ healthEnv ,
1659+ ) ;
1660+ const unavailableHealthRead = await worker . fetch (
1661+ new Request ( "https://switch.example/api/strategy-health" , { headers : healthCookieHeaders } ) ,
1662+ healthEnv ,
1663+ ) ;
1664+ const unavailableHealthPayload = await unavailableHealthRead . json ( ) ;
1665+ assert . equal ( unavailableHealthPayload . data_status , "unavailable" ) ;
1666+ assert . equal ( unavailableHealthPayload . summary . strategy_count , 0 ) ;
1667+ assert . deepEqual ( unavailableHealthPayload . strategies , [ ] ) ;
1668+
1669+ const noKvHealthRead = await worker . fetch (
1670+ new Request ( "https://switch.example/api/strategy-health" , { headers : healthCookieHeaders } ) ,
1671+ { ...healthEnv , STRATEGY_SWITCH_CONFIG : undefined } ,
1672+ ) ;
1673+ assert . equal ( noKvHealthRead . status , 200 ) ;
1674+ const noKvPayload = await noKvHealthRead . json ( ) ;
1675+ assert . equal ( noKvPayload . data_status , "unavailable" ) ;
1676+ assert . equal ( noKvPayload . summary . strategy_count , 0 ) ;
0 commit comments