Files
snapcad-studio/tests/unit/cloudApi.test.ts
T

121 lines
4.1 KiB
TypeScript
Raw Permalink Normal View History

import { afterEach, describe, expect, it, vi } from "vitest";
import { cloudApiFetch } from "../../apps/web/src/lib/cloudApi";
import type { SnapCadDesktopBridge } from "../../apps/web/src/types/desktop";
describe("cloudApiFetch", () => {
const globalAny = globalThis as unknown as {
window?: Partial<Window>;
fetch?: typeof fetch;
};
const originalWindow = globalAny.window;
const originalFetch = globalAny.fetch;
afterEach(() => {
globalAny.window = originalWindow;
globalAny.fetch = originalFetch;
vi.restoreAllMocks();
});
it("rejects non-cloud-api paths before attempting network or IPC", async () => {
await expect(cloudApiFetch("https://evil.example/api")).rejects.toThrow(/Geçersiz bulut API yolu/);
await expect(cloudApiFetch("/login.php")).rejects.toThrow(/Geçersiz bulut API yolu/);
});
it("routes /cloud-api.php requests through Electron IPC bridge in desktop runtime", async () => {
const cloudRequest = vi.fn(async () => ({
ok: true,
status: 200,
bodyText: JSON.stringify({ ok: true, authenticated: true, user: { id: 7, name: "Mimar", email: "[email protected]" } }),
contentType: "application/json; charset=utf-8",
}));
const webFetch = vi.fn();
globalAny.fetch = webFetch as unknown as typeof fetch;
globalAny.window = {
snapcadDesktop: {
isDesktop: true,
getVersion: async () => "1.0.10",
checkForUpdates: async () => ({
currentVersion: "1.0.10",
latestVersion: null,
updateAvailable: false,
downloaded: false,
checkedAt: "2026-01-01T00:00:00.000Z",
}),
installUpdate: async () => ({
currentVersion: "1.0.10",
latestVersion: null,
updateAvailable: false,
downloaded: false,
checkedAt: "2026-01-01T00:00:00.000Z",
}),
cloudRequest,
} satisfies SnapCadDesktopBridge,
};
const res = await cloudApiFetch("/cloud-api.php?action=session", { cache: "no-store" });
expect(res.ok).toBe(true);
expect(res.status).toBe(200);
expect(cloudRequest).toHaveBeenCalledWith({
path: "/cloud-api.php?action=session",
method: "GET",
headers: undefined,
body: undefined,
});
expect(webFetch).not.toHaveBeenCalled();
const json = (await res.json()) as { ok: boolean; user?: { name: string } };
expect(json.ok).toBe(true);
expect(json.user?.name).toBe("Mimar");
});
it("throws when Electron IPC cloud proxy encounters a transport error (status 0)", async () => {
globalAny.window = {
snapcadDesktop: {
isDesktop: true,
getVersion: async () => "1.0.10",
checkForUpdates: async () => ({
currentVersion: "1.0.10",
latestVersion: null,
updateAvailable: false,
downloaded: false,
checkedAt: "2026-01-01T00:00:00.000Z",
}),
installUpdate: async () => ({
currentVersion: "1.0.10",
latestVersion: null,
updateAvailable: false,
downloaded: false,
checkedAt: "2026-01-01T00:00:00.000Z",
}),
cloudRequest: async () => ({
ok: false,
status: 0,
bodyText: "",
error: "Ağ bağlantısı kesildi.",
}),
} satisfies SnapCadDesktopBridge,
};
await expect(cloudApiFetch("/cloud-api.php?action=session")).rejects.toThrow("Ağ bağlantısı kesildi.");
});
it("falls back to standard fetch in browser builds", async () => {
const mockResponse = new Response(JSON.stringify({ ok: true, authenticated: false }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
const webFetch = vi.fn(async () => mockResponse);
globalAny.window = {};
globalAny.fetch = webFetch as unknown as typeof fetch;
const res = await cloudApiFetch("/cloud-api.php?action=sync_all", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ action: "sync_all", projects: [] }),
});
expect(webFetch).toHaveBeenCalledTimes(1);
expect(res.ok).toBe(true);
});
});