File size: 4,232 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
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
// @vitest-environment jsdom
import React, { act } from "react";
import { createRoot, Root } from "react-dom/client";
import { afterEach, beforeEach, expect, test, vi } from "vitest";
import CameraConfiguration from "./CameraConfiguration";
import { CameraConfig, normalizeCameraConfig, serializeCameras } from "@/lib/cameraConfig";
vi.mock("@/hooks/useAvailableCameras", () => ({ useAvailableCameras: () => ({ cameras: [], isLoading: false, refresh: vi.fn() }) }));
vi.mock("@/hooks/use-toast", () => ({ useToast: () => ({ toast: vi.fn() }) }));
const reference = { url: null as string | null, error: null, save: vi.fn(), clear: vi.fn() };
vi.mock("@/hooks/useCameraReference", () => ({ useCameraReference: () => reference }));
vi.mock("@/hooks/useCameraStream", () => ({ useCameraStream: () => ({ videoRef: React.createRef<HTMLVideoElement>(), hasError: false }) }));
let root: Root;
let container: HTMLDivElement;
const camera: CameraConfig = { id: "wrist", name: "wrist", device_id: "usb-wrist", type: "opencv", width: 640, height: 480 };
beforeEach(() => { Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true }); container = document.createElement("div"); root = createRoot(container); });
afterEach(() => act(() => root.unmount()));
test("rotation updates the profile while capture dimensions stay native", () => {
  const change = vi.fn();
  act(() => root.render(<CameraConfiguration cameras={[camera]} onCamerasChange={change} />));
  const select = container.querySelector("select")!;
  expect(select.value).toBe("0");
  act(() => { select.value = "90"; select.dispatchEvent(new Event("change", { bubbles: true })); });
  expect(change).toHaveBeenCalledWith([{ ...camera, rotation: 90 }]);
  const saved = JSON.parse(JSON.stringify(change.mock.calls[0][0])).map(normalizeCameraConfig);
  act(() => root.render(<CameraConfiguration cameras={saved} onCamerasChange={change} />));
  expect(container.querySelector("select")!.value).toBe("90");
  expect(container.querySelector("video")!.style.transform).toContain("rotate(90deg)");
  expect(serializeCameras(saved).wrist).toMatchObject({ rotation: 90, width: 640, height: 480 });
});
test("the recording configuration displays rotated output without an editing control", () => {
  act(() => root.render(<CameraConfiguration cameras={[{ ...camera, rotation: 90 }]} readOnly onCamerasChange={vi.fn()} />));
  expect(container.textContent).toContain("480×640 · 90° clockwise");
  expect(container.querySelector("select")).toBeNull();
  expect(container.querySelector("video")!.parentElement!.style.aspectRatio).toBe("480 / 640");
});

test("an invalid stored rotation stays visible until the user selects a valid value", () => {
  const change = vi.fn();
  const invalid = { ...camera, rotation: 45 as never };
  act(() => root.render(<CameraConfiguration cameras={[invalid]} onCamerasChange={change} />));
  expect(container.querySelector("[role=alert]")!.textContent).toContain("Select a valid rotation");
  expect(container.querySelector("video")).toBeNull();
  const select = container.querySelector("select")!;
  expect(select.value).toBe("");
  act(() => { select.value = "180"; select.dispatchEvent(new Event("change", { bubbles: true })); });
  expect(change).toHaveBeenCalledWith([{ ...camera, rotation: 180 }]);
});
test("reference controls appear only for a named robot in read-only mode", () => {
  act(() => root.render(<CameraConfiguration cameras={[camera]} onCamerasChange={vi.fn()} readOnly />));
  expect(container.textContent).not.toContain("Set reference");
  act(() => root.render(<CameraConfiguration cameras={[camera]} onCamerasChange={vi.fn()} readOnly robotName="arm" />));
  expect(container.textContent).toContain("Set reference");
  expect(container.querySelector('input[type="range"]')).toBeNull();
  reference.url = "blob:ref";
  act(() => root.render(<CameraConfiguration cameras={[camera]} onCamerasChange={vi.fn()} readOnly robotName="arm" />));
  expect(container.textContent).toContain("Replace reference");
  expect(container.querySelector("img")!.getAttribute("src")).toBe("blob:ref");
  const slider = container.querySelector<HTMLInputElement>('input[type="range"]')!;
  expect(slider).not.toBeNull();
  reference.url = null;
});