|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const loadAgentsMock = vi.hoisted(() => |
| 4 | + vi.fn(async (host: { agentsList?: unknown }) => { |
| 5 | + host.agentsList = { |
| 6 | + defaultId: "main", |
| 7 | + mainKey: "main", |
| 8 | + scope: "per-sender", |
| 9 | + agents: [{ id: "main" }], |
| 10 | + }; |
| 11 | + }), |
| 12 | +); |
| 13 | +const loadConfigMock = vi.hoisted(() => vi.fn(async () => undefined)); |
| 14 | +const loadAgentIdentitiesMock = vi.hoisted(() => vi.fn(async () => undefined)); |
| 15 | +const loadAgentIdentityMock = vi.hoisted(() => vi.fn(async () => undefined)); |
| 16 | +const loadAgentSkillsMock = vi.hoisted(() => vi.fn(async () => undefined)); |
| 17 | +const loadAgentFilesMock = vi.hoisted(() => vi.fn(async () => undefined)); |
| 18 | +const loadChannelsMock = vi.hoisted(() => vi.fn(async () => undefined)); |
| 19 | + |
| 20 | +vi.mock("../ui/src/ui/controllers/agents.ts", async (importOriginal) => { |
| 21 | + const actual = await importOriginal<typeof import("../ui/src/ui/controllers/agents.ts")>(); |
| 22 | + return { ...actual, loadAgents: loadAgentsMock }; |
| 23 | +}); |
| 24 | + |
| 25 | +vi.mock("../ui/src/ui/controllers/config.ts", async (importOriginal) => { |
| 26 | + const actual = await importOriginal<typeof import("../ui/src/ui/controllers/config.ts")>(); |
| 27 | + return { |
| 28 | + ...actual, |
| 29 | + loadConfig: loadConfigMock, |
| 30 | + loadConfigSchema: vi.fn(async () => undefined), |
| 31 | + }; |
| 32 | +}); |
| 33 | + |
| 34 | +vi.mock("../ui/src/ui/controllers/agent-identity.ts", async (importOriginal) => { |
| 35 | + const actual = |
| 36 | + await importOriginal<typeof import("../ui/src/ui/controllers/agent-identity.ts")>(); |
| 37 | + return { |
| 38 | + ...actual, |
| 39 | + loadAgentIdentities: loadAgentIdentitiesMock, |
| 40 | + loadAgentIdentity: loadAgentIdentityMock, |
| 41 | + }; |
| 42 | +}); |
| 43 | + |
| 44 | +vi.mock("../ui/src/ui/controllers/agent-skills.ts", async (importOriginal) => { |
| 45 | + const actual = await importOriginal<typeof import("../ui/src/ui/controllers/agent-skills.ts")>(); |
| 46 | + return { ...actual, loadAgentSkills: loadAgentSkillsMock }; |
| 47 | +}); |
| 48 | + |
| 49 | +vi.mock("../ui/src/ui/controllers/agent-files.ts", async (importOriginal) => { |
| 50 | + const actual = await importOriginal<typeof import("../ui/src/ui/controllers/agent-files.ts")>(); |
| 51 | + return { ...actual, loadAgentFiles: loadAgentFilesMock }; |
| 52 | +}); |
| 53 | + |
| 54 | +vi.mock("../ui/src/ui/controllers/channels.ts", async (importOriginal) => { |
| 55 | + const actual = await importOriginal<typeof import("../ui/src/ui/controllers/channels.ts")>(); |
| 56 | + return { ...actual, loadChannels: loadChannelsMock }; |
| 57 | +}); |
| 58 | + |
| 59 | +vi.mock("../ui/src/ui/controllers/cron.ts", async (importOriginal) => { |
| 60 | + const actual = await importOriginal<typeof import("../ui/src/ui/controllers/cron.ts")>(); |
| 61 | + return { |
| 62 | + ...actual, |
| 63 | + loadCronJobs: vi.fn(async () => undefined), |
| 64 | + loadCronRuns: vi.fn(async () => undefined), |
| 65 | + loadCronStatus: vi.fn(async () => undefined), |
| 66 | + }; |
| 67 | +}); |
| 68 | + |
| 69 | +import { refreshActiveTab } from "../ui/src/ui/app-settings.ts"; |
| 70 | + |
| 71 | +type AgentsPanel = "overview" | "files" | "tools" | "skills" | "channels" | "cron"; |
| 72 | + |
| 73 | +function createHost(agentsPanel: AgentsPanel): Parameters<typeof refreshActiveTab>[0] { |
| 74 | + return { |
| 75 | + tab: "agents", |
| 76 | + connected: true, |
| 77 | + agentsPanel, |
| 78 | + agentsList: null, |
| 79 | + agentsSelectedId: null, |
| 80 | + settings: { |
| 81 | + gatewayUrl: "", |
| 82 | + token: "", |
| 83 | + sessionKey: "main", |
| 84 | + lastActiveSessionKey: "main", |
| 85 | + theme: "claw", |
| 86 | + themeMode: "system", |
| 87 | + chatFocusMode: false, |
| 88 | + chatShowThinking: true, |
| 89 | + chatShowToolCalls: true, |
| 90 | + splitRatio: 0.6, |
| 91 | + navCollapsed: false, |
| 92 | + navWidth: 220, |
| 93 | + navGroupsCollapsed: {}, |
| 94 | + borderRadius: 50, |
| 95 | + }, |
| 96 | + theme: "claw", |
| 97 | + themeMode: "system", |
| 98 | + themeResolved: "dark", |
| 99 | + applySessionKey: "main", |
| 100 | + sessionKey: "main", |
| 101 | + chatHasAutoScrolled: false, |
| 102 | + logsAtBottom: false, |
| 103 | + eventLog: [], |
| 104 | + eventLogBuffer: [], |
| 105 | + basePath: "", |
| 106 | + } as Parameters<typeof refreshActiveTab>[0]; |
| 107 | +} |
| 108 | + |
| 109 | +describe("refreshActiveTab (agents/files)", () => { |
| 110 | + beforeEach(() => { |
| 111 | + loadAgentsMock.mockClear(); |
| 112 | + loadConfigMock.mockClear(); |
| 113 | + loadAgentIdentitiesMock.mockClear(); |
| 114 | + loadAgentIdentityMock.mockClear(); |
| 115 | + loadAgentSkillsMock.mockClear(); |
| 116 | + loadAgentFilesMock.mockClear(); |
| 117 | + loadChannelsMock.mockClear(); |
| 118 | + }); |
| 119 | + |
| 120 | + it("loads agent files when the active agents panel is files", async () => { |
| 121 | + const host = createHost("files"); |
| 122 | + await refreshActiveTab(host); |
| 123 | + |
| 124 | + expect(loadAgentFilesMock).toHaveBeenCalledTimes(1); |
| 125 | + expect(loadAgentFilesMock).toHaveBeenCalledWith(host, "main"); |
| 126 | + }); |
| 127 | + |
| 128 | + it("does not load agent files on non-files panels", async () => { |
| 129 | + const host = createHost("overview"); |
| 130 | + await refreshActiveTab(host); |
| 131 | + |
| 132 | + expect(loadAgentFilesMock).not.toHaveBeenCalled(); |
| 133 | + }); |
| 134 | +}); |
0 commit comments