@@ -38,8 +38,23 @@ function stageIndex(stageId: RestTraceStageId): number {
3838 return restTraceStages . findIndex ( ( stage ) => stage . id === stageId ) ;
3939}
4040
41- function isKnownStage ( stageId : RestTraceStageId ) : boolean {
42- return stageIndex ( stageId ) >= 0 ;
41+ function highestVisitedStageIndex ( state : RestLabState ) : number {
42+ return state . currentVisitedStageIds . reduce (
43+ ( highestIndex , visitedStageId ) => Math . max ( highestIndex , stageIndex ( visitedStageId ) ) ,
44+ - 1 ,
45+ ) ;
46+ }
47+
48+ export function isRestStageUnlocked ( state : RestLabState , stageId : RestTraceStageId ) : boolean {
49+ const scenario = findRestScenario ( state . selectedScenarioId ) ;
50+ const targetIndex = stageIndex ( stageId ) ;
51+ const terminalIndex = stageIndex ( scenario . terminalStageId ) ;
52+
53+ if ( ! state . requestStarted || targetIndex < 0 || targetIndex > terminalIndex ) {
54+ return false ;
55+ }
56+
57+ return targetIndex <= highestVisitedStageIndex ( state ) + 1 ;
4358}
4459
4560function isComplete ( state : RestLabState ) : boolean {
@@ -62,7 +77,7 @@ function withStage(current: RestLabState, stageId: RestTraceStageId): RestLabSta
6277 const terminalIndex = stageIndex ( scenario . terminalStageId ) ;
6378 const nextIndex = stageIndex ( stageId ) ;
6479
65- if ( ! state . requestStarted || ! isKnownStage ( stageId ) || nextIndex > terminalIndex ) {
80+ if ( ! isRestStageUnlocked ( state , stageId ) ) {
6681 return { ...state , lastMessage : "這個 stage 不會在目前 request 執行;請依 lifecycle 前進。" } ;
6782 }
6883
@@ -150,8 +165,7 @@ export function runRestEvent(current: RestLabState, event: RestLabEvent): RestEv
150165 }
151166
152167 if ( event . type === "inspect-stage" ) {
153- const scenario = findRestScenario ( state . selectedScenarioId ) ;
154- const accepted = isKnownStage ( event . stageId ) && stageIndex ( event . stageId ) <= stageIndex ( scenario . terminalStageId ) ;
168+ const accepted = isRestStageUnlocked ( state , event . stageId ) ;
155169 return { state : withStage ( state , event . stageId ) , accepted } ;
156170 }
157171
0 commit comments