File size: 1,667 Bytes
3201ca6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { beforeEach, describe, expect, it, vi } from "vitest";
import {
  setPendingTaskAttachments,
  usePendingTaskAttachmentsStore,
} from "#/stores/pending-task-attachments-store";
import { flushPendingTaskAttachments } from "#/utils/flush-pending-task-attachments";

const sendMessageWithAttachments = vi.fn();

vi.mock("#/utils/send-message-with-attachments", () => ({
  sendMessageWithAttachments: (...args: unknown[]) =>
    sendMessageWithAttachments(...args),
}));

vi.mock("#/i18n", () => ({
  default: { getFixedT: () => (key: string) => key },
  OPENHANDS_I18N_NAMESPACE: "openhands",
  waitForI18n: vi.fn().mockResolvedValue(undefined),
}));

describe("flushPendingTaskAttachments", () => {
  beforeEach(() => {
    vi.clearAllMocks();
    usePendingTaskAttachmentsStore.setState({ byTaskId: {} });

    sendMessageWithAttachments.mockResolvedValue({
      text: "hello",
      content: "hello",
      imageUrls: ["data:image/png;base64,abc"],
      fileUrls: [],
      timestamp: "2020-01-01T00:00:00.000Z",
    });
  });

  it("sends queued attachments to the real conversation id", async () => {
    const image = new File(["x"], "shot.png", { type: "image/png" });
    setPendingTaskAttachments("start-task-id", {
      content: "hello",
      images: [image],
      files: [],
      imagesMarkedUploadAsFile: [],
    });

    await flushPendingTaskAttachments(
      "start-task-id",
      "550e8400-e29b-41d4-a716-446655440000",
    );

    expect(sendMessageWithAttachments).toHaveBeenCalledWith(
      expect.objectContaining({
        conversationId: "550e8400-e29b-41d4-a716-446655440000",
        content: "hello",
      }),
    );
  });
});