Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: report undefined for no expected value() #1230

Merged
merged 8 commits into from
Feb 22, 2024
14 changes: 11 additions & 3 deletions packages/core/src/interaction-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import Ajv from "ajv";
import { createLoggers } from "./logger";

const { debug } = createLoggers("core", "interaction-output");
const { debug, warn } = createLoggers("core", "interaction-output");

// Problem: strict mode ajv does not accept unknown keywords in schemas
// however property affordances could contain all sort of fields
Expand Down Expand Up @@ -78,6 +78,14 @@
}

async value<T extends WoT.DataSchemaValue>(): Promise<T> {
// is there any value expected at all?
if (this.schema == null) {
warn(
`No schema defined. Hence undefined is reported for value() function. If you are invoking an action with no output that is on purpose, otherwise consider using arrayBuffer().`
);
return undefined as unknown as T;
}

// the value has been already read?
if (this.#value !== undefined) {
return this.#value as T;
Expand All @@ -91,8 +99,8 @@
throw new NotReadableError("No form defined");
}

if (this.schema == null || this.schema.type == null) {
throw new NotReadableError("No schema defined");
if (this.schema.type == null) {
throw new NotReadableError("No schema type defined");

Check warning on line 103 in packages/core/src/interaction-output.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/interaction-output.ts#L103

Added line #L103 was not covered by tests
}

// is content type valid?
Expand Down
16 changes: 15 additions & 1 deletion packages/core/test/InteractionOutputTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class InteractionOutputTests {
const stream = Readable.from([1, 2, 3]);
const content = new Content("application/json", stream);

const out = new InteractionOutput(content, {});
const out = new InteractionOutput(content, {}, { type: "string" });
const result = [];
const reader = out.data.getReader();
expect(reader).not.to.be.undefined;
Expand All @@ -73,6 +73,20 @@ class InteractionOutputTests {
return expect(out.value()).to.be.rejected;
}

@test async "should return undefined for value call if no DataSchema is provided"() {
const stream = Readable.from([]);
const content = new Content("application/json", stream);

const out = new InteractionOutput(content, {});

const result1 = await out.value();
expect(result1).be.undefined;

// try a second time also
const result2 = await out.value();
expect(result2).be.undefined;
}

@test async "should return the value"() {
const stream = Readable.from(Buffer.from("true", "utf-8"));
const content = new Content("application/json", stream);
Expand Down
Loading