Files
piik/tests/channel-site-entry.test.ts
T
AAA 49b5b9e78e fix(ui): reload an outdated page from the version guidance
Use the existing refresh action when a newer Domain requires new page code.
Reconnecting the same loaded code cannot fix this mismatch. A site that needs
updating keeps its administrator guidance and existing reconnect action.

Verified the two entry states and actual Chrome clicks at 390 and 1280 pixels:
page refresh produces a reload, while reconnect preserves the document.
2026-11-27 15:19:09 -05:00

93 lines
4.8 KiB
TypeScript

import { createElement } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { afterEach, expect, it, vi } from "vitest";
import { ChannelSiteEntry } from "../src/client/channel/components/ChannelSiteEntry";
import { channelErrorPresentation } from "../src/client/components/error-presentation";
import { currentSiteResponds } from "../src/client/channel/site-health";
import { setCopy, t } from "../src/client/ui/copy";
afterEach(() => vi.unstubAllGlobals());
it("offers the site check and the matching 3.0 guide after a failed first connection", () => {
setCopy({ lang: "en" });
vi.stubGlobal("location", new URL("https://site.invalid"));
const html = renderToStaticMarkup(createElement(ChannelSiteEntry, {
channel: { connection: "reconnecting", error: null, reconnect: vi.fn(), joinAsNewIdentity: vi.fn() },
failure: { message: "Connection failed", connectionHelp: true },
}));
expect(html).toContain("<details");
expect(html).toContain("troubleshooting.md#the-page-opens-but-the-site-will-not-connect");
});
it.each(["connecting", "reconnecting"] as const)("shows the previous session timeout during %s", connection => {
setCopy({ lang: "en" });
vi.stubGlobal("location", new URL("https://site.invalid"));
const error = "session_expired";
const html = renderToStaticMarkup(createElement(ChannelSiteEntry, {
channel: { connection, error, reconnect: vi.fn(), joinAsNewIdentity: vi.fn() },
failure: channelErrorPresentation(error, (key, vars) => t("en", key, vars)),
}));
expect(html).toContain(`role="alert">${t("en", "channel.error.sessionExpired")}</p>`);
expect(html).toContain(`role="status" class="visually-hidden">${t("en", "channel.siteEntry.connecting")}</span>`);
});
it.each([
["domain_restore_unconfirmed", "channel.error.domainRestoreUnconfirmed"],
["domain_restored_connection_failed", "channel.error.domainRestoredConnectionFailed"],
] as const)("shows %s and offers reconnect from the site entry", (error, message) => {
setCopy({ lang: "zh" });
vi.stubGlobal("location", new URL("https://site.invalid"));
const html = renderToStaticMarkup(createElement(ChannelSiteEntry, {
channel: { connection: "closed", error, reconnect: vi.fn(), joinAsNewIdentity: vi.fn() },
failure: channelErrorPresentation(error, (key, vars) => t("zh", key, vars)),
}));
expect(html).toContain(`role="alert">${t("zh", message)}</p>`);
expect(html).toContain(`aria-label="${t("zh", "channel.siteEntry.reconnect")}"`);
expect(html.match(/<button\b/g)).toHaveLength(1);
});
it.each([
["page_update_required", "channel.error.pageUpdateRequired", "common.refresh"],
["domain_update_required", "channel.error.domainUpdateRequired", "channel.siteEntry.reconnect"],
] as const)("shows the required recovery action for %s", (error, message, action) => {
setCopy({ lang: "en" });
vi.stubGlobal("location", new URL("https://site.invalid"));
const html = renderToStaticMarkup(createElement(ChannelSiteEntry, {
channel: { connection: "closed", error, reconnect: vi.fn(), joinAsNewIdentity: vi.fn() },
failure: channelErrorPresentation(error, (key, vars) => t("en", key, vars)),
}));
expect(html).toContain('role="alert">' + t("en", message) + "</p>");
expect(html).toContain('aria-label="' + t("en", action) + '"');
expect(html.match(/<button\b/g)).toHaveLength(1);
});
it("checks only the current origin and recognizes the Domain response", async () => {
vi.stubGlobal("location", new URL("https://site.invalid/invitation?room=private#owner=private"));
const request = vi.fn()
.mockResolvedValueOnce(new Response("piik-channel-prototype"))
.mockResolvedValueOnce(new Response("<html>Proxy page</html>"))
.mockResolvedValueOnce(new Response("piik-channel-prototype", { status: 503 }))
.mockRejectedValueOnce(new Error("unreachable"));
vi.stubGlobal("fetch", request);
const signal = new AbortController().signal;
expect(await currentSiteResponds(signal)).toBe(true);
expect(request).toHaveBeenCalledWith(new URL("https://site.invalid/healthz"), expect.objectContaining({
mode: "same-origin", credentials: "omit", redirect: "error", referrerPolicy: "no-referrer",
}));
expect(await currentSiteResponds(signal)).toBe(false);
expect(await currentSiteResponds(signal)).toBe(false);
expect(await currentSiteResponds(signal)).toBe(false);
});
it("stops reading a response as soon as it differs from the site marker", async () => {
vi.stubGlobal("location", new URL("https://site.invalid"));
const cancel = vi.fn();
const body = new ReadableStream<Uint8Array>({
start(controller) { controller.enqueue(new TextEncoder().encode("piik-channel-prototype-extra")); },
cancel,
});
vi.stubGlobal("fetch", vi.fn().mockResolvedValue(new Response(body)));
expect(await currentSiteResponds(new AbortController().signal)).toBe(false);
expect(cancel).toHaveBeenCalledOnce();
});