import { test } from "node:test"; import assert from "node:assert/strict"; import { ACTIVE_SITE_COOKIE, ALL_SITES, activeSiteCookieName, isStorableActiveSite, resolveActiveSiteFrom, siteIdFromPathname, withoutSiteParam, } from "./activeSite"; // Run with: pnpm -C editor exec tsx --test "app/**/*.test.ts" // // The picker and the site's own pages must agree on which site is selected, and // under /sites// the PATH is the answer. This is the whole rule, pure: // no router, no storage, no disk. test("a site's own page yields the id and no segment", () => { assert.deepEqual(siteIdFromPathname("/sites/alpha"), { siteId: "alpha", segment: null, }); }); test("a tab yields the id and the segment", () => { assert.deepEqual(siteIdFromPathname("/sites/alpha/charts"), { siteId: "alpha", segment: "charts", }); }); test("the family page names no site", () => { assert.equal(siteIdFromPathname("/sites"), null); }); test("/sites/new is a static route, not a site", () => { assert.equal(siteIdFromPathname("/sites/new"), null); }); test("routes outside /sites name no site", () => { assert.equal(siteIdFromPathname("/channels"), null); }); test("a deeper path than a tab names no site", () => { assert.equal(siteIdFromPathname("/sites/a/b/c"), null); }); // ── The cookie (release 15 slice SS) ───────────────────────────────────────── // // The store is a cookie read on the server; these are its pure rules: the name a // request uses, what may be written, and which value wins. test("the cookie name carries the port, so two editors on one host keep one each", () => { assert.equal(activeSiteCookieName("localhost:3001"), `${ACTIVE_SITE_COOKIE}-3001`); assert.equal(activeSiteCookieName("127.0.0.1:3411"), `${ACTIVE_SITE_COOKIE}-3411`); assert.equal(activeSiteCookieName("[::1]:3101"), `${ACTIVE_SITE_COOKIE}-3101`); }); test("a host with no port, or no host, uses the base name", () => { assert.equal(activeSiteCookieName("editor.example.org"), ACTIVE_SITE_COOKIE); assert.equal(activeSiteCookieName(null), ACTIVE_SITE_COOKIE); assert.equal(activeSiteCookieName(undefined), ACTIVE_SITE_COOKIE); assert.equal(activeSiteCookieName("[::1]"), ACTIVE_SITE_COOKIE); }); test("a site id or the all-sites sentinel may be stored", () => { assert.equal(isStorableActiveSite("alpha"), true); assert.equal(isStorableActiveSite("a-1"), true); assert.equal(isStorableActiveSite(ALL_SITES), true); }); test("anything else is refused: the value goes into a header", () => { for (const bad of [ "", "Alpha", "-alpha", "a b", "a;b", "a=b", "a\r\nSet-Cookie: x=y", "_homepage", "a".repeat(129), null, undefined, 42, ]) { assert.equal(isStorableActiveSite(bad), false, JSON.stringify(bad)); } assert.equal(isStorableActiveSite("a".repeat(128)), true); }); test("the first candidate naming a configured site wins", () => { const ids = ["alpha", "beta"]; // A `?site=` link beats the cookie. assert.equal(resolveActiveSiteFrom(["beta", "alpha"], ids).value, "beta"); // No param: the cookie. assert.equal(resolveActiveSiteFrom([null, "alpha"], ids).value, "alpha"); assert.equal(resolveActiveSiteFrom([undefined, "alpha"], ids).siteId, "alpha"); // The sentinel is a real choice, not a miss. assert.equal(resolveActiveSiteFrom([ALL_SITES, "alpha"], ids).isAll, true); }); test("a param naming no site falls through to the cookie", () => { const ids = ["alpha", "beta"]; assert.equal(resolveActiveSiteFrom(["gone", "beta"], ids).value, "beta"); }); test("nothing valid resolves to the default: the lone site, else all sites", () => { assert.equal(resolveActiveSiteFrom([null, "gone"], ["alpha", "beta"]).value, ALL_SITES); assert.equal(resolveActiveSiteFrom([null, null], ["solo"]).value, "solo"); assert.equal(resolveActiveSiteFrom([], []).value, ALL_SITES); }); test("dropping the site param keeps every other param", () => { assert.equal(withoutSiteParam("/channels", "site=alpha"), "/channels"); assert.equal( withoutSiteParam("/channels", "site=alpha&location=internal&sort=size"), "/channels?location=internal&sort=size", ); assert.equal(withoutSiteParam("/", ""), "/"); });