Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

57.7 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

import assert from "node:assert/strict";
import test from "node:test";
import vm from "node:vm";

import { readSrc, readText } from "./helpers/kit.ts";
import { readAudioWorkspaceSource } from "./helpers/audio-workspace.ts";

const script = readText("../public/reload-snapshot.js");
const indexHtml = readText("../index.html");
const indexCss = readSrc("index.css");
const rootRouteSource = readSrc("app/routes/__root.tsx");
const runtimeProviderSource = readSrc("features/chat/runtime-provider.tsx");
const chatPageSource = readSrc("features/chat/chat-page.tsx");
const sharedComposerSource = readSrc("features/chat/shared-composer.tsx");
const imageDropzoneSource = readSrc("components/image-dropzone.tsx");
const attachmentPreviewSource = readSrc("components/assistant-ui/attachment-preview.tsx");

const attachmentSource = readSrc("components/assistant-ui/attachment.tsx");
const imagesPageSource = readSrc("features/images/images-page.tsx");
const videoPageSource = readSrc("features/video/video-page.tsx");
const audioPageSource = readAudioWorkspaceSource();
const hubPageSource = readSrc("features/hub/hub-page.tsx");
const referencePickerSource = readSrc("features/video/reference-picker.tsx");

const unstructuredDropZoneSource = readSrc("features/recipe-studio/dialogs/seed/unstructured-drop-zone.tsx");

const projectsPageSource = readSrc("features/chat/projects-page.tsx");

const seedDialogSource = readSrc("features/recipe-studio/dialogs/seed/seed-dialog.tsx");

const projectSourceDropzoneSource = readSrc("features/rag/components/project-source-dropzone.tsx");
const dataRecipesPageSource = readSrc("features/data-recipes/pages/data-recipes-page.tsx");
const editRecipePageSource = readSrc("features/data-recipes/pages/edit-recipe-page.tsx");
const exportPageSource = readSrc("features/export/export-page.tsx");
const studioPageSource = readSrc("features/studio/studio-page.tsx");
const apiMonitorPageSource = readSrc("features/api-monitor/api-monitor-page.tsx");
const authFormSource = readSrc("features/auth/components/auth-form.tsx");

