import { After , When } from "@cucumber/cucumber" ; import { Attribute , Thing } from "../../../../dist" ; import { RootLabel , ScopedLabel } from "../../config/Parameters" ; import { tx } from "../../connection/ConnectionStepsBase" ; import { assertThrows } from "../../util/Util" ; import { getThingType } from "../type/thingtype/ThingTypeSteps" ; import assert = require ( "assert" ) ; export const things = new Map < string , Thing > ( ) ; export const get = ( name ) => things . get ( name ) ; export const put = ( name , thing ) => things . set ( name , thing ) ; export const clearThings = ( ) => things . clear ( ) ; After ( clearThings ) ; When ( "entity/attribute/relation {var} is null: {bool}" , async ( thingName , isNull ) => { if ( things . has ( thingName ) ) assert . ok ( ( things . get ( thingName ) === null ) === isNull ) ; else assert . ok ( isNull ) ; } ) ; When ( "entity/attribute/relation {var} is deleted: {bool}" , async ( thingName , isDeleted ) => { assert . ok ( isDeleted === await get ( thingName ) . asRemote ( tx ( ) ) . isDeleted ( ) ) ; } ) ; When ( "{root_label} {var} has type: {type_label}" , async ( rootLabel , thingName , label ) => { const desiredType = await getThingType ( rootLabel , label ) ; assert ( get ( thingName ) . type . equals ( desiredType ) ) ; } ) ; When ( "delete entity:/attribute:/relation: {var}" , async ( thingName ) => { await get ( thingName ) . asRemote ( tx ( ) ) . delete ( ) ; } ) ; When ( "entity/attribute/relation {var} set has: {var}" , async ( thingName , attributeName ) => { await get ( thingName ) . asRemote ( tx ( ) ) . setHas ( get ( attributeName ) . asAttribute ( ) ) ; } ) ; When ( "entity/attribute/relation {var} set has: {var}; throws exception" , async ( thingName , attributeName ) => { await assertThrows ( async ( ) => get ( thingName ) . asRemote ( tx ( ) ) . setHas ( get ( attributeName ) . asAttribute ( ) ) ) ; } ) ; When ( "entity/attribute/relation {var} unset has: {var}" , async ( thingName , attributeName ) => { await get ( thingName ) . asRemote ( tx ( ) ) . unsetHas ( get ( attributeName ) . asAttribute ( ) ) ; } ) ; When ( "entity/attribute/relation {var} get keys contain: {var}" , async ( thingName , attributeName ) => { for await ( const keyAttribute of get ( thingName ) . asRemote ( tx ( ) ) . getHas ( true ) ) { if ( keyAttribute . equals ( get ( attributeName ) ) ) return ; } assert . fail ( ) ; } ) ; When ( "entity/attribute/relation {var} get keys do not contain: {var}" , async ( thingName , attributeName ) => { for await ( const keyAttribute of get ( thingName ) . asRemote ( tx ( ) ) . getHas ( true ) ) { if ( keyAttribute . equals ( get ( attributeName ) ) ) assert . fail ( ) ; } } ) ; When ( "entity/attribute/relation {var} get attributes contain: {var}" , async ( thingName , attributeName ) => { for await ( const attribute of get ( thingName ) . asRemote ( tx ( ) ) . getHas ( ) ) { if ( attribute . equals ( get ( attributeName ) ) ) return ; } assert . fail ( ) ; } ) ; When ( "entity/attribute/relation {var} get attributes do not contain: {var}" , async ( thingName , attributeName ) => { for await ( const attribute of get ( thingName ) . asRemote ( tx ( ) ) . getHas ( ) ) { if ( attribute . equals ( get ( attributeName ) ) ) assert . fail ( ) ; } } ) ; When ( "entity/attribute/relation {var} get attributes\\({type_label}) contain: {var}" , async ( thingName , typeLabel , attributeName ) => { const type = await tx ( ) . concepts . getAttributeType ( typeLabel ) ; for await ( const attribute of get ( thingName ) . asRemote ( tx ( ) ) . getHas ( [ type ] ) ) { if ( attribute . equals ( get ( attributeName ) ) ) return ; } assert . fail ( ) ; } ) ; When ( "entity/attribute/relation {var} get attributes\\({type_label}) as\\(boolean) contain: {var}" , async ( thingName , typeLabel , attributeName ) => { const type = ( await tx ( ) . concepts . getAttributeType ( typeLabel ) ) . asBoolean ( ) ; for await ( const attribute of get ( thingName ) . asRemote ( tx ( ) ) . getHas ( type ) ) { if ( attribute . equals ( get ( attributeName ) ) ) return ; } assert . fail ( ) ; } ) ; When ( "entity/attribute/relation {var} get attributes\\({type_label}) as\\(long) contain: {var}" , async ( thingName , typeLabel , attributeName ) => { const type = ( await tx ( ) . concepts . getAttributeType ( typeLabel ) ) . asLong ( ) ; for await ( const attribute of get ( thingName ) . asRemote ( tx ( ) ) . getHas ( type ) ) { if ( attribute . equals ( get ( attributeName ) ) ) return ; } assert . fail ( ) ; } ) ; When ( "entity/attribute/relation {var} get attributes\\({type_label}) as\\(double) contain: {var}" , async ( thingName , typeLabel , attributeName ) => { const type = ( await tx ( ) . concepts . getAttributeType ( typeLabel ) ) . asDouble ( ) ; for await ( const attribute of get ( thingName ) . asRemote ( tx ( ) ) . getHas ( type ) ) { if ( attribute . equals ( get ( attributeName ) ) ) return ; } assert . fail ( ) ; } ) ; When ( "entity/attribute/relation {var} get attributes\\({type_label}) as\\(string) contain: {var}" , async ( thingName , typeLabel , attributeName ) => { const type = ( await tx ( ) . concepts . getAttributeType ( typeLabel ) ) . asString ( ) ; for await ( const attribute of get ( thingName ) . asRemote ( tx ( ) ) . getHas ( type ) ) { if ( attribute . equals ( get ( attributeName ) ) ) return ; } assert . fail ( ) ; } ) ; When ( "entity/attribute/relation {var} get attributes\\({type_label}) as\\(datetime) contain: {var}" , async ( thingName , typeLabel , attributeName ) => { const type = ( await tx ( ) . concepts . getAttributeType ( typeLabel ) ) . asDateTime ( ) ; for await ( const attribute of get ( thingName ) . asRemote ( tx ( ) ) . getHas ( type ) ) { if ( attribute . equals ( get ( attributeName ) ) ) return ; } assert . fail ( ) ; } ) ; When ( "entity/attribute/relation {var} get attributes\\({type_label}) do not contain: {var}" , async ( thingName , typeLabel , attributeName ) => { const type = await tx ( ) . concepts . getAttributeType ( typeLabel ) ; for await ( const attribute of get ( thingName ) . asRemote ( tx ( ) ) . getHas ( [ type ] ) ) { if ( attribute . equals ( get ( attributeName ) ) ) assert . fail ( ) ; } } ) ; When ( "entity/attribute/relation {var} get attributes\\({type_label}) as\\(boolean) do not contain: {var}" , async ( thingName , typeLabel , attributeName ) => { const type = ( await tx ( ) . concepts . getAttributeType ( typeLabel ) ) . asBoolean ( ) ; for await ( const attribute of get ( thingName ) . asRemote ( tx ( ) ) . getHas ( type ) ) { if ( attribute . equals ( get ( attributeName ) ) ) assert . fail ( ) ; } } ) ; When ( "entity/attribute/relation {var} get attributes\\({type_label}) as\\(long) do not contain: {var}" , async ( thingName , typeLabel , attributeName ) => { const type = ( await tx ( ) . concepts . getAttributeType ( typeLabel ) ) . asLong ( ) ; for await ( const attribute of get ( thingName ) . asRemote ( tx ( ) ) . getHas ( type ) ) { if ( attribute . equals ( get ( attributeName ) ) ) assert . fail ( ) ; } } ) ; When ( "entity/attribute/relation {var} get attributes\\({type_label}) as\\(double) do not contain: {var}" , async ( thingName , typeLabel , attributeName ) => { const type = ( await tx ( ) . concepts . getAttributeType ( typeLabel ) ) . asDouble ( ) ; for await ( const attribute of get ( thingName ) . asRemote ( tx ( ) ) . getHas ( type ) ) { if ( attribute . equals ( get ( attributeName ) ) ) assert . fail ( ) ; } } ) ; When ( "entity/attribute/relation {var} get attributes\\({type_label}) as\\(string) do not contain: {var}" , async ( thingName , typeLabel , attributeName ) => { const type = ( await tx ( ) . concepts . getAttributeType ( typeLabel ) ) . asString ( ) ; for await ( const attribute of get ( thingName ) . asRemote ( tx ( ) ) . getHas ( type ) ) { if ( attribute . equals ( get ( attributeName ) ) ) assert . fail ( ) ; } } ) ; When ( "entity/attribute/relation {var} get attributes\\({type_label}) as\\(datetime) do not contain: {var}" , async ( thingName , typeLabel , attributeName ) => { const type = ( await tx ( ) . concepts . getAttributeType ( typeLabel ) ) . asDateTime ( ) ; for await ( const attribute of get ( thingName ) . asRemote ( tx ( ) ) . getHas ( type ) ) { if ( attribute . equals ( get ( attributeName ) ) ) assert . fail ( ) ; } } ) ; When ( "entity/attribute/relation {var} get relations\\({scoped_label}) contain: {var}" , async ( thingName , scopedLabel , relationName ) => { const roleType = await ( await tx ( ) . concepts . getRelationType ( scopedLabel . scope ) ) . asRemote ( tx ( ) ) . getRelates ( scopedLabel . role ) ; for await ( const relation of get ( thingName ) . asRemote ( tx ( ) ) . getRelations ( [ roleType ] ) ) { if ( relation . equals ( get ( relationName ) ) ) return ; } assert . fail ( ) ; } ) ; When ( "entity/attribute/relation {var} get relations contain: {var}" , async ( thingName , relationName ) => { for await ( const relation of get ( thingName ) . asRemote ( tx ( ) ) . getRelations ( ) ) { if ( relation . equals ( get ( relationName ) ) ) return ; } assert . fail ( ) ; } ) ; When ( "entity/attribute/relation {var} get relations\\({scoped_label}) do not contain: {var}" , async ( thingName , scopedLabel , relationName ) => { const roleType = await ( await tx ( ) . concepts . getRelationType ( scopedLabel . scope ) ) . asRemote ( tx ( ) ) . getRelates ( scopedLabel . role ) ; for await ( const relation of get ( thingName ) . asRemote ( tx ( ) ) . getRelations ( [ roleType ] ) ) { if ( relation . equals ( get ( relationName ) ) ) assert . fail ( ) ; } } ) ; When ( "entity/attribute/relation {var} get relations do not contain: {var}" , async ( thingName , relationName ) => { for await ( const relation of get ( thingName ) . asRemote ( tx ( ) ) . getRelations ( ) ) { if ( relation . equals ( get ( relationName ) ) ) assert . fail ( ) ; } } ) ; When ( "root\\(thing) get instances count: {int}" , async ( count ) => { assert . strictEqual ( ( await ( await tx ( ) . concepts . getRootThingType ( ) ) . asRemote ( tx ( ) ) . getInstances ( ) . collect ( ) ) . length , count ) ; } ) ; When ( "root\\(thing) get instances contain: {var}" , async ( variableName ) => { for await ( const instance of ( await tx ( ) . concepts . getRootThingType ( ) ) . asRemote ( tx ( ) ) . getInstances ( ) ) { if ( instance . equals ( get ( variableName ) ) ) return ; } assert . fail ( ) ; } ) ; When ( "root\\(thing) get instances is empty" , async ( ) => { assert . strictEqual ( ( await ( await tx ( ) . concepts . getRootThingType ( ) ) . asRemote ( tx ( ) ) . getInstances ( ) . collect ( ) ) . length , 0 ) ; } ) ;