|
1 | 1 | /* @vitest-environment jsdom */ |
2 | 2 |
|
3 | 3 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import { createDeferred } from "../../../../test/helpers/promise.js"; |
4 | 5 | import type { ChannelsStatusSnapshot } from "../../api/types.ts"; |
| 6 | +import { channelSnapshotEntryIsActive, createChannelCapability } from "../../lib/channels/index.ts"; |
5 | 7 | import { waitForFast } from "../../test-helpers/wait-for.ts"; |
6 | 8 | import { createContext, mountPage } from "./custodian-page.test-harness.ts"; |
7 | 9 |
|
@@ -100,6 +102,136 @@ describe("custodian channel onboarding", () => { |
100 | 102 | expect(context.channels.refresh).toHaveBeenNthCalledWith(2, false); |
101 | 103 | }); |
102 | 104 |
|
| 105 | + it("awaits fresh channel status after reconnecting with a stale successful snapshot and error", async () => { |
| 106 | + const freshStatus = createDeferred<ChannelsStatusSnapshot>(); |
| 107 | + let statusRequestCount = 0; |
| 108 | + const request = vi.fn((method: string) => { |
| 109 | + if (method === "channels.status") { |
| 110 | + statusRequestCount += 1; |
| 111 | + if (statusRequestCount === 1) { |
| 112 | + return Promise.resolve(channelSnapshot()); |
| 113 | + } |
| 114 | + if (statusRequestCount === 2) { |
| 115 | + return Promise.reject(new Error("status unavailable")); |
| 116 | + } |
| 117 | + return freshStatus.promise; |
| 118 | + } |
| 119 | + return Promise.resolve({ |
| 120 | + sessionId: "control-ui-onboarding-00000000-0000-4000-8000-000000000001", |
| 121 | + reply: "Ready.", |
| 122 | + action: "none", |
| 123 | + }); |
| 124 | + }); |
| 125 | + const { context, setGatewaySnapshot } = createContext(request); |
| 126 | + const channels = createChannelCapability(context.gateway); |
| 127 | + Object.assign(context, { channels }); |
| 128 | + await channels.refresh(); |
| 129 | + await channels.refresh(); |
| 130 | + expect(channels.state.channelsSnapshot).not.toBeNull(); |
| 131 | + expect(channels.state.channelsError).toBe("status unavailable"); |
| 132 | + const { page } = await mountPage(context, { onboarding: true }); |
| 133 | + |
| 134 | + setGatewaySnapshot({ phase: "reconnecting" }); |
| 135 | + setGatewaySnapshot({ phase: "connected" }); |
| 136 | + |
| 137 | + await waitForFast(() => expect(statusRequestCount).toBe(3)); |
| 138 | + expect(channels.state.channelsSnapshot).toBeNull(); |
| 139 | + expect(channels.state.channelsLoading).toBe(true); |
| 140 | + expect(page.querySelector(".custodian__nudge--channel-onboarding")).toBeNull(); |
| 141 | + |
| 142 | + freshStatus.resolve( |
| 143 | + channelSnapshot({ |
| 144 | + channels: { telegram: { configured: true, running: true, connected: true } }, |
| 145 | + }), |
| 146 | + ); |
| 147 | + await waitForFast(() => expect(channels.state.channelsLoading).toBe(false)); |
| 148 | + expect(channelSnapshotEntryIsActive(channels.state.channelsSnapshot, "telegram")).toBe(true); |
| 149 | + expect(page.querySelector(".custodian__nudge--channel-onboarding")).toBeNull(); |
| 150 | + channels.dispose(); |
| 151 | + }); |
| 152 | + |
| 153 | + it("shows an error instead of a healthy nudge when refresh fails with a stale snapshot", async () => { |
| 154 | + const request = vi.fn().mockResolvedValue({ |
| 155 | + sessionId: "control-ui-onboarding-00000000-0000-4000-8000-000000000001", |
| 156 | + reply: "Ready.", |
| 157 | + action: "none", |
| 158 | + }); |
| 159 | + const { context, setChannelsError } = createContext(request, ["openclaw.chat"], { |
| 160 | + channelsSnapshot: channelSnapshot(), |
| 161 | + }); |
| 162 | + const { page } = await mountPage(context, { onboarding: true }); |
| 163 | + |
| 164 | + setChannelsError("status unavailable"); |
| 165 | + await page.updateComplete; |
| 166 | + |
| 167 | + expect(page.querySelector('[role="alert"]')?.textContent).toContain( |
| 168 | + "Channel status is unavailable", |
| 169 | + ); |
| 170 | + expect(page.textContent).not.toContain("The web app already works"); |
| 171 | + }); |
| 172 | + |
| 173 | + it("keeps retry feedback visible until a deferred channel refresh succeeds", async () => { |
| 174 | + const retryStatus = createDeferred<ChannelsStatusSnapshot>(); |
| 175 | + let statusRequestCount = 0; |
| 176 | + const request = vi.fn((method: string) => { |
| 177 | + if (method === "channels.status") { |
| 178 | + statusRequestCount += 1; |
| 179 | + return statusRequestCount === 1 |
| 180 | + ? Promise.reject(new Error("status unavailable")) |
| 181 | + : retryStatus.promise; |
| 182 | + } |
| 183 | + return Promise.resolve({ |
| 184 | + sessionId: "control-ui-onboarding-00000000-0000-4000-8000-000000000001", |
| 185 | + reply: "Ready.", |
| 186 | + action: "none", |
| 187 | + }); |
| 188 | + }); |
| 189 | + const { context } = createContext(request); |
| 190 | + const channels = createChannelCapability(context.gateway); |
| 191 | + Object.assign(context, { channels }); |
| 192 | + await channels.refresh(); |
| 193 | + const { page } = await mountPage(context, { onboarding: true }); |
| 194 | + |
| 195 | + const retry = page.querySelector<HTMLButtonElement>(".custodian__nudge-cta"); |
| 196 | + expect(retry?.textContent).toContain("Retry"); |
| 197 | + retry?.click(); |
| 198 | + |
| 199 | + await waitForFast(() => expect(statusRequestCount).toBe(2)); |
| 200 | + await page.updateComplete; |
| 201 | + expect(channels.state.channelsLoading).toBe(true); |
| 202 | + expect(channels.state.channelsError).toBe("status unavailable"); |
| 203 | + expect(page.querySelector('[role="alert"]')).not.toBeNull(); |
| 204 | + expect(page.querySelector<HTMLButtonElement>(".custodian__nudge-cta")?.disabled).toBe(true); |
| 205 | + expect(page.querySelector(".custodian__nudge-cta")?.textContent).toContain("Loading"); |
| 206 | + |
| 207 | + retryStatus.resolve( |
| 208 | + channelSnapshot({ |
| 209 | + channels: { telegram: { configured: true, running: true, connected: true } }, |
| 210 | + }), |
| 211 | + ); |
| 212 | + await waitForFast(() => expect(channels.state.channelsLoading).toBe(false)); |
| 213 | + expect(channels.state.channelsError).toBeNull(); |
| 214 | + expect(page.querySelector(".custodian__nudge--channel-onboarding")).toBeNull(); |
| 215 | + channels.dispose(); |
| 216 | + }); |
| 217 | + |
| 218 | + it("does not refresh after the channel prompt is dismissed", async () => { |
| 219 | + const request = vi.fn().mockResolvedValue({ |
| 220 | + sessionId: "control-ui-onboarding-00000000-0000-4000-8000-000000000001", |
| 221 | + reply: "Ready.", |
| 222 | + action: "none", |
| 223 | + }); |
| 224 | + const { context, setChannelsConnected } = createContext(request); |
| 225 | + const { page } = await mountPage(context, { onboarding: true }); |
| 226 | + await waitForFast(() => expect(context.channels.refresh).toHaveBeenCalledOnce()); |
| 227 | + |
| 228 | + page.store.dismissChannelOnboardingNudge(); |
| 229 | + setChannelsConnected(false); |
| 230 | + setChannelsConnected(true); |
| 231 | + |
| 232 | + expect(context.channels.refresh).toHaveBeenCalledOnce(); |
| 233 | + }); |
| 234 | + |
103 | 235 | it.each([ |
104 | 236 | { |
105 | 237 | name: "normal caretaker visits", |
|
0 commit comments