import { describe, expect, it } from "bun:test"; import { createElement } from "react"; import { renderToStaticMarkup } from "react-dom/server"; import { ApiError } from "./api"; import { anchoredChildPaths, AnchoredChildSurface, childCloseAction, childHistoryState, childPresentation, rebaseChildHistoryState, } from "./anchored-child-surface"; import { githubLoginHref, hostedRoute, retryableChannelFailure } from "./hosted"; import { prepareDocumentLoad, validatedChildPath } from "./document-loader"; import type { ChannelDetail } from "./api"; function detail( id: string, slug: string, parentChannelId?: string, repositoryId = "R_score", ): ChannelDetail { return { repository: { id: repositoryId, owner: "octo-org", name: "score", fullName: "octo-org/score", private: false, url: "https://github.com/octo-org/score", defaultBranch: "main", permissions: { pull: true, push: true, admin: false }, }, canEdit: true, canManage: true, channel: { descriptionRevision: 0, id, repositoryId, repositoryOwner: "octo-org", repositoryName: "score", ...(parentChannelId ? { parentChannelId } : {}), title: slug, slug, createdBy: "user-one", revision: 0, createdAt: "2026-08-24T00:00:00.000Z", updatedAt: "2026-08-24T00:00:00.000Z", }, }; } describe("hosted routes", () => { it("recognizes canonical document routes and rejects the removed research route", () => { expect(hostedRoute("/")).toEqual({ page: "repositories" }); expect(hostedRoute("/documents/octo-org/score")).toEqual({ page: "repository", owner: "octo-org", repository: "score", }); expect(hostedRoute("/documents/octo-org/score/r%C3%A9sum%C3%A9-%E8%A8%88%E7%94%BB")) .toEqual({ page: "document", owner: "octo-org", repository: "score", slug: "résumé-計画", }); expect(hostedRoute( "/documents/octo-org/score/r%C3%A9sum%C3%A9-%E8%A8%88%E7%94%BB/research/workspace%3A1", )).toEqual({ page: "missing" }); expect(hostedRoute("/repositories/octo-org/score")).toEqual({ page: "repository", owner: "octo-org", repository: "score", }); expect(hostedRoute("/repositories/github%20next/chopin")).toEqual({ page: "repository", owner: "github next", repository: "chopin", }); expect(hostedRoute("/channels/019c1234-1234-4123-8123-123456789abc")).toEqual({ page: "channel", id: "019c1234-1234-4123-8123-123456789abc", }); expect(hostedRoute("/channels/019c1234-1234-5123-8123-123456789abc")).toEqual({ page: "channel", id: "019c1234-1234-5123-8123-123456789abc", }); expect(hostedRoute("/plans/main")).toEqual({ page: "missing" }); expect(hostedRoute("/documents/octo-org/score/plan/extra")) .toEqual({ page: "missing" }); expect(hostedRoute("/documents/octo-org/score/plan/research")) .toEqual({ page: "missing" }); }); it("recognizes a child route before the ordinary document route", () => { expect(hostedRoute( "/documents/octo-org/score/release-plan/children/source%20review", )).toEqual({ page: "child", owner: "octo-org", repository: "score", parentSlug: "release-plan", childSlug: "source review", }); }); it("accepts direct child entry only for its recorded parent and repository", () => { let parent = detail("parent-one", "canonical-parent"); let child = detail("child-one", "canonical-child", parent.channel.id); expect(validatedChildPath(child, parent)).toBe( "/documents/octo-org/score/canonical-parent/children/canonical-child", ); for ( let invalid of [ detail("child-one", "canonical-child"), detail("child-one", "canonical-child", "different-parent"), detail("child-one", "canonical-child", parent.channel.id, "R_other"), ] ) { expect(() => validatedChildPath(invalid, parent)).toThrow(ApiError); try { validatedChildPath(invalid, parent); } catch (error) { expect((error as ApiError).status).toBe(404); } } }); }); describe("anchored child lifecycle", () => { it("renders one parent-aware close control over an inert parent with separate rooms", () => { let markup = renderToStaticMarkup(createElement(AnchoredChildSurface, { child: createElement("div", { "data-workspace-room": "child-room" }), childLabel: "Source review", onClose() {}, parent: createElement("div", { "data-workspace-room": "parent-room" }), parentLabel: "Release plan", presentation: "open", })); expect(markup).toContain('aria-hidden="true"'); expect(markup).toContain('inert=""'); expect(markup).toContain('aria-label="Back to Release plan"'); expect(markup.match(/