import { test } from '@japa/runner'
import {
  DomainAssociation,
  AmfShapes,
  DomainAssociationKind,
  Thing,
  AssociationBinding,
  DataDomain,
  DataFormat,
} from '../../../src/index.js'

test.group('DomainAssociation.createSchema()', () => {
  test('creates a valid schema with default values', ({ assert }) => {
    const schema = DomainAssociation.createSchema()
    assert.equal(schema.kind, DomainAssociationKind)
    assert.typeOf(schema.key, 'string')
    assert.isDefined(schema.info)
    assert.deepEqual(schema.info, Thing.fromJSON({ name: 'new_association' }).toJSON())
    assert.isUndefined(schema.schema)
    assert.isUndefined(schema.multiple)
    assert.isUndefined(schema.required)
    assert.isUndefined(schema.bindings)
    assert.isUndefined(schema.targets)
  })

  test('creates a schema with provided key', ({ assert }) => {
    const key = 'test-key'
    const schema = DomainAssociation.createSchema({ key })
    assert.equal(schema.key, key)
  })

  test('creates a schema with provided info', ({ assert }) => {
    const info = { name: 'Test Association', description: 'Test description' }
    const schema = DomainAssociation.createSchema({ info })
    assert.deepEqual(schema.info, Thing.fromJSON(info).toJSON())
  })

  test('creates a schema with provided schema', ({ assert }) => {
    const schemaDefinition: AmfShapes.IApiAssociationShape = { linked: false }
    const schema = DomainAssociation.createSchema({ schema: schemaDefinition })
    assert.deepEqual(schema.schema, schemaDefinition)
  })

  test('creates a schema with provided multiple', ({ assert }) => {
    const schema = DomainAssociation.createSchema({ multiple: true })
    assert.isTrue(schema.multiple)
  })

  test('creates a schema with provided required', ({ assert }) => {
    const schema = DomainAssociation.createSchema({ required: true })
    assert.isTrue(schema.required)
  })

  test('creates a schema with provided bindings', ({ assert }) => {
    const bindings: AssociationBinding[] = [{ type: 'web', schema: { hidden: true } }]
    const schema = DomainAssociation.createSchema({ bindings })
    assert.deepEqual(schema.bindings, bindings)
  })

  test('creates a schema with provided targets', ({ assert }) => {
    const targets = [{ key: 'target1', domain: 'domain1' }, { key: 'target2' }]
    const schema = DomainAssociation.createSchema({ targets })
    assert.deepEqual(schema.targets, targets)
  })

  test('creates a schema with all properties', ({ assert }) => {
    const key = 'test-key'
    const info = { name: 'Test Association', description: 'Test description' }
    const schemaDefinition: AmfShapes.IApiAssociationShape = { linked: false }
    const multiple = true
    const required = true
    const bindings: AssociationBinding[] = [{ type: 'web', schema: { hidden: false } }]
    const targets = [{ key: 'target1', domain: 'domain1' }, { key: 'target2' }]

    const schema = DomainAssociation.createSchema({
      key,
      info,
      schema: schemaDefinition,
      multiple,
      required,
      bindings,
      targets,
    })

    assert.equal(schema.key, key)
    assert.deepEqual(schema.info, Thing.fromJSON(info).toJSON())
    assert.deepEqual(schema.schema, schemaDefinition)
    assert.equal(schema.multiple, multiple)
    assert.equal(schema.required, required)
    assert.deepEqual(schema.bindings, bindings)
    assert.deepEqual(schema.targets, targets)
  })

  test('creates a schema with cloned bindings', ({ assert }) => {
    const bindings: AssociationBinding[] = [{ type: 'web', schema: { hidden: true } }]
    const schema = DomainAssociation.createSchema({ bindings })
    assert.deepEqual(schema.bindings, bindings)
    assert.notStrictEqual(schema.bindings, bindings)
  })

  test('creates a schema with cloned targets', ({ assert }) => {
    const targets = [{ key: 'target1', domain: 'domain1' }, { key: 'target2' }]
    const schema = DomainAssociation.createSchema({ targets })
    assert.deepEqual(schema.targets, targets)
    assert.notStrictEqual(schema.targets, targets)
  })

  test('creates a schema with cloned schema', ({ assert }) => {
    const schemaDefinition: AmfShapes.IApiAssociationShape = { linked: false }
    const schema = DomainAssociation.createSchema({ schema: schemaDefinition })
    assert.deepEqual(schema.schema, schemaDefinition)
    assert.notStrictEqual(schema.schema, schemaDefinition)
  })
})

