/** * @license * Copyright 2022-2026 Matter.js Authors * SPDX-License-Identifier: Apache-2.0 */ import { Behavior } from "#behavior/Behavior.js"; import type { BehaviorAt, BehaviorOf, BehaviorSelection, Endpoint, RawBehaviorSelection, StateSelector, StateSliceOf, } from "#endpoint/Endpoint.js"; import { SupportedBehaviors } from "#endpoint/properties/SupportedBehaviors.js"; import { EndpointType } from "#endpoint/type/EndpointType.js"; import type { Immutable } from "@matter/general"; type FakeState = { value: number; label: string }; type FakeBehaviorType = Behavior.Type & { readonly State: new () => FakeState; readonly id: "fake" }; // A minimal EndpointType shape for type-level testing type TestEndpoint = EndpointType & { behaviors: { fake: FakeBehaviorType } }; describe("Endpoint get type helpers", () => { it("compiles correctly (types validated at compile time)", () => { // BehaviorOf resolves to a Behavior.Type union over behaviors type _BehOf = BehaviorOf; const _checkBehOf: _BehOf = {} as FakeBehaviorType; void _checkBehOf; // BehaviorAt resolves to the specific behavior type at a key type _BehAt = BehaviorAt; const _checkBehAt: _BehAt = {} as FakeBehaviorType; void _checkBehAt; // BehaviorSelection accepts `true` const _selTrue: BehaviorSelection = true; void _selTrue; // BehaviorSelection accepts a readonly array of state keys const _selKeys: BehaviorSelection = ["value"] as const; void _selKeys; // RawBehaviorSelection accepts `true` const _rawTrue: RawBehaviorSelection = true; void _rawTrue; // RawBehaviorSelection accepts a readonly string array const _rawArr: RawBehaviorSelection = ["value", "label"] as const; void _rawArr; // StateSelector accepts { fake: true } const _selectorTrue: StateSelector = { fake: true }; void _selectorTrue; // StateSelector accepts { fake: readonly (keyof State)[] } const _selectorKeys: StateSelector = { fake: ["value"] as const }; void _selectorKeys; // StateSelector accepts partial (no keys required) const _selectorEmpty: StateSelector = {}; void _selectorEmpty; // StateSelector rejects attribute keys that are not in the behavior's State for typed endpoints. // @ts-expect-error - "missing" is not a key of FakeState const _selectorBadKey: StateSelector = { fake: ["missing"] as const }; void _selectorBadKey; // Fallback branch: unbound `T extends EndpointType` widens `keyof T["behaviors"]` to `string` // via the SupportedBehaviors index signature, so values must accept RawBehaviorSelection // (arbitrary string-keyed attribute lists) for backward compatibility with generic code. type _GenericValue = NonNullable[string]>; type _AssertEqual = (() => T extends A ? 1 : 0) extends () => T extends B ? 1 : 0 ? true : false; const _genericValueIsRaw: _AssertEqual<_GenericValue, RawBehaviorSelection> = true; void _genericValueIsRaw; const _genericSelector: StateSelector = { someBehavior: ["arbitraryAttr"] as const }; void _genericSelector; // Real factory path: exercise EndpointType(...) + SupportedBehaviors(...) + MapOf<>, not a // hand-built intersection. Catches regressions in EndpointType.For / MapOf that would // widen `keyof T["behaviors"]` back to `string` and silently drop the per-behavior tightening. class FactoryTestBehavior extends Behavior { static override readonly id = "factoryTest"; static override readonly State = class FactoryTestState { value = 0; label = ""; }; } const FactoryTestEndpoint = EndpointType({ name: "FactoryTest", deviceType: 0xfff1, deviceRevision: 1, behaviors: SupportedBehaviors(FactoryTestBehavior), }); type _FactoryTestType = typeof FactoryTestEndpoint; // Behavior keys must remain narrow (literal "factoryTest"), not widened to `string`. type _FactoryKeys = keyof _FactoryTestType["behaviors"]; const _factoryKeysNarrow: string extends _FactoryKeys ? false : true = true; void _factoryKeysNarrow; // Typed attribute key accepted on factory-built endpoint const _factoryGood: StateSelector<_FactoryTestType> = { factoryTest: ["value"] as const }; void _factoryGood; // @ts-expect-error - "missing" is not a key of FactoryTestBehavior State const _factoryBad: StateSelector<_FactoryTestType> = { factoryTest: ["missing"] as const }; void _factoryBad; // StateSliceOf with { fake: true } produces a slice with full fake state type _TrueSlice = StateSliceOf; const _checkTrueSlice: _TrueSlice = {} as { fake: Immutable> }; void _checkTrueSlice; // StateSliceOf with key selection produces a Partial slice (values may be absent) type _PickSlice = StateSliceOf; const _checkPickSlice: _PickSlice = {} as { fake: Immutable, "value">>>; }; void _checkPickSlice; // getStateOf overload return types — assert each overload resolves to the documented shape. // The `(false as boolean) === true` guard keeps the body type-checked but unreachable at // runtime, so the phantom `_ep`/`_fakeBeh` values are never dereferenced. The cast through // `boolean` is required to defeat TypeScript's constant-condition unreachable-code check // that a literal `if (false)` would trigger. if ((false as boolean) === true) { const _ep = null as unknown as Endpoint; const _fakeBeh = null as unknown as FakeBehaviorType; // Overload 1: (B) and (B, true) → Promise> const _stateOfNoSelector: Promise> = _ep.getStateOf(_fakeBeh); void _stateOfNoSelector; const _stateOfTrue: Promise> = _ep.getStateOf(_fakeBeh, true); void _stateOfTrue; // Overload 2: (B, K[]) → Promise<{ readonly [P in K]?: Behavior.StateOf[P] }>. // Each selected key is optional because partial-state-on-failure may omit it. // Use the bidirectional `_AssertEqual` helper so the assertion fails if the resolved // shape gains or loses properties (a one-sided assignability check would not catch // an extra optional property creeping into the return type). const _stateOfKeys = _ep.getStateOf(_fakeBeh, ["value"] as const); const _checkKeysExact: _AssertEqual, { readonly value?: number }> = true; void _checkKeysExact; // Overload 2 must reject keys not in the behavior's State. // @ts-expect-error - "missing" is not a key of FakeState const _stateOfBadKey = _ep.getStateOf(_fakeBeh, ["missing"] as const); void _stateOfBadKey; // Overload 3: (string, readonly string[]) → Promise. Accepts arbitrary string keys. const _stateOfStringId = _ep.getStateOf("anyId", ["foo", "bar"]); void _stateOfStringId; } }); });