import { describe, expect, it } from 'vitest';

import { resolveLoginRedirectPath } from '#/utils/login-redirect';

describe('login redirect', () => {
  it('restores an encoded knowledge share route', () => {
    expect(
      resolveLoginRedirectPath(
        encodeURIComponent('/share/knowledge?shareKey=knowledge-key'),
      ),
    ).toBe('/share/knowledge?shareKey=knowledge-key');
  });

  it('accepts an unencoded workflow share route', () => {
    expect(
      resolveLoginRedirectPath('/share/workflow?shareKey=workflow-key'),
    ).toBe('/share/workflow?shareKey=workflow-key');
  });

  it('uses the first valid query value', () => {
    expect(
      resolveLoginRedirectPath([
        undefined,
        encodeURIComponent('/share/knowledge?shareKey=knowledge-key'),
      ]),
    ).toBe('/share/knowledge?shareKey=knowledge-key');
  });

  it('rejects external and malformed redirects', () => {
    expect(resolveLoginRedirectPath('https://example.test')).toBeNull();
    expect(resolveLoginRedirectPath('//example.test/path')).toBeNull();
    expect(resolveLoginRedirectPath('%E0%A4%A')).toBeNull();
  });
});
