File size: 1,378 Bytes
63522a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { afterEach, describe, expect, it, vi } from "vitest";
import { fetchLatestAgentCanvasVersion } from "./agent-canvas-updates";

function stubFetchResponse(response: Partial<Response>) {
  const fetchMock = vi.fn().mockResolvedValue(response as Response);
  vi.stubGlobal("fetch", fetchMock);
  return fetchMock;
}

afterEach(() => {
  vi.unstubAllGlobals();
});

describe("fetchLatestAgentCanvasVersion", () => {
  it("fetches the npm registry latest dist-tag and returns the trimmed version", async () => {
    const fetchMock = stubFetchResponse({
      ok: true,
      json: async () => ({ version: " 1.6.0 " }),
    });

    await expect(fetchLatestAgentCanvasVersion()).resolves.toBe("1.6.0");
    expect(fetchMock).toHaveBeenCalledWith(
      "https://registry.npmjs.org/@openhands/agent-canvas/latest",
      expect.objectContaining({ headers: { Accept: "application/json" } }),
    );
  });

  it("rejects when the registry responds with an error status", async () => {
    stubFetchResponse({ ok: false, status: 503 });

    await expect(fetchLatestAgentCanvasVersion()).rejects.toThrow("503");
  });

  it("rejects when the response has no usable version field", async () => {
    stubFetchResponse({ ok: true, json: async () => ({ version: 42 }) });

    await expect(fetchLatestAgentCanvasVersion()).rejects.toThrow(
      "missing version",
    );
  });
});