File size: 1,155 Bytes
c8cad77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { expect, test, vi } from "vitest";

import { getCameraReference, saveCameraReference } from "./cameraReferenceApi";

const BASE = "http://localhost:8000";

test("a missing reference reads as null rather than an error", async () => {
  const fetcher = vi.fn().mockResolvedValue(new Response("{}", { status: 404 }));
  await expect(getCameraReference(BASE, fetcher, "my arm", "front")).resolves.toBeNull();
  expect(fetcher.mock.calls[0][0]).toBe(`${BASE}/robots/my%20arm/camera-reference/front`);
});

test("saving puts the PNG bytes and surfaces a rejection", async () => {
  const png = new Blob(["png"], { type: "image/png" });
  const fetcher = vi.fn().mockResolvedValueOnce(new Response("{}", { status: 200 }));
  await saveCameraReference(BASE, fetcher, "arm", "front", png);
  expect(fetcher).toHaveBeenCalledWith(`${BASE}/robots/arm/camera-reference/front`, {
    method: "PUT",
    headers: { "Content-Type": "image/png" },
    body: png,
  });

  fetcher.mockResolvedValueOnce(new Response("{}", { status: 400 }));
  await expect(saveCameraReference(BASE, fetcher, "arm", "front", png)).rejects.toThrow("Save reference failed: 400");
});