mirror of
https://github.com/grp06/openclaw-studio.git
synced 2026-08-14 00:47:51 +00:00
272 lines
7.3 KiB
TypeScript
272 lines
7.3 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
planBootstrapSelection,
|
|
planFocusedFilterPatch,
|
|
planFocusedPreferenceRestore,
|
|
planFocusedSelectionPatch,
|
|
planStartupFleetBootstrapIntent,
|
|
} from "@/features/agents/operations/studioBootstrapWorkflow";
|
|
import type { StudioSettings } from "@/lib/studio/settings";
|
|
|
|
describe("studioBootstrapWorkflow", () => {
|
|
it("keeps existing selection when one is already active", () => {
|
|
const intent = planBootstrapSelection({
|
|
hasCurrentSelection: true,
|
|
preferredSelectedAgentId: "agent-1",
|
|
availableAgentIds: ["agent-1", "agent-2"],
|
|
suggestedSelectedAgentId: "agent-2",
|
|
});
|
|
|
|
expect(intent).toEqual({ initialSelectedAgentId: undefined });
|
|
});
|
|
|
|
it("prefers saved selected agent when present in seeds", () => {
|
|
const intent = planBootstrapSelection({
|
|
hasCurrentSelection: false,
|
|
preferredSelectedAgentId: "agent-2",
|
|
availableAgentIds: ["agent-1", "agent-2"],
|
|
suggestedSelectedAgentId: "agent-1",
|
|
});
|
|
|
|
expect(intent).toEqual({ initialSelectedAgentId: "agent-2" });
|
|
});
|
|
|
|
it("falls back to suggested selected agent when saved preference is unavailable", () => {
|
|
const intent = planBootstrapSelection({
|
|
hasCurrentSelection: false,
|
|
preferredSelectedAgentId: "agent-9",
|
|
availableAgentIds: ["agent-1", "agent-2"],
|
|
suggestedSelectedAgentId: "agent-1",
|
|
});
|
|
|
|
expect(intent).toEqual({ initialSelectedAgentId: "agent-1" });
|
|
});
|
|
|
|
it("builds focused filter patch only when gateway key and touch state allow it", () => {
|
|
expect(
|
|
planFocusedFilterPatch({
|
|
gatewayKey: "",
|
|
focusFilterTouched: true,
|
|
focusFilter: "running",
|
|
})
|
|
).toEqual({ kind: "skip", reason: "missing-gateway-key" });
|
|
|
|
expect(
|
|
planFocusedFilterPatch({
|
|
gatewayKey: "https://gateway.test",
|
|
focusFilterTouched: false,
|
|
focusFilter: "running",
|
|
})
|
|
).toEqual({ kind: "skip", reason: "focus-filter-not-touched" });
|
|
|
|
expect(
|
|
planFocusedFilterPatch({
|
|
gatewayKey: "https://gateway.test",
|
|
focusFilterTouched: true,
|
|
focusFilter: "running",
|
|
})
|
|
).toEqual({
|
|
kind: "patch",
|
|
patch: {
|
|
focused: {
|
|
"https://gateway.test": {
|
|
mode: "focused",
|
|
filter: "running",
|
|
},
|
|
},
|
|
},
|
|
debounceMs: 300,
|
|
});
|
|
});
|
|
|
|
it("builds focused selected-agent patch only when connection and load gates pass", () => {
|
|
expect(
|
|
planFocusedSelectionPatch({
|
|
gatewayKey: "",
|
|
status: "connected",
|
|
focusedPreferencesLoaded: true,
|
|
agentsLoadedOnce: true,
|
|
selectedAgentId: "agent-1",
|
|
})
|
|
).toEqual({ kind: "skip", reason: "missing-gateway-key" });
|
|
|
|
expect(
|
|
planFocusedSelectionPatch({
|
|
gatewayKey: "https://gateway.test",
|
|
status: "connecting",
|
|
focusedPreferencesLoaded: true,
|
|
agentsLoadedOnce: true,
|
|
selectedAgentId: "agent-1",
|
|
})
|
|
).toEqual({ kind: "skip", reason: "not-connected" });
|
|
|
|
expect(
|
|
planFocusedSelectionPatch({
|
|
gatewayKey: "https://gateway.test",
|
|
status: "connected",
|
|
focusedPreferencesLoaded: false,
|
|
agentsLoadedOnce: true,
|
|
selectedAgentId: "agent-1",
|
|
})
|
|
).toEqual({ kind: "skip", reason: "focused-preferences-not-loaded" });
|
|
|
|
expect(
|
|
planFocusedSelectionPatch({
|
|
gatewayKey: "https://gateway.test",
|
|
status: "connected",
|
|
focusedPreferencesLoaded: true,
|
|
agentsLoadedOnce: false,
|
|
selectedAgentId: "agent-1",
|
|
})
|
|
).toEqual({ kind: "skip", reason: "agents-not-loaded" });
|
|
|
|
expect(
|
|
planFocusedSelectionPatch({
|
|
gatewayKey: "https://gateway.test",
|
|
status: "connected",
|
|
focusedPreferencesLoaded: true,
|
|
agentsLoadedOnce: true,
|
|
selectedAgentId: "agent-2",
|
|
lastPersistedSelectedAgentId: "agent-1",
|
|
})
|
|
).toEqual({
|
|
kind: "patch",
|
|
patch: {
|
|
focused: {
|
|
"https://gateway.test": {
|
|
mode: "focused",
|
|
selectedAgentId: "agent-2",
|
|
},
|
|
},
|
|
},
|
|
persistence: "immediate",
|
|
});
|
|
});
|
|
|
|
it("skips focused selected-agent patch when value is unchanged", () => {
|
|
expect(
|
|
planFocusedSelectionPatch({
|
|
gatewayKey: "https://gateway.test",
|
|
status: "connected",
|
|
focusedPreferencesLoaded: true,
|
|
agentsLoadedOnce: true,
|
|
selectedAgentId: "agent-2",
|
|
lastPersistedSelectedAgentId: "agent-2",
|
|
})
|
|
).toEqual({ kind: "skip", reason: "selected-agent-unchanged" });
|
|
});
|
|
|
|
it("resolves focused preference restore values from settings", () => {
|
|
const settings: StudioSettings = {
|
|
version: 1,
|
|
gateway: null,
|
|
gatewayAutoStart: true,
|
|
focused: {
|
|
"https://gateway.test": {
|
|
mode: "focused",
|
|
selectedAgentId: "agent-3",
|
|
filter: "approvals",
|
|
},
|
|
},
|
|
avatars: {},
|
|
};
|
|
|
|
expect(
|
|
planFocusedPreferenceRestore({
|
|
settings,
|
|
gatewayKey: "https://gateway.test",
|
|
focusFilterTouched: false,
|
|
})
|
|
).toEqual({
|
|
preferredSelectedAgentId: "agent-3",
|
|
focusFilter: "all",
|
|
});
|
|
|
|
expect(
|
|
planFocusedPreferenceRestore({
|
|
settings,
|
|
gatewayKey: "https://gateway.unknown",
|
|
focusFilterTouched: false,
|
|
})
|
|
).toEqual({
|
|
preferredSelectedAgentId: null,
|
|
focusFilter: "all",
|
|
});
|
|
});
|
|
|
|
it("restores running filter as all", () => {
|
|
const settings: StudioSettings = {
|
|
version: 1,
|
|
gateway: null,
|
|
gatewayAutoStart: true,
|
|
focused: {
|
|
"https://gateway.test": {
|
|
mode: "focused",
|
|
selectedAgentId: "agent-7",
|
|
filter: "running",
|
|
},
|
|
},
|
|
avatars: {},
|
|
};
|
|
|
|
expect(
|
|
planFocusedPreferenceRestore({
|
|
settings,
|
|
gatewayKey: "https://gateway.test",
|
|
focusFilterTouched: false,
|
|
})
|
|
).toEqual({
|
|
preferredSelectedAgentId: "agent-7",
|
|
focusFilter: "all",
|
|
});
|
|
});
|
|
|
|
it("plans startup fleet bootstrap only once per gateway/mode key", () => {
|
|
expect(
|
|
planStartupFleetBootstrapIntent({
|
|
coreConnected: true,
|
|
focusedPreferencesLoaded: true,
|
|
hasRestartingMutationBlock: false,
|
|
hasCreateAgentBlock: false,
|
|
gatewayUrl: "https://gateway.test",
|
|
lastCompletedKey: null,
|
|
inFlightKey: null,
|
|
})
|
|
).toEqual({
|
|
kind: "load",
|
|
key: "domain:https://gateway.test",
|
|
});
|
|
|
|
expect(
|
|
planStartupFleetBootstrapIntent({
|
|
coreConnected: true,
|
|
focusedPreferencesLoaded: true,
|
|
hasRestartingMutationBlock: false,
|
|
hasCreateAgentBlock: false,
|
|
gatewayUrl: "https://gateway.test",
|
|
lastCompletedKey: "domain:https://gateway.test",
|
|
inFlightKey: null,
|
|
})
|
|
).toEqual({
|
|
kind: "skip",
|
|
reason: "already-loaded",
|
|
});
|
|
|
|
expect(
|
|
planStartupFleetBootstrapIntent({
|
|
coreConnected: true,
|
|
focusedPreferencesLoaded: true,
|
|
hasRestartingMutationBlock: false,
|
|
hasCreateAgentBlock: false,
|
|
gatewayUrl: "https://gateway.test",
|
|
lastCompletedKey: null,
|
|
inFlightKey: "domain:https://gateway.test",
|
|
})
|
|
).toEqual({
|
|
kind: "skip",
|
|
reason: "in-flight",
|
|
});
|
|
});
|
|
});
|