import { describe, expect, it } from 'vitest'
import {
  getSystemDeprecationDependents,
  type OrganizationModel,
  type OmTopologyDomain
} from '../index'

const model = {
  systems: {
    sales: {
      id: 'sales',
      order: 1,
      label: 'Sales',
      lifecycle: 'active',
      systems: {
        crm: {
          id: 'sales.crm',
          order: 2,
          label: 'CRM',
          lifecycle: 'active'
        }
      }
    }
  },
  resources: {
    'crm-active-workflow': {
      id: 'crm-active-workflow',
      order: 1,
      kind: 'workflow',
      systemPath: 'sales.crm',
      status: 'active',
      codeRefs: []
    },
    'crm-archived-workflow': {
      id: 'crm-archived-workflow',
      order: 2,
      kind: 'workflow',
      systemPath: 'sales.crm',
      status: 'archived',
      codeRefs: []
    }
  },
  knowledge: {},
  topology: {
    version: 1,
    relationships: {
      'crm-active-uses-sales': {
        from: { kind: 'resource', id: 'crm-active-workflow' },
        kind: 'uses',
        to: { kind: 'system', id: 'sales' }
      },
      'archived-resource-edge': {
        from: { kind: 'resource', id: 'crm-archived-workflow' },
        kind: 'uses',
        to: { kind: 'system', id: 'sales' }
      }
    }
  } satisfies OmTopologyDomain
} as OrganizationModel

describe('getSystemDeprecationDependents', () => {
  it('enumerates active resources and topology edges for a system tree', () => {
    const dependents = getSystemDeprecationDependents(model, 'sales', { includeDescendants: true })

    expect(dependents.resources.map((resource) => resource.id)).toEqual(['crm-active-workflow'])
    expect(dependents.topologyEdges.map((edge) => edge.id).sort()).toEqual([
      'archived-resource-edge',
      'crm-active-uses-sales'
    ])
  })
})
