index.test.ts 3.07 KB
import { describe, expect, it } from 'vitest';

import {
  DEFAULT_SOP_PREVIEW_ALLOWED_ORIGINS,
  DEFAULT_PRELEARNING_ALLOWED_ORIGINS,
  buildBackendApiPath,
  resolveBackendApiBasePath,
  parseSopPreviewAllowedOrigins,
  parsePrelearningAllowedOrigins,
} from './index';

describe('parseSopPreviewAllowedOrigins', () => {
  it('falls back to the default allowlist when the env value is empty', () => {
    expect(parseSopPreviewAllowedOrigins(undefined)).toEqual([...DEFAULT_SOP_PREVIEW_ALLOWED_ORIGINS]);
    expect(parseSopPreviewAllowedOrigins('')).toEqual([...DEFAULT_SOP_PREVIEW_ALLOWED_ORIGINS]);
    expect(parseSopPreviewAllowedOrigins('   ')).toEqual([...DEFAULT_SOP_PREVIEW_ALLOWED_ORIGINS]);
  });

  it('accepts explicit wildcard and normalizes origin strings', () => {
    expect(parseSopPreviewAllowedOrigins('*')).toBe('*');
    expect(parseSopPreviewAllowedOrigins('https://dj.glsx.com.cn, https://frontend.test.glsx.com.cn/path, http://192.168.1.146:8080')).toEqual([
      'https://dj.glsx.com.cn',
      'https://frontend.test.glsx.com.cn',
      'http://192.168.1.146:8080',
    ]);
  });
});

describe('parsePrelearningAllowedOrigins', () => {
  it('falls back to the default allowlist when the env value is empty', () => {
    expect(parsePrelearningAllowedOrigins(undefined)).toEqual([...DEFAULT_PRELEARNING_ALLOWED_ORIGINS]);
    expect(parsePrelearningAllowedOrigins('')).toEqual([...DEFAULT_PRELEARNING_ALLOWED_ORIGINS]);
    expect(parsePrelearningAllowedOrigins('   ')).toEqual([...DEFAULT_PRELEARNING_ALLOWED_ORIGINS]);
  });

  it('accepts explicit wildcard and normalizes origin strings', () => {
    expect(parsePrelearningAllowedOrigins('*')).toBe('*');
    expect(parsePrelearningAllowedOrigins('https://dj.glsx.com.cn, https://frontend.test.glsx.com.cn/path, http://192.168.1.146:8080')).toEqual([
      'https://dj.glsx.com.cn',
      'https://frontend.test.glsx.com.cn',
      'http://192.168.1.146:8080',
    ]);
  });
});

describe('resolveBackendApiBasePath', () => {
  it('does not add a backend prefix for local runtime hosts', () => {
    expect(resolveBackendApiBasePath({ hostname: 'localhost' })).toBe('');
    expect(resolveBackendApiBasePath({ hostname: '127.0.0.1' })).toBe('');
    expect(resolveBackendApiBasePath({ hostname: '192.168.1.146' })).toBe('');
    expect(resolveBackendApiBasePath({ hostname: '10.0.0.8' })).toBe('');
    expect(resolveBackendApiBasePath({ hostname: '172.16.1.8' })).toBe('');
  });

  it('adds the backend prefix for remote deployed front-end hosts', () => {
    expect(resolveBackendApiBasePath({ hostname: 'single-pages.test.glsx.com.cn' })).toBe('/backend');
    expect(buildBackendApiPath('/api/prelearning/init', { hostname: 'single-pages.test.glsx.com.cn' })).toBe(
      '/backend/api/prelearning/init',
    );
  });

  it('allows an explicit env base path override', () => {
    expect(resolveBackendApiBasePath({ hostname: 'single-pages.test.glsx.com.cn', envBasePath: '' })).toBe('');
    expect(buildBackendApiPath('api/prelearning/init', { envBasePath: '/custom-backend/' })).toBe(
      '/custom-backend/api/prelearning/init',
    );
  });
});