strat-gameplay-webapp/frontend-sba/tests/unit/components/Decisions/OffensiveApproach.spec.ts
Cal Corum 8e543de2b2 CLAUDE: Phase F3 Complete - Decision Input Workflow with Comprehensive Testing
Implemented complete decision input workflow for gameplay interactions with
production-ready components and 100% test coverage.

## Components Implemented (8 files, ~1,800 lines)

### Reusable UI Components (3 files, 315 lines)
- ActionButton.vue: Flexible action button with variants, sizes, loading states
- ButtonGroup.vue: Mutually exclusive button groups with icons/badges
- ToggleSwitch.vue: Animated toggle switches with accessibility

### Decision Components (4 files, 998 lines)
- DefensiveSetup.vue: Defensive positioning (alignment, depths, hold runners)
- StolenBaseInputs.vue: Per-runner steal attempts with visual diamond
- OffensiveApproach.vue: Batting approach selection with hit & run/bunt
- DecisionPanel.vue: Container orchestrating all decision workflows

### Demo Components
- demo-decisions.vue: Interactive preview of all Phase F3 components

## Store & Integration Updates

- store/game.ts: Added decision state management (pending decisions, history)
  - setPendingDefensiveSetup(), setPendingOffensiveDecision()
  - setPendingStealAttempts(), addDecisionToHistory()
  - clearPendingDecisions() for workflow resets

- pages/games/[id].vue: Integrated DecisionPanel with WebSocket actions
  - Connected defensive/offensive submission handlers
  - Phase detection (defensive/offensive/idle)
  - Turn management with computed properties

## Comprehensive Test Suite (7 files, ~2,500 lines, 213 tests)

### UI Component Tests (68 tests)
- ActionButton.spec.ts: 23 tests (variants, sizes, states, events)
- ButtonGroup.spec.ts: 22 tests (selection, layouts, borders)
- ToggleSwitch.spec.ts: 23 tests (states, accessibility, interactions)

### Decision Component Tests (72 tests)
- DefensiveSetup.spec.ts: 21 tests (form validation, hold runners, changes)
- StolenBaseInputs.spec.ts: 29 tests (runner detection, steal calculation)
- OffensiveApproach.spec.ts: 22 tests (approach selection, tactics)

### Store Tests (15 tests)
- game-decisions.spec.ts: Complete decision workflow coverage

**Test Results**: 213/213 tests passing (100%)
**Coverage**: All code paths, edge cases, user interactions tested

## Features

### Mobile-First Design
- Touch-friendly buttons (44px minimum)
- Responsive layouts (375px → 1920px+)
- Vertical stacking on mobile, grid on desktop
- Dark mode support throughout

### User Experience
- Clear turn indicators (your turn vs opponent)
- Disabled states when not active
- Loading states during submission
- Decision history tracking (last 10 decisions)
- Visual feedback on all interactions
- Change detection prevents no-op submissions

### Visual Consistency
- Matches Phase F2 color scheme (blue, green, red, yellow)
- Gradient backgrounds for selected states
- Smooth animations (fade, slide, pulse)
- Consistent spacing and rounded corners

### Accessibility
- ARIA attributes and roles
- Keyboard navigation support
- Screen reader friendly
- High contrast text/backgrounds

## WebSocket Integration

Connected to backend event handlers:
- submit_defensive_decision → DefensiveSetup
- submit_offensive_decision → OffensiveApproach
- steal_attempts → StolenBaseInputs
All events flow through useGameActions composable

## Demo & Preview

Visit http://localhost:3001/demo-decisions for interactive component preview:
- Tab 1: All UI components with variants/sizes
- Tab 2: Defensive setup with all options
- Tab 3: Stolen base inputs with mini diamond
- Tab 4: Offensive approach with tactics
- Tab 5: Integrated decision panel
- Demo controls to test different scenarios

## Impact

