import { describe, it, expect } from 'vitest';
import { resolvePreviewImageHref } from './filePreviewPath';

// Mirrors the base URL the FilePreview component builds for relative images:
// `/api/local/tasks/:id/files/raw?path=`.
const BASE = '/api/local/tasks/123/files/raw?path=';

describe('resolvePreviewImageHref', () => {
  it('rewrites a plain relative path to the raw-file endpoint (encoded)', () => {
    expect(resolvePreviewImageHref('output/chart.png', BASE)).toBe(
      '/api/local/tasks/123/files/raw?path=output%2Fchart.png',
    );
  });

  it('strips a leading "./" before encoding', () => {
    expect(resolvePreviewImageHref('./output/chart.png', BASE)).toBe(
      '/api/local/tasks/123/files/raw?path=output%2Fchart.png',
    );
  });

  it('strips only a single leading "./" (not "../")', () => {
    expect(resolvePreviewImageHref('../sibling/a.png', BASE)).toBe(
      '/api/local/tasks/123/files/raw?path=..%2Fsibling%2Fa.png',
    );
  });

  it('encodes a root-relative "/..." path as a single component (leading slash encoded)', () => {
    expect(resolvePreviewImageHref('/abs/in/workspace.png', BASE)).toBe(
      '/api/local/tasks/123/files/raw?path=%2Fabs%2Fin%2Fworkspace.png',
    );
  });

  it('encodes nested path segments and spaces', () => {
    expect(resolvePreviewImageHref('a/b c/d.png', BASE)).toBe(
      '/api/local/tasks/123/files/raw?path=a%2Fb%20c%2Fd.png',
    );
  });

  it('encodes query/anchor characters present in the relative path (treated as one component)', () => {
    // encodeURIComponent escapes ? and # so they cannot break out of the path param.
    expect(resolvePreviewImageHref('img.png?v=2#frag', BASE)).toBe(
      '/api/local/tasks/123/files/raw?path=img.png%3Fv%3D2%23frag',
    );
  });

  it('leaves an http:// URL untouched', () => {
    expect(resolvePreviewImageHref('http://example.com/x.png', BASE)).toBe(
      'http://example.com/x.png',
    );
  });

  it('leaves an https:// URL untouched', () => {
    expect(resolvePreviewImageHref('https://example.com/x.png', BASE)).toBe(
      'https://example.com/x.png',
    );
  });

  it('leaves a data: URI untouched', () => {
    const dataUri = 'data:image/png;base64,iVBORw0KGgo=';
    expect(resolvePreviewImageHref(dataUri, BASE)).toBe(dataUri);
  });

  it('returns empty string for empty href', () => {
    expect(resolvePreviewImageHref('', BASE)).toBe('');
  });

  it('returns empty string for null/undefined href', () => {
    expect(resolvePreviewImageHref(null, BASE)).toBe('');
    expect(resolvePreviewImageHref(undefined, BASE)).toBe('');
  });

  it('returns the href unchanged when no imageBaseUrl is provided', () => {
    // Component only installs the rewrite renderer when imageBaseUrl is truthy;
    // the helper defensively no-ops otherwise.
    expect(resolvePreviewImageHref('output/chart.png', '')).toBe('output/chart.png');
    expect(resolvePreviewImageHref('output/chart.png', null)).toBe('output/chart.png');
    expect(resolvePreviewImageHref('output/chart.png', undefined)).toBe('output/chart.png');
  });
});
