import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; /** * 主系统对接模块的**两条契约**(开发者 2026-10-11 要求): * ① **域名单一来源**:所有端点 URL 都由 `MAIN_SYSTEM_BASE_URL` 派生, * 换域名/换路径只改 env(或 `MAIN_SYSTEM_*_PATH`),业务代码里没有第二份地址; * ② **默认关**:未设 `MAIN_SYSTEM_PRICE_QTY_ENABLED=1` 时,跨系统改价/改量整条链路关闭 * ⇒ 正常下单/改明细路径零影响(与改造前逐字节一致)。 */ const saved = { baseUrl: process.env.MAIN_SYSTEM_BASE_URL, priceQty: process.env.MAIN_SYSTEM_PRICE_QTY_ENABLED, requestPath: process.env.MAIN_SYSTEM_PRICE_QTY_REQUEST_PATH, }; function restoreEnv() { for (const [key, value] of Object.entries({ MAIN_SYSTEM_BASE_URL: saved.baseUrl, MAIN_SYSTEM_PRICE_QTY_ENABLED: saved.priceQty, MAIN_SYSTEM_PRICE_QTY_REQUEST_PATH: saved.requestPath, })) { if (value === undefined) delete process.env[key]; else process.env[key] = value; } } beforeEach(() => { vi.resetModules(); }); afterEach(() => { restoreEnv(); vi.resetModules(); }); describe('主系统域名 / 端点:单一来源', () => { it('改 MAIN_SYSTEM_BASE_URL 即改所有端点(含尾斜杠归一)', async () => { process.env.MAIN_SYSTEM_BASE_URL = 'https://main.example.com/erp/'; vi.resetModules(); const { buildMainSystemUrl } = await import('../src/services/mainSystem/endpoints'); expect(buildMainSystemUrl('preparations')).toBe('https://main.example.com/erp/api/supplier/preparations'); expect(buildMainSystemUrl('preparationsStatus')).toBe( 'https://main.example.com/erp/api/supplier/preparations/status' ); expect(buildMainSystemUrl('priceQtyRequest')).toBe( 'https://main.example.com/erp/api/supplier/preparations/price-qty-request' ); expect(buildMainSystemUrl('preparations', { view: 'price_amend' })).toBe( 'https://main.example.com/erp/api/supplier/preparations?view=price_amend' ); }); it('端点路径可被 env 覆盖(主系统改路径时不用改代码)', async () => { process.env.MAIN_SYSTEM_BASE_URL = 'http://127.0.0.1:3001'; process.env.MAIN_SYSTEM_PRICE_QTY_REQUEST_PATH = '/api/supplier/v2/price-qty-request'; vi.resetModules(); const { buildMainSystemUrl } = await import('../src/services/mainSystem/endpoints'); expect(buildMainSystemUrl('priceQtyRequest')).toBe( 'http://127.0.0.1:3001/api/supplier/v2/price-qty-request' ); }); it('域名为空时抛错而不是拼出奇怪地址', async () => { // 注意:`src/config.ts` 在 import 时会 `loadEnv()` 回读 `server/.env`(且不覆盖已存在的 env), // 所以这里用**空串**表达"未配置",而不是 delete(delete 会被回读补回来)。 process.env.MAIN_SYSTEM_BASE_URL = ''; vi.resetModules(); const { buildMainSystemUrl } = await import('../src/services/mainSystem/endpoints'); expect(() => buildMainSystemUrl('preparations')).toThrow(/MAIN_SYSTEM_BASE_URL/); }); }); describe('跨系统改价/改量:开关判定(`=1` 才开,其余一律关)', () => { // ⚠️ 不能用 delete 表达"未配置":`src/config.ts` 在 import 时会 `loadEnv()` 回读 `server/.env` // (且不覆盖已存在的 env)⇒ delete 会被回读补回来。用**空串/其它值**表达"关", // 这样本地 `.env` 里联调把开关置 1 时,本用例仍然稳定。 it('空串 / 0 / true 都当关;只有 =1 才算开', async () => { for (const value of ['', '0', 'true', 'yes']) { process.env.MAIN_SYSTEM_PRICE_QTY_ENABLED = value; vi.resetModules(); const config = await import('../src/services/mainSystem/config'); expect(config.isPriceQtyRequestEnabled(), `值 "${value}" 应为关`).toBe(false); } process.env.MAIN_SYSTEM_PRICE_QTY_ENABLED = '1'; vi.resetModules(); const on = await import('../src/services/mainSystem/config'); expect(on.isPriceQtyRequestEnabled()).toBe(true); }); });