Skip to content

πŸ™Œ OpenHands: AI-Driven Development

test: strengthen optimistic message store (#17328)

aivong-openhands7ad87096baa8
diff --git a/__tests__/stores/optimistic-user-message-store.test.ts b/__tests__/stores/optimistic-user-message-store.test.ts
index fe23f93307..c768ae6c73 100644
--- a/__tests__/stores/optimistic-user-message-store.test.ts
+++ b/__tests__/stores/optimistic-user-message-store.test.ts
@@ -65,13 +65,21 @@ describe("optimistic-user-message-store", () => {
conversationId: CONVO,
text: "broken",
});
+ const otherId = store.enqueuePendingMessage({
+ conversationId: CONVO,
+ text: "other",
+ });
store.markPendingMessageError(id, "boom");
+ store.markPendingMessageError(otherId, "other boom");
store.markPendingMessageSending(id);
- const [entry] = useOptimisticUserMessageStore.getState().pendingMessages;
+ const [entry, other] =
+ useOptimisticUserMessageStore.getState().pendingMessages;
expect(entry.status).toBe("sending");
expect(entry.errorMessage).toBeUndefined();
+ expect(other.status).toBe("error");
+ expect(other.errorMessage).toBe("other boom");
});
it("enqueue stores `content` separately from `text` and defaults it to `text`", () => {
@@ -111,8 +119,7 @@ describe("optimistic-user-message-store", () => {
const consumed = store.consumeMatchingPendingMessage(CONVO, "second");
expect(consumed?.id).toBe(secondId);
- const remaining =
- useOptimisticUserMessageStore.getState().pendingMessages;
+ const remaining = useOptimisticUserMessageStore.getState().pendingMessages;
expect(remaining).toHaveLength(1);
expect(remaining[0].id).toBe(firstId);
});
@@ -153,8 +160,7 @@ describe("optimistic-user-message-store", () => {
const consumed = store.consumeMatchingPendingMessage(CONVO, "second");
expect(consumed?.id).toBe(secondId);
- const remaining =
- useOptimisticUserMessageStore.getState().pendingMessages;
+ const remaining = useOptimisticUserMessageStore.getState().pendingMessages;
expect(remaining).toHaveLength(1);
expect(remaining[0].id).toBe(firstId);
expect(remaining[0].status).toBe("error");
@@ -192,8 +198,7 @@ describe("optimistic-user-message-store", () => {
const consumed = store.consumeMatchingPendingMessage("conv-b", "shared");
expect(consumed?.id).toBe(bId);
- const remaining =
- useOptimisticUserMessageStore.getState().pendingMessages;
+ const remaining = useOptimisticUserMessageStore.getState().pendingMessages;
expect(remaining).toHaveLength(1);
expect(remaining[0].id).toBe(aId);
});
@@ -237,13 +242,21 @@ describe("optimistic-user-message-store", () => {
conversationId: CONVO,
text: "explicit-error",
});
+ const waitingId = store.enqueuePendingMessage({
+ conversationId: CONVO,
+ text: "still-waiting",
+ });
store.markPendingMessageError(id, "boom");
vi.advanceTimersByTime(PENDING_MESSAGE_TIMEOUT_MS);
- const [entry] = useOptimisticUserMessageStore.getState().pendingMessages;
+ const [entry, waiting] =
+ useOptimisticUserMessageStore.getState().pendingMessages;
// Should keep the original error message, not get overwritten to "Send timed out".
expect(entry.errorMessage).toBe("boom");
+ expect(waiting.id).toBe(waitingId);
+ expect(waiting.status).toBe("error");
+ expect(waiting.errorMessage).toBe("Send timed out");
});
it("removePendingMessage drops a specific entry by id", () => {
@@ -256,8 +269,7 @@ describe("optimistic-user-message-store", () => {
store.removePendingMessage(firstId);
- const remaining =
- useOptimisticUserMessageStore.getState().pendingMessages;
+ const remaining = useOptimisticUserMessageStore.getState().pendingMessages;
expect(remaining.map((m) => m.text)).toEqual(["second"]);
});
@@ -292,4 +304,68 @@ describe("optimistic-user-message-store", () => {
["other-convo", "untouched"],
]);
});
+
+ it("creates a complete fresh store whose actions remain scoped", async () => {
+ const randomSpy = vi
+ .spyOn(Math, "random")
+ .mockReturnValueOnce(0.5)
+ .mockReturnValue(0.25);
+ vi.resetModules();
+
+ try {
+ const { useOptimisticUserMessageStore: freshStore } =
+ await import("#/stores/optimistic-user-message-store");
+ expect(freshStore.getState().pendingMessages).toEqual([]);
+
+ const firstId = freshStore.getState().enqueuePendingMessage({
+ conversationId: CONVO,
+ text: "first",
+ });
+ const secondId = freshStore.getState().enqueuePendingMessage({
+ conversationId: "conv-b",
+ text: "second",
+ });
+ expect(typeof firstId).toBe("string");
+ expect(typeof secondId).toBe("string");
+ expect(firstId).not.toBe(secondId);
+
+ freshStore.getState().markPendingMessageError(firstId, "failed");
+ expect(
+ freshStore
+ .getState()
+ .pendingMessages.map(({ text, status, errorMessage }) => [
+ text,
+ status,
+ errorMessage,
+ ]),
+ ).toEqual([
+ ["first", "error", "failed"],
+ ["second", "sending", undefined],
+ ]);
+
+ freshStore.getState().markPendingMessageSending(firstId);
+ expect(freshStore.getState().pendingMessages[0]).toMatchObject({
+ id: firstId,
+ status: "sending",
+ errorMessage: undefined,
+ });
+
+ freshStore.getState().reassignPendingMessages("conv-b", "moved");
+ expect(
+ freshStore
+ .getState()
+ .pendingMessages.map(({ conversationId }) => conversationId),
+ ).toEqual([CONVO, "moved"]);
+
+ freshStore.getState().removePendingMessage(firstId);
+ expect(freshStore.getState().pendingMessages).toHaveLength(1);
+ expect(freshStore.getState().pendingMessages[0].id).toBe(secondId);
+
+ freshStore.getState().clearPendingMessages();
+ expect(freshStore.getState().pendingMessages).toEqual([]);
+ } finally {
+ randomSpy.mockRestore();
+ vi.resetModules();
+ }
+ });
});