File size: 4,010 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import {
  __resetActiveStoreForTests,
  setActiveSelection,
  setRegisteredBackends,
} from "#/api/backend-registry/active-store";
import EventService from "#/api/event-service/event-service.api";
import { callCloudProxy } from "#/api/cloud/proxy";
import type { Backend } from "#/api/backend-registry/types";

vi.mock("#/api/cloud/proxy", () => ({
  callCloudProxy: vi.fn(),
}));

const cloudBackend: Backend = {
  id: "cloud-1",
  name: "Production",
  host: "https://app.all-hands.dev",
  apiKey: "cloud-key",
  kind: "cloud",
};

beforeEach(() => {
  window.localStorage.clear();
  __resetActiveStoreForTests();
  setRegisteredBackends([cloudBackend]);
  setActiveSelection({ backendId: cloudBackend.id, orgId: "org-1" });
  vi.mocked(callCloudProxy).mockReset();
  vi.mocked(callCloudProxy).mockResolvedValue({
    items: [],
    next_page_id: null,
  });
});

afterEach(() => {
  window.localStorage.clear();
  __resetActiveStoreForTests();
  vi.mocked(callCloudProxy).mockReset();
});

describe("EventService.searchEvents — cloud branch", () => {
  it("forwards all pagination params to the cloud proxy and clamps limit to <=100", async () => {
    const options = {
      limit: 500,
      sortOrder: "TIMESTAMP_DESC" as const,
      pageId: "p1",
      timestampGte: "2026-05-01T00:00:00.000000",
      timestampLt: "2026-05-12T07:20:29.087853",
    };

    await EventService.searchEvents("conv-1", null, null, options);

    const proxyCall = vi.mocked(callCloudProxy).mock.calls[0][0];
    const url = new URL(`https://x${proxyCall.path}`);
    expect(url.searchParams.get("limit")).toBe("100");
    expect(url.searchParams.get("sort_order")).toBe("TIMESTAMP_DESC");
    expect(url.searchParams.get("page_id")).toBe("p1");
    expect(url.searchParams.get("timestamp__gte")).toBe(
      "2026-05-01T00:00:00.000000",
    );
    expect(url.searchParams.get("timestamp__lt")).toBe(
      "2026-05-12T07:20:29.087853",
    );
  });

  it("sends only limit when no filter params are provided", async () => {
    await EventService.searchEvents("conv-1", null, null, { limit: 50 });

    const proxyCall = vi.mocked(callCloudProxy).mock.calls[0][0];
    expect(proxyCall.path).toBe(
      "/api/v1/conversation/conv-1/events/search?limit=50",
    );
  });

  it("returns empty page when full-param request fails (graceful degradation)", async () => {
    const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});

    vi.mocked(callCloudProxy).mockRejectedValueOnce(
      new Error("Internal Server Error"),
    );

    const result = await EventService.searchEvents("conv-1", null, null, {
      limit: 50,
      sortOrder: "TIMESTAMP_DESC",
      timestampLt: "2026-05-12T00:00:00.000000",
    });

    // Only one call — no retry, just returns empty page to stop pagination.
    expect(vi.mocked(callCloudProxy)).toHaveBeenCalledTimes(1);
    expect(result.items).toHaveLength(0);
    expect(result.next_page_id).toBeNull();
    expect(warnSpy).toHaveBeenCalledWith(
      expect.stringContaining("doesn't support pagination filters"),
    );

    warnSpy.mockRestore();
  });

  it("rethrows when a limit-only request (no filter params) fails", async () => {
    vi.mocked(callCloudProxy).mockRejectedValueOnce(
      new Error("Network error"),
    );

    await expect(
      EventService.searchEvents("conv-1", null, null, { limit: 50 }),
    ).rejects.toThrow("Network error");

    expect(vi.mocked(callCloudProxy)).toHaveBeenCalledTimes(1);
  });

  it("stops pagination when server returns fewer items than limit", async () => {
    vi.mocked(callCloudProxy).mockResolvedValueOnce({
      items: [{ id: "evt-1" }, { id: "evt-2" }],
      next_page_id: null,
    });

    const result = await EventService.searchEvents("conv-1", null, null, {
      limit: 50,
      sortOrder: "TIMESTAMP_DESC",
    });

    expect(result.items).toHaveLength(2);
    expect(result.next_page_id).toBeNull();
  });
});