|
1 | 1 | import type { RouteLocation } from "@openclaw/uirouter"; |
2 | 2 | import { describe, expect, it, vi } from "vitest"; |
3 | 3 | import type { GatewayBrowserClient } from "../api/gateway.ts"; |
4 | | -import type { RouteId } from "../app-routes.ts"; |
| 4 | +import { routeIdFromPath, type RouteId } from "../app-routes.ts"; |
5 | 5 | import { sessionRefFromPath } from "../app-session-route-paths.ts"; |
6 | | -import { startModelSetupFirstRunRedirectAfterLocation } from "../pages/model-setup/first-run.ts"; |
| 6 | +import { |
| 7 | + isDefaultChatLanding, |
| 8 | + startModelSetupFirstRunRedirectAfterLocation, |
| 9 | +} from "../pages/model-setup/first-run.ts"; |
7 | 10 | import { |
8 | 11 | normalizeInitialApplicationLocation, |
9 | 12 | resolveInitialApplicationLocation, |
@@ -367,6 +370,56 @@ describe("normalizeInitialApplicationLocation", () => { |
367 | 370 | expect(replaceRoute).toHaveBeenCalledWith("model-setup", { search: "?firstRun=1" }); |
368 | 371 | }); |
369 | 372 |
|
| 373 | + it("does not replace a user route with the deferred default chat location", async () => { |
| 374 | + const currentLocation = { pathname: "/new", search: "", hash: "" }; |
| 375 | + const installLocation = vi.fn(); |
| 376 | + |
| 377 | + await startModelSetupFirstRunRedirectAfterLocation({ |
| 378 | + context: {} as ApplicationContext<RouteId>, |
| 379 | + enabled: false, |
| 380 | + history: { location: () => currentLocation, replace: vi.fn() }, |
| 381 | + initialLocationReady: Promise.resolve({ pathname: "/chat/main", search: "", hash: "" }), |
| 382 | + installLocation, |
| 383 | + shouldInstallLocation: () => isDefaultChatLanding(currentLocation, "", routeIdFromPath), |
| 384 | + }); |
| 385 | + |
| 386 | + expect(installLocation).not.toHaveBeenCalled(); |
| 387 | + }); |
| 388 | + |
| 389 | + it("keeps the latest navigation requested before router start", async () => { |
| 390 | + const previousSettings = loadSettings(); |
| 391 | + const previousUrl = window.location.href; |
| 392 | + saveSettings({ |
| 393 | + ...previousSettings, |
| 394 | + sessionKey: "agent:main:main", |
| 395 | + lastActiveSessionKey: "agent:main:main", |
| 396 | + }); |
| 397 | + window.history.replaceState({}, "", "/chat"); |
| 398 | + const sessionPathBuilder = deferred<void>(); |
| 399 | + const runtime = bootstrapApplication({ sessionPathBuilderReady: sessionPathBuilder.promise }); |
| 400 | + const pushState = vi.spyOn(window.history, "pushState"); |
| 401 | + |
| 402 | + try { |
| 403 | + const start = runtime.start(); |
| 404 | + runtime.context.replace("about"); |
| 405 | + runtime.context.navigate("new-session"); |
| 406 | + expect(window.location.pathname).toBe("/chat"); |
| 407 | + |
| 408 | + sessionPathBuilder.resolve(); |
| 409 | + await start; |
| 410 | + |
| 411 | + expect(runtime.router.getState().matches[0]?.routeId).toBe("new-session"); |
| 412 | + expect(runtime.router.getState().resolvedLocation?.pathname).toBe("/new"); |
| 413 | + expect(window.location.pathname).toBe("/new"); |
| 414 | + expect(pushState).toHaveBeenCalledWith({}, "", "/new"); |
| 415 | + } finally { |
| 416 | + pushState.mockRestore(); |
| 417 | + runtime.stop(); |
| 418 | + saveSettings(previousSettings); |
| 419 | + window.history.replaceState({}, "", previousUrl); |
| 420 | + } |
| 421 | + }); |
| 422 | + |
370 | 423 | it("does not restart routing after stop wins the session-path loader race", async () => { |
371 | 424 | const previousSettings = loadSettings(); |
372 | 425 | const previousUrl = window.location.href; |
|
0 commit comments