messages: pure builders for every event the bridge sends

src/messages builds Response, StateReport, DeferredResponse, ErrorResponse, ChangeReport and the scene, doorbell
and simple events from plain values; messageId and times are parameters, so a test compares whole objects.
StateBuilder collects properties checked by the descriptors, AlexaError and AlexaErrors carry the payload fields
of an error type. AlexaStatusMessage and AlexaErrorResponse move to src/compat and are written on the builders.
On the wire, against 1.5.2: a DeferredResponse has no context key, a ChangeReport has no correlationToken, the
namespace of an ErrorResponse follows its type, and a ChangeReport without a changed property is not published:
send() resolves "" and the bridge reports an error that names the endpoint.
143 tests pass (108 before).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
David 2026-09-28 19:28:05 +00:00
parent 2558e21787
commit 46fa06728c
68 changed files with 3410 additions and 1208 deletions

49
dist/esm/messages/StateBuilder.d.ts vendored Normal file
View file

@ -0,0 +1,49 @@
import { CONNECTIVITY_REASONS } from "../registry/interfaces/EndpointHealth.js";
import type { Infer } from "../registry/schema.js";
import type { InterfaceDescriptor, Properties } from "../registry/types.js";
import type { PropertyOptions } from "./property.js";
import type { Property } from "./types.js";
/** Where a property goes in a ChangeReport: with the ones that changed, or with the rest in the context. */
export type Target = "context" | "change";
/** A capability of a device: its properties are reported under its instance. */
interface DeclaredCapability<P extends Properties> {
readonly descriptor: InterfaceDescriptor<P, any, any, boolean>;
readonly instance: string;
}
export interface StateBuilderOptions {
/** Where the properties go until changed() or unchanged() says otherwise. Default: "context". */
target?: Target;
/** The time of a property that is given none. Default: the clock. */
now?: () => Date;
}
/**
* Collects the properties of one message. For a Response or a StateReport they are its context. For a ChangeReport
* changed() and unchanged() say which of the two lists the properties that follow belong to.
*/
export declare class StateBuilder {
private readonly lists;
private target;
private readonly now;
constructor(options?: StateBuilderOptions);
/**
* A property of an interface the library describes, its value checked: set(PowerController, "powerState", "ON").
* Given a capability of a device in place of the interface, the property carries the instance of the capability.
* Throws SchemaError for a value Alexa would not take.
*/
set<P extends Properties, K extends keyof P & string>(source: InterfaceDescriptor<P, any, any, boolean> | DeclaredCapability<P>, name: K, value: Infer<P[K]["value"]>, options?: PropertyOptions): this;
/** A property as given, nothing checked: for an interface or a value the library does not describe. */
setRaw(namespace: string, name: string, value: unknown, options?: PropertyOptions): this;
/** The connectivity of Alexa.EndpointHealth, which belongs in every report of an endpoint that declares it. */
health(value: "OK" | "UNREACHABLE", reason?: (typeof CONNECTIVITY_REASONS)[number], options?: PropertyOptions): this;
/** A property built elsewhere, to the list that is filled now or to the one named. */
add(built: Property, target?: Target): this;
/** The properties that follow are the ones that changed. */
changed(): this;
/** The properties that follow did not change: a ChangeReport lists them in its context. */
unchanged(): this;
/** The properties for the context of the message, in the order they were set. */
get context(): Property[];
/** The properties for payload.change of a ChangeReport. */
get change(): Property[];
}
export {};