import { readFileSync } from "node:fs"; import { join } from "node:path"; import ts from "typescript"; import { isCoarsePointer, isMobileViewport, isSaveData, prefersReducedMotion, sceneTier, } from "./device"; /** * Testes de caminhos-base de lib/device.ts (complemento de device.test.ts): matches() sem matchMedia, * cada condição de sceneTier isolada (inclusive os limites), isSaveData e o ambiente sem window/navigator * (servidor). O servidor é simulado transpilando o fonte e executando-o com `window` e `navigator` indefinidos, * porque o jsdom não deixa esconder esses globais. */ type DeviceModule = typeof import("./device"); const originalMatchMedia = window.matchMedia; function mockMedia(active: string[]) { window.matchMedia = jest.fn().mockImplementation((query: string) => ({ matches: active.includes(query), media: query, addEventListener() {}, removeEventListener() {}, })); } function mockNavigator(values: Record) { for (const [key, value] of Object.entries(values)) Object.defineProperty(navigator, key, { configurable: true, value }); } function loadOnServer(globals: { window?: unknown; navigator?: unknown }) { const source = readFileSync(join(process.cwd(), "lib/device.ts"), "utf8"); const { outputText } = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS }, }); const loaded = { exports: {} as DeviceModule }; new Function("module", "exports", "window", "navigator", outputText)( loaded, loaded.exports, globals.window, globals.navigator, ); return loaded.exports; } beforeEach(() => { mockMedia([]); mockNavigator({ hardwareConcurrency: 8, deviceMemory: undefined, connection: undefined, }); }); afterEach(() => { window.matchMedia = originalMatchMedia; }); describe("matches() (through the media helpers)", () => { it("path: matchMedia is not available -> every media hint is false", () => { // @ts-expect-error simula um navegador sem matchMedia window.matchMedia = undefined; expect(isCoarsePointer()).toBe(false); expect(prefersReducedMotion()).toBe(false); expect(isMobileViewport()).toBe(false); expect(sceneTier()).toBe("full"); }); it("path: matchMedia answers false for the query -> false", () => { expect(isCoarsePointer()).toBe(false); }); it("path: matchMedia answers true only for the asked query -> only that hint is true", () => { mockMedia(["(prefers-reduced-motion: reduce)"]); expect(prefersReducedMotion()).toBe(true); expect(isCoarsePointer()).toBe(false); expect(isMobileViewport()).toBe(false); }); }); describe("isSaveData()", () => { it.each([ ["connection is missing", undefined], ["connection has no saveData", {}], ["saveData is false", { saveData: false }], ["saveData is a truthy non-boolean", { saveData: "true" }], ])("path: %s -> false", (_label, connection) => { mockNavigator({ connection }); expect(isSaveData()).toBe(false); }); it("path: saveData is exactly true -> true", () => { mockNavigator({ connection: { saveData: true } }); expect(isSaveData()).toBe(true); }); }); describe("sceneTier(): limites de cada condição, uma de cada vez", () => { it.each([ ["5 cores", { hardwareConcurrency: 5 }], ["8 GB of memory", { hardwareConcurrency: 8, deviceMemory: 8 }], ["5 GB of memory", { hardwareConcurrency: 8, deviceMemory: 5 }], ["cores reported as text", { hardwareConcurrency: "2" }], ["memory reported as text", { deviceMemory: "2" }], ["cores reported as null", { hardwareConcurrency: null }], ])( "path: %s -> full power (just above the limit or not a number)", (_label, hints) => { mockNavigator(hints); expect(sceneTier()).toBe("full"); }, ); it.each([ ["exactly 4 cores", { hardwareConcurrency: 4 }], ["exactly 4 GB", { deviceMemory: 4 }], ["0.5 GB", { deviceMemory: 0.5 }], ["1 core", { hardwareConcurrency: 1 }], ])("path: %s -> static", (_label, hints) => { mockNavigator(hints); expect(sceneTier()).toBe("static"); }); it("path: touch on a capable phone -> lite, never full", () => { mockMedia(["(pointer: coarse)"]); mockNavigator({ hardwareConcurrency: 16, deviceMemory: 16 }); expect(sceneTier()).toBe("lite"); }); it("path: several conditions true at once -> still just static", () => { mockMedia(["(pointer: coarse)", "(prefers-reduced-motion: reduce)"]); mockNavigator({ hardwareConcurrency: 2, deviceMemory: 2, connection: { saveData: true }, }); expect(sceneTier()).toBe("static"); }); }); describe("without window and navigator (server)", () => { it("path: no window -> media hints are false", () => { const device = loadOnServer({ window: undefined, navigator: undefined }); expect(device.isCoarsePointer()).toBe(false); expect(device.prefersReducedMotion()).toBe(false); expect(device.isMobileViewport()).toBe(false); }); it("path: no navigator -> save data is off and the scene is static", () => { const device = loadOnServer({ window: undefined, navigator: undefined }); expect(device.isSaveData()).toBe(false); expect(device.sceneTier()).toBe("static"); }); it("path: a navigator without window (runtime that exposes only navigator) -> hints still read", () => { const device = loadOnServer({ window: undefined, navigator: { hardwareConcurrency: 16, connection: { saveData: true } }, }); expect(device.isSaveData()).toBe(true); expect(device.sceneTier()).toBe("static"); const capable = loadOnServer({ window: undefined, navigator: { hardwareConcurrency: 16 }, }); expect(capable.sceneTier()).toBe("full"); }); });