import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { appName, pageTitle } from './branding';

const setGlobal = (g: { appName: string } | undefined) => {
  if (g === undefined) {
    delete (window as { __MINSTREL__?: unknown }).__MINSTREL__;
  } else {
    (window as { __MINSTREL__?: typeof g }).__MINSTREL__ = g;
  }
};

describe('branding helpers', () => {
  beforeEach(() => setGlobal(undefined));
  afterEach(() => setGlobal(undefined));

  it('appName returns the global value when set', () => {
    setGlobal({ appName: 'Family Jukebox' });
    expect(appName()).toBe('Family Jukebox');
  });

  it('appName falls back to "Minstrel" when global is undefined', () => {
    expect(appName()).toBe('Minstrel');
  });

  it('appName falls back to "Minstrel" when value contains an unrendered Go token', () => {
    setGlobal({ appName: '{{ .AppName }}' });
    expect(appName()).toBe('Minstrel');
  });

  it('pageTitle with section returns "{appName} · {section}"', () => {
    setGlobal({ appName: 'Minstrel' });
    expect(pageTitle('Search')).toBe('Minstrel · Search');
  });

  it('pageTitle without section returns just the appName', () => {
    setGlobal({ appName: 'Minstrel' });
    expect(pageTitle()).toBe('Minstrel');
  });

  it('pageTitle composes nested sections via the · separator', () => {
    setGlobal({ appName: 'Minstrel' });
    expect(pageTitle('Library · Artists')).toBe('Minstrel · Library · Artists');
  });
});