test.group('DomainAssociation.constructor()', () => {
  test('creates a valid instance with default values', ({ assert }) => {
    const dataDomain = new DataDomain()
    const parentKey = 'test-parent'
    const association = new DomainAssociation(dataDomain, parentKey)
    assert.equal(association.kind, DomainAssociationKind)
    assert.typeOf(association.key, 'string')
    assert.deepEqual(association.info.toJSON(), Thing.fromJSON({ name: 'new_association' }).toJSON())
    assert.isUndefined(association.schema)
    assert.isUndefined(association.multiple)
    assert.isUndefined(association.required)
    assert.deepEqual(association.bindings, [])
    assert.deepEqual(association.targets, [])
  })

  test('creates an instance with provided key', ({ assert }) => {
    const dataDomain = new DataDomain()
    const parentKey = 'test-parent'
    const key = 'test-key'
    const association = new DomainAssociation(dataDomain, parentKey, { key })
    assert.equal(association.key, key)
  })

  test('creates an instance with provided info', ({ assert }) => {
    const dataDomain = new DataDomain()
    const parentKey = 'test-parent'
    const info = { name: 'Test Association', description: 'Test description' }
    const association = new DomainAssociation(dataDomain, parentKey, { info })
    assert.deepEqual(association.info.toJSON(), Thing.fromJSON(info).toJSON())
  })

  test('creates an instance with provided schema', ({ assert }) => {
    const dataDomain = new DataDomain()
    const parentKey = 'test-parent'
    const schemaDefinition: AmfShapes.IApiAssociationShape = { linked: false }
    const association = new DomainAssociation(dataDomain, parentKey, { schema: schemaDefinition })
    assert.deepEqual(association.schema, schemaDefinition)
  })

  test('creates an instance with provided multiple', ({ assert }) => {
    const dataDomain = new DataDomain()
    const parentKey = 'test-parent'
    const association = new DomainAssociation(dataDomain, parentKey, { multiple: true })
    assert.isTrue(association.multiple)
  })

  test('creates an instance with provided required', ({ assert }) => {
    const dataDomain = new DataDomain()
    const parentKey = 'test-parent'
    const association = new DomainAssociation(dataDomain, parentKey, { required: true })
    assert.isTrue(association.required)
  })

  test('creates an instance with provided bindings', ({ assert }) => {
    const dataDomain = new DataDomain()
    const parentKey = 'test-parent'
    const bindings: AssociationBinding[] = [{ type: 'web', schema: { hidden: true } }]
    const association = new DomainAssociation(dataDomain, parentKey, { bindings })
    assert.deepEqual(association.bindings, bindings)
  })

  test('creates an instance with provided targets', ({ assert }) => {
    const dataDomain = new DataDomain()
    const parentKey = 'test-parent'
    const targets = [{ key: 'target1', domain: 'domain1' }, { key: 'target2' }]
    const association = new DomainAssociation(dataDomain, parentKey, { targets })
    assert.deepEqual(association.targets, targets)
  })

  test('creates an instance with all properties', ({ assert }) => {
    const dataDomain = new DataDomain()
    const parentKey = 'test-parent'
    const key = 'test-key'
    const info = { name: 'Test Association', description: 'Test description' }
    const schemaDefinition: AmfShapes.IApiAssociationShape = { linked: false }
    const multiple = true
    const required = true
    const bindings: AssociationBinding[] = [{ type: 'web', schema: { hidden: false } }]
    const targets = [{ key: 'target1', domain: 'domain1' }, { key: 'target2' }]

    const association = new DomainAssociation(dataDomain, parentKey, {
      key,
      info,
      schema: schemaDefinition,
      multiple,
      required,
      bindings,
      targets,
    })

    assert.equal(association.key, key)
    assert.deepEqual(association.info.toJSON(), Thing.fromJSON(info).toJSON())
    assert.deepEqual(association.schema, schemaDefinition)
    assert.equal(association.multiple, multiple)
    assert.equal(association.required, required)
    assert.deepEqual(association.bindings, bindings)
    assert.deepEqual(association.targets, targets)
  })

  test('creates an instance with cloned bindings', ({ assert }) => {
    const dataDomain = new DataDomain()
    const parentKey = 'test-parent'
    const bindings: AssociationBinding[] = [{ type: 'web', schema: { hidden: true } }]
    const association = new DomainAssociation(dataDomain, parentKey, { bindings })
    assert.deepEqual(association.bindings, bindings)
    assert.notStrictEqual(association.bindings, bindings)
  })

  test('creates an instance with cloned targets', ({ assert }) => {
    const dataDomain = new DataDomain()
    const parentKey = 'test-parent'
    const targets = [{ key: 'target1', domain: 'domain1' }, { key: 'target2' }]
    const association = new DomainAssociation(dataDomain, parentKey, { targets })
    assert.deepEqual(association.targets, targets)
    assert.notStrictEqual(association.targets, targets)
  })

  test('creates an instance with cloned schema', ({ assert }) => {
    const dataDomain = new DataDomain()
    const parentKey = 'test-parent'
    const schemaDefinition: AmfShapes.IApiAssociationShape = { linked: false }
    const association = new DomainAssociation(dataDomain, parentKey, { schema: schemaDefinition })
    assert.deepEqual(association.schema, schemaDefinition)
    assert.notStrictEqual(association.schema, schemaDefinition)
  })
})

