Merge pull request 'test(canvas): add pure-function tests for deriveWsBaseUrl, statusDotClass, and readThemeCookie' (#238) from test/canvas-utility-pure-tests into main
This commit is contained in:
commit
02a1de75aa
52
canvas/src/lib/__tests__/statusDotClass.test.ts
Normal file
52
canvas/src/lib/__tests__/statusDotClass.test.ts
Normal file
@ -0,0 +1,52 @@
|
||||
// @vitest-environment jsdom
|
||||
/**
|
||||
* Tests for statusDotClass — maps a workspace status string to the
|
||||
* CSS tailwind class used on the status indicator dot.
|
||||
*/
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { statusDotClass } from "../design-tokens";
|
||||
|
||||
describe("statusDotClass", () => {
|
||||
it('returns "bg-emerald-400" for "online"', () => {
|
||||
expect(statusDotClass("online")).toBe("bg-emerald-400");
|
||||
});
|
||||
|
||||
it('returns "bg-zinc-500" for "offline"', () => {
|
||||
expect(statusDotClass("offline")).toBe("bg-zinc-500");
|
||||
});
|
||||
|
||||
it('returns "bg-indigo-400" for "paused"', () => {
|
||||
expect(statusDotClass("paused")).toBe("bg-indigo-400");
|
||||
});
|
||||
|
||||
it('returns "bg-amber-400" for "degraded"', () => {
|
||||
expect(statusDotClass("degraded")).toBe("bg-amber-400");
|
||||
});
|
||||
|
||||
it('returns "bg-red-400" for "failed"', () => {
|
||||
expect(statusDotClass("failed")).toBe("bg-red-400");
|
||||
});
|
||||
|
||||
it('returns "bg-sky-400 motion-safe:animate-pulse" for "provisioning"', () => {
|
||||
expect(statusDotClass("provisioning")).toBe("bg-sky-400 motion-safe:animate-pulse");
|
||||
});
|
||||
|
||||
it('returns "bg-amber-300" for "not_configured"', () => {
|
||||
expect(statusDotClass("not_configured")).toBe("bg-amber-300");
|
||||
});
|
||||
|
||||
it("falls back to bg-zinc-500 for unknown status strings", () => {
|
||||
expect(statusDotClass("unknown")).toBe("bg-zinc-500");
|
||||
expect(statusDotClass("")).toBe("bg-zinc-500");
|
||||
expect(statusDotClass("ONLINE")).toBe("bg-zinc-500"); // case-sensitive
|
||||
expect(statusDotClass(" online")).toBe("bg-zinc-500"); // whitespace-sensitive
|
||||
expect(statusDotClass("online\n")).toBe("bg-zinc-500");
|
||||
});
|
||||
|
||||
it("is a pure function — same input always returns same output", () => {
|
||||
const result = statusDotClass("online");
|
||||
for (let i = 0; i < 5; i++) {
|
||||
expect(statusDotClass("online")).toBe(result);
|
||||
}
|
||||
});
|
||||
});
|
||||
47
canvas/src/lib/__tests__/theme-cookie.test.ts
Normal file
47
canvas/src/lib/__tests__/theme-cookie.test.ts
Normal file
@ -0,0 +1,47 @@
|
||||
// @vitest-environment jsdom
|
||||
/**
|
||||
* Tests for readThemeCookie — parses a cookie value into a ThemePreference.
|
||||
*/
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { readThemeCookie } from "../theme-cookie";
|
||||
|
||||
describe("readThemeCookie", () => {
|
||||
it('returns "light" when cookie value is "light"', () => {
|
||||
expect(readThemeCookie("light")).toBe("light");
|
||||
});
|
||||
|
||||
it('returns "dark" when cookie value is "dark"', () => {
|
||||
expect(readThemeCookie("dark")).toBe("dark");
|
||||
});
|
||||
|
||||
it('returns "system" when cookie value is "system"', () => {
|
||||
expect(readThemeCookie("system")).toBe("system");
|
||||
});
|
||||
|
||||
it('returns "system" for undefined', () => {
|
||||
expect(readThemeCookie(undefined)).toBe("system");
|
||||
});
|
||||
|
||||
it('returns "system" for empty string', () => {
|
||||
expect(readThemeCookie("")).toBe("system");
|
||||
});
|
||||
|
||||
it('returns "system" for any non-matching value', () => {
|
||||
expect(readThemeCookie("auto")).toBe("system");
|
||||
expect(readThemeCookie("dark-mode")).toBe("system");
|
||||
expect(readThemeCookie("DARK")).toBe("system"); // case-sensitive
|
||||
expect(readThemeCookie("light\n")).toBe("system"); // whitespace-sensitive
|
||||
expect(readThemeCookie(" system ")).toBe("system");
|
||||
expect(readThemeCookie("null")).toBe("system");
|
||||
expect(readThemeCookie("0")).toBe("system");
|
||||
});
|
||||
|
||||
it("is pure — same input always returns same output", () => {
|
||||
const inputs = ["light", "dark", "system", undefined, ""];
|
||||
for (const input of inputs) {
|
||||
for (let i = 0; i < 3; i++) {
|
||||
expect(readThemeCookie(input)).toBe(readThemeCookie(input));
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
134
canvas/src/lib/__tests__/ws-url.test.ts
Normal file
134
canvas/src/lib/__tests__/ws-url.test.ts
Normal file
@ -0,0 +1,134 @@
|
||||
// @vitest-environment jsdom
|
||||
/**
|
||||
* Tests for deriveWsBaseUrl — WebSocket base URL derivation from env / window.location.
|
||||
*/
|
||||
import { describe, it, expect, beforeEach, vi, afterEach } from "vitest";
|
||||
import { deriveWsBaseUrl } from "../ws-url";
|
||||
|
||||
const ORIGINAL_WS = process.env.NEXT_PUBLIC_WS_URL;
|
||||
const ORIGINAL_PLATFORM = process.env.NEXT_PUBLIC_PLATFORM_URL;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.stubEnv("NEXT_PUBLIC_WS_URL", "");
|
||||
vi.stubEnv("NEXT_PUBLIC_PLATFORM_URL", "");
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
if (ORIGINAL_WS !== undefined) vi.stubEnv("NEXT_PUBLIC_WS_URL", ORIGINAL_WS);
|
||||
else delete process.env.NEXT_PUBLIC_WS_URL;
|
||||
if (ORIGINAL_PLATFORM !== undefined) vi.stubEnv("NEXT_PUBLIC_PLATFORM_URL", ORIGINAL_PLATFORM);
|
||||
else delete process.env.NEXT_PUBLIC_PLATFORM_URL;
|
||||
});
|
||||
|
||||
describe("deriveWsBaseUrl — NEXT_PUBLIC_WS_URL (priority 1)", () => {
|
||||
it("uses NEXT_PUBLIC_WS_URL when set", () => {
|
||||
vi.stubEnv("NEXT_PUBLIC_WS_URL", "wss://ws.example.com/ws");
|
||||
expect(deriveWsBaseUrl()).toBe("wss://ws.example.com");
|
||||
});
|
||||
|
||||
it("strips trailing /ws suffix from NEXT_PUBLIC_WS_URL", () => {
|
||||
vi.stubEnv("NEXT_PUBLIC_WS_URL", "wss://ws.example.com/ws");
|
||||
expect(deriveWsBaseUrl()).toBe("wss://ws.example.com");
|
||||
});
|
||||
|
||||
it("uses ws:// for HTTP NEXT_PUBLIC_WS_URL", () => {
|
||||
vi.stubEnv("NEXT_PUBLIC_WS_URL", "ws://localhost:8080/ws");
|
||||
expect(deriveWsBaseUrl()).toBe("ws://localhost:8080");
|
||||
});
|
||||
|
||||
it("wins over NEXT_PUBLIC_PLATFORM_URL", () => {
|
||||
vi.stubEnv("NEXT_PUBLIC_WS_URL", "wss://ws.example.com");
|
||||
vi.stubEnv("NEXT_PUBLIC_PLATFORM_URL", "http://platform.example.com");
|
||||
expect(deriveWsBaseUrl()).toBe("wss://ws.example.com");
|
||||
});
|
||||
|
||||
it("wins over window.location", () => {
|
||||
vi.stubEnv("NEXT_PUBLIC_WS_URL", "wss://ws.example.com");
|
||||
Object.defineProperty(window, "location", {
|
||||
value: { protocol: "https:", host: "canvas.example.com" },
|
||||
writable: true,
|
||||
});
|
||||
expect(deriveWsBaseUrl()).toBe("wss://ws.example.com");
|
||||
});
|
||||
});
|
||||
|
||||
describe("deriveWsBaseUrl — NEXT_PUBLIC_PLATFORM_URL (priority 2)", () => {
|
||||
it("derives ws:// from http:// platform URL", () => {
|
||||
vi.stubEnv("NEXT_PUBLIC_PLATFORM_URL", "http://localhost:8080");
|
||||
expect(deriveWsBaseUrl()).toBe("ws://localhost:8080");
|
||||
});
|
||||
|
||||
it("derives wss:// from https:// platform URL", () => {
|
||||
vi.stubEnv("NEXT_PUBLIC_PLATFORM_URL", "https://platform.example.com");
|
||||
expect(deriveWsBaseUrl()).toBe("wss://platform.example.com");
|
||||
});
|
||||
|
||||
it("preserves non-standard ports", () => {
|
||||
vi.stubEnv("NEXT_PUBLIC_PLATFORM_URL", "http://localhost:9000");
|
||||
expect(deriveWsBaseUrl()).toBe("ws://localhost:9000");
|
||||
});
|
||||
|
||||
it("wins over window.location", () => {
|
||||
vi.stubEnv("NEXT_PUBLIC_PLATFORM_URL", "https://platform.example.com");
|
||||
Object.defineProperty(window, "location", {
|
||||
value: { protocol: "https:", host: "canvas.example.com" },
|
||||
writable: true,
|
||||
});
|
||||
expect(deriveWsBaseUrl()).toBe("wss://platform.example.com");
|
||||
});
|
||||
});
|
||||
|
||||
describe("deriveWsBaseUrl — window.location (priority 3)", () => {
|
||||
it("uses wss:// when page is served over HTTPS", () => {
|
||||
Object.defineProperty(window, "location", {
|
||||
value: { protocol: "https:", host: "canvas.example.com" },
|
||||
writable: true,
|
||||
});
|
||||
expect(deriveWsBaseUrl()).toBe("wss://canvas.example.com");
|
||||
});
|
||||
|
||||
it("uses ws:// when page is served over HTTP", () => {
|
||||
Object.defineProperty(window, "location", {
|
||||
value: { protocol: "http:", host: "localhost:3000" },
|
||||
writable: true,
|
||||
});
|
||||
expect(deriveWsBaseUrl()).toBe("ws://localhost:3000");
|
||||
});
|
||||
|
||||
it("includes the host with port", () => {
|
||||
Object.defineProperty(window, "location", {
|
||||
value: { protocol: "https:", host: "canvas.example.com:8443" },
|
||||
writable: true,
|
||||
});
|
||||
expect(deriveWsBaseUrl()).toBe("wss://canvas.example.com:8443");
|
||||
});
|
||||
});
|
||||
|
||||
describe("deriveWsBaseUrl — fallback (priority 4)", () => {
|
||||
it("falls back to localhost when no env vars or window is unavailable", () => {
|
||||
// process.env is empty (already stubbed), window is not stubbed but we
|
||||
// can't remove it entirely in jsdom — the function checks typeof window
|
||||
// which is always defined. Since we have no env vars, it falls through
|
||||
// to the window branch; we test the final fallback by stubbing window
|
||||
// location to undefined (not possible in jsdom — skip this edge case).
|
||||
// The test below verifies the no-env-var path works.
|
||||
Object.defineProperty(window, "location", {
|
||||
value: { protocol: "http:", host: "localhost:3000" },
|
||||
writable: true,
|
||||
});
|
||||
expect(deriveWsBaseUrl()).toBe("ws://localhost:3000");
|
||||
});
|
||||
});
|
||||
|
||||
describe("deriveWsBaseUrl — protocol derivation", () => {
|
||||
it("derives ws:// from http:// and keeps it", () => {
|
||||
vi.stubEnv("NEXT_PUBLIC_PLATFORM_URL", "http://platform:8080");
|
||||
expect(deriveWsBaseUrl()).toMatch(/^ws:/);
|
||||
});
|
||||
|
||||
it("derives wss:// from https:// and keeps it", () => {
|
||||
vi.stubEnv("NEXT_PUBLIC_PLATFORM_URL", "https://platform:8080");
|
||||
expect(deriveWsBaseUrl()).toMatch(/^wss:/);
|
||||
});
|
||||
});
|
||||
Loading…
Reference in New Issue
Block a user