|
| 1 | +// @vitest-environment node |
| 2 | + |
| 3 | +import type { RouteLoaderOptions } from "@openclaw/uirouter"; |
| 4 | +import { describe, expect, it, vi } from "vitest"; |
| 5 | +import type { SessionsListResult } from "../../api/types.ts"; |
| 6 | +import type { ApplicationContext } from "../../app/context.ts"; |
| 7 | +import type { SessionListOptions, SessionListSnapshot } from "../../lib/sessions/index.ts"; |
| 8 | +import { page, type SessionsRouteData } from "./route.ts"; |
| 9 | + |
| 10 | +const result: SessionsListResult = { |
| 11 | + ts: 1, |
| 12 | + path: "", |
| 13 | + count: 0, |
| 14 | + defaults: { modelProvider: null, model: null, contextTokens: null }, |
| 15 | + sessions: [], |
| 16 | +}; |
| 17 | + |
| 18 | +async function loadSessionsRoute(options: { |
| 19 | + search: string; |
| 20 | + scopeId: string | null; |
| 21 | + expectedQuery: SessionListOptions; |
| 22 | +}) { |
| 23 | + let snapshot: SessionListSnapshot = { |
| 24 | + result: null, |
| 25 | + agentId: null, |
| 26 | + loading: false, |
| 27 | + error: null, |
| 28 | + }; |
| 29 | + const list = vi.fn(); |
| 30 | + const listSnapshot = vi.fn(() => snapshot); |
| 31 | + const refreshList = vi.fn(async () => { |
| 32 | + snapshot = { result, agentId: options.scopeId, loading: false, error: null }; |
| 33 | + }); |
| 34 | + const context = { |
| 35 | + gateway: { snapshot: { phase: "connected", client: {} } }, |
| 36 | + sessions: { list, listSnapshot, refreshList }, |
| 37 | + runtimeConfig: { ensureLoaded: vi.fn(async () => undefined) }, |
| 38 | + agentSelection: { state: { selectedId: options.scopeId, scopeId: options.scopeId } }, |
| 39 | + } as unknown as ApplicationContext; |
| 40 | + const loaderOptions: RouteLoaderOptions = { |
| 41 | + signal: new AbortController().signal, |
| 42 | + shouldRun: () => true, |
| 43 | + revalidating: false, |
| 44 | + location: { pathname: "/sessions", search: options.search, hash: "" }, |
| 45 | + deps: "", |
| 46 | + cause: "navigation", |
| 47 | + }; |
| 48 | + |
| 49 | + const data = (await page.loader?.(context, loaderOptions)) as SessionsRouteData; |
| 50 | + |
| 51 | + expect(refreshList).toHaveBeenCalledWith({ ...options.expectedQuery, force: true }); |
| 52 | + expect(listSnapshot).toHaveBeenLastCalledWith(options.expectedQuery); |
| 53 | + expect(list).not.toHaveBeenCalled(); |
| 54 | + expect(data).toMatchObject({ result, loading: false, error: null }); |
| 55 | +} |
| 56 | + |
| 57 | +describe("sessions route", () => { |
| 58 | + it.each([ |
| 59 | + { |
| 60 | + name: "default selected-agent roster", |
| 61 | + search: "", |
| 62 | + scopeId: "writer", |
| 63 | + expectedQuery: { |
| 64 | + limit: 50, |
| 65 | + includeGlobal: true, |
| 66 | + includeUnknown: false, |
| 67 | + includeDerivedTitles: false, |
| 68 | + includeLastMessage: false, |
| 69 | + archivedFilter: "active" as const, |
| 70 | + agentId: "writer", |
| 71 | + }, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "archived all-agent roster", |
| 75 | + search: "?status=archived", |
| 76 | + scopeId: null, |
| 77 | + expectedQuery: { |
| 78 | + limit: 50, |
| 79 | + includeGlobal: true, |
| 80 | + includeUnknown: false, |
| 81 | + includeDerivedTitles: false, |
| 82 | + includeLastMessage: false, |
| 83 | + archivedFilter: "archived" as const, |
| 84 | + }, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "all-status selected-agent roster", |
| 88 | + search: "?status=all", |
| 89 | + scopeId: "main", |
| 90 | + expectedQuery: { |
| 91 | + limit: 50, |
| 92 | + includeGlobal: true, |
| 93 | + includeUnknown: false, |
| 94 | + includeDerivedTitles: false, |
| 95 | + includeLastMessage: false, |
| 96 | + archivedFilter: "all" as const, |
| 97 | + agentId: "main", |
| 98 | + }, |
| 99 | + }, |
| 100 | + { |
| 101 | + name: "deep link owned by a different agent", |
| 102 | + search: "?session=agent%3Aresearch%3Alinked", |
| 103 | + scopeId: "main", |
| 104 | + expectedQuery: { |
| 105 | + limit: 50, |
| 106 | + search: "agent:research:linked", |
| 107 | + includeGlobal: true, |
| 108 | + includeUnknown: true, |
| 109 | + includeDerivedTitles: false, |
| 110 | + includeLastMessage: false, |
| 111 | + archivedFilter: "active" as const, |
| 112 | + agentId: "research", |
| 113 | + }, |
| 114 | + }, |
| 115 | + ])("loads the managed $name without a raw list", async (testCase) => { |
| 116 | + await loadSessionsRoute(testCase); |
| 117 | + }); |
| 118 | +}); |
0 commit comments