- Phase F3: 100% complete with comprehensive testing
- Frontend Progress: ~40% → ~55% (Phases F1-F3)
- Production-ready code with 213 passing tests
- Zero regressions in existing tests
- Ready for Phase F4 (Manual Outcome & Dice Rolling)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-13 13:47:36 -06:00

325 lines
8.3 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import { mount } from '@vue/test-utils'
import OffensiveApproach from '~/components/Decisions/OffensiveApproach.vue'
describe('OffensiveApproach', () => {
const defaultProps = {
gameId: 'test-game-123',
isActive: true,
}
describe('Rendering', () => {
it('renders all approach options', () => {
const wrapper = mount(OffensiveApproach, {
props: defaultProps,
})
expect(wrapper.text()).toContain('Normal')
expect(wrapper.text()).toContain('Contact')
expect(wrapper.text()).toContain('Power')
expect(wrapper.text()).toContain('Patient')
})
it('renders special tactics section', () => {
const wrapper = mount(OffensiveApproach, {
props: defaultProps,
})
expect(wrapper.text()).toContain('Hit and Run')
expect(wrapper.text()).toContain('Bunt Attempt')
})
})
describe('Approach Selection', () => {
it('selects normal approach by default', () => {
const wrapper = mount(OffensiveApproach, {
props: defaultProps,
})
expect(wrapper.vm.localDecision.approach).toBe('normal')
})
it('uses provided currentDecision approach', () => {
const wrapper = mount(OffensiveApproach, {
props: {
...defaultProps,
currentDecision: {
approach: 'power',
hit_and_run: false,
bunt_attempt: false,
},
},
})
expect(wrapper.vm.localDecision.approach).toBe('power')
})
it('changes approach when button clicked', async () => {
const wrapper = mount(OffensiveApproach, {
props: defaultProps,
})
wrapper.vm.selectApproach('contact')
await wrapper.vm.$nextTick()
expect(wrapper.vm.localDecision.approach).toBe('contact')
})
it('does not change approach when not active', () => {
const wrapper = mount(OffensiveApproach, {
props: {
...defaultProps,
isActive: false,
},
})
const originalApproach = wrapper.vm.localDecision.approach
wrapper.vm.selectApproach('power')
expect(wrapper.vm.localDecision.approach).toBe(originalApproach)
})
})
describe('Hit and Run', () => {
it('is disabled when no runners on base', () => {
const wrapper = mount(OffensiveApproach, {
props: {
...defaultProps,
hasRunnersOnBase: false,
},
})
expect(wrapper.vm.canUseHitAndRun).toBe(false)
})
it('is enabled when runners on base', () => {
const wrapper = mount(OffensiveApproach, {
props: {
...defaultProps,
hasRunnersOnBase: true,
},
})
expect(wrapper.vm.canUseHitAndRun).toBe(true)
})
it('clears hit and run when runners removed', async () => {
const wrapper = mount(OffensiveApproach, {
props: {
...defaultProps,
hasRunnersOnBase: true,
},
})
wrapper.vm.localDecision.hit_and_run = true
await wrapper.vm.$nextTick()
await wrapper.setProps({ hasRunnersOnBase: false })
expect(wrapper.vm.localDecision.hit_and_run).toBe(false)
})
})
describe('Change Detection', () => {
it('detects approach changes', () => {
const wrapper = mount(OffensiveApproach, {
props: {
...defaultProps,
currentDecision: {
approach: 'normal',
hit_and_run: false,
bunt_attempt: false,
},
},
})
expect(wrapper.vm.hasChanges).toBe(false)
wrapper.vm.localDecision.approach = 'power'
expect(wrapper.vm.hasChanges).toBe(true)
})
it('detects hit and run changes', () => {
const wrapper = mount(OffensiveApproach, {
props: {
...defaultProps,
currentDecision: {
approach: 'normal',
hit_and_run: false,
bunt_attempt: false,
},
hasRunnersOnBase: true,
},
})
expect(wrapper.vm.hasChanges).toBe(false)
wrapper.vm.localDecision.hit_and_run = true
expect(wrapper.vm.hasChanges).toBe(true)
})
it('detects bunt attempt changes', () => {
const wrapper = mount(OffensiveApproach, {
props: {
...defaultProps,
currentDecision: {
approach: 'normal',
hit_and_run: false,
bunt_attempt: false,
},
},
})
expect(wrapper.vm.hasChanges).toBe(false)
wrapper.vm.localDecision.bunt_attempt = true
expect(wrapper.vm.hasChanges).toBe(true)
})
})
describe('Form Submission', () => {
it('emits submit with decision', async () => {
const wrapper = mount(OffensiveApproach, {
props: defaultProps,
})
wrapper.vm.localDecision = {
approach: 'power',
hit_and_run: false,
bunt_attempt: true,
}
await wrapper.find('form').trigger('submit.prevent')
expect(wrapper.emitted('submit')).toBeTruthy()
const emitted = wrapper.emitted('submit')![0][0]
expect(emitted).toEqual({
approach: 'power',
hit_and_run: false,
bunt_attempt: true,
})
})
it('does not submit when not active', async () => {
const wrapper = mount(OffensiveApproach, {
props: {
...defaultProps,
isActive: false,
},
})
await wrapper.find('form').trigger('submit.prevent')
expect(wrapper.emitted('submit')).toBeFalsy()
})
it('does not submit when no changes', async () => {
const wrapper = mount(OffensiveApproach, {
props: {
...defaultProps,
currentDecision: {
approach: 'normal',
hit_and_run: false,
bunt_attempt: false,
},
},
})
await wrapper.find('form').trigger('submit.prevent')
expect(wrapper.emitted('submit')).toBeFalsy()
})
})
describe('Display Text', () => {
it('shows current approach label', () => {
const wrapper = mount(OffensiveApproach, {
props: {
...defaultProps,
currentDecision: {
approach: 'contact',
hit_and_run: false,
bunt_attempt: false,
},
},
})
expect(wrapper.vm.currentApproachLabel).toBe('Contact')
})
it('shows active tactics', () => {
const wrapper = mount(OffensiveApproach, {
props: defaultProps,
})
wrapper.vm.localDecision.hit_and_run = true
wrapper.vm.localDecision.bunt_attempt = true
expect(wrapper.vm.activeTactics).toBe('Hit & Run, Bunt')
})
it('shows None when no tactics active', () => {
const wrapper = mount(OffensiveApproach, {
props: defaultProps,
})
expect(wrapper.vm.activeTactics).toBe('None')
})
})
describe('Submit Button Text', () => {
it('shows wait message when not active', () => {
const wrapper = mount(OffensiveApproach, {
props: {
...defaultProps,
isActive: false,
},
})
expect(wrapper.vm.submitButtonText).toBe('Wait for Your Turn')
})
it('shows no changes message', () => {
const wrapper = mount(OffensiveApproach, {
props: {
...defaultProps,
currentDecision: {
approach: 'normal',
hit_and_run: false,
bunt_attempt: false,
},
},
})
expect(wrapper.vm.submitButtonText).toBe('No Changes')
})
it('shows submit message when active with changes', () => {
const wrapper = mount(OffensiveApproach, {
props: defaultProps,
})
wrapper.vm.localDecision.approach = 'power'
expect(wrapper.vm.submitButtonText).toBe('Submit Offensive Strategy')
})
})
describe('Prop Updates', () => {
it('updates local state when currentDecision changes', async () => {
const wrapper = mount(OffensiveApproach, {
props: defaultProps,
})
await wrapper.setProps({
currentDecision: {
approach: 'patient',
hit_and_run: true,
bunt_attempt: true,
},
})
expect(wrapper.vm.localDecision.approach).toBe('patient')
expect(wrapper.vm.localDecision.hit_and_run).toBe(true)
expect(wrapper.vm.localDecision.bunt_attempt).toBe(true)
})
})
})