Skip to content

πŸ™Œ OpenHands: AI-Driven Development

test: cover small state stores (#17326)

aivong-openhands2a11a9be023d
diff --git a/__tests__/stores/btw-store.test.ts b/__tests__/stores/btw-store.test.ts
index d96d7dead4..004ea8ba2c 100644
--- a/__tests__/stores/btw-store.test.ts
+++ b/__tests__/stores/btw-store.test.ts
@@ -1,4 +1,4 @@
-import { beforeEach, describe, expect, it } from "vitest";
+import { beforeEach, describe, expect, it, vi } from "vitest";
import { useBtwStore } from "#/stores/btw-store";
const CONV_A = "conv-a";
@@ -22,6 +22,7 @@ describe("btw store", () => {
it("resolve and fail update status and response", () => {
const id = useBtwStore.getState().addPending(CONV_A, "why?");
+ const otherId = useBtwStore.getState().addPending(CONV_A, "what?");
useBtwStore.getState().resolve(CONV_A, id, "because");
expect(entriesFor(CONV_A)[0]).toMatchObject({
status: "done",
@@ -32,13 +33,86 @@ describe("btw store", () => {
status: "error",
response: "boom",
});
+ expect(entriesFor(CONV_A)[1]).toEqual({
+ id: otherId,
+ question: "what?",
+ status: "pending",
+ });
});
it("dismiss removes only the targeted entry in the scoped conversation", () => {
const aId = useBtwStore.getState().addPending(CONV_A, "qa");
+ const remainingId = useBtwStore.getState().addPending(CONV_A, "still here");
useBtwStore.getState().addPending(CONV_B, "qb");
useBtwStore.getState().dismiss(CONV_A, aId);
- expect(entriesFor(CONV_A)).toEqual([]);
+ expect(entriesFor(CONV_A)).toEqual([
+ {
+ id: remainingId,
+ question: "still here",
+ status: "pending",
+ },
+ ]);
expect(entriesFor(CONV_B)).toHaveLength(1);
});
+
+ it("creates a complete fresh store with its named devtools connection", async () => {
+ const connection = {
+ init: vi.fn(),
+ send: vi.fn(),
+ subscribe: vi.fn(),
+ };
+ const connect = vi.fn(() => connection);
+ const previousExtension = Object.getOwnPropertyDescriptor(
+ window,
+ "__REDUX_DEVTOOLS_EXTENSION__",
+ );
+ Object.defineProperty(window, "__REDUX_DEVTOOLS_EXTENSION__", {
+ configurable: true,
+ value: { connect },
+ });
+ vi.resetModules();
+
+ try {
+ const { useBtwStore: freshStore } = await import("#/stores/btw-store");
+
+ expect(connect).toHaveBeenCalledWith({ name: "BtwStore" });
+ expect(freshStore.getState().entriesByConversation).toEqual({});
+
+ const dismissedId = freshStore
+ .getState()
+ .addPending(CONV_A, "dismiss me");
+ const resolvedId = freshStore.getState().addPending(CONV_A, "resolve me");
+ const failedId = freshStore.getState().addPending(CONV_A, "fail me");
+ freshStore.getState().resolve(CONV_A, resolvedId, "resolved");
+ freshStore.getState().fail(CONV_A, failedId, "failed");
+ freshStore.getState().dismiss(CONV_A, dismissedId);
+
+ expect(freshStore.getState().entriesByConversation[CONV_A]).toEqual([
+ {
+ id: resolvedId,
+ question: "resolve me",
+ response: "resolved",
+ status: "done",
+ },
+ {
+ id: failedId,
+ question: "fail me",
+ response: "failed",
+ status: "error",
+ },
+ ]);
+ freshStore.devtools?.cleanup();
+ } finally {
+ if (previousExtension) {
+ Object.defineProperty(
+ window,
+ "__REDUX_DEVTOOLS_EXTENSION__",
+ previousExtension,
+ );
+ } else {
+ Reflect.deleteProperty(window, "__REDUX_DEVTOOLS_EXTENSION__");
+ }
+ vi.resetModules();
+ }
+ });
});
diff --git a/__tests__/stores/model-store.test.ts b/__tests__/stores/model-store.test.ts
index e8976f2180..1ff123d3dc 100644
--- a/__tests__/stores/model-store.test.ts
+++ b/__tests__/stores/model-store.test.ts
@@ -1,4 +1,4 @@
-import { beforeEach, describe, expect, it } from "vitest";
+import { beforeEach, describe, expect, it, vi } from "vitest";
import { useModelStore } from "#/stores/model-store";
import type { ProfileInfo } from "#/api/profiles-service/profiles-service.api";
@@ -61,12 +61,16 @@ describe("model store", () => {
it("clearActiveProfile drops only the optimistic profile entry", () => {
useModelStore.getState().show(CONV_A, "event-1", [profile]);
useModelStore.getState().recordSwitch(CONV_A, "event-2", "gpt");
+ useModelStore.getState().recordSwitch(CONV_B, "event-3", "haiku");
useModelStore.getState().clearActiveProfile(CONV_A);
expect(
useModelStore.getState().activeProfileByConversation[CONV_A],
).toBeUndefined();
+ expect(useModelStore.getState().activeProfileByConversation[CONV_B]).toBe(
+ "haiku",
+ );
// Chat-history entries for the conversation are preserved.
expect(entriesFor(CONV_A)).toHaveLength(2);
});
@@ -79,9 +83,125 @@ describe("model store", () => {
expect(entriesFor(CONV_A)).toEqual([]);
expect(entriesFor(CONV_B)).toHaveLength(1);
+ expect(useModelStore.getState().activeProfileByConversation[CONV_B]).toBe(
+ "gpt",
+ );
useModelStore.getState().clearAll();
expect(entriesFor(CONV_B)).toEqual([]);
+ expect(useModelStore.getState().activeProfileByConversation).toEqual({});
+ });
+
+ it("seeds historical switches once while preserving live entries", () => {
+ useModelStore
+ .getState()
+ .seedSwitches(CONV_B, [
+ { id: "first", anchorEventId: null, profileName: "seeded" },
+ ]);
+ expect(entriesFor(CONV_B)).toHaveLength(1);
+
+ useModelStore.getState().show(CONV_A, "live-event", [profile]);
+ const liveId = entriesFor(CONV_A)[0].id;
+ const switches = [
+ { id: "switch-1", anchorEventId: null, profileName: "haiku" },
+ { id: "switch-2", anchorEventId: "event-2", profileName: "gpt" },
+ { id: liveId, anchorEventId: "duplicate", profileName: "ignored" },
+ ];
+
+ useModelStore.getState().seedSwitches(CONV_A, switches);
+ expect(entriesFor(CONV_A)).toEqual([
+ expect.objectContaining({ id: liveId, profiles: [profile] }),
+ {
+ id: "switch-1",
+ anchorEventId: null,
+ profiles: [],
+ switchedTo: "haiku",
+ },
+ {
+ id: "switch-2",
+ anchorEventId: "event-2",
+ profiles: [],
+ switchedTo: "gpt",
+ },
+ ]);
+
+ const beforeDuplicateSeed = useModelStore.getState();
+ useModelStore.getState().seedSwitches(CONV_A, switches);
+ expect(useModelStore.getState()).toBe(beforeDuplicateSeed);
+ });
+
+ it("does nothing when clearing a missing optimistic profile", () => {
+ const before = useModelStore.getState();
+ useModelStore.getState().clearActiveProfile("missing");
+ expect(useModelStore.getState()).toBe(before);
+ });
+
+ it("creates a complete fresh store with its named devtools connection", async () => {
+ const connection = {
+ init: vi.fn(),
+ send: vi.fn(),
+ subscribe: vi.fn(),
+ };
+ const connect = vi.fn(() => connection);
+ const previousExtension = Object.getOwnPropertyDescriptor(
+ window,
+ "__REDUX_DEVTOOLS_EXTENSION__",
+ );
+ Object.defineProperty(window, "__REDUX_DEVTOOLS_EXTENSION__", {
+ configurable: true,
+ value: { connect },
+ });
+ vi.resetModules();
+
+ try {
+ const { useModelStore: freshStore } =
+ await import("#/stores/model-store");
+
+ expect(connect).toHaveBeenCalledWith({ name: "ModelStore" });
+ expect(freshStore.getState().entriesByConversation).toEqual({});
+ expect(freshStore.getState().activeProfileByConversation).toEqual({});
+
+ freshStore.getState().show(CONV_A, "event-1", [profile]);
+ freshStore.getState().recordSwitch(CONV_B, "event-2", "gpt");
+ freshStore
+ .getState()
+ .seedSwitches(CONV_A, [
+ { id: "seeded", anchorEventId: null, profileName: "haiku" },
+ ]);
+ expect(freshStore.getState().entriesByConversation[CONV_A]).toHaveLength(
+ 2,
+ );
+ expect(freshStore.getState().activeProfileByConversation[CONV_B]).toBe(
+ "gpt",
+ );
+
+ freshStore.getState().clearActiveProfile(CONV_B);
+ expect(freshStore.getState().activeProfileByConversation).toEqual({});
+
+ freshStore.getState().clear(CONV_A);
+ expect(
+ freshStore.getState().entriesByConversation[CONV_A],
+ ).toBeUndefined();
+ expect(freshStore.getState().entriesByConversation[CONV_B]).toHaveLength(
+ 1,
+ );
+
+ freshStore.getState().clearAll();
+ expect(freshStore.getState().entriesByConversation).toEqual({});
+ expect(freshStore.getState().activeProfileByConversation).toEqual({});
+ freshStore.devtools?.cleanup();
+ } finally {
+ if (previousExtension) {
+ Object.defineProperty(
+ window,
+ "__REDUX_DEVTOOLS_EXTENSION__",
+ previousExtension,
+ );
+ } else {
+ Reflect.deleteProperty(window, "__REDUX_DEVTOOLS_EXTENSION__");
+ }
+ vi.resetModules();
+ }
});
});
diff --git a/__tests__/stores/pinned-conversations-store.test.ts b/__tests__/stores/pinned-conversations-store.test.ts
index d806e0d7a4..306fa6489c 100644
--- a/__tests__/stores/pinned-conversations-store.test.ts
+++ b/__tests__/stores/pinned-conversations-store.test.ts
@@ -1,4 +1,4 @@
-import { beforeEach, describe, expect, it } from "vitest";
+import { beforeEach, describe, expect, it, vi } from "vitest";
import { usePinnedConversationsStore } from "#/stores/pinned-conversations-store";
const STORAGE_KEY = "pinned-conversations";
@@ -44,12 +44,16 @@ describe("pinned-conversations store", () => {
usePinnedConversationsStore.getState().pinsByBackendId[BACKEND_ID],
).toEqual(["conversation-a"]);
+ usePinnedConversationsStore
+ .getState()
+ .togglePin(BACKEND_ID, "conversation-b");
+
usePinnedConversationsStore
.getState()
.togglePin(BACKEND_ID, "conversation-a");
expect(
usePinnedConversationsStore.getState().pinsByBackendId[BACKEND_ID],
- ).toEqual([]);
+ ).toEqual(["conversation-b"]);
});
it("prunes missing conversations and persists pin order", () => {
@@ -74,4 +78,53 @@ describe("pinned-conversations store", () => {
"conversation-b",
]);
});
+
+ it("does not change state when unpinning or pruning already-current pins", () => {
+ const initial = usePinnedConversationsStore.getState().pinsByBackendId;
+ usePinnedConversationsStore
+ .getState()
+ .unpinConversation(BACKEND_ID, "missing");
+ expect(usePinnedConversationsStore.getState().pinsByBackendId).toBe(
+ initial,
+ );
+
+ usePinnedConversationsStore
+ .getState()
+ .pinConversation(BACKEND_ID, "conversation-a");
+ const pinned = usePinnedConversationsStore.getState().pinsByBackendId;
+ usePinnedConversationsStore
+ .getState()
+ .pruneMissingConversations(BACKEND_ID, ["conversation-a", "other"]);
+ expect(usePinnedConversationsStore.getState().pinsByBackendId).toBe(pinned);
+ });
+
+ it("creates a complete fresh store with scoped persistence", async () => {
+ window.localStorage.clear();
+ vi.resetModules();
+
+ try {
+ const { usePinnedConversationsStore: freshStore } =
+ await import("#/stores/pinned-conversations-store");
+
+ expect(freshStore.getState().pinsByBackendId).toEqual({});
+ freshStore.getState().pinConversation(BACKEND_ID, "conversation-a");
+
+ expect(freshStore.getState().pinsByBackendId).toEqual({
+ [BACKEND_ID]: ["conversation-a"],
+ });
+ expect(
+ JSON.parse(window.localStorage.getItem(STORAGE_KEY) ?? "{}"),
+ ).toEqual({
+ state: {
+ pinsByBackendId: {
+ [BACKEND_ID]: ["conversation-a"],
+ },
+ },
+ version: 0,
+ });
+ } finally {
+ window.localStorage.clear();
+ vi.resetModules();
+ }
+ });
});