import { describe, expect, it } from 'vitest'
import { ResourceCategorySchema, ResourceLinkSchema, kindPrefix, validateNodeId } from '../resource-link'
import type { OrganizationGraph } from '../../../organization-model/graph/types'

describe('resource-link', () => {
  it('parses resource links and categories', () => {
    expect(ResourceLinkSchema.parse({ nodeId: 'system:sales.crm', kind: 'references' })).toEqual({
      nodeId: 'system:sales.crm',
      kind: 'references'
    })
    expect(ResourceCategorySchema.parse('diagnostic')).toBe('diagnostic')
  })

  it('rejects node ids without a kind prefix', () => {
    expect(() => ResourceLinkSchema.parse({ nodeId: 'sales.crm', kind: 'references' })).toThrow()
  })

  it('rejects retired ad hoc resource-operation links', () => {
    const retiredKind = ['operates', 'on'].join('-')
    expect(() => ResourceLinkSchema.parse({ nodeId: 'system:sales.crm', kind: retiredKind })).toThrow()
  })

  it('builds and validates graph node ids', () => {
    const graph: OrganizationGraph = {
      version: 1,
      organizationModelVersion: 1,
      nodes: [{ id: 'system:sales.crm', kind: 'system', label: 'CRM', sourceId: 'sales.crm' }],
      edges: []
    }

    expect(kindPrefix('system', 'sales.crm')).toBe('system:sales.crm')
    expect(() => validateNodeId('system:sales.crm', graph)).not.toThrow()
    expect(() => validateNodeId('system:missing', graph)).toThrow(/Unknown organization graph node/)
  })
})
