import { describe, expect, it } from 'vitest';
import { encodeWtKey, decodeWtKey } from '@arboretum/shared';
import { normalizeBaseUrl, workspaceUrl, wsUrlFromBase } from '../src/config.js';

describe('config', () => {
  it('normalise la base URL (slash final, défaut)', () => {
    expect(normalizeBaseUrl('http://127.0.0.1:7317/')).toBe('http://127.0.0.1:7317');
    expect(normalizeBaseUrl('  http://host:8080//  ')).toBe('http://host:8080');
    expect(normalizeBaseUrl('')).toBe('http://127.0.0.1:7317');
  });

  it('dérive l’URL WebSocket (/ws, http→ws, https→wss)', () => {
    expect(wsUrlFromBase('http://127.0.0.1:7317')).toBe('ws://127.0.0.1:7317/ws');
    expect(wsUrlFromBase('https://box.ts.net')).toBe('wss://box.ts.net/ws');
  });

  it('construit l’URL de la vue IDE web (workspaceUrl : repoId par segment, worktree en base64url)', () => {
    const url = workspaceUrl('http://127.0.0.1:7317', 'repo id', '/home/user/wt');
    expect(url).toBe(`http://127.0.0.1:7317/workspace/repo%20id/${encodeWtKey('/home/user/wt')}`);
    // le segment worktree est décodable par la source partagée (cohérence avec le front).
    const seg = url.split('/').pop() as string;
    expect(decodeWtKey(seg)).toBe('/home/user/wt');
    expect(seg).not.toMatch(/[%+/=]/);
  });
});