test.group('DomainAssociation.toJSON()', () => {
  test('returns a valid JSON representation with default values', ({ assert }) => {
    const dataDomain = new DataDomain()
    const parentKey = 'test-parent'
    const association = new DomainAssociation(dataDomain, parentKey)
    const json = association.toJSON()
    assert.equal(json.kind, DomainAssociationKind)
    assert.typeOf(json.key, 'string')
    assert.deepEqual(json.info, Thing.fromJSON({ name: 'new_association' }).toJSON())
    assert.isUndefined(json.schema)
    assert.isUndefined(json.multiple)
    assert.isUndefined(json.required)
    assert.isUndefined(json.bindings)
    assert.isUndefined(json.targets)
  })

  test('returns a JSON representation with provided key', ({ assert }) => {
    const dataDomain = new DataDomain()
    const parentKey = 'test-parent'
    const key = 'test-key'
    const association = new DomainAssociation(dataDomain, parentKey, { key })
    const json = association.toJSON()
    assert.equal(json.key, key)
  })

  test('returns a JSON representation with provided info', ({ assert }) => {
    const dataDomain = new DataDomain()
    const parentKey = 'test-parent'
    const info = { name: 'Test Association', description: 'Test description' }
    const association = new DomainAssociation(dataDomain, parentKey, { info })
    const json = association.toJSON()
    assert.deepEqual(json.info, Thing.fromJSON(info).toJSON())
  })

  test('returns a JSON representation with provided schema', ({ assert }) => {
    const dataDomain = new DataDomain()
    const parentKey = 'test-parent'
    const schemaDefinition: AmfShapes.IApiAssociationShape = { linked: false }
    const association = new DomainAssociation(dataDomain, parentKey, { schema: schemaDefinition })
    const json = association.toJSON()
    assert.deepEqual(json.schema, schemaDefinition)
    assert.notStrictEqual(json.schema, schemaDefinition)
  })

  test('returns a JSON representation with provided multiple', ({ assert }) => {
    const dataDomain = new DataDomain()
    const parentKey = 'test-parent'
    const association = new DomainAssociation(dataDomain, parentKey, { multiple: true })
    const json = association.toJSON()
    assert.isTrue(json.multiple)
  })

  test('returns a JSON representation with provided required', ({ assert }) => {
    const dataDomain = new DataDomain()
    const parentKey = 'test-parent'
    const association = new DomainAssociation(dataDomain, parentKey, { required: true })
    const json = association.toJSON()
    assert.isTrue(json.required)
  })

  test('returns a JSON representation with provided bindings', ({ assert }) => {
    const dataDomain = new DataDomain()
    const parentKey = 'test-parent'
    const bindings: AssociationBinding[] = [{ type: 'web', schema: { hidden: true } }]
    const association = new DomainAssociation(dataDomain, parentKey, { bindings })
    const json = association.toJSON()
    assert.deepEqual(json.bindings, bindings)
    assert.notStrictEqual(json.bindings, bindings)
  })

  test('returns a JSON representation with provided targets', ({ assert }) => {
    const dataDomain = new DataDomain()
    const parentKey = 'test-parent'
    const targets = [{ key: 'target1', domain: 'domain1' }, { key: 'target2' }]
    const association = new DomainAssociation(dataDomain, parentKey, { targets })
    const json = association.toJSON()
    assert.deepEqual(json.targets, targets)
    assert.notStrictEqual(json.targets, targets)
  })

  test('returns a JSON representation with all properties', ({ assert }) => {
    const dataDomain = new DataDomain()
    const parentKey = 'test-parent'
    const key = 'test-key'
    const info = { name: 'Test Association', description: 'Test description' }
    const schemaDefinition: AmfShapes.IApiAssociationShape = { linked: false }
    const multiple = true
    const required = true
    const bindings: AssociationBinding[] = [{ type: 'web', schema: { hidden: false } }]
    const targets = [{ key: 'target1', domain: 'domain1' }, { key: 'target2' }]

    const association = new DomainAssociation(dataDomain, parentKey, {
      key,
      info,
      schema: schemaDefinition,
      multiple,
      required,
      bindings,
      targets,
    })

    const json = association.toJSON()

    assert.equal(json.key, key)
    assert.deepEqual(json.info, Thing.fromJSON(info).toJSON())
    assert.deepEqual(json.schema, schemaDefinition)
    assert.equal(json.multiple, multiple)
    assert.equal(json.required, required)
    assert.deepEqual(json.bindings, bindings)
    assert.deepEqual(json.targets, targets)
  })

  test('returns a JSON representation with cloned bindings', ({ assert }) => {
    const dataDomain = new DataDomain()
    const parentKey = 'test-parent'
    const bindings: AssociationBinding[] = [{ type: 'web', schema: { hidden: true } }]
    const association = new DomainAssociation(dataDomain, parentKey, { bindings })
    const json = association.toJSON()
    assert.deepEqual(json.bindings, bindings)
    assert.notStrictEqual(json.bindings, bindings)
  })

  test('returns a JSON representation with cloned targets', ({ assert }) => {
    const dataDomain = new DataDomain()
    const parentKey = 'test-parent'
    const targets = [{ key: 'target1', domain: 'domain1' }, { key: 'target2' }]
    const association = new DomainAssociation(dataDomain, parentKey, { targets })
    const json = association.toJSON()
    assert.deepEqual(json.targets, targets)
    assert.notStrictEqual(json.targets, targets)
  })

  test('returns a JSON representation with cloned schema', ({ assert }) => {
    const dataDomain = new DataDomain()
    const parentKey = 'test-parent'
    const schemaDefinition: AmfShapes.IApiAssociationShape = { linked: false }
    const association = new DomainAssociation(dataDomain, parentKey, { schema: schemaDefinition })
    const json = association.toJSON()
    assert.deepEqual(json.schema, schemaDefinition)
    assert.notStrictEqual(json.schema, schemaDefinition)
  })

  test('returns a JSON representation with cloned schema when it is set after creation', ({ assert }) => {
    const dataDomain = new DataDomain()
    const parentKey = 'test-parent'
    const association = new DomainAssociation(dataDomain, parentKey)
    const schemaDefinition: AmfShapes.IApiAssociationShape = { linked: false }
    association.schema = schemaDefinition
    const json = association.toJSON()
    assert.deepEqual(json.schema, schemaDefinition)
    assert.notStrictEqual(json.schema, schemaDefinition)
  })

  test('returns a JSON representation with cloned bindings when it is set after creation', ({ assert }) => {
    const dataDomain = new DataDomain()
    const parentKey = 'test-parent'
    const association = new DomainAssociation(dataDomain, parentKey)
    const bindings: AssociationBinding[] = [{ type: 'web', schema: { hidden: true } }]
    association.bindings = bindings
    const json = association.toJSON()
    assert.deepEqual(json.bindings, bindings)
    assert.notStrictEqual(json.bindings, bindings)
  })

  test('returns a JSON representation with cloned targets when it is set after creation', ({ assert }) => {
    const dataDomain = new DataDomain()
    const parentKey = 'test-parent'
    const association = new DomainAssociation(dataDomain, parentKey)
    const targets = [{ key: 'target1', domain: 'domain1' }, { key: 'target2' }]
    association.targets = targets
    const json = association.toJSON()
    assert.deepEqual(json.targets, targets)
    assert.notStrictEqual(json.targets, targets)
  })
})

