11import { beforeEach , describe , expect , it , vi } from "vitest" ;
22
3+ type TestUpdateAvailable = {
4+ currentVersion : string ;
5+ latestVersion : string ;
6+ channel : string ;
7+ } | null ;
8+ type TestUpdateSentinel = {
9+ kind : string ;
10+ status : string ;
11+ ts : number ;
12+ stats : Record < string , unknown > ;
13+ } | null ;
14+ type TestUpdateSchedule =
15+ | import ( "../../../packages/gateway-protocol/src/index.js" ) . UpdateScheduleState
16+ | null ;
17+
318const checkUpdateStatusMock = vi . hoisted ( ( ) => vi . fn ( ) ) ;
419const versionMock = vi . hoisted ( ( ) => ( { value : "1.0.0" } ) ) ;
20+ const getUpdateAvailableMock = vi . hoisted ( ( ) => vi . fn < ( ) => TestUpdateAvailable > ( ( ) => null ) ) ;
21+ const getUpdateScheduleMock = vi . hoisted ( ( ) => vi . fn < ( ) => TestUpdateSchedule > ( ( ) => null ) ) ;
22+ const refreshGatewayUpdateStatusMock = vi . hoisted ( ( ) => vi . fn ( async ( ) => { } ) ) ;
23+ const getLatestUpdateRestartSentinelMock = vi . hoisted ( ( ) =>
24+ vi . fn < ( ) => TestUpdateSentinel > ( ( ) => null ) ,
25+ ) ;
26+ const refreshLatestUpdateRestartSentinelMock = vi . hoisted ( ( ) =>
27+ vi . fn < ( ) => Promise < TestUpdateSentinel > > ( async ( ) => null ) ,
28+ ) ;
529
630vi . mock ( "../../infra/openclaw-root.js" , async ( ) => {
731 const actual = await vi . importActual < typeof import ( "../../infra/openclaw-root.js" ) > (
@@ -15,9 +39,9 @@ vi.mock("../../infra/update-check.js", () => ({
1539} ) ) ;
1640
1741vi . mock ( "../../infra/update-startup.js" , ( ) => ( {
18- getUpdateAvailable : ( ) => null ,
19- getUpdateSchedule : ( ) => null ,
20- refreshGatewayUpdateStatus : async ( ) => { } ,
42+ getUpdateAvailable : getUpdateAvailableMock ,
43+ getUpdateSchedule : getUpdateScheduleMock ,
44+ refreshGatewayUpdateStatus : refreshGatewayUpdateStatusMock ,
2145} ) ) ;
2246
2347vi . mock ( "../../version.js" , ( ) => ( {
@@ -27,8 +51,8 @@ vi.mock("../../version.js", () => ({
2751} ) ) ;
2852
2953vi . mock ( "../server-restart-sentinel.js" , ( ) => ( {
30- getLatestUpdateRestartSentinel : ( ) => null ,
31- refreshLatestUpdateRestartSentinel : async ( ) => null ,
54+ getLatestUpdateRestartSentinel : getLatestUpdateRestartSentinelMock ,
55+ refreshLatestUpdateRestartSentinel : refreshLatestUpdateRestartSentinelMock ,
3256} ) ) ;
3357
3458vi . mock ( "./validation.js" , ( ) => ( {
@@ -38,6 +62,16 @@ vi.mock("./validation.js", () => ({
3862beforeEach ( ( ) => {
3963 versionMock . value = "1.0.0" ;
4064 checkUpdateStatusMock . mockReset ( ) ;
65+ getUpdateAvailableMock . mockReset ( ) ;
66+ getUpdateAvailableMock . mockReturnValue ( null ) ;
67+ getUpdateScheduleMock . mockReset ( ) ;
68+ getUpdateScheduleMock . mockReturnValue ( null ) ;
69+ refreshGatewayUpdateStatusMock . mockReset ( ) ;
70+ refreshGatewayUpdateStatusMock . mockResolvedValue ( undefined ) ;
71+ getLatestUpdateRestartSentinelMock . mockReset ( ) ;
72+ getLatestUpdateRestartSentinelMock . mockReturnValue ( null ) ;
73+ refreshLatestUpdateRestartSentinelMock . mockReset ( ) ;
74+ refreshLatestUpdateRestartSentinelMock . mockResolvedValue ( null ) ;
4175} ) ;
4276
4377describe ( "update.status effective channel" , ( ) => {
@@ -66,4 +100,68 @@ describe("update.status effective channel", () => {
66100 expect . objectContaining ( { effectiveChannel : "extended-stable" } ) ,
67101 ) ;
68102 } ) ;
103+
104+ it ( "refreshes the latest update sentinel before responding" , async ( ) => {
105+ getUpdateAvailableMock . mockReturnValueOnce ( {
106+ currentVersion : "1.0.0" ,
107+ latestVersion : "2.0.0" ,
108+ channel : "latest" ,
109+ } ) ;
110+ getLatestUpdateRestartSentinelMock . mockReturnValueOnce ( {
111+ kind : "update" ,
112+ status : "skipped" ,
113+ ts : 1 ,
114+ stats : { reason : "restart-health-pending" } ,
115+ } ) ;
116+ refreshLatestUpdateRestartSentinelMock . mockResolvedValueOnce ( {
117+ kind : "update" ,
118+ status : "ok" ,
119+ ts : 2 ,
120+ stats : { after : { version : "2.0.0" } } ,
121+ } ) ;
122+ getUpdateScheduleMock . mockReturnValueOnce ( { channel : "beta" , autoEnabled : true } ) ;
123+ const { updateHandlers } = await import ( "./update.js" ) ;
124+ const respond = vi . fn ( ) ;
125+
126+ const handler = updateHandlers [ "update.status" ] ;
127+ if ( ! handler ) {
128+ throw new Error ( "update.status handler is unavailable" ) ;
129+ }
130+ await handler ( { params : { } , respond } as never ) ;
131+
132+ expect ( refreshLatestUpdateRestartSentinelMock ) . toHaveBeenCalledTimes ( 1 ) ;
133+ expect ( respond ) . toHaveBeenCalledWith (
134+ true ,
135+ expect . objectContaining ( {
136+ sentinel : expect . objectContaining ( { kind : "update" , status : "ok" } ) ,
137+ updateAvailable : expect . objectContaining ( { latestVersion : "2.0.0" } ) ,
138+ schedule : expect . objectContaining ( { channel : "beta" } ) ,
139+ } ) ,
140+ ) ;
141+ } ) ;
142+
143+ it ( "falls back to the cached update sentinel when refresh fails" , async ( ) => {
144+ refreshLatestUpdateRestartSentinelMock . mockRejectedValueOnce ( new Error ( "read failed" ) ) ;
145+ getLatestUpdateRestartSentinelMock . mockReturnValueOnce ( {
146+ kind : "update" ,
147+ status : "skipped" ,
148+ ts : 1 ,
149+ stats : { reason : "restart-health-pending" } ,
150+ } ) ;
151+ const warn = vi . fn ( ) ;
152+ const { updateHandlers } = await import ( "./update.js" ) ;
153+ const respond = vi . fn ( ) ;
154+
155+ const handler = updateHandlers [ "update.status" ] ;
156+ if ( ! handler ) {
157+ throw new Error ( "update.status handler is unavailable" ) ;
158+ }
159+ await handler ( { params : { } , respond, context : { logGateway : { warn } } } as never ) ;
160+
161+ expect ( warn ) . toHaveBeenCalledWith ( "update.status sentinel refresh failed: read failed" ) ;
162+ expect ( respond ) . toHaveBeenCalledWith (
163+ true ,
164+ expect . objectContaining ( { sentinel : expect . objectContaining ( { status : "skipped" } ) } ) ,
165+ ) ;
166+ } ) ;
69167} ) ;
0 commit comments