/**
 * Unit tests for chat store PLAN_EXEC phase state (U4).
 *
 * Verifies the phase state slice is exposed with correct initial values
 * and that `isPlanExec` derives from `currentPhase`. The full event
 * handling is covered by the E2E test in U5.
 */

import { beforeEach, describe, expect, it, vi } from 'vitest'
import { setActivePinia, createPinia } from 'pinia'

// Mock the API client so the store doesn't touch the network.
vi.mock('@/api/client', () => ({
  apiClient: {
    get: vi.fn(),
    post: vi.fn(),
    put: vi.fn(),
    delete: vi.fn(),
    patch: vi.fn(),
  },
}))

// Mock team/documents/calendar stores to avoid pulling their deps.
vi.mock('@/stores/team', () => ({
  useTeamStore: vi.fn(() => null),
}))
vi.mock('@/stores/documents', () => ({
  useDocumentsStore: vi.fn(() => null),
}))
vi.mock('@/stores/calendar', () => ({
  useCalendarStore: vi.fn(() => null),
}))
vi.mock('@/api/documents', () => ({
  isDocumentMeta: vi.fn(),
}))

describe('chat store — PLAN_EXEC phase state (U4)', () => {
  beforeEach(() => {
    setActivePinia(createPinia())
  })

  it('exposes currentPhase, phaseViolations, isPlanExec with initial values', async () => {
    const { useChatStore } = await import('@/stores/chatStore')
    const store = useChatStore()
    expect(store.currentPhase).toBeNull()
    expect(store.phaseViolations).toEqual([])
    expect(store.isPlanExec).toBe(false)
  })

  it('isPlanExec is true when currentPhase is set', async () => {
    const { useChatStore } = await import('@/stores/chatStore')
    const store = useChatStore()
    store.currentPhase = 'planning'
    expect(store.isPlanExec).toBe(true)
  })

  it('phaseViolations array is directly mutable for test fixtures', async () => {
    // Direct mutation bypasses the capping logic in handleWsMessage;
    // the cap is enforced inside the case handler, not as a setter.
    // This test verifies the array is accessible; the cap-at-5 behavior
    // is exercised through handleWsMessage in the U5 E2E test.
    const { useChatStore } = await import('@/stores/chatStore')
    const store = useChatStore()
    for (let i = 0; i < 7; i++) {
      store.phaseViolations = [
        ...store.phaseViolations,
        {
          phase: 'planning',
          tool: `tool_${i}`,
          message: 'blocked',
          violation_kind: 'tool_not_allowed',
          ts: Date.now(),
        },
      ]
    }
    expect(store.phaseViolations.length).toBe(7)
  })
})
