Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

2.8 KB
Raw Blame
/**
 * SpritePreview — the one way to render a sprite asset. Covers the default
 * (plain checkerboarded image, no interactivity) and the opt-in `zoomable`
 * path, which turns the box into a button that opens a SpriteLightbox.
 */

import { describe, it, expect, vi } from 'vitest';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
// The lightbox's best-effort provenance lookup has a separate API contract.
vi.mock('../../services/apiSprites.js', async (importOriginal) => ({
  ...await importOriginal(),
  getSpriteAssetPrompt: vi.fn().mockResolvedValue(null),
}));
import SpritePreview from './SpritePreview.jsx';

describe('SpritePreview', () => {
  it('renders a non-interactive image by default', () => {
    render(<SpritePreview recordId="field-medic" path="reference/main.png" />);
    expect(screen.getByRole('img')).toHaveAttribute('src', '/data/sprites/field-medic/reference/main.png');
    expect(screen.queryByRole('button')).not.toBeInTheDocument();
  });

  it('opens an enlarged lightbox when zoomable and clicked', async () => {
    render(<SpritePreview recordId="field-medic" path="reference/walk-south-v1.png" zoomable />);

    const trigger = screen.getByRole('button', { name: /Enlarge/ });
    expect(screen.queryByRole('button', { name: 'Close preview' })).not.toBeInTheDocument();

    await userEvent.click(trigger);
    expect(screen.getByRole('button', { name: 'Close preview' })).toBeInTheDocument();
    // The lightbox shows the file name and the enlarged image.
    expect(screen.getByText('walk-south-v1.png')).toBeInTheDocument();

    await userEvent.click(screen.getByRole('button', { name: 'Close preview' }));
    expect(screen.queryByRole('button', { name: 'Close preview' })).not.toBeInTheDocument();
  });

  it('opens the lightbox on the SAME asset the thumbnail shows', async () => {
    // The lightbox resolves its own URL from (recordId, path, version) rather
    // than reusing the thumbnail's — so a mis-threaded prop would enlarge a
    // different (or cache-stale) file while the thumbnail still looks right.
    render(
      <SpritePreview recordId="field-medic" path="reference/walk-south-v1.png" version="v7" zoomable />,
    );
    const expectedSrc = '/data/sprites/field-medic/reference/walk-south-v1.png?v=v7';
    expect(screen.getByRole('img')).toHaveAttribute('src', expectedSrc);

    await userEvent.click(screen.getByRole('button', { name: /Enlarge/ }));

    // aria-label carries the record-relative path; both images point at it.
    expect(screen.getByRole('dialog')).toHaveAttribute('aria-label', 'Preview reference/walk-south-v1.png');
    const srcs = screen.getAllByRole('img').map((img) => img.getAttribute('src'));
    expect(srcs).toEqual([expectedSrc, expectedSrc]);
  });
});