Skip to content

Commit fae9bd6

Browse files
authored
fix(ui): refresh mixed-role node status (#126677)
1 parent 1b9d3ac commit fae9bd6

2 files changed

Lines changed: 163 additions & 1 deletion

File tree

‎ui/src/pages/devices/devices-page.test.ts‎

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,167 @@ describe("DevicesPage gateway lifecycle", () => {
439439
page.remove();
440440
});
441441

442+
it.each([
443+
{
444+
name: "node disconnects while its operator stays connected",
445+
role: "node",
446+
previousReason: "connect",
447+
nextReason: "disconnect",
448+
operatorRoles: ["operator"],
449+
},
450+
{
451+
name: "node reconnects while its operator stays connected",
452+
role: "node",
453+
previousReason: "disconnect",
454+
nextReason: "connect",
455+
operatorRoles: ["operator"],
456+
},
457+
{
458+
name: "merged node-role presence disconnects while its operator stays connected",
459+
role: "node",
460+
previousReason: "connect",
461+
nextReason: "disconnect",
462+
nodeRoles: ["operator", "node"],
463+
operatorRoles: ["operator"],
464+
},
465+
{
466+
name: "operator disconnects while its node stays connected",
467+
role: "operator",
468+
previousReason: "connect",
469+
nextReason: "disconnect",
470+
operatorRoles: ["operator"],
471+
},
472+
{
473+
name: "operator reconnects while its node stays connected",
474+
role: "operator",
475+
previousReason: "disconnect",
476+
nextReason: "connect",
477+
operatorRoles: ["operator"],
478+
},
479+
{
480+
name: "node disconnects while a roleless device stays connected",
481+
role: "node",
482+
previousReason: "connect",
483+
nextReason: "disconnect",
484+
operatorRoles: undefined,
485+
},
486+
{
487+
name: "node disconnects while a device with empty roles stays connected",
488+
role: "node",
489+
previousReason: "connect",
490+
nextReason: "disconnect",
491+
operatorRoles: [],
492+
},
493+
])("reloads mixed-role inventory when $name", async (scenario) => {
494+
const request = vi.fn(async (method: string) =>
495+
method === "node.list" ? { nodes: [] } : { paired: [], pending: [] },
496+
);
497+
const client = { request } as unknown as GatewayBrowserClient;
498+
const snapshot = {
499+
...gatewaySnapshot(client, true),
500+
hello: {
501+
type: "hello-ok",
502+
protocol: 1,
503+
auth: { role: "operator", scopes: ["operator.read", "operator.pairing"] },
504+
features: { methods: ["node.list", "device.pair.list"] },
505+
},
506+
} as ApplicationGatewaySnapshot;
507+
let onEvent: ((event: { event: string; payload?: unknown }) => void) | undefined;
508+
const currentGateway = gateway(client, snapshot);
509+
currentGateway.subscribeEvents = vi.fn((listener) => {
510+
onEvent = listener as typeof onEvent;
511+
return () => undefined;
512+
});
513+
const page = document.createElement("openclaw-devices-page") as TestDevicesPage;
514+
page.context = {
515+
gateway: currentGateway,
516+
runtimeConfig: {
517+
state: { configSnapshot: {}, configLoading: false },
518+
subscribe: vi.fn(() => () => undefined),
519+
},
520+
} as unknown as ApplicationContext;
521+
document.body.append(page);
522+
await vi.waitFor(() => expect(onEvent).toBeDefined());
523+
524+
const nodePresence: PresenceEntry = {
525+
deviceId: "mixed-role-device",
526+
instanceId: "mixed-role-device",
527+
roles: "nodeRoles" in scenario ? scenario.nodeRoles : ["node"],
528+
reason: scenario.role === "node" ? scenario.previousReason : "connect",
529+
ts: 2_000,
530+
};
531+
const operatorPresence: PresenceEntry = {
532+
deviceId: "mixed-role-device",
533+
instanceId: "operator-session",
534+
...(scenario.operatorRoles ? { roles: scenario.operatorRoles } : {}),
535+
reason: scenario.role === "operator" ? scenario.previousReason : "connect",
536+
ts: 1_000,
537+
};
538+
page.presence = [nodePresence, operatorPresence];
539+
request.mockClear();
540+
541+
onEvent?.({
542+
event: "presence",
543+
payload: {
544+
presence: [
545+
scenario.role === "node"
546+
? { ...nodePresence, reason: scenario.nextReason }
547+
: nodePresence,
548+
scenario.role === "operator"
549+
? { ...operatorPresence, reason: scenario.nextReason }
550+
: operatorPresence,
551+
],
552+
},
553+
});
554+
555+
await vi.waitFor(() => expect(request).toHaveBeenCalledWith("node.list", {}));
556+
expect(request).toHaveBeenCalledWith("device.pair.list", {});
557+
page.remove();
558+
});
559+
560+
it("does not reload mixed-role inventory for presence activity updates", async () => {
561+
const request = vi.fn(async (method: string) =>
562+
method === "node.list" ? { nodes: [] } : { paired: [], pending: [] },
563+
);
564+
const client = { request } as unknown as GatewayBrowserClient;
565+
let onEvent: ((event: { event: string; payload?: unknown }) => void) | undefined;
566+
const currentGateway = gateway(client, gatewaySnapshot(client, true));
567+
currentGateway.subscribeEvents = vi.fn((listener) => {
568+
onEvent = listener as typeof onEvent;
569+
return () => undefined;
570+
});
571+
const page = document.createElement("openclaw-devices-page") as TestDevicesPage;
572+
page.context = {
573+
gateway: currentGateway,
574+
runtimeConfig: {
575+
state: { configSnapshot: {}, configLoading: false },
576+
subscribe: vi.fn(() => () => undefined),
577+
},
578+
} as unknown as ApplicationContext;
579+
document.body.append(page);
580+
await vi.waitFor(() => expect(onEvent).toBeDefined());
581+
582+
const presence: PresenceEntry[] = [
583+
{ deviceId: "mixed-role-device", roles: ["node"], reason: "connect", ts: 2_000 },
584+
{ deviceId: "mixed-role-device", roles: ["operator"], reason: "connect", ts: 1_000 },
585+
];
586+
page.presence = presence;
587+
request.mockClear();
588+
589+
onEvent?.({
590+
event: "presence",
591+
payload: {
592+
presence: presence.map((entry) =>
593+
Object.assign({}, entry, { lastInputSeconds: 3, ts: entry.ts + 100 }),
594+
),
595+
},
596+
});
597+
await Promise.resolve();
598+
599+
expect(request).not.toHaveBeenCalled();
600+
page.remove();
601+
});
602+
442603
it("retries a node load after a same-client disconnect", async () => {
443604
const first = deferred<{ nodes: Array<Record<string, unknown>> }>();
444605
const second = deferred<{ nodes: Array<Record<string, unknown>> }>();

‎ui/src/pages/devices/devices-page.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ function presenceConnectivitySignature(entries: PresenceEntry[]): string {
7575
if (!id || entry.mode?.trim().toLowerCase() === "gateway") {
7676
continue;
7777
}
78-
states.set(id, entry.reason?.trim().toLowerCase() === "disconnect" ? "offline" : "connected");
78+
const key = entry.roles?.includes("node") ? `${id}:node` : id;
79+
states.set(key, entry.reason?.trim().toLowerCase() === "disconnect" ? "offline" : "connected");
7980
}
8081
return JSON.stringify([...states].toSorted(([left], [right]) => left.localeCompare(right)));
8182
}

0 commit comments

Comments
 (0)