@@ -20,12 +20,13 @@ import Servient, { ExposedThing, Content } from "@node-wot/core";
2020
2121import { suite , test } from "@testdeck/mocha" ;
2222import { expect , should } from "chai" ;
23- import { DataSchemaValue , InteractionInput , InteractionOptions } from "wot-typescript-definitions" ;
23+ import { DataSchemaValue , InteractionInput , InteractionOptions , ThingDescription } from "wot-typescript-definitions" ;
2424import * as TD from "@node-wot/td-tools" ;
2525import CoapServer from "../src/coap-server" ;
2626import { CoapClient } from "../src/coap" ;
2727import { Readable } from "stream" ;
2828import { IncomingMessage , registerFormat , request } from "coap" ;
29+ import { filterEventOperations , filterPropertyObserveOperations , filterPropertyReadWriteOperations } from "../src/util" ;
2930
3031// should must be called to augment all variables
3132should ( ) ;
@@ -634,4 +635,91 @@ class CoapServerTest {
634635
635636 await coapServer . stop ( ) ;
636637 }
638+
639+ @test async "should add the cov:observe subprotocol value to obervable properties and events " ( ) {
640+ const coapServer = new CoapServer ( { port : 5683 } ) ;
641+ const servient = new Servient ( ) ;
642+ servient . addServer ( coapServer ) ;
643+
644+ await coapServer . start ( servient ) ;
645+
646+ const covObserveThing = new ExposedThing ( servient , {
647+ title : "Test" ,
648+ properties : {
649+ observableProperty : {
650+ observable : true ,
651+ } ,
652+ nonObservableProperty : { } ,
653+ } ,
654+ events : {
655+ testEvent : { } ,
656+ } ,
657+ } ) ;
658+
659+ await coapServer . expose ( covObserveThing ) ;
660+
661+ await new Promise < void > ( ( resolve ) => {
662+ const req = request ( {
663+ host : "localhost" ,
664+ pathname : "test" ,
665+ port : 5683 ,
666+ method : "GET" ,
667+ } ) ;
668+ req . on ( "response" , ( res : IncomingMessage ) => {
669+ const payload = res . payload . toString ( ) ;
670+ const td = JSON . parse ( payload ) as ThingDescription ;
671+
672+ for ( const property of Object . values ( td . properties ! ) ) {
673+ let observeOpValueFormCount = 0 ;
674+ for ( const form of property . forms ) {
675+ const opValues = form . op ! ;
676+ expect ( opValues . length ) . to . be . greaterThan ( 0 ) ;
677+
678+ const observeOpValueCount = filterPropertyObserveOperations ( opValues as Array < string > ) . length ;
679+ const observeOpValuePresent = observeOpValueCount > 0 ;
680+
681+ if ( observeOpValuePresent ) {
682+ observeOpValueFormCount ++ ;
683+ expect ( form . subprotocol ) . to . eql ( "cov:observe" ) ;
684+ }
685+
686+ const readWriteOpValueCount = filterPropertyReadWriteOperations (
687+ opValues as Array < string >
688+ ) . length ;
689+ const readWriteOpValuePresent = readWriteOpValueCount > 0 ;
690+
691+ // eslint-disable-next-line no-unused-expressions
692+ expect ( observeOpValuePresent && readWriteOpValuePresent ) . to . not . be . true ;
693+
694+ if ( property . observable !== true ) {
695+ expect ( observeOpValueCount ) . to . eql ( 0 ) ;
696+ }
697+ }
698+
699+ if ( property . observable === true ) {
700+ expect ( observeOpValueFormCount ) . to . be . greaterThan ( 0 ) ;
701+ }
702+ }
703+
704+ for ( const event of Object . values ( td . events ! ) ) {
705+ for ( const form of event . forms ) {
706+ const opValues = form . op ! ;
707+ expect ( opValues . length > 0 ) ;
708+
709+ const eventOpValueCount = filterEventOperations ( opValues as Array < string > ) . length ;
710+ const eventOpValueCountPresent = eventOpValueCount > 0 ;
711+
712+ expect ( eventOpValueCountPresent ) ;
713+
714+ expect ( form . subprotocol === "cov:observe" ) ;
715+ }
716+ }
717+
718+ resolve ( ) ;
719+ } ) ;
720+ req . end ( ) ;
721+ } ) ;
722+
723+ await coapServer . stop ( ) ;
724+ }
637725}
0 commit comments