File size: 3,807 Bytes
fd2c364
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
127
128
129
130
131
132
133
134
135
136
137
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import {
  __resetActiveStoreForTests,
  setActiveSelection,
  setRegisteredBackends,
} from "#/api/backend-registry/active-store";
import type { Backend } from "#/api/backend-registry/types";
import SkillsService from "#/api/skills-service";
import { getFetchCall, mockJsonResponse } from "./fetch-test-utils";

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

const originalFetch = global.fetch;
const fetchMock = vi.fn();

beforeEach(() => {
  window.localStorage.clear();
  __resetActiveStoreForTests();
  setRegisteredBackends([cloudBackend]);
  setActiveSelection({ backendId: cloudBackend.id });
  fetchMock.mockReset();
  global.fetch = fetchMock as typeof fetch;
});

afterEach(() => {
  window.localStorage.clear();
  __resetActiveStoreForTests();
  fetchMock.mockReset();
  global.fetch = originalFetch;
});

describe("SkillsService.getSkills against cloud backend", () => {
  it("paginates /api/v1/skills/search directly and returns the merged list", async () => {
    fetchMock
      .mockResolvedValueOnce(
        mockJsonResponse({
          items: [
            { name: "alpha", type: "knowledge", source: "global" },
            {
              name: "beta",
              type: "task",
              source: "user",
              triggers: ["foo"],
            },
          ],
          next_page_id: "beta",
        }),
      )
      .mockResolvedValueOnce(
        mockJsonResponse({
          items: [{ name: "gamma", type: "knowledge", source: "user" }],
          next_page_id: null,
        }),
      );

    const skills = await SkillsService.getSkills();

    expect(fetchMock).toHaveBeenCalledTimes(2);

    const [firstUrl, firstInit] = getFetchCall(fetchMock, 0);
    expect(firstInit).toMatchObject({
      method: "GET",
      headers: { Authorization: "Bearer bearer-token" },
    });
    expect(firstUrl).toMatch(
      /^https:\/\/app\.all-hands\.dev\/api\/v1\/skills\/search\?/,
    );
    expect(firstUrl).not.toContain("page_id=");

    const [secondUrl] = getFetchCall(fetchMock, 1);
    expect(secondUrl).toContain("page_id=beta");

    expect(skills.map((s) => s.name)).toEqual(["alpha", "beta", "gamma"]);
    expect(skills[1]).toMatchObject({ triggers: ["foo"] });
  });
});

describe("SkillsService.getConversationSkills against cloud backend", () => {
  it("reads the conversation's own skills route and maps entries to SkillInfo", async () => {
    // Arrange
    fetchMock.mockResolvedValueOnce(
      mockJsonResponse({
        skills: [
          {
            name: "release-notes",
            type: "agentskills",
            content: "# Release notes",
            triggers: ["/release-notes"],
          },
          {
            name: "repo",
            type: "repo",
            content: "Repository instructions",
            triggers: [],
          },
        ],
      }),
    );

    // Act
    const skills = await SkillsService.getConversationSkills("conv-1");

    // Assert
    expect(fetchMock).toHaveBeenCalledTimes(1);
    const [url, init] = getFetchCall(fetchMock, 0);
    expect(url).toBe(
      "https://app.all-hands.dev/api/v1/app-conversations/conv-1/skills",
    );
    expect(init).toMatchObject({
      method: "GET",
      headers: { Authorization: "Bearer bearer-token" },
    });
    expect(skills).toEqual([
      {
        name: "release-notes",
        type: "agentskills",
        content: "# Release notes",
        triggers: ["/release-notes"],
        source: null,
      },
      {
        name: "repo",
        type: "repo",
        content: "Repository instructions",
        triggers: [],
        source: null,
      },
    ]);
  });
});