// #region DatasetReviewSessionTests [C:3] [TYPE Module] [SEMANTICS dataset, review, session, store, test, diff]
// @BRIEF Unit tests for dataset review session store: session set/reset, loading, error, dirty flag, and patch operations.
// @RELATION DEPENDS_ON -> [DatasetReviewSessionStore]

// #region DatasetReviewSessionStoreTests:Module [TYPE Function]
// @SEMANTICS: dataset-review, store, session, tests
// @PURPOSE: Unit tests for dataset review session store.
// @LAYER UI
// @RELATION DEPENDS_ON -> [EXT:frontend:datasetReviewSession]
// @UX_STATE: Idle -> Store helpers are exercised without asynchronous UI transitions.

import { describe, it, expect, beforeEach, vi } from 'vitest';

describe('datasetReviewSession store', () => {
  beforeEach(async () => {
    vi.resetModules();
  });

  it('should have correct initial state', async () => {
    const { datasetReviewSessionStore } = await import('../datasetReviewSession.svelte.js');
    
    let state = null;
    const unsubscribe = datasetReviewSessionStore.subscribe(s => { state = s; });
    unsubscribe();
    
    expect(state.session).toBeNull();
    expect(state.isLoading).toBe(false);
    expect(state.isSaving).toBe(false);
    expect(state.error).toBeNull();
    expect(state.isDirty).toBe(false);
  });

  it('should set session data', async () => {
    const { datasetReviewSessionStore, setSession } = await import('../datasetReviewSession.svelte.js');
    const mockSession = { session_id: 's1', user_id: 'u1' };
    
    setSession(mockSession);
    
    let state = null;
    const unsubscribe = datasetReviewSessionStore.subscribe(s => { state = s; });
    unsubscribe();
    
    expect(state.session).toEqual(mockSession);
    expect(state.isLoading).toBe(false);
    expect(state.error).toBeNull();
    expect(state.lastUpdated).toBeInstanceOf(Date);
    expect(state.isDirty).toBe(false);
  });

  it('should update loading state', async () => {
    const { datasetReviewSessionStore, setLoading } = await import('../datasetReviewSession.svelte.js');
    
    setLoading(true);
    
    let state = null;
    const unsubscribe = datasetReviewSessionStore.subscribe(s => { state = s; });
    unsubscribe();
    
    expect(state.isLoading).toBe(true);
  });

  it('should set error state', async () => {
    const { datasetReviewSessionStore, setError } = await import('../datasetReviewSession.svelte.js');
    
    setError('Failed to load');
    
    let state = null;
    const unsubscribe = datasetReviewSessionStore.subscribe(s => { state = s; });
    unsubscribe();
    
    expect(state.error).toBe('Failed to load');
    expect(state.isLoading).toBe(false);
  });

  it('should mark as dirty', async () => {
    const { datasetReviewSessionStore, setDirty } = await import('../datasetReviewSession.svelte.js');
    
    setDirty(true);
    
    let state = null;
    const unsubscribe = datasetReviewSessionStore.subscribe(s => { state = s; });
    unsubscribe();
    
    expect(state.isDirty).toBe(true);
  });

  it('should patch session data and mark as dirty', async () => {
    const { datasetReviewSessionStore, setSession, patchSession } = await import('../datasetReviewSession.svelte.js');
    const mockSession = { session_id: 's1', readiness_state: 'empty' };
    
    setSession(mockSession);
    patchSession({ readiness_state: 'ready' });
    
    let state = null;
    const unsubscribe = datasetReviewSessionStore.subscribe(s => { state = s; });
    unsubscribe();
    
    expect(state.session.readiness_state).toBe('ready');
    expect(state.isDirty).toBe(true);
  });

  it('should reset session', async () => {
    const { datasetReviewSessionStore, setSession, resetSession } = await import('../datasetReviewSession.svelte.js');
    setSession({ id: 's1' });
    
    resetSession();
    
    let state = null;
    const unsubscribe = datasetReviewSessionStore.subscribe(s => { state = s; });
    unsubscribe();
    
    expect(state.session).toBeNull();
  });
});

// #endregion DatasetReviewSessionStoreTests:Module
// #endregion DatasetReviewSessionTests
