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

6.1 KB
Raw Blame
import { describe, it, expect } from 'vitest';
import { isHttpsUrl, isUrl, normalizeUrl, tiktokEmbedSrc, tiktokVideoId } from './urlNormalize';

describe('tiktokVideoId', () => {
  it('extracts the id from the share/watch, short, and embed forms', () => {
    expect(tiktokVideoId('https://www.tiktok.com/@some.user/video/1234567890')).toBe('1234567890');
    expect(tiktokVideoId('https://tiktok.com/v/1234567890')).toBe('1234567890');
    expect(tiktokVideoId('https://www.tiktok.com/embed/v2/1234567890')).toBe('1234567890');
    expect(tiktokVideoId('https://www.tiktok.com/player/v1/1234567890')).toBe('1234567890');
  });

  it('anchors the host so look-alikes do not match', () => {
    expect(tiktokVideoId('https://nottiktok.com/@u/video/1234567890')).toBe(null);
    expect(tiktokVideoId('https://example.com/#tiktok.com/@u/video/1234567890')).toBe(null);
  });

  it('returns null for non-TikTok and empty input', () => {
    expect(tiktokVideoId('https://example.com/watch?v=abc')).toBe(null);
    expect(tiktokVideoId('')).toBe(null);
    expect(tiktokVideoId(null)).toBe(null);
  });
});

describe('tiktokEmbedSrc', () => {
  it('builds the Embed Player URL for an id', () => {
    expect(tiktokEmbedSrc('1234567890')).toBe('https://www.tiktok.com/player/v1/1234567890');
  });
});

describe('isUrl', () => {
  it('detects explicit http/https schemes', () => {
    expect(isUrl('http://example.com')).toBe(true);
    expect(isUrl('https://example.com')).toBe(true);
    expect(isUrl('HTTPS://EXAMPLE.COM')).toBe(true);
  });

  it('detects git@ ssh remotes', () => {
    expect(isUrl('[email protected]:owner/repo.git')).toBe(true);
  });

  it('detects bare domain-like tokens', () => {
    expect(isUrl('example.com')).toBe(true);
    expect(isUrl('sub.example.co.uk/path')).toBe(true);
  });

  it('rejects free text and whitespace-only input', () => {
    expect(isUrl('just a thought')).toBe(false);
    expect(isUrl('hello')).toBe(false);
    expect(isUrl('   ')).toBe(false);
    expect(isUrl('')).toBe(false);
  });

  it('rejects domain-like tokens that contain whitespace', () => {
    // DOMAIN_PATTERN requires a single non-space token
    expect(isUrl('foo. bar')).toBe(false);
  });

  it('trims before detecting', () => {
    expect(isUrl('  example.com  ')).toBe(true);
  });

  it('tolerates null/undefined', () => {
    expect(isUrl(null)).toBe(false);
    expect(isUrl(undefined)).toBe(false);
  });
});

describe('isHttpsUrl', () => {
  it('accepts valid HTTPS browser handoffs and rejects unsafe schemes', () => {
    expect(isHttpsUrl('https://auth.example.com/sign-in')).toBe(true);
    expect(isHttpsUrl('http://auth.example.com/sign-in')).toBe(false);
    expect(isHttpsUrl('javascript:alert(1)')).toBe(false);
    expect(isHttpsUrl('not a URL')).toBe(false);
  });
});

describe('normalizeUrl', () => {
  it('returns null for empty / whitespace-only input', () => {
    expect(normalizeUrl('')).toBe(null);
    expect(normalizeUrl('   ')).toBe(null);
    expect(normalizeUrl(null)).toBe(null);
    expect(normalizeUrl(undefined)).toBe(null);
  });

  it('leaves existing http/https schemes untouched', () => {
    expect(normalizeUrl('http://example.com')).toBe('http://example.com');
    expect(normalizeUrl('https://example.com')).toBe('https://example.com');
  });

  it('trims surrounding whitespace', () => {
    expect(normalizeUrl('  https://example.com  ')).toBe('https://example.com');
  });

  // --- allowGit option (LinksTab + QuickBrainCapture behavior) ---
  describe('allowGit: true (default)', () => {
    it('treats git@ as already-normalized', () => {
      expect(normalizeUrl('[email protected]:owner/repo.git', { allowGit: true }))
        .toBe('[email protected]:owner/repo.git');
    });

    it('prepends https to a bare domain', () => {
      expect(normalizeUrl('example.com', { allowGit: true })).toBe('https://example.com');
    });

    it('defaults allowGit to true', () => {
      expect(normalizeUrl('[email protected]:owner/repo.git'))
        .toBe('[email protected]:owner/repo.git');
    });
  });

  // --- FeedsTab behavior: no git@ handling ---
  describe('allowGit: false (FeedsTab)', () => {
    it('prepends https to a git@ string (does NOT treat it as a scheme)', () => {
      expect(normalizeUrl('[email protected]:owner/repo.git', { allowGit: false }))
        .toBe('https://[email protected]:owner/repo.git');
    });

    it('still leaves http/https untouched', () => {
      expect(normalizeUrl('http://feed.example.com/rss', { allowGit: false }))
        .toBe('http://feed.example.com/rss');
    });

    it('prepends https to a bare feed host with no dot requirement', () => {
      expect(normalizeUrl('feedhost', { allowGit: false })).toBe('https://feedhost');
    });
  });

  // --- requireDot option (LinksTab quick-add guard) ---
  describe('requireDot: true (LinksTab)', () => {
    it('prepends https when the value contains a dot', () => {
      expect(normalizeUrl('example.com', { allowGit: true, requireDot: true }))
        .toBe('https://example.com');
    });

    it('prepends https when the value contains github.com (even without a dot token match)', () => {
      expect(normalizeUrl('github.com', { allowGit: true, requireDot: true }))
        .toBe('https://github.com');
    });

    it('returns null for a bare word with no dot', () => {
      expect(normalizeUrl('notaurl', { allowGit: true, requireDot: true })).toBe(null);
    });

    it('still treats git@ as already-normalized under requireDot', () => {
      expect(normalizeUrl('[email protected]:owner/repo.git', { allowGit: true, requireDot: true }))
        .toBe('[email protected]:owner/repo.git');
    });
  });

  describe('parity with original LinksTab normalizeUrl', () => {
    const linksTabNormalize = (raw) => normalizeUrl(raw, { allowGit: true, requireDot: true });
    it('matches: empty -> null', () => expect(linksTabNormalize('  ')).toBe(null));
    it('matches: bare word -> null', () => expect(linksTabNormalize('hello')).toBe(null));
    it('matches: domain -> https', () => expect(linksTabNormalize('foo.bar')).toBe('https://foo.bar'));
    it('matches: git@ -> unchanged', () =>
      expect(linksTabNormalize('git@host:repo')).toBe('git@host:repo'));
  });
});
// @vitest-environment node