test("scoped readiness still emits the reload snapshot event", () => {
  const readinessSource = readSrc("components/app-readiness.ts");
  assert.match(readinessSource, /function signalReloadSnapshotReady\(\): void \{\s*window\.dispatchEvent\(new Event\("unsloth:app-shell-ready"\)\)/);
  assert.match(readinessSource, /signalReady: \(\) => \{\s*if \(!active\) return;\s*onReady\(\);\s*signalReloadSnapshotReady\(\)/);
  assert.match(readinessSource, /value: scope\.signalReady/);
  assert.match(readinessSource, /return useContext\(AppShellReadyContext\)/);
  for (const source of [rootRouteSource, runtimeProviderSource, chatPageSource,
    hubPageSource, projectsPageSource, dataRecipesPageSource, editRecipePageSource,
    exportPageSource, studioPageSource, apiMonitorPageSource, authFormSource]) {
    assert.match(source, /useAppShellReadySignal/);
  }
});


type Listener = (event: Record<string, unknown>) => void;

interface ElementSpec {
  tag: string;
  value?: string;
  checked?: boolean;
  selected?: boolean;
  type?: string;
  autocomplete?: string;
  attributes?: Record<string, string>;
  display?: string;
  visibility?: string;
  overflowX?: string;
  overflowY?: string;
  scrollTop?: number;
  scrollLeft?: number;
  scrollHeight?: number;
  scrollWidth?: number;
  clientHeight?: number;
  clientWidth?: number;
  currentSrc?: string;
  naturalWidth?: number;
  naturalHeight?: number;
  videoWidth?: number;
  videoHeight?: number;
  width?: number;
  height?: number;
  rect?: [number, number, number, number];
  text?: string;
  children?: ElementSpec[];
}

interface ShadowStub {
  mode: string;
  children: Array<Record<string, unknown>>;
  appendChild(child: Record<string, unknown>): Record<string, unknown>;
}

interface StubElement {
  tagName: string;
  spec: ElementSpec;
  value: string;
  checked: boolean;
  selected: boolean;
  type: string;
  autocomplete: string;
  scrollTop: number;
  scrollLeft: number;
  scrollHeight: number;
  scrollWidth: number;
  clientHeight: number;
  clientWidth: number;
  currentSrc: string;
  naturalWidth: number;
  naturalHeight: number;
  videoWidth: number;
  videoHeight: number;
  width: number;
  height: number;
  textContent: string;
  attributeOverrides: Record<string, string>;
  hasAttribute(name: string): boolean;
  getAttribute(name: string): string | null;
  setAttribute(name: string, value: string): void;
  children: StubElement[];
  parent: StubElement | null;
  readonly parentElement: StubElement | null;
  readonly nextElementSibling: StubElement | null;
  attributes: Array<{ name: string }>;
  closest(selector: string): StubElement | null;
  getBoundingClientRect(): {
    top: number;
    right: number;
    bottom: number;
    left: number;
  };
  remove(): void;
  replaceChildren(): void;
  removeAttribute(name: string): void;
  querySelectorAll(selector: string): StubElement[];
  cloneNode(deep: boolean): StubElement;
  readonly firstElementChild: StubElement | undefined;
  readonly innerHTML: string;
  readonly outerHTML: string;
}

function createElement(spec: ElementSpec, parent: StubElement | null = null) {
  const element = {
    tagName: spec.tag.toUpperCase(),
    spec,
    value: spec.value ?? "",
    checked: spec.checked ?? false,
    selected: spec.selected ?? false,
    type: spec.type ?? "text",
    autocomplete: spec.autocomplete ?? "",
    scrollTop: spec.scrollTop ?? 0,
    scrollLeft: spec.scrollLeft ?? 0,
    scrollHeight: spec.scrollHeight ?? spec.clientHeight ?? 900,
    scrollWidth: spec.scrollWidth ?? spec.clientWidth ?? 1440,
    clientHeight: spec.clientHeight ?? 900,
    clientWidth: spec.clientWidth ?? 1440,
    currentSrc: spec.currentSrc ?? spec.attributes?.src ?? "",
    naturalWidth: spec.naturalWidth ?? 0,
    naturalHeight: spec.naturalHeight ?? 0,
    videoWidth: spec.videoWidth ?? 0,
    videoHeight: spec.videoHeight ?? 0,
    width: spec.width ?? 0,
    height: spec.height ?? 0,
    textContent: spec.text ?? "",
    children: [] as StubElement[],
    parent,
    attributeOverrides: {
      ...(spec.attributes ?? {}),
      ...(spec.autocomplete ? { autocomplete: spec.autocomplete } : {}),
    } as Record<string, string>,
    get attributes() {
      return Object.keys(element.attributeOverrides).map((name) => ({ name }));
    },
    hasAttribute(name: string) {
      return Object.hasOwn(element.attributeOverrides, name);
    },
    getAttribute(name: string) {
      return element.attributeOverrides[name] ?? null;
    },
    closest(selector: string) {
      let node: StubElement | null = element;
      while (node) {
        if (node.tagName.toLowerCase() === selector) return node;
        node = node.parent;
      }
      return null;
    },
    getBoundingClientRect() {
      // Browsers report an empty rect for a box that is not laid out.
      const [top, right, bottom, left] =
        spec.display === "contents"
          ? [0, 0, 0, 0]
          : (spec.rect ?? [0, 1440, 900, 0]);
      return { top, right, bottom, left };
    },
    remove() {
      if (!element.parent) return;
      element.parent.children = element.parent.children.filter(
        (child) => child !== element,
      );
      element.parent = null;
    },
    replaceChildren() {
      for (const child of element.children) child.parent = null;
      element.children = [];
      element.textContent = "";
    },
    setAttribute(name: string, value: string) {
      element.attributeOverrides[name] = value;
    },
    removeAttribute(name: string) {
      delete element.attributeOverrides[name];
    },
    get parentElement() {
      return element.parent;
    },
    get nextElementSibling() {
      if (!element.parent) return null;
      const index = element.parent.children.indexOf(element);
      return element.parent.children[index + 1] ?? null;
    },
    querySelectorAll(selector: string) {
      const wanted = selector.split(",").map((name) => name.trim());
      const found: StubElement[] = [];
      const walk = (node: StubElement) => {
        for (const child of node.children) {
          if (
            selector === "*" ||
            wanted.includes(child.tagName.toLowerCase()) ||
            wanted.some(
              (name) =>
                name.startsWith("[") &&
                name.endsWith("]") &&
                child.hasAttribute(name.slice(1, -1)),
            )
          ) {
            found.push(child);
          }
          walk(child);
        }
      };
      walk(element);
      return found;
    },
    cloneNode() {
      return createElement(spec);
    },
    get firstElementChild() {
      return element.children[0];
    },
    get innerHTML() {
      return element.children
        .map((child) => child.outerHTML)
        .concat(element.textContent)
        .join("");
    },
    get outerHTML() {
      const attributes = Object.entries(element.attributeOverrides)
        .map(([name, value]) => ` ${name}="${value}"`)
        .join("");
      return `<${spec.tag}${attributes}>${element.innerHTML}</${spec.tag}>`;
    },
  } as StubElement;
  element.children = (spec.children ?? []).map((child) =>
    createElement(child, element),
  );
  return element;
}

type StubStyle = {
  length: number;
  getPropertyValue(name: string): string;
  setProperty(name: string, value: string): void;
  removeProperty(name: string): void;
} & Record<number, string>;

function createStyle(initial: Record<string, string> = {}) {
  const values = new Map(Object.entries(initial));
  const style = {
    getPropertyValue: (name: string) => values.get(name) ?? "",
    setProperty: (name: string, value: string) => {
      values.set(name, value);
      index();
    },
    removeProperty: (name: string) => {
      values.delete(name);
      index();
    },
    get length() {
      return values.size;
    },
  } as StubStyle;
  const index = () => {
    [...values.keys()].forEach((name, position) => {
      style[position] = name;
    });
  };
  index();
  return style;
}

function createEnvironment(options: {
  navigationType: "navigate" | "reload";
  storage?: Map<string, string>;
  rootHtml?: string;
  rootTree?: ElementSpec;
  bodyPortals?: ElementSpec[];
  viewport?: { width: number; height: number };
  htmlVariables?: Record<string, string>;
  htmlAttributes?: Record<string, string>;
  styleSheets?: string[];
  frameDataBody?: string;
  inlineStyleSheets?: string[];
  trackScrollRestores?: boolean;
  localStorage?: Map<string, string>;
  supportsPageSwap?: boolean;
}) {
  const storage = options.storage ?? new Map<string, string>();
  const listeners = new Map<string, Listener[]>();
  const animationFrames: Array<() => void> = [];
  const fontFaces: Array<{ family: string; source: string }> = [];
  const appended: Array<{ innerHTML: string; removed: boolean }> = [];
  let scrollRestoreCalls = 0;
  let attributeObserver: ((records: { attributeName: string }[]) => void) | null = null;
  let observingAttributes = false;
  const bodyTree = options.rootTree
    ? createElement({
        tag: "body",
        children: [
          {
            ...options.rootTree,
            attributes: { ...options.rootTree.attributes, id: "root" },
          },
          ...(options.bodyPortals ?? []),
        ],
      })
    : null;
  const clone = {
    innerHTML: `<div id="root">${options.rootHtml ?? "<main>Chat is ready</main>"}</div>`,
    querySelectorAll: () => [],
  };
  const root = bodyTree?.children[0] ?? {
    firstElementChild: {},
    querySelectorAll: () => [],
  };
  const body = bodyTree ?? {
    cloneNode: () => clone,
    querySelectorAll: () => [],
  };

  const window = {
    addEventListener(name: string, listener: Listener) {
      const current = listeners.get(name) ?? [];
      current.push(listener);
      listeners.set(name, current);
    },
  };
  if (options.supportsPageSwap) {
    Object.assign(window, { onpageswap: null });
  }
  const htmlAttributes = new Map(Object.entries(options.htmlAttributes ?? {}));
  const documentElement = {
    style: createStyle(options.htmlVariables),
    hasAttribute: (name: string) => htmlAttributes.has(name),
    getAttribute: (name: string) => htmlAttributes.get(name) ?? null,
    setAttribute: (name: string, value: string) => {
      htmlAttributes.set(name, value);
    },
    removeAttribute: (name: string) => {
      htmlAttributes.delete(name);
    },
    get attributes() {
      return [...htmlAttributes].map(([name, value]) => ({ name, value }));
    },
    appendChild(element: { innerHTML: string; removed: boolean }) {
      appended.push(element);
    },
  };
  const document = {
    body,
    documentElement,
    fonts: {
      add(face: { family: string; source: string }) {
        fontFaces.push(face);
      },
    },
    getElementById: () => root,
    querySelectorAll: (selector: string) => {
      if (selector === 'link[rel="stylesheet"]') {
        return (options.styleSheets ?? []).map((href) => ({ href }));
      }
      if (selector === "style[data-vite-dev-id]") {
        return (options.inlineStyleSheets ?? []).map((textContent) => ({
          textContent,
        }));
      }
      return [];
    },
    createElement: (tag?: string) => {
      if (tag === "canvas") {
        return {
          width: 0,
          height: 0,
          getContext: () => ({ drawImage() {} }),
          toDataURL: () =>
            "data:image/webp;base64," +
            (options.frameDataBody ?? "retained-frame"),
        };
      }
      const element = {
        tagName: (tag ?? "div").toUpperCase(),
        className: "",
        attributes: {} as Record<string, string>,
        style: {
          values: {} as Record<string, string>,
          setProperty(name: string, value: string) {
            element.style.values[name] = value;
          },
        },
        rel: "",
        href: "",
        onload: null as (() => void) | null,
        onerror: null as (() => void) | null,
        inert: false,
        innerHTML: "",
        textContent: "",
        children: [] as Record<string, unknown>[],
        removed: false,
        shadow: null as ShadowStub | null,
        attachShadow(init: { mode: string }) {
          const shadow: ShadowStub = {
            mode: init.mode,
            children: [],
            appendChild(child: Record<string, unknown>) {
              shadow.children.push(child);
              return child;
            },
          };
          element.shadow = shadow;
          return shadow;
        },
        setAttribute(name: string, value: string) {
          element.attributes[name] = value;
        },
        toggleAttribute(name: string, force: boolean) {
          if (force) element.attributes[name] = "";
          else delete element.attributes[name];
          return force;
        },
        getAttribute(name: string) {
          return element.attributes[name] ?? null;
        },
        querySelectorAll(selector?: string) {
          if (
            tag === "body" &&
            options.trackScrollRestores &&
            selector ===
              "[data-reload-scroll-top], [data-reload-scroll-left]"
          ) {
            scrollRestoreCalls += 1;
            return [
              {
                scrollTop: 0,
                scrollLeft: 0,
                getAttribute(name: string) {
                  return name === "data-reload-scroll-top" ? "600" : null;
                },
              },
            ];
          }
          return [];
        },
        appendChild(child: Record<string, unknown>) {
          element.children.push(child);
          return child;
        },
        remove() {
          element.removed = true;
        },
      };
      return element;
    },
  };

  vm.runInNewContext(script, {
    Array,
    Date,
    JSON,
    Object,
    clearTimeout() {
      // Timer bookkeeping is outside this lifecycle test.
    },
    document,
    MutationObserver: class {
      constructor(callback: (records: { attributeName: string }[]) => void) {
        attributeObserver = callback;
      }
      observe(target: unknown, options: { attributes: boolean }) {
        assert.equal(target, documentElement);
        assert.equal(options.attributes, true);
        observingAttributes = true;
      }
      disconnect() {
        observingAttributes = false;
      }
    },
    getComputedStyle: (element: StubElement) =>
      (element as unknown) === documentElement
        ? documentElement.style
        : {
            display: element.spec?.display ?? "block",
            visibility: element.spec?.visibility ?? "visible",
            overflowX: element.spec?.overflowX ?? "visible",
            overflowY: element.spec?.overflowY ?? "visible",
          },
    innerHeight: options.viewport?.height ?? 900,
    innerWidth: options.viewport?.width ?? 1440,
    location: { pathname: "/chat", search: "" },
    localStorage: {
      getItem: (key: string) => options.localStorage?.get(key) ?? null,
    },
    FontFace: class {
      family: string;
      source: string;
      constructor(family: string, source: string) {
        this.family = family;
        this.source = source;
      }
      load() {
        return Promise.resolve(this);
      }
    },
    performance: {
      getEntriesByType: () => [{ type: options.navigationType }],
    },
    requestAnimationFrame(callback: () => void) {
      animationFrames.push(callback);
    },
    sessionStorage: {
      getItem: (key: string) => storage.get(key) ?? null,
      removeItem: (key: string) => storage.delete(key),
      setItem: (key: string, value: string) => storage.set(key, value),
    },
    setTimeout: () => 1,
    window,
  });

  return {
    storage,
    fontFaces,
    appended,
    htmlVariables: documentElement.style,
    htmlAttributes,
    get observingAttributes() {
      return observingAttributes;
    },
    changeRootAttribute(name: string, value: string | null) {
      if (value === null) htmlAttributes.delete(name);
      else htmlAttributes.set(name, value);
      if (observingAttributes) attributeObserver?.([{ attributeName: name }]);
    },
    get scrollRestoreCalls() {
      return scrollRestoreCalls;
    },
    get shell() {
      const host = appended[0] as unknown as
        | {
            shadow: ShadowStub | null;
            removed: boolean;
            style: Record<string, unknown>;
          }
        | undefined;
      if (!host?.shadow) return null;
      const children = host.shadow.children;
      const root = children[children.length - 1];
      const body = (
        root?.children as Record<string, unknown>[] | undefined
      )?.find((child) => child.tagName === "BODY");
      return {
        host,
        hostStyle: host.style,
        mode: host.shadow.mode,
        stylesheets: children
          .filter((child) => child.rel === "stylesheet")
          .map((child) => child.href as string),
        inlineStyles: children
          .filter((child) => child.tagName === "STYLE")
          .map((child) => child.textContent as string),
        html: (body?.innerHTML ?? root?.innerHTML ?? "") as string,
        bodyTag: (body?.tagName ?? "") as string,
        rootClass: (root?.className ?? "") as string,
        rootTag: (root?.tagName ?? "") as string,
        rootAttributes: (root?.attributes ?? {}) as Record<string, string>,
        rootTokens: ((
          root?.style as
            | { values?: Record<string, string> }
            | undefined
        )?.values ?? {}) as Record<string, string>,
        rootVisibility: (root?.style as { visibility?: string } | undefined)
          ?.visibility,
      };
    },
    dispatch(name: string, event: Record<string, unknown> = {}) {
      for (const listener of listeners.get(name) ?? []) {
        listener(event);
      }
    },
    loadStyleSheets() {
      const host = appended[0] as unknown as
        | { shadow: ShadowStub | null }
        | undefined;
      for (const child of host?.shadow?.children ?? []) {
        if (child.rel === "stylesheet") {
          (child.onload as (() => void) | null)?.();
        }
      }
    },
    runAnimationFrame() {
      const callbacks = animationFrames.splice(0);
      for (const callback of callbacks) {
        callback();
      }
    },
  };
}

function storedSnapshot(storage: Map<string, string>) {
  const raw = storage.get("unsloth.reload-snapshot.v1");
  assert.ok(raw, "expected a stored snapshot");
  return JSON.parse(raw) as {
    html: string;
    styles?: string[];
    inlineStyles?: string[];
    rootClass?: string;
    tokens?: Record<string, string>;
    appearance?: {
      variables: Record<string, string>;
      attributes: Record<string, string>;
    };
  };
}

test("carries the rendered shell through a reload until the new shell is ready", () => {
  const outgoing = createEnvironment({
    navigationType: "navigate",
    rootHtml: "<main>Existing chat</main>",
    styleSheets: ["/assets/index-abc123.css"],
    supportsPageSwap: true,
  });
  outgoing.dispatch("pageswap", {
    activation: { navigationType: "reload" },
  });

  const incoming = createEnvironment({
    navigationType: "reload",
    storage: outgoing.storage,
  });
  assert.equal(incoming.appended.length, 1);
  assert.equal(
    incoming.shell?.html,
    '<div id="root"><main>Existing chat</main></div>',
  );
  assert.equal(incoming.storage.size, 0);

  incoming.dispatch("unsloth:app-shell-ready");
  assert.equal(incoming.appended[0].removed, false);
  incoming.runAnimationFrame();
  assert.equal(incoming.appended[0].removed, false);
  incoming.runAnimationFrame();
  assert.equal(incoming.appended[0].removed, true);
});

test("captures reloads through WebKit's pagehide fallback", () => {
  const outgoing = createEnvironment({
    navigationType: "navigate",
    rootHtml: "<main>WebKit chat</main>",
    styleSheets: ["/assets/index-abc123.css"],
  });
  outgoing.dispatch("pagehide", { persisted: false });

  const incoming = createEnvironment({
    navigationType: "reload",
    storage: outgoing.storage,
  });
  assert.match(incoming.shell?.html ?? "", /WebKit chat/);
});

test("does not run the pagehide fallback when pageswap is available", () => {
  const environment = createEnvironment({
    navigationType: "navigate",
    rootHtml: "<main>Chromium chat</main>",
    styleSheets: ["/assets/index-abc123.css"],
    supportsPageSwap: true,
  });
  environment.dispatch("pagehide", { persisted: false });
  assert.equal(environment.storage.size, 0);
});

test("carries the retained shell through consecutive reloads", () => {
  const outgoing = createEnvironment({
    navigationType: "navigate",
    rootHtml: "<main>Stable chat</main>",
    styleSheets: ["/assets/index-abc123.css"],
    supportsPageSwap: true,
  });
  outgoing.dispatch("pageswap", {
    activation: { navigationType: "reload" },
  });

  const firstReload = createEnvironment({
    navigationType: "reload",
    storage: outgoing.storage,
    rootHtml: "<main>Still loading</main>",
    supportsPageSwap: true,
  });
  firstReload.dispatch("pageswap", {
    activation: { navigationType: "reload" },
  });
  firstReload.dispatch("pagehide", { persisted: false });

  const secondReload = createEnvironment({
    navigationType: "reload",
    storage: firstReload.storage,
  });
  assert.match(secondReload.shell?.html ?? "", /Stable chat/);
  assert.doesNotMatch(secondReload.shell?.html ?? "", /Still loading/);
});

test("restores from a parser-blocking head script before the first body paint", () => {
  const scriptPosition = indexHtml.indexOf(
    '<script src="/reload-snapshot.js">',
  );
  assert.ok(scriptPosition > 0);
  assert.ok(scriptPosition < indexHtml.indexOf("</head>"));
});

test("does not retain the shell for a non-reload navigation", () => {
  const environment = createEnvironment({ navigationType: "navigate" });
  environment.dispatch("pageswap", {
    activation: { navigationType: "push" },
  });
  assert.equal(environment.storage.size, 0);
});

test("never persists a Temporary Chat shell", () => {
  const environment = createEnvironment({
    navigationType: "navigate",
    rootHtml: "<main>Sensitive temporary transcript</main>",
    htmlAttributes: { "data-reload-snapshot-private": "" },
  });
  environment.dispatch("pageswap", {
    activation: { navigationType: "reload" },
  });
  assert.equal(environment.storage.size, 0);
});

test("mirrors Temporary Chat privacy and lets history completion retire chat shells", () => {
  assert.match(
    rootRouteSource,
    /toggleAttribute\(\s*"data-reload-snapshot-private",\s*incognito,/,
  );
  assert.equal(
    [...rootRouteSource.matchAll(/<ReloadSnapshotReady \/>/g)].length,
    1,
    "chat must not use the generic commit-time readiness marker",
  );
  assert.match(
    runtimeProviderSource,
    /async load\(\) \{[\s\S]*?const completeLoad =[\s\S]*?signalReady\(\)[\s\S]*?await loadGenerationOverlaySnapshot\([\s\S]*?listStoredChatMessages[\s\S]*?return completeLoad/,
  );
  assert.match(
    runtimeProviderSource,
    /!reloadReadyThreadId \|\| loadedThreadId === reloadReadyThreadId/,
  );
  // A failed switchToThread must release the shell, ahead of the staleness guard.
  assert.match(
    runtimeProviderSource,
    /switchToThread\(threadId\)[\s\S]*?\.then\([\s\S]*?onSwitchFailed\?\.\(\)/,
  );
  const rejectionArm = runtimeProviderSource.slice(
    runtimeProviderSource.indexOf("onSwitchFailed?.()"),
  );
  assert.ok(
    rejectionArm.indexOf("onSwitchFailed?.()") <
      rejectionArm.indexOf("attempt !== attemptAtStart"),
    "onSwitchFailed must fire before the staleness guard can return early",
  );
  assert.match(
    runtimeProviderSource,
    /const signalFailedInitialSwitchReady = useCallback[\s\S]*?onInitialHistoryReady\(\)[\s\S]*?signalReady\(\)[\s\S]*?onSwitchFailed=\{signalFailedInitialSwitchReady\}/,
  );
  assert.match(
    runtimeProviderSource,
    /createRuntimeHook\([\s\S]*?modelType,[\s\S]*?pairId,[\s\S]*?initialThreadId,[\s\S]*?onInitialHistoryReady/,
  );
  // Compare panes bootstrap on an empty thread first, so readiness must check the thread.
  assert.match(
    runtimeProviderSource,
    /const completeLoad =[\s\S]*?if \(onInitialHistoryReady\) \{\s*if \(loadedTheRequestedThread\) onInitialHistoryReady\(\);/,
  );
  assert.match(
    runtimeProviderSource,
    /const signalFailedInitialSwitchReady = useCallback[\s\S]*?if \(onInitialHistoryReady\) \{\s*onInitialHistoryReady\(\);/,
  );
  assert.match(
    chatPageSource,
    /state\.panes\.add\(pane\)[\s\S]*?state\.panes\.size < 2[\s\S]*?signalReady\(\)/,
  );
  assert.match(
    chatPageSource,
    /\.finally\(\(\) => \{\s*if \(isActive\) setThreadsSettled\(true\)/,
  );
  assert.match(
    chatPageSource,
    /if \(!threadsSettled\) return;\s*if \(!baseThreadId\) markInitialHistoryReady\("base"\);\s*if \(!loraThreadId\) markInitialHistoryReady\("lora"\);/,
  );
  assert.match(
    chatPageSource,
    /if \(!threadsSettled\) return;\s*if \(!model1ThreadId\) markInitialHistoryReady\("model1"\);\s*if \(!model2ThreadId\) markInitialHistoryReady\("model2"\);/,
  );
  assert.match(
    chatPageSource,
    /const previewsReady = items\.every[\s\S]*?!dataLoaded \|\|[\s\S]*?!runtimeReady \|\|[\s\S]*?!previewsReady \|\|[\s\S]*?signalReady\(\)/,
  );
  assert.match(
    chatPageSource,
    /dataLoaded=\{currentProjectItemsLoaded && !projectsLoading\}/,
  );
});

test("media pages retire the shell only after gallery and preview hydration", () => {
  assert.doesNotMatch(
    rootRouteSource,
    /<RouteBoundary>\s*<(?:ImagesPage|VideoPage|AudioPage)/,
  );
  assert.match(
    imagesPageSource,
    /Promise\.all\(\[\s*refreshStatus\(\)[\s\S]*?await loadGallery\(\)[\s\S]*?await ensureSrc\(initialSelection\)[\s\S]*?onInitialReady\?\.\(\)/,
  );
  assert.match(
    videoPageSource,
    /Promise\.all\(\[\s*refreshStatus\(\)[\s\S]*?await loadGallery\(\)[\s\S]*?await ensureSrc\(initialSelection\)[\s\S]*?onInitialReady\?\.\(\)/,
  );
  assert.match(
    audioPageSource,
    /Promise\.all\(\[\s*refreshGallery\(\),\s*refreshStatus\(\),\s*refreshSttStatus\(\)[\s\S]*?await ensureClipSrc\(initialSelection\)[\s\S]*?onInitialReady\?\.\(\)/,
  );
  assert.match(
    imagesPageSource,
    /if \(initialReadySent\.current\) \{\s*void refreshStatus\(\)/,
  );
  assert.match(
    videoPageSource,
    /if \(initialReadySent\.current\) \{\s*void refreshStatus\(\)/,
  );
  assert.match(
    audioPageSource,
    // audio-page-policy.test.ts pins the argument list: reactivation asks for the loaded window.
    /if \(initialReadySent\.current\) \{\s*void refreshStatus\(\);\s*void refreshSttStatus\(\);\s*void refreshGallery\(/,
  );
  assert.match(
    audioPageSource,
    /mode === "transcribe"[\s\S]*?data-reload-snapshot-sensitive=\{\s*transcript \|\| transcribedName \? "" : undefined/,
  );
});

test("data-backed routes own reload readiness until hydration settles", () => {
  assert.match(
    rootRouteSource,
    /const routeOwnsReloadReadiness =\s*pathname === "\/hub" \|\|\s*pathname === "\/projects"/,
  );
  assert.match(
    rootRouteSource,
    /readyWhenCommitted=\{!routeOwnsReloadReadiness\}/,
  );
  assert.match(
    hubPageSource,
    /!initialResidentStatusSettled[\s\S]*?\(isDiscoverTab \? isLoading : !inventorySettled\)[\s\S]*?signalReady\(\)/,
  );
  assert.match(
    projectsPageSource,
    /if \(!hasLoaded \|\| reloadReadySent\.current\) \{\s*return;\s*\}[\s\S]*?signalReady\(\)/,
  );
  assert.match(
    rootRouteSource,
    /pathname === "\/data-recipes" \|\|\s*pathname\.startsWith\("\/data-recipes\/"\)/,
  );
  assert.match(
    dataRecipesPageSource,
    /if \(!ready \|\| reloadReadySent\.current\) \{\s*return;\s*\}[\s\S]*?signalReady\(\)/,
  );
  assert.match(
    editRecipePageSource,
    /if \(loadState\.status === "loading" \|\| reloadReadySent\.current\) \{\s*return;\s*\}[\s\S]*?signalReady\(\)/,
  );
  assert.match(rootRouteSource, /pathname === "\/export"/);
  assert.match(
    exportPageSource,
    /loadingCheckpoints \|\|[\s\S]*?isLoadingLocalModels \|\|[\s\S]*?signalReady\(\)/,
  );
  assert.match(rootRouteSource, /pathname === "\/studio"/);
  assert.match(
    studioPageSource,
    /capabilitiesUnknown \|\|[\s\S]*?!hasHydratedRuntime \|\|[\s\S]*?isHydratingRuntime \|\|[\s\S]*?signalReady\(\)/,
  );
  assert.match(rootRouteSource, /pathname === "\/api-monitor"/);
  assert.match(
    apiMonitorPageSource,
    /if \(loading \|\| reloadReadySent\.current\) \{\s*return;\s*\}[\s\S]*?signalReady\(\)/,
  );
  assert.match(rootRouteSource, /pathname === "\/login"/);
  assert.match(rootRouteSource, /pathname === "\/change-password"/);
  assert.match(
    authFormSource,
    /if \(statusLoading \|\| reloadReadySent\.current\) return;[\s\S]*?signalReady\(\)/,
  );
});

test("captures Vite's injected development stylesheet", () => {
  const css = ".reload-snapshot-shell { color: rgb(1 2 3); }";
  const outgoing = createEnvironment({
    navigationType: "navigate",
    rootHtml: "<main>Development shell</main>",
    inlineStyleSheets: [css],
  });
  outgoing.dispatch("pageswap", {
    activation: { navigationType: "reload" },
  });

  const snapshot = storedSnapshot(outgoing.storage);
  assert.deepEqual(snapshot.styles, []);
  assert.deepEqual(snapshot.inlineStyles, [css]);

  const incoming = createEnvironment({
    navigationType: "reload",
    storage: outgoing.storage,
  });
  assert.deepEqual(incoming.shell?.inlineStyles, [css]);
});

test("positions the retained host before development CSS arrives", () => {
  const outgoing = createEnvironment({
    navigationType: "navigate",
    rootHtml: "<main>Development shell</main>",
    inlineStyleSheets: [".reload-snapshot-shell { display: block; }"],
  });
  outgoing.dispatch("pageswap", {
    activation: { navigationType: "reload" },
  });

  const incoming = createEnvironment({
    navigationType: "reload",
    storage: outgoing.storage,
  });
  assert.equal(incoming.shell?.hostStyle.position, "fixed");
  assert.equal(incoming.shell?.hostStyle.inset, "0");
  assert.equal(incoming.shell?.hostStyle.zIndex, "2147483647");
  assert.equal(incoming.shell?.hostStyle.pointerEvents, "none");
  assert.equal(incoming.shell?.hostStyle.background, "var(--background)");
});

test("restores scroll offsets again after linked styles load", () => {
  const outgoing = createEnvironment({
    navigationType: "navigate",
    rootHtml: "<main>Scrolled shell</main>",
    styleSheets: ["/assets/index-abc123.css"],
  });
  outgoing.dispatch("pageswap", {
    activation: { navigationType: "reload" },
  });

  const incoming = createEnvironment({
    navigationType: "reload",
    storage: outgoing.storage,
    trackScrollRestores: true,
  });
  assert.equal(incoming.scrollRestoreCalls, 1);
  incoming.runAnimationFrame();
  assert.equal(incoming.scrollRestoreCalls, 2);
  incoming.loadStyleSheets();
  assert.equal(incoming.scrollRestoreCalls, 3);
  incoming.loadStyleSheets();
  assert.equal(incoming.scrollRestoreCalls, 3);
});

test("keeps what a display:contents wrapper renders, drops what is offscreen", () => {
  // /studio wraps its page in a display: contents box, whose rect is empty.
  const environment = createEnvironment({
    navigationType: "navigate",
    rootTree: {
      tag: "div",
      rect: [0, 1440, 900, 0],
      children: [
        {
          tag: "div",
          display: "contents",
          children: [
            { tag: "main", rect: [0, 1440, 900, 0], text: "Unsloth is ready" },
          ],
        },
        { tag: "aside", rect: [-400, 1440, -100, 0], text: "Scrolled past" },
        { tag: "footer", display: "none", text: "Collapsed" },
      ],
    },
  });
  environment.dispatch("pageswap", {
    activation: { navigationType: "reload" },
  });

  const { html } = storedSnapshot(environment.storage);
  assert.match(html, /Unsloth is ready/);
  assert.doesNotMatch(html, /Scrolled past/);
  assert.doesNotMatch(html, /Collapsed/);
});

test("carries the appearance customization so the shell paints in its own colors", () => {
  // theme-boot.js resolves mode and palette only; the rest lands in a later React effect.
  const outgoing = createEnvironment({
    navigationType: "navigate",
    rootHtml: "<main>Existing chat</main>",
    styleSheets: ["/assets/index-abc123.css"],
    htmlVariables: {
      "--background": "#241b2f",
      "--font-sans": '"Inter"',
      "--studio-sidebar-live-width": "317px",
    },
    htmlAttributes: {
      "data-contrast-adjust": "",
      "data-panel-resizing": "true",
    },
  });
  outgoing.dispatch("pageswap", {
    activation: { navigationType: "reload" },
  });

  const { appearance } = storedSnapshot(outgoing.storage);
  assert.deepEqual(appearance?.variables, {
    "--background": "#241b2f",
    "--font-sans": '"Inter"',
  });
  assert.deepEqual(appearance?.attributes, { "data-contrast-adjust": "" });

  const incoming = createEnvironment({
    navigationType: "reload",
    storage: outgoing.storage,
  });
  assert.equal(
    incoming.htmlVariables.getPropertyValue("--background"),
    "#241b2f",
  );
  assert.equal(incoming.htmlAttributes.get("data-contrast-adjust"), "");
  // Transient state (a panel mid-drag) is not appearance and must not be replayed.
  assert.equal(incoming.htmlAttributes.has("data-panel-resizing"), false);
  assert.equal(
    incoming.htmlVariables.getPropertyValue("--studio-sidebar-live-width"),
    "",
  );
});

test("loads selected imported fonts before revealing the shell", async () => {
  const persistedAppearance = new Map([
    [
      "unsloth_appearance_customization",
      JSON.stringify({
        state: {
          customization: {
            uiFont: "Studio Sans",
            headingFont: "Bundled Heading",
            chatFont: null,
            codeFont: null,
            importedFonts: [
              {
                name: "Studio Sans",
                dataUrl: "data:font/woff2;base64,QUJD",
              },
              {
                name: "Unused Font",
                dataUrl: "data:font/woff2;base64,REVG",
              },
            ],
          },
        },
        version: 7,
      }),
    ],
  ]);
  const outgoing = createEnvironment({
    navigationType: "navigate",
    rootHtml: "<main>Existing chat</main>",
    styleSheets: ["/assets/index-abc123.css"],
    localStorage: persistedAppearance,
  });
  outgoing.dispatch("pageswap", {
    activation: { navigationType: "reload" },
  });

  const incoming = createEnvironment({
    navigationType: "reload",
    storage: outgoing.storage,
    localStorage: persistedAppearance,
  });
  assert.equal(incoming.shell?.rootVisibility, "hidden");
  assert.deepEqual(
    incoming.fontFaces.map((face) => ({
      family: face.family,
      source: face.source,
    })),
    [
      {
        family: "Studio Sans",
        source: "url(data:font/woff2;base64,QUJD)",
      },
    ],
  );
  await new Promise<void>((resolve) => setImmediate(resolve));
  assert.equal(incoming.shell?.rootVisibility, "visible");
});

test("drops the retained shell's animations instead of pausing them", () => {
  // A paused animation holds its first keyframe, so fade-in entrances would stay invisible.
  const start = indexCss.indexOf(".reload-snapshot-shell *,");
  assert.ok(start > 0);
  const rule = indexCss.slice(start, indexCss.indexOf("}", start));
  assert.doesNotMatch(rule, /animation-play-state:\s*paused/);
  assert.match(rule, /animation:\s*none\s*!important/);
});

test("leaves the retained shell click-through", () => {
  // A pointer-capturing overlay would swallow clicks until the five-second fail-open.
  const start = indexCss.indexOf(".reload-snapshot {");
  assert.ok(start > 0);
  const rule = indexCss.slice(start, indexCss.indexOf("}", start));
  assert.match(rule, /pointer-events:\s*none/);
});

test("builds the retained shell inside a closed shadow tree", () => {
  // Left in the page tree, the copy makes aria-label queries ambiguous.
  const outgoing = createEnvironment({
    navigationType: "navigate",
    rootHtml: "<main>Existing chat</main>",
    styleSheets: ["/assets/index-abc123.css", "/assets/chat-def456.css"],
  });
  outgoing.dispatch("pageswap", {
    activation: { navigationType: "reload" },
  });

  const stored = storedSnapshot(outgoing.storage);
  assert.deepEqual(stored.styles, [
    "/assets/index-abc123.css",
    "/assets/chat-def456.css",
  ]);

  const incoming = createEnvironment({
    navigationType: "reload",
    storage: outgoing.storage,
  });
  assert.equal(incoming.shell?.mode, "closed");
  assert.equal(
    incoming.shell?.html,
    '<div id="root"><main>Existing chat</main></div>',
  );
  // Selectors do not cross the boundary, so the copy needs its own styles.
  assert.deepEqual(incoming.shell?.stylesheets, [
    "/assets/index-abc123.css",
    "/assets/chat-def456.css",
  ]);
  assert.match(incoming.shell?.rootClass ?? "", /^reload-snapshot-shell /);
});

test("roots the copy in an html element so html-anchored rules still match", () => {
  // Many app rules anchor on `html`, which selectors cannot reach across the shadow boundary.
  const outgoing = createEnvironment({
    navigationType: "navigate",
    rootHtml: "<main>Existing chat</main>",
    styleSheets: ["/assets/index-abc123.css"],
    htmlAttributes: { "data-palette": "matcha", "data-contrast-adjust": "" },
  });
  outgoing.dispatch("pageswap", {
    activation: { navigationType: "reload" },
  });

  const incoming = createEnvironment({
    navigationType: "reload",
    storage: outgoing.storage,
  });
  assert.equal(incoming.shell?.rootTag, "HTML");
  assert.equal(incoming.shell?.bodyTag, "BODY");
  assert.deepEqual(incoming.shell?.rootAttributes, {
    "data-palette": "matcha",
    "data-contrast-adjust": "",
  });
});

test("gives the copy's root the live root's viewport gates, so width-gated rules match the same", () => {
  const outgoing = createEnvironment({
    navigationType: "navigate",
    rootHtml: "<main>Existing chat</main>",
    styleSheets: ["/assets/index-abc123.css"],
    htmlAttributes: { "data-palette": "matcha" },
  });
  outgoing.dispatch("pageswap", {
    activation: { navigationType: "reload" },
  });

  const incoming = createEnvironment({
    navigationType: "reload",
    storage: outgoing.storage,
    htmlAttributes: {
      lang: "en",
      "data-mq-min-width-40rem": "",
      "data-mq-min-width-48rem": "",
    },
  });
  assert.deepEqual(incoming.shell?.rootAttributes, {
    "data-palette": "matcha",
    "data-mq-min-width-40rem": "",
    "data-mq-min-width-48rem": "",
  });
  incoming.changeRootAttribute("data-mq-min-width-48rem", null);
  incoming.changeRootAttribute("data-mq-min-width-64rem", "");
  incoming.changeRootAttribute("data-palette", "changed");
  assert.deepEqual(incoming.shell?.rootAttributes, {
    "data-palette": "matcha",
    "data-mq-min-width-40rem": "",
    "data-mq-min-width-64rem": "",
  });
  assert.equal(incoming.observingAttributes, true);
  incoming.dispatch("unsloth:app-shell-ready");
  incoming.runAnimationFrame();
  incoming.runAnimationFrame();
  assert.equal(incoming.observingAttributes, false);
  incoming.changeRootAttribute("data-mq-min-width-40rem", null);
  assert.equal(incoming.shell?.rootAttributes["data-mq-min-width-40rem"], "");
});

test("freezes the design tokens onto the copy's own root", () => {
  // `:root` tokens never reach a shadow tree, so the copy carries the resolved set.
  const outgoing = createEnvironment({
    navigationType: "navigate",
    rootHtml: "<main>Existing chat</main>",
    styleSheets: ["/assets/index-abc123.css"],
    htmlVariables: { "--font-heading": '"Hellix"', "--tracking-normal": "0em" },
  });
  outgoing.dispatch("pageswap", {
    activation: { navigationType: "reload" },
  });
  assert.deepEqual(storedSnapshot(outgoing.storage).tokens, {
    "--font-heading": '"Hellix"',
    "--tracking-normal": "0em",
  });

  const incoming = createEnvironment({
    navigationType: "reload",
    storage: outgoing.storage,
  });
  assert.equal(incoming.shell?.rootTokens["--font-heading"], '"Hellix"');
  assert.equal(incoming.shell?.rootTokens["--tracking-normal"], "0em");
});

test("carries live form state, except what sensitive fields hide", () => {
  // cloneNode copies attributes, not React's value/checked properties; secrets must stay behind.
  const environment = createEnvironment({
    navigationType: "navigate",
    styleSheets: ["/assets/index-abc123.css"],
    rootTree: {
      tag: "div",
      rect: [0, 1440, 900, 0],
      children: [
        {
          tag: "textarea",
          rect: [0, 1440, 200, 0],
          value: "half-written prompt",
        },
        {
          tag: "input",
          rect: [0, 1440, 240, 0],
          type: "search",
          value: "gemma",
        },
        {
          tag: "input",
          rect: [0, 1440, 280, 0],
          type: "checkbox",
          checked: true,
        },
        {
          tag: "input",
          rect: [0, 1440, 320, 0],
          type: "password",
          value: "hunter2",
          attributes: { value: "hunter2" },
        },
        {
          tag: "input",
          rect: [0, 1440, 360, 0],
          type: "text",
          autocomplete: "current-password",
          value: "revealed-password",
          attributes: { value: "revealed-password" },
        },
        {
          tag: "input",
          rect: [0, 1440, 400, 0],
          type: "file",
          value: "C:\\fakepath\\private-dataset.jsonl",
          attributes: { value: "C:\\fakepath\\private-dataset.jsonl" },
        },
        {
          tag: "input",
          rect: [0, 1440, 440, 0],
          type: "text",
          value: "revealed-api-key",
          attributes: {
            value: "revealed-api-key",
            "data-reload-snapshot-sensitive": "",
          },
        },
        {
          tag: "textarea",
          rect: [0, 1440, 480, 0],
          value: "Authorization: Bearer secret-header",
          text: "Authorization: Bearer stale-header",
          attributes: { "data-reload-snapshot-sensitive": "" },
        },
        {
          tag: "code",
          rect: [0, 1440, 520, 0],
          text: "Authorization: Bearer rendered-api-key",
          attributes: { "data-reload-snapshot-sensitive": "" },
        },
        {
          // The value also appears in the tooltip and accessible name, so clearing text is not enough.
          tag: "span",
          rect: [0, 1440, 560, 0],
          text: "rendered-local-filename.csv",
          attributes: {
            "data-reload-snapshot-sensitive": "",
            title: "rendered-local-filename.csv",
            "aria-label": "Remove rendered-local-filename.csv",
            placeholder: "rendered-local-filename.csv",
          },
        },
        {
          tag: "div",
          rect: [0, 1440, 600, 0],
          attributes: { "data-reload-snapshot-sensitive": "" },
          children: [
            {
              tag: "img",
              rect: [530, 100, 590, 40],
              naturalWidth: 1024,
              naturalHeight: 768,
              attributes: {
                src: "blob:https://studio.test/pending-local-attachment",
                alt: "pending-local-attachment",
              },
            },
          ],
        },
        {
          tag: "div",
          rect: [0, 1440, 700, 0],
          attributes: { "data-reload-snapshot-sensitive": "" },
          children: [
            {
              tag: "img",
              rect: [630, 100, 690, 40],
              naturalWidth: 1024,
              naturalHeight: 768,
              attributes: {
                src: "data:image/png;base64,private-local-picker",
                alt: "Source",
              },
            },
          ],
        },
      ],
    },
  });
  environment.dispatch("pageswap", {
    activation: { navigationType: "reload" },
  });

  const { html } = storedSnapshot(environment.storage);
  assert.match(html, /half-written prompt/);
  assert.match(html, /value="gemma"/);
  assert.match(html, /checked=""/);
  assert.doesNotMatch(html, /hunter2/);
  assert.doesNotMatch(html, /revealed-password/);
  assert.doesNotMatch(html, /fakepath|private-dataset/);
  assert.doesNotMatch(html, /revealed-api-key/);
  assert.doesNotMatch(html, /secret-header|stale-header/);
  assert.doesNotMatch(html, /rendered-api-key/);
  assert.doesNotMatch(
    html,
    /pending-local-attachment|private-local-picker|retained-frame/,
  );
  assert.doesNotMatch(html, /rendered-local-filename/);
  assert.match(
    sharedComposerSource,
    /function PendingImageThumb[\s\S]*?data-reload-snapshot-sensitive/,
  );
  assert.match(
    imageDropzoneSource,
    /if \(value\)[\s\S]*?data-reload-snapshot-sensitive/,
  );
  // Picked file names render outside the input in portaled dialogs.
  assert.match(
    referencePickerSource,
    /if \(value\)[\s\S]*?data-reload-snapshot-sensitive[\s\S]*?value\.name/,
  );
  assert.match(
    projectsPageSource,
    /data-reload-snapshot-sensitive[\s\S]*?importFile\?\.name/,
  );
  assert.match(
    unstructuredDropZoneSource,
    /data-reload-snapshot-sensitive[\s\S]*?entry\.name/,
  );
  assert.match(
    seedDialogSource,
    /data-reload-snapshot-sensitive[\s\S]*?localFile\?\.name/,
  );
  assert.match(
    apiMonitorPageSource,
    /function PayloadBlock[\s\S]*?data-reload-snapshot-sensitive/,
  );
  assert.match(
    apiMonitorPageSource,
    /data-reload-snapshot-sensitive[\s\S]*?\{entry\.error\}/,
  );
  assert.match(
    apiMonitorPageSource,
    /data-reload-snapshot-sensitive[\s\S]*?\{preview\}/,
  );
  assert.match(
    sharedComposerSource,
    /pendingAudio\.map\([\s\S]*?data-reload-snapshot-sensitive[\s\S]*?\{clip\.name\}/,
  );
  assert.match(
    projectSourceDropzoneSource,
    /data-reload-snapshot-sensitive[\s\S]*?title=\{entry\.name\}/,
  );
  assert.match(
    projectSourceDropzoneSource,
    /data-reload-snapshot-sensitive\s*\n\s*aria-label=\{`Remove \$\{entry\.name\}`\}/,
  );
  assert.match(
    attachmentSource,
    /export const ComposerAttachments[\s\S]*?data-reload-snapshot-sensitive/,
  );
  assert.match(attachmentSource, /<AttachmentPreviewDialog redactFromReload=/);
  assert.match(
    attachmentPreviewSource,
    /AttachmentPreviewDialog[\s\S]*?redactFromReload/,
  );
  for (const dialog of [
    "ImageGalleryDialog",
    "AttachmentTextDialog",
    "AttachmentAudioDialog",
  ]) {
    const body = attachmentPreviewSource.slice(
      attachmentPreviewSource.indexOf(`const ${dialog}: FC`),
    );
    assert.match(
      body,
      /^[\s\S]*?<AttachmentViewer[\s\S]*?redactFromReload=\{redactFromReload\}/,
      `${dialog} must redact its portaled content`,
    );
  }
  const viewer = readSrc("components/assistant-ui/attachment-document-dialog.tsx");
  assert.match(
    viewer,
    /export const AttachmentViewer[\s\S]*?<MediaViewer[\s\S]*?redactFromReload=\{redactFromReload\}/,
  );
  assert.match(
    readSrc("components/media-viewer.tsx"),
    /data-reload-snapshot-sensitive=\{redactFromReload \? "" : undefined\}/,
  );
});

test("keeps native select options that paint the closed control label", () => {
  const environment = createEnvironment({
    navigationType: "navigate",
    styleSheets: ["/assets/index-abc123.css"],
    rootTree: {
      tag: "div",
      children: [
        {
          tag: "select",
          rect: [100, 420, 140, 200],
          children: [
            {
              tag: "option",
              rect: [0, 0, 0, 0],
              text: "GPU 0",
            },
            {
              tag: "optgroup",
              rect: [0, 0, 0, 0],
              children: [
                {
                  tag: "option",
                  rect: [0, 0, 0, 0],
                  text: "GPU 1",
                  selected: true,
                },
              ],
            },
          ],
        },
      ],
    },
  });
  environment.dispatch("pageswap", {
    activation: { navigationType: "reload" },
  });

  const { html } = storedSnapshot(environment.storage);
  assert.match(html, /<option>GPU 0<\/option>/);
  assert.match(html, /<optgroup><option selected="">GPU 1<\/option><\/optgroup>/);
});

test("preserves IDs that the isolated copy references internally", () => {
  const environment = createEnvironment({
    navigationType: "navigate",
    styleSheets: ["/assets/index-abc123.css"],
    rootTree: {
      tag: "div",
      children: [
        {
          tag: "svg",
          children: [
            {
              tag: "linearGradient",
              attributes: { id: "history-fill" },
            },
            {
              tag: "path",
              attributes: { fill: "url(#history-fill)" },
            },
          ],
        },
      ],
    },
  });
  environment.dispatch("pageswap", {
    activation: { navigationType: "reload" },
  });

  const { html } = storedSnapshot(environment.storage);
  assert.match(html, /id="history-fill"/);
  assert.match(html, /fill="url\(#history-fill\)"/);
});

test("carries visible body portals along with the app root", () => {
  const environment = createEnvironment({
    navigationType: "navigate",
    styleSheets: ["/assets/index-abc123.css"],
    rootTree: {
      tag: "div",
      children: [{ tag: "main", text: "Settings page" }],
    },
    bodyPortals: [
      {
        tag: "div",
        rect: [100, 1100, 700, 300],
        attributes: { role: "dialog" },
        text: "Confirm deletion",
      },
    ],
  });
  environment.dispatch("pageswap", {
    activation: { navigationType: "reload" },
  });

  const { html } = storedSnapshot(environment.storage);
  assert.match(html, /Settings page/);
  assert.match(html, /role="dialog"/);
  assert.match(html, /Confirm deletion/);
});

test("keeps scroll-container geometry with a bounded visible slice", () => {
  const environment = createEnvironment({
    navigationType: "navigate",
    styleSheets: ["/assets/index-abc123.css"],
    rootTree: {
      tag: "div",
      children: [
        {
          tag: "section",
          overflowY: "auto",
          clientHeight: 300,
          scrollHeight: 1200,
          scrollTop: 600,
          children: [
            {
              tag: "p",
              rect: [-500, 900, -450, 100],
              text: "Earlier message keeps the scroll geometry",
            },
            {
              tag: "p",
              rect: [300, 900, 350, 100],
              text: "Visible message",
            },
          ],
        },
      ],
    },
  });
  environment.dispatch("pageswap", {
    activation: { navigationType: "reload" },
  });

  const { html } = storedSnapshot(environment.storage);
  assert.match(html, /data-reload-scroll-top="600"/);
  assert.doesNotMatch(html, /Earlier message keeps the scroll geometry/);
  assert.match(html, /data-reload-spacer/);
  assert.match(html, /Visible message/);
});

test("coalesces a transcript larger than the snapshot cap", () => {
  const earlierMessages = Array.from({ length: 1100 }, (_, index) => ({
    tag: "article",
    rect: [-5000 + index * 4, 900, -4996 + index * 4, 100] as [
      number,
      number,
      number,
      number,
    ],
    text: "Earlier message " + "x".repeat(4096),
  }));
  const environment = createEnvironment({
    navigationType: "navigate",
    styleSheets: ["/assets/index-abc123.css"],
    rootTree: {
      tag: "div",
      children: [
        {
          tag: "section",
          overflowY: "auto",
          clientHeight: 300,
          scrollHeight: 6000,
          scrollTop: 5000,
          children: [
            ...earlierMessages,
            {
              tag: "article",
              rect: [300, 900, 350, 100],
              text: "Visible message survives",
            },
          ],
        },
      ],
    },
  });
  environment.dispatch("pageswap", {
    activation: { navigationType: "reload" },
  });

  const { html } = storedSnapshot(environment.storage);
  assert.match(html, /Visible message survives/);
  assert.equal((html.match(/data-reload-spacer/g) ?? []).length, 1);
  assert.ok(html.length < 3 * 1024 * 1024);
});

test("materializes visible ephemeral media and drops protected URLs", () => {
  const environment = createEnvironment({
    navigationType: "navigate",
    styleSheets: ["/assets/index-abc123.css"],
    rootTree: {
      tag: "div",
      children: [
        {
          tag: "img",
          rect: [100, 900, 700, 100],
          naturalWidth: 1024,
          naturalHeight: 768,
          attributes: { src: "blob:https://studio.test/generated-image" },
        },
        {
          tag: "audio",
          rect: [720, 900, 780, 100],
          attributes: {
            controls: "",
            src: "blob:https://studio.test/generated-audio",
          },
        },
        {
          tag: "video",
          rect: [100, 1300, 700, 950],
          videoWidth: 1280,
          videoHeight: 720,
          attributes: {
            src:
              "/api/inference/video/gallery/clip/file-signed?token=clip.123.secret",
          },
        },
      ],
    },
  });
  environment.dispatch("pageswap", {
    activation: { navigationType: "reload" },
  });

  const { html } = storedSnapshot(environment.storage);
  assert.match(html, /src="data:image\/webp;base64,retained-frame"/);
  assert.match(html, /<audio controls="">/);
  assert.match(html, /<video poster="data:image\/webp;base64,retained-frame">/);
  assert.doesNotMatch(html, /blob:/);
  assert.doesNotMatch(html, /clip\.123\.secret|[?&]token=/);
});

test("keeps trusted chart CSS while stripping every other style", () => {
  const environment = createEnvironment({
    navigationType: "navigate",
    styleSheets: ["/assets/index-abc123.css"],
    rootTree: {
      tag: "div",
      children: [
        {
          tag: "style",
          rect: [0, 0, 0, 0],
          attributes: { "data-reload-snapshot-style": "" },
          text: "[data-chart=loss] { --color-displayLoss: #16a34a; }",
        },
        {
          tag: "style",
          rect: [0, 0, 0, 0],
          text: ".untrusted { display: block; }",
        },
        {
          tag: "svg",
          children: [{ tag: "path", attributes: { "data-chart": "loss" } }],
        },
      ],
    },
  });
  environment.dispatch("pageswap", {
    activation: { navigationType: "reload" },
  });

  const { html } = storedSnapshot(environment.storage);
  assert.match(html, /--color-displayLoss: #16a34a/);
  assert.doesNotMatch(html, /\.untrusted/);
});

test("keeps the shell when materialized media alone passes the cap", () => {
  // Pixels cost 4x on 2x displays; drop the pixels but keep the layout instead of the snapshot.
  const tiles = Array.from({ length: 6 }, (_, index) => ({
    tag: "img",
    rect: [10 + index, 400, 200 + index, 200] as [number, number, number, number],
    naturalWidth: 1400,
    naturalHeight: 1000,
    attributes: { src: "blob:https://studio/tile-" + index },
  }));
  const environment = createEnvironment({
    navigationType: "navigate",
    styleSheets: ["/assets/index-abc123.css"],
    frameDataBody: "A".repeat(600 * 1024),
    rootTree: {
      tag: "div",
      children: [
        { tag: "h1", rect: [10, 60, 400, 20], text: "Images" },
        ...tiles,
      ],
    },
  });
  environment.dispatch("pageswap", {
    activation: { navigationType: "reload" },
  });

  const { html } = storedSnapshot(environment.storage);
  assert.match(html, /Images/, "the shell must survive the overflow");
  assert.ok(html.length < 3 * 1024 * 1024);
  assert.equal(
    (html.match(/data:image/g) ?? []).length,
    0,
    "the pixels that caused the overflow must be the thing dropped",
  );
});

test("materializes visible canvas pixels into the retained shell", () => {
  const environment = createEnvironment({
    navigationType: "navigate",
    styleSheets: ["/assets/index-abc123.css"],
    rootTree: {
      tag: "div",
      children: [
        {
          tag: "canvas",
          rect: [100, 900, 700, 100],
          width: 1024,
          height: 768,
          attributes: { class: "absolute inset-0", style: "opacity:0.8" },
        },
      ],
    },
  });
  environment.dispatch("pageswap", {
    activation: { navigationType: "reload" },
  });

  const { html } = storedSnapshot(environment.storage);
  assert.match(
    html,
    /style="opacity:0\.8;background-image:url\(data:image\/webp;base64,retained-frame\);background-size:100% 100%;background-repeat:no-repeat;"/,
  );
});

test("skips the shell when the snapshot carries no stylesheets", () => {
  const outgoing = createEnvironment({
    navigationType: "navigate",
    rootHtml: "<main>Existing chat</main>",
  });
  outgoing.dispatch("pageswap", {
    activation: { navigationType: "reload" },
  });
  assert.deepEqual(storedSnapshot(outgoing.storage).styles, []);

  const incoming = createEnvironment({
    navigationType: "reload",
    storage: outgoing.storage,
  });
  assert.equal(incoming.appended.length, 0);
});