|
1 | 1 | // @vitest-environment node |
2 | | -import type { RouteLocation, RouterHistory } from "@openclaw/uirouter"; |
| 2 | +import { notFound, type RouteLocation, type RouterHistory } from "@openclaw/uirouter"; |
3 | 3 | import { describe, expect, it, vi } from "vitest"; |
4 | 4 | import { |
5 | 5 | agentRouteFromPath, |
@@ -194,6 +194,120 @@ describe("Dynamic route startup bridge", () => { |
194 | 194 | route.component = originalComponent; |
195 | 195 | } |
196 | 196 | }); |
| 197 | + |
| 198 | + it("keeps a loader not-found state without rejecting startup", async () => { |
| 199 | + let location: RouteLocation = { pathname: "/", search: "", hash: "" }; |
| 200 | + const history: RouterHistory = { |
| 201 | + location: () => location, |
| 202 | + push: vi.fn(), |
| 203 | + replace: vi.fn((next: RouteLocation) => { |
| 204 | + location = next; |
| 205 | + }), |
| 206 | + listen: () => () => undefined, |
| 207 | + }; |
| 208 | + const router = createApplicationRouter(); |
| 209 | + const route = router.getRoute("chat"); |
| 210 | + if (!route) { |
| 211 | + throw new Error("Chat route missing"); |
| 212 | + } |
| 213 | + const originalLoader = route.loader; |
| 214 | + const originalComponent = route.component; |
| 215 | + try { |
| 216 | + route.loader = () => notFound({ routeId: "chat" }); |
| 217 | + route.component = async () => ({ render: () => null }); |
| 218 | + |
| 219 | + await expect( |
| 220 | + startApplicationRouter(router, history, "", { |
| 221 | + basePath: "", |
| 222 | + } as unknown as ApplicationContext), |
| 223 | + ).resolves.toBeUndefined(); |
| 224 | + |
| 225 | + expect(location.pathname).toBe("/chat"); |
| 226 | + expect(router.getState().status).toBe("notFound"); |
| 227 | + expect(router.getState().matches[0]).toMatchObject({ |
| 228 | + routeId: "chat", |
| 229 | + status: "notFound", |
| 230 | + error: { type: "notFound", data: { routeId: "chat" } }, |
| 231 | + }); |
| 232 | + } finally { |
| 233 | + router.stop(); |
| 234 | + route.loader = originalLoader; |
| 235 | + route.component = originalComponent; |
| 236 | + } |
| 237 | + }); |
| 238 | + |
| 239 | + it("tolerates not-found from both dynamic startup navigations", async () => { |
| 240 | + const location: RouteLocation = { |
| 241 | + pathname: "/chat/main/01JSESSIONA", |
| 242 | + search: "", |
| 243 | + hash: "", |
| 244 | + }; |
| 245 | + const history: RouterHistory = { |
| 246 | + location: () => location, |
| 247 | + push: vi.fn(), |
| 248 | + replace: vi.fn(), |
| 249 | + listen: () => () => undefined, |
| 250 | + }; |
| 251 | + const router = createApplicationRouter(); |
| 252 | + const route = router.getRoute("chat"); |
| 253 | + if (!route) { |
| 254 | + throw new Error("Chat route missing"); |
| 255 | + } |
| 256 | + const loader = vi.fn(() => notFound({ routeId: "chat" })); |
| 257 | + const originalLoader = route.loader; |
| 258 | + const originalComponent = route.component; |
| 259 | + try { |
| 260 | + route.loader = loader; |
| 261 | + route.component = async () => ({ render: () => null }); |
| 262 | + |
| 263 | + await expect( |
| 264 | + startApplicationRouter(router, history, "", { |
| 265 | + basePath: "", |
| 266 | + } as unknown as ApplicationContext), |
| 267 | + ).resolves.toBeUndefined(); |
| 268 | + |
| 269 | + expect(loader).toHaveBeenCalledTimes(2); |
| 270 | + expect(router.getState().status).toBe("notFound"); |
| 271 | + expect(router.getState().location).toEqual(location); |
| 272 | + } finally { |
| 273 | + router.stop(); |
| 274 | + route.loader = originalLoader; |
| 275 | + route.component = originalComponent; |
| 276 | + } |
| 277 | + }); |
| 278 | + |
| 279 | + it("still rejects non-not-found startup failures", async () => { |
| 280 | + const failure = new Error("chat loader failed"); |
| 281 | + const history: RouterHistory = { |
| 282 | + location: () => ({ pathname: "/chat", search: "", hash: "" }), |
| 283 | + push: vi.fn(), |
| 284 | + replace: vi.fn(), |
| 285 | + listen: () => () => undefined, |
| 286 | + }; |
| 287 | + const router = createApplicationRouter(); |
| 288 | + const route = router.getRoute("chat"); |
| 289 | + if (!route) { |
| 290 | + throw new Error("Chat route missing"); |
| 291 | + } |
| 292 | + const originalLoader = route.loader; |
| 293 | + const originalComponent = route.component; |
| 294 | + try { |
| 295 | + route.loader = () => { |
| 296 | + throw failure; |
| 297 | + }; |
| 298 | + route.component = async () => ({ render: () => null }); |
| 299 | + |
| 300 | + await expect( |
| 301 | + startApplicationRouter(router, history, "", { |
| 302 | + basePath: "", |
| 303 | + } as unknown as ApplicationContext), |
| 304 | + ).rejects.toBe(failure); |
| 305 | + } finally { |
| 306 | + router.stop(); |
| 307 | + route.loader = originalLoader; |
| 308 | + route.component = originalComponent; |
| 309 | + } |
| 310 | + }); |
197 | 311 | }); |
198 | 312 |
|
199 | 313 | describe("Agent panel route paths", () => { |
|
0 commit comments