






















11import { describe, expect, it, vi } from "vitest";
22import type { runCommandWithTimeout } from "../process/exec.js";
3-import { discoverGatewayBeacons, resolveGatewayDiscoveryEndpoint } from "./bonjour-discovery.js";
3+import {
4+discoverGatewayBeacons,
5+type GatewayBonjourBeacon,
6+resolveGatewayDiscoveryEndpoint,
7+} from "./bonjour-discovery.js";
4859const WIDE_AREA_DOMAIN = "openclaw.internal.";
6107-type BeaconRecord = {
8-domain?: string;
9-instanceName?: string;
10-displayName?: string;
11-host?: string;
12-port?: number;
13-tailnetDns?: string;
14-gatewayPort?: number;
15-sshPort?: number;
16-cliPath?: string;
17-txt?: Record<string, unknown>;
18-};
19-2011function collectMatching<T, U>(
2112items: readonly T[],
2213predicate: (item: T) => boolean,
@@ -31,14 +22,26 @@ function collectMatching<T, U>(
3122return matches;
3223}
332434-function findBeaconByInstance(beacons: readonly BeaconRecord[], instanceName: string) {
25+function findBeaconByInstance(
26+beacons: readonly GatewayBonjourBeacon[],
27+instanceName: string,
28+): GatewayBonjourBeacon {
3529const beacon = beacons.find((item) => item.instanceName === instanceName);
3630if (!beacon) {
3731throw new Error(`Expected beacon ${instanceName}`);
3832}
3933return beacon;
4034}
413536+function getOnlyBeacon(beacons: readonly GatewayBonjourBeacon[]): GatewayBonjourBeacon {
37+expect(beacons).toHaveLength(1);
38+const beacon = beacons[0];
39+if (!beacon) {
40+throw new Error("Expected one beacon");
41+}
42+return beacon;
43+}
44+4245describe("bonjour-discovery", () => {
4346it("discovers beacons on darwin across local + wide-area domains", async () => {
4447const calls: Array<{ argv: string[]; timeoutMs: number }> = [];
@@ -179,8 +182,7 @@ describe("bonjour-discovery", () => {
179182run: run as unknown as typeof runCommandWithTimeout,
180183});
181184182-expect(beacons).toHaveLength(1);
183-const beacon = beacons[0] as BeaconRecord;
185+const beacon = getOnlyBeacon(beacons);
184186expect(beacon.domain).toBe("local.");
185187expect(beacon.instanceName).toBe("Studio Gateway");
186188expect(beacon.displayName).toBe("Peter’s Mac Studio");
@@ -224,8 +226,7 @@ describe("bonjour-discovery", () => {
224226run: run as unknown as typeof runCommandWithTimeout,
225227});
226228227-expect(beacons).toHaveLength(1);
228-const beacon = beacons[0] as BeaconRecord;
229+const beacon = getOnlyBeacon(beacons);
229230expect(beacon.host).toBe("broken.local");
230231expect(beacon.port).toBeUndefined();
231232expect(beacon.gatewayPort).toBeUndefined();
@@ -324,8 +325,7 @@ describe("bonjour-discovery", () => {
324325run: run as unknown as typeof runCommandWithTimeout,
325326});
326327327-expect(beacons).toHaveLength(1);
328-const beacon = beacons[0] as BeaconRecord;
328+const beacon = getOnlyBeacon(beacons);
329329expect(beacon.domain).toBe(WIDE_AREA_DOMAIN);
330330expect(beacon.instanceName).toBe("studio-gateway");
331331expect(beacon.displayName).toBe("Studio");
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。