// #region [EXT:frontend:AssistantChatTest]s [C:3] [TYPE Module] [SEMANTICS assistant, chat, store, test, session]
// @BRIEF Unit tests for assistant chat store validating open/close/toggle, conversation binding, session context, and seed message state transitions.
// @RELATION DEPENDS_ON -> [AssistantChatStore]

// #region [EXT:frontend:AssistantChatTest]:Module [TYPE Function]
// @SEMANTICS: test, store, assistant, toggle, conversation
// @PURPOSE: Validate assistant chat store visibility and conversation binding transitions.
// @LAYER UI (Tests)
// @RELATION DEPENDS_ON -> [EXT:frontend:assistantChat]
// @INVARIANT: Each test starts from default closed state.

import { describe, it, expect, beforeEach } from 'vitest';
import { get } from 'svelte/store';
import {
  assistantChatStore,
  closeAssistantChat,
  openAssistantChat,
  openAssistantChatWithContext,
  setAssistantConversationId,
  setAssistantDatasetReviewSessionId,
  setAssistantFocusTarget,
  toggleAssistantChat,
} from '../assistantChat.svelte.js';

// #region assistantChatStore_tests:Function [TYPE Function]
// @RELATION BINDS_TO -> [EXT:frontend:AssistantChatTest]]
// @PURPOSE: Group store unit scenarios for assistant panel behavior.
// @PRE: Store can be reset to baseline state in beforeEach hook.
// @POST: Open/close/toggle/conversation transitions are validated.
describe('assistantChatStore', () => {
  beforeEach(() => {
    closeAssistantChat();
  });

  it('should open assistant panel', () => {
    openAssistantChat();
    const state = get(assistantChatStore);
    expect(state.isOpen).toBe(true);
  });

  it('should close assistant panel', () => {
    openAssistantChat();
    closeAssistantChat();
    const state = get(assistantChatStore);
    expect(state.isOpen).toBe(false);
  });

  it('should toggle assistant panel state', () => {
    toggleAssistantChat();
    expect(get(assistantChatStore).isOpen).toBe(true);
    toggleAssistantChat();
    expect(get(assistantChatStore).isOpen).toBe(false);
  });

  it('should set conversation id', () => {
    setAssistantConversationId('conv-123');
    const state = get(assistantChatStore);
    expect(state.conversationId).toBe('conv-123');
  });

  it('should open assistant panel with dataset review context', () => {
    openAssistantChatWithContext({
      datasetReviewSessionId: 'session-42',
      seedMessage: 'Explain mapping warning',
      focusTarget: { target: 'mapping:m-1', source: 'test' },
    });
    const state = get(assistantChatStore);
    expect(state.isOpen).toBe(true);
    expect(state.datasetReviewSessionId).toBe('session-42');
    expect(state.seedMessage).toBe('Explain mapping warning');
    expect(state.focusTarget).toEqual({ target: 'mapping:m-1', source: 'test' });
  });

  it('should update dataset review session id and focus target independently', () => {
    setAssistantDatasetReviewSessionId('session-99');
    setAssistantFocusTarget({ target: 'finding:f-1', source: 'test' });
    const state = get(assistantChatStore);
    expect(state.datasetReviewSessionId).toBe('session-99');
    expect(state.focusTarget).toEqual({ target: 'finding:f-1', source: 'test' });
  });
});
// #endregion assistantChatStore_tests:Function
// #endregion [EXT:frontend:AssistantChatTest]:Module
// #endregion [EXT:frontend:AssistantChatTest]s
