Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Test a failed first /new message through the conversation the store actually creates.
Shreerama9c4d55f773926
diff --git a/src/components/Composer.slash.test.ts b/src/components/Composer.slash.test.tsindex 4dddbbb1a3..ef21ba2de4 100644--- a/src/components/Composer.slash.test.ts+++ b/src/components/Composer.slash.test.ts@@ -5,7 +5,7 @@ import { flushSync } from "react-dom";import { createRoot, type Root } from "react-dom/client";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";import type { Bot, Group } from "@/state/store";-import { getDraft, getDraftAttachments, noteNewChatLanding, restoreComposerDraft } from "@/lib/drafts";+import { getDraft, getDraftAttachments, restoreComposerDraft } from "@/lib/drafts";import { newChatThreadId } from "@/state/new-chat";const dispatch = vi.hoisted(() => vi.fn());@@ -271,22 +271,17 @@ describe("the / menu in the composer", () => {expect(getDraft(localStorage, nextDraft)).toBe("ship the notes");expect(getDraftAttachments(localStorage, nextDraft)).toEqual([file]);expect(input.value).toBe("");+ restoreComposerDraft(nextDraft, { text: "", attachments: [] });});- it("does not reply to the old conversation, and a failed first turn returns to the conversation that was made", () => {+ it("does not carry a reply from the old conversation into the new one", () => {const earlier = { id: "visible-leaf", role: "user" as const, kind: "text" as const, text: "Earlier", at: 1 };- const started: Bot = { ...bot, messages: [earlier] };- flushSync(() => root.render(createElement(Composer, { bot: started, replyTo: earlier })));+ flushSync(() => root.render(createElement(Composer, { bot: { ...bot, messages: [earlier] }, replyTo: earlier })));const input = fill("/new ship the notes");press(input, "Enter");const sent = dispatch.mock.calls.map(([action]) => action).find((action) => action.type === "send");- expect(sent.replyToId).toBeUndefined();- expect(sent.threadId).toBe(newChatThreadId(bot.id));- restoreComposerDraft(`bot:${bot.id}:${newChatThreadId(bot.id)}`, { text: "", attachments: [] });- noteNewChatLanding(sent.sendId, "made-thread");- flushSync(() => sent.onError());- expect(getDraft(localStorage, `bot:${bot.id}:made-thread`)).toBe("ship the notes");- expect(getDraft(localStorage, `bot:${bot.id}:${newChatThreadId(bot.id)}`)).toBe("");+ expect(sent).toEqual(expect.objectContaining({ threadId: newChatThreadId(bot.id), text: "ship the notes", replyToId: undefined }));+ expect(input.value).toBe("");});it("clears a bare /new in a room and does not post it", () => {@@ -300,6 +295,9 @@ describe("the / menu in the composer", () => {press(input, "Enter");expect(dispatch).not.toHaveBeenCalled();expect(input.value).toBe("");+ const again = fill("/new ship the notes", "/new ship the notes".length, roomDraft);+ press(again, "Enter");+ expect(dispatch).toHaveBeenCalledWith(expect.objectContaining({ type: "sendGroup", groupId: room.id, text: "/new ship the notes" }));restoreComposerDraft(roomDraft, { text: "", attachments: [] });});diff --git a/src/components/Composer.tsx b/src/components/Composer.tsxindex 458ce19d0c..fd4447b043 100644--- a/src/components/Composer.tsx+++ b/src/components/Composer.tsx@@ -625,8 +625,8 @@ export function Composer({});roomMessageSent(effectiveChannelMode, busy);} else if (bot) {- if (freshPrompt && startedThread) dispatch({ type: "newTask", botId: bot.id }); const points = effectiveChannelMode === "goal" ? null : takePointedChips(sentThread);- const page = effectiveChannelMode === "goal" ? undefined : openPageFor(sentThread);+ if (freshPrompt && startedThread) dispatch({ type: "newTask", botId: bot.id });+ const points = effectiveChannelMode === "goal" ? null : takePointedChips(sentThread); const page = effectiveChannelMode === "goal" ? undefined : openPageFor(sentThread);dispatch({type: "send", botId: bot.id, text: body, sendId: sentDraft.sendId, replyToId: sentThread === threadId ? replyTo?.id : undefined, threadId: sentThread, dataContext, mode: effectiveChannelMode, mentions: sentDraft.mentions,...(points ? { pointed: points.pointed } : {}), ...(page ? { openPage: page } : {}), ...(followUp ? { followUp } : {}),diff --git a/src/state/new-chat.test.ts b/src/state/new-chat.test.tsindex d81ec55aa8..7568b498b4 100644--- a/src/state/new-chat.test.ts+++ b/src/state/new-chat.test.ts@@ -3,6 +3,7 @@ import { createElement, type Dispatch } from "react";import { flushSync } from "react-dom";import { createRoot, type Root } from "react-dom/client";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import { getDraft, recoverFailedComposerSend } from "@/lib/drafts";import { StoreProvider, useStore, type Action, type AppState, type Bot, type Message } from "./store";import { newChatBot, newChatThreadId } from "./new-chat";@@ -38,6 +39,7 @@ let creates: Array<{ projectId?: string }>;let sends: Array<{ threadId?: string; text: string }>;let taskPatches: string[];let createAnswer: Promise<Response> | null;+let rejectMessage: boolean;let switchAnswer: Promise<Response> | null;let patchAnswer: Promise<Response> | null;@@ -50,6 +52,7 @@ beforeEach(async () => {sends = [];taskPatches = [];createAnswer = null;+ rejectMessage = false;switchAnswer = null;patchAnswer = null;const stored = new Map<string, string>();@@ -67,6 +70,7 @@ beforeEach(async () => {if (path === "/api/bots/bot/messages") {const body = JSON.parse(String(init?.body));sends.push(body);+ if (rejectMessage) return json({ error: "not sent" }, 500);return json({ threadId: body.threadId, message: { ...line(body.sendId), sendId: body.sendId } });}if (/^\/api\/bots\/bot\/tasks\/[^/]+$/.test(path) && init?.method === "PATCH") {@@ -287,6 +291,23 @@ describe("a new conversation opens blank and is made by its first send", () => {expect(creates).toEqual([{ projectId: "folder-1" }]);});+ it("puts a failed first message back in the conversation that was created", async () => {+ rejectMessage = true;+ const failed = vi.fn();+ openBlank();+ send("Hi", { onError: () => {+ failed();+ recoverFailedComposerSend({+ draftId: `bot:bot:${DRAFT}`, revision: 0, attachments: [], sendId: "send-Hi", text: "Hi", requestText: "Hi", threadId: DRAFT,+ });+ } });+ await vi.waitFor(() => expect(failed).toHaveBeenCalledOnce());++ expect(sends).toEqual([expect.objectContaining({ threadId: "made", text: "Hi" })]);+ expect(getDraft(localStorage, "bot:bot:made")).toBe("Hi");+ expect(getDraft(localStorage, `bot:bot:${DRAFT}`)).toBe("");+ });+it("gives the message back when the conversation cannot be made, and makes nothing", async () => {createAnswer = Promise.resolve(json({ error: "no room for another" }, 409));const failed = vi.fn();