Files
homeassistant_espscreen/web/tests/api.spec.ts
T
Max Gramser 1a6308aba7 Fix what a full test round of today's releases found: a ticking single clock, a switch that works in standby, no taps from a swipe (0.2.78)
A test and code-analysis round of 0.2.72 to 0.2.76 (suites at every tag, sanitizers, ESP32 and GitHub builds, host
harnesses on both boards, the editor in a browser, best practice checked against ESPHome, LVGL, Home Assistant and
Vue) found these, fixed here with Max's choices for the product questions:

- Firmware 0.2.65: the second hand of a single analog clock moves again; on a page that is one full on/off tile the
  waking push also switches it; on the Guition a flick is never a tap and a finger that slides off a tile doesn't tap
  it; Previous/Next react to a firm press; the same Go to page tile may sit on several pages; a layout the screen has
  no memory for is refused instead of crashing; 64-bit redraw mask for 48 tiles; Montserrat 14 no longer built.
- App: Save refuses a layout too long for one message or too big for eight pages; an offline screen keeps its
  firmware version from the device registry; HA refusals reach the editor; 128 KB requests; changelog out of the
  live stream; cached hashed assets; tini as init and a clean stop; changelog parsing without asterisks or crashes.
- Editor: clicking the open screen keeps unsaved edits, edits during a save stay unsaved, a Page row and phone
  dragging to other pages, repeated page tiles, limits from the server, HA's error messages, a type-checked build.
- tools/check.sh + GitHub Actions + a CYD flash budget; docs match 48 tiles and the new editor.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-18 17:25:23 +02:00

36 lines
2.1 KiB
TypeScript

// The add-on's answers: what a failed request says to the person using the page.
import { afterEach, describe, expect, it, vi } from "vitest";
import { getJson, send } from "../src/api";
const answer = (status: number, body: string, type = "application/json") =>
vi.stubGlobal("fetch", vi.fn(async () => new Response(body, { status, headers: { "Content-Type": type } })));
afterEach(() => vi.unstubAllGlobals());
describe("api", () => {
it("shows the add-on's own sentence for any failed answer with a JSON error (app 0.2.78)", async () => {
answer(400, JSON.stringify({ error: "Home Assistant refused light.turn_on: the entity is unavailable." }));
await expect(send("screens/x", "PUT", {})).rejects.toThrow("Home Assistant refused light.turn_on: the entity is unavailable.");
answer(503, JSON.stringify({ error: "Home Assistant didn't answer in time. Try again in a moment." }));
await expect(getJson("capabilities?entity=light.a")).rejects.toThrow("Home Assistant didn't answer in time. Try again in a moment.");
answer(502, JSON.stringify({ error: "Bad gateway from Home Assistant." }));
await expect(getJson("states")).rejects.toThrow("Bad gateway from Home Assistant.");
});
it("keeps the general sentence for an answer that isn't JSON or has no error in it", async () => {
const general = "That didn't work. Refresh the page and try again.";
answer(502, "<html><body>502 Bad Gateway</body></html>", "text/html");
await expect(getJson("inventory")).rejects.toThrow(general);
answer(500, JSON.stringify({ detail: "nope" }));
await expect(getJson("inventory")).rejects.toThrow(general);
answer(500, JSON.stringify({ error: "" }));
await expect(getJson("inventory")).rejects.toThrow(general);
});
it("sends relative URLs with the CSRF header and reads an empty answer as null", async () => {
answer(200, "");
expect(await send("screens/x/identify", "POST")).toBeNull();
const call = (fetch as unknown as ReturnType<typeof vi.fn>).mock.calls[0];
expect(call[0]).toBe("api/screens/x/identify");
expect(call[1].headers).toHaveProperty("X-Screen-CSRF");
});
});