test.group('DomainAssociation.getParentInstance()', () => {
  test('returns the parent entity when the association is a child', ({ assert }) => {
    const dataDomain = new DataDomain()
    const model = dataDomain.addModel()
    const entity = model.addEntity()
    const association = entity.addAssociation()
    const parent = association.getParentInstance()
    assert.deepEqual(parent, entity)
  })

  test('returns undefined when the association has no parent', ({ assert }) => {
    const dataDomain = new DataDomain()
    const association = new DomainAssociation(dataDomain, 'non-existent-entity')
    dataDomain.graph.setNode(association.key, association)
    const parent = association.getParentInstance()
    assert.isUndefined(parent)
  })

  test('returns the correct parent when there are multiple levels of nesting', ({ assert }) => {
    const dataDomain = new DataDomain()
    const model = dataDomain.addModel()
    const entity = model.addEntity()
    const association = entity.addAssociation()
    const parent = association.getParentInstance()
    assert.deepEqual(parent, entity)
  })

  test('returns undefined when the parent entity is removed from the graph', ({ assert }) => {
    const dataDomain = new DataDomain()
    const model = dataDomain.addModel()
    const entity = model.addEntity()
    const association = entity.addAssociation()
    dataDomain.graph.removeNode(entity.key)
    const parent = association.getParentInstance()
    assert.isUndefined(parent)
  })
})

