Skip to content

The open-source full-stack studio for Opus 5.5 motion video. Reverse engineering for any video. Code video with HTML: recreate, edit and render. BYOA + BYOK · Motion.md · Images, Video, Music & Voice.

5.7 KB
Raw Blame
// @vitest-environment happy-dom

import React, { act } from "react";
import { I18nProvider, translate } from "../i18n/I18n";
import { StudioHostContext, useStudioHost } from "../motion/StudioHost";
import { createRoot } from "react-dom/client";
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";

vi.mock('../motion/diagnostics', () => ({ recordBrowserDiagnostic: vi.fn(), browserDiagnostics: () => [], nextDiagnosticRequest: () => 'fixture-request', diagnosticsActive: () => false }));

vi.mock("../utils/studioTelemetry", () => ({ trackStudioEvent: vi.fn() }));
vi.mock("../telemetry/policy", () => ({ browserTelemetryAllowed: () => true }));
vi.mock("../telemetry/events", () => ({
  trackStudioFeedback: vi.fn(),
  trackStudioFeedbackShown: vi.fn(),
  trackStudioFeedbackDismissed: vi.fn(),
  trackStudioFeedbackInterviewClick: vi.fn(),
}));

const { StudioErrorBoundary } = await import("./StudioErrorBoundary");
const { trackStudioFeedbackShown } = await import("../telemetry/events");

function Boom(): React.ReactElement {
  throw new Error("timeline exploded");
}

let container: HTMLDivElement;
let root: ReturnType<typeof createRoot>;
let consoleError: ReturnType<typeof vi.spyOn>;

beforeEach(() => {
  localStorage.clear();
  sessionStorage.clear();
  vi.clearAllMocks();
  container = document.createElement("div");
  document.body.appendChild(container);
  root = createRoot(container);
  // React logs the caught error itself; the boundary is the thing under test.
  consoleError = vi.spyOn(console, "error").mockImplementation(() => {});
});

afterEach(() => {
  act(() => root.unmount());
  container.remove();
  consoleError.mockRestore();
});

const renderCrashed = () =>
  act(() => {
    root.render(
      <StudioErrorBoundary>
        <Boom />
      </StudioErrorBoundary>,
    );
  });

describe("StudioErrorBoundary", () => {
  it("shows the crash screen with the error message and both recovery paths", () => {
    renderCrashed();
    expect(container.textContent).toContain("Something went wrong");
    expect(container.textContent).not.toContain("timeline exploded");
    act(() => container.querySelector<HTMLButtonElement>('[aria-label="Error details"]')!.click());
    expect(container.querySelector("pre")?.textContent).toBe("timeline exploded");
    expect(container.textContent).toContain("Try again");
    expect(container.textContent).toContain("Reload Studio");
  });

  it("asks what the user was doing, since the stack trace cannot say", () => {
    renderCrashed();
    const card = container.querySelector('[aria-label="Send feedback to the HyperFrames team"]');
    expect(card).not.toBeNull();
    expect(card?.textContent).toContain("Studio crashed. What were you doing?");
  });

  it("offers one-tap answers instead of a 0-10 score", () => {
    renderCrashed();
    const card = container.querySelector('[aria-label="Send feedback to the HyperFrames team"]');
    const chips = [...(card?.querySelectorAll("button") ?? [])]
      .map((b) => b.textContent?.trim())
      .filter((t) => t && t !== "Send");
    expect(chips).toContain("Editing the timeline");
    expect(chips).toContain("Just opened it");
    // Rating a crash is a question with no useful answer.
    expect(card?.querySelector('input[name="hf-studio-feedback-rating"]')).toBeNull();
  });

  it("reports the crash prompt to telemetry so the funnel is visible", () => {
    renderCrashed();
    expect(trackStudioFeedbackShown).toHaveBeenCalledWith(
      expect.objectContaining({ reason: "crash" }),
    );
  });

  it("stays quiet when the user already gave feedback recently", () => {
    localStorage.setItem("hyperframes-studio:feedbackAnsweredAt", String(Date.now()));
    renderCrashed();
    // The crash screen itself still works; only the ask is suppressed.
    expect(container.textContent).toContain("Something went wrong");
    expect(
      container.querySelector('[aria-label="Send feedback to the HyperFrames team"]'),
    ).toBeNull();
  });
});

it("localizes crash recovery inside the product host and respects its feedback setting", () => {
  function Product() {
    const host = useStudioHost();
    return <I18nProvider initialLocale="ja"><StudioHostContext.Provider value={{ ...host, feedback: false }}><StudioErrorBoundary><Boom /></StudioErrorBoundary></StudioHostContext.Provider></I18nProvider>;
  }
  act(() => root.render(<Product />));
  expect(container.textContent).toContain(translate('ja', 'Something went wrong'));
  expect(container.textContent).toContain(translate('ja', 'Reload Studio'));
  expect(container.textContent).not.toContain('timeline exploded');
  act(() => container.querySelector<HTMLButtonElement>(`[aria-label="${translate('ja', 'Error details')}"]`)!.click());
  expect(container.querySelector('pre')?.textContent).toBe('timeline exploded');
  expect(container.querySelector('[aria-label="Send feedback to the HyperFrames team"]')).toBeNull();
});

it("localizes Chinese crash prompts, chips and hints without changing answer IDs", () => {
  act(() => root.render(<I18nProvider initialLocale="zh-CN"><StudioErrorBoundary><Boom /></StudioErrorBoundary></I18nProvider>));
  expect(container.textContent).toContain('编辑器崩溃了。你刚才在做什么?');
  expect(container.textContent).toContain('选择一项,或填写其他情况。');
  expect(container.querySelector('input')?.placeholder).toBe(translate('zh-CN', 'The last thing you touched'));
  const chip = [...container.querySelectorAll('button')].find(b => b.textContent?.trim() === '编辑时间轴')!;
  expect(chip.getAttribute('aria-label')).toBe('编辑时间轴. 拖动、裁剪或分割片段');
  act(() => chip.dispatchEvent(new FocusEvent('focusin', { bubbles: true })));
  expect(container.textContent).toContain('拖动、裁剪或分割片段');
});