78 lines
2.6 KiB
TypeScript
78 lines
2.6 KiB
TypeScript
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||
|
|
import { backendStore } from "./backend.svelte.ts";
|
||
|
|
|
||
|
|
// Mock WebSocket globally so the store module can reference it
|
||
|
|
class MockWebSocket {
|
||
|
|
onopen: ((ev: Event) => void) | null = null;
|
||
|
|
onclose: ((ev: CloseEvent) => void) | null = null;
|
||
|
|
onmessage: ((ev: MessageEvent) => void) | null = null;
|
||
|
|
onerror: ((ev: Event) => void) | null = null;
|
||
|
|
close = vi.fn();
|
||
|
|
}
|
||
|
|
vi.stubGlobal("WebSocket", MockWebSocket);
|
||
|
|
|
||
|
|
// Mock fetch to prevent real network calls
|
||
|
|
vi.stubGlobal(
|
||
|
|
"fetch",
|
||
|
|
vi.fn(() =>
|
||
|
|
Promise.resolve({
|
||
|
|
ok: true,
|
||
|
|
json: () => Promise.resolve({}),
|
||
|
|
})
|
||
|
|
)
|
||
|
|
);
|
||
|
|
|
||
|
|
describe("backend store", () => {
|
||
|
|
beforeEach(() => {
|
||
|
|
backendStore.disconnect();
|
||
|
|
backendStore.setPort(8081);
|
||
|
|
});
|
||
|
|
|
||
|
|
it("test_exports_expected_properties", () => {
|
||
|
|
expect(backendStore).toHaveProperty("port");
|
||
|
|
expect(backendStore).toHaveProperty("connectionState");
|
||
|
|
expect(backendStore).toHaveProperty("connected");
|
||
|
|
expect(backendStore).toHaveProperty("appState");
|
||
|
|
expect(backendStore).toHaveProperty("stateMessage");
|
||
|
|
expect(backendStore).toHaveProperty("deviceInfo");
|
||
|
|
expect(backendStore).toHaveProperty("version");
|
||
|
|
expect(backendStore).toHaveProperty("lastError");
|
||
|
|
expect(backendStore).toHaveProperty("apiBaseUrl");
|
||
|
|
expect(backendStore).toHaveProperty("wsUrl");
|
||
|
|
expect(backendStore).toHaveProperty("obsUrl");
|
||
|
|
expect(backendStore).toHaveProperty("syncUrl");
|
||
|
|
});
|
||
|
|
|
||
|
|
it("test_exports_expected_methods", () => {
|
||
|
|
expect(typeof backendStore.setPort).toBe("function");
|
||
|
|
expect(typeof backendStore.connect).toBe("function");
|
||
|
|
expect(typeof backendStore.disconnect).toBe("function");
|
||
|
|
expect(typeof backendStore.apiUrl).toBe("function");
|
||
|
|
expect(typeof backendStore.apiFetch).toBe("function");
|
||
|
|
expect(typeof backendStore.apiGet).toBe("function");
|
||
|
|
expect(typeof backendStore.apiPost).toBe("function");
|
||
|
|
expect(typeof backendStore.apiPut).toBe("function");
|
||
|
|
});
|
||
|
|
|
||
|
|
it("test_obsUrl_derives_from_port", () => {
|
||
|
|
backendStore.setPort(8081);
|
||
|
|
expect(backendStore.obsUrl).toBe("http://localhost:8080");
|
||
|
|
});
|
||
|
|
|
||
|
|
it("test_apiBaseUrl_uses_port", () => {
|
||
|
|
backendStore.setPort(8081);
|
||
|
|
expect(backendStore.apiBaseUrl).toBe("http://localhost:8081");
|
||
|
|
});
|
||
|
|
|
||
|
|
it("test_wsUrl_uses_port", () => {
|
||
|
|
backendStore.setPort(8081);
|
||
|
|
expect(backendStore.wsUrl).toBe("ws://localhost:8081/ws/control");
|
||
|
|
});
|
||
|
|
|
||
|
|
it("test_initial_state", () => {
|
||
|
|
// After disconnect() in beforeEach, state should be disconnected
|
||
|
|
expect(backendStore.connectionState).toBe("disconnected");
|
||
|
|
expect(backendStore.appState).toBe("initializing");
|
||
|
|
});
|
||
|
|
});
|