119 lines
6.4 KiB
TypeScript
119 lines
6.4 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/ui/error-presentation";
|
|
import { currentSiteResponds } from "../src/client/channel/site-health";
|
|
import { setCopy, t } from "../src/client/ui/copy";
|
|
|
|
// The hook's clock is held by the picture caption's test; here only what the entry shows on either side of it.
|
|
const longWait = vi.hoisted(() => ({ reached: false }));
|
|
vi.mock("../src/client/ui/long-wait", () => ({ useLongWait: (since: number | undefined) => since !== undefined && longWait.reached }));
|
|
|
|
afterEach(() => { vi.unstubAllGlobals(); longWait.reached = false; });
|
|
|
|
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>`);
|
|
// After a failed attempt the heading says that this is another one.
|
|
const title = t("en", connection === "connecting" ? "channel.siteEntry.connecting" : "channel.previewPage.reconnecting");
|
|
expect(html).toContain(`<h1 id="c3-site-entry-title">${title}</h1>`);
|
|
expect(html).toContain(`role="status" class="visually-hidden">${title}</p>`);
|
|
});
|
|
|
|
it("says a first connection is taking long and offers the site check, with no failure to go by", () => {
|
|
setCopy({ lang: "en" });
|
|
vi.stubGlobal("location", new URL("https://site.invalid"));
|
|
const render = (connection: "connecting" | "closed") => renderToStaticMarkup(createElement(ChannelSiteEntry, {
|
|
channel: { connection, error: null, reconnect: vi.fn(), joinAsNewIdentity: vi.fn() }, failure: { message: "", connectionHelp: false },
|
|
}));
|
|
const slow = t("en", "common.waitingLong");
|
|
const waiting = render("connecting");
|
|
expect(waiting).not.toContain(slow);
|
|
expect(waiting).not.toContain("<details");
|
|
longWait.reached = true;
|
|
const long = render("connecting");
|
|
expect(long).toContain(`<p role="status">${slow}</p>`);
|
|
expect(long).toContain(t("en", "channel.siteEntry.connectionHelp"));
|
|
expect(long).toContain(`<h1 id="c3-site-entry-title">${t("en", "channel.siteEntry.connecting")}</h1>`);
|
|
// The line belongs to the wait: an ended attempt shows its own verdict.
|
|
expect(render("closed")).not.toContain(slow);
|
|
});
|
|
|
|
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).toContain('aria-label="' + t("zh", "debug.exportHint") + '"');
|
|
});
|
|
|
|
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).toContain('aria-label="' + t("en", "debug.exportHint") + '"');
|
|
});
|
|
|
|
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();
|
|
});
|