test.group('DomainAssociation.remove()', () => {
  test('removes the association from the parent entity', ({ assert }) => {
    const dataDomain = new DataDomain()
    const model = dataDomain.addModel()
    const entity = model.addEntity()
    const association = entity.addAssociation()
    association.remove()
    assert.isFalse(dataDomain.graph.hasNode(association.key))
    assert.deepEqual(entity.fields, [])
  })

  test('throws if the parent entity does not have the association', ({ assert }) => {
    const dataDomain = new DataDomain()
    const model = dataDomain.addModel()
    const entity1 = model.addEntity()
    const entity2 = model.addEntity()
    const association = entity1.addAssociation()
    assert.throws(() => {
      entity2.removeAssociation(association.key)
    }, `Unable to find a connection between this entity and the ${association.key} association`)
  })

  test('removes the association from the graph', ({ assert }) => {
    const dataDomain = new DataDomain()
    const model = dataDomain.addModel()
    const entity = model.addEntity()
    const association = entity.addAssociation()
    association.remove()
    assert.isFalse(dataDomain.graph.hasNode(association.key))
  })

  test('removes the association from the parent entity fields', ({ assert }) => {
    const dataDomain = new DataDomain()
    const model = dataDomain.addModel()
    const entity = model.addEntity()
    const association = entity.addAssociation()
    association.remove()
    assert.deepEqual(entity.fields, [])
  })

  test('removes the association from the graph when it has targets', ({ assert }) => {
    const dataDomain = new DataDomain()
    const model = dataDomain.addModel()
    const entity1 = model.addEntity()
    const entity2 = model.addEntity()
    const association = entity1.addAssociation()
    association.addTarget(entity2)
    association.remove()
    assert.isFalse(dataDomain.graph.hasNode(association.key))
    assert.isFalse(dataDomain.graph.hasEdge(association.key, entity2.key))
  })
})

test.group('DomainAssociation.ensureSchema()', () => {
  test('creates the schema object if it does not exist', ({ assert }) => {
    const dataDomain = new DataDomain()
    const association = new DomainAssociation(dataDomain, 'test-parent')
    assert.isUndefined(association.schema)
    const schema = association.ensureSchema()
    assert.isDefined(association.schema)
    assert.deepEqual(schema, {})
    assert.deepEqual(schema, schema)
  })

  test('returns the existing schema object if it exists', ({ assert }) => {
    const dataDomain = new DataDomain()
    const schemaDefinition: AmfShapes.IApiAssociationShape = { linked: false }
    const association = new DomainAssociation(dataDomain, 'test-parent', { schema: schemaDefinition })
    const originalSchema = association.schema
    const schema = association.ensureSchema()
    assert.deepEqual(schema, schemaDefinition)
    assert.strictEqual(schema, originalSchema)
  })

  test('does not modify the existing schema object', ({ assert }) => {
    const dataDomain = new DataDomain()
    const schemaDefinition: AmfShapes.IApiAssociationShape = { linked: false }
    const association = new DomainAssociation(dataDomain, 'test-parent', { schema: schemaDefinition })
    const originalSchema = association.schema
    association.ensureSchema()
    assert.deepEqual(association.schema, schemaDefinition)
    assert.strictEqual(association.schema, originalSchema)
  })
})

