Files
baserow/e2e-tests/tests/agent/websiteWidget.spec.ts
Davide Silvestri 2659033445 feat(agent): website widget channel (#6884)
* feat(agent): serve a website widget channel and its embed script

A website widget is a public chat a website opens in a panel from a
button. Its model extends the public chat's with the button's text and
corner and never has a password. GET /api/agent/public-chat/<slug>/widget.js
returns the button's script with the widget's settings written in,
cached for five minutes. ADR 016 records the decision.

* feat(agent): add website widgets in the Channels section

Channel types now come from a frontend registry: "Add channel" opens a
menu of them and each card shows its type's form. The website widget's
form has the embed code and the button's text and corner, and shares the
title, welcome text and appearance with the public chat. Embedded in the
widget's panel, the chat page fills it with its background, keeps text
readable on a dark one, and asks the widget to close on Escape.

* fix(agent): copy only a channel type's own settings

Export and import copied the public chat's appearance fields for every
public chat type, so an imported widget kept a background it can't show
or change. Each type now lists its appearance fields once, and copies use
that list; the password follows the same rule. The widget's chat URL is
built with get_embedded_share_url, so a share URL ending in / works.

* test(agent): run the widget script on another website

An e2e test embeds the widget on a page from another origin, with markup
as the button text and title, and checks both show as text and the chat
opens in the panel.

* fix(agent): backend default button text, focus the channel menu, keep widget names visible

A new widget gets its button text from the backend only, as one created
through the API does. "Add channel" moves focus into its menu like the
other menus, and a card's type label gives its space up before the name.
A test covers choosing Automatic to clear a colour.

* fix(agent): visible hover without an accent, no suggestions while a message is locked

Without an accent colour the chat's buttons darken on hover as the
member chat's do, and custom accents get the stronger hover again. The
member chat hides suggested messages while the message box is read-only
for a pending or failed send, since a click couldn't change it.

* test(agent): serve the widget test's website on loopback

CI's Chrome blocks a public page's scripts from loopback, so the routed
website never loaded the widget.
2026-11-27 10:53:47 +01:00

76 lines
2.6 KiB
TypeScript

import { createServer } from "http";
import type { AddressInfo } from "net";
import { getClient } from "../../client";
import { createAgent } from "../../fixtures/agent/agent";
import {
createLicense,
deleteLicense,
ENTERPRISE_LICENSE,
} from "../../fixtures/licence";
import { baserowConfig } from "../../playwright.config";
import { expect, test } from "../baserowTest";
const markup = '<img src="missing.png" onerror="window.injected = true">';
/** Serves another website on loopback, as Chrome blocks a public page's loopback scripts. */
async function serveWebsite(
html: string
): Promise<{ url: string; close: () => Promise<void> }> {
const server = createServer((_request, response) => {
response.writeHead(200, { "Content-Type": "text/html" });
response.end(html);
});
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
const { port } = server.address() as AddressInfo;
return {
url: `http://127.0.0.1:${port}/`,
close: () =>
new Promise((resolve) => {
server.closeAllConnections();
server.close(() => resolve());
}),
};
}
test("a website shows the widget's texts as text and opens the chat", async ({
page,
workspacePage,
}) => {
const license = await createLicense(ENTERPRISE_LICENSE);
try {
const { workspace, user } = workspacePage;
const agent = await createAgent("Customer support", workspace);
const { data: widget } = await getClient(user).post(
`agent/${agent.id}/channels/`,
{ type: "website_widget", title: markup, button_text: markup }
);
const script = `${baserowConfig.PUBLIC_BACKEND_URL}/api/agent/public-chat/${widget.slug}/widget.js`;
const website = await serveWebsite(
`<html><body><h1>Acme</h1><script src="${script}" async></script></body></html>`
);
try {
await page.goto(website.url);
const host = page.locator(`#baserow-agent-widget-${widget.slug}`);
const button = host.getByRole("button", { name: markup, exact: true });
await expect(button).toBeVisible();
await expect(host.locator("img")).toHaveCount(0);
await button.click();
await expect(button).toHaveAttribute("aria-expanded", "true");
const chat = page.frameLocator(
`#baserow-agent-widget-${widget.slug} iframe`
);
await expect(
chat.getByRole("heading", { name: markup, exact: true }).first()
).toBeVisible();
expect(
await page.evaluate(() => (window as any).injected)
).toBeUndefined();
} finally {
await website.close();
}
} finally {
await deleteLicense(license);
}
});