import { describe, it, expect } from 'vitest';
import { sharedImageBaseUrl, sharedRawUrlFor } from './sharedView';
import type { LocalFileEntry } from '../api';

const file = (path: string): LocalFileEntry => ({
  name: path.split('/').pop() ?? path,
  path,
  kind: 'file',
  size: 0,
  modifiedAt: '2026-01-01T00:00:00.000Z',
});

describe('sharedImageBaseUrl', () => {
  it('builds the shared raw image base url with trailing path=', () => {
    expect(sharedImageBaseUrl('tok123')).toBe('/api/shared/tok123/files/raw?path=');
  });
});

describe('sharedRawUrlFor', () => {
  it('builds a per-entry shared raw url from the entry path', () => {
    const build = sharedRawUrlFor('tok123');
    expect(build(file('report.md'))).toBe('/api/shared/tok123/files/raw?path=report.md');
  });

  it('url-encodes nested paths and special chars', () => {
    const build = sharedRawUrlFor('tok123');
    expect(build(file('sub dir/a b.png'))).toBe(
      '/api/shared/tok123/files/raw?path=sub+dir%2Fa+b.png',
    );
  });

  it('returns distinct urls for distinct entries', () => {
    const build = sharedRawUrlFor('tok');
    expect(build(file('a.txt'))).not.toBe(build(file('b.txt')));
  });
});
