刘光辉
5 小时以前 cf11e63b659afcc9792ab3e395f1b45d844431a8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* @vitest-environment happy-dom */
 
import { afterEach, describe, expect, it, vi } from 'vitest';
 
import { getDocumentServerUrl } from '../loadDocsApi';
 
const RUNTIME_KEY = '_JNPF_ADMIN_PRO_APP_CONF_';
 
function setRuntimeConfig(value: Record<string, string> | undefined) {
  if (value === undefined) delete (window as any)[RUNTIME_KEY];
  else (window as any)[RUNTIME_KEY] = value;
}
 
describe('getDocumentServerUrl', () => {
  afterEach(() => {
    setRuntimeConfig(undefined);
    vi.unstubAllEnvs();
  });
 
  it('优先使用运行时 _app.config.js 注入的地址(由部署环境 .env 决定)', () => {
    vi.stubEnv('VITE_GLOB_ONLYOFFICE_URL', 'http://build-time:20000');
    setRuntimeConfig({ VITE_GLOB_ONLYOFFICE_URL: 'http://document-server.example:20000' });
    expect(getDocumentServerUrl()).toBe('http://document-server.example:20000');
  });
 
  it('运行时为空时回落到构建期变量(本地开发 .env.development)', () => {
    vi.stubEnv('VITE_GLOB_ONLYOFFICE_URL', 'http://localhost:20000');
    setRuntimeConfig({ VITE_GLOB_ONLYOFFICE_URL: '' });
    expect(getDocumentServerUrl()).toBe('http://localhost:20000');
  });
 
  it('没有运行时配置对象时回落到构建期变量', () => {
    vi.stubEnv('VITE_GLOB_ONLYOFFICE_URL', 'http://localhost:20000');
    expect(getDocumentServerUrl()).toBe('http://localhost:20000');
  });
 
  it('两处都没配置时返回空串,调用方据此提示未配置', () => {
    vi.stubEnv('VITE_GLOB_ONLYOFFICE_URL', '');
    expect(getDocumentServerUrl()).toBe('');
  });
 
  it('去掉末尾斜杠', () => {
    setRuntimeConfig({ VITE_GLOB_ONLYOFFICE_URL: 'http://document-server.example:20000/' });
    expect(getDocumentServerUrl()).toBe('http://document-server.example:20000');
  });
});