import { describe, it, expect, vi, beforeEach } from 'vitest'
import { mount } from '@vue/test-utils'
import { createPinia, setActivePinia } from 'pinia'

vi.mock('../../stores/cloudConnections.js', () => ({
  useCloudConnectionsStore: () => ({
    connections: [],
    loading: false,
    error: null,
    fetchConnections: vi.fn(),
    defaultDisplayName: (c) => c.provider,
  }),
}))

vi.mock('vue-router', () => ({
  useRouter: () => ({ push: vi.fn() }),
  useRoute: () => ({ params: {}, query: {} }),
}))

import CloudStorageView from '../CloudStorageView.vue'

const globalStubs = {
  StorageBrowser: {
    template: '<div data-test="storage-browser"><slot /></div>',
    props: ['folders', 'files', 'breadcrumb', 'uploadQueue', 'loading', 'emptyMessage', 'emptyHint'],
    emits: ['folder-navigate'],
  },
}

beforeEach(() => {
  setActivePinia(createPinia())
  vi.clearAllMocks()
})

describe('CloudStorageView', () => {
  it('renders StorageBrowser (no parallel grid)', () => {
    const w = mount(CloudStorageView, { global: { stubs: globalStubs } })
    expect(w.find('[data-test="storage-browser"]').exists()).toBe(true)
  })

  it('contains no file-grid markup of its own', () => {
    const w = mount(CloudStorageView, { global: { stubs: globalStubs } })
    // The view must not render its own grid (no grid/list/table/tr outside StorageBrowser)
    const html = w.html()
    // Only StorageBrowser is the grid consumer
    expect(html).not.toContain('<table')
    expect(html).not.toContain('<ul class')
  })

  it('passes loading=false when store is not loading', () => {
    const w = mount(CloudStorageView, { global: { stubs: globalStubs } })
    const sb = w.find('[data-test="storage-browser"]')
    expect(sb).toBeTruthy()
  })
})
