import { mount } from '@vue/test-utils'
import { describe, expect, it } from 'vitest'
import DataGridShell from '../DataGridShell.vue'
import { nativeUiAdapter } from '../adapter/native'
import { uiAdapterKey } from '../adapter/contracts'

const global = {
  provide: { [uiAdapterKey as unknown as string]: nativeUiAdapter },
  stubs: { Paginator: { template: '<div data-test="paginator" />' } },
}

describe('DataGridShell server-side pagination contract', () => {
  it('does not invent pagination when server page metadata is absent', () => {
    const wrapper = mount(DataGridShell, { global, props: { rows: [{ id: 1 }], columns: [{ field: 'id', header: 'ID' }] } })
    expect(wrapper.findComponent({ name: 'KsPaginator' }).exists()).toBe(false)
  })

  it('forwards explicit page changes without owning the data fetch', async () => {
    const wrapper = mount(DataGridShell, {
      global,
      props: { rows: [{ id: 1 }], columns: [{ field: 'id', header: 'ID' }], page: 1, pageSize: 20, total: 40 }
    })
    const paginator = wrapper.findComponent({ name: 'KsPaginator' })
    await paginator.vm.$emit('pageChange', { page: 2, pageSize: 20 })
    expect(wrapper.emitted('pageChange')).toEqual([[{ page: 2, pageSize: 20 }]])
  })
})
