import { z } from 'zod'
import { describe, expect, it } from 'vitest'
import { assertCrudResourceDefinition, type CrudResourceDefinition } from '../resourceDefinition'

interface ClientRow { email: string }

const definition: CrudResourceDefinition<unknown, ClientRow, unknown> = {
  resourceId: 'client-advisory', querySchemaVersion: '1', responseSchemaVersion: '1',
  querySchema: z.unknown(), rowSchema: z.object({ email: z.string() }), formSchema: z.unknown(),
  columns: [{ field: 'email', header: 'Email', sensitive: true }], sensitiveFields: ['email'],
  permissionPolicy: 'ClientAdvisory.Read', concurrencyMode: 'ETAG_IF_MATCH', idempotencyMode: 'IDEMPOTENCY_KEY'
}

describe('CrudResourceDefinition', () => {
  it('accepts an explicit secure resource contract', () => expect(() => assertCrudResourceDefinition(definition)).not.toThrow())
  it('rejects sensitive fields without a sensitive column contract', () => expect(() => assertCrudResourceDefinition({ ...definition, columns: [{ field: 'email', header: 'Email' }] })).toThrow('sensitive grid column'))
  it('rejects missing permission policy', () => expect(() => assertCrudResourceDefinition({ ...definition, permissionPolicy: ' ' })).toThrow('permissionPolicy'))
})