test.group('DomainAssociation.isValidAttribute()', () => {
  test('returns true for valid data association attributes', ({ assert }) => {
    const validAttributes: DataFormat.DomainAttributeAttribute[] = ['required', 'multiple']
    validAttributes.forEach((attribute) => {
      assert.isTrue(DomainAssociation.isValidAttribute(attribute), `Attribute "${attribute}" should be valid`)
    })
  })

  test('returns false for invalid data association attributes', ({ assert }) => {
    assert.isFalse(DomainAssociation.isValidAttribute('invalid-attribute'))
    assert.isFalse(DomainAssociation.isValidAttribute(123))
    assert.isFalse(DomainAssociation.isValidAttribute(true))
    assert.isFalse(DomainAssociation.isValidAttribute({}))
    assert.isFalse(DomainAssociation.isValidAttribute([]))
    assert.isFalse(DomainAssociation.isValidAttribute(null))
    assert.isFalse(DomainAssociation.isValidAttribute(undefined))
  })

  test('returns false for non-string values', ({ assert }) => {
    assert.isFalse(DomainAssociation.isValidAttribute(123))
    assert.isFalse(DomainAssociation.isValidAttribute(true))
    assert.isFalse(DomainAssociation.isValidAttribute({}))
    assert.isFalse(DomainAssociation.isValidAttribute([]))
    assert.isFalse(DomainAssociation.isValidAttribute(null))
    assert.isFalse(DomainAssociation.isValidAttribute(undefined))
  })

  test('returns false for empty string', ({ assert }) => {
    assert.isFalse(DomainAssociation.isValidAttribute(''))
  })
})

test.group('DomainAssociation.getWebBinding()', () => {
  test('creates web bindings if they do not exist', ({ assert }) => {
    const dataDomain = new DataDomain()
    const association = new DomainAssociation(dataDomain, 'test-parent')
    const webBindings = association.getWebBinding()
    assert.isDefined(webBindings)
    assert.deepEqual(webBindings, {})
    assert.lengthOf(association.bindings, 1)
    assert.equal(association.bindings[0].type, 'web')
  })

  test('returns existing web bindings if they exist', ({ assert }) => {
    const dataDomain = new DataDomain()
    const association = new DomainAssociation(dataDomain, 'test-parent', {
      bindings: [{ type: 'web', schema: { hidden: true } }],
    })
    const webBindings = association.getWebBinding()
    assert.isDefined(webBindings)
    assert.deepEqual(webBindings, { hidden: true })
    assert.lengthOf(association.bindings, 1)
    assert.equal(association.bindings[0].type, 'web')
  })

  test('does not modify existing web bindings object', ({ assert }) => {
    const dataDomain = new DataDomain()
    const association = new DomainAssociation(dataDomain, 'test-parent', {
      bindings: [{ type: 'web', schema: { hidden: true } }],
    })
    const originalBindings = association.bindings[0].schema
    association.getWebBinding()
    assert.deepEqual(association.bindings[0].schema, { hidden: true })
    assert.deepEqual(association.bindings[0].schema, originalBindings)
  })

  test('notifies change when creating new bindings', async ({ assert }) => {
    const dataDomain = new DataDomain()
    const association = new DomainAssociation(dataDomain, 'test-parent')
    const webBindings = association.getWebBinding()
    assert.isDefined(webBindings)
    await assert.dispatches(dataDomain, 'change', { timeout: 20 })
  })
})

test.group('DomainAssociation.readBinding()', () => {
  test('returns undefined if no binding exists for the given type', ({ assert }) => {
    const dataDomain = new DataDomain()
    const association = new DomainAssociation(dataDomain, 'test-parent')
    const binding = association.readBinding('web')
    assert.isUndefined(binding)
  })

  test('returns the web binding schema if it exists', ({ assert }) => {
    const dataDomain = new DataDomain()
    const association = new DomainAssociation(dataDomain, 'test-parent', {
      bindings: [{ type: 'web', schema: { hidden: true } }],
    })
    const webBindings = association.readBinding('web')
    assert.deepEqual(webBindings, { hidden: true })
  })

  test('returns undefined if the binding exists but has no schema', ({ assert }) => {
    const dataDomain = new DataDomain()
    const association = new DomainAssociation(dataDomain, 'test-parent', {
      // @ts-expect-error Testing undefined schema
      bindings: [{ type: 'web', schema: undefined }],
    })
    const webBindings = association.readBinding('web')
    assert.isUndefined(webBindings)
  })
})
