Skip to content

Commit

Permalink
test: add test for exploreDirectory method
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Dec 21, 2023
1 parent 57affb8 commit b292de9
Showing 1 changed file with 78 additions and 34 deletions.
112 changes: 78 additions & 34 deletions packages/core/test/DiscoveryTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,57 @@ import Servient from "../src/servient";
import { Readable } from "stream";
import { expect } from "chai";

const { debug } = createLoggers("core", "DiscoveryTest");

const directoryTdUrl = "test://localhost/.well-known/wot";
const directoryThingsUrl = "test://localhost/things";

const directoryThingDescription = {
"@context": "https://www.w3.org/2022/wot/td/v1.1",
title: "Directory Test TD",
security: "nosec_sc",
securityDefinitions: {
nosec_sc: {
scheme: "nosec",
const { debug, error } = createLoggers("core", "DiscoveryTest");

function createDirectoryTestTd(title: string, thingsPropertyHref: string) {
return {
"@context": "https://www.w3.org/2022/wot/td/v1.1",
title,
security: "nosec_sc",
securityDefinitions: {
nosec_sc: {
scheme: "nosec",
},
},
},
properties: {
things: {
forms: [
{
href: directoryThingsUrl,
},
],
properties: {
things: {
forms: [
{
href: thingsPropertyHref,
},
],
},
},
},
};
};
}

function createDiscoveryContent(td: unknown, contentType: string) {
const buffer = Buffer.from(JSON.stringify(td));
const content = new Content(contentType, Readable.from(buffer));
return content;
}

const directoryTdUrl1 = "test://localhost/.well-known/wot";
const directoryTdUrl2 = "test://[::1]/.well-known/wot";

const directoryTdTitle1 = "Directory Test TD 1";
const directoryTdTitle2 = "Directory Test TD 2";

const directoryThingsUrl1 = "test://localhost/things1";
const directoryThingsUrl2 = "test://localhost/things2";

const directoryThingDescription1 = createDirectoryTestTd(directoryTdTitle1, directoryThingsUrl1);
const directoryThingDescription2 = createDirectoryTestTd(directoryTdTitle2, directoryThingsUrl2);

class TestProtocolClient implements ProtocolClient {
async readResource(form: Form): Promise<Content> {
if (form.href === directoryThingsUrl) {
const buffer = Buffer.from(JSON.stringify([directoryThingDescription]));
const content = new Content("application/ld+json", Readable.from(buffer));
return content;
const href = form.href;

switch (href) {
case directoryThingsUrl1:
return createDiscoveryContent([directoryThingDescription1], "application/ld+json");
case directoryThingsUrl2:
return createDiscoveryContent(["I am an invalid TD!"], "application/ld+json");
}

throw new Error("Invalid URL");
Expand Down Expand Up @@ -80,11 +100,13 @@ class TestProtocolClient implements ProtocolClient {
}

async requestThingDescription(uri: string): Promise<Content> {
if (uri === directoryTdUrl) {
debug(`Found corrent URL ${directoryTdUrl} to fetch directory TD`);
const buffer = Buffer.from(JSON.stringify(directoryThingDescription));
const content = new Content("application/td+json", Readable.from(buffer));
return content;
switch (uri) {
case directoryTdUrl1:
debug(`Found corrent URL ${uri} to fetch directory TD`);
return createDiscoveryContent(directoryThingDescription1, "application/td+json");
case directoryTdUrl2:
debug(`Found corrent URL ${uri} to fetch directory TD`);
return createDiscoveryContent(directoryThingDescription2, "application/td+json");
}

throw Error("Invalid URL");
Expand Down Expand Up @@ -119,17 +141,39 @@ class TestProtocolClientFactory implements ProtocolClientFactory {
}
}

describe("Discovery Tests", () => {
describe.only("Discovery Tests", () => {
it("should be possible to use the exploreDirectory method", async () => {
const servient = new Servient();
servient.addClientFactory(new TestProtocolClientFactory());

const WoT = await servient.start();

const discoveryProcess = await WoT.exploreDirectory(directoryTdUrl1);

let tdCounter = 0;
for await (const thingDescription of discoveryProcess) {
expect(thingDescription.title).to.eql(directoryTdTitle1);
tdCounter++;
}
expect(tdCounter).to.eql(1);
console.log(discoveryProcess.error);
expect(discoveryProcess.error).to.eq(undefined);
});

it("should be possible to use the exploreDirectory method", async () => {
const servient = new Servient();
servient.addClientFactory(new TestProtocolClientFactory());

const WoT = await servient.start();

const discoveryProcess = await WoT.exploreDirectory(directoryTdUrl);
const discoveryProcess = await WoT.exploreDirectory(directoryTdUrl2);

let tdCounter = 0;
for await (const thingDescription of discoveryProcess) {
expect(thingDescription.title === "Directory Test TD");
error(`Encountered unexpected TD with title ${thingDescription.title}`);
tdCounter++;
}
expect(tdCounter).to.eql(0);
expect(discoveryProcess.error).to.not.eq(undefined);
});
});

0 comments on commit b292de9

Please sign in to comment.