import { afterAll, afterEach, beforeAll, expect, mock, test } from "bun:test"; import { GlobalRegistrator } from "@happy-dom/global-registrator"; import { OpenGeniApiError } from "@opengeni/sdk/browser"; import { act, createContext, useContext, type ReactNode } from "react"; import { createRoot, type Root } from "react-dom/client"; import type { Session, SessionListPageOptions } from "@opengeni/sdk"; const workspaceId = "11111111-1111-4111-8111-111111111111"; const siteId = "22222222-2222-4222-8222-222222222222"; const otherSiteId = "44444444-4444-4444-8444-444444444444"; const id = (n: number) => `00000000-0000-4000-9000-${String(n).padStart(12, "0")}`; let permissions = ["sessions:create"]; let subjectId = "me"; let conversations: Session[] = []; let listPage: ((options: SessionListPageOptions) => object) | null = null; let fullSessions: Record = {}; let siteDetails: () => Promise = async () => ({ artifact: { title: "Opengeni usage", status: "active", currentVersion: { id: "version-4", revision: 4 }, }, versions: [], events: [], }); let hintRefresh: (() => Promise) | null = null; let hintOptions: { siteConversations?: boolean } | undefined; const listCalls: SessionListPageOptions[] = []; const sessionReads: string[] = []; const navigations: unknown[] = []; const requests: Array<{ path: string; init: RequestInit | undefined }> = []; const conversationProps: Array> = []; const composerProps: Array> = []; const opens: string[] = []; const siteViewProps: Array> = []; const versionMenuProps: Array> = []; let siteReads = 0; /** The Site as read with version `revision` live. */ const siteAt = (revision: number, title = "Opengeni usage") => ({ artifact: { title, status: "active", currentVersion: { id: `version-${revision}`, revision }, }, versions: [], events: [], }); const client = { recordSiteOpened: async (_workspace: string, opened: string) => { opens.push(opened); return { openedAt: new Date().toISOString() }; }, listSessionPage: async (_workspace: string, options: SessionListPageOptions) => { listCalls.push(options); return listPage?.(options) ?? { pinned: [], sessions: conversations, nextCursor: null }; }, getSession: async (_workspace: string, sessionId: string) => { sessionReads.push(sessionId); const session = fullSessions[sessionId]; if (!session) throw new OpenGeniApiError(404, ""); return session; }, getWorkspaceArtifact: async () => { siteReads++; return await siteDetails(); }, // The header's published-link read: an API without the route (404) unless // a test publishes the Site. getSitePublicationLink: async () => await publicationLink(), }; let publicationLink: () => Promise = async () => { throw new OpenGeniApiError(404, ""); }; const notPublished = async () => { throw new OpenGeniApiError(404, JSON.stringify({ error: { code: "site_not_published" } })); }; const published = { siteId, hostname: "usage-4k9x.sites.example", url: "https://usage-4k9x.sites.example/", status: "active", }; const publishPageProps: Array> = []; const managementArgs: Array> = []; const publications: unknown[] = []; mock.module("../app-context", () => ({ useAppContext: () => ({ client, accessContext: { subjectId, workspaceGrants: [{ workspaceId, permissions }] }, }), })); mock.module("../lib/app-api", () => ({ useAppApi: () => ({ request: async (path: string, init?: RequestInit) => { requests.push({ path, init }); return { id: id(99) }; }, }), })); mock.module("../lib/navigation", () => ({ useNavigate: () => async (options: unknown) => void navigations.push(options), useSearch: () => ({}), useStringifySearch: () => () => "", // The window bar's module imports these; its title never calls them. usePage: () => null, useRouter: () => ({ state: { location: { pathname: "/" } } }), useRouterState: () => undefined, Link: ({ children, to, params, search, ...props }: { children: ReactNode; to: string; params: Record; search?: Record; "aria-label"?: string; "aria-current"?: "page"; }) => ( { event.preventDefault(); navigations.push({ to, params, search }); }} > {children} ), })); mock.module("../lib/console-site", () => ({ useConsoleSiteViewer: () => ({ kind: "member", id: "user:me" }), useConsoleSiteToolBridge: () => () => undefined, })); mock.module("../lib/use-session-list-hint-refresh", () => ({ useSessionListHintRefresh: ( refresh: () => Promise, _error: unknown, options?: { siteConversations?: boolean }, ) => { hintRefresh = refresh; hintOptions = options; }, })); mock.module("../../artifacts", () => ({ SiteView: (props: { siteId: string; archivedMessage?: string; versionId?: string | null; versionControl?: ReactNode; liveStatus?: ReactNode; fullScreen?: boolean; }) => { siteViewProps.push(props); return (
{props.versionControl}
{props.liveStatus}
); }, })); // The Site's own publish form and its management, as the Settings tab uses // them; the form's own behaviour is tested with it. mock.module("../components/artifacts/site-settings", () => ({ useSiteManagement: (args: Record) => { managementArgs.push(args); return { canPublish: true, isAdmin: false, manageData: true, workspaceName: "Acme", publication: { load: { state: "none" }, reload: async () => {}, setPublication: (next: unknown) => publications.push(next), }, audience: { share: undefined, dialog:
}, }; }, })); mock.module("../components/artifacts/site-publishing", () => ({ SitePublishPage: (props: Record) => { publishPageProps.push(props); // As FormPage does on a page: its title claims the macOS window bar. windowBarTitles.useWindowBarTitle(props.layout === "page" ? "Publish to web" : null); const publish = () => { (props.onPublication as (next: unknown) => void)({ ...published, access: "members" }); (props.onSubmitted as () => void)(); }; return (
{/* As FormPage's title: focusable from script only. */}

Publish to web

); }, })); mock.module("../components/artifacts/site-version-menu", () => ({ SiteVersionMenu: (props: Record) => { versionMenuProps.push(props); return ; }, })); // The regular chat route and new-chat composer; their own tests cover them. // Here the panel's file drop target stands in for the chat's, so drops on // the Site can be followed to it. let dropped: File[] = []; mock.module("./session", () => ({ SessionRoute: (props: { sessionId: string }) => { conversationProps.push(props); return ( (dropped = [...dropped, ...files])} >
); }, })); mock.module("../components/session/console-new-conversation", () => ({ ConsoleNewConversation: (props: { createSession: (text: string, key: string, options: object) => Promise; onCreated: (created: object) => void; title: string; }) => { composerProps.push(props); return ( <>