/**
 * AppSidebar cloud active-state tests.
 *
 * Verifies that the "Cloud Storage" nav link is active only when the route
 * is exactly /cloud (the overview), NOT when a connection or folder is active.
 */
import { describe, it, expect, beforeEach, vi } from 'vitest'
import { mount, flushPromises } from '@vue/test-utils'
import { setActivePinia, createPinia } from 'pinia'
import { createRouter, createMemoryHistory } from 'vue-router'

vi.mock('../../../api/client.js', () => ({
  listFolders: vi.fn().mockResolvedValue({ items: [] }),
  getSharedWithMe: vi.fn().mockResolvedValue([]),
  listCloudConnections: vi.fn().mockResolvedValue({ items: [] }),
  listTopics: vi.fn().mockResolvedValue({ topics: [] }),
  getMyQuota: vi.fn().mockResolvedValue({ used_bytes: 0, limit_bytes: 104857600 }),
}))

const STUBS = {
  FolderTreeItem: true,
  CloudProviderTreeItem: true,
  QuotaBar: true,
  EmptyState: true,
}

function makeRouter(initialPath = '/cloud') {
  const router = createRouter({
    history: createMemoryHistory(),
    routes: [
      { path: '/', component: { template: '<div/>' } },
      { path: '/settings', component: { template: '<div/>' } },
      { path: '/topics', component: { template: '<div/>' } },
      { path: '/shared', component: { template: '<div/>' } },
      { path: '/cloud', component: { template: '<div/>' } },
      { path: '/cloud/:connectionId/:folderId', component: { template: '<div/>' } },
    ],
  })
  router.push(initialPath)
  return router
}

import AppSidebar from '../AppSidebar.vue'

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

describe('AppSidebar cloud active state', () => {
  it('Cloud Storage link has nav-link-active class at /cloud (exact)', async () => {
    const router = makeRouter('/cloud')
    await router.isReady()
    const wrapper = mount(AppSidebar, {
      global: { plugins: [router], stubs: STUBS },
    })
    await flushPromises()
    // Find the Cloud Storage router-link — it should be active on /cloud
    const cloudLink = wrapper.findAll('a').find(a => a.text().includes('Cloud Storage'))
    expect(cloudLink).toBeTruthy()
    expect(cloudLink.classes()).toContain('nav-link-active')
  })

  it('Cloud Storage link does NOT have nav-link-active when inside a connection folder', async () => {
    const router = makeRouter('/cloud/uuid-conn-1/root')
    await router.isReady()
    const wrapper = mount(AppSidebar, {
      global: { plugins: [router], stubs: STUBS },
    })
    await flushPromises()
    const cloudLink = wrapper.findAll('a').find(a => a.text().includes('Cloud Storage'))
    expect(cloudLink).toBeTruthy()
    // Must NOT be active — only the connection/folder node is active
    expect(cloudLink.classes()).not.toContain('nav-link-active')
  })
})
