|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { evaluateExecutionGate, type ExecutionGateInput } from './execution-gate'; |
| 3 | +import type { ClarificationMetadata } from './clarification-gate'; |
| 4 | +import type { PlanningStageMetadata } from './planning-stage'; |
| 5 | + |
| 6 | +// --------------------------------------------------------------------------- |
| 7 | +// Helper factories |
| 8 | +// --------------------------------------------------------------------------- |
| 9 | + |
| 10 | +function ambiguousClarification(): ClarificationMetadata { |
| 11 | + return { |
| 12 | + clarificationNeeded: true, |
| 13 | + planReady: false, |
| 14 | + questionBudget: 2, |
| 15 | + nextQuestion: 'What should change?', |
| 16 | + clarificationTopics: ['vague-intent'], |
| 17 | + }; |
| 18 | +} |
| 19 | + |
| 20 | +function clearClarification(): ClarificationMetadata { |
| 21 | + return { |
| 22 | + clarificationNeeded: false, |
| 23 | + planReady: true, |
| 24 | + questionBudget: 3, |
| 25 | + }; |
| 26 | +} |
| 27 | + |
| 28 | +function budgetExhaustedClarification(): ClarificationMetadata { |
| 29 | + return { |
| 30 | + clarificationNeeded: false, |
| 31 | + planReady: true, |
| 32 | + questionBudget: 0, |
| 33 | + assumptionNote: 'Proceeding with assumptions.', |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +function discoverStage(): PlanningStageMetadata { |
| 38 | + return { |
| 39 | + currentStage: 'discover', |
| 40 | + stageDescription: 'Discover: Surface questions...', |
| 41 | + nextStage: 'design', |
| 42 | + stageTransitionHint: 'Answer clarification to proceed.', |
| 43 | + recommendedAgent: 'solution-architect', |
| 44 | + recommendedSkill: 'brainstorming', |
| 45 | + }; |
| 46 | +} |
| 47 | + |
| 48 | +function designStage(): PlanningStageMetadata { |
| 49 | + return { |
| 50 | + currentStage: 'design', |
| 51 | + stageDescription: 'Design: Synthesize approaches...', |
| 52 | + nextStage: 'plan', |
| 53 | + stageTransitionHint: 'Confirm approach to proceed.', |
| 54 | + recommendedAgent: 'solution-architect', |
| 55 | + }; |
| 56 | +} |
| 57 | + |
| 58 | +function planStage(): PlanningStageMetadata { |
| 59 | + return { |
| 60 | + currentStage: 'plan', |
| 61 | + stageDescription: 'Plan: Produce implementation plan.', |
| 62 | + recommendedAgent: 'technical-planner', |
| 63 | + recommendedSkill: 'writing-plans', |
| 64 | + }; |
| 65 | +} |
| 66 | + |
| 67 | +const SAMPLE_SPECIALISTS = [ |
| 68 | + 'security-specialist', |
| 69 | + 'performance-specialist', |
| 70 | + 'code-quality-specialist', |
| 71 | +]; |
| 72 | + |
| 73 | +// --------------------------------------------------------------------------- |
| 74 | +// Tests |
| 75 | +// --------------------------------------------------------------------------- |
| 76 | + |
| 77 | +describe('execution-gate', () => { |
| 78 | + describe('evaluateExecutionGate', () => { |
| 79 | + // ------------------------------------------------------------------ |
| 80 | + // Gated scenarios |
| 81 | + // ------------------------------------------------------------------ |
| 82 | + |
| 83 | + describe('gated (ambiguous / early stage)', () => { |
| 84 | + it('gates when clarificationNeeded=true', () => { |
| 85 | + const input: ExecutionGateInput = { |
| 86 | + clarification: ambiguousClarification(), |
| 87 | + specialists: SAMPLE_SPECIALISTS, |
| 88 | + }; |
| 89 | + |
| 90 | + const result = evaluateExecutionGate(input); |
| 91 | + |
| 92 | + expect(result.gated).toBe(true); |
| 93 | + expect(result.reason).toContain('ambiguous'); |
| 94 | + expect(result.unblockCondition).toBeTruthy(); |
| 95 | + expect(result.deferredSpecialists).toEqual(SAMPLE_SPECIALISTS); |
| 96 | + }); |
| 97 | + |
| 98 | + it('gates when currentStage=discover', () => { |
| 99 | + const input: ExecutionGateInput = { |
| 100 | + clarification: { clarificationNeeded: false, planReady: false }, |
| 101 | + planningStage: discoverStage(), |
| 102 | + specialists: SAMPLE_SPECIALISTS, |
| 103 | + }; |
| 104 | + |
| 105 | + const result = evaluateExecutionGate(input); |
| 106 | + |
| 107 | + expect(result.gated).toBe(true); |
| 108 | + expect(result.reason).toContain('discover'); |
| 109 | + expect(result.unblockCondition).toContain('Design'); |
| 110 | + }); |
| 111 | + |
| 112 | + it('gates when currentStage=design', () => { |
| 113 | + const input: ExecutionGateInput = { |
| 114 | + clarification: { clarificationNeeded: false, planReady: false }, |
| 115 | + planningStage: designStage(), |
| 116 | + specialists: SAMPLE_SPECIALISTS, |
| 117 | + }; |
| 118 | + |
| 119 | + const result = evaluateExecutionGate(input); |
| 120 | + |
| 121 | + expect(result.gated).toBe(true); |
| 122 | + expect(result.reason).toContain('design'); |
| 123 | + expect(result.unblockCondition).toContain('Plan'); |
| 124 | + }); |
| 125 | + |
| 126 | + it('defers specialists list when gated', () => { |
| 127 | + const input: ExecutionGateInput = { |
| 128 | + clarification: ambiguousClarification(), |
| 129 | + specialists: ['security-specialist', 'accessibility-specialist'], |
| 130 | + }; |
| 131 | + |
| 132 | + const result = evaluateExecutionGate(input); |
| 133 | + |
| 134 | + expect(result.deferredSpecialists).toEqual([ |
| 135 | + 'security-specialist', |
| 136 | + 'accessibility-specialist', |
| 137 | + ]); |
| 138 | + }); |
| 139 | + |
| 140 | + it('omits deferredSpecialists when no specialists provided', () => { |
| 141 | + const input: ExecutionGateInput = { |
| 142 | + clarification: ambiguousClarification(), |
| 143 | + }; |
| 144 | + |
| 145 | + const result = evaluateExecutionGate(input); |
| 146 | + |
| 147 | + expect(result.gated).toBe(true); |
| 148 | + expect(result.deferredSpecialists).toBeUndefined(); |
| 149 | + }); |
| 150 | + |
| 151 | + it('omits deferredSpecialists when specialists list is empty', () => { |
| 152 | + const input: ExecutionGateInput = { |
| 153 | + clarification: ambiguousClarification(), |
| 154 | + specialists: [], |
| 155 | + }; |
| 156 | + |
| 157 | + const result = evaluateExecutionGate(input); |
| 158 | + |
| 159 | + expect(result.gated).toBe(true); |
| 160 | + expect(result.deferredSpecialists).toBeUndefined(); |
| 161 | + }); |
| 162 | + }); |
| 163 | + |
| 164 | + // ------------------------------------------------------------------ |
| 165 | + // Ungated scenarios |
| 166 | + // ------------------------------------------------------------------ |
| 167 | + |
| 168 | + describe('ungated (clear / plan stage)', () => { |
| 169 | + it('does not gate when planReady=true', () => { |
| 170 | + const input: ExecutionGateInput = { |
| 171 | + clarification: clearClarification(), |
| 172 | + specialists: SAMPLE_SPECIALISTS, |
| 173 | + }; |
| 174 | + |
| 175 | + const result = evaluateExecutionGate(input); |
| 176 | + |
| 177 | + expect(result.gated).toBe(false); |
| 178 | + expect(result.reason).toContain('clear'); |
| 179 | + expect(result.unblockCondition).toBeUndefined(); |
| 180 | + expect(result.deferredSpecialists).toBeUndefined(); |
| 181 | + }); |
| 182 | + |
| 183 | + it('does not gate when currentStage=plan', () => { |
| 184 | + const input: ExecutionGateInput = { |
| 185 | + clarification: clearClarification(), |
| 186 | + planningStage: planStage(), |
| 187 | + specialists: SAMPLE_SPECIALISTS, |
| 188 | + }; |
| 189 | + |
| 190 | + const result = evaluateExecutionGate(input); |
| 191 | + |
| 192 | + expect(result.gated).toBe(false); |
| 193 | + }); |
| 194 | + |
| 195 | + it('does not gate when budget is exhausted (planReady forced)', () => { |
| 196 | + const input: ExecutionGateInput = { |
| 197 | + clarification: budgetExhaustedClarification(), |
| 198 | + specialists: SAMPLE_SPECIALISTS, |
| 199 | + }; |
| 200 | + |
| 201 | + const result = evaluateExecutionGate(input); |
| 202 | + |
| 203 | + expect(result.gated).toBe(false); |
| 204 | + }); |
| 205 | + |
| 206 | + it('planReady=true overrides discover stage', () => { |
| 207 | + const input: ExecutionGateInput = { |
| 208 | + clarification: clearClarification(), |
| 209 | + planningStage: discoverStage(), |
| 210 | + specialists: SAMPLE_SPECIALISTS, |
| 211 | + }; |
| 212 | + |
| 213 | + const result = evaluateExecutionGate(input); |
| 214 | + |
| 215 | + // planReady takes precedence over stage |
| 216 | + expect(result.gated).toBe(false); |
| 217 | + }); |
| 218 | + |
| 219 | + it('planReady=true overrides design stage', () => { |
| 220 | + const input: ExecutionGateInput = { |
| 221 | + clarification: clearClarification(), |
| 222 | + planningStage: designStage(), |
| 223 | + specialists: SAMPLE_SPECIALISTS, |
| 224 | + }; |
| 225 | + |
| 226 | + const result = evaluateExecutionGate(input); |
| 227 | + |
| 228 | + expect(result.gated).toBe(false); |
| 229 | + }); |
| 230 | + }); |
| 231 | + |
| 232 | + // ------------------------------------------------------------------ |
| 233 | + // Priority / edge cases |
| 234 | + // ------------------------------------------------------------------ |
| 235 | + |
| 236 | + describe('priority and edge cases', () => { |
| 237 | + it('clarificationNeeded takes priority over planningStage', () => { |
| 238 | + const input: ExecutionGateInput = { |
| 239 | + clarification: ambiguousClarification(), |
| 240 | + planningStage: planStage(), // even though stage says plan |
| 241 | + specialists: SAMPLE_SPECIALISTS, |
| 242 | + }; |
| 243 | + |
| 244 | + const result = evaluateExecutionGate(input); |
| 245 | + |
| 246 | + // clarificationNeeded=true is checked before stage |
| 247 | + expect(result.gated).toBe(true); |
| 248 | + expect(result.reason).toContain('ambiguous'); |
| 249 | + }); |
| 250 | + |
| 251 | + it('works without planningStage metadata', () => { |
| 252 | + const input: ExecutionGateInput = { |
| 253 | + clarification: clearClarification(), |
| 254 | + // no planningStage |
| 255 | + }; |
| 256 | + |
| 257 | + const result = evaluateExecutionGate(input); |
| 258 | + |
| 259 | + expect(result.gated).toBe(false); |
| 260 | + }); |
| 261 | + |
| 262 | + it('does not mutate the input specialists array', () => { |
| 263 | + const specialists = ['a', 'b', 'c']; |
| 264 | + const input: ExecutionGateInput = { |
| 265 | + clarification: ambiguousClarification(), |
| 266 | + specialists, |
| 267 | + }; |
| 268 | + |
| 269 | + const result = evaluateExecutionGate(input); |
| 270 | + |
| 271 | + expect(result.deferredSpecialists).toEqual(['a', 'b', 'c']); |
| 272 | + expect(result.deferredSpecialists).not.toBe(specialists); // different reference |
| 273 | + }); |
| 274 | + }); |
| 275 | + }); |
| 276 | +}); |
0 